diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index a4a2ae10e..2e8673353 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -831,6 +831,7 @@ fresh-session threshold and the soft context limit, which each read "its own" wh stores; saving does a `PUT /api/agents/{agent}/inference` of the whole value. A field you leave empty stays out of it, so the agent keeps its own value for that field; an empty max subagents is no cap, and the two context limits share one slider with two handles on the model's context window, from 5 to 100 percent: a turn that finds the prompt cache cold starts a fresh session once the context passes the left (green) handle, and the agent compacts at the end of a turn once it passes the right (purple) one, and a turn can run past it. The handles can't cross. A handle the swarm hasn't set sits at the harness's own value (50 and 75), dimmed; **use its own** on its line puts it back there, which leaves that field out of 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 card reads `GET /api/agents/{agent}/inference`; before the agent's first report reaches the controller, it shows the swarm's stored values only. [inference](../swarm/inference.md) describes the settings themselves. diff --git a/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx b/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx index 93aec6554..0bb99d066 100644 --- a/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx +++ b/frontend/packages/swarm-ui/src/agent/detail/AgentInference.tsx @@ -26,6 +26,13 @@ import { FormStack } from "../../ui/form-stack/FormStack.js"; import "../../ui/badge-row/BadgeRow.css"; import "./AgentInference.css"; import { ContextLimits } from "./ContextLimits.js"; +import { + CUSTOM_MODEL, + isListedModel, + modelOptions, + modelSelectValue, + useKnownModels, +} from "./model-choice.js"; import { MAX_SUBAGENTS, maxSubagentsValue } from "./max-subagents.js"; import { MAX_SOFT_CONTEXT_PERCENT, @@ -157,6 +164,48 @@ export function draftBody(d: InferenceDraft): Settings { return body; } +// The model as a dropdown of the models the swarm knows, with "other…" opening +// a text box for any model name the swarm accepts. +function ModelField({ + id, + value, + onChange, +}: { + id: string; + value: string; + onChange: (model: string) => void; +}) { + const known = 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 ( + <> + { + setCustom(choice === CUSTOM_MODEL); + if (choice !== CUSTOM_MODEL) onChange(choice); + else if (isListedModel(value, known)) onChange(""); + }} + /> + {open ? ( + + ) : null} + + ); +} + export function InferenceFields({ draft, onChange, @@ -171,12 +220,10 @@ export function InferenceFields({ const problem = draftProblem(draft); return ( <> - onChange({ ...draft, model })} + onChange={(model) => onChange({ ...draft, model })} /> onChange({ ...draft, subagents })} /> ({ value: m, label: m })), + { value: CUSTOM_MODEL, label: "other…" }, + ]; +} + +/** Whether `model` is one the dropdown lists itself, so it needs no text box. */ +export function isListedModel(model: string, known: string[]): boolean { + return model === "" || known.includes(model); +} + +/** The dropdown's value for `model`, given whether the custom entry is open. */ +export function modelSelectValue( + model: string, + known: string[], + custom: boolean, +): string { + return custom || !isListedModel(model, known) ? CUSTOM_MODEL : model; +} + +// 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. +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((body) => { + const models = body.models ?? []; + // 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(() => { + cached = null; + return []; + }); + return cached; +} + +/** The model names the swarm knows; empty until they are read. */ +export function useKnownModels(): string[] { + const [models, setModels] = useState([]); + useEffect(() => { + let cancelled = false; + void readKnownModels().then((m) => { + if (!cancelled) setModels(m); + }); + return () => { + cancelled = true; + }; + }, []); + return models; +} diff --git a/frontend/packages/swarm-ui/test/model-choice.test.mjs b/frontend/packages/swarm-ui/test/model-choice.test.mjs new file mode 100644 index 000000000..4962dc53b --- /dev/null +++ b/frontend/packages/swarm-ui/test/model-choice.test.mjs @@ -0,0 +1,40 @@ +// The model dropdown: which names it lists itself and when the text box opens. +// Run by `npm test`, which imports the `.ts` module as it is. +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { + CUSTOM_MODEL, + isListedModel, + modelOptions, + modelSelectValue, +} from "../src/agent/detail/model-choice.ts"; + +const KNOWN = ["haiku", "opus"]; + +test("the options are the agent's own, the known models and other", () => { + assert.deepEqual( + modelOptions(KNOWN).map((o) => o.value), + ["", "haiku", "opus", CUSTOM_MODEL], + ); + assert.deepEqual( + modelOptions([]).map((o) => o.value), + ["", CUSTOM_MODEL], + ); +}); + +test("the agent's own value and the known models are listed", () => { + assert.ok(isListedModel("", KNOWN)); + assert.ok(isListedModel("opus", KNOWN)); + assert.ok(!isListedModel("claude-opus-4-8", KNOWN)); +}); + +test("a name the list lacks selects the custom entry", () => { + assert.equal(modelSelectValue("claude-opus-4-8", KNOWN, false), CUSTOM_MODEL); + assert.equal(modelSelectValue("opus", KNOWN, false), "opus"); + assert.equal(modelSelectValue("", KNOWN, false), ""); +}); + +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); +});