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:
parent
58236e1b7f
commit
658178f9db
4 changed files with 165 additions and 5 deletions
|
|
@ -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
|
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
|
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`.
|
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
|
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.
|
report reaches the controller, it shows the swarm's stored values only.
|
||||||
[inference](../swarm/inference.md) describes the settings themselves.
|
[inference](../swarm/inference.md) describes the settings themselves.
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,13 @@ import { FormStack } from "../../ui/form-stack/FormStack.js";
|
||||||
import "../../ui/badge-row/BadgeRow.css";
|
import "../../ui/badge-row/BadgeRow.css";
|
||||||
import "./AgentInference.css";
|
import "./AgentInference.css";
|
||||||
import { ContextLimits } from "./ContextLimits.js";
|
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_SUBAGENTS, maxSubagentsValue } from "./max-subagents.js";
|
||||||
import {
|
import {
|
||||||
MAX_SOFT_CONTEXT_PERCENT,
|
MAX_SOFT_CONTEXT_PERCENT,
|
||||||
|
|
@ -157,6 +164,48 @@ export function draftBody(d: InferenceDraft): Settings {
|
||||||
return body;
|
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({
|
export function InferenceFields({
|
||||||
draft,
|
draft,
|
||||||
onChange,
|
onChange,
|
||||||
|
|
@ -171,12 +220,10 @@ export function InferenceFields({
|
||||||
const problem = draftProblem(draft);
|
const problem = draftProblem(draft);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<TextField
|
<ModelField
|
||||||
id={`${idPrefix}-model`}
|
id={`${idPrefix}-model`}
|
||||||
label="model"
|
|
||||||
value={draft.model}
|
value={draft.model}
|
||||||
placeholder="e.g. opus"
|
onChange={(model) => onChange({ ...draft, model })}
|
||||||
onInput={(model) => onChange({ ...draft, model })}
|
|
||||||
/>
|
/>
|
||||||
<SelectField
|
<SelectField
|
||||||
id={`${idPrefix}-effort`}
|
id={`${idPrefix}-effort`}
|
||||||
|
|
@ -189,7 +236,7 @@ export function InferenceFields({
|
||||||
id={`${idPrefix}-subagent-models`}
|
id={`${idPrefix}-subagent-models`}
|
||||||
label="subagent models"
|
label="subagent models"
|
||||||
value={draft.subagents}
|
value={draft.subagents}
|
||||||
placeholder="e.g. haiku, sonnet"
|
placeholder="model names, separated by spaces or commas"
|
||||||
onInput={(subagents) => onChange({ ...draft, subagents })}
|
onInput={(subagents) => onChange({ ...draft, subagents })}
|
||||||
/>
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
|
|
|
||||||
72
frontend/packages/swarm-ui/src/agent/detail/model-choice.ts
Normal file
72
frontend/packages/swarm-ui/src/agent/detail/model-choice.ts
Normal 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;
|
||||||
|
}
|
||||||
40
frontend/packages/swarm-ui/test/model-choice.test.mjs
Normal file
40
frontend/packages/swarm-ui/test/model-choice.test.mjs
Normal 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);
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue