Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: pick an agent's model from a dropdown of the models the swarm knows, with an other entry for any name

This commit is contained in:
iris 2026-11-21 14:59:20 +01:00 • committed by mara
commit 658178f9db
3 changed files with 165 additions and 5 deletions

View file

@ -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 (
<>
<SelectField
id={id}
label="model"
value={modelSelectValue(value, known, custom)}
options={modelOptions(known)}
onChange={(choice) => {
setCustom(choice === CUSTOM_MODEL);
if (choice !== CUSTOM_MODEL) onChange(choice);
else if (isListedModel(value, known)) onChange("");
}}
/>
{open ? (
<TextField
id={`${id}-name`}
label="model name"
value={value}
placeholder="a model name claude accepts"
onInput={onChange}
/>
) : null}
</>
);
}
export function InferenceFields({
draft,
onChange,
@ -171,12 +220,10 @@ export function InferenceFields({
const problem = draftProblem(draft);
return (
<>
<TextField
<ModelField
id={`${idPrefix}-model`}
label="model"
value={draft.model}
placeholder="e.g. opus"
onInput={(model) => onChange({ ...draft, model })}
onChange={(model) => onChange({ ...draft, model })}
/>
<SelectField
id={`${idPrefix}-effort`}
@ -189,7 +236,7 @@ export function InferenceFields({
id={`${idPrefix}-subagent-models`}
label="subagent models"
value={draft.subagents}
placeholder="e.g. haiku, sonnet"
placeholder="model names, separated by spaces or commas"
onInput={(subagents) => onChange({ ...draft, subagents })}
/>
<TextField

View file

@ -0,0 +1,72 @@
// The model dropdown's choices: the model names the swarm already knows (from
// `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 type { SelectOption } from "../../ui/select-field/SelectField.js";
/** The select's value for "type a model name myself". */
export const CUSTOM_MODEL = "*other*";
/** The options of the model dropdown, an empty value being the agent's own. */
export function modelOptions(known: string[]): SelectOption[] {
return [
{ value: "", label: "the agent's own" },
...known.map((m) => ({ 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<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((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<string[]>([]);
useEffect(() => {
let cancelled = false;
void readKnownModels().then((m) => {
if (!cancelled) setModels(m);
});
return () => {
cancelled = true;
};
}, []);
return models;
}

View file

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