swarm ui: the instructions section holds the AGENTS.md editor itself
The section shows the editor, with save, undo and the conflict handling, instead of an edit badge that opens it in a dialog. It loads when the section is first opened and keeps an unsaved edit while the section is mounted, so the kept-edit wrapper the dialog needed goes away.
This commit is contained in:
parent
ea84d746dd
commit
ded017953b
3 changed files with 36 additions and 67 deletions
|
|
@ -196,7 +196,7 @@ starts open, the other five folded:
|
||||||
its subsystem, one line of text and how long ago it changed (read only,
|
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
|
refetched every ten seconds); its subagents and their terminal preview (next
|
||||||
section); and its bash tasks (see the section on them below).
|
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
|
- **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
|
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
|
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
|
### Editing an agent's instructions
|
||||||
|
|
||||||
The **instructions** section of an agent's details has an **edit** button. It
|
The **instructions** section of an agent's details holds `AGENTS.md`, the file
|
||||||
opens `AGENTS.md`, the file the agent reads its instructions from, in a plain
|
the agent reads its instructions from, in a plain text box. The box loads when
|
||||||
text box. The text is the copy the agent last published, so the box still opens
|
you first open the section. The text is the copy the agent last published, so
|
||||||
for a stopped agent, but **save** needs the agent running.
|
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
|
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
|
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 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
|
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
|
**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
|
other reason shows git's message and leaves the file as it was. Folding the
|
||||||
dialog keeps an unsaved edit for the next time you open it.
|
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
|
### Resource caps
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
/* <AgentInstructions> — the AGENTS.md editor in a dialog. */
|
/* <AgentInstructions> — the AGENTS.md editor in the instructions section. */
|
||||||
.ui-agent-instructions-body,
|
.ui-agent-instructions-body,
|
||||||
.ui-agent-instructions-form {
|
.ui-agent-instructions-form {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
@ -6,8 +6,9 @@
|
||||||
align-items: stretch;
|
align-items: stretch;
|
||||||
gap: 0.75em;
|
gap: 0.75em;
|
||||||
}
|
}
|
||||||
.ui-agent-instructions-body {
|
.ui-agent-instructions-note {
|
||||||
margin-top: 1em;
|
margin: 0 0 0.75em;
|
||||||
|
color: var(--muted);
|
||||||
}
|
}
|
||||||
.ui-agent-instructions-text textarea {
|
.ui-agent-instructions-text textarea {
|
||||||
font-family: var(--mono, ui-monospace, monospace);
|
font-family: var(--mono, ui-monospace, monospace);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
// <AgentInstructions> — an editor for an agent's AGENTS.md, in a dialog opened
|
// <AgentInstructions> — an editor for an agent's AGENTS.md, in the details'
|
||||||
// from the agent's details.
|
// instructions section.
|
||||||
//
|
//
|
||||||
// It loads the copy the agent's harness last published
|
// It loads the copy the agent's harness last published
|
||||||
// (`GET /api/agents/{name}/agents-md`, so a stopped agent shows the text it
|
// (`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
|
// agent's version beside it, and offers the two ways on: save over it, or take
|
||||||
// the agent's version.
|
// the agent's version.
|
||||||
//
|
//
|
||||||
// Closing the dialog does not lose an unsaved edit: the wrapper keeps it, and
|
// The section builds when it is first opened and stays mounted after a fold,
|
||||||
// the next open starts from it (and notices if the agent moved on).
|
// so an unsaved edit survives folding it. Switching to another agent remounts
|
||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
// 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 { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.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 { ActionRow } from "../../ui/action-row/ActionRow.js";
|
||||||
import { Button } from "../../ui/button/Button.js";
|
import { Button } from "../../ui/button/Button.js";
|
||||||
import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js";
|
import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js";
|
||||||
import { Preformatted } from "../../ui/preformatted/Preformatted.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 { TextAreaField } from "../../ui/text-area-field/TextAreaField.js";
|
||||||
import {
|
import {
|
||||||
editBody,
|
editBody,
|
||||||
|
|
@ -39,38 +39,12 @@ interface Unsaved {
|
||||||
text: string;
|
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<string, Unsaved>();
|
||||||
|
|
||||||
export function AgentInstructions({ agent }: { agent: string }) {
|
export function AgentInstructions({ agent }: { agent: string }) {
|
||||||
const [editing, setEditing] = useState(false);
|
return <InstructionsForm agent={agent} />;
|
||||||
const unsaved = useRef<Unsaved | null>(null);
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Badge
|
|
||||||
variant="quiet"
|
|
||||||
value="edit"
|
|
||||||
ariaLabel={`edit ${agent}'s AGENTS.md`}
|
|
||||||
title={`read and change ${agent}'s AGENTS.md`}
|
|
||||||
onClick={() => setEditing(true)}
|
|
||||||
/>
|
|
||||||
<Dialog
|
|
||||||
open={editing}
|
|
||||||
onClose={() => setEditing(false)}
|
|
||||||
label={`edit ${agent}'s AGENTS.md`}
|
|
||||||
plain
|
|
||||||
>
|
|
||||||
{/* Remounted per open, so it starts from the kept edit or a fresh read. */}
|
|
||||||
{editing ? (
|
|
||||||
<InstructionsForm
|
|
||||||
agent={agent}
|
|
||||||
kept={unsaved.current}
|
|
||||||
onKeep={(u) => {
|
|
||||||
unsaved.current = u;
|
|
||||||
}}
|
|
||||||
onClose={() => setEditing(false)}
|
|
||||||
/>
|
|
||||||
) : null}
|
|
||||||
</Dialog>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Load =
|
type Load =
|
||||||
|
|
@ -93,17 +67,7 @@ function asProblem(e: unknown): ProblemDetails {
|
||||||
return { detail: String(e) };
|
return { detail: String(e) };
|
||||||
}
|
}
|
||||||
|
|
||||||
function InstructionsForm({
|
function InstructionsForm({ agent }: { agent: string }) {
|
||||||
agent,
|
|
||||||
kept,
|
|
||||||
onKeep,
|
|
||||||
onClose,
|
|
||||||
}: {
|
|
||||||
agent: string;
|
|
||||||
kept: Unsaved | null;
|
|
||||||
onKeep: (u: Unsaved | null) => void;
|
|
||||||
onClose: () => void;
|
|
||||||
}) {
|
|
||||||
const [load, setLoad] = useState<Load>({ status: "loading" });
|
const [load, setLoad] = useState<Load>({ status: "loading" });
|
||||||
// The version the draft is made against, and the draft.
|
// The version the draft is made against, and the draft.
|
||||||
const [base, setBase] = useState<Instructions | null>(null);
|
const [base, setBase] = useState<Instructions | null>(null);
|
||||||
|
|
@ -136,7 +100,8 @@ function InstructionsForm({
|
||||||
fetchInstructions(agent)
|
fetchInstructions(agent)
|
||||||
.then((current) => {
|
.then((current) => {
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
if (kept === null) {
|
const kept = drafts.get(agent);
|
||||||
|
if (kept === undefined) {
|
||||||
setBase(current);
|
setBase(current);
|
||||||
setDraft(current.content);
|
setDraft(current.content);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -152,13 +117,14 @@ function InstructionsForm({
|
||||||
return () => {
|
return () => {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
};
|
};
|
||||||
// `kept` is only the starting point, read once when the form mounts.
|
|
||||||
}, [agent]);
|
}, [agent]);
|
||||||
|
|
||||||
const dirty = base !== null && draft !== base.content;
|
const dirty = base !== null && draft !== base.content;
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (base !== null) onKeep(dirty ? { base, text: draft } : null);
|
if (base === null) return;
|
||||||
}, [base, draft, dirty]);
|
if (dirty) drafts.set(agent, { base, text: draft });
|
||||||
|
else drafts.delete(agent);
|
||||||
|
}, [agent, base, draft, dirty]);
|
||||||
|
|
||||||
async function save(e: Event) {
|
async function save(e: Event) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
@ -226,8 +192,8 @@ function InstructionsForm({
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Panel title={`AGENTS.md — ${agent}`} icon="📜" onClose={onClose}>
|
<>
|
||||||
<p>
|
<p class="ui-agent-instructions-note">
|
||||||
The instructions {agent} reads. This is the copy the agent last
|
The instructions {agent} reads. This is the copy the agent last
|
||||||
published, so a stopped agent still shows. Saving needs the agent
|
published, so a stopped agent still shows. Saving needs the agent
|
||||||
running: it applies your change to its own file, and refuses when it
|
running: it applies your change to its own file, and refuses when it
|
||||||
|
|
@ -319,6 +285,6 @@ function InstructionsForm({
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
{confirmDialog}
|
{confirmDialog}
|
||||||
</Panel>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue