-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconnectionutil.js
More file actions
664 lines (586 loc) · 23.7 KB
/
Copy pathconnectionutil.js
File metadata and controls
664 lines (586 loc) · 23.7 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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/**
* Summaryception Connection Utility
*
* Routes summarization requests through one of four backends:
* - default: SillyTavern's generateRaw() (active connection)
* - profile: ST Connection Profile via ConnectionManagerRequestService
* - ollama: Ollama instance (via ST CORS proxy to avoid browser CORS issues)
* - openai: OpenAI-compatible endpoint (via ST CORS proxy, streaming supported)
*
* CORS Note: Ollama and OpenAI modes route through ST's /cors/ proxy endpoint
* to avoid browser CORS restrictions. Requires enableCorsProxy: true in config.yaml
* OR the target server must have permissive CORS headers.
*
* AGPL-3.0
*/
const MODULE_NAME = '[Summaryception][Connection]';
// ─── Custom Error Class ──────────────────────────────────────────────
/**
* Error class for connection errors with explicit retryable flag.
* The retry logic in callSummarizer checks for this to avoid
* burning through retries on errors that will never succeed
* (e.g. missing config, auth failures, deleted profiles).
*/
class ConnectionError extends Error {
constructor(message, { retryable = false, status = null } = {}) {
super(message);
this.name = 'ConnectionError';
this.retryable = retryable;
this.status = status;
}
}
export { ConnectionError };
// ─── CORS Proxy Helper ───────────────────────────────────────────────
/**
* Wrap a URL through SillyTavern's CORS proxy if needed.
* @param {string} url - The target URL
* @param {boolean} useProxy - Whether to attempt proxying
* @returns {string} - The (possibly proxied) URL
*/
function proxiedUrl(url, useProxy = true) {
if (!useProxy) return url;
return `/proxy/${url}`;
}
/**
* Get standard request headers including ST's CSRF token if available.
* Required when routing through ST's /cors/ proxy.
* @returns {object}
*/
function getProxyHeaders() {
try {
const ctx = SillyTavern.getContext();
if (typeof ctx.getRequestHeaders === 'function') {
return ctx.getRequestHeaders();
}
} catch (e) { /* fallback */ }
return { 'Content-Type': 'application/json' };
}
// ─── Main Entry Point ────────────────────────────────────────────────
/**
* Send a summarization request using the configured connection.
* @param {object} settings - The extension settings containing connection config
* @param {string} systemPrompt - The system prompt
* @param {string} userPrompt - The user prompt
* @returns {Promise<string>} - The generated response text
* @throws {ConnectionError|Error} - If the request fails
*/
export async function sendSummarizerRequest(settings, systemPrompt, userPrompt) {
const source = settings.connectionSource || 'default';
switch (source) {
case 'profile':
return await sendViaProfile(settings.connectionProfileId, systemPrompt, userPrompt);
case 'ollama':
return await sendViaOllama(settings.ollamaUrl, settings.ollamaModel, systemPrompt, userPrompt);
case 'openai':
return await sendViaOpenAI(settings.openaiUrl, settings.openaiKey, settings.openaiModel, systemPrompt, userPrompt, settings.openaiMaxTokens);
case 'default':
default:
return await sendViaDefault(systemPrompt, userPrompt, settings.summarizerResponseLength);
}
}
// ─── Mode 1: Default (generateRaw) ──────────────────────────────────
/**
* Uses ST's built-in generateRaw(), which routes through the active connection.
*/
async function sendViaDefault(systemPrompt, userPrompt, responseLength) {
const { generateRaw } = SillyTavern.getContext();
if (!generateRaw) {
throw new ConnectionError(
'generateRaw is not available in the current SillyTavern context.',
{ retryable: false }
);
}
// ST refactored generateRaw from positional args to an object param
// in PR #4277 (July 2025). We need to support both signatures.
//
// New (July 2025+): generateRaw({ prompt, systemPrompt, responseLength })
// Old (pre-July 2025): generateRaw(prompt, systemPrompt)
//
// Detection: the new signature destructures an object, so if we check
// the function's length (expected positional params), 0 or 1 means
// object-style, 2+ means positional-style.
let result;
if (generateRaw.length <= 1) {
// Modern ST: object-based params
const options = {
systemPrompt: systemPrompt,
prompt: userPrompt,
};
if (responseLength && responseLength > 0) {
options.responseLength = responseLength;
}
result = await generateRaw(options);
} else {
// Legacy ST: positional args — generateRaw(prompt, systemPrompt)
// Note: legacy signature does not support responseLength override
console.warn(
'[Summaryception] Detected legacy generateRaw (positional args). ' +
'Consider updating SillyTavern to July 2025+ for full feature support.'
);
result = await generateRaw(userPrompt, systemPrompt);
}
if (!result || typeof result !== 'string') {
throw new ConnectionError(
'generateRaw returned an empty or invalid response.',
{ retryable: true }
);
}
return result;
}
// ─── Mode 2: Connection Profile ──────────────────────────────────────
/**
* Uses ST's ConnectionManagerRequestService to send a request via a saved profile.
* Requires SillyTavern with PR #3603 merged (March 2025+).
* Full API key support requires staging with Issue #5348 fix (March 30, 2026+).
*
* IMPORTANT: sendRequest() expects messages as an array of {role, content} objects,
* NOT as a generateRaw()-style options object. Passing {systemPrompt, prompt} as
* the second argument causes the entire object to be stuffed into the message
* content field, resulting in "Invalid input" / validation errors from the API.
*/
async function sendViaProfile(profileId, systemPrompt, userPrompt) {
if (!profileId) {
throw new ConnectionError(
'No Connection Profile selected. Please select one in Summaryception settings.',
{ retryable: false }
);
}
const context = SillyTavern.getContext();
const service = context.ConnectionManagerRequestService;
if (!service) {
throw new ConnectionError(
'ConnectionManagerRequestService is not available. ' +
'Your SillyTavern version may be too old. Requires ST with PR #3603 (March 2025+).',
{ retryable: false }
);
}
if (typeof service.sendRequest !== 'function') {
throw new ConnectionError(
'ConnectionManagerRequestService.sendRequest() is not available. ' +
'Please update SillyTavern to the latest staging version.',
{ retryable: false }
);
}
try {
// Build messages as proper {role, content} objects.
// sendRequest expects: sendRequest(profileId, messages, options?)
// where messages is a string OR an array of {role, content} objects.
const messages = [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt },
];
const raw = await service.sendRequest(profileId, messages, {
ignoreInstruct: true,
});
// Debug: log what we actually got back
console.log('[Summaryception][Connection] Profile sendRequest returned:', typeof raw, raw);
// Handle various possible return types
let result;
if (typeof raw === 'string') {
result = raw;
} else if (raw?.content) {
result = raw.content;
} else if (raw?.message?.content) {
result = raw.message.content;
} else if (raw?.choices?.[0]?.message?.content) {
result = raw.choices[0].message.content;
} else if (raw?.data) {
result = typeof raw.data === 'string' ? raw.data : JSON.stringify(raw.data);
} else if (raw && typeof raw === 'object') {
const str = JSON.stringify(raw);
console.warn('[Summaryception][Connection] Unexpected return type from sendRequest:', str.substring(0, 500));
throw new ConnectionError(
`Connection Profile returned unexpected type: ${typeof raw}. ` +
`Preview: ${str.substring(0, 200)}. ` +
`Please report this on the Summaryception GitHub.`,
{ retryable: false }
);
} else {
throw new ConnectionError(
'Connection Profile returned an empty or invalid response.',
{ retryable: true }
);
}
if (!result || !result.trim()) {
throw new ConnectionError(
'Connection Profile returned an empty response.',
{ retryable: true }
);
}
return result;
} catch (error) {
if (error instanceof ConnectionError) throw error;
const msg = error?.message || String(error);
const status = error?.status || error?.response?.status;
if (status === 401 || msg.includes('401') || msg.toLowerCase().includes('unauthorized')) {
throw new ConnectionError(
`Connection Profile auth failed (401). This is likely the API key switching bug ` +
`(ST Issue #5348). Update SillyTavern to staging (March 30, 2026+) to fix this. ` +
`Original error: ${msg}`,
{ retryable: false, status: 401 }
);
}
if (msg.includes('not found') || msg.includes('profile')) {
throw new ConnectionError(
`Connection Profile "${profileId}" not found. It may have been deleted. ` +
`Please re-select a profile in Summaryception settings.`,
{ retryable: false, status: 404 }
);
}
throw new ConnectionError(
`Connection Profile request failed: ${msg}`,
{ retryable: true, status: status }
);
}
}
// ─── Mode 3: Ollama (Local) ─────────────────────────────────────────
/**
* Send a request to a local Ollama instance using /api/chat.
* Routes through ST's CORS proxy to avoid browser CORS restrictions.
*/
async function sendViaOllama(url, model, systemPrompt, userPrompt) {
if (!url) {
throw new ConnectionError(
'Ollama URL is not configured. Please set it in Summaryception settings.',
{ retryable: false }
);
}
if (!model) {
throw new ConnectionError(
'Ollama model is not selected. Please select one in Summaryception settings.',
{ retryable: false }
);
}
const baseUrl = url.replace(/\/+$/, '');
const targetUrl = `${baseUrl}/api/chat`;
let response;
try {
response = await fetch(proxiedUrl(targetUrl), {
method: 'POST',
headers: {
...getProxyHeaders(),
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: model,
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt },
],
stream: false,
options: {
temperature: 0.3,
},
}),
});
} catch (proxyError) {
console.warn(`${MODULE_NAME} CORS proxy failed, trying direct:`, proxyError.message);
try {
response = await fetch(targetUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: model,
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt },
],
stream: false,
options: { temperature: 0.3 },
}),
});
} catch (directError) {
throw new ConnectionError(
`Failed to connect to Ollama at ${baseUrl}. ` +
`CORS proxy error: ${proxyError.message}. Direct error: ${directError.message}. ` +
`Make sure enableCorsProxy is set to true in config.yaml, or set OLLAMA_ORIGINS=* on your Ollama instance.`,
{ retryable: true }
);
}
}
if (!response.ok) {
const errorText = await response.text().catch(() => 'Unknown error');
throw new ConnectionError(
`Ollama request failed (${response.status}): ${errorText}`,
{ retryable: response.status >= 500, status: response.status }
);
}
const data = await response.json();
if (!data?.message?.content) {
throw new ConnectionError(
'Ollama returned an empty or invalid response.',
{ retryable: true }
);
}
return data.message.content;
}
/**
* Fetch available models from an Ollama instance.
* @param {string} url - The Ollama base URL
* @returns {Promise<Array<{name: string, size: number, modified_at: string}>>}
*/
export async function fetchOllamaModels(url) {
if (!url) {
throw new Error('Ollama URL is not configured.');
}
const baseUrl = url.replace(/\/+$/, '');
const targetUrl = `${baseUrl}/api/tags`;
let response;
try {
response = await fetch(proxiedUrl(targetUrl), {
method: 'GET',
headers: getProxyHeaders(),
});
} catch (proxyError) {
console.warn(`${MODULE_NAME} CORS proxy failed for model list, trying direct:`, proxyError.message);
try {
response = await fetch(targetUrl, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
} catch (directError) {
throw new Error(
`Failed to connect to Ollama at ${baseUrl}. ` +
`Enable the CORS proxy in config.yaml (enableCorsProxy: true) or set OLLAMA_ORIGINS=* on your Ollama instance. ` +
`Proxy error: ${proxyError.message}. Direct error: ${directError.message}`
);
}
}
if (!response.ok) {
const errorText = await response.text().catch(() => 'Unknown error');
throw new Error(`Failed to fetch Ollama models (${response.status}): ${errorText}`);
}
const data = await response.json();
if (!data?.models || !Array.isArray(data.models)) {
throw new Error('Unexpected response format from Ollama /api/tags.');
}
return data.models;
}
// ─── Mode 4: OpenAI Compatible (Streaming) ──────────────────────────
/**
* Send a request to any OpenAI-compatible endpoint using streaming.
* Streaming avoids the non-streaming token ceiling (4096 on many providers)
* and allows reasoning models to complete their full thinking + output.
*
* Routes through ST's CORS proxy for local endpoints.
* Cloud endpoints skip the proxy since they have CORS headers.
*/
async function sendViaOpenAI(url, apiKey, model, systemPrompt, userPrompt, maxTokens) {
if (!url) {
throw new ConnectionError(
'OpenAI Compatible URL is not configured. Please set it in Summaryception settings.',
{ retryable: false }
);
}
if (!model) {
throw new ConnectionError(
'OpenAI Compatible model name is not set. Please enter one in Summaryception settings.',
{ retryable: false }
);
}
const baseUrl = url.replace(/\/+$/, '');
// Build the endpoint URL
let endpoint = baseUrl;
if (!endpoint.endsWith('/chat/completions')) {
if (endpoint.endsWith('/v1')) {
endpoint += '/chat/completions';
} else if (!endpoint.includes('/chat/completions')) {
endpoint += '/v1/chat/completions';
}
}
// Decide whether to use CORS proxy
const isLocal = /^https?:\/\/(localhost|127\.0\.0\.1|192\.168\.\d+\.\d+|10\.\d+\.\d+\.\d+|172\.(1[6-9]|2\d|3[01])\.\d+\.\d+)(:\d+)?/i.test(endpoint);
const headers = {
'Content-Type': 'application/json',
};
if (apiKey) {
headers['Authorization'] = `Bearer ${apiKey}`;
}
// Use maxTokens from settings, default to 0 (no limit / provider default)
const tokenLimit = maxTokens && maxTokens > 0 ? maxTokens : undefined;
const requestBody = {
model: model,
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt },
],
temperature: 0.8,
stream: true,
};
// Only include max_tokens if explicitly set
if (tokenLimit) {
requestBody.max_tokens = tokenLimit;
}
const body = JSON.stringify(requestBody);
let response;
if (isLocal) {
try {
response = await fetch(proxiedUrl(endpoint), {
method: 'POST',
headers: { ...getProxyHeaders(), ...headers },
body: body,
});
} catch (proxyError) {
console.warn(`${MODULE_NAME} CORS proxy failed for OpenAI endpoint, trying direct:`, proxyError.message);
try {
response = await fetch(endpoint, {
method: 'POST',
headers: headers,
body: body,
});
} catch (directError) {
throw new ConnectionError(
`Failed to connect to ${baseUrl}. ` +
`Enable the CORS proxy in config.yaml (enableCorsProxy: true). ` +
`Proxy error: ${proxyError.message}. Direct error: ${directError.message}`,
{ retryable: true }
);
}
}
} else {
try {
response = await fetch(endpoint, {
method: 'POST',
headers: headers,
body: body,
});
} catch (fetchError) {
throw new ConnectionError(
`Failed to connect to ${baseUrl}: ${fetchError.message}`,
{ retryable: true }
);
}
}
if (!response.ok) {
const errorText = await response.text().catch(() => 'Unknown error');
if (response.status === 401) {
throw new ConnectionError(
'OpenAI Compatible endpoint returned 401 Unauthorized. Check your API key.',
{ retryable: false, status: 401 }
);
}
if (response.status === 403) {
throw new ConnectionError(
`OpenAI Compatible endpoint returned 403 Forbidden: ${errorText}`,
{ retryable: false, status: 403 }
);
}
throw new ConnectionError(
`OpenAI Compatible request failed (${response.status}): ${errorText}`,
{ retryable: response.status >= 500 || response.status === 429, status: response.status }
);
}
// ─── Stream reading ──────────────────────────────────────────
// Read SSE chunks and assemble the full response content.
const reader = response.body.getReader();
const decoder = new TextDecoder();
let fullContent = '';
let buffer = '';
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
// Process complete SSE lines
const lines = buffer.split('\n');
// Keep the last potentially incomplete line in the buffer
buffer = lines.pop() || '';
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || !trimmed.startsWith('data:')) continue;
const data = trimmed.slice(5).trim();
if (data === '[DONE]') continue;
try {
const parsed = JSON.parse(data);
const delta = parsed.choices?.[0]?.delta?.content;
if (delta) {
fullContent += delta;
}
} catch (e) {
// Skip unparseable chunks (comments, keep-alive, etc.)
}
}
}
} finally {
reader.releaseLock();
}
if (!fullContent.trim()) {
throw new ConnectionError(
'OpenAI Compatible endpoint returned an empty response (streaming).',
{ retryable: true }
);
}
return fullContent;
}
/**
* Test the connection to an OpenAI-compatible endpoint.
* @param {string} url
* @param {string} apiKey
* @param {string} model
* @returns {Promise<{success: boolean, message: string}>}
*/
export async function testOpenAIConnection(url, apiKey, model) {
try {
const result = await sendViaOpenAI(
url,
apiKey,
model || 'test',
'You are a test assistant.',
'Respond with exactly: CONNECTION_OK',
100 // small token limit for test
);
return {
success: true,
message: `Connection successful! Response: "${result.substring(0, 100)}"`,
};
} catch (error) {
return {
success: false,
message: `Connection failed: ${error.message}`,
};
}
}
// ─── Helpers ─────────────────────────────────────────────────────────
/**
* Populate a <select> element with connection profiles using ST's built-in handler.
* @param {HTMLSelectElement} selectElement - The dropdown to populate
* @param {string} currentValue - The currently selected profile ID
* @returns {boolean} - Whether population succeeded
*/
export function populateProfileDropdown(selectElement, currentValue) {
try {
const context = SillyTavern.getContext();
const service = context.ConnectionManagerRequestService;
if (service && typeof service.handleDropdown === 'function') {
service.handleDropdown(selectElement);
if (currentValue) {
selectElement.value = currentValue;
}
return true;
}
console.warn(`${MODULE_NAME} handleDropdown not available.`);
return false;
} catch (error) {
console.error(`${MODULE_NAME} Error populating profile dropdown:`, error);
return false;
}
}
/**
* Get a human-readable name for the current connection source.
* @param {object} settings
* @returns {string}
*/
export function getConnectionDisplayName(settings) {
switch (settings.connectionSource) {
case 'default':
return 'Default (Main API)';
case 'profile':
return `Profile: ${settings.connectionProfileId || '(none)'}`;
case 'ollama':
return `Ollama: ${settings.ollamaModel || '(no model)'}`;
case 'openai':
return `OpenAI: ${settings.openaiModel || '(no model)'}`;
default:
return 'Default (Main API)';
}
}