diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index b74f64e46..a0c33f675 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -196,7 +196,7 @@ starts open, the other five folded: its subsystem, one line of text and how long ago it changed (read only, refetched every ten seconds); its subagents and their terminal preview (next section); and its bash tasks (see the section on them below). -- **instructions**: the agent's AGENTS.md, with an **edit** button. +- **instructions**: the agent's AGENTS.md, as an editor you can type in. - **inference**: how the agent thinks and what it costs: its model and effort and the other inference settings, its resource caps, and the claude login and API key (the login's state, **log in**, and **set API key**), since the key is @@ -905,10 +905,10 @@ controller, it shows the swarm's stored values only. ### Editing an agent's instructions -The **instructions** section of an agent's details has an **edit** button. It -opens `AGENTS.md`, the file the agent reads its instructions from, in a plain -text box. The text is the copy the agent last published, so the box still opens -for a stopped agent, but **save** needs the agent running. +The **instructions** section of an agent's details holds `AGENTS.md`, the file +the agent reads its instructions from, in a plain text box. The box loads when +you first open the section. The text is the copy the agent last published, so +the box still opens for a stopped agent, but **save** needs the agent running. Save sends the swarm a diff against the version you opened, and the agent applies it to its own file with git. When the agent changed the file in the @@ -916,8 +916,10 @@ meantime, the swarm refuses the diff. The box then keeps your text, shows the agent's version below it, and offers two ways on: **keep my text, replace the agent's changes**, which makes your text the new base for the next save, or **use the agent's version**, which drops yours. A diff that git refuses for any -other reason shows git's message and leaves the file as it was. Closing the -dialog keeps an unsaved edit for the next time you open it. +other reason shows git's message and leaves the file as it was. Folding the +section keeps an unsaved edit, and so does switching to another agent: coming +back to this one starts from your text and notices if the agent changed the file +meanwhile. A reload of the page drops it. ### Resource caps diff --git a/frontend/packages/swarm-ui/src/agent/detail/AgentInstructions.css b/frontend/packages/swarm-ui/src/agent/detail/AgentInstructions.css index 3bcf2cded..d8f3bfc1d 100644 --- a/frontend/packages/swarm-ui/src/agent/detail/AgentInstructions.css +++ b/frontend/packages/swarm-ui/src/agent/detail/AgentInstructions.css @@ -1,4 +1,4 @@ -/* — the AGENTS.md editor in a dialog. */ +/* — the AGENTS.md editor in the instructions section. */ .ui-agent-instructions-body, .ui-agent-instructions-form { display: flex; @@ -6,8 +6,9 @@ align-items: stretch; gap: 0.75em; } -.ui-agent-instructions-body { - margin-top: 1em; +.ui-agent-instructions-note { + margin: 0 0 0.75em; + color: var(--muted); } .ui-agent-instructions-text textarea { font-family: var(--mono, ui-monospace, monospace); diff --git a/frontend/packages/swarm-ui/src/agent/detail/AgentInstructions.tsx b/frontend/packages/swarm-ui/src/agent/detail/AgentInstructions.tsx index 7aecb4573..99f6558ee 100644 --- a/frontend/packages/swarm-ui/src/agent/detail/AgentInstructions.tsx +++ b/frontend/packages/swarm-ui/src/agent/detail/AgentInstructions.tsx @@ -1,5 +1,5 @@ -// — an editor for an agent's AGENTS.md, in a dialog opened -// from the agent's details. +// — an editor for an agent's AGENTS.md, in the details' +// instructions section. // // It loads the copy the agent's harness last published // (`GET /api/agents/{name}/agents-md`, so a stopped agent shows the text it @@ -10,18 +10,18 @@ // agent's version beside it, and offers the two ways on: save over it, or take // the agent's version. // -// Closing the dialog does not lose an unsaved edit: the wrapper keeps it, and -// the next open starts from it (and notices if the agent moved on). -import { useEffect, useRef, useState } from "preact/hooks"; +// The section builds when it is first opened and stays mounted after a fold, +// so an unsaved edit survives folding it. Switching to another agent remounts +// it (callers key it by agent name), so a dirty draft is kept per agent for the +// visit: coming back to that agent starts from it, and notices if the agent +// changed the file meanwhile. +import { useEffect, 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 { Button } from "../../ui/button/Button.js"; import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js"; import { Preformatted } from "../../ui/preformatted/Preformatted.js"; -import { Dialog } from "../../ui/dialog/Dialog.js"; -import { Panel } from "../../ui/panel/Panel.js"; import { TextAreaField } from "../../ui/text-area-field/TextAreaField.js"; import { editBody, @@ -39,38 +39,12 @@ interface Unsaved { text: string; } +// The unsaved edits of this visit, by agent. Read once when an agent's form +// mounts and written as the draft changes. +const drafts = new Map(); + export function AgentInstructions({ agent }: { agent: string }) { - const [editing, setEditing] = useState(false); - const unsaved = useRef(null); - return ( - <> - setEditing(true)} - /> - setEditing(false)} - label={`edit ${agent}'s AGENTS.md`} - plain - > - {/* Remounted per open, so it starts from the kept edit or a fresh read. */} - {editing ? ( - { - unsaved.current = u; - }} - onClose={() => setEditing(false)} - /> - ) : null} - - - ); + return ; } type Load = @@ -93,17 +67,7 @@ function asProblem(e: unknown): ProblemDetails { return { detail: String(e) }; } -function InstructionsForm({ - agent, - kept, - onKeep, - onClose, -}: { - agent: string; - kept: Unsaved | null; - onKeep: (u: Unsaved | null) => void; - onClose: () => void; -}) { +function InstructionsForm({ agent }: { agent: string }) { const [load, setLoad] = useState({ status: "loading" }); // The version the draft is made against, and the draft. const [base, setBase] = useState(null); @@ -136,7 +100,8 @@ function InstructionsForm({ fetchInstructions(agent) .then((current) => { if (cancelled) return; - if (kept === null) { + const kept = drafts.get(agent); + if (kept === undefined) { setBase(current); setDraft(current.content); } else { @@ -152,13 +117,14 @@ function InstructionsForm({ return () => { cancelled = true; }; - // `kept` is only the starting point, read once when the form mounts. }, [agent]); const dirty = base !== null && draft !== base.content; useEffect(() => { - if (base !== null) onKeep(dirty ? { base, text: draft } : null); - }, [base, draft, dirty]); + if (base === null) return; + if (dirty) drafts.set(agent, { base, text: draft }); + else drafts.delete(agent); + }, [agent, base, draft, dirty]); async function save(e: Event) { e.preventDefault(); @@ -226,8 +192,8 @@ function InstructionsForm({ } return ( - -

+ <> +

The instructions {agent} reads. This is the copy the agent last published, so a stopped agent still shows. Saving needs the agent running: it applies your change to its own file, and refuses when it @@ -319,6 +285,6 @@ function InstructionsForm({ ) : null} {confirmDialog} - + ); }