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
2 changed files with 70 additions and 22 deletions
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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) };
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue