Watch
0
0
Fork
You've already forked hyperhive
0

swarm: /clear starts an agent's next turn on a fresh session

This commit is contained in:
iris 2026-11-16 01:01:31 +01:00
commit fb9d4b5040
3 changed files with 155 additions and 22 deletions

View file

@ -4,6 +4,10 @@
// sees it as an inbox turn from the logged-in user (the controller takes the
// sender from the gateway, not from this request).
//
// Sending exactly `/clear` is not a message: it POSTs
// `/api/agents/{name}/new-session`, which queues a fresh claude session for the
// agent's next turn (`controlRoute`).
//
// While the text is one `/word`, a menu of the agent's commands and skills
// opens above the box (`CommandMenu`): the arrow keys move, Tab or Enter take
// the selected one into the box, Esc closes it. The list is the agent's own
@ -16,6 +20,7 @@ import { useSubmit } from "../../ui/submit/useSubmit.js";
import { CommandMenu } from "./CommandMenu.js";
import {
completion,
controlRoute,
enterSends,
matchCommands,
menuQuery,
@ -47,13 +52,17 @@ export function AgentInputForm({ agentName }: { agentName: string }) {
function submit(e: Event) {
e.preventDefault();
const base = `/api/agents/${encodeURIComponent(agentName)}`;
const route = controlRoute(body, commands);
void run(
() =>
fetch(`/api/agents/${encodeURIComponent(agentName)}/input`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ body }),
}),
route
? fetch(`${base}/${route}`, { method: "POST" })
: fetch(`${base}/input`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ body }),
}),
{ onDone: () => setBody("") },
);
}

View file

@ -92,3 +92,19 @@ export function moveSelection(
export function enterSends(text: string, selected: AgentCommand): boolean {
return text.trim() === `/${selected.name}`;
}
/**
* The route, under `/api/agents/{name}/`, a sent text stands for when it is one
* of the swarm's own control commands, else `null` for a plain message. Only
* the list the backend served counts: `/clear` is a command there, so an agent
* with a skill of that name (which the backend drops) still sends text.
*/
export function controlRoute(
text: string,
commands: AgentCommand[],
): "new-session" | null {
const isClear =
text.trim() === "/clear" &&
commands.some((c) => c.kind === "command" && c.name === "clear");
return isClear ? "new-session" : null;
}

View file

@ -4,6 +4,7 @@ import assert from "node:assert/strict";
import { test } from "node:test";
import {
completion,
controlRoute,
enterSends,
matchCommands,
menuQuery,
@ -98,3 +99,17 @@ test("Enter sends once the selected command is already typed in full", () => {
assert.equal(enterSends("/comp", cmd("compact")), false);
assert.equal(enterSends("/", cmd("compact")), false);
});
test("exactly /clear, with the backend's command of that name, is a new session", () => {
const list = [cmd("compact"), cmd("clear")];
assert.equal(controlRoute("/clear", list), "new-session");
assert.equal(controlRoute("/clear ", list), "new-session");
assert.equal(controlRoute("/clear it", list), null);
assert.equal(controlRoute("/compact", list), null);
assert.equal(controlRoute("clear", list), null);
});
test("without the backend's /clear command, or with a skill of that name, it is text", () => {
assert.equal(controlRoute("/clear", []), null);
assert.equal(controlRoute("/clear", [cmd("clear", "skill")]), null);
});