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