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
648 lines (586 loc) · 15.6 KB
/
Copy pathapi.ts
File metadata and controls
648 lines (586 loc) · 15.6 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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
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 { getGroups as getUserGroups } from '@/features/users/api'
import { api, type ApiRequestConfig } from '@/lib/api'
import type {
AddChannelRequest,
BatchDeleteParams,
BatchSetTagParams,
Channel,
ChannelBalanceResponse,
ChannelOpsResponse,
ChannelTestResponse,
CopyChannelParams,
CopyChannelResponse,
FetchModelsResponse,
GetChannelResponse,
GetChannelsParams,
GetChannelsResponse,
MultiKeyManageParams,
MultiKeyStatusResponse,
SearchChannelsParams,
SearchChannelsResponse,
TagOperationParams,
} from './types'
const channelActionConfig = (
config: ApiRequestConfig = {}
): ApiRequestConfig => ({
...config,
skipBusinessError: true,
skipErrorHandler: true,
})
export type CodexUsageResponse = {
success: boolean
message?: string
upstream_status?: number
data?: Record<string, unknown>
}
export type CodexResetCreditsResponse = CodexUsageResponse
export type CodexUsageResetResponse = CodexUsageResponse
export type CodexCredentialRefreshResponse = {
success: boolean
message?: string
data?: {
expires_at?: string
last_refresh?: string
account_id?: string
email?: string
channel_id?: number
channel_type?: number
channel_name?: string
}
}
// ============================================================================
// Base Channel CRUD Operations
// ============================================================================
/**
* Get paginated list of channels
*/
export async function getChannels(
params: GetChannelsParams = {}
): Promise<GetChannelsResponse> {
const res = await api.get('/api/channel', { params })
return res.data
}
/**
* Search channels with filters
*/
export async function searchChannels(
params: SearchChannelsParams
): Promise<SearchChannelsResponse> {
const res = await api.get('/api/channel/search', { params })
return res.data
}
/**
* Get single channel by ID
*/
export async function getChannel(id: number): Promise<GetChannelResponse> {
const res = await api.get(`/api/channel/${id}`)
return res.data
}
/**
* Get channel operations summary for administrators
*/
export async function getChannelOps(): Promise<ChannelOpsResponse> {
const res = await api.get('/api/channel/ops', channelActionConfig())
return res.data
}
/**
* Create new channel(s)
* Supports single, batch, and multi-key modes
*/
export async function createChannel(
data: AddChannelRequest
): Promise<{ success: boolean; message?: string }> {
const res = await api.post('/api/channel', data, channelActionConfig())
return res.data
}
/**
* Update existing channel
*/
export async function updateChannel(
id: number,
data: Partial<Channel>
): Promise<{ success: boolean; message?: string; data?: Channel }> {
const res = await api.put(
'/api/channel/',
{ id, ...data },
channelActionConfig()
)
return res.data
}
/**
* Update channel enabled/disabled status.
*/
export async function updateChannelStatus(
id: number,
status: number
): Promise<{ success: boolean; message?: string; data?: boolean }> {
const res = await api.post(
`/api/channel/${id}/status`,
{ status },
channelActionConfig()
)
return res.data
}
/**
* Batch update channel enabled/disabled status.
*/
export async function batchUpdateChannelStatus(
ids: number[],
status: number
): Promise<{ success: boolean; message?: string; data?: number }> {
const res = await api.post(
'/api/channel/status/batch',
{ ids, status },
channelActionConfig()
)
return res.data
}
/**
* Delete single channel
*/
export async function deleteChannel(
id: number
): Promise<{ success: boolean; message?: string }> {
const res = await api.delete(`/api/channel/${id}`, channelActionConfig())
return res.data
}
/**
* Batch delete channels
*/
export async function batchDeleteChannels(
data: BatchDeleteParams
): Promise<{ success: boolean; message?: string; data?: number }> {
const res = await api.post('/api/channel/batch', data, channelActionConfig())
return res.data
}
/**
* Batch set tag for channels
*/
export async function batchSetChannelTag(
data: BatchSetTagParams
): Promise<{ success: boolean; message?: string; data?: number }> {
const res = await api.post(
'/api/channel/batch/tag',
data,
channelActionConfig()
)
return res.data
}
// ============================================================================
// Channel Operations
// ============================================================================
/**
* Test channel connectivity
*/
export async function testChannel(
id: number,
params?: { model?: string; endpoint_type?: string; stream?: boolean }
): Promise<ChannelTestResponse> {
const res = await api.get(
`/api/channel/test/${id}`,
channelActionConfig({ params })
)
return res.data
}
/**
* Update channel balance
*/
export async function updateChannelBalance(
id: number
): Promise<ChannelBalanceResponse> {
const res = await api.get(
`/api/channel/update_balance/${id}`,
channelActionConfig()
)
return res.data
}
/**
* Fetch available models from upstream provider
*/
export async function fetchUpstreamModels(
id: number
): Promise<FetchModelsResponse> {
const res = await api.get(
`/api/channel/fetch_models/${id}`,
channelActionConfig()
)
return res.data
}
/**
* Copy/clone a channel
*/
export async function copyChannel(
id: number,
params: CopyChannelParams = {}
): Promise<CopyChannelResponse> {
const res = await api.post(
`/api/channel/copy/${id}`,
null,
channelActionConfig({ params })
)
return res.data
}
/**
* Fix channel abilities
*/
export async function fixChannelAbilities(): Promise<{
success: boolean
message?: string
data?: { success: number; fails: number }
}> {
const res = await api.post(
'/api/channel/fix',
undefined,
channelActionConfig()
)
return res.data
}
/**
* Delete all disabled channels
*/
export async function deleteDisabledChannels(): Promise<{
success: boolean
message?: string
data?: number
}> {
const res = await api.delete('/api/channel/disabled', channelActionConfig())
return res.data
}
/**
* Get channel key (requires 2FA verification)
*/
export async function getChannelKey(
id: number,
proofToken?: string
): Promise<{ success: boolean; message?: string; data?: { key: string } }> {
const res = await api.post(
`/api/channel/${id}/key`,
undefined,
channelActionConfig({
headers: proofToken ? { 'X-Security-Proof': proofToken } : undefined,
})
)
return res.data
}
// ============================================================================
// Codex Channel Operations
// ============================================================================
export async function refreshCodexCredential(
channelId: number
): Promise<CodexCredentialRefreshResponse> {
const res = await api.post(
`/api/channel/${channelId}/codex/refresh`,
{},
channelActionConfig()
)
return res.data
}
export async function getCodexUsage(
channelId: number
): Promise<CodexUsageResponse> {
const res = await api.get(
`/api/channel/${channelId}/codex/usage`,
channelActionConfig({ disableDuplicate: true })
)
return res.data
}
export async function getCodexResetCredits(
channelId: number
): Promise<CodexResetCreditsResponse> {
const res = await api.get(
`/api/channel/${channelId}/codex/usage/reset-credits`,
channelActionConfig({ disableDuplicate: true })
)
return res.data
}
export async function resetCodexUsage(
channelId: number
): Promise<CodexUsageResetResponse> {
const res = await api.post(
`/api/channel/${channelId}/codex/usage/reset`,
{},
channelActionConfig({ disableDuplicate: true })
)
return res.data
}
// ============================================================================
// Multi-Key Management
// ============================================================================
/**
* Manage multi-key channel operations
*/
export async function manageMultiKeys(
params: MultiKeyManageParams
): Promise<MultiKeyStatusResponse | { success: boolean; message?: string }> {
const res = await api.post(
'/api/channel/multi_key/manage',
params,
channelActionConfig()
)
return res.data
}
/**
* Get key status for multi-key channel
*/
export async function getMultiKeyStatus(
channelId: number,
page = 1,
pageSize = 50,
status?: number
): Promise<MultiKeyStatusResponse> {
return manageMultiKeys({
channel_id: channelId,
action: 'get_key_status',
page,
page_size: pageSize,
status,
}) as Promise<MultiKeyStatusResponse>
}
/**
* Enable a specific key in multi-key channel
*/
export async function enableMultiKey(
channelId: number,
keyIndex: number
): Promise<{ success: boolean; message?: string }> {
return manageMultiKeys({
channel_id: channelId,
action: 'enable_key',
key_index: keyIndex,
}) as Promise<{ success: boolean; message?: string }>
}
/**
* Disable a specific key in multi-key channel
*/
export async function disableMultiKey(
channelId: number,
keyIndex: number
): Promise<{ success: boolean; message?: string }> {
return manageMultiKeys({
channel_id: channelId,
action: 'disable_key',
key_index: keyIndex,
}) as Promise<{ success: boolean; message?: string }>
}
/**
* Delete a specific key in multi-key channel
*/
export async function deleteMultiKey(
channelId: number,
keyIndex: number
): Promise<{ success: boolean; message?: string }> {
return manageMultiKeys({
channel_id: channelId,
action: 'delete_key',
key_index: keyIndex,
}) as Promise<{ success: boolean; message?: string }>
}
/**
* Enable all keys in multi-key channel
*/
export async function enableAllMultiKeys(
channelId: number
): Promise<{ success: boolean; message?: string }> {
return manageMultiKeys({
channel_id: channelId,
action: 'enable_all_keys',
}) as Promise<{ success: boolean; message?: string }>
}
/**
* Disable all keys in multi-key channel
*/
export async function disableAllMultiKeys(
channelId: number
): Promise<{ success: boolean; message?: string }> {
return manageMultiKeys({
channel_id: channelId,
action: 'disable_all_keys',
}) as Promise<{ success: boolean; message?: string }>
}
/**
* Delete all disabled keys in multi-key channel
*/
export async function deleteDisabledMultiKeys(
channelId: number
): Promise<{ success: boolean; message?: string; data?: number }> {
return manageMultiKeys({
channel_id: channelId,
action: 'delete_disabled_keys',
}) as Promise<{ success: boolean; message?: string; data?: number }>
}
// ============================================================================
// Tag Operations
// ============================================================================
/**
* Enable all channels with a specific tag
*/
export async function enableTagChannels(
tag: string
): Promise<{ success: boolean; message?: string }> {
const res = await api.post(
'/api/channel/tag/enabled',
{ tag },
channelActionConfig()
)
return res.data
}
/**
* Disable all channels with a specific tag
*/
export async function disableTagChannels(
tag: string
): Promise<{ success: boolean; message?: string }> {
const res = await api.post(
'/api/channel/tag/disabled',
{ tag },
channelActionConfig()
)
return res.data
}
/**
* Edit all channels with a specific tag
*/
export async function editTagChannels(
params: TagOperationParams
): Promise<{ success: boolean; message?: string }> {
const res = await api.put('/api/channel/tag', params, channelActionConfig())
return res.data
}
/**
* Get models for a specific tag
*/
export async function getTagModels(
tag: string
): Promise<{ success: boolean; message?: string; data?: string }> {
const res = await api.get('/api/channel/tag/models', { params: { tag } })
return res.data
}
// ============================================================================
// Utility Functions
// ============================================================================
/**
* Fetch models from the current unsaved channel form configuration.
*/
export async function fetchModels(data: {
base_url: string
type: number
key?: string
channel_id?: number
advanced_custom?: string
header_override?: string
proxy?: string
}): Promise<FetchModelsResponse> {
const res = await api.post(
'/api/channel/fetch_models',
data,
channelActionConfig()
)
return res.data
}
/**
* Delete an Ollama model from a channel
*/
export async function deleteOllamaModel(params: {
channel_id: number
model_name: string
}): Promise<{ success: boolean; message?: string }> {
const res = await api.delete(
'/api/channel/ollama/delete',
channelActionConfig({ data: params })
)
return res.data
}
/**
* Test all enabled channels
*/
export async function testAllChannels(): Promise<{
success: boolean
message?: string
}> {
const res = await api.get('/api/channel/test', channelActionConfig())
return res.data
}
/**
* Update balance for all enabled channels
*/
export async function updateAllChannelsBalance(): Promise<{
success: boolean
message?: string
}> {
const res = await api.get(
'/api/channel/update_balance',
channelActionConfig()
)
return res.data
}
/**
* Get all available models
*/
export async function getAllModels(): Promise<{
success: boolean
message?: string
data?: Array<{ id: string; [key: string]: unknown }>
}> {
const res = await api.get('/api/channel/models')
return res.data
}
/**
* Get all enabled models
*/
export async function getEnabledModels(): Promise<{
success: boolean
message?: string
data?: string[]
}> {
const res = await api.get('/api/channel/models_enabled')
return res.data
}
// ============================================================================
// Ollama Utilities
// ============================================================================
/**
* Check Ollama version for a given channel
*/
export async function getOllamaVersion(
channelId: number
): Promise<{ success: boolean; message?: string; data?: { version: string } }> {
const res = await api.get(`/api/channel/ollama/version/${channelId}`)
return res.data
}
// ============================================================================
// Group Management
// ============================================================================
/**
* Get all available groups (re-exported from users API for convenience)
*/
export const getGroups = getUserGroups
// ============================================================================
// Prefill Groups (Model Groups)
// ============================================================================
/**
* Get prefill groups for quick model selection
*/
export async function getPrefillGroups(
type: 'model' | 'group' = 'model'
): Promise<{
success: boolean
message?: string
data?: Array<{ id: number; name: string; items: string | string[] }>
}> {
const res = await api.get('/api/prefill_group', { params: { type } })
return res.data
}