llm-switcher 1.6.15 → 1.6.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog — LLM Switcher
2
2
 
3
+ ## Release 1.6.16 — 2026-10-13
4
+
5
+ - **A silent upstream stream.** The proxy now ends an upstream stream that sends no byte for 5 minutes, with an error. Before, only the default limit of the Node HTTP client stopped it. Set `LLM_SWITCHER_UPSTREAM_IDLE_MS` to change the limit.
6
+ - **A test for a stream that ends with no finish reason.** A converted stream that ends cleanly without a finish reason gives an error, not a finished answer. A test now holds this behavior.
7
+
3
8
  ## Release 1.6.15 — 2026-10-13
4
9
 
5
10
  - **A message inside a tool batch.** Codex can put a notice between two tool outputs of one parallel batch. The proxy now moves the notice after the batch. Before, the next real output was replaced with a placeholder.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llm-switcher",
3
- "version": "1.6.15",
3
+ "version": "1.6.16",
4
4
  "description": "Zero-dependency multi-protocol edge gateway & provider switcher for Claude Code and Codex, with OpenAI-compatible, Anthropic and Vertex upstreams",
5
5
  "keywords": [
6
6
  "llm",
@@ -46,6 +46,12 @@ function startUpstream() {
46
46
  try { json = raw ? JSON.parse(raw) : null; } catch {}
47
47
  received.push({ method: req.method, url: req.url, headers: req.headers, raw, body: json });
48
48
 
49
+ if (req.url.startsWith('/eof/v1/chat/completions')) {
50
+ // A chunk of text, then a clean end with no finish_reason and no [DONE].
51
+ res.writeHead(200, { 'Content-Type': 'text/event-stream' });
52
+ res.write('data: {"id":"c","choices":[{"index":0,"delta":{"content":"half an ans"}}]}\n\n');
53
+ return res.end();
54
+ }
49
55
  if (req.url.startsWith('/fail/v1/')) {
50
56
  res.writeHead(500, { 'Content-Type': 'application/json' });
51
57
  return res.end(JSON.stringify({ error: { message: 'upstream exploded' } }));
@@ -188,6 +194,7 @@ before(async () => {
188
194
  agyb: { name: 'agy on intact', mode: 'convert', tool: 'agy', outFormat: 'openai-chat', baseURL: `${base}/intact/v1`, apiKey: 'sk-intact', defaultModels: { main: 'antigravity/*' } },
189
195
  agyc: { name: 'agy converted', mode: 'convert', tool: 'agy', outFormat: 'openai-chat', baseURL: `${base}/chat/v1`, apiKey: 'sk-chat', defaultModels: { main: 'gem/flash' } },
190
196
  agye: { name: 'agy empty slot', mode: 'convert', tool: 'agy', outFormat: 'openai-chat', baseURL: `${base}/chat/v1`, apiKey: 'sk-chat', defaultModels: {} },
197
+ agyeof: { name: 'agy clean EOF', mode: 'convert', tool: 'agy', outFormat: 'openai-chat', baseURL: `${base}/eof/v1`, apiKey: 'sk-eof', defaultModels: { main: 'gem/flash' } },
191
198
  agy500: { name: 'agy failing upstream', mode: 'convert', tool: 'agy', outFormat: 'openai-chat', baseURL: `${base}/fail/v1`, apiKey: 'sk-fail', defaultModels: { main: 'gem/flash' } },
192
199
  agyf: { name: 'agy flaky lookup', mode: 'convert', tool: 'agy', outFormat: 'openai-chat', baseURL: `${base}/flaky/v1`, apiKey: 'sk-flaky', defaultModels: { main: 'antigravity/*' } },
193
200
  agynf: { name: 'agy lookup 404', mode: 'convert', tool: 'agy', outFormat: 'openai-chat', baseURL: `${base}/nf/v1`, apiKey: 'sk-nf', defaultModels: { main: 'antigravity/*' } },
@@ -456,6 +463,15 @@ test('an agy profile with no main slot sends the model that agy picked', async (
456
463
  assert.equal(lastTo('/chat/v1/chat/completions').body.model, 'gemini-3.8-flash-high');
457
464
  });
458
465
 
466
+ test('a converted stream that ends cleanly without a finish reason is an error, not a finished answer', async () => {
467
+ // router-for-me/CLIProxyAPI#6533: a clean EOF must not become finishReason STOP.
468
+ const r = await fetch(url('/v1internal:streamGenerateContent?alt=sse'), { method: 'POST', headers: agyHeaders({ 'x-llm-profile': 'agyeof' }), body: JSON.stringify(envelope()) });
469
+ const text = await r.text();
470
+ assert.ok(text.includes('half an ans'), text);
471
+ assert.ok(!/"finishReason":"STOP"/.test(text), `a cut stream ended as STOP:\n${text}`);
472
+ assert.match(text, /error/i);
473
+ });
474
+
459
475
  test('a converted agy turn whose upstream fails gets the Code Assist error shape', async () => {
460
476
  const r = await fetch(url('/v1internal:generateContent'), { method: 'POST', headers: agyHeaders({ 'x-llm-profile': 'agy500' }), body: JSON.stringify(envelope()) });
461
477
  assert.ok(r.status >= 500, `status ${r.status}`);
@@ -28,3 +28,22 @@ test('malformed JSON is surfaced and stopping consumption cancels the upstream',
28
28
  for await (const item of readUpstreamPayloads({ body })) { assert.equal(item.one, 1); break; }
29
29
  assert.equal(canceled, true);
30
30
  });
31
+
32
+ test('an upstream that goes silent mid-stream ends with an error after the idle limit', async () => {
33
+ // A dead HTTP/2 session can hold a stream open with no bytes (router-for-me/CLIProxyAPI#6534).
34
+ let sent = false;
35
+ let cancelled = false;
36
+ const body = new ReadableStream({
37
+ pull(controller) {
38
+ if (!sent) { sent = true; controller.enqueue(Buffer.from('data: {"a":1}\n\n')); }
39
+ return new Promise(() => {});
40
+ },
41
+ cancel() { cancelled = true; }
42
+ });
43
+ const values = [];
44
+ await assert.rejects(async () => {
45
+ for await (const v of readUpstreamPayloads({ body }, { idleMs: 50 })) values.push(v);
46
+ }, /sent nothing for/);
47
+ assert.deepEqual(values, [{ a: 1 }]);
48
+ assert.ok(cancelled, 'the silent upstream body is cancelled');
49
+ });
@@ -1,8 +1,20 @@
1
1
  // SSE dispatches one event at a blank line; data fields are joined with newlines.
2
2
  // Vertex also sends bare JSON lines. Keep both framings without parsing data fields separately.
3
3
  // `state.done` turns true on an OpenAI [DONE] line, which ends a stream cleanly even without a finish reason.
4
+ // A stream that sends no byte for `state.idleMs` ends with an error: a dead HTTP/2 session can hold it open
5
+ // with no bytes, and the default limit of the HTTP client is not this module's to rely on.
6
+ export const UPSTREAM_IDLE_MS = Number(process.env.LLM_SWITCHER_UPSTREAM_IDLE_MS) || 300000;
7
+
4
8
  export async function* readUpstreamPayloads(upstreamRes, state = {}) {
5
9
  const reader = upstreamRes.body.getReader();
10
+ const idleMs = state.idleMs || UPSTREAM_IDLE_MS;
11
+ const read = () => {
12
+ let timer;
13
+ const idle = new Promise((_, reject) => {
14
+ timer = setTimeout(() => reject(new Error(`upstream stream sent nothing for ${Math.round(idleMs / 1000)}s`)), idleMs);
15
+ });
16
+ return Promise.race([reader.read(), idle]).finally(() => clearTimeout(timer));
17
+ };
6
18
  const decoder = new TextDecoder();
7
19
  let buffer = '', data = [], finished = false;
8
20
  const parse = value => {
@@ -20,7 +32,7 @@ export async function* readUpstreamPayloads(upstreamRes, state = {}) {
20
32
  }
21
33
  try {
22
34
  while (true) {
23
- const chunk = await reader.read();
35
+ const chunk = await read();
24
36
  if (chunk.done) { finished = true; buffer += decoder.decode(); break; }
25
37
  buffer += decoder.decode(chunk.value, { stream: true });
26
38
  let newline;