-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
478 lines (431 loc) · 16.6 KB
/
Copy pathapi.js
File metadata and controls
478 lines (431 loc) · 16.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
// ============================================
// AI Chat Pro Client – Web App API Layer
// Supports both non-streaming and streaming responses.
// On error throws an object: { message, errorCode, errorParams }
// ============================================
(function () {
"use strict";
function apiError(code, params, fallback) {
const err = new Error(fallback);
err.errorCode = code;
err.errorParams = params || [];
return err;
}
function buildMessages(s, requestMessages) {
const msgs = [];
if (s.systemPrompt) msgs.push({ role: "system", content: s.systemPrompt });
msgs.push(...requestMessages);
return msgs;
}
async function handleHttpError(response, provider) {
const err = await response.json().catch(() => ({}));
const detail = err.error?.message || response.statusText;
const code = provider === "lmstudio" ? "lmstudio" : "api";
throw apiError(code, [response.status, detail],
`${provider} error (${response.status}): ${detail}`);
}
// ============================================
// Non-streaming calls (fallback)
// ============================================
async function callPerplexity(s, apiKey, model, messages) {
const base = (s.baseUrls?.perplexity || "https://api.perplexity.ai").replace(/\/$/, "");
const resp = await fetch(`${base}/chat/completions`, {
method: "POST",
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
body: JSON.stringify({ model, messages, temperature: s.temperature, max_tokens: s.maxTokens, stream: false }),
});
if (!resp.ok) await handleHttpError(resp, "perplexity");
const data = await resp.json();
return { content: data.choices[0].message.content, citations: data.citations || [], usage: data.usage };
}
async function callOpenAI(s, apiKey, model, messages) {
const base = (s.baseUrls?.openai || "https://api.openai.com").replace(/\/$/, "");
const resp = await fetch(`${base}/v1/chat/completions`, {
method: "POST",
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
body: JSON.stringify({ model, messages, temperature: s.temperature, max_tokens: s.maxTokens }),
});
if (!resp.ok) await handleHttpError(resp, "openai");
const data = await resp.json();
return { content: data.choices[0].message.content, usage: data.usage };
}
async function callAnthropic(s, apiKey, model, messages) {
const body = {
model,
max_tokens: s.maxTokens,
messages: messages.filter((m) => m.role !== "system"),
};
if (s.systemPrompt) body.system = s.systemPrompt;
const base = (s.baseUrls?.anthropic || "https://api.anthropic.com").replace(/\/$/, "");
const resp = await fetch(`${base}/v1/messages`, {
method: "POST",
headers: {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"anthropic-dangerous-allow-browser": "true",
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!resp.ok) await handleHttpError(resp, "anthropic");
const data = await resp.json();
return { content: data.content[0].text, usage: data.usage };
}
async function callGemini(s, apiKey, model, messages) {
const contents = messages.map((m) => ({
role: m.role === "assistant" ? "model" : "user",
parts: [{ text: m.content }],
}));
const generationConfig = {
temperature: s.temperature,
maxOutputTokens: s.maxTokens,
};
if (geminiSupportsThinking(model)) {
// -1 = dynamic thinking: the model decides how much to think.
// This also tends to produce longer, more incremental thought summaries.
generationConfig.thinkingConfig = {
includeThoughts: true,
thinkingBudget: -1,
};
}
const body = { contents, generationConfig };
if (s.systemPrompt) body.systemInstruction = { parts: [{ text: s.systemPrompt }] };
const base = (s.baseUrls?.gemini || "https://generativelanguage.googleapis.com").replace(/\/$/, "");
const resp = await fetch(`${base}/v1beta/models/${model}:generateContent?key=${apiKey}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!resp.ok) await handleHttpError(resp, "gemini");
const data = await resp.json();
const parts = data.candidates?.[0]?.content?.parts || [];
let content = "";
let reasoning = "";
for (const p of parts) {
if (!p.text) continue;
if (p.thought === true) reasoning += p.text;
else content += p.text;
}
return { content, reasoning };
}
async function callLMStudio(s, apiKey, model, messages) {
const base = (s.baseUrls?.lmstudio || "http://localhost:1234").replace(/\/$/, "");
const body = { messages, temperature: s.temperature, max_tokens: s.maxTokens, stream: false };
if (model) body.model = model;
const resp = await fetch(`${base}/v1/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
},
body: JSON.stringify(body),
});
if (!resp.ok) await handleHttpError(resp, "lmstudio");
const data = await resp.json();
return { content: data.choices[0].message.content, usage: data.usage };
}
// ============================================
// Streaming calls
// ============================================
/**
* Parse SSE lines from a text chunk.
* Handles buffering of incomplete lines across chunks.
*/
function createSSEParser() {
let buffer = "";
return function parse(chunk) {
buffer += chunk;
const events = [];
const lines = buffer.split("\n");
buffer = lines.pop(); // keep incomplete line
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed === "data: [DONE]") continue;
if (trimmed.startsWith("data: ")) {
try {
events.push(JSON.parse(trimmed.slice(6)));
} catch (e) { /* skip malformed */ }
}
}
return events;
};
}
/**
* Stream from OpenAI-compatible endpoint (OpenAI, Perplexity, LM Studio).
* Calls onContent(text), onReasoning(text) for each chunk.
* Returns { content, reasoning, citations, usage }.
*/
async function streamOpenAICompatible(url, headers, body, callbacks) {
const resp = await fetch(url, {
method: "POST",
headers: { ...headers, "Content-Type": "application/json" },
body: JSON.stringify({ ...body, stream: true }),
});
if (!resp.ok) {
const err = await resp.json().catch(() => ({}));
throw apiError("api", [resp.status, err.error?.message || resp.statusText],
`API error (${resp.status}): ${err.error?.message || resp.statusText}`);
}
const reader = resp.body.getReader();
const decoder = new TextDecoder();
const parse = createSSEParser();
let content = "";
let reasoning = "";
let citations = [];
let usage = null;
while (true) {
const { done, value } = await reader.read();
if (done) break;
const events = parse(decoder.decode(value, { stream: true }));
for (const data of events) {
const delta = data.choices?.[0]?.delta;
if (!delta) continue;
// Reasoning tokens (OpenAI reasoning models, some providers)
const reasoningChunk = delta.reasoning_content || delta.reasoning || null;
if (reasoningChunk) {
reasoning += reasoningChunk;
if (callbacks.onReasoning) callbacks.onReasoning(reasoningChunk);
}
// Content tokens
if (delta.content) {
content += delta.content;
if (callbacks.onContent) callbacks.onContent(delta.content);
}
// Citations (Perplexity)
if (data.citations) citations = data.citations;
if (data.usage) usage = data.usage;
}
}
return { content, reasoning, citations, usage };
}
/**
* Stream from Anthropic API.
* Anthropic uses a different SSE format with event types.
*/
async function streamAnthropic(s, apiKey, model, messages, callbacks) {
const body = {
model,
max_tokens: s.maxTokens,
stream: true,
messages: messages.filter((m) => m.role !== "system"),
};
if (s.systemPrompt) body.system = s.systemPrompt;
// Extended thinking for Claude models that support it
if (model && (model.includes("claude-3-7") || model.includes("claude-4") || model.includes("opus") || model.includes("sonnet-4"))) {
body.thinking = { type: "enabled", budget_tokens: Math.min(s.maxTokens, 8000) };
}
const base = (s.baseUrls?.anthropic || "https://api.anthropic.com").replace(/\/$/, "");
const resp = await fetch(`${base}/v1/messages`, {
method: "POST",
headers: {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"anthropic-dangerous-allow-browser": "true",
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!resp.ok) await handleHttpError(resp, "anthropic");
const reader = resp.body.getReader();
const decoder = new TextDecoder();
const parse = createSSEParser();
let content = "";
let reasoning = "";
let usage = null;
let currentBlockType = null;
while (true) {
const { done, value } = await reader.read();
if (done) break;
const events = parse(decoder.decode(value, { stream: true }));
for (const data of events) {
// Anthropic event types
if (data.type === "content_block_start") {
currentBlockType = data.content_block?.type || null;
} else if (data.type === "content_block_delta") {
if (data.delta?.type === "thinking_delta") {
reasoning += data.delta.thinking;
if (callbacks.onReasoning) callbacks.onReasoning(data.delta.thinking);
} else if (data.delta?.type === "text_delta") {
content += data.delta.text;
if (callbacks.onContent) callbacks.onContent(data.delta.text);
}
} else if (data.type === "message_delta") {
if (data.usage) usage = data.usage;
}
}
}
return { content, reasoning, usage };
}
/**
* Detect whether a Gemini model supports the "thinking" feature.
* Supported by 2.5+, 3.x and explicit "thinking" / "flash-lite-preview" models.
*/
function geminiSupportsThinking(model) {
if (!model) return false;
const m = model.toLowerCase();
return (
m.includes("2.5") ||
m.includes("3.0") ||
m.includes("3.1") ||
m.includes("3-") ||
m.includes("thinking") ||
m.includes("flash-lite")
);
}
/**
* Stream from Google Gemini API.
* Uses :streamGenerateContent?alt=sse which returns OpenAI-like SSE chunks.
* For thinking-capable models, asks for thought summaries via
* generationConfig.thinkingConfig.includeThoughts and routes parts with
* thought:true to onReasoning.
*/
async function streamGemini(s, apiKey, model, messages, callbacks) {
const contents = messages.map((m) => ({
role: m.role === "assistant" ? "model" : "user",
parts: [{ text: m.content }],
}));
const generationConfig = {
temperature: s.temperature,
maxOutputTokens: s.maxTokens,
};
if (geminiSupportsThinking(model)) {
// -1 = dynamic thinking: the model decides how much to think.
// This also tends to produce longer, more incremental thought summaries.
generationConfig.thinkingConfig = {
includeThoughts: true,
thinkingBudget: -1,
};
}
const body = { contents, generationConfig };
if (s.systemPrompt) body.systemInstruction = { parts: [{ text: s.systemPrompt }] };
const base = (s.baseUrls?.gemini || "https://generativelanguage.googleapis.com").replace(/\/$/, "");
const resp = await fetch(
`${base}/v1beta/models/${model}:streamGenerateContent?alt=sse&key=${apiKey}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
}
);
if (!resp.ok) await handleHttpError(resp, "gemini");
const reader = resp.body.getReader();
const decoder = new TextDecoder();
const parse = createSSEParser();
let content = "";
let reasoning = "";
let usage = null;
while (true) {
const { done, value } = await reader.read();
if (done) break;
const events = parse(decoder.decode(value, { stream: true }));
for (const data of events) {
const parts = data.candidates?.[0]?.content?.parts;
if (parts && Array.isArray(parts)) {
for (const p of parts) {
if (!p.text) continue;
if (p.thought === true) {
reasoning += p.text;
if (callbacks.onReasoning) callbacks.onReasoning(p.text);
} else {
content += p.text;
if (callbacks.onContent) callbacks.onContent(p.text);
}
}
}
if (data.usageMetadata) usage = data.usageMetadata;
}
}
return { content, reasoning, usage };
}
/**
* Main streaming entry point.
* @param {Object} settings
* @param {Array} messages
* @param {Object} callbacks - { onContent(chunk), onReasoning(chunk) }
* @returns {Promise<{content, reasoning, citations?, usage?}>}
*/
async function chatStream(settings, messages, callbacks) {
const s = settings || {};
const provider = s.provider || "perplexity";
const apiKey = s.apiKeys?.[provider] || "";
const model = s.models?.[provider] || "";
const built = buildMessages(s, messages);
const cb = callbacks || {};
if (provider !== "lmstudio" && !apiKey) {
throw apiError("apiKeyMissing", [], "API key not configured.");
}
try {
switch (provider) {
case "anthropic":
return await streamAnthropic(s, apiKey, model, built, cb);
case "openai": {
const base = (s.baseUrls?.openai || "https://api.openai.com").replace(/\/$/, "");
return await streamOpenAICompatible(
`${base}/v1/chat/completions`,
{ Authorization: `Bearer ${apiKey}` },
{ model, messages: built, temperature: s.temperature, max_tokens: s.maxTokens },
cb
);
}
case "lmstudio": {
const base = (s.baseUrls?.lmstudio || "http://localhost:1234").replace(/\/$/, "");
const lmBody = { messages: built, temperature: s.temperature, max_tokens: s.maxTokens };
if (model) lmBody.model = model;
return await streamOpenAICompatible(
`${base}/v1/chat/completions`,
apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
lmBody,
cb
);
}
case "perplexity":
default: {
const base = (s.baseUrls?.perplexity || "https://api.perplexity.ai").replace(/\/$/, "");
return await streamOpenAICompatible(
`${base}/chat/completions`,
{ Authorization: `Bearer ${apiKey}` },
{ model, messages: built, temperature: s.temperature, max_tokens: s.maxTokens },
cb
);
}
case "gemini":
try {
return await streamGemini(s, apiKey, model, built, cb);
} catch (e) {
// Fallback to non-streaming if the streaming endpoint fails
if (e.errorCode) throw e;
return await callGemini(s, apiKey, model, built);
}
}
} catch (err) {
if (err.errorCode) throw err;
throw apiError("connection", [err.message], `Connection error: ${err.message}`);
}
}
/**
* Non-streaming main entry point (legacy).
*/
async function chat(settings, messages) {
const s = settings || {};
const provider = s.provider || "perplexity";
const apiKey = s.apiKeys?.[provider] || "";
const model = s.models?.[provider] || "";
const built = buildMessages(s, messages);
if (provider !== "lmstudio" && !apiKey) {
throw apiError("apiKeyMissing", [], "API key not configured.");
}
try {
switch (provider) {
case "openai": return await callOpenAI(s, apiKey, model, built);
case "anthropic": return await callAnthropic(s, apiKey, model, built);
case "gemini": return await callGemini(s, apiKey, model, built);
case "lmstudio": return await callLMStudio(s, apiKey, model, built);
default: return await callPerplexity(s, apiKey, model, built);
}
} catch (err) {
if (err.errorCode) throw err;
throw apiError("connection", [err.message], `Connection error: ${err.message}`);
}
}
window.Api = { chat, chatStream };
})();