-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiWorker.js
More file actions
378 lines (315 loc) · 10.9 KB
/
apiWorker.js
File metadata and controls
378 lines (315 loc) · 10.9 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
/**
* API Worker for handling external API providers (OpenRouter, llama.cpp, custom APIs)
*/
class ApiProvider {
constructor(settings) {
this.settings = settings;
}
async generateCompletion(messages) {
throw new Error('generateCompletion must be implemented by subclass');
}
}
class CustomApiProvider extends ApiProvider {
async generateCompletion(messages) {
// Get the selected custom API configuration
const apiConfig = this.getSelectedApiConfig();
if (!apiConfig) {
throw new Error('No custom API selected');
}
const headers = {
'Content-Type': 'application/json'
};
// Add API key if provided
if (apiConfig.apiKey) {
headers['Authorization'] = `Bearer ${apiConfig.apiKey}`;
}
const apiUrl = apiConfig.apiUrl;
const body = {
messages: messages,
stream: true,
max_tokens: apiConfig.maxTokens || 2048,
// Use the custom API's parameters
temperature: apiConfig.temperature || 0.7,
top_p: apiConfig.topP || 0.9,
min_p: apiConfig.minP || 0.0,
top_k: apiConfig.topK || 50,
repetition_penalty: apiConfig.repetitionPenalty || 1.1,
presence_penalty: apiConfig.presencePenalty || 0.0,
frequency_penalty: apiConfig.frequencyPenalty || 0.0
};
// Add model if specified
if (apiConfig.modelName) {
body.model = apiConfig.modelName;
}
const response = await fetch(apiUrl, {
method: 'POST',
headers,
body: JSON.stringify(body)
});
if (!response.ok) {
let errorMessage = `API Error (${response.status}): ${response.statusText}`;
let errorDetails = null;
try {
const errorData = await response.json();
if (errorData.error) {
errorMessage = errorData.error.message || errorMessage;
errorDetails = errorData.error;
}
} catch (e) {
// Could not parse error response
}
const error = new Error(errorMessage);
error.status = response.status;
error.details = errorDetails;
throw error;
}
return response;
}
getSelectedApiConfig() {
if (!this.settings.customApis || !Array.isArray(this.settings.customApis)) {
return null;
}
const selectedIndex = this.settings.selectedCustomApi;
if (selectedIndex === null || selectedIndex === undefined || selectedIndex < 0) {
return null;
}
const apiConfig = this.settings.customApis[selectedIndex];
if (!apiConfig) {
return null;
}
// Find the associated provider
const provider = this.getProviderById(apiConfig.providerId);
if (!provider) {
return null; // Provider not found
}
// Find the selected API key
const selectedKey = provider.apiKeys.find(key => key.name === apiConfig.keyId);
if (!selectedKey) {
// Fallback to default key if selected key not found
const defaultKey = provider.apiKeys.find(key => key.isDefault);
if (!defaultKey) {
return null; // No usable key found
}
apiConfig.keyId = defaultKey.name;
}
// Return enhanced config with provider details
return {
...apiConfig,
apiUrl: provider.apiUrl,
apiKey: selectedKey ? selectedKey.key : (provider.apiKeys.find(key => key.isDefault)?.key || '')
};
}
getProviderById(providerId) {
if (!this.settings.providers || !Array.isArray(this.settings.providers)) {
return null;
}
return this.settings.providers.find(p => p.id === providerId);
}
}
// Current provider instance
let currentProvider = null;
let currentSettings = null;
async function initializeProvider(settings) {
currentSettings = settings;
switch (settings.provider) {
case 'custom':
currentProvider = new CustomApiProvider(settings);
break;
default:
throw new Error(`Unknown provider: ${settings.provider}`);
}
}
async function processStreamResponse(response, onUpdate, onComplete) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let fullResponse = '';
let fullReasoning = '';
const startTime = performance.now();
let tokenCount = 0;
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const chunk = decoder.decode(value, { stream: true });
buffer += chunk;
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') {
onComplete(fullResponse, fullReasoning, tokenCount, startTime);
return;
}
try {
const parsed = JSON.parse(data);
// Try different content paths for different providers
let content = null;
let reasoning = null;
// Check for reasoning content (CoT models)
if (parsed.choices?.[0]?.delta?.reasoning) {
reasoning = parsed.choices[0].delta.reasoning;
}
// OpenAI/OpenRouter format
if (parsed.choices?.[0]?.delta?.content) {
content = parsed.choices[0].delta.content;
}
// llama.cpp format (might be different)
else if (parsed.choices?.[0]?.text) {
content = parsed.choices[0].text;
}
// Alternative llama.cpp format
else if (parsed.content) {
content = parsed.content;
}
// Another possible format
else if (parsed.message?.content) {
content = parsed.message.content;
}
// Check if this is a final completion chunk with finish_reason
else if (parsed.choices?.[0]?.finish_reason === 'stop' && !fullResponse) {
// This might be a non-streaming response or final chunk
// Let's try to get the full message content if available
if (parsed.choices[0].message?.content) {
content = parsed.choices[0].message.content;
}
}
// Handle reasoning updates
if (reasoning) {
fullReasoning += reasoning;
}
if (content || reasoning) {
if (content) {
fullResponse += content;
}
// Estimate tokens (include reasoning in token count)
tokenCount = Math.ceil((fullResponse.length + fullReasoning.length) / 4);
const tps = tokenCount > 0 ? (tokenCount / (performance.now() - startTime)) * 1000 : 0;
onUpdate(content || '', reasoning || '', tps, tokenCount, fullReasoning);
}
} catch (e) {
// Skip malformed JSON lines
console.warn('Failed to parse streaming response:', data, e);
}
}
}
}
// If we reach here without a [DONE] marker, complete anyway
if (fullResponse || fullReasoning) {
onComplete(fullResponse, fullReasoning, tokenCount, startTime);
}
} catch (error) {
throw new Error(`Stream processing error: ${error.message}`);
}
}
async function generate(messages) {
if (!currentProvider) {
throw new Error('No provider initialized');
}
// Add system prompt from settings
const systemPrompt = currentProvider.settings.currentSystemPrompt;
const platformPrompt = `
---
When writing any mathematical expression, equation, or formula, always use LaTeX syntax inside math delimiters:
Inline math: $...$
Display math: $$...$$
Use proper LaTeX commands for symbols, operators, and formatting.
Do not output plain text approximations of math (e.g., "sqrt(x)" or "x^2").
Assume all environments support LaTeX rendering via KaTeX.`;
// Use the system prompt content, with fallback to default only if no system prompt is provided at all
let basePrompt = 'Be a helpful assistant'; // Default fallback
if (systemPrompt && systemPrompt.content) {
basePrompt = systemPrompt.content;
}
const system_prompt = basePrompt + platformPrompt;
// Create a copy of messages and add system prompt if not present
const processedMessages = [...messages];
if (processedMessages[0]?.role !== 'system') {
processedMessages.unshift({
role: 'system',
content: system_prompt
});
}
// Tell the main thread we are starting
self.postMessage({ status: 'start' });
try {
const response = await currentProvider.generateCompletion(processedMessages);
await processStreamResponse(
response,
(content, reasoning, tps, numTokens, fullReasoning) => {
self.postMessage({
status: 'update',
output: content,
reasoning: reasoning,
fullReasoning: fullReasoning,
tps,
numTokens,
state: reasoning ? 'thinking' : 'answering'
});
},
(fullResponse, fullReasoning, numTokens, startTime) => {
const totalTime = (performance.now() - startTime) / 1000;
const avgTps = numTokens / totalTime;
self.postMessage({
status: 'complete',
output: fullResponse,
reasoning: fullReasoning,
tps: avgTps,
numTokens
});
}
);
} catch (error) {
console.error('Generation error:', error);
// Create structured error response
const errorResponse = {
status: 'error',
data: {
message: error.message,
isRetryable: true,
errorType: 'api_error'
}
};
// Add additional details if available
if (error.status) {
errorResponse.data.httpStatus = error.status;
errorResponse.data.isRetryable = error.status >= 500 || error.status === 429; // Server errors and rate limits are retryable
}
if (error.details) {
errorResponse.data.details = error.details;
// Check for specific error patterns that might indicate retry-ability
if (error.details.code === 429 || error.message.includes('rate-limited')) {
errorResponse.data.isRetryable = true;
errorResponse.data.errorType = 'rate_limit';
}
}
self.postMessage(errorResponse);
}
}
// Listen for messages from the main thread
self.addEventListener('message', async (e) => {
const { type, data } = e.data;
switch (type) {
case 'init':
try {
await initializeProvider(data);
self.postMessage({ status: 'ready' });
} catch (error) {
self.postMessage({ status: 'error', data: error.message });
}
break;
case 'generate':
await generate(data);
break;
case 'interrupt':
// For API providers, we can't really interrupt mid-stream easily
// This would require AbortController support
break;
case 'reset':
// Nothing to reset for API providers
break;
}
});