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:
parent
da5c52e4c8
commit
479cdd6efb
4 changed files with 70 additions and 22 deletions
|
|
@ -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
|
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 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
|
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
|
name, and a stored name the list lacks opens it too. When the swarm can't read
|
||||||
typed list. The editor reads `GET /api/agents/{agent}/inference`; before the
|
the list, a warning says why, with a **retry** button, and the dropdown has only
|
||||||
agent's first report reaches the controller, it shows no running line.
|
those two entries. The subagent models stay a typed list. The editor reads `GET
|
||||||
[inference](../swarm/inference.md) describes the settings themselves.
|
/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
|
### Editing an agent's instructions
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -163,13 +163,22 @@ function ModelField({
|
||||||
value: string;
|
value: string;
|
||||||
onChange: (model: string) => void;
|
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
|
// A name the list lacks opens the box by itself; picking "other…" opens it
|
||||||
// too, and keeps it open while the name is typed.
|
// too, and keeps it open while the name is typed.
|
||||||
const [custom, setCustom] = useState(false);
|
const [custom, setCustom] = useState(false);
|
||||||
const open = custom || !isListedModel(value, known);
|
const open = custom || !isListedModel(value, known);
|
||||||
return (
|
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
|
<SelectField
|
||||||
id={id}
|
id={id}
|
||||||
label="model"
|
label="model"
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// `GET /api/models`), plus the agent's own value and a custom entry. No model
|
// `GET /api/models`), plus the agent's own value and a custom entry. No model
|
||||||
// name lives here.
|
// name lives here.
|
||||||
import { useEffect, useState } from "preact/hooks";
|
import { useEffect, useState } from "preact/hooks";
|
||||||
|
import { problemMessage, readApiError } from "@hive/shared/api-error.js";
|
||||||
import type { SelectOption } from "../../ui/select-field/SelectField.js";
|
import type { SelectOption } from "../../ui/select-field/SelectField.js";
|
||||||
|
|
||||||
/** The select's value for "type a model name myself". */
|
/** 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;
|
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
|
// 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
|
// once. A failed or empty read isn't kept, so the next form tries again.
|
||||||
// meanwhile the dropdown has only its own and custom entries.
|
|
||||||
let cached: Promise<string[]> | null = null;
|
let cached: Promise<string[]> | null = null;
|
||||||
|
|
||||||
function readKnownModels(): Promise<string[]> {
|
function readKnownModels(): Promise<string[]> {
|
||||||
cached ??= fetch("/api/models")
|
cached ??= fetch("/api/models")
|
||||||
.then((r) =>
|
.then(async (r) => {
|
||||||
r.ok
|
if (r.ok) return (await r.json()) as unknown;
|
||||||
? (r.json() as Promise<{ models?: string[] }>)
|
throw new Error(problemMessage(await readApiError(r)));
|
||||||
: Promise.reject(new Error(`http ${r.status}`)),
|
})
|
||||||
)
|
|
||||||
.then((body) => {
|
.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
|
// An empty answer isn't kept: on a fresh swarm the first agent to report
|
||||||
// a model would otherwise stay missing until a reload.
|
// a model would otherwise stay missing until a reload.
|
||||||
if (models.length === 0) cached = null;
|
if (models.length === 0) cached = null;
|
||||||
return models;
|
return models;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch((e: unknown) => {
|
||||||
cached = null;
|
cached = null;
|
||||||
return [];
|
throw e;
|
||||||
});
|
});
|
||||||
return cached;
|
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 [models, setModels] = useState<string[]>([]);
|
||||||
|
const [problem, setProblem] = useState<string | null>(null);
|
||||||
|
const [attempt, setAttempt] = useState(0);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
void readKnownModels().then((m) => {
|
readKnownModels().then(
|
||||||
if (!cancelled) setModels(m);
|
(m) => {
|
||||||
});
|
if (cancelled) return;
|
||||||
|
setModels(m);
|
||||||
|
setProblem(null);
|
||||||
|
},
|
||||||
|
(e: unknown) => {
|
||||||
|
if (!cancelled) setProblem(e instanceof Error ? e.message : String(e));
|
||||||
|
},
|
||||||
|
);
|
||||||
return () => {
|
return () => {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
};
|
};
|
||||||
}, []);
|
}, [attempt]);
|
||||||
return models;
|
return { models, problem, retry: () => setAttempt((n) => n + 1) };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import {
|
||||||
isListedModel,
|
isListedModel,
|
||||||
modelOptions,
|
modelOptions,
|
||||||
modelSelectValue,
|
modelSelectValue,
|
||||||
|
parseKnownModels,
|
||||||
} from "../src/agent/detail/model-choice.ts";
|
} from "../src/agent/detail/model-choice.ts";
|
||||||
|
|
||||||
const KNOWN = ["haiku", "opus"];
|
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("", KNOWN, true), CUSTOM_MODEL);
|
||||||
assert.equal(modelSelectValue("opus", 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), []);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue