Skip to content

Commit 88af65d

Browse files
committed
fix(llama-cpp): skip failed models during automatic setup
1 parent a82042f commit 88af65d

2 files changed

Lines changed: 54 additions & 57 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ describe("llama-server setup", () => {
247247
],
248248
expected: "llama-cpp/meta-llama/Llama-3.3-8B",
249249
},
250+
{
251+
name: "does not recommend a server when every model has failed",
252+
models: [{ id: "google/gemma-4-27b", status: "unloaded", failed: true }],
253+
expected: null,
254+
},
250255
{
251256
name: "returns no candidate for an empty model catalog",
252257
models: [],
@@ -851,4 +856,21 @@ describe("llama-server setup", () => {
851856
);
852857
expect(ctx.runtime.exit).toHaveBeenCalledWith(1);
853858
});
859+
860+
it("rejects failed-only implicit setup while preserving an explicitly selected model", async () => {
861+
const discovery = successfulDiscovery();
862+
discovery.models = discovery.models.map((model) => ({ ...model, failed: true }));
863+
discoverMock.mockResolvedValue(discovery);
864+
865+
const implicit = nonInteractiveContext();
866+
await expect(validateLlamaServerNonInteractive(implicit)).resolves.toBe(false);
867+
expect(implicit.runtime.error).toHaveBeenCalledWith(
868+
"No llama-server text models were found at http://localhost:8080.",
869+
);
870+
expect(removeProviderAuthProfilesWithLockMock).not.toHaveBeenCalled();
871+
expect(upsertAuthProfileWithLockMock).not.toHaveBeenCalled();
872+
873+
const explicit = nonInteractiveContext({ customModelId: "qwen/model:Q4_K_M" });
874+
await expect(validateLlamaServerNonInteractive(explicit)).resolves.toBe(true);
875+
});
854876
});

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

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ import { resolveLlamaServerEndpoint } from "./endpoint.js";
3838
import { buildLlamaServerProviderConfig } from "./models.js";
3939

4040
function selectSetupModelId(discovery: Extract<LlamaServerDiscoveryResult, { kind: "success" }>) {
41-
const healthy = discovery.models.filter((model) => !model.failed);
42-
const candidates = healthy.length > 0 ? healthy : discovery.models;
41+
const candidates = discovery.models.filter((model) => !model.failed);
4342
const ready = candidates.filter(
4443
(model) => model.status === "loaded" || model.status === "sleeping",
4544
);
@@ -224,28 +223,33 @@ async function removeDefaultAuthProfile(agentDir?: string): Promise<void> {
224223
}
225224
}
226225

227-
async function discoverForSetup(params: {
228-
config: OpenClawConfig;
229-
baseUrl: string;
230-
env?: NodeJS.ProcessEnv;
231-
signal?: AbortSignal;
232-
}): Promise<LlamaServerDiscoveryResult> {
233-
const providerConfig = params.config.models?.providers?.[LLAMA_CPP_PROVIDER_ID];
234-
const headers = await resolveLlamaServerProviderHeaders({
235-
config: params.config,
236-
env: params.env,
237-
headers: providerConfig?.headers,
238-
});
239-
const resolvedApiKey = !hasLlamaServerAuthorizationHeader(headers)
240-
? await resolveLlamaServerRuntimeApiKey({ config: params.config })
241-
: undefined;
242-
return await discoverLlamaServer({
243-
baseUrl: params.baseUrl,
244-
apiKey: resolvedApiKey,
245-
headers,
246-
signal: params.signal,
247-
cacheTtlMs: 0,
248-
});
226+
async function discoverForSetup(
227+
ctx: ProviderAppGuidedSetupContext,
228+
): Promise<Extract<LlamaServerDiscoveryResult, { kind: "success" }> | null> {
229+
const provider = ctx.config.models?.providers?.[LLAMA_CPP_PROVIDER_ID];
230+
if (provider?.localService) {
231+
return null;
232+
}
233+
try {
234+
const headers = await resolveLlamaServerProviderHeaders({
235+
config: ctx.config,
236+
env: ctx.env,
237+
headers: provider?.headers,
238+
});
239+
const apiKey = !hasLlamaServerAuthorizationHeader(headers)
240+
? await resolveLlamaServerRuntimeApiKey({ config: ctx.config })
241+
: undefined;
242+
const discovery = await discoverLlamaServer({
243+
baseUrl: provider?.baseUrl ?? LLAMA_SERVER_DEFAULT_ORIGIN,
244+
apiKey,
245+
headers,
246+
signal: ctx.signal,
247+
cacheTtlMs: 0,
248+
});
249+
return discovery.kind === "success" ? discovery : null;
250+
} catch {
251+
return null;
252+
}
249253
}
250254

251255
async function discoverWithAccess(params: {
@@ -267,23 +271,8 @@ async function discoverWithAccess(params: {
267271
export async function detectLlamaServerSetup(
268272
ctx: ProviderAppGuidedSetupContext,
269273
): Promise<{ modelRef: string; detail?: string } | null> {
270-
const provider = ctx.config.models?.providers?.[LLAMA_CPP_PROVIDER_ID];
271-
if (provider?.localService) {
272-
return null;
273-
}
274-
const baseUrl = provider?.baseUrl ?? LLAMA_SERVER_DEFAULT_ORIGIN;
275-
let discovery: LlamaServerDiscoveryResult;
276-
try {
277-
discovery = await discoverForSetup({
278-
config: ctx.config,
279-
baseUrl,
280-
env: ctx.env,
281-
signal: ctx.signal,
282-
});
283-
} catch {
284-
return null;
285-
}
286-
if (discovery.kind !== "success") {
274+
const discovery = await discoverForSetup(ctx);
275+
if (!discovery) {
287276
return null;
288277
}
289278
const modelId = selectSetupModelId(discovery);
@@ -300,22 +289,8 @@ export async function detectLlamaServerSetup(
300289
export async function prepareLlamaServerSetup(
301290
ctx: ProviderAppGuidedSetupContext & { modelRef: string },
302291
): Promise<ProviderAuthResult | null> {
303-
const provider = ctx.config.models?.providers?.[LLAMA_CPP_PROVIDER_ID];
304-
if (provider?.localService) {
305-
return null;
306-
}
307-
let discovery: LlamaServerDiscoveryResult;
308-
try {
309-
discovery = await discoverForSetup({
310-
config: ctx.config,
311-
baseUrl: provider?.baseUrl ?? LLAMA_SERVER_DEFAULT_ORIGIN,
312-
env: ctx.env,
313-
signal: ctx.signal,
314-
});
315-
} catch {
316-
return null;
317-
}
318-
if (discovery.kind !== "success") {
292+
const discovery = await discoverForSetup(ctx);
293+
if (!discovery) {
319294
return null;
320295
}
321296
const prefix = `${LLAMA_CPP_PROVIDER_ID}/`;

0 commit comments

Comments
 (0)