Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: say why the model dropdown has no models, and let it retry

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.
This commit is contained in:
iris 2026-11-23 20:31:55 +01:00 • committed by mara
commit 479cdd6efb
4 changed files with 70 additions and 22 deletions

View file

@ -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

View file

@ -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 ? (
<FieldGridFull>
<Hint tone="warn">
The swarm's model list could not be read ({problem}), so only the
agent's own and "other…" are offered.
</Hint>
<Button onClick={retry}>retry</Button>
</FieldGridFull>
) : null}
<SelectField
id={id}
label="model"

View file

@ -2,6 +2,7 @@
// `GET /api/models`), plus the agent's own value and a custom entry. No model
// name lives here.
import { useEffect, useState } from "preact/hooks";
import { problemMessage, readApiError } from "@hive/shared/api-error.js";
import type { SelectOption } from "../../ui/select-field/SelectField.js";
/** The select's value for "type a model name myself". */
@ -30,43 +31,65 @@ export function modelSelectValue(
return custom || !isListedModel(model, known) ? CUSTOM_MODEL : model;
}
/** The model names in `GET /api/models`'s answer; none for an answer of another shape. */
export function parseKnownModels(body: unknown): string[] {
const models = (body as { models?: unknown } | null)?.models;
return Array.isArray(models)
? models.filter((m): m is string => 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<string[]> | null = null;
function readKnownModels(): Promise<string[]> {
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<string[]>([]);
const [problem, setProblem] = useState<string | null>(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) };
}

View file

@ -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), []);
});