swarm-ui: edit an agent's inference in place, and move the resource caps to access
The inference section was one wrapping line of badges, with the CPU and memory caps among them, and an edit button that opened the same fields in a dialog. The editor is now the section itself: a line says what the agent runs on now and where it comes from, and the fields sit in one row where there is room, with save, reset and clear below. Unsaved edits are kept per agent. The claude login and API key row stays below. The caps are not inference settings, so they become a row of the access section.
This commit is contained in:
parent
ef4c9214f7
commit
e4cab003d1
5 changed files with 376 additions and 305 deletions
|
|
@ -2,11 +2,11 @@
|
|||
// is doing, `AgentIdentity`), then folded sections (`sections.ts`), one for
|
||||
// each question an operator comes with: the terminal, what it is working on
|
||||
// (activity), its instructions, how it thinks (inference, which holds the
|
||||
// claude login and API key), what it may reach (access) and which accounts it
|
||||
// is linked to. Each section builds when first opened. In the details popover
|
||||
// on a terminal page (`AgentDetailPopover`) it is the identity card without
|
||||
// the name, then the four settings sections, since the page is already beside
|
||||
// a terminal.
|
||||
// claude login and API key), what it may reach and use (access, which holds
|
||||
// the CPU and memory caps) and which accounts it is linked to. Each section
|
||||
// builds when first opened. In the details popover on a terminal page
|
||||
// (`AgentDetailPopover`) it is the identity card without the name, then the
|
||||
// four settings sections, since the page is already beside a terminal.
|
||||
//
|
||||
// Takes the agent's *live* row and the shared `AgentActions`, so a state
|
||||
// change or a link made here shows wherever else the same row is drawn.
|
||||
|
|
@ -15,7 +15,11 @@ import { Hint } from "../../ui/hint/Hint.js";
|
|||
import type { AgentActions } from "../roster/AgentActions.js";
|
||||
import { AgentApiKey } from "./AgentApiKey.js";
|
||||
import { AgentClaudeLogin } from "./AgentClaudeLogin.js";
|
||||
import { AgentInference } from "./AgentInference.js";
|
||||
import {
|
||||
AgentInference,
|
||||
InferencePair,
|
||||
InferenceRows,
|
||||
} from "./AgentInference.js";
|
||||
import { AgentInstructions } from "./AgentInstructions.js";
|
||||
import { AgentLimits } from "./AgentLimits.js";
|
||||
import { AgentSsh } from "./AgentSsh.js";
|
||||
|
|
@ -43,27 +47,17 @@ import { BashTasks } from "../terminal/BashTasks.js";
|
|||
import { SubagentTerms } from "../terminal/SubagentTerms.js";
|
||||
import "./AgentDetail.css";
|
||||
|
||||
function InferenceRows({
|
||||
agent,
|
||||
hive,
|
||||
}: {
|
||||
agent: string;
|
||||
hive: string | null;
|
||||
}) {
|
||||
// How the agent signs in to claude: the login and, on a hive, its API key.
|
||||
function ClaudeRows({ agent, hive }: { agent: string; hive: string | null }) {
|
||||
return (
|
||||
<>
|
||||
<dt>resource caps</dt>
|
||||
<dd>
|
||||
<AgentLimits key={agent} agent={agent} />
|
||||
</dd>
|
||||
<dt>claude login</dt>
|
||||
<dd>
|
||||
<InferenceRows>
|
||||
<InferencePair label="claude login">
|
||||
<AgentClaudeLogin key={agent} agent={agent} />
|
||||
{hive ? (
|
||||
<AgentApiKey key={`${hive}/${agent}`} hive={hive} agent={agent} />
|
||||
) : null}
|
||||
</dd>
|
||||
</>
|
||||
</InferencePair>
|
||||
</InferenceRows>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -168,15 +162,17 @@ function SectionBody({
|
|||
return (
|
||||
<>
|
||||
<AgentInference key={row.name} agent={row.name} />
|
||||
<dl class="ui-agent-detail-fields">
|
||||
<InferenceRows agent={row.name} hive={row.hive} />
|
||||
</dl>
|
||||
<ClaudeRows agent={row.name} hive={row.hive} />
|
||||
</>
|
||||
);
|
||||
case "access":
|
||||
return (
|
||||
<dl class="ui-agent-detail-fields">
|
||||
<GrantRows key={row.name} agent={row.name} />
|
||||
<dt>resource caps</dt>
|
||||
<dd>
|
||||
<AgentLimits key={row.name} agent={row.name} />
|
||||
</dd>
|
||||
</dl>
|
||||
);
|
||||
case "accounts":
|
||||
|
|
|
|||
|
|
@ -1,22 +1,42 @@
|
|||
/* <AgentInference> — the settings in one wrapping line: each badge, then where it comes from. */
|
||||
/* <AgentInference> — the settings as labelled values, two to a line where there
|
||||
is room, each value followed by where it comes from. */
|
||||
.ui-agent-inference {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 0.3em;
|
||||
gap: 0.5em;
|
||||
}
|
||||
.ui-agent-inference-list {
|
||||
display: contents;
|
||||
list-style: none;
|
||||
.ui-agent-inference-rows {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(22em, 1fr));
|
||||
gap: 0.3em 2em;
|
||||
align-self: stretch;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
.ui-agent-inference-list li {
|
||||
display: contents;
|
||||
.ui-agent-inference-pair {
|
||||
display: grid;
|
||||
grid-template-columns: 12em minmax(0, 1fr);
|
||||
gap: 0 1em;
|
||||
}
|
||||
.ui-agent-inference-source {
|
||||
margin-left: -0.25em;
|
||||
.ui-agent-inference-pair dt {
|
||||
color: var(--muted);
|
||||
}
|
||||
.ui-agent-inference-pair dd {
|
||||
margin: 0;
|
||||
}
|
||||
.ui-agent-inference-source {
|
||||
color: var(--muted);
|
||||
}
|
||||
.ui-agent-inference-now {
|
||||
margin: 0;
|
||||
}
|
||||
.ui-agent-inference .ui-form-stack {
|
||||
align-items: stretch;
|
||||
align-self: stretch;
|
||||
max-width: 60em;
|
||||
margin-top: 0;
|
||||
}
|
||||
/* The claude login row under the editor. */
|
||||
.ui-agent-inference + .ui-agent-inference-rows {
|
||||
margin-top: 1em;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,28 @@
|
|||
// <AgentInference> — one agent's inference settings in an agent's details: the
|
||||
// model and effort it runs on now, with where each came from, and an editor
|
||||
// for the value the swarm sets.
|
||||
// <AgentInference> — one agent's inference settings in an agent's details, as
|
||||
// the editor itself: boxes filled with the value the swarm stores, and above
|
||||
// them the model and effort the agent runs on now, with where each came from.
|
||||
//
|
||||
// Reads `GET /api/agents/{name}/inference`, which answers
|
||||
// `{ configured, effective }`: `configured` is the swarm's stored value (null
|
||||
// when none), `effective` what the agent reported running on (null until its
|
||||
// first report reaches the controller). The editor PUTs the whole value and
|
||||
// the clear action DELETEs it; a cleared field puts the agent back on its own
|
||||
// value, which is why an empty field is left out of the body rather than sent.
|
||||
// first report reaches the controller). Save PUTs the whole value and the clear
|
||||
// action DELETEs it; a cleared field puts the agent back on its own value,
|
||||
// which is why an empty field is left out of the body rather than sent. Unsaved
|
||||
// edits are kept per agent, so switching to another agent and back keeps them.
|
||||
//
|
||||
// Its state belongs to one agent: callers key it by agent name, so switching
|
||||
// agents remounts it instead of showing the previous agent's settings.
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import type { ComponentChildren } from "preact";
|
||||
import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { Badge } from "@hive/shared/badge.js";
|
||||
import { ActionRow } from "../../ui/action-row/ActionRow.js";
|
||||
import { Dialog } from "../../ui/dialog/Dialog.js";
|
||||
import { FieldGrid, FieldGridFull } from "../../ui/field-grid/FieldGrid.js";
|
||||
import { Hint } from "../../ui/hint/Hint.js";
|
||||
import { Panel } from "../../ui/panel/Panel.js";
|
||||
import { SelectField } from "../../ui/select-field/SelectField.js";
|
||||
import { TextField } from "../../ui/text-field/TextField.js";
|
||||
import { Button } from "../../ui/button/Button.js";
|
||||
import { FormStack } from "../../ui/form-stack/FormStack.js";
|
||||
import "../../ui/badge-row/BadgeRow.css";
|
||||
import "./AgentInference.css";
|
||||
import { ContextLimits } from "./ContextLimits.js";
|
||||
import {
|
||||
|
|
@ -33,6 +32,13 @@ import {
|
|||
modelSelectValue,
|
||||
useKnownModels,
|
||||
} from "./model-choice.js";
|
||||
import {
|
||||
draftOf,
|
||||
EMPTY_INFERENCE_DRAFT,
|
||||
rememberDraft,
|
||||
sameDraft,
|
||||
type InferenceDraft,
|
||||
} from "./inference-draft.js";
|
||||
import { MAX_SUBAGENTS, maxSubagentsValue } from "./max-subagents.js";
|
||||
import {
|
||||
MAX_SOFT_CONTEXT_PERCENT,
|
||||
|
|
@ -97,25 +103,7 @@ function inferenceUrl(agent: string): string {
|
|||
return `/api/agents/${encodeURIComponent(agent)}/inference`;
|
||||
}
|
||||
|
||||
// The inference boxes as typed. Shared by the editor here and the
|
||||
// create-agent form, which send what `draftBody` makes of them.
|
||||
export interface InferenceDraft {
|
||||
model: string;
|
||||
effort: string;
|
||||
subagents: string;
|
||||
maxSubagents: string;
|
||||
softContext: string;
|
||||
freshSession: string;
|
||||
}
|
||||
|
||||
export const EMPTY_INFERENCE_DRAFT: InferenceDraft = {
|
||||
model: "",
|
||||
effort: "",
|
||||
subagents: "",
|
||||
maxSubagents: "",
|
||||
softContext: "",
|
||||
freshSession: "",
|
||||
};
|
||||
export { EMPTY_INFERENCE_DRAFT, type InferenceDraft };
|
||||
|
||||
// Why the draft would be refused, or null when it is fine to send.
|
||||
export function draftProblem(d: InferenceDraft): string | null {
|
||||
|
|
@ -220,50 +208,98 @@ export function InferenceFields({
|
|||
const problem = draftProblem(draft);
|
||||
return (
|
||||
<>
|
||||
<ModelField
|
||||
id={`${idPrefix}-model`}
|
||||
value={draft.model}
|
||||
onChange={(model) => onChange({ ...draft, model })}
|
||||
/>
|
||||
<SelectField
|
||||
id={`${idPrefix}-effort`}
|
||||
label="effort"
|
||||
value={draft.effort}
|
||||
options={EFFORT_OPTIONS}
|
||||
onChange={(effort) => onChange({ ...draft, effort })}
|
||||
/>
|
||||
<TextField
|
||||
id={`${idPrefix}-subagent-models`}
|
||||
label="subagent models"
|
||||
value={draft.subagents}
|
||||
placeholder="model names, separated by spaces or commas"
|
||||
onInput={(subagents) => onChange({ ...draft, subagents })}
|
||||
/>
|
||||
<TextField
|
||||
id={`${idPrefix}-max-subagents`}
|
||||
label="max subagents"
|
||||
value={draft.maxSubagents}
|
||||
placeholder="empty for no cap"
|
||||
pattern="[0-9]*"
|
||||
onInput={(maxSubagents) => onChange({ ...draft, maxSubagents })}
|
||||
/>
|
||||
<ContextLimits
|
||||
idPrefix={idPrefix}
|
||||
fresh={draft.freshSession}
|
||||
soft={draft.softContext}
|
||||
onChange={(limits) => onChange({ ...draft, ...limits })}
|
||||
/>
|
||||
<FieldGrid>
|
||||
<ModelField
|
||||
id={`${idPrefix}-model`}
|
||||
value={draft.model}
|
||||
onChange={(model) => onChange({ ...draft, model })}
|
||||
/>
|
||||
<SelectField
|
||||
id={`${idPrefix}-effort`}
|
||||
label="effort"
|
||||
value={draft.effort}
|
||||
options={EFFORT_OPTIONS}
|
||||
onChange={(effort) => onChange({ ...draft, effort })}
|
||||
/>
|
||||
<TextField
|
||||
id={`${idPrefix}-subagent-models`}
|
||||
label="subagent models"
|
||||
value={draft.subagents}
|
||||
placeholder="model names, separated by spaces or commas"
|
||||
onInput={(subagents) => onChange({ ...draft, subagents })}
|
||||
/>
|
||||
<TextField
|
||||
id={`${idPrefix}-max-subagents`}
|
||||
label="max subagents"
|
||||
value={draft.maxSubagents}
|
||||
placeholder="empty for no cap"
|
||||
pattern="[0-9]*"
|
||||
onInput={(maxSubagents) => onChange({ ...draft, maxSubagents })}
|
||||
/>
|
||||
<FieldGridFull>
|
||||
<ContextLimits
|
||||
idPrefix={idPrefix}
|
||||
fresh={draft.freshSession}
|
||||
soft={draft.softContext}
|
||||
onChange={(limits) => onChange({ ...draft, ...limits })}
|
||||
/>
|
||||
</FieldGridFull>
|
||||
</FieldGrid>
|
||||
{problem ? <Hint tone="error">{problem}</Hint> : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** Labelled values, two to a line where there is room. */
|
||||
export function InferenceRows({ children }: { children: ComponentChildren }) {
|
||||
return <dl class="ui-agent-inference-rows">{children}</dl>;
|
||||
}
|
||||
|
||||
/** One label and its value. */
|
||||
export function InferencePair({
|
||||
label,
|
||||
title,
|
||||
valueTitle,
|
||||
children,
|
||||
}: {
|
||||
label: string;
|
||||
/** What the setting does, on the label. */
|
||||
title?: string;
|
||||
/** Why the value is what it is, on the value. */
|
||||
valueTitle?: string;
|
||||
children: ComponentChildren;
|
||||
}) {
|
||||
return (
|
||||
<div class="ui-agent-inference-pair">
|
||||
<dt title={title}>{label}</dt>
|
||||
<dd title={valueTitle}>{children}</dd>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Where a value came from, after the value.
|
||||
function Source({ source }: { source: Source }) {
|
||||
return (
|
||||
<span class="ui-agent-inference-source"> ({SOURCE_LABEL[source]})</span>
|
||||
);
|
||||
}
|
||||
|
||||
// The unsaved edits of this visit, by agent: switching to another agent and
|
||||
// back keeps what was typed. Read once when an agent's editor mounts and
|
||||
// written as the draft changes.
|
||||
const drafts = new Map<string, InferenceDraft>();
|
||||
|
||||
export function AgentInference({ agent }: { agent: string }) {
|
||||
const [view, setView] = useState<View | null>(null);
|
||||
const [error, setError] = useState<ProblemDetails | null>(null);
|
||||
// Bumped after a save or a clear, so the settings are fetched again.
|
||||
const [reload, setReload] = useState(0);
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [draft, setDraft] = useState<InferenceDraft | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [problem, setProblem] = useState<ProblemDetails | null>(null);
|
||||
// Set by a save or a clear: the next read replaces the boxes with the stored
|
||||
// value instead of keeping the typed one.
|
||||
const resetOnLoad = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
|
@ -275,9 +311,14 @@ export function AgentInference({ agent }: { agent: string }) {
|
|||
}
|
||||
const body = (await r.json()) as Partial<View>;
|
||||
if (cancelled) return;
|
||||
setView({
|
||||
configured: body.configured ?? null,
|
||||
effective: body.effective ?? null,
|
||||
const configured = body.configured ?? null;
|
||||
setView({ configured, effective: body.effective ?? null });
|
||||
setDraft((current) => {
|
||||
if (resetOnLoad.current || current === null) {
|
||||
resetOnLoad.current = false;
|
||||
return drafts.get(agent) ?? draftOf(configured);
|
||||
}
|
||||
return current;
|
||||
});
|
||||
setError(null);
|
||||
})().catch((e: unknown) => {
|
||||
|
|
@ -288,6 +329,13 @@ export function AgentInference({ agent }: { agent: string }) {
|
|||
};
|
||||
}, [agent, reload]);
|
||||
|
||||
const base = view === null ? null : draftOf(view.configured);
|
||||
const dirty = draft !== null && base !== null && !sameDraft(draft, base);
|
||||
useEffect(() => {
|
||||
if (draft === null || base === null) return;
|
||||
rememberDraft(drafts, agent, draft, base);
|
||||
}, [agent, draft, base]);
|
||||
|
||||
// Shown as text, not a hover title. Settings already loaded stay under a
|
||||
// failed refetch, so a save that worked is not hidden by a read that did not.
|
||||
const errorPanel = error ? (
|
||||
|
|
@ -296,155 +344,25 @@ export function AgentInference({ agent }: { agent: string }) {
|
|||
problem={error}
|
||||
/>
|
||||
) : null;
|
||||
if (view === null) {
|
||||
if (view === null || draft === null || base === null) {
|
||||
return errorPanel ?? <Hint as="span">…</Hint>;
|
||||
}
|
||||
|
||||
const { configured, effective } = view;
|
||||
const swarmModels = configured?.subagent_models ?? [];
|
||||
return (
|
||||
<div class="ui-agent-inference">
|
||||
{errorPanel}
|
||||
<div class="ui-badge-row">
|
||||
<ul class="ui-agent-inference-list">
|
||||
<li>
|
||||
{effective ? (
|
||||
<>
|
||||
<Badge label="model" value={effective.model} />
|
||||
<span class="ui-agent-inference-source">
|
||||
{SOURCE_LABEL[effective.model_source]}
|
||||
</span>
|
||||
</>
|
||||
) : (
|
||||
<Badge
|
||||
label="model"
|
||||
value={configured?.model ?? "its own"}
|
||||
title="the agent has not reported what it runs on yet"
|
||||
/>
|
||||
)}
|
||||
</li>
|
||||
<li>
|
||||
{effective ? (
|
||||
<>
|
||||
<Badge label="effort" value={effective.effort} />
|
||||
<span class="ui-agent-inference-source">
|
||||
{SOURCE_LABEL[effective.effort_source]}
|
||||
</span>
|
||||
</>
|
||||
) : (
|
||||
<Badge
|
||||
label="effort"
|
||||
value={configured?.effort ?? "its own"}
|
||||
title="the agent has not reported what it runs on yet"
|
||||
/>
|
||||
)}
|
||||
</li>
|
||||
<li>
|
||||
<Badge
|
||||
label="subagent models"
|
||||
value={
|
||||
swarmModels.length > 0 ? swarmModels.join(", ") : "its own"
|
||||
}
|
||||
/>
|
||||
</li>
|
||||
<li>
|
||||
<Badge
|
||||
label="max subagents"
|
||||
value={
|
||||
configured?.max_subagents === 0
|
||||
? "0 (no subagents)"
|
||||
: String(configured?.max_subagents ?? "no cap")
|
||||
}
|
||||
/>
|
||||
</li>
|
||||
<li>
|
||||
<Badge
|
||||
label="fresh-session threshold"
|
||||
value={
|
||||
configured?.fresh_session_percent === undefined
|
||||
? "its own"
|
||||
: `${configured.fresh_session_percent}%`
|
||||
}
|
||||
title="a turn that finds the prompt cache cold starts a fresh session once the context is past this share of the window"
|
||||
/>
|
||||
</li>
|
||||
<li>
|
||||
<Badge
|
||||
label="soft context limit"
|
||||
value={
|
||||
configured?.soft_context_percent === undefined
|
||||
? "its own"
|
||||
: `${configured.soft_context_percent}%`
|
||||
}
|
||||
title="the agent compacts at the end of a turn once its context passes this share of the window; a turn can run past it"
|
||||
/>
|
||||
</li>
|
||||
</ul>
|
||||
<Badge
|
||||
variant="quiet"
|
||||
value="edit"
|
||||
ariaLabel={`edit ${agent}'s inference settings`}
|
||||
title={`set the swarm's model, effort, subagent models, subagent cap and context limits for ${agent}`}
|
||||
onClick={() => setEditing(true)}
|
||||
/>
|
||||
</div>
|
||||
<Dialog
|
||||
open={editing}
|
||||
onClose={() => setEditing(false)}
|
||||
label="edit inference settings"
|
||||
plain
|
||||
>
|
||||
{/* Remounted per open, so a cancelled edit leaves nothing behind. */}
|
||||
{editing ? (
|
||||
<InferenceForm
|
||||
agent={agent}
|
||||
configured={configured}
|
||||
onClose={() => setEditing(false)}
|
||||
onSaved={() => {
|
||||
setEditing(false);
|
||||
setReload((n) => n + 1);
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</Dialog>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function InferenceForm({
|
||||
agent,
|
||||
configured,
|
||||
onClose,
|
||||
onSaved,
|
||||
}: {
|
||||
agent: string;
|
||||
configured: Settings | null;
|
||||
onClose: () => void;
|
||||
onSaved: () => void;
|
||||
}) {
|
||||
const [draft, setDraft] = useState<InferenceDraft>({
|
||||
model: configured?.model ?? "",
|
||||
effort: configured?.effort ?? "",
|
||||
subagents: (configured?.subagent_models ?? []).join(", "),
|
||||
maxSubagents: configured?.max_subagents?.toString() ?? "",
|
||||
softContext: configured?.soft_context_percent?.toString() ?? "",
|
||||
freshSession: configured?.fresh_session_percent?.toString() ?? "",
|
||||
});
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [problem, setProblem] = useState<ProblemDetails | null>(null);
|
||||
|
||||
const formProblem = draftProblem(draft);
|
||||
const empty = draftIsEmpty(draft);
|
||||
|
||||
async function send(init: RequestInit) {
|
||||
setBusy(true);
|
||||
setProblem(null);
|
||||
try {
|
||||
const r = await fetch(inferenceUrl(agent), init);
|
||||
if (!r.ok) {
|
||||
setProblem(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
onSaved();
|
||||
drafts.delete(agent);
|
||||
resetOnLoad.current = true;
|
||||
setReload((n) => n + 1);
|
||||
} catch (e: unknown) {
|
||||
setProblem({ detail: String(e) });
|
||||
} finally {
|
||||
|
|
@ -457,23 +375,20 @@ function InferenceForm({
|
|||
void send({
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(draftBody(draft)),
|
||||
body: JSON.stringify(draftBody(draft as InferenceDraft)),
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<Panel title={`inference — ${agent}`} icon="🧠" onClose={onClose}>
|
||||
<p>
|
||||
Sets what the swarm makes {agent} run on. A field left empty stays the
|
||||
agent's own value; an empty max subagents is no cap. The two context
|
||||
limits are shares of the context window: a turn that finds the prompt
|
||||
cache cold starts a fresh session once the context is past the
|
||||
fresh-session threshold, and the agent compacts at the end of a turn
|
||||
once it is past the soft limit (a turn can run past it). The agent picks
|
||||
a change up within seconds; a turn already running finishes on what it
|
||||
started with, and subagents already running past a lowered cap keep
|
||||
running.
|
||||
</p>
|
||||
<div class="ui-agent-inference">
|
||||
{errorPanel}
|
||||
{effective ? (
|
||||
<Hint class="ui-agent-inference-now">
|
||||
running {effective.model}
|
||||
<Source source={effective.model_source} />, effort {effective.effort}
|
||||
<Source source={effective.effort_source} />
|
||||
</Hint>
|
||||
) : null}
|
||||
<FormStack onSubmit={save}>
|
||||
<InferenceFields draft={draft} onChange={setDraft} />
|
||||
{problem ? (
|
||||
|
|
@ -483,18 +398,28 @@ function InferenceForm({
|
|||
<Button
|
||||
variant="primary"
|
||||
type="submit"
|
||||
disabled={busy || empty || formProblem !== null}
|
||||
busy={busy}
|
||||
disabled={
|
||||
busy || !dirty || draftIsEmpty(draft) || formProblem !== null
|
||||
}
|
||||
>
|
||||
{busy ? "saving…" : "save"}
|
||||
</Button>
|
||||
<Button
|
||||
disabled={busy || configured === null}
|
||||
onClick={() => void send({ method: "DELETE" })}
|
||||
>
|
||||
clear the swarm's setting
|
||||
save
|
||||
</Button>
|
||||
{dirty ? (
|
||||
<Button disabled={busy} onClick={() => setDraft(base)}>
|
||||
reset
|
||||
</Button>
|
||||
) : null}
|
||||
{configured !== null ? (
|
||||
<Button
|
||||
disabled={busy}
|
||||
onClick={() => void send({ method: "DELETE" })}
|
||||
>
|
||||
clear the swarm's setting
|
||||
</Button>
|
||||
) : null}
|
||||
</ActionRow>
|
||||
</FormStack>
|
||||
</Panel>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
// The inference boxes as typed, and the bookkeeping around them, without a DOM:
|
||||
// what a stored value fills them with, whether they differ from it, and which
|
||||
// unsaved drafts are kept while the operator looks at another agent. Shared by
|
||||
// the editor in an agent's details and the create-agent form.
|
||||
|
||||
/** The inference boxes as typed; every one is text, empty for "its own". */
|
||||
export interface InferenceDraft {
|
||||
model: string;
|
||||
effort: string;
|
||||
subagents: string;
|
||||
maxSubagents: string;
|
||||
softContext: string;
|
||||
freshSession: string;
|
||||
}
|
||||
|
||||
export const EMPTY_INFERENCE_DRAFT: InferenceDraft = {
|
||||
model: "",
|
||||
effort: "",
|
||||
subagents: "",
|
||||
maxSubagents: "",
|
||||
softContext: "",
|
||||
freshSession: "",
|
||||
};
|
||||
|
||||
/** The swarm's stored value, every field optional. */
|
||||
export interface StoredInference {
|
||||
model?: string;
|
||||
effort?: string;
|
||||
subagent_models?: string[];
|
||||
max_subagents?: number;
|
||||
soft_context_percent?: number;
|
||||
fresh_session_percent?: number;
|
||||
}
|
||||
|
||||
/** The boxes as a stored value fills them; none stored fills them empty. */
|
||||
export function draftOf(stored: StoredInference | null): InferenceDraft {
|
||||
return {
|
||||
model: stored?.model ?? "",
|
||||
effort: stored?.effort ?? "",
|
||||
subagents: (stored?.subagent_models ?? []).join(", "),
|
||||
maxSubagents: stored?.max_subagents?.toString() ?? "",
|
||||
softContext: stored?.soft_context_percent?.toString() ?? "",
|
||||
freshSession: stored?.fresh_session_percent?.toString() ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
/** Whether two drafts hold the same text in every box. */
|
||||
export function sameDraft(a: InferenceDraft, b: InferenceDraft): boolean {
|
||||
return (
|
||||
a.model === b.model &&
|
||||
a.effort === b.effort &&
|
||||
a.subagents === b.subagents &&
|
||||
a.maxSubagents === b.maxSubagents &&
|
||||
a.softContext === b.softContext &&
|
||||
a.freshSession === b.freshSession
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps `draft` as this agent's unsaved edit while it differs from `base`
|
||||
* (what the swarm stores), and drops the kept one when it doesn't, so an agent
|
||||
* with nothing unsaved leaves nothing behind.
|
||||
*/
|
||||
export function rememberDraft(
|
||||
kept: Map<string, InferenceDraft>,
|
||||
agent: string,
|
||||
draft: InferenceDraft,
|
||||
base: InferenceDraft,
|
||||
): void {
|
||||
if (sameDraft(draft, base)) kept.delete(agent);
|
||||
else kept.set(agent, draft);
|
||||
}
|
||||
59
frontend/packages/swarm-ui/test/inference-draft.test.mjs
Normal file
59
frontend/packages/swarm-ui/test/inference-draft.test.mjs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// The inference boxes and the drafts kept per agent. Run by `npm test`, which
|
||||
// Node runs with its built-in test runner and type stripping.
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import {
|
||||
EMPTY_INFERENCE_DRAFT,
|
||||
draftOf,
|
||||
rememberDraft,
|
||||
sameDraft,
|
||||
} from "../src/agent/detail/inference-draft.ts";
|
||||
|
||||
const stored = {
|
||||
model: "sonnet",
|
||||
effort: "high",
|
||||
subagent_models: ["haiku", "sonnet"],
|
||||
max_subagents: 0,
|
||||
soft_context_percent: 70,
|
||||
fresh_session_percent: 40,
|
||||
};
|
||||
|
||||
test("a stored value fills the boxes, and none stored fills them empty", () => {
|
||||
assert.deepEqual(draftOf(stored), {
|
||||
model: "sonnet",
|
||||
effort: "high",
|
||||
subagents: "haiku, sonnet",
|
||||
maxSubagents: "0",
|
||||
softContext: "70",
|
||||
freshSession: "40",
|
||||
});
|
||||
assert.deepEqual(draftOf(null), EMPTY_INFERENCE_DRAFT);
|
||||
assert.deepEqual(draftOf({}), EMPTY_INFERENCE_DRAFT);
|
||||
});
|
||||
|
||||
test("drafts are the same only when every box matches", () => {
|
||||
const base = draftOf(stored);
|
||||
assert.ok(sameDraft(base, { ...base }));
|
||||
for (const box of Object.keys(base)) {
|
||||
assert.ok(!sameDraft(base, { ...base, [box]: base[box] + "x" }), box);
|
||||
}
|
||||
});
|
||||
|
||||
test("an edited draft is kept for its agent, and dropped once it matches again", () => {
|
||||
const kept = new Map();
|
||||
const base = draftOf(stored);
|
||||
const edited = { ...base, effort: "low" };
|
||||
rememberDraft(kept, "iris", edited, base);
|
||||
assert.deepEqual(kept.get("iris"), edited);
|
||||
rememberDraft(kept, "iris", { ...base }, base);
|
||||
assert.equal(kept.has("iris"), false);
|
||||
});
|
||||
|
||||
test("one agent's draft does not touch another's", () => {
|
||||
const kept = new Map();
|
||||
const base = draftOf(stored);
|
||||
rememberDraft(kept, "iris", { ...base, model: "opus" }, base);
|
||||
rememberDraft(kept, "argus", { ...base }, base);
|
||||
assert.equal(kept.size, 1);
|
||||
assert.equal(kept.get("iris").model, "opus");
|
||||
});
|
||||
Loading…
Reference in a new issue