// @ts-check // The transcript's items, drawn the way the TUI draws them: your messages on a band, replies as // text, reasoning muted, and a line for each tool call that opens to show what it returned. // // Text arrives as the host renders it (`render.js`). Until it does, an item shows its plain text, // so nothing waits on the round trip to be readable. import { useLayoutEffect, useRef, useState } from "preact/hooks"; import { html, landedIn } from "./html.js"; import { useRendered } from "./render.js"; /** @import { Signal } from "@preact/signals" */ /** @import { Card, Item as TranscriptItem, TextItem } from "./transcript.js" */ /** How long a copy control says it copied. */ const COPIED_MS = 1500; /** * How many items are drawn at first, and how many more each tap on "Show earlier" adds. WebKit * slows every keystroke as a page passes a few thousand items (a 9,000-item session took 100ms a * key), so a long session's history waits above until it's asked for. */ export const WINDOW = 300; /** * The items to draw when the latest `shown` are wanted, and how many earlier ones wait above. * @template T @param {T[]} items @param {number} shown */ export function windowOf(items, shown) { const hidden = Math.max(0, items.length - shown); return { hidden, drawn: hidden ? items.slice(hidden) : items }; } /** @param {{ items: TranscriptItem[] }} props */ export function Transcript({ items }) { const [shown, setShown] = useState(WINDOW); const list = useRef(/** @type {HTMLDivElement | null} */ (null)); const keepPlace = useKeptPlace(list); const { hidden, drawn } = windowOf(items, shown); const more = () => keepPlace(() => setShown(shown + WINDOW)); const count = html`${hidden.toLocaleString()} more`; return html`
${hidden ? html`` : null} ${drawn.map((item) => html`<${Item} key=${item.key} item=${item} />`)}
`; } /** * Runs a change that adds items above, then scrolls the item that was first back to where it was. * @param {{ current: HTMLElement | null }} list @returns {(change: () => void) => void} */ function useKeptPlace(list) { const held = useRef(/** @type {{ item: Element, top: number } | null} */ (null)); useLayoutEffect(() => { const kept = held.current; held.current = null; if (kept) holdInPlace(kept.item, kept.top); }); return (change) => { const item = list.current?.querySelector(".item"); if (item) held.current = { item, top: item.getBoundingClientRect().top }; change(); }; } /** How many frames an item is held in place while the items drawn above it settle. */ const HOLD_FRAMES = 45; /** * Keeps `item` at `top` while what was added above settles: items off screen are drawn at a guessed * height until they near it, and WebKit doesn't anchor the scroll as they do. A touch or a wheel * hands the scroll back. * @param {Element} item @param {number} top */ function holdInPlace(item, top) { let frames = HOLD_FRAMES; const release = () => (frames = 0); const kinds = ["touchstart", "wheel"]; for (const kind of kinds) addEventListener(kind, release, { once: true, passive: true }); const step = () => { if (frames-- <= 0) return kinds.forEach((kind) => removeEventListener(kind, release)); scrollBy(0, item.getBoundingClientRect().top - top); requestAnimationFrame(step); }; step(); } /** @param {{ item: TranscriptItem }} props */ function Item({ item }) { switch (item.kind) { case "user": return html`<${UserMessage} item=${item} />`; case "assistant": return html`<${Reply} item=${item} />`; case "thinking": return html`<${Thinking} item=${item} />`; case "tool": return html`<${ToolCard} card=${item.card} />`; case "error": return html`
${item.text}
`; default: return html`
${item.text}
`; } } /** * Host HTML when it has come, the plain text until then. * @param {{ className: string, rendered: string | undefined, text: string }} props */ function Rendered({ className, rendered, text }) { if (rendered === undefined) return html`
${text}
`; return html`
`; } /** @param {{ item: TextItem }} props */ function UserMessage({ item }) { const rendered = useRendered({ kind: "user", content: item.content ?? [] }, false); return html`<${Rendered} className="item user-message" rendered=${rendered} text=${item.text.value} />`; } /** @param {{ item: TextItem }} props */ function Reply({ item }) { const rendered = useRendered({ kind: "answer", text: item.text.value }, !!item.provisional); return html`<${Rendered} className="item reply md" rendered=${rendered} text=${item.text.value} />`; } /** @param {{ item: TextItem }} props */ function Thinking({ item }) { const [open, setOpen] = useState(false); const rendered = useRendered({ kind: "reasoning", text: item.text.value }, !!item.provisional); const toggle = () => setOpen(!open); /** @param {KeyboardEvent} event */ const onKey = (event) => (event.key === "Enter" || event.key === " ") && toggle(); return html`
<${Rendered} className="md reasoning" rendered=${rendered} text=${item.text.value} />
`; } /** @param {{ card: Signal }} props */ function ToolCard({ card }) { const call = card.value; const output = call.output_preview; const head = html`<${ToolGlyph} name=${call.name} /> ${call.name} ${call.label} ${standing(call)} ${body(call)}`; if (!output) return html`
${head}
`; return html`
${head}
${output}
`; } /** The mark the TUI's card wears for this tool, which the host knows. @param {{ name: string }} props */ function ToolGlyph({ name }) { const glyph = useRendered({ kind: "tool", name }, false); if (!glyph) return null; return html`