Watch
0
0
Fork
You've already forked hyperhive
0

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:
iris 2026-11-22 23:41:47 +01:00 • committed by mara
commit ded017953b
2 changed files with 36 additions and 67 deletions

View file

@ -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-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);

View file

@ -1,5 +1,5 @@
// <AgentInstructions> — an editor for an agent's AGENTS.md, in a dialog opened
// from the agent's details.
// <AgentInstructions> — 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<string, Unsaved>();
export function AgentInstructions({ agent }: { agent: string }) {
const [editing, setEditing] = useState(false);
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>
</>
);
return <InstructionsForm agent={agent} />;
}
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<Load>({ status: "loading" });
// The version the draft is made against, and the draft.
const [base, setBase] = useState<Instructions | null>(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 (
<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
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}
</div>
{confirmDialog}
</Panel>
</>
);
}