133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
// <AgentInputForm> — text for one agent, under its swarm terminal.
|
|
// POSTs `/api/agents/{name}/input`: 202 once the agent's harness holds the
|
|
// message, 503 when it is not listening, problem+json otherwise. The agent
|
|
// 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
|
|
// (`useAgentCommands`); an agent without one gets a plain box.
|
|
import { useEffect, useState } from "preact/hooks";
|
|
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
|
import { TextField } from "../../ui/text-field/TextField.js";
|
|
import { Button } from "../../ui/button/Button.js";
|
|
import { useSubmit } from "../../ui/submit/useSubmit.js";
|
|
import { CommandMenu } from "./CommandMenu.js";
|
|
import {
|
|
completion,
|
|
controlRoute,
|
|
enterSends,
|
|
matchCommands,
|
|
menuQuery,
|
|
moveSelection,
|
|
type AgentCommand,
|
|
} from "./command-menu.js";
|
|
import { useAgentCommands } from "./useAgentCommands.js";
|
|
import "./AgentInputForm.css";
|
|
|
|
export function AgentInputForm({ agentName }: { agentName: string }) {
|
|
const [body, setBody] = useState("");
|
|
const [selected, setSelected] = useState(0);
|
|
const [closed, setClosed] = useState(false);
|
|
const { state, submitting, run } = useSubmit();
|
|
const commands = useAgentCommands(agentName);
|
|
|
|
const query = menuQuery(body);
|
|
const matches =
|
|
query === null || closed ? [] : matchCommands(commands, query);
|
|
const open = matches.length > 0;
|
|
const menuId = `agent-input-${agentName}-commands`;
|
|
|
|
// A new query starts at the top again, and Esc only lasts until the text
|
|
// changes.
|
|
useEffect(() => {
|
|
setSelected(0);
|
|
setClosed(false);
|
|
}, [body]);
|
|
|
|
function submit(e: Event) {
|
|
e.preventDefault();
|
|
const base = `/api/agents/${encodeURIComponent(agentName)}`;
|
|
const route = controlRoute(body, commands);
|
|
void run(
|
|
() =>
|
|
route
|
|
? fetch(`${base}/${route}`, { method: "POST" })
|
|
: fetch(`${base}/input`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ body }),
|
|
}),
|
|
{ onDone: () => setBody("") },
|
|
);
|
|
}
|
|
|
|
function take(command: AgentCommand) {
|
|
setBody(completion(command));
|
|
}
|
|
|
|
function onKeyDown(e: KeyboardEvent) {
|
|
if (!open) return;
|
|
const current = matches[Math.min(selected, matches.length - 1)];
|
|
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
e.preventDefault();
|
|
setSelected(moveSelection(selected, matches.length, e.key));
|
|
} else if (
|
|
e.key === "Tab" ||
|
|
(e.key === "Enter" && !enterSends(body, current))
|
|
) {
|
|
e.preventDefault();
|
|
take(current);
|
|
} else if (e.key === "Escape") {
|
|
e.preventDefault();
|
|
setClosed(true);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div class="ui-agent-input">
|
|
<form class="ui-agent-input-form" onSubmit={submit}>
|
|
<div class="ui-agent-input-field">
|
|
{open && (
|
|
<CommandMenu
|
|
id={menuId}
|
|
commands={matches}
|
|
query={query ?? ""}
|
|
selected={Math.min(selected, matches.length - 1)}
|
|
onPick={take}
|
|
onHover={setSelected}
|
|
/>
|
|
)}
|
|
<TextField
|
|
id={`agent-input-${agentName}`}
|
|
label={`message ${agentName}`}
|
|
hideLabel
|
|
placeholder={`message ${agentName}`}
|
|
value={body}
|
|
required
|
|
onInput={setBody}
|
|
onKeyDown={onKeyDown}
|
|
combobox={{
|
|
expanded: open,
|
|
controls: menuId,
|
|
activeDescendant: open
|
|
? `${menuId}-${Math.min(selected, matches.length - 1)}`
|
|
: undefined,
|
|
}}
|
|
/>
|
|
</div>
|
|
<Button variant="primary" type="submit" busy={submitting}>
|
|
send
|
|
</Button>
|
|
</form>
|
|
{state.status === "error" && (
|
|
<ApiErrorPanel context="failed to send" problem={state.problem} />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|