llm-switcher 1.6.16 → 1.6.17
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 +5 -0
- package/formats.mjs +16 -3
- package/package.json +1 -1
- package/tests/formats.test.mjs +88 -0
- package/tests/upstream-stream.test.mjs +23 -1
- package/upstream-stream.mjs +16 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog — LLM Switcher
|
|
2
2
|
|
|
3
|
+
## Release 1.6.17 — 2026-10-13
|
|
4
|
+
|
|
5
|
+
- **Documents in a tool result.** A document with a text source, a content source or a URL goes to the model as its text or its URL. Before 1.6.17, it went as a bare `[document]` label. Only base64 data becomes a label, for example `[file: a.pdf]`.
|
|
6
|
+
- **The upstream idle limit.** `LLM_SWITCHER_UPSTREAM_IDLE_MS` must be a positive number. The proxy uses 300000 for any other value, and it writes a warning. A value above 2147483647 becomes 2147483647. Before 1.6.17, a negative, infinite or too large value ended every stream after 1 ms. The limit counts from the response headers. It applies to the body only, and an SSE comment line resets it.
|
|
7
|
+
|
|
3
8
|
## Release 1.6.16 — 2026-10-13
|
|
4
9
|
|
|
5
10
|
- **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.
|
package/formats.mjs
CHANGED
|
@@ -697,9 +697,22 @@ function responsesOutputImages(out) {
|
|
|
697
697
|
// A file or document block carries its bytes as base64; stringified into text it costs millions of tokens
|
|
698
698
|
// (The-NeXT-AI/ai-gateway#28), so it is named instead. Null for any other block.
|
|
699
699
|
function mediaPlaceholder(b) {
|
|
700
|
-
|
|
701
|
-
if (
|
|
702
|
-
|
|
700
|
+
const src = b?.source;
|
|
701
|
+
if (src?.type === 'base64' || typeof b?.file_data === 'string') {
|
|
702
|
+
if (b.type === 'input_file') return `[file: ${b.filename || b.file_id || 'attachment'}]`;
|
|
703
|
+
if (b.type === 'document') return `[document${b.title ? `: ${b.title}` : ''}]`;
|
|
704
|
+
return `[${b.type || 'attachment'}]`;
|
|
705
|
+
}
|
|
706
|
+
// Not base64: the content itself goes on, as text the model can read.
|
|
707
|
+
if (b?.type === 'document' && src?.type === 'text' && typeof src.data === 'string') return src.data;
|
|
708
|
+
if (b?.type === 'document' && src?.type === 'content') {
|
|
709
|
+
if (typeof src.content === 'string') return src.content;
|
|
710
|
+
if (Array.isArray(src.content)) {
|
|
711
|
+
return src.content.map(c => (c?.type === 'text' ? c.text || '' : c?.type === 'image' ? '[image omitted]' : '')).filter(Boolean).join('\n');
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
if (b?.type === 'document' && src?.type === 'url' && src.url) return `[document: ${src.url}]`;
|
|
715
|
+
if (b?.type === 'input_file' && b.file_url) return `[file: ${b.file_url}]`;
|
|
703
716
|
return null;
|
|
704
717
|
}
|
|
705
718
|
|
package/package.json
CHANGED
package/tests/formats.test.mjs
CHANGED
|
@@ -1754,3 +1754,91 @@ test('irToAnthropicBody: a whitespace-only text block is blank too', () => {
|
|
|
1754
1754
|
const body = irToAnthropicBody(ir, 'claude');
|
|
1755
1755
|
assert.ok(body.messages.every(m => m.content.every(b => b.type !== 'text' || b.text.trim())), JSON.stringify(body.messages));
|
|
1756
1756
|
});
|
|
1757
|
+
|
|
1758
|
+
// ---- roundtable ruling 2026-10-13: media in tool results, held notices ----
|
|
1759
|
+
|
|
1760
|
+
const BLOB = 'QUFB'.repeat(2000);
|
|
1761
|
+
const MISSING_TOOL_RESULT = '[Tool result unavailable: it was removed from the conversation history]';
|
|
1762
|
+
function anthropicToolText(content) {
|
|
1763
|
+
const ir = anthropicToIR({ model: 'x', messages: [
|
|
1764
|
+
{ role: 'assistant', content: [{ type: 'tool_use', id: 't1', name: 'read', input: {} }] },
|
|
1765
|
+
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 't1', content }] }
|
|
1766
|
+
] });
|
|
1767
|
+
return ir.messages.find(m => m.role === 'tool').content;
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
test('a tool-result document keeps its text, its URL and its label; only base64 becomes a placeholder', () => {
|
|
1771
|
+
assert.match(anthropicToolText([{ type: 'document', source: { type: 'text', media_type: 'text/plain', data: 'hello doc' } }]), /hello doc/);
|
|
1772
|
+
const content = anthropicToolText([{ type: 'document', source: { type: 'content', content: [
|
|
1773
|
+
{ type: 'text', text: 'part one' }, { type: 'image', source: { type: 'base64', media_type: 'image/png', data: BLOB } }] } }]);
|
|
1774
|
+
assert.match(content, /part one/);
|
|
1775
|
+
assert.match(content, /\[image omitted\]/);
|
|
1776
|
+
assert.ok(!content.includes(BLOB));
|
|
1777
|
+
assert.equal(anthropicToolText([{ type: 'document', source: { type: 'content', content: 'plain string' } }]), 'plain string');
|
|
1778
|
+
assert.match(anthropicToolText([{ type: 'document', source: { type: 'url', url: 'https://example.com/a.pdf' } }]), /https:\/\/example\.com\/a\.pdf/);
|
|
1779
|
+
assert.match(anthropicToolText([{ type: 'input_file', file_url: 'https://example.com/f.pdf' }]), /https:\/\/example\.com\/f\.pdf/);
|
|
1780
|
+
assert.equal(anthropicToolText([{ type: 'input_file', filename: 'a.pdf', file_data: `data:application/pdf;base64,${BLOB}` }]), '[file: a.pdf]');
|
|
1781
|
+
assert.equal(anthropicToolText([{ type: 'document', title: 'T', source: { type: 'base64', media_type: 'application/pdf', data: BLOB } }]), '[document: T]');
|
|
1782
|
+
assert.ok(!anthropicToolText([{ type: 'document', source: { type: 'url' } }]).includes('[document: undefined]'));
|
|
1783
|
+
});
|
|
1784
|
+
|
|
1785
|
+
test('an orphan tool result turned into text keeps a text document', () => {
|
|
1786
|
+
const { payload } = healAnthropicPayload({ model: 'x', max_tokens: 10, messages: [
|
|
1787
|
+
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'orphan', content: [{ type: 'document', source: { type: 'text', data: 'hello doc' } }] }] }
|
|
1788
|
+
] });
|
|
1789
|
+
assert.match(JSON.stringify(payload.messages), /hello doc/);
|
|
1790
|
+
});
|
|
1791
|
+
|
|
1792
|
+
function chatShape(input) {
|
|
1793
|
+
return irToChatBody(responsesToIR({ model: 'x', input }), 'gpt-4o').messages
|
|
1794
|
+
.map(m => (m.role === 'tool' ? `tool:${m.content}` : m.role === 'user' ? `user:${typeof m.content === 'string' ? m.content : JSON.stringify(m.content)}` : m.role));
|
|
1795
|
+
}
|
|
1796
|
+
const callAB = [
|
|
1797
|
+
{ type: 'function_call', call_id: 'c1', name: 'a', arguments: '{}' },
|
|
1798
|
+
{ type: 'function_call', call_id: 'c2', name: 'b', arguments: '{}' }
|
|
1799
|
+
];
|
|
1800
|
+
|
|
1801
|
+
test('healToolPairs: a held notice follows the placeholder of an output that never came', () => {
|
|
1802
|
+
const missing = chatShape([...callAB, { type: 'function_call_output', call_id: 'c1', output: 'out1' }, { role: 'user', content: 'notice' }]);
|
|
1803
|
+
assert.deepEqual(missing.slice(-4), ['assistant', 'tool:out1', `tool:${MISSING_TOOL_RESULT}`, 'user:notice']);
|
|
1804
|
+
const later = chatShape([...callAB, { type: 'function_call_output', call_id: 'c1', output: 'out1' }, { role: 'user', content: 'notice' },
|
|
1805
|
+
{ type: 'message', role: 'assistant', content: [{ type: 'output_text', text: 'next turn' }] }]);
|
|
1806
|
+
assert.deepEqual(later.slice(-2), ['user:notice', 'assistant']);
|
|
1807
|
+
const two = chatShape([...callAB, { role: 'user', content: 'notice1' }, { type: 'function_call_output', call_id: 'c1', output: 'out1' }, { role: 'user', content: 'notice2' }]);
|
|
1808
|
+
assert.deepEqual(two.slice(-5), ['assistant', 'tool:out1', `tool:${MISSING_TOOL_RESULT}`, 'user:notice1', 'user:notice2']);
|
|
1809
|
+
});
|
|
1810
|
+
|
|
1811
|
+
test('placeholders and URLs are exact on every route that turns a tool result into text', () => {
|
|
1812
|
+
assert.equal(anthropicToolText([{ type: 'document', source: { type: 'base64', media_type: 'application/pdf', data: BLOB } }]), '[document]');
|
|
1813
|
+
assert.equal(anthropicToolText([{ type: 'document', source: { type: 'url', url: 'https://example.com/a.pdf' } }]), '[document: https://example.com/a.pdf]');
|
|
1814
|
+
assert.equal(anthropicToolText([{ type: 'input_file', file_url: 'https://example.com/f.pdf' }]), '[file: https://example.com/f.pdf]');
|
|
1815
|
+
const orphanText = (content) => {
|
|
1816
|
+
const { payload } = healAnthropicPayload({ model: 'x', max_tokens: 10, messages: [
|
|
1817
|
+
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'orphan', content }] }] });
|
|
1818
|
+
return JSON.stringify(payload.messages);
|
|
1819
|
+
};
|
|
1820
|
+
const doc = orphanText([{ type: 'document', title: 'T', source: { type: 'base64', media_type: 'application/pdf', data: BLOB } }]);
|
|
1821
|
+
assert.ok(!doc.includes(BLOB) && doc.includes('[document: T]'), doc.slice(0, 300));
|
|
1822
|
+
const file = orphanText([{ type: 'input_file', filename: 'a.pdf', file_data: `data:application/pdf;base64,${BLOB}` }]);
|
|
1823
|
+
assert.ok(!file.includes(BLOB) && file.includes('[file: a.pdf]'), file.slice(0, 300));
|
|
1824
|
+
});
|
|
1825
|
+
|
|
1826
|
+
test('the image of a Responses tool output reaches the chat and Anthropic bodies', () => {
|
|
1827
|
+
for (const image_url of ['data:image/png;base64,AAAA', { url: 'data:image/png;base64,AAAA' }]) {
|
|
1828
|
+
const ir = responsesToIR({ model: 'x', input: [
|
|
1829
|
+
{ type: 'function_call', call_id: 'c1', name: 'shot', arguments: '{}' },
|
|
1830
|
+
{ type: 'function_call_output', call_id: 'c1', output: [{ type: 'input_image', image_url }] }] });
|
|
1831
|
+
const chat = JSON.stringify(irToChatBody(ir, 'gpt-4o').messages);
|
|
1832
|
+
assert.match(chat, /data:image\/png;base64,AAAA/);
|
|
1833
|
+
const anth = JSON.stringify(irToAnthropicBody(ir, 'claude-x').messages);
|
|
1834
|
+
assert.match(anth, /"type":"image"/);
|
|
1835
|
+
}
|
|
1836
|
+
});
|
|
1837
|
+
|
|
1838
|
+
test('healToolPairs: an orphan output and a held notice in one batch come out orphan first, then the notice', () => {
|
|
1839
|
+
const shape = chatShape([...callAB,
|
|
1840
|
+
{ type: 'function_call_output', call_id: 'zz', output: 'stray' },
|
|
1841
|
+
{ role: 'user', content: 'notice' },
|
|
1842
|
+
{ type: 'function_call_output', call_id: 'c1', output: 'out1' }]);
|
|
1843
|
+
assert.deepEqual(shape.slice(-5), ['assistant', 'tool:out1', `tool:${MISSING_TOOL_RESULT}`, 'user:[Tool Result (zz)]: stray', 'user:notice']);
|
|
1844
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { test } from 'node:test';
|
|
2
2
|
import assert from 'node:assert/strict';
|
|
3
|
-
import { readUpstreamPayloads } from '../upstream-stream.mjs';
|
|
3
|
+
import { readUpstreamPayloads, parseIdleMs } from '../upstream-stream.mjs';
|
|
4
4
|
|
|
5
5
|
async function collect(text, widths = [1, 3, 7]) {
|
|
6
6
|
const bytes = Buffer.from(text); let offset = 0, i = 0;
|
|
@@ -47,3 +47,25 @@ test('an upstream that goes silent mid-stream ends with an error after the idle
|
|
|
47
47
|
assert.deepEqual(values, [{ a: 1 }]);
|
|
48
48
|
assert.ok(cancelled, 'the silent upstream body is cancelled');
|
|
49
49
|
});
|
|
50
|
+
|
|
51
|
+
test('parseIdleMs keeps the idle limit inside what setTimeout accepts', () => {
|
|
52
|
+
// Node fires a timer of a negative, infinite or > 2^31-1 delay after 1 ms.
|
|
53
|
+
assert.equal(parseIdleMs('3000000000'), 2147483647);
|
|
54
|
+
for (const bad of ['-5', 'Infinity', 'abc', '0', '0.5']) assert.equal(parseIdleMs(bad), 300000, bad);
|
|
55
|
+
assert.equal(parseIdleMs('120000'), 120000);
|
|
56
|
+
assert.equal(parseIdleMs(undefined), 300000);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('the idle limit counts the gap between reads, not the whole stream', async () => {
|
|
60
|
+
let n = 0;
|
|
61
|
+
const body = new ReadableStream({
|
|
62
|
+
async pull(controller) {
|
|
63
|
+
if (n === 5) return controller.close();
|
|
64
|
+
await new Promise(r => setTimeout(r, 60));
|
|
65
|
+
controller.enqueue(Buffer.from(`data: {"n":${n++}}\n\n`));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const values = [];
|
|
69
|
+
for await (const v of readUpstreamPayloads({ body }, { idleMs: 200 })) values.push(v.n);
|
|
70
|
+
assert.deepEqual(values, [0, 1, 2, 3, 4]);
|
|
71
|
+
});
|
package/upstream-stream.mjs
CHANGED
|
@@ -3,11 +3,25 @@
|
|
|
3
3
|
// `state.done` turns true on an OpenAI [DONE] line, which ends a stream cleanly even without a finish reason.
|
|
4
4
|
// A stream that sends no byte for `state.idleMs` ends with an error: a dead HTTP/2 session can hold it open
|
|
5
5
|
// with no bytes, and the default limit of the HTTP client is not this module's to rely on.
|
|
6
|
-
|
|
6
|
+
const DEFAULT_IDLE_MS = 300000;
|
|
7
|
+
const MAX_TIMER_MS = 2147483647;
|
|
8
|
+
|
|
9
|
+
// The idle limit as setTimeout takes it: Node fires a negative, infinite or larger delay after 1 ms.
|
|
10
|
+
export function parseIdleMs(value) {
|
|
11
|
+
if (value === undefined || value === null || value === '') return DEFAULT_IDLE_MS;
|
|
12
|
+
const n = Number(value);
|
|
13
|
+
if (!Number.isFinite(n) || n < 1) {
|
|
14
|
+
console.warn(`[llm-switcher] upstream idle limit ${JSON.stringify(value)} is not a positive number; using ${DEFAULT_IDLE_MS} ms`);
|
|
15
|
+
return DEFAULT_IDLE_MS;
|
|
16
|
+
}
|
|
17
|
+
return Math.min(n, MAX_TIMER_MS);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const UPSTREAM_IDLE_MS = parseIdleMs(process.env.LLM_SWITCHER_UPSTREAM_IDLE_MS);
|
|
7
21
|
|
|
8
22
|
export async function* readUpstreamPayloads(upstreamRes, state = {}) {
|
|
9
23
|
const reader = upstreamRes.body.getReader();
|
|
10
|
-
const idleMs = state.idleMs
|
|
24
|
+
const idleMs = state.idleMs === undefined ? UPSTREAM_IDLE_MS : parseIdleMs(state.idleMs);
|
|
11
25
|
const read = () => {
|
|
12
26
|
let timer;
|
|
13
27
|
const idle = new Promise((_, reject) => {
|