swarm ui: a command menu above an agent's message box
This commit is contained in:
parent
49719842b0
commit
fc650f6af3
1 changed files with 478 additions and 12 deletions
|
|
@ -3,16 +3,47 @@
|
|||
// 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).
|
||||
import { useState } from "preact/hooks";
|
||||
//
|
||||
// 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,
|
||||
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();
|
||||
|
|
@ -27,18 +58,60 @@ export function AgentInputForm({ agentName }: { agentName: string }) {
|
|||
);
|
||||
}
|
||||
|
||||
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}>
|
||||
<TextField
|
||||
id={`agent-input-${agentName}`}
|
||||
label={`message ${agentName}`}
|
||||
hideLabel
|
||||
placeholder={`message ${agentName}`}
|
||||
value={body}
|
||||
required
|
||||
onInput={setBody}
|
||||
/>
|
||||
<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>
|
||||
|
|
|
|||
Loading…
Reference in a new issue