-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-utils.js
More file actions
602 lines (570 loc) · 17.3 KB
/
api-utils.js
File metadata and controls
602 lines (570 loc) · 17.3 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
// shared API utility functions for VibeReader
// loaded by both background.js (importScripts) and popup.html (<script>)
// provider presets: local-first, then China cloud, then international, then routers
var API_PROVIDERS = {
// ── local ──────────────────────────────────────────────────
ollama: {
label: 'Ollama',
baseUrl: 'http://localhost:11434/v1',
apiFormat: 'openai',
requiresKey: false,
models: [
'llama3.2',
'qwen2.5:7b',
'deepseek-r1:8b',
'mistral',
'gemma2:9b',
'phi3:mini'
],
defaultModel: 'llama3.2',
hint: 'Runs entirely on your machine. Install from ollama.com and run "ollama serve".'
},
lmstudio: {
label: 'LM Studio',
baseUrl: 'http://localhost:1234/v1',
apiFormat: 'openai',
requiresKey: false,
models: [],
defaultModel: '',
hint: 'Local inference GUI. Download models inside LM Studio, then start the server.'
},
jan: {
label: 'Jan',
baseUrl: 'http://localhost:1337/v1',
apiFormat: 'openai',
requiresKey: false,
models: [],
defaultModel: '',
hint: 'Open-source local AI. Download from jan.ai, load a model, then start the server.'
},
gpt4all: {
label: 'GPT4All',
baseUrl: 'http://localhost:4891/v1',
apiFormat: 'openai',
requiresKey: false,
models: [],
defaultModel: '',
hint: 'Privacy-first local AI. Download from gpt4all.io, enable API server in settings.'
},
localai: {
label: 'LocalAI',
baseUrl: 'http://localhost:8080/v1',
apiFormat: 'openai',
requiresKey: false,
models: [],
defaultModel: '',
hint: 'Self-hosted OpenAI-compatible server. See localai.io for setup instructions.'
},
vllm: {
label: 'vLLM',
baseUrl: 'http://localhost:8000/v1',
apiFormat: 'openai',
requiresKey: false,
models: [],
defaultModel: '',
hint: 'High-throughput serving engine. See docs.vllm.ai for setup instructions.'
},
textgenwebui: {
label: 'text-generation-webui',
baseUrl: 'http://localhost:5000/v1',
apiFormat: 'openai',
requiresKey: false,
models: [],
defaultModel: '',
hint: 'Gradio web UI for LLMs. Enable --api flag. See github.com/oobabooga/text-generation-webui'
},
// ── china cloud ────────────────────────────────────────────
deepseek: {
label: 'DeepSeek',
baseUrl: 'https://api.deepseek.com/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'deepseek-chat',
'deepseek-reasoner'
],
defaultModel: 'deepseek-chat',
hint: 'Get your API key from platform.deepseek.com'
},
siliconflow: {
label: 'SiliconFlow',
baseUrl: 'https://api.siliconflow.cn/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'Qwen/Qwen2.5-72B-Instruct',
'deepseek-ai/DeepSeek-V3',
'deepseek-ai/DeepSeek-R1',
'Qwen/Qwen2.5-7B-Instruct',
'THUDM/glm-4-9b-chat'
],
defaultModel: 'Qwen/Qwen2.5-7B-Instruct',
hint: 'AI model aggregator. Get your key from siliconflow.cn'
},
moonshot: {
label: 'Moonshot / Kimi',
baseUrl: 'https://api.moonshot.cn/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'moonshot-v1-128k',
'moonshot-v1-32k',
'moonshot-v1-8k'
],
defaultModel: 'moonshot-v1-32k',
hint: 'Get your key from platform.moonshot.cn'
},
zhipu: {
label: 'Zhipu AI',
baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
apiFormat: 'openai',
requiresKey: true,
models: [
'glm-4-plus',
'glm-4-flash',
'glm-4-long',
'glm-4-air'
],
defaultModel: 'glm-4-flash',
hint: 'Get your key from open.bigmodel.cn'
},
dashscope: {
label: 'Alibaba Tongyi',
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'qwen-max',
'qwen-plus',
'qwen-turbo',
'qwen-long'
],
defaultModel: 'qwen-turbo',
hint: 'DashScope OpenAI-compatible mode. Get your key from dashscope.console.aliyun.com'
},
doubao: {
label: 'Doubao / Volcengine',
baseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
apiFormat: 'openai',
requiresKey: true,
models: [],
defaultModel: '',
hint: 'Use your Ark endpoint ID as the model name. Get your key from console.volcengine.com'
},
// ── international cloud ────────────────────────────────────
cohere: {
label: 'Cohere',
baseUrl: 'https://api.cohere.com/compatibility/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'command-r-plus',
'command-r',
'command-a-03-2025'
],
defaultModel: 'command-r',
hint: 'RAG-optimized models. Get your key from dashboard.cohere.com'
},
perplexity: {
label: 'Perplexity',
baseUrl: 'https://api.perplexity.ai',
apiFormat: 'openai',
requiresKey: true,
models: [
'sonar-pro',
'sonar',
'sonar-reasoning-pro',
'sonar-reasoning'
],
defaultModel: 'sonar',
hint: 'Search-augmented AI. Get your key from perplexity.ai/settings/api'
},
ai21: {
label: 'AI21 Labs',
baseUrl: 'https://api.ai21.com/studio/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'jamba-1.5-large',
'jamba-1.5-mini'
],
defaultModel: 'jamba-1.5-mini',
hint: 'Jamba model family. Get your key from studio.ai21.com'
},
fireworks: {
label: 'Fireworks AI',
baseUrl: 'https://api.fireworks.ai/inference/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'accounts/fireworks/models/firefunction-v2',
'accounts/fireworks/models/llama-v3p1-70b-instruct',
'accounts/fireworks/models/llama-v3p1-8b-instruct'
],
defaultModel: 'accounts/fireworks/models/llama-v3p1-8b-instruct',
hint: 'Fast open-model inference. Get your key from fireworks.ai/account/api-keys'
},
cerebras: {
label: 'Cerebras',
baseUrl: 'https://api.cerebras.ai/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'llama-3.3-70b',
'llama-4-scout-17b-16e'
],
defaultModel: 'llama-3.3-70b',
hint: 'Ultra-fast wafer-scale inference. Get your key from cloud.cerebras.ai'
},
lambda: {
label: 'Lambda',
baseUrl: 'https://api.lambdalabs.com/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'llama-3.3-70b-instruct'
],
defaultModel: 'llama-3.3-70b-instruct',
hint: 'GPU cloud + inference API. Get your key from cloud.lambda.ai'
},
replicate: {
label: 'Replicate',
baseUrl: 'https://openai-proxy.replicate.com/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'meta/llama-3-70b-instruct'
],
defaultModel: 'meta/llama-3-70b-instruct',
hint: 'Open-source model hosting. Get your key from replicate.com/account/api-tokens'
},
openai: {
label: 'OpenAI',
baseUrl: 'https://api.openai.com/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'gpt-4o',
'gpt-4o-mini',
'o1-mini',
'gpt-3.5-turbo'
],
defaultModel: 'gpt-4o-mini',
hint: 'Get your key from platform.openai.com'
},
anthropic: {
label: 'Anthropic Claude',
baseUrl: 'https://api.anthropic.com/v1',
apiFormat: 'anthropic',
requiresKey: true,
models: [
'claude-sonnet-4-20250514',
'claude-3-5-haiku-20241022',
'claude-3-opus-20240229'
],
defaultModel: 'claude-sonnet-4-20250514',
hint: 'Get your key from console.anthropic.com'
},
gemini: {
label: 'Google Gemini',
baseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai',
apiFormat: 'openai',
requiresKey: true,
models: [
'gemini-2.0-flash',
'gemini-1.5-pro',
'gemini-1.5-flash'
],
defaultModel: 'gemini-2.0-flash',
hint: 'OpenAI-compatible endpoint. Get your key from aistudio.google.com'
},
groq: {
label: 'Groq',
baseUrl: 'https://api.groq.com/openai/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'llama-3.3-70b-versatile',
'mixtral-8x7b-32768',
'gemma2-9b-it'
],
defaultModel: 'llama-3.3-70b-versatile',
hint: 'Ultra-fast inference. Get your key from console.groq.com'
},
mistral: {
label: 'Mistral AI',
baseUrl: 'https://api.mistral.ai/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'mistral-large-latest',
'mistral-small-latest',
'open-mistral-nemo'
],
defaultModel: 'mistral-small-latest',
hint: 'Get your key from console.mistral.ai'
},
xai: {
label: 'xAI Grok',
baseUrl: 'https://api.x.ai/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'grok-2',
'grok-2-mini'
],
defaultModel: 'grok-2-mini',
hint: 'Get your key from console.x.ai'
},
// ── routers / aggregators ──────────────────────────────────
openrouter: {
label: 'OpenRouter',
baseUrl: 'https://openrouter.ai/api/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'openai/gpt-4o',
'anthropic/claude-sonnet-4-20250514',
'google/gemini-2.0-flash-001',
'meta-llama/llama-3.3-70b-instruct',
'deepseek/deepseek-r1'
],
defaultModel: 'openai/gpt-4o',
hint: 'Universal AI router — access 200+ models with one key. Get your key from openrouter.ai'
},
together: {
label: 'Together AI',
baseUrl: 'https://api.together.xyz/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'meta-llama/Llama-3.3-70B-Instruct-Turbo',
'Qwen/Qwen2.5-72B-Instruct-Turbo',
'deepseek-ai/DeepSeek-R1',
'mistralai/Mixtral-8x22B-Instruct-v0.1'
],
defaultModel: 'meta-llama/Llama-3.3-70B-Instruct-Turbo',
hint: 'Open-source model cloud. Get your key from api.together.xyz'
},
// ── enterprise ────────────────────────────────────────────
azure_openai: {
label: 'Azure OpenAI',
baseUrl: '',
apiFormat: 'azure-openai',
requiresKey: true,
models: [],
defaultModel: '',
hint: 'Enter your Azure endpoint as Base URL (e.g. https://{resource}.openai.azure.com/openai/deployments/{deployment}). Get your key from portal.azure.com'
},
// ── NVIDIA ───────────────────────────────────────────────
nvidia_nim: {
label: 'NVIDIA NIM',
baseUrl: 'https://integrate.api.nvidia.com/v1',
apiFormat: 'openai',
requiresKey: true,
models: [
'deepseek-ai/deepseek-v3.2',
'qwen/qwen3.5-122b-a10b',
'minimaxai/minimax-m2.1',
'nvidia/nemotron-3-nano-30b-a3b',
'moonshotai/kimi-k2-instruct',
'moonshotai/kimi-k2.5'
],
defaultModel: 'deepseek-ai/deepseek-v3.2',
hint: 'NVIDIA hosted models. Free credits available — register at build.nvidia.com'
},
// ── custom ─────────────────────────────────────────────────
custom: {
label: 'Custom Endpoint',
baseUrl: '',
apiFormat: 'openai',
requiresKey: true,
models: [],
defaultModel: '',
hint: 'Any OpenAI / Anthropic / Responses API compatible endpoint.'
}
};
// construct the API endpoint URL from settings
function constructApiUrl(cfg) {
var base = cfg.baseUrl || '';
if (base.endsWith('/')) {
base = base.slice(0, -1);
}
var format = cfg.apiFormat || 'openai';
if (format === 'anthropic') {
return base + '/messages';
}
if (format === 'responses') {
return base + '/responses';
}
if (format === 'azure-openai') {
return base + '/chat/completions?api-version=2024-10-21';
}
return base + '/chat/completions';
}
// build request headers based on provider/format
function buildApiHeaders(cfg) {
var headers = { 'Content-Type': 'application/json' };
var format = cfg.apiFormat || 'openai';
if (format === 'anthropic') {
if (cfg.apiKey) {
headers['x-api-key'] = cfg.apiKey;
}
headers['anthropic-version'] = '2023-06-01';
return headers;
}
// azure-openai: api-key header
if (format === 'azure-openai') {
if (cfg.apiKey) {
headers['api-key'] = cfg.apiKey;
}
return headers;
}
// openai / responses / default: Bearer token
if (cfg.apiKey) {
var auth = cfg.apiKey;
if (!auth.startsWith('Bearer ')) {
auth = 'Bearer ' + auth;
}
headers['Authorization'] = auth;
}
return headers;
}
// build request payload from messages array and settings
// when cfg.stream is true, adds stream:true to payload
function buildApiPayload(messages, cfg) {
var format = cfg.apiFormat || 'openai';
var model = cfg.model || '';
var useStream = !!cfg.stream;
if (format === 'anthropic') {
var systemParts = [];
var userMessages = [];
for (var i = 0; i < messages.length; i++) {
if (messages[i].role === 'system') {
systemParts.push(messages[i].content);
} else {
userMessages.push({
role: messages[i].role,
content: messages[i].content
});
}
}
var payload = {
model: model,
max_tokens: 16384,
messages: userMessages,
stream: useStream
};
if (systemParts.length > 0) {
payload.system = systemParts.join('\n\n');
}
return payload;
}
if (format === 'responses') {
var rPayload = {
model: model,
input: messages,
max_output_tokens: 16384,
reasoning: { effort: cfg.reasoningEffort || 'high' },
stream: useStream
};
return rPayload;
}
// default: openai / azure-openai chat completions
return {
model: model,
messages: messages,
max_tokens: 16384,
temperature: 0.7,
stream: useStream
};
}
// extract text delta from a single SSE chunk (streaming mode).
// returns the text fragment or '' if the chunk has no content.
function extractStreamDelta(data, format) {
if (!data) return '';
format = format || 'openai';
// anthropic: {type:"content_block_delta", delta:{type:"text_delta", text:"..."}}
if (format === 'anthropic') {
if (data.type === 'content_block_delta' && data.delta && data.delta.text) {
return data.delta.text;
}
return '';
}
// responses API: {type:"response.output_text.delta", delta:"..."}
if (format === 'responses') {
if (data.type === 'response.output_text.delta' && typeof data.delta === 'string') {
return data.delta;
}
return '';
}
// openai / azure-openai: {choices:[{delta:{content:"..."}}]}
if (data.choices && data.choices.length > 0) {
var delta = data.choices[0].delta;
if (delta && delta.content) return delta.content;
}
return '';
}
// extract text content from any supported API response format.
// detection order: anthropic → responses API → openai → fallback
function extractApiResponse(data) {
if (!data) return null;
// anthropic Messages API: {type:"message", content:[{type:"text", text:"..."}]}
if (data.type === 'message' && Array.isArray(data.content)) {
var texts = [];
for (var i = 0; i < data.content.length; i++) {
if (data.content[i].text) {
texts.push(data.content[i].text);
}
}
if (texts.length > 0) return texts.join('');
}
// Responses API: {output:[{type:"message", content:[{..., text:"..."}]}]}
if (data.output && Array.isArray(data.output)) {
for (var i = 0; i < data.output.length; i++) {
var item = data.output[i];
if (item.type === 'message' && item.content) {
if (Array.isArray(item.content)) {
for (var j = 0; j < item.content.length; j++) {
if (item.content[j].text) {
return item.content[j].text;
}
}
}
if (typeof item.content === 'string') {
return item.content;
}
}
}
}
// OpenAI Chat Completions: {choices:[{message:{content:"..."}}]}
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
var choice = data.choices[0];
if (choice.message && choice.message.content) return choice.message.content;
if (choice.delta && choice.delta.content) return choice.delta.content;
if (choice.text) return choice.text;
if (typeof choice.content === 'string') return choice.content;
}
// fallback: common direct fields from various providers
if (data.output && typeof data.output === 'string') return data.output;
if (data.content && typeof data.content === 'string') return data.content;
if (data.content && Array.isArray(data.content)) {
return data.content.map(function(c) { return c.text || c.content || ''; }).join('');
}
if (data.result) return data.result;
if (data.text) return data.text;
if (data.response) return data.response;
return null;
}
// default storage values for settings (used by all consumers)
var API_DEFAULTS = {
provider: 'ollama',
apiFormat: 'openai',
baseUrl: 'http://localhost:11434/v1',
apiKey: '',
model: 'llama3.2',
reasoningEffort: 'high'
};
// backward-compatible alias: templates now live in i18n.js per language.
// evaluated lazily since i18n.js loads after api-utils.js.
var BUILTIN_TEMPLATES = [];