// @ts-check // Where you write to the agent. Idle, Send starts a turn; while one runs, what you send steers it // between tool calls, as typing into the TUI's composer does, and Stop ends it. Steering carries // only words, so a message with photos waits for the turn to end and starts the next one. // // Return is a new line, since a phone's keyboard has no other way to make one; Command- or // Control-Return sends from a hardware keyboard. The line beside the buttons says what sending // does while a turn runs, since the buttons are icons. A message that's one of the slash commands // the phone can carry out does that instead of going to the agent. // // One panel, as the TUI's composer is one box: the box takes the whole width, with the buttons // along its foot, since a narrow phone (or a closed iPhone Duo beside its side bar) has no room // for both in a row. import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks"; import { withMention } from "./files.js"; import { fieldOf, html } from "./html.js"; import { Icon } from "./icons.js"; import { messageFrame } from "./pending.js"; import { forTheWire, usePhotos } from "./photos.js"; import { Picker } from "./picker.js"; /** @import { Frame } from "./connection.js" */ /** @import { Draft, Listings } from "./picker.js" */ /** @typedef {ReturnType} Photos */ /** @type {Draft} */ const EMPTY = { text: "", caret: 0 }; /** The rows the box grows to before it scrolls. */ const MOST_ROWS = 8; /** * `run` carries out a slash command: null when the text isn't one, else whether it went out. * `mention` is what a mention from elsewhere (the project panel) adds to the draft, added and * then `taken`. * @param {{ busy: boolean, live: boolean, steering: number, listings: Listings, * send: (frame: Frame) => boolean, run: (text: string) => boolean | null, onSteered: () => void, * onDropped: () => void, mention: string | null, taken: () => void }} props */ export function Composer({ busy, live, steering, listings, send, run, onSteered, onDropped, mention, taken }) { const [draft, setDraft] = useState(EMPTY); useMentioned(mention, setDraft, taken); const photos = usePhotos(); const clear = () => setDraft(EMPTY); const submit = useSubmit({ text: draft.text, photos, busy, send, run, onSent: clear, onSteered }); const withPhotos = photos.list.length > 0; const ready = live && (draft.text.trim() !== "" || withPhotos); return html`
<${Picker} draft=${draft} listings=${listings} send=${send} place=${setDraft} run=${(/** @type {string} */ text) => run(text) && clear()} />
<${PhotoStrip} photos=${photos} /> <${MessageBox} draft=${draft} setDraft=${setDraft} busy=${busy} live=${live} submit=${submit} /> <${Tools} live=${live} busy=${busy} ready=${ready} steering=${steering} photos=${photos} send=${send} onDropped=${onDropped} />
`; } /** * The panel's foot: a photo, the steering line, then Stop and Send. * @param {{ live: boolean, busy: boolean, ready: boolean, steering: number, photos: Photos, * send: (frame: Frame) => boolean, onDropped: () => void }} props */ function Tools({ live, busy, ready, steering, photos, send, onDropped }) { const withPhotos = photos.list.length > 0; return html`
<${Attach} live=${live} onPick=${photos.add} /> <${Steering} line=${steeringLine(busy, steering, withPhotos)} count=${steering} withdraw=${() => send({ kind: "withdraw_steering" }) && onDropped()} /> <${Actions} label=${sendLabel(busy, withPhotos)} busy=${busy} ready=${ready} stop=${() => send({ kind: "cancel" }) && onDropped()} />
`; } /** * The box you write in, growing with what's written; Command- or Control-Return sends. It keeps * where the caret is, which is what an `@` mention is read back from. * @param {{ draft: Draft, setDraft: (draft: Draft) => void, busy: boolean, live: boolean, * submit: (event: Event) => void }} props */ function MessageBox({ draft, setDraft, busy, live, submit }) { const box = useRef(/** @type {HTMLTextAreaElement | null} */ (null)); useGrowing(box, draft.text); usePlacedCaret(box, draft); /** @param {KeyboardEvent} event */ const onKey = (event) => event.key === "Enter" && (event.metaKey || event.ctrlKey) && submit(event); /** @param {Event} event */ const onInput = (event) => { const field = fieldOf(event); setDraft({ text: field.value, caret: field.selectionStart ?? field.value.length }); }; return html`