Skip to content

Commit 7c1779e

Browse files
authored
refactor(llama-cpp): adopt canonical runtime owners (#126371)
1 parent 61221c4 commit 7c1779e

10 files changed

Lines changed: 386 additions & 151 deletions

File tree

‎extensions/llama-cpp/src/external-server/auth.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import {
77
} from "./auth.js";
88

99
describe("llama-server auth", () => {
10+
it.each([undefined, null, [], "Bearer proxy-token"])(
11+
"rejects a non-record authorization header container: %j",
12+
(headers) => {
13+
expect(hasLlamaServerAuthorizationHeader(headers)).toBe(false);
14+
},
15+
);
16+
1017
it("uses synthetic runtime auth for no-auth and header-only providers", () => {
1118
expect(
1219
shouldUseLlamaServerSyntheticAuth({ baseUrl: "http://localhost:8080/v1", models: [] }),
@@ -71,4 +78,17 @@ describe("llama-server auth", () => {
7178
}),
7279
).resolves.toEqual({ "X-Proxy-Key": "proxy-token" });
7380
});
81+
82+
it("filters and trims plain provider headers without config", async () => {
83+
await expect(
84+
resolveLlamaServerProviderHeaders({
85+
headers: {
86+
"X-Proxy-Key": " proxy-token ",
87+
"X-Empty": " ",
88+
"X-Invalid": 42,
89+
},
90+
}),
91+
).resolves.toEqual({ "X-Proxy-Key": "proxy-token" });
92+
await expect(resolveLlamaServerProviderHeaders({ headers: [] })).resolves.toBeUndefined();
93+
});
7494
});

‎extensions/llama-cpp/src/external-server/auth.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import {
88
import { resolveApiKeyForProvider } from "openclaw/plugin-sdk/provider-auth-runtime";
99
import type { ModelProviderConfig } from "openclaw/plugin-sdk/provider-model-shared";
1010
import { resolveConfiguredSecretInputString } from "openclaw/plugin-sdk/secret-input-runtime";
11+
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
1112
import { LLAMA_SERVER_LOCAL_AUTH_MARKER, LLAMA_SERVER_PROVIDER_ID } from "./defaults.js";
1213

1314
export function hasLlamaServerAuthorizationHeader(headers: unknown): boolean {
14-
if (!headers || typeof headers !== "object" || Array.isArray(headers)) {
15+
const record = asOptionalRecord(headers);
16+
if (!record) {
1517
return false;
1618
}
17-
return Object.entries(headers).some(
19+
return Object.entries(record).some(
1820
([name, value]) =>
1921
name.trim().toLowerCase() === "authorization" && hasConfiguredSecretInput(value),
2022
);
@@ -56,11 +58,12 @@ export async function resolveLlamaServerProviderHeaders(params: {
5658
env?: NodeJS.ProcessEnv;
5759
headers?: unknown;
5860
}): Promise<Record<string, string> | undefined> {
59-
if (!params.headers || typeof params.headers !== "object" || Array.isArray(params.headers)) {
61+
const headers = asOptionalRecord(params.headers);
62+
if (!headers) {
6063
return undefined;
6164
}
6265
const resolved: Record<string, string> = {};
63-
for (const [name, value] of Object.entries(params.headers)) {
66+
for (const [name, value] of Object.entries(headers)) {
6467
if (!params.config) {
6568
if (typeof value === "string" && value.trim()) {
6669
resolved[name] = value.trim();

‎extensions/llama-cpp/src/external-server/discovery.test.ts‎

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { clearLiveCatalogCacheForTests } from "openclaw/plugin-sdk/provider-catalog-shared";
12
import type { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
2-
import { describe, expect, it, vi } from "vitest";
3+
import { beforeEach, describe, expect, it, vi } from "vitest";
34
import { discoverLlamaServer } from "./discovery.js";
45

56
type LlamaServerFetchGuard = typeof fetchWithSsrFGuard;
@@ -40,6 +41,10 @@ function json(value: unknown, status = 200): Response {
4041
}
4142

4243
describe("llama-server discovery", () => {
44+
beforeEach(() => {
45+
clearLiveCatalogCacheForTests();
46+
});
47+
4348
it("discovers a single model and reads runtime properties", async () => {
4449
const { guard, requests } = createFetchGuard({
4550
"http://localhost:8080/health": json({ status: "ok" }),
@@ -201,6 +206,60 @@ describe("llama-server discovery", () => {
201206
expect(requests.filter((url) => url.includes("/props?"))).toHaveLength(8);
202207
});
203208

209+
it("keeps router properties associated with their model when probes finish out of order", async () => {
210+
const rows = [0, 1, 2].map((index) => ({
211+
id: `model-${index}`,
212+
object: "model",
213+
status: { value: "loaded" },
214+
}));
215+
const routes: Record<string, RouteValue> = {
216+
"http://localhost:8080/health": json({ status: "ok" }),
217+
"http://localhost:8080/models": json({ data: rows }),
218+
};
219+
for (const [index, row] of rows.entries()) {
220+
routes[`http://localhost:8080/props?model=${row.id}&autoload=false`] = async () => {
221+
await new Promise((resolve) => {
222+
setTimeout(resolve, (2 - index) * 5);
223+
});
224+
return json({ default_generation_settings: { n_ctx: 8_000 + index } });
225+
};
226+
}
227+
const { guard } = createFetchGuard(routes);
228+
229+
const result = await discoverTest({
230+
baseUrl: "http://localhost:8080",
231+
fetchGuard: guard,
232+
});
233+
234+
expect(
235+
result.kind === "success" ? result.models.map((model) => model.config.contextWindow) : [],
236+
).toEqual([8_000, 8_001, 8_002]);
237+
});
238+
239+
it("caps router property probes at 200 models", async () => {
240+
const rows = Array.from({ length: 205 }, (_, index) => ({
241+
id: `model-${index}`,
242+
object: "model",
243+
status: { value: "loaded" },
244+
}));
245+
const routes: Record<string, RouteValue> = {
246+
"http://localhost:8080/health": json({ status: "ok" }),
247+
"http://localhost:8080/models": json({ data: rows }),
248+
};
249+
for (const row of rows.slice(0, 200)) {
250+
routes[`http://localhost:8080/props?model=${row.id}&autoload=false`] = json({});
251+
}
252+
const { guard, requests } = createFetchGuard(routes);
253+
254+
const result = await discoverTest({
255+
baseUrl: "http://localhost:8080",
256+
fetchGuard: guard,
257+
});
258+
259+
expect(result.kind === "success" ? result.models : []).toHaveLength(205);
260+
expect(requests.filter((url) => url.includes("/props?"))).toHaveLength(200);
261+
});
262+
204263
it("falls back to /v1/models for an older compatible server", async () => {
205264
const { guard } = createFetchGuard({
206265
"http://localhost:8080/health": json({ status: "ok" }),
@@ -313,6 +372,131 @@ describe("llama-server discovery", () => {
313372
expect(guard).toHaveBeenCalledTimes(2);
314373
});
315374

375+
it("coalesces concurrent discovery for the same uncredentialed endpoint", async () => {
376+
let releaseHealth: ((response: Response) => void) | undefined;
377+
const { guard } = createFetchGuard({
378+
"http://localhost:8080/health": () =>
379+
new Promise<Response>((resolve) => {
380+
releaseHealth = resolve;
381+
}),
382+
"http://localhost:8080/models": () => json({ data: [] }),
383+
});
384+
385+
const first = discoverTest({
386+
baseUrl: "http://localhost:8080",
387+
apiKey: "custom-local",
388+
fetchGuard: guard,
389+
cacheTtlMs: 30_000,
390+
});
391+
const second = discoverTest({
392+
baseUrl: "http://localhost:8080",
393+
apiKey: "custom-local",
394+
fetchGuard: guard,
395+
cacheTtlMs: 30_000,
396+
});
397+
398+
expect(releaseHealth).toBeDefined();
399+
expect(guard).toHaveBeenCalledTimes(1);
400+
releaseHealth?.(json({ status: "ok" }));
401+
await expect(Promise.all([first, second])).resolves.toEqual([
402+
expect.objectContaining({ kind: "success" }),
403+
expect.objectContaining({ kind: "success" }),
404+
]);
405+
expect(guard).toHaveBeenCalledTimes(2);
406+
});
407+
408+
it("reloads cached discovery after its configured TTL", async () => {
409+
vi.useFakeTimers();
410+
try {
411+
vi.setSystemTime(1_000);
412+
const { guard } = createFetchGuard({
413+
"http://localhost:8080/health": () => json({ status: "ok" }),
414+
"http://localhost:8080/models": () => json({ data: [] }),
415+
});
416+
417+
await discoverTest({
418+
baseUrl: "http://localhost:8080",
419+
apiKey: "custom-local",
420+
fetchGuard: guard,
421+
cacheTtlMs: 100,
422+
});
423+
vi.setSystemTime(1_101);
424+
await discoverTest({
425+
baseUrl: "http://localhost:8080",
426+
apiKey: "custom-local",
427+
fetchGuard: guard,
428+
cacheTtlMs: 100,
429+
});
430+
431+
expect(guard).toHaveBeenCalledTimes(4);
432+
} finally {
433+
vi.useRealTimers();
434+
}
435+
});
436+
437+
it("does not retain unsuccessful discovery results", async () => {
438+
const { guard } = createFetchGuard({
439+
"http://localhost:8080/health": () => json({ error: "unauthorized" }, 401),
440+
});
441+
442+
for (let index = 0; index < 2; index += 1) {
443+
await discoverTest({
444+
baseUrl: "http://localhost:8080",
445+
apiKey: "custom-local",
446+
fetchGuard: guard,
447+
cacheTtlMs: 30_000,
448+
});
449+
}
450+
451+
expect(guard).toHaveBeenCalledTimes(2);
452+
});
453+
454+
it("bypasses an existing endpoint cache when a real API key is supplied", async () => {
455+
const { guard } = createFetchGuard({
456+
"http://localhost:8080/health": () => json({ status: "ok" }),
457+
"http://localhost:8080/models": () => json({ data: [] }),
458+
});
459+
460+
await discoverTest({
461+
baseUrl: "http://localhost:8080",
462+
apiKey: "custom-local",
463+
fetchGuard: guard,
464+
cacheTtlMs: 30_000,
465+
});
466+
await discoverTest({
467+
baseUrl: "http://localhost:8080",
468+
apiKey: "real-secret",
469+
fetchGuard: guard,
470+
cacheTtlMs: 30_000,
471+
});
472+
473+
expect(guard).toHaveBeenCalledTimes(4);
474+
});
475+
476+
it("keeps uncredentialed discovery caches isolated by endpoint", async () => {
477+
const { guard } = createFetchGuard({
478+
"http://localhost:8080/health": json({ status: "ok" }),
479+
"http://localhost:8080/models": json({ data: [] }),
480+
"http://localhost:8081/health": json({ status: "ok" }),
481+
"http://localhost:8081/models": json({ data: [] }),
482+
});
483+
484+
await discoverTest({
485+
baseUrl: "http://localhost:8080",
486+
apiKey: "custom-local",
487+
fetchGuard: guard,
488+
cacheTtlMs: 30_000,
489+
});
490+
await discoverTest({
491+
baseUrl: "http://localhost:8081",
492+
apiKey: "custom-local",
493+
fetchGuard: guard,
494+
cacheTtlMs: 30_000,
495+
});
496+
497+
expect(guard).toHaveBeenCalledTimes(4);
498+
});
499+
316500
it("bounds cached endpoint discovery", async () => {
317501
const guard = vi.fn(async (params: Parameters<LlamaServerFetchGuard>[0]) => ({
318502
response: params.url.endsWith("/health") ? json({ status: "ok" }) : json({ data: [] }),

0 commit comments

Comments
 (0)