pi-llama-switch 1.0.2 → 1.0.3
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/LICENSE +21 -21
- package/README.md +218 -216
- package/examples/model-switcher-vllm.json +65 -61
- package/examples/model-switcher.json +53 -53
- package/package.json +44 -41
- package/src/config.ts +170 -123
- package/src/health.ts +53 -9
- package/src/index.ts +273 -63
- package/src/provider.ts +76 -2
- package/src/switcher.ts +219 -60
- package/test/fixes.test.cjs +636 -0
- package/tsconfig.json +17 -17
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { test, after } = require("node:test");
|
|
4
|
+
const assert = require("node:assert/strict");
|
|
5
|
+
const fs = require("node:fs");
|
|
6
|
+
const http = require("node:http");
|
|
7
|
+
const net = require("node:net");
|
|
8
|
+
const os = require("node:os");
|
|
9
|
+
const path = require("node:path");
|
|
10
|
+
|
|
11
|
+
// Isolate all PID/log/config writes from the real ~/.pi/agent.
|
|
12
|
+
const TEST_HOME = path.join(os.tmpdir(), `pi-llama-switch-test-${process.pid}`);
|
|
13
|
+
const AGENT_DIR = path.join(TEST_HOME, ".pi", "agent");
|
|
14
|
+
fs.mkdirSync(AGENT_DIR, { recursive: true });
|
|
15
|
+
process.env.USERPROFILE = TEST_HOME;
|
|
16
|
+
process.env.HOME = TEST_HOME;
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
formatHostForUrl,
|
|
20
|
+
formatServerUrl,
|
|
21
|
+
loadConfig,
|
|
22
|
+
normalizeHostForSocket,
|
|
23
|
+
} = require("../.test-dist/config.js");
|
|
24
|
+
const { checkHealth, waitForHealth } = require("../.test-dist/health.js");
|
|
25
|
+
const {
|
|
26
|
+
buildProviderModels,
|
|
27
|
+
resolveApiModelId,
|
|
28
|
+
} = require("../.test-dist/provider.js");
|
|
29
|
+
const {
|
|
30
|
+
getState,
|
|
31
|
+
hasPortArg,
|
|
32
|
+
isManagedServerCommand,
|
|
33
|
+
switchModel,
|
|
34
|
+
} = require("../.test-dist/switcher.js");
|
|
35
|
+
const {
|
|
36
|
+
activateModel,
|
|
37
|
+
detectModelFromArgs,
|
|
38
|
+
extractModelMatchTokens,
|
|
39
|
+
handleReload,
|
|
40
|
+
handleStatus,
|
|
41
|
+
} = require("../.test-dist/index.js");
|
|
42
|
+
|
|
43
|
+
after(() => {
|
|
44
|
+
fs.rmSync(TEST_HOME, { recursive: true, force: true });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
function model(overrides = {}) {
|
|
48
|
+
return {
|
|
49
|
+
name: "Test Model",
|
|
50
|
+
description: "Test model",
|
|
51
|
+
command: ["llama-server", "-m", "/models/test.gguf"],
|
|
52
|
+
vision: false,
|
|
53
|
+
contextWindow: 8192,
|
|
54
|
+
maxTokens: 4096,
|
|
55
|
+
tags: [],
|
|
56
|
+
...overrides,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function configWith(models, server = {}) {
|
|
61
|
+
return {
|
|
62
|
+
server: {
|
|
63
|
+
host: "127.0.0.1",
|
|
64
|
+
port: 59999,
|
|
65
|
+
healthTimeout: 5,
|
|
66
|
+
portReleaseTimeout: 5,
|
|
67
|
+
...server,
|
|
68
|
+
},
|
|
69
|
+
defaultModel: Object.keys(models)[0],
|
|
70
|
+
models,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getFreePort() {
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
const server = net.createServer();
|
|
77
|
+
server.once("error", reject);
|
|
78
|
+
server.listen(0, "127.0.0.1", () => {
|
|
79
|
+
const { port } = server.address();
|
|
80
|
+
server.close(() => resolve(port));
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// --- IPv6 URL helpers -------------------------------------------------------
|
|
86
|
+
|
|
87
|
+
test("formatServerUrl brackets bare IPv6 hosts and leaves bracketed ones", () => {
|
|
88
|
+
assert.equal(formatServerUrl("::1", 8080, "/health"), "http://[::1]:8080/health");
|
|
89
|
+
assert.equal(formatServerUrl("[::1]", 8080, "/v1"), "http://[::1]:8080/v1");
|
|
90
|
+
assert.equal(formatServerUrl("127.0.0.1", 8080, "/health"), "http://127.0.0.1:8080/health");
|
|
91
|
+
assert.equal(formatHostForUrl("fe80::1"), "[fe80::1]");
|
|
92
|
+
assert.equal(normalizeHostForSocket("[::1]"), "::1");
|
|
93
|
+
assert.equal(normalizeHostForSocket("::1"), "::1");
|
|
94
|
+
assert.equal(normalizeHostForSocket("127.0.0.1"), "127.0.0.1");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// --- Port argument parsing --------------------------------------------------
|
|
98
|
+
|
|
99
|
+
test("hasPortArg matches only explicit exact --port values", () => {
|
|
100
|
+
assert.equal(hasPortArg("llama-server --port 8080", 8080), true);
|
|
101
|
+
assert.equal(hasPortArg("llama-server --port=8080", 8080), true);
|
|
102
|
+
assert.equal(hasPortArg("llama-server --port 18080", 8080), false);
|
|
103
|
+
assert.equal(hasPortArg("llama-server --port 80801", 8080), false);
|
|
104
|
+
assert.equal(hasPortArg("llama-server -m foo-8080.gguf --port 9000", 8080), false);
|
|
105
|
+
assert.equal(hasPortArg("llama-server -p 8080", 8080), false);
|
|
106
|
+
assert.equal(hasPortArg("llama-server --port 9000 --port 8080", 8080), true);
|
|
107
|
+
assert.equal(hasPortArg("llama-server --port 8080 --port 9000", 8080), false);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// --- Managed server detection ----------------------------------------------
|
|
111
|
+
|
|
112
|
+
test("isManagedServerCommand recognizes llama-server and vLLM", () => {
|
|
113
|
+
assert.equal(isManagedServerCommand("llama-server -m /models/a.gguf"), true);
|
|
114
|
+
assert.equal(
|
|
115
|
+
isManagedServerCommand(
|
|
116
|
+
"python3 -m vllm.entrypoints.openai.api_server --port 8000"
|
|
117
|
+
),
|
|
118
|
+
true
|
|
119
|
+
);
|
|
120
|
+
assert.equal(isManagedServerCommand("vllm serve org/model --port 8000"), true);
|
|
121
|
+
assert.equal(isManagedServerCommand("python3 my_script.py"), false);
|
|
122
|
+
assert.equal(isManagedServerCommand("node server.js"), false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("isManagedServerCommand honors configured non-generic executables", () => {
|
|
126
|
+
const config = configWith({
|
|
127
|
+
custom: model({ command: ["/opt/my-llama-server", "-m", "/models/a.gguf"] }),
|
|
128
|
+
});
|
|
129
|
+
assert.equal(
|
|
130
|
+
isManagedServerCommand("/opt/my-llama-server -m /models/a.gguf", config),
|
|
131
|
+
true
|
|
132
|
+
);
|
|
133
|
+
assert.equal(isManagedServerCommand("/opt/other-binary --port 8080", config), false);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// --- API model ID resolution ------------------------------------------------
|
|
137
|
+
|
|
138
|
+
test("resolveApiModelId follows documented precedence", () => {
|
|
139
|
+
const served = model({
|
|
140
|
+
command: ["llama-server", "-m", "/m.gguf", "--alias", "aliased", "--served-model-name", "served"],
|
|
141
|
+
});
|
|
142
|
+
assert.equal(resolveApiModelId("key", served), "served");
|
|
143
|
+
assert.equal(
|
|
144
|
+
resolveApiModelId("key", { ...served, modelId: "org/model" }),
|
|
145
|
+
"org/model"
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
assert.equal(
|
|
149
|
+
resolveApiModelId("key", model({ command: ["llama-server", "-m", "/m.gguf", "--alias", "aliased"] })),
|
|
150
|
+
"aliased"
|
|
151
|
+
);
|
|
152
|
+
assert.equal(
|
|
153
|
+
resolveApiModelId("key", model({ command: ["llama-server", "-a", "short"] })),
|
|
154
|
+
"short"
|
|
155
|
+
);
|
|
156
|
+
assert.equal(
|
|
157
|
+
resolveApiModelId("key", model({ command: ["llama-server", "--alias=eq"] })),
|
|
158
|
+
"eq"
|
|
159
|
+
);
|
|
160
|
+
assert.equal(
|
|
161
|
+
resolveApiModelId("key", model({ command: ["llama-server", "-m", "/m.gguf"] })),
|
|
162
|
+
"key"
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("resolveApiModelId resolves vLLM served models", () => {
|
|
167
|
+
const moduleCommand = [
|
|
168
|
+
"python3",
|
|
169
|
+
"-m",
|
|
170
|
+
"vllm.entrypoints.openai.api_server",
|
|
171
|
+
"--model",
|
|
172
|
+
"/models/qwen",
|
|
173
|
+
];
|
|
174
|
+
assert.equal(
|
|
175
|
+
resolveApiModelId("qwen", model({ command: moduleCommand })),
|
|
176
|
+
"/models/qwen"
|
|
177
|
+
);
|
|
178
|
+
assert.equal(
|
|
179
|
+
resolveApiModelId("qwen", model({ command: ["vllm", "serve", "org/model", "--port", "8000"] })),
|
|
180
|
+
"org/model"
|
|
181
|
+
);
|
|
182
|
+
assert.equal(
|
|
183
|
+
resolveApiModelId(
|
|
184
|
+
"qwen",
|
|
185
|
+
model({ command: [...moduleCommand, "--served-model-name", "qwen-api"] })
|
|
186
|
+
),
|
|
187
|
+
"qwen-api"
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("vLLM example config parses and resolves both served names", () => {
|
|
192
|
+
const examplePath = path.join(__dirname, "..", "examples", "model-switcher-vllm.json");
|
|
193
|
+
const config = loadConfig(examplePath);
|
|
194
|
+
assert.ok(config);
|
|
195
|
+
assert.equal(resolveApiModelId("qwen", config.models.qwen), "qwen");
|
|
196
|
+
assert.equal(resolveApiModelId("llama", config.models.llama), "llama");
|
|
197
|
+
assert.deepEqual(
|
|
198
|
+
buildProviderModels(config).map((m) => m.id).sort(),
|
|
199
|
+
["llama", "qwen"]
|
|
200
|
+
);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("config validation rejects empty modelId and unknown defaultModel", () => {
|
|
204
|
+
const invalidModelId = path.join(TEST_HOME, "invalid-model-id.json");
|
|
205
|
+
fs.writeFileSync(
|
|
206
|
+
invalidModelId,
|
|
207
|
+
JSON.stringify({
|
|
208
|
+
server: { host: "127.0.0.1", port: 8080 },
|
|
209
|
+
models: { a: { name: "A", description: "A", command: ["llama-server"], modelId: " " } },
|
|
210
|
+
})
|
|
211
|
+
);
|
|
212
|
+
assert.throws(() => loadConfig(invalidModelId), /modelId must be a non-empty string/);
|
|
213
|
+
|
|
214
|
+
const invalidDefault = path.join(TEST_HOME, "invalid-default.json");
|
|
215
|
+
fs.writeFileSync(
|
|
216
|
+
invalidDefault,
|
|
217
|
+
JSON.stringify({
|
|
218
|
+
server: { host: "127.0.0.1", port: 8080 },
|
|
219
|
+
defaultModel: "missing",
|
|
220
|
+
models: { a: { name: "A", description: "A", command: ["llama-server"] } },
|
|
221
|
+
})
|
|
222
|
+
);
|
|
223
|
+
assert.throws(() => loadConfig(invalidDefault), /defaultModel 'missing' not found/);
|
|
224
|
+
|
|
225
|
+
const emptyDefault = path.join(TEST_HOME, "empty-default.json");
|
|
226
|
+
fs.writeFileSync(
|
|
227
|
+
emptyDefault,
|
|
228
|
+
JSON.stringify({
|
|
229
|
+
server: { host: "127.0.0.1", port: 8080 },
|
|
230
|
+
defaultModel: "",
|
|
231
|
+
models: { a: { name: "A", description: "A", command: ["llama-server"] } },
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
const fallback = loadConfig(emptyDefault);
|
|
235
|
+
assert.ok(fallback);
|
|
236
|
+
assert.equal(fallback.defaultModel, "a");
|
|
237
|
+
assert.equal(fallback.models.a.modelId, undefined);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// --- Model selection in Pi --------------------------------------------------
|
|
241
|
+
|
|
242
|
+
test("activateModel selects the resolved API model id and preserves reasoning", async () => {
|
|
243
|
+
const config = configWith(
|
|
244
|
+
{
|
|
245
|
+
qwen: model({
|
|
246
|
+
name: "Qwen",
|
|
247
|
+
command: ["llama-server", "--served-model-name", "qwen-api", "-m", "/q.gguf"],
|
|
248
|
+
tags: ["reasoning"],
|
|
249
|
+
}),
|
|
250
|
+
gemma: model({
|
|
251
|
+
name: "Gemma",
|
|
252
|
+
command: ["llama-server", "--served-model-name", "gemma-api", "-m", "/g.gguf"],
|
|
253
|
+
tags: ["reasoning"],
|
|
254
|
+
}),
|
|
255
|
+
},
|
|
256
|
+
{ host: "::1", port: 8080 }
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
let registered;
|
|
260
|
+
const selected = [];
|
|
261
|
+
const statuses = [];
|
|
262
|
+
const pi = {
|
|
263
|
+
registerProvider(providerId, options) {
|
|
264
|
+
registered = { providerId, options };
|
|
265
|
+
},
|
|
266
|
+
async setModel(m) {
|
|
267
|
+
selected.push(m);
|
|
268
|
+
return true;
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
const ctx = {
|
|
272
|
+
ui: {
|
|
273
|
+
notify() {},
|
|
274
|
+
setStatus(key, value) {
|
|
275
|
+
statuses.push([key, value]);
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
modelRegistry: {
|
|
279
|
+
find(provider, id) {
|
|
280
|
+
return id === "qwen-api" ? { provider, id } : undefined;
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const ok = await activateModel(pi, config, "qwen", ctx);
|
|
286
|
+
|
|
287
|
+
assert.equal(ok, true);
|
|
288
|
+
assert.equal(selected.length, 1);
|
|
289
|
+
assert.equal(selected[0].id, "qwen-api");
|
|
290
|
+
assert.equal(registered.providerId, "llama-local");
|
|
291
|
+
assert.equal(registered.options.baseUrl, "http://[::1]:8080/v1");
|
|
292
|
+
assert.deepEqual(
|
|
293
|
+
registered.options.models.map((m) => m.id).sort(),
|
|
294
|
+
["gemma-api", "qwen-api"]
|
|
295
|
+
);
|
|
296
|
+
assert.equal(
|
|
297
|
+
registered.options.models.find((m) => m.id === "gemma-api").reasoning,
|
|
298
|
+
true
|
|
299
|
+
);
|
|
300
|
+
assert.deepEqual(statuses.at(-1), ["llama-switch", "⚡ Qwen"]);
|
|
301
|
+
|
|
302
|
+
const notFound = await activateModel(pi, config, "qwen", {
|
|
303
|
+
...ctx,
|
|
304
|
+
modelRegistry: { find: () => undefined },
|
|
305
|
+
});
|
|
306
|
+
assert.equal(notFound, false);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// --- Process lifecycle ------------------------------------------------------
|
|
310
|
+
|
|
311
|
+
test("switchModel reports ENOENT without crashing and closes the log fd", async () => {
|
|
312
|
+
const port = await getFreePort();
|
|
313
|
+
const config = configWith(
|
|
314
|
+
{ ghost: model({ command: ["definitely-not-a-real-binary-xyz"] }) },
|
|
315
|
+
{ port }
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
const openedFds = new Set();
|
|
319
|
+
let opened = 0;
|
|
320
|
+
let closed = 0;
|
|
321
|
+
const realOpen = fs.openSync;
|
|
322
|
+
const realClose = fs.closeSync;
|
|
323
|
+
|
|
324
|
+
fs.openSync = function (file, ...args) {
|
|
325
|
+
const fd = realOpen.call(this, file, ...args);
|
|
326
|
+
if (String(file).endsWith("llama-switch.log")) {
|
|
327
|
+
opened++;
|
|
328
|
+
openedFds.add(fd);
|
|
329
|
+
}
|
|
330
|
+
return fd;
|
|
331
|
+
};
|
|
332
|
+
fs.closeSync = function (fd) {
|
|
333
|
+
if (openedFds.delete(fd)) {
|
|
334
|
+
closed++;
|
|
335
|
+
}
|
|
336
|
+
return realClose.call(this, fd);
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
try {
|
|
340
|
+
await assert.rejects(
|
|
341
|
+
() => switchModel(config, "ghost"),
|
|
342
|
+
/Failed to start definitely-not-a-real-binary-xyz/
|
|
343
|
+
);
|
|
344
|
+
} finally {
|
|
345
|
+
fs.openSync = realOpen;
|
|
346
|
+
fs.closeSync = realClose;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
assert.equal(opened, 1);
|
|
350
|
+
assert.equal(closed, 1);
|
|
351
|
+
assert.equal(openedFds.size, 0);
|
|
352
|
+
assert.equal(getState().activePid, null);
|
|
353
|
+
assert.equal(getState().activeModelKey, null);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// --- Health checks ----------------------------------------------------------
|
|
357
|
+
|
|
358
|
+
test("waitForHealth rejects on a stalled server instead of hanging", async () => {
|
|
359
|
+
const server = http.createServer(() => {
|
|
360
|
+
// Never respond.
|
|
361
|
+
});
|
|
362
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
363
|
+
const { port } = server.address();
|
|
364
|
+
|
|
365
|
+
const started = Date.now();
|
|
366
|
+
try {
|
|
367
|
+
await assert.rejects(
|
|
368
|
+
() =>
|
|
369
|
+
waitForHealth({
|
|
370
|
+
host: "127.0.0.1",
|
|
371
|
+
port,
|
|
372
|
+
healthTimeout: 1,
|
|
373
|
+
portReleaseTimeout: 1,
|
|
374
|
+
}),
|
|
375
|
+
/timed out after 1s/
|
|
376
|
+
);
|
|
377
|
+
const elapsed = Date.now() - started;
|
|
378
|
+
assert.ok(elapsed < 3000, `expected a fast rejection, took ${elapsed}ms`);
|
|
379
|
+
} finally {
|
|
380
|
+
server.closeAllConnections?.();
|
|
381
|
+
await new Promise((resolve) => server.close(resolve));
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
test("checkHealth honors its own timeout", async () => {
|
|
386
|
+
const server = http.createServer(() => {
|
|
387
|
+
// Never respond.
|
|
388
|
+
});
|
|
389
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
390
|
+
const { port } = server.address();
|
|
391
|
+
|
|
392
|
+
const started = Date.now();
|
|
393
|
+
try {
|
|
394
|
+
assert.equal(await checkHealth("127.0.0.1", port, 300), false);
|
|
395
|
+
const elapsed = Date.now() - started;
|
|
396
|
+
assert.ok(elapsed < 2000, `expected a fast rejection, took ${elapsed}ms`);
|
|
397
|
+
} finally {
|
|
398
|
+
server.closeAllConnections?.();
|
|
399
|
+
await new Promise((resolve) => server.close(resolve));
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// --- Model detection from a running process --------------------------------
|
|
404
|
+
|
|
405
|
+
test("extractModelMatchTokens skips secondary artifacts", () => {
|
|
406
|
+
assert.deepEqual(
|
|
407
|
+
extractModelMatchTokens([
|
|
408
|
+
"llama-server",
|
|
409
|
+
"--mmproj",
|
|
410
|
+
"/models/mm.gguf",
|
|
411
|
+
"-m",
|
|
412
|
+
"/models/shared.gguf",
|
|
413
|
+
]),
|
|
414
|
+
["/models/shared.gguf"]
|
|
415
|
+
);
|
|
416
|
+
assert.deepEqual(
|
|
417
|
+
extractModelMatchTokens([
|
|
418
|
+
"python3",
|
|
419
|
+
"-m",
|
|
420
|
+
"vllm.entrypoints.openai.api_server",
|
|
421
|
+
"--model",
|
|
422
|
+
"/models/qwen",
|
|
423
|
+
]),
|
|
424
|
+
["/models/qwen"]
|
|
425
|
+
);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
test("detectModelFromArgs disambiguates models sharing a GGUF", () => {
|
|
429
|
+
const config = configWith({
|
|
430
|
+
a: model({
|
|
431
|
+
command: [
|
|
432
|
+
"llama-server",
|
|
433
|
+
"-m",
|
|
434
|
+
"/models/shared.gguf",
|
|
435
|
+
"--mmproj",
|
|
436
|
+
"/models/mm.gguf",
|
|
437
|
+
"--ctx-size",
|
|
438
|
+
"8192",
|
|
439
|
+
],
|
|
440
|
+
}),
|
|
441
|
+
b: model({
|
|
442
|
+
command: [
|
|
443
|
+
"llama-server",
|
|
444
|
+
"--mmproj",
|
|
445
|
+
"/models/mm.gguf",
|
|
446
|
+
"-m",
|
|
447
|
+
"/models/shared.gguf",
|
|
448
|
+
"--ctx-size",
|
|
449
|
+
"16384",
|
|
450
|
+
],
|
|
451
|
+
}),
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const procArgsA =
|
|
455
|
+
"llama-server -m /models/shared.gguf --mmproj /models/mm.gguf --ctx-size 8192 --port 8080";
|
|
456
|
+
const procArgsB =
|
|
457
|
+
"llama-server --mmproj /models/mm.gguf -m /models/shared.gguf --ctx-size 16384 --port 8080";
|
|
458
|
+
|
|
459
|
+
assert.equal(detectModelFromArgs(config, procArgsA), "a");
|
|
460
|
+
assert.equal(detectModelFromArgs(config, procArgsB), "b");
|
|
461
|
+
assert.equal(detectModelFromArgs(config, "python3 other_script.py"), null);
|
|
462
|
+
assert.equal(
|
|
463
|
+
detectModelFromArgs(
|
|
464
|
+
config,
|
|
465
|
+
"llama-server -m /models/shared.gguf.bak --mmproj /models/mm.gguf --ctx-size 8192"
|
|
466
|
+
),
|
|
467
|
+
null
|
|
468
|
+
);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
test("detectModelFromArgs recognizes vLLM model flags and serve positional", () => {
|
|
472
|
+
const config = configWith({
|
|
473
|
+
qwen: model({
|
|
474
|
+
command: [
|
|
475
|
+
"python3",
|
|
476
|
+
"-m",
|
|
477
|
+
"vllm.entrypoints.openai.api_server",
|
|
478
|
+
"--model",
|
|
479
|
+
"/models/qwen",
|
|
480
|
+
"--served-model-name",
|
|
481
|
+
"qwen-api",
|
|
482
|
+
],
|
|
483
|
+
}),
|
|
484
|
+
llama: model({
|
|
485
|
+
command: [
|
|
486
|
+
"python3",
|
|
487
|
+
"-m",
|
|
488
|
+
"vllm.entrypoints.openai.api_server",
|
|
489
|
+
"--model",
|
|
490
|
+
"/models/llama",
|
|
491
|
+
],
|
|
492
|
+
}),
|
|
493
|
+
served: model({
|
|
494
|
+
command: ["vllm", "serve", "org/model-s", "--port", "8000"],
|
|
495
|
+
}),
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
assert.equal(
|
|
499
|
+
detectModelFromArgs(
|
|
500
|
+
config,
|
|
501
|
+
"python3 -m vllm.entrypoints.openai.api_server --model /models/llama --port 8000"
|
|
502
|
+
),
|
|
503
|
+
"llama"
|
|
504
|
+
);
|
|
505
|
+
assert.equal(
|
|
506
|
+
detectModelFromArgs(
|
|
507
|
+
config,
|
|
508
|
+
"python3 -m vllm.entrypoints.openai.api_server --model /models/qwen --served-model-name qwen-api --port 8000"
|
|
509
|
+
),
|
|
510
|
+
"qwen"
|
|
511
|
+
);
|
|
512
|
+
assert.equal(
|
|
513
|
+
detectModelFromArgs(config, "vllm serve org/model-s --port 8000"),
|
|
514
|
+
"served"
|
|
515
|
+
);
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
// --- Reload safety ----------------------------------------------------------
|
|
519
|
+
|
|
520
|
+
function writeConfigFile(config) {
|
|
521
|
+
fs.writeFileSync(
|
|
522
|
+
path.join(AGENT_DIR, "model-switcher.json"),
|
|
523
|
+
JSON.stringify(config)
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
test("handleReload refuses to run while a switch is in progress", async () => {
|
|
528
|
+
const state = getState();
|
|
529
|
+
const notifications = [];
|
|
530
|
+
let setConfigCalls = 0;
|
|
531
|
+
const pi = { registerProvider() {} };
|
|
532
|
+
const ctx = {
|
|
533
|
+
ui: {
|
|
534
|
+
notify(message, type) {
|
|
535
|
+
notifications.push([type, message]);
|
|
536
|
+
},
|
|
537
|
+
setStatus() {},
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
state.isSwitching = true;
|
|
542
|
+
try {
|
|
543
|
+
await handleReload(pi, ctx, () => {
|
|
544
|
+
setConfigCalls++;
|
|
545
|
+
});
|
|
546
|
+
} finally {
|
|
547
|
+
state.isSwitching = false;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
assert.equal(setConfigCalls, 0);
|
|
551
|
+
assert.equal(notifications[0][0], "error");
|
|
552
|
+
assert.match(notifications[0][1], /in progress/);
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
test("handleReload reports malformed config without throwing", async () => {
|
|
556
|
+
fs.writeFileSync(path.join(AGENT_DIR, "model-switcher.json"), "{ not json");
|
|
557
|
+
const notifications = [];
|
|
558
|
+
let setConfigCalls = 0;
|
|
559
|
+
const ctx = {
|
|
560
|
+
ui: {
|
|
561
|
+
notify(message, type) {
|
|
562
|
+
notifications.push([type, message]);
|
|
563
|
+
},
|
|
564
|
+
setStatus() {},
|
|
565
|
+
},
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
await handleReload({ registerProvider() {} }, ctx, () => {
|
|
569
|
+
setConfigCalls++;
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
assert.equal(setConfigCalls, 0);
|
|
573
|
+
assert.equal(notifications[0][0], "error");
|
|
574
|
+
assert.match(notifications[0][1], /Failed to reload config/);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test("handleReload clears a dropped active model but keeps the running PID", async () => {
|
|
578
|
+
writeConfigFile({
|
|
579
|
+
server: { host: "127.0.0.1", port: 12345 },
|
|
580
|
+
defaultModel: "new",
|
|
581
|
+
models: {
|
|
582
|
+
new: model({ name: "New Model", command: ["llama-server", "-m", "/models/new.gguf"] }),
|
|
583
|
+
},
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
const state = getState();
|
|
587
|
+
state.activeModelKey = "dropped";
|
|
588
|
+
state.activePid = 4242;
|
|
589
|
+
|
|
590
|
+
const notifications = [];
|
|
591
|
+
const statuses = [];
|
|
592
|
+
let registered = null;
|
|
593
|
+
let nextConfig = null;
|
|
594
|
+
const pi = {
|
|
595
|
+
registerProvider(providerId, options) {
|
|
596
|
+
registered = { providerId, options };
|
|
597
|
+
},
|
|
598
|
+
};
|
|
599
|
+
const ctx = {
|
|
600
|
+
ui: {
|
|
601
|
+
notify(message, type) {
|
|
602
|
+
notifications.push([type, message]);
|
|
603
|
+
},
|
|
604
|
+
setStatus(key, value) {
|
|
605
|
+
statuses.push([key, value]);
|
|
606
|
+
},
|
|
607
|
+
},
|
|
608
|
+
modelRegistry: { find: () => undefined },
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
try {
|
|
612
|
+
await handleReload(pi, ctx, (next) => {
|
|
613
|
+
nextConfig = next;
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
assert.ok(nextConfig);
|
|
617
|
+
assert.equal(state.activeModelKey, null);
|
|
618
|
+
assert.equal(state.activePid, 4242);
|
|
619
|
+
assert.deepEqual(statuses.at(-1), ["llama-switch", "⚠ unlisted model running"]);
|
|
620
|
+
assert.ok(
|
|
621
|
+
notifications.some(
|
|
622
|
+
([type, message]) => type === "warning" && message.includes("dropped")
|
|
623
|
+
)
|
|
624
|
+
);
|
|
625
|
+
assert.ok(registered);
|
|
626
|
+
|
|
627
|
+
// /switch status must not crash when the active key is no longer in config.
|
|
628
|
+
notifications.length = 0;
|
|
629
|
+
handleStatus(nextConfig, ctx);
|
|
630
|
+
assert.match(notifications.at(-1)[1], /not in current config/);
|
|
631
|
+
assert.match(notifications.at(-1)[1], /4242/);
|
|
632
|
+
} finally {
|
|
633
|
+
state.activeModelKey = null;
|
|
634
|
+
state.activePid = null;
|
|
635
|
+
}
|
|
636
|
+
});
|
package/tsconfig.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "bundler",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"strict": true,
|
|
8
|
-
"skipLibCheck": true,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"rootDir": "./src",
|
|
11
|
-
"declaration": true,
|
|
12
|
-
"sourceMap": true,
|
|
13
|
-
"resolveJsonModule": true
|
|
14
|
-
},
|
|
15
|
-
"include": ["src/**/*.ts"],
|
|
16
|
-
"exclude": ["node_modules", "dist"]
|
|
17
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"rootDir": "./src",
|
|
11
|
+
"declaration": true,
|
|
12
|
+
"sourceMap": true,
|
|
13
|
+
"resolveJsonModule": true
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*.ts"],
|
|
16
|
+
"exclude": ["node_modules", "dist"]
|
|
17
|
+
}
|