From 479cdd6efbe834d10c010e17cabc607ae75016d1 Mon Sep 17 00:00:00 2001 From: iris Date: Mon, 23 Nov 2026 20:31:55 +0100 Subject: [PATCH] swarm-ui: say why the model dropdown has no models, and let it retry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The model list read swallowed every failure, so a swarm that timed out answering /api/models looked the same as one with no models: the dropdown held only the agent's own value and "other…", with nothing to say why. The failure now shows as a warning with the controller's reason and a retry button, and the answer is read through a parser that ignores any shape but a list of names. --- docs/4-implementation/web-ui/swarm-ui.md | 10 ++-- .../src/agent/detail/AgentInference.tsx | 11 +++- .../swarm-ui/src/agent/detail/model-choice.ts | 57 +++++++++++++------ .../swarm-ui/test/model-choice.test.mjs | 14 +++++ 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index ce77e3428..9e20dd14d 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -901,10 +901,12 @@ the `PUT`. **clear the swarm's setting** sends the `DELETE`. The model is a dropdown of the models the swarm already knows (`GET /api/models`: those in any agent's stored settings and those the agents report running on), with the agent's own as the empty choice; **other…** opens a box for any other model -name, and a stored name the list lacks opens it too. The subagent models stay a -typed list. The editor reads `GET /api/agents/{agent}/inference`; before the -agent's first report reaches the controller, it shows no running line. -[inference](../swarm/inference.md) describes the settings themselves. +name, and a stored name the list lacks opens it too. When the swarm can't read +the list, a warning says why, with a **retry** button, and the dropdown has only +those two entries. The subagent models stay a typed list. The editor reads `GET +/api/agents/{agent}/inference`; before the agent's first report reaches the +controller, it shows no running line. [inference](../swarm/inference.md) +describes the settings themselves. ### Editing an agent's instructions diff --git a/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx b/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx index b4ec0fd5c..dffa701f2 100644 --- a/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx +++ b/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx @@ -163,13 +163,22 @@ function ModelField({ value: string; onChange: (model: string) => void; }) { - const known = useKnownModels(); + const { models: known, problem, retry } = useKnownModels(); // A name the list lacks opens the box by itself; picking "other…" opens it // too, and keeps it open while the name is typed. const [custom, setCustom] = useState(false); const open = custom || !isListedModel(value, known); return ( <> + {problem !== null ? ( + + + The swarm's model list could not be read ({problem}), so only the + agent's own and "other…" are offered. + + + + ) : null} typeof m === "string" && m !== "") + : []; +} + // The models the swarm knows, kept once read: a form opened later shows them at -// once. A failed or empty read isn't kept, so the next form tries again, and -// meanwhile the dropdown has only its own and custom entries. +// once. A failed or empty read isn't kept, so the next form tries again. let cached: Promise | null = null; function readKnownModels(): Promise { cached ??= fetch("/api/models") - .then((r) => - r.ok - ? (r.json() as Promise<{ models?: string[] }>) - : Promise.reject(new Error(`http ${r.status}`)), - ) + .then(async (r) => { + if (r.ok) return (await r.json()) as unknown; + throw new Error(problemMessage(await readApiError(r))); + }) .then((body) => { - const models = body.models ?? []; + const models = parseKnownModels(body); // An empty answer isn't kept: on a fresh swarm the first agent to report // a model would otherwise stay missing until a reload. if (models.length === 0) cached = null; return models; }) - .catch(() => { + .catch((e: unknown) => { cached = null; - return []; + throw e; }); return cached; } -/** The model names the swarm knows; empty until they are read. */ -export function useKnownModels(): string[] { +/** + * The model names the swarm knows, empty until they are read, and why the read + * failed (an empty list that is only empty has no problem). `retry` reads again. + */ +export function useKnownModels(): { + models: string[]; + problem: string | null; + retry: () => void; +} { const [models, setModels] = useState([]); + const [problem, setProblem] = useState(null); + const [attempt, setAttempt] = useState(0); useEffect(() => { let cancelled = false; - void readKnownModels().then((m) => { - if (!cancelled) setModels(m); - }); + readKnownModels().then( + (m) => { + if (cancelled) return; + setModels(m); + setProblem(null); + }, + (e: unknown) => { + if (!cancelled) setProblem(e instanceof Error ? e.message : String(e)); + }, + ); return () => { cancelled = true; }; - }, []); - return models; + }, [attempt]); + return { models, problem, retry: () => setAttempt((n) => n + 1) }; } diff --git a/frontend/packages/swarm-ui/test/model-choice.test.mjs b/frontend/packages/swarm-ui/test/model-choice.test.mjs index 4962dc53b..c672f8da0 100644 --- a/frontend/packages/swarm-ui/test/model-choice.test.mjs +++ b/frontend/packages/swarm-ui/test/model-choice.test.mjs @@ -7,6 +7,7 @@ import { isListedModel, modelOptions, modelSelectValue, + parseKnownModels, } from "../src/agent/detail/model-choice.ts"; const KNOWN = ["haiku", "opus"]; @@ -38,3 +39,16 @@ test("an open custom entry stays selected while its name is empty or listed", () assert.equal(modelSelectValue("", KNOWN, true), CUSTOM_MODEL); assert.equal(modelSelectValue("opus", KNOWN, true), CUSTOM_MODEL); }); + +test("the model list is read from the answer, and any other shape is none", () => { + assert.deepEqual(parseKnownModels({ models: ["opus", "sonnet"] }), [ + "opus", + "sonnet", + ]); + assert.deepEqual(parseKnownModels({ models: ["opus", "", 3, null] }), [ + "opus", + ]); + assert.deepEqual(parseKnownModels({}), []); + assert.deepEqual(parseKnownModels({ models: "opus" }), []); + assert.deepEqual(parseKnownModels(null), []); +});