llm-switcher 1.3.7 → 1.4.0
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 +24 -0
- package/README.md +25 -17
- package/README.vi.md +24 -13
- package/config.example.json +0 -11
- package/docs/TOKEN-OPTIMIZER-INTEROP.md +110 -110
- package/docs/response-matrix.json +1130 -1130
- package/docs/tool-vocabulary.md +236 -0
- package/formats.mjs +277 -55
- package/package.json +1 -1
- package/proxy.mjs +55 -19
- package/service.mjs +6 -3
- package/skills/llm-switcher/SKILL.md +93 -93
- package/state.mjs +23 -91
- package/switch +0 -0
- package/switch.cmd +2 -2
- package/switch.mjs +2 -2
- package/tests/dashboard.e2e.test.mjs +10 -13
- package/tests/formats.test.mjs +98 -1
- package/tests/gateway.e2e.test.mjs +23 -9
- package/tests/helpers.mjs +24 -24
- package/tests/live-optimizer-interop.mjs +205 -205
- package/tests/service.test.mjs +6 -4
- package/tests/state.test.mjs +6 -7
- package/tests/tool-repair.test.mjs +280 -0
- package/tests/ui.test.mjs +22 -15
- package/tests/update.test.mjs +16 -0
- package/tests/windows.test.mjs +124 -0
- package/toolvocab.mjs +272 -0
- package/ui.html +32 -76
- package/update.mjs +13 -5
- package/windows.mjs +141 -0
|
@@ -1,205 +1,205 @@
|
|
|
1
|
-
// LIVE test: requires a running gateway (switch on) and a real upstream (costs tokens).
|
|
2
|
-
// Offline test, no network needed: npm test
|
|
3
|
-
const GATEWAY_PORT = Number(process.env.LLM_SWITCHER_PORT) || 3456;
|
|
4
|
-
const BASE_URL = `http://127.0.0.1:${GATEWAY_PORT}`;
|
|
5
|
-
|
|
6
|
-
console.log('================================================================');
|
|
7
|
-
console.log(' LLM SWITCHER — TOKEN OPTIMIZER INTEROPERABILITY TEST SUITE ');
|
|
8
|
-
console.log(' Simulating failure modes from Headroom, RTK, and Ponytail ');
|
|
9
|
-
console.log('================================================================\n');
|
|
10
|
-
|
|
11
|
-
let passed = 0;
|
|
12
|
-
let failed = 0;
|
|
13
|
-
|
|
14
|
-
async function runTest(name, fn) {
|
|
15
|
-
process.stdout.write(`[TEST] ${name}... `);
|
|
16
|
-
const t0 = Date.now();
|
|
17
|
-
try {
|
|
18
|
-
const detail = await fn();
|
|
19
|
-
console.log(`PASS (${Date.now() - t0}ms)`);
|
|
20
|
-
if (detail) console.log(` ↳ ${detail}`);
|
|
21
|
-
passed++;
|
|
22
|
-
} catch (err) {
|
|
23
|
-
console.log(`FAIL (${Date.now() - t0}ms)`);
|
|
24
|
-
console.log(` ↳ ERROR: ${err.message}`);
|
|
25
|
-
failed++;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// -----------------------------------------------------------------------------
|
|
30
|
-
// TEST 1: Headroom Failure Mode — Orphaned tool_result
|
|
31
|
-
// When Headroom prunes history to save tokens, it drops the assistant tool_use turn.
|
|
32
|
-
// Anthropic strictly throws: 400 invalid_request_error: 'tool_use_id does not correspond to any tool_use'
|
|
33
|
-
// LLM Switcher Healer Engine: Repairs orphaned result into context text -> 200 OK
|
|
34
|
-
// -----------------------------------------------------------------------------
|
|
35
|
-
await runTest('Headroom Simulation: Orphaned tool_result turn', async () => {
|
|
36
|
-
const payload = {
|
|
37
|
-
model: 'claude-opus-4-6',
|
|
38
|
-
max_tokens: 40,
|
|
39
|
-
messages: [
|
|
40
|
-
{ role: 'user', content: 'Context turn before pruning' },
|
|
41
|
-
{
|
|
42
|
-
role: 'user',
|
|
43
|
-
content: [
|
|
44
|
-
{ type: 'tool_result', tool_use_id: 'headroom_pruned_call_99', content: 'Database query result: 42 rows found' },
|
|
45
|
-
{ type: 'text', text: 'Reply: healed successfully' }
|
|
46
|
-
]
|
|
47
|
-
}
|
|
48
|
-
],
|
|
49
|
-
stream: false
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const res = await fetch(`${BASE_URL}/v1/messages`, {
|
|
53
|
-
method: 'POST',
|
|
54
|
-
headers: { 'Content-Type': 'application/json', 'anthropic-version': '2023-06-01' },
|
|
55
|
-
body: JSON.stringify(payload)
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
if (res.status !== 200) {
|
|
59
|
-
const err = await res.text();
|
|
60
|
-
throw new Error(`Expected HTTP 200, got ${res.status}: ${err}`);
|
|
61
|
-
}
|
|
62
|
-
const json = await res.json();
|
|
63
|
-
const text = json.content?.map(c => c.text).join('') || '';
|
|
64
|
-
return `Healed orphaned tool_result. Response HTTP 200: "${text.slice(0, 50).replace(/\n/g, ' ')}..."`;
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// -----------------------------------------------------------------------------
|
|
68
|
-
// TEST 2: Headroom Failure Mode — Consecutive User turns (Roles must alternate)
|
|
69
|
-
// When Headroom collapses history, multiple user turns occur back-to-back.
|
|
70
|
-
// Anthropic strictly throws: 400 invalid_request_error: 'roles must alternate'
|
|
71
|
-
// LLM Switcher Healer Engine: Merges consecutive same-role turns -> 200 OK
|
|
72
|
-
// -----------------------------------------------------------------------------
|
|
73
|
-
await runTest('Headroom Simulation: Consecutive User turns (Role alternation violation)', async () => {
|
|
74
|
-
const payload = {
|
|
75
|
-
model: 'claude-opus-4-6',
|
|
76
|
-
max_tokens: 30,
|
|
77
|
-
messages: [
|
|
78
|
-
{ role: 'user', content: 'User message turn 1' },
|
|
79
|
-
{ role: 'user', content: 'User message turn 2 (no assistant in between)' },
|
|
80
|
-
{ role: 'user', content: 'User message turn 3: reply pong' }
|
|
81
|
-
],
|
|
82
|
-
stream: false
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const res = await fetch(`${BASE_URL}/v1/messages`, {
|
|
86
|
-
method: 'POST',
|
|
87
|
-
headers: { 'Content-Type': 'application/json', 'anthropic-version': '2023-06-01' },
|
|
88
|
-
body: JSON.stringify(payload)
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
if (res.status !== 200) {
|
|
92
|
-
const err = await res.text();
|
|
93
|
-
throw new Error(`Expected HTTP 200, got ${res.status}: ${err}`);
|
|
94
|
-
}
|
|
95
|
-
const json = await res.json();
|
|
96
|
-
return `Merged consecutive turns seamlessly. Stop reason: ${json.stop_reason}`;
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// -----------------------------------------------------------------------------
|
|
100
|
-
// TEST 3: Headroom / RTK Failure Mode — Stripped Thinking Parameter
|
|
101
|
-
// Optimizer stripped the 'thinking' object to reduce tokens.
|
|
102
|
-
// LLM Switcher Thinking Guard: Detects reasoning model (ag/claude-opus-4-6-thinking)
|
|
103
|
-
// and restores thinking.budget_tokens automatically -> thinking_delta emitted!
|
|
104
|
-
// -----------------------------------------------------------------------------
|
|
105
|
-
await runTest('Thinking Guard: Restoring stripped thinking parameter on reasoning models', async () => {
|
|
106
|
-
const payload = {
|
|
107
|
-
model: 'claude-opus-4-6',
|
|
108
|
-
max_tokens: 200,
|
|
109
|
-
// Note: NO thinking parameter included (simulating optimizer stripping it)
|
|
110
|
-
messages: [
|
|
111
|
-
{ role: 'user', content: 'Solve step by step: what is 17 * 23? Show reasoning.' }
|
|
112
|
-
],
|
|
113
|
-
stream: true
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const res = await fetch(`${BASE_URL}/v1/messages`, {
|
|
117
|
-
method: 'POST',
|
|
118
|
-
headers: { 'Content-Type': 'application/json', 'anthropic-version': '2023-06-01' },
|
|
119
|
-
body: JSON.stringify(payload)
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
if (res.status !== 200) {
|
|
123
|
-
throw new Error(`Expected HTTP 200, got ${res.status}`);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const text = await res.text();
|
|
127
|
-
const hasThinkingDelta = text.includes('thinking_delta');
|
|
128
|
-
const hasSignatureDelta = text.includes('signature_delta');
|
|
129
|
-
const hasTextDelta = text.includes('text_delta');
|
|
130
|
-
|
|
131
|
-
if (!hasThinkingDelta) {
|
|
132
|
-
throw new Error('Thinking Guard failed: thinking_delta was NOT emitted in stream!');
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return `Automatically restored thinking: thinking_delta=${hasThinkingDelta}, signature_delta=${hasSignatureDelta}, text_delta=${hasTextDelta}`;
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
// -----------------------------------------------------------------------------
|
|
139
|
-
// TEST 4: RTK & Intermediary Headers Passthrough
|
|
140
|
-
// RTK / tracing tools inject headers: x-rtk-version, traceparent, x-request-id.
|
|
141
|
-
// LLM Switcher: Transparently preserves all tracking headers without rejection.
|
|
142
|
-
// -----------------------------------------------------------------------------
|
|
143
|
-
await runTest('RTK Intermediary: Custom headers and traceparent passthrough', async () => {
|
|
144
|
-
const payload = {
|
|
145
|
-
model: 'claude-opus-4-6',
|
|
146
|
-
max_tokens: 20,
|
|
147
|
-
messages: [{ role: 'user', content: 'ping' }],
|
|
148
|
-
stream: false
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
const res = await fetch(`${BASE_URL}/v1/messages`, {
|
|
152
|
-
method: 'POST',
|
|
153
|
-
headers: {
|
|
154
|
-
'Content-Type': 'application/json',
|
|
155
|
-
'anthropic-version': '2023-06-01',
|
|
156
|
-
'x-rtk-version': '0.37.2',
|
|
157
|
-
'x-optimizer-id': 'rtk-cli-hook',
|
|
158
|
-
'traceparent': '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
|
|
159
|
-
},
|
|
160
|
-
body: JSON.stringify(payload)
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
if (res.status !== 200) {
|
|
164
|
-
throw new Error(`Expected HTTP 200, got ${res.status}`);
|
|
165
|
-
}
|
|
166
|
-
return `Headers accepted cleanly with HTTP 200 OK`;
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
// -----------------------------------------------------------------------------
|
|
170
|
-
// TEST 5: OpenAI Chat Healer Mode — Orphaned Tool Result in Chat Completions
|
|
171
|
-
// Same test but over /v1/chat/completions: tool role message without prior tool_calls assistant message.
|
|
172
|
-
// OpenAI API strictly throws: 400 'messages with role tool must be a response to a preceding message with tool_calls'
|
|
173
|
-
// LLM Switcher: Converts orphaned tool to user text message -> 200 OK
|
|
174
|
-
// -----------------------------------------------------------------------------
|
|
175
|
-
await runTest('OpenAI Chat Healer: Orphaned role tool without preceding assistant tool_calls', async () => {
|
|
176
|
-
const payload = {
|
|
177
|
-
model: 'ag/gpt-oss-120b-medium',
|
|
178
|
-
max_tokens: 30,
|
|
179
|
-
messages: [
|
|
180
|
-
{ role: 'user', content: 'Turn 1 context' },
|
|
181
|
-
{ role: 'tool', tool_call_id: 'orphaned_chat_call_77', content: 'Tool execution logs: OK' },
|
|
182
|
-
{ role: 'user', content: 'reply pong' }
|
|
183
|
-
],
|
|
184
|
-
stream: false
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
const res = await fetch(`${BASE_URL}/v1/chat/completions`, {
|
|
188
|
-
method: 'POST',
|
|
189
|
-
headers: { 'Content-Type': 'application/json' },
|
|
190
|
-
body: JSON.stringify(payload)
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
if (res.status !== 200) {
|
|
194
|
-
const err = await res.text();
|
|
195
|
-
throw new Error(`Expected HTTP 200, got ${res.status}: ${err}`);
|
|
196
|
-
}
|
|
197
|
-
const json = await res.json();
|
|
198
|
-
return `Chat Healer rescued orphaned tool role. Stop reason: ${json.choices?.[0]?.finish_reason}`;
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
console.log('\n================================================================');
|
|
202
|
-
console.log(` TEST RESULTS: ${passed} PASSED / ${failed} FAILED `);
|
|
203
|
-
console.log('================================================================\n');
|
|
204
|
-
|
|
205
|
-
process.exit(failed ? 1 : 0);
|
|
1
|
+
// LIVE test: requires a running gateway (switch on) and a real upstream (costs tokens).
|
|
2
|
+
// Offline test, no network needed: npm test
|
|
3
|
+
const GATEWAY_PORT = Number(process.env.LLM_SWITCHER_PORT) || 3456;
|
|
4
|
+
const BASE_URL = `http://127.0.0.1:${GATEWAY_PORT}`;
|
|
5
|
+
|
|
6
|
+
console.log('================================================================');
|
|
7
|
+
console.log(' LLM SWITCHER — TOKEN OPTIMIZER INTEROPERABILITY TEST SUITE ');
|
|
8
|
+
console.log(' Simulating failure modes from Headroom, RTK, and Ponytail ');
|
|
9
|
+
console.log('================================================================\n');
|
|
10
|
+
|
|
11
|
+
let passed = 0;
|
|
12
|
+
let failed = 0;
|
|
13
|
+
|
|
14
|
+
async function runTest(name, fn) {
|
|
15
|
+
process.stdout.write(`[TEST] ${name}... `);
|
|
16
|
+
const t0 = Date.now();
|
|
17
|
+
try {
|
|
18
|
+
const detail = await fn();
|
|
19
|
+
console.log(`PASS (${Date.now() - t0}ms)`);
|
|
20
|
+
if (detail) console.log(` ↳ ${detail}`);
|
|
21
|
+
passed++;
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.log(`FAIL (${Date.now() - t0}ms)`);
|
|
24
|
+
console.log(` ↳ ERROR: ${err.message}`);
|
|
25
|
+
failed++;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// -----------------------------------------------------------------------------
|
|
30
|
+
// TEST 1: Headroom Failure Mode — Orphaned tool_result
|
|
31
|
+
// When Headroom prunes history to save tokens, it drops the assistant tool_use turn.
|
|
32
|
+
// Anthropic strictly throws: 400 invalid_request_error: 'tool_use_id does not correspond to any tool_use'
|
|
33
|
+
// LLM Switcher Healer Engine: Repairs orphaned result into context text -> 200 OK
|
|
34
|
+
// -----------------------------------------------------------------------------
|
|
35
|
+
await runTest('Headroom Simulation: Orphaned tool_result turn', async () => {
|
|
36
|
+
const payload = {
|
|
37
|
+
model: 'claude-opus-4-6',
|
|
38
|
+
max_tokens: 40,
|
|
39
|
+
messages: [
|
|
40
|
+
{ role: 'user', content: 'Context turn before pruning' },
|
|
41
|
+
{
|
|
42
|
+
role: 'user',
|
|
43
|
+
content: [
|
|
44
|
+
{ type: 'tool_result', tool_use_id: 'headroom_pruned_call_99', content: 'Database query result: 42 rows found' },
|
|
45
|
+
{ type: 'text', text: 'Reply: healed successfully' }
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
stream: false
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const res = await fetch(`${BASE_URL}/v1/messages`, {
|
|
53
|
+
method: 'POST',
|
|
54
|
+
headers: { 'Content-Type': 'application/json', 'anthropic-version': '2023-06-01' },
|
|
55
|
+
body: JSON.stringify(payload)
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (res.status !== 200) {
|
|
59
|
+
const err = await res.text();
|
|
60
|
+
throw new Error(`Expected HTTP 200, got ${res.status}: ${err}`);
|
|
61
|
+
}
|
|
62
|
+
const json = await res.json();
|
|
63
|
+
const text = json.content?.map(c => c.text).join('') || '';
|
|
64
|
+
return `Healed orphaned tool_result. Response HTTP 200: "${text.slice(0, 50).replace(/\n/g, ' ')}..."`;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// -----------------------------------------------------------------------------
|
|
68
|
+
// TEST 2: Headroom Failure Mode — Consecutive User turns (Roles must alternate)
|
|
69
|
+
// When Headroom collapses history, multiple user turns occur back-to-back.
|
|
70
|
+
// Anthropic strictly throws: 400 invalid_request_error: 'roles must alternate'
|
|
71
|
+
// LLM Switcher Healer Engine: Merges consecutive same-role turns -> 200 OK
|
|
72
|
+
// -----------------------------------------------------------------------------
|
|
73
|
+
await runTest('Headroom Simulation: Consecutive User turns (Role alternation violation)', async () => {
|
|
74
|
+
const payload = {
|
|
75
|
+
model: 'claude-opus-4-6',
|
|
76
|
+
max_tokens: 30,
|
|
77
|
+
messages: [
|
|
78
|
+
{ role: 'user', content: 'User message turn 1' },
|
|
79
|
+
{ role: 'user', content: 'User message turn 2 (no assistant in between)' },
|
|
80
|
+
{ role: 'user', content: 'User message turn 3: reply pong' }
|
|
81
|
+
],
|
|
82
|
+
stream: false
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const res = await fetch(`${BASE_URL}/v1/messages`, {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
headers: { 'Content-Type': 'application/json', 'anthropic-version': '2023-06-01' },
|
|
88
|
+
body: JSON.stringify(payload)
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
if (res.status !== 200) {
|
|
92
|
+
const err = await res.text();
|
|
93
|
+
throw new Error(`Expected HTTP 200, got ${res.status}: ${err}`);
|
|
94
|
+
}
|
|
95
|
+
const json = await res.json();
|
|
96
|
+
return `Merged consecutive turns seamlessly. Stop reason: ${json.stop_reason}`;
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// -----------------------------------------------------------------------------
|
|
100
|
+
// TEST 3: Headroom / RTK Failure Mode — Stripped Thinking Parameter
|
|
101
|
+
// Optimizer stripped the 'thinking' object to reduce tokens.
|
|
102
|
+
// LLM Switcher Thinking Guard: Detects reasoning model (ag/claude-opus-4-6-thinking)
|
|
103
|
+
// and restores thinking.budget_tokens automatically -> thinking_delta emitted!
|
|
104
|
+
// -----------------------------------------------------------------------------
|
|
105
|
+
await runTest('Thinking Guard: Restoring stripped thinking parameter on reasoning models', async () => {
|
|
106
|
+
const payload = {
|
|
107
|
+
model: 'claude-opus-4-6',
|
|
108
|
+
max_tokens: 200,
|
|
109
|
+
// Note: NO thinking parameter included (simulating optimizer stripping it)
|
|
110
|
+
messages: [
|
|
111
|
+
{ role: 'user', content: 'Solve step by step: what is 17 * 23? Show reasoning.' }
|
|
112
|
+
],
|
|
113
|
+
stream: true
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const res = await fetch(`${BASE_URL}/v1/messages`, {
|
|
117
|
+
method: 'POST',
|
|
118
|
+
headers: { 'Content-Type': 'application/json', 'anthropic-version': '2023-06-01' },
|
|
119
|
+
body: JSON.stringify(payload)
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
if (res.status !== 200) {
|
|
123
|
+
throw new Error(`Expected HTTP 200, got ${res.status}`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const text = await res.text();
|
|
127
|
+
const hasThinkingDelta = text.includes('thinking_delta');
|
|
128
|
+
const hasSignatureDelta = text.includes('signature_delta');
|
|
129
|
+
const hasTextDelta = text.includes('text_delta');
|
|
130
|
+
|
|
131
|
+
if (!hasThinkingDelta) {
|
|
132
|
+
throw new Error('Thinking Guard failed: thinking_delta was NOT emitted in stream!');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return `Automatically restored thinking: thinking_delta=${hasThinkingDelta}, signature_delta=${hasSignatureDelta}, text_delta=${hasTextDelta}`;
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// -----------------------------------------------------------------------------
|
|
139
|
+
// TEST 4: RTK & Intermediary Headers Passthrough
|
|
140
|
+
// RTK / tracing tools inject headers: x-rtk-version, traceparent, x-request-id.
|
|
141
|
+
// LLM Switcher: Transparently preserves all tracking headers without rejection.
|
|
142
|
+
// -----------------------------------------------------------------------------
|
|
143
|
+
await runTest('RTK Intermediary: Custom headers and traceparent passthrough', async () => {
|
|
144
|
+
const payload = {
|
|
145
|
+
model: 'claude-opus-4-6',
|
|
146
|
+
max_tokens: 20,
|
|
147
|
+
messages: [{ role: 'user', content: 'ping' }],
|
|
148
|
+
stream: false
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const res = await fetch(`${BASE_URL}/v1/messages`, {
|
|
152
|
+
method: 'POST',
|
|
153
|
+
headers: {
|
|
154
|
+
'Content-Type': 'application/json',
|
|
155
|
+
'anthropic-version': '2023-06-01',
|
|
156
|
+
'x-rtk-version': '0.37.2',
|
|
157
|
+
'x-optimizer-id': 'rtk-cli-hook',
|
|
158
|
+
'traceparent': '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
|
|
159
|
+
},
|
|
160
|
+
body: JSON.stringify(payload)
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
if (res.status !== 200) {
|
|
164
|
+
throw new Error(`Expected HTTP 200, got ${res.status}`);
|
|
165
|
+
}
|
|
166
|
+
return `Headers accepted cleanly with HTTP 200 OK`;
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// -----------------------------------------------------------------------------
|
|
170
|
+
// TEST 5: OpenAI Chat Healer Mode — Orphaned Tool Result in Chat Completions
|
|
171
|
+
// Same test but over /v1/chat/completions: tool role message without prior tool_calls assistant message.
|
|
172
|
+
// OpenAI API strictly throws: 400 'messages with role tool must be a response to a preceding message with tool_calls'
|
|
173
|
+
// LLM Switcher: Converts orphaned tool to user text message -> 200 OK
|
|
174
|
+
// -----------------------------------------------------------------------------
|
|
175
|
+
await runTest('OpenAI Chat Healer: Orphaned role tool without preceding assistant tool_calls', async () => {
|
|
176
|
+
const payload = {
|
|
177
|
+
model: 'ag/gpt-oss-120b-medium',
|
|
178
|
+
max_tokens: 30,
|
|
179
|
+
messages: [
|
|
180
|
+
{ role: 'user', content: 'Turn 1 context' },
|
|
181
|
+
{ role: 'tool', tool_call_id: 'orphaned_chat_call_77', content: 'Tool execution logs: OK' },
|
|
182
|
+
{ role: 'user', content: 'reply pong' }
|
|
183
|
+
],
|
|
184
|
+
stream: false
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const res = await fetch(`${BASE_URL}/v1/chat/completions`, {
|
|
188
|
+
method: 'POST',
|
|
189
|
+
headers: { 'Content-Type': 'application/json' },
|
|
190
|
+
body: JSON.stringify(payload)
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
if (res.status !== 200) {
|
|
194
|
+
const err = await res.text();
|
|
195
|
+
throw new Error(`Expected HTTP 200, got ${res.status}: ${err}`);
|
|
196
|
+
}
|
|
197
|
+
const json = await res.json();
|
|
198
|
+
return `Chat Healer rescued orphaned tool role. Stop reason: ${json.choices?.[0]?.finish_reason}`;
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
console.log('\n================================================================');
|
|
202
|
+
console.log(` TEST RESULTS: ${passed} PASSED / ${failed} FAILED `);
|
|
203
|
+
console.log('================================================================\n');
|
|
204
|
+
|
|
205
|
+
process.exit(failed ? 1 : 0);
|
package/tests/service.test.mjs
CHANGED
|
@@ -28,7 +28,7 @@ test('systemd unit quotes the command and carries the environment', () => {
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
const plist = () => launchdPlist({ nodeBin: '/n', script: '/a&b/proxy.mjs', port: 4001, logPath: '/l<og>', env: [['CLAUDE_CONFIG_DIR', '/x&y']] });
|
|
31
|
-
const taskXml = () => scheduledTaskXml({ nodeBin: 'C:\\Program Files\\nodejs\\node.exe', script: 'C:\\Users\\A & B\\llm-switcher\\proxy.mjs', port: 4002, userId: 'PC\\a&b' });
|
|
31
|
+
const taskXml = () => scheduledTaskXml({ nodeBin: 'C:\\Program Files\\nodejs\\node.exe', script: 'C:\\Users\\A & B\\llm-switcher\\proxy.mjs', port: 4002, userId: 'PC\\a&b', systemRoot: 'C:\\Windows' });
|
|
32
32
|
|
|
33
33
|
test('the plist and the task XML are well-formed', { skip: !HAS_XMLLINT && 'needs xmllint' }, () => {
|
|
34
34
|
execFileSync('xmllint', ['--noout', '-'], { input: plist() });
|
|
@@ -43,8 +43,10 @@ test('launchd plist escapes values and carries the environment', () => {
|
|
|
43
43
|
|
|
44
44
|
test('Windows task XML keeps the command and its arguments apart and has no run-time limit', () => {
|
|
45
45
|
const xml = taskXml();
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
// node.exe is a console program: started directly at logon it opens a terminal window, and closing
|
|
47
|
+
// that window stops the gateway. conhost --headless gives it a console that nobody sees.
|
|
48
|
+
assert.match(xml, /<Command>C:\\Windows\\System32\\conhost.exe<\/Command>/);
|
|
49
|
+
assert.match(xml, /<Arguments>--headless "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\A & B\\llm-switcher\\proxy.mjs" --port 4002<\/Arguments>/);
|
|
48
50
|
assert.match(xml, /<ExecutionTimeLimit>PT0S<\/ExecutionTimeLimit>/);
|
|
49
51
|
assert.match(xml, /<LogonTrigger>\s*<Enabled>true<\/Enabled>\s*<UserId>PC\\a&b<\/UserId>/);
|
|
50
52
|
assert.equal(portFromServiceText(xml), 4002);
|
|
@@ -60,7 +62,7 @@ test('the port is read back from UTF-16 console output, as schtasks /Query /XML
|
|
|
60
62
|
|
|
61
63
|
test('autoupdate flag is placed into task XML, systemd unit, and launchd plist', () => {
|
|
62
64
|
const xml = scheduledTaskXml({ nodeBin: 'node.exe', script: 'proxy.mjs', port: 3456, userId: 'u', autoupdate: true });
|
|
63
|
-
assert.match(xml, /<Arguments
|
|
65
|
+
assert.match(xml, /<Arguments>--headless "node\.exe" "proxy\.mjs" --port 3456 --autoupdate<\/Arguments>/);
|
|
64
66
|
assert.equal(portFromServiceText(xml), 3456);
|
|
65
67
|
|
|
66
68
|
const unit = systemdUnit({ nodeBin: '/usr/bin/node', script: '/opt/proxy.mjs', port: 3456, autoupdate: true });
|
package/tests/state.test.mjs
CHANGED
|
@@ -797,6 +797,7 @@ test('A5: migration splits shared auto profile into -claude and -codex halves wi
|
|
|
797
797
|
name: 'Shared Profile',
|
|
798
798
|
apiKey: 'secret-key',
|
|
799
799
|
defaultModels: { opus: 'opus-model', sonnet: 'sonnet-model', subagent: 'sub-model' },
|
|
800
|
+
// The retired 1M flags are in an old config: the split must not carry them into a half.
|
|
800
801
|
model1M: { opus: true, sonnet: false }
|
|
801
802
|
}
|
|
802
803
|
}
|
|
@@ -819,10 +820,8 @@ test('A5: migration splits shared auto profile into -claude and -codex halves wi
|
|
|
819
820
|
assert.equal(codex.defaultModels.main, 'opus-model');
|
|
820
821
|
assert.equal(codex.defaultModels.review, 'sonnet-model');
|
|
821
822
|
assert.equal(codex.defaultModels.subagent, 'sub-model');
|
|
822
|
-
assert.equal(claude.model1M
|
|
823
|
-
assert.equal(
|
|
824
|
-
assert.equal(codex.model1M.main, true);
|
|
825
|
-
assert.equal(codex.model1M.review, false);
|
|
823
|
+
assert.equal(claude.model1M, undefined, 'the retired 1M flags do not survive the split');
|
|
824
|
+
assert.equal(codex.model1M, undefined);
|
|
826
825
|
});
|
|
827
826
|
|
|
828
827
|
test('A5: R7 row 4 splits profile with Claude/no-tool fields active for responses with slot fallbacks', () => {
|
|
@@ -881,7 +880,7 @@ test('A5: migration preserves legacy "default" key on both halves during split',
|
|
|
881
880
|
assert.equal(res.config.profiles['combo-codex'].defaultModels.default, 'chat-default');
|
|
882
881
|
});
|
|
883
882
|
|
|
884
|
-
test('A5: migration never fills slot whose value is empty string
|
|
883
|
+
test('A5: migration never fills slot whose value is empty string', () => {
|
|
885
884
|
const raw = {
|
|
886
885
|
activeProfile: 'combo',
|
|
887
886
|
profiles: {
|
|
@@ -895,7 +894,7 @@ test('A5: migration never fills slot whose value is empty string or false', () =
|
|
|
895
894
|
const res = migrateConfigInMemory(raw);
|
|
896
895
|
const codex = res.config.profiles['combo-codex'];
|
|
897
896
|
assert.equal(codex.defaultModels.main, '');
|
|
898
|
-
assert.equal(codex.model1M
|
|
897
|
+
assert.equal(codex.model1M, undefined, 'and the retired flags do not come along');
|
|
899
898
|
});
|
|
900
899
|
|
|
901
900
|
test('A5: migration splits renamed command-word profile as <key>-profile-claude and <key>-profile-codex', () => {
|
|
@@ -1610,7 +1609,7 @@ test('each routed tool gets a route line; a tool that is off gets an empty one',
|
|
|
1610
1609
|
assert.match(r.on.claude, /\bcl\b/, 'the line names the profile that serves claude');
|
|
1611
1610
|
assert.match(r.on.claude, /intact\.example\.io/, 'and the host the traffic goes to');
|
|
1612
1611
|
assert.match(r.on.claude, /gemini-3\.8-flash/, 'and the model it maps to');
|
|
1613
|
-
assert.
|
|
1612
|
+
assert.doesNotMatch(r.on.claude, /1M/, 'and no window: the tool sizes the session from the model');
|
|
1614
1613
|
assert.match(r.on.codex, /\bcx\b/);
|
|
1615
1614
|
assert.match(r.on.codex, /other\.example\.io/);
|
|
1616
1615
|
assert.doesNotMatch(r.on.codex, /intact\.example\.io/, 'one tool never shows the other tool route');
|