// @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 { Answers, questionOutcome } from "./answers.js"; 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 }; } /** * Where the drawn items start, of `length`, when at least the latest `shown` are wanted. A reader * following the bottom gets the latest `shown`. One reading above it keeps the start where it was, * so new items grow the list below them rather than taking items from over them: WebKit doesn't * anchor the scroll, and every item dropped from the top moved what they were reading. * @param {number | null} start where the drawn items started last time, if they have * @param {number} length @param {number} shown @param {boolean} following */ export function windowStart(start, length, shown, following) { const latest = Math.max(0, length - shown); return following || start === null ? latest : Math.min(start, latest); } /** @param {{ items: TranscriptItem[], following: boolean }} props */ export function Transcript({ items, following }) { const [shown, setShown] = useState(WINDOW); const list = useRef(/** @type {HTMLDivElement | null} */ (null)); const start = useRef(/** @type {number | null} */ (null)); const keepPlace = useKeptPlace(list); start.current = windowStart(start.current, items.length, shown, following); const { hidden, drawn } = windowOf(items, items.length - start.current); const more = () => keepPlace(() => setShown(items.length - Math.max(0, hidden - 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} />`; } /** Reasoning in full, as the TUI shows it by default. @param {{ item: TextItem }} props */ function Thinking({ item }) { const rendered = useRendered({ kind: "reasoning", text: item.text.value }, !!item.provisional); 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)}`; const outcome = call.name === "question" && output ? questionOutcome(output) : null; if (outcome) return html`
${head}
<${Answers} outcome=${outcome} />
`; 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`