forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
633 lines (581 loc) · 15 KB
/
Copy pathapi.ts
File metadata and controls
633 lines (581 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { api } from '@/lib/api'
import type {
GetModelsParams,
GetModelsResponse,
GetModelResponse,
GetVendorsResponse,
GetVendorResponse,
Model,
Vendor,
SearchModelsParams,
SyncUpstreamResponse,
PreviewUpstreamDiffResponse,
MissingModelsResponse,
PrefillGroupsResponse,
SyncLocale,
SyncSource,
SyncOverwritePayload,
DeploymentSettingsResponse,
ListDeploymentsResponse,
} from './types'
// ============================================================================
// Model CRUD Operations
// ============================================================================
/**
* Get paginated list of models
*/
export async function getModels(
params: GetModelsParams = {}
): Promise<GetModelsResponse> {
const res = await api.get('/api/models/', { params })
return res.data
}
/**
* Search models with filters
*/
export async function searchModels(
params: SearchModelsParams
): Promise<GetModelsResponse> {
const res = await api.get('/api/models/search', { params })
return res.data
}
/**
* Get single model by ID
*/
export async function getModel(id: number): Promise<GetModelResponse> {
const res = await api.get(`/api/models/${id}`)
return res.data
}
/**
* Create new model
*/
export async function createModel(
data: Partial<Model>
): Promise<{ success: boolean; message?: string; data?: Model }> {
const res = await api.post('/api/models/', data)
return res.data
}
/**
* Update existing model
*/
export async function updateModel(
data: Partial<Model> & { id: number }
): Promise<{ success: boolean; message?: string; data?: Model }> {
const res = await api.put('/api/models/', data)
return res.data
}
/**
* Update model status only
*/
export async function updateModelStatus(
id: number,
status: number
): Promise<{ success: boolean; message?: string }> {
const res = await api.put('/api/models/?status_only=true', { id, status })
return res.data
}
/**
* Delete model
*/
export async function deleteModel(
id: number
): Promise<{ success: boolean; message?: string }> {
const res = await api.delete(`/api/models/${id}`)
return res.data
}
// ============================================================================
// Vendor Management
// ============================================================================
/**
* Get paginated list of vendors
*/
export async function getVendors(params?: {
p?: number
page_size?: number
}): Promise<GetVendorsResponse> {
const res = await api.get('/api/vendors/', {
params: params || { page_size: 1000 },
})
return res.data
}
/**
* Search vendors
*/
export async function searchVendors(params: {
keyword?: string
p?: number
page_size?: number
}): Promise<GetVendorsResponse> {
const res = await api.get('/api/vendors/search', { params })
return res.data
}
/**
* Get single vendor by ID
*/
export async function getVendor(id: number): Promise<GetVendorResponse> {
const res = await api.get(`/api/vendors/${id}`)
return res.data
}
/**
* Create new vendor
*/
export async function createVendor(
data: Partial<Vendor>
): Promise<{ success: boolean; message?: string; data?: Vendor }> {
const res = await api.post('/api/vendors/', data)
return res.data
}
/**
* Update existing vendor
*/
export async function updateVendor(
data: Partial<Vendor> & { id: number }
): Promise<{ success: boolean; message?: string; data?: Vendor }> {
const res = await api.put('/api/vendors/', data)
return res.data
}
/**
* Delete vendor
*/
export async function deleteVendor(
id: number
): Promise<{ success: boolean; message?: string }> {
const res = await api.delete(`/api/vendors/${id}`)
return res.data
}
// ============================================================================
// Sync Operations
// ============================================================================
/**
* Sync upstream models (missing only or with overwrite)
*/
export async function syncUpstream(params?: {
locale?: SyncLocale
source?: SyncSource
overwrite?: SyncOverwritePayload[]
}): Promise<SyncUpstreamResponse> {
const res = await api.post('/api/models/sync_upstream', params)
return res.data
}
/**
* Preview upstream diff
*/
export async function previewUpstreamDiff(params?: {
locale?: SyncLocale
source?: SyncSource
}): Promise<PreviewUpstreamDiffResponse> {
const searchParams = new URLSearchParams()
if (params?.locale) {
searchParams.set('locale', params.locale)
}
if (params?.source) {
searchParams.set('source', params.source)
}
const queryString = searchParams.toString()
const url = queryString
? `/api/models/sync_upstream/preview?${queryString}`
: '/api/models/sync_upstream/preview'
const res = await api.get(url)
return res.data
}
/**
* Apply upstream overwrite
*/
export async function applyUpstreamOverwrite(params: {
overwrite: SyncOverwritePayload[]
locale?: SyncLocale
source?: SyncSource
}): Promise<SyncUpstreamResponse> {
return syncUpstream(params)
}
// ============================================================================
// Utility Operations
// ============================================================================
/**
* Get missing models (used but not configured)
*/
export async function getMissingModels(): Promise<MissingModelsResponse> {
const res = await api.get('/api/models/missing')
return res.data
}
/**
* Get prefill groups
*/
export async function getPrefillGroups(
type?: 'model' | 'tag' | 'endpoint'
): Promise<PrefillGroupsResponse> {
const res = await api.get('/api/prefill_group', {
params: type ? { type } : undefined,
})
return res.data
}
/**
* Create prefill group
*/
export async function createPrefillGroup(data: {
name: string
type: 'model' | 'tag' | 'endpoint'
items: string | string[]
description?: string
}): Promise<{ success: boolean; message?: string }> {
const res = await api.post('/api/prefill_group', data)
return res.data
}
/**
* Update prefill group
*/
export async function updatePrefillGroup(data: {
id: number
type?: 'model' | 'tag' | 'endpoint'
name?: string
items?: string | string[]
description?: string
}): Promise<{ success: boolean; message?: string }> {
const res = await api.put('/api/prefill_group', data)
return res.data
}
/**
* Delete prefill group
*/
export async function deletePrefillGroup(
id: number
): Promise<{ success: boolean; message?: string }> {
const res = await api.delete(`/api/prefill_group/${id}`)
return res.data
}
// ============================================================================
// Deployment Operations
// ============================================================================
/**
* Get deployment settings (io.net config)
*/
export async function getDeploymentSettings(): Promise<DeploymentSettingsResponse> {
const res = await api.get('/api/deployments/settings')
return res.data
}
/**
* Test deployment connection
*/
export async function testDeploymentConnection(): Promise<{
success: boolean
message?: string
}> {
const config = { skipErrorHandler: true } as unknown as Parameters<
typeof api.post
>[2]
const res = await api.post(
'/api/deployments/settings/test-connection',
{},
config
)
return res.data
}
/**
* Test deployment connection with optional api_key (allow test before saving)
*/
export async function testDeploymentConnectionWithKey(
apiKey?: string
): Promise<{
success: boolean
message?: string
}> {
const payload =
typeof apiKey === 'string' && apiKey.trim()
? { api_key: apiKey.trim() }
: {}
const config = { skipErrorHandler: true } as unknown as Parameters<
typeof api.post
>[2]
const res = await api.post(
'/api/deployments/settings/test-connection',
payload,
config
)
return res.data
}
/**
* List deployments
*/
export async function listDeployments(params: {
p?: number
page_size?: number
status?: string
}): Promise<ListDeploymentsResponse> {
const res = await api.get('/api/deployments/', { params })
return res.data
}
/**
* Search deployments (keyword + status)
*
* Backend exposes a dedicated `/api/deployments/search` route that supports
* filtering by keyword (and status). Use this when keyword is provided.
*/
export async function searchDeployments(params: {
p?: number
page_size?: number
status?: string
keyword?: string
}): Promise<ListDeploymentsResponse> {
const res = await api.get('/api/deployments/search', { params })
return res.data
}
/**
* Get deployment details
*/
export async function getDeployment(id: string | number): Promise<{
success: boolean
message?: string
data?: Record<string, unknown>
}> {
const res = await api.get(`/api/deployments/${id}`)
return res.data
}
/**
* List deployment containers
*/
export async function listDeploymentContainers(
deploymentId: string | number
): Promise<{
success: boolean
message?: string
data?: {
total?: number
containers?: Array<Record<string, unknown>>
}
}> {
const res = await api.get(`/api/deployments/${deploymentId}/containers`)
return res.data
}
/**
* Get single container details
*/
export async function getDeploymentContainerDetails(
deploymentId: string | number,
containerId: string
): Promise<{
success: boolean
message?: string
data?: Record<string, unknown>
}> {
const res = await api.get(
`/api/deployments/${deploymentId}/containers/${encodeURIComponent(containerId)}`
)
return res.data
}
/**
* Delete deployment
*/
export async function deleteDeployment(
id: string | number
): Promise<{ success: boolean; message?: string }> {
const res = await api.delete(`/api/deployments/${id}`)
return res.data
}
/**
* Get deployment logs (raw)
*/
export async function getDeploymentLogs(
deploymentId: string | number,
params: {
container_id: string
stream?: 'stdout' | 'stderr' | 'all' | string
level?: string
cursor?: string
limit?: number
follow?: boolean
start_time?: string
end_time?: string
}
): Promise<{ success: boolean; message?: string; data?: string }> {
const res = await api.get(`/api/deployments/${deploymentId}/logs`, { params })
return res.data
}
/**
* Get hardware types for deployment
*/
export async function getHardwareTypes(): Promise<{
success: boolean
data?: { hardware_types?: Array<Record<string, unknown>> }
}> {
const res = await api.get('/api/deployments/hardware-types')
return res.data
}
/**
* Get locations for deployment
*/
export async function getDeploymentLocations(): Promise<{
success: boolean
message?: string
data?: { locations?: Array<Record<string, unknown>>; total?: number }
}> {
const res = await api.get('/api/deployments/locations')
return res.data
}
/**
* Get available replicas
*/
export async function getAvailableReplicas(params: {
hardware_id: string
gpu_count: number
}): Promise<{
success: boolean
data?: { replicas?: Array<Record<string, unknown>> }
}> {
const res = await api.get('/api/deployments/available-replicas', { params })
return res.data
}
/**
* Estimate deployment price
*/
export async function estimatePrice(params: {
location_ids: Array<string | number>
hardware_id: string | number
gpus_per_container: number
duration_hours: number
replica_count: number
currency?: string
}): Promise<{
success: boolean
message?: string
data?: Record<string, unknown>
}> {
const locationIds = (params.location_ids || [])
.map((x) => Number(x))
.filter((n) => Number.isInteger(n) && n > 0)
const hardwareId = Number(params.hardware_id)
const duration = Number(params.duration_hours)
const gpus = Number(params.gpus_per_container)
const replicaCount = Number(params.replica_count)
const currency =
typeof params.currency === 'string' && params.currency.trim()
? params.currency.trim().toLowerCase()
: 'usdc'
const payload = {
location_ids: locationIds,
hardware_id: hardwareId,
gpus_per_container: gpus,
duration_hours: duration,
replica_count: replicaCount,
currency,
duration_type: 'hour',
duration_qty: duration,
hardware_qty: gpus,
}
const res = await api.post('/api/deployments/price-estimation', payload)
return res.data
}
/**
* Create deployment
*/
export async function createDeployment(data: {
resource_private_name: string
duration_hours: number
gpus_per_container: number
hardware_id: number
location_ids: number[]
container_config: {
replica_count: number
env_variables?: Record<string, string>
secret_env_variables?: Record<string, string>
entrypoint?: string[]
traffic_port?: number
args?: string[]
}
registry_config: {
image_url: string
registry_username?: string
registry_secret?: string
}
}): Promise<{
success: boolean
message?: string
data?: Record<string, unknown>
}> {
const res = await api.post('/api/deployments/', data)
return res.data
}
/**
* Update deployment configuration
*/
export async function updateDeployment(
id: string | number,
data: {
env_variables?: Record<string, string>
secret_env_variables?: Record<string, string>
entrypoint?: string[]
traffic_port?: number | null
image_url?: string
registry_username?: string
registry_secret?: string
args?: string[]
command?: string
}
): Promise<{
success: boolean
message?: string
data?: Record<string, unknown>
}> {
const payload: Record<string, unknown> = { ...data }
if (data.traffic_port === null) {
delete payload.traffic_port
}
const res = await api.put(`/api/deployments/${id}`, payload)
return res.data
}
/**
* Update deployment name
*/
export async function updateDeploymentName(
id: string | number,
name: string
): Promise<{
success: boolean
message?: string
data?: Record<string, unknown>
}> {
const res = await api.put(`/api/deployments/${id}/name`, { name })
return res.data
}
/**
* Extend deployment duration
*/
export async function extendDeployment(
id: string | number,
durationHours: number
): Promise<{
success: boolean
message?: string
data?: Record<string, unknown>
}> {
const res = await api.post(`/api/deployments/${id}/extend`, {
duration_hours: durationHours,
})
return res.data
}
/**
* Check cluster name availability
*/
export async function checkClusterNameAvailability(name: string): Promise<{
success: boolean
message?: string
data?: { available?: boolean; name?: string }
}> {
const res = await api.get('/api/deployments/check-name', {
params: { name },
})
return res.data
}