// @ts-check // The status line under the composer: the mode, which a tap switches, then the bar's right-hand // facts (context, cost, thinking, model) as the host words them, so they read as the TUI's do. A // phone keeps it to one row and leaves the cost to the details sheet a tap on the facts opens. import { batch, signal } from "@preact/signals"; import { MODES } from "./details.js"; import { html } from "./html.js"; import { useRendered } from "./render.js"; /** @import { Signal } from "@preact/signals" */ /** @import { Frame } from "./connection.js" */ /** @typedef {{ facts: Signal>, mode: Signal }} State */ /** Nothing reported yet, and the function that folds frames into what is. */ export function createStatus() { /** @type {State} */ const state = { facts: signal({}), mode: signal("") }; /** @param {Frame} frame */ const fold = (frame) => batch(() => apply(state, frame)); return { facts: state.facts, mode: state.mode, apply: fold }; } /** @param {State} state @param {Frame} frame */ function apply(state, { kind, content }) { switch (kind) { case "state_changed": state.mode.value = content.mode ?? ""; return update(state, { model: content.model, thinking: content.thinking }); case "context_usage": return update(state, { context: content }); case "plan_usage": return update(state, { plan: content }); // the total is all the line shows; the breakdown would only make every request longer case "session_usage": return update(state, { session: { total: content.total } }); } } /** @param {State} state @param {Record} changed */ function update(state, changed) { state.facts.value = { ...state.facts.value, ...changed }; } /** The frame that switches from `mode` to the next one. @param {string} mode @returns {Frame} */ export function nextModeFrame(mode) { const next = MODES[(MODES.indexOf(mode) + 1) % MODES.length]; return { kind: "set_mode", content: next }; } /** * @param {{ facts: Record, mode: string, send: (frame: Frame) => boolean, * details: () => void }} props */ export function StatusLine({ facts, mode, send, details }) { const rendered = useRendered({ kind: "status", ...facts }, true); return html`
${mode ? html`<${ModeButton} mode=${mode} send=${send} />` : null}
`; } /** * The mode behind the mark the TUI's composer title gives it, drawn by the host; the bare name * until it is. A tap switches to the next mode. * @param {{ mode: string, send: (frame: Frame) => boolean }} props */ function ModeButton({ mode, send }) { const rendered = useRendered({ kind: "mode_name", mode }, true); const label = rendered ? { dangerouslySetInnerHTML: { __html: rendered } } : { children: mode }; return html`