// @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`
${output}
${shown.join("\n")}${more > 0 ? html`\n… ${more} more lines` : null}`;
}
/**
* A fence's copy control, for every rendered block at once; the page's policy allows no handler
* in the host's HTML, so the transcript listens for it.
* @param {Event} event
*/
function copyCode(event) {
const button = landedIn(event, ".fence .copy");
if (!button) return;
event.stopPropagation();
const code = button.closest(".fence")?.querySelector("code")?.textContent ?? "";
navigator.clipboard.writeText(code).then(
() => flash(button, "copied"),
() => flash(button, "copy-failed"),
);
}
/** @param {Element} element @param {string} className */
function flash(element, className) {
element.classList.add(className);
setTimeout(() => element.classList.remove(className), COPIED_MS);
}
/** How the call stands, in words: its time once done, otherwise its state. @param {Card} call */
export function standing(call) {
if (call.state === "failed") return call.summary || "failed";
if (call.state !== "succeeded") return call.state;
return call.elapsed_ms == null ? "" : elapsed(call.elapsed_ms);
}
/** A duration the way the TUI writes it. @param {number} ms */
export function elapsed(ms) {
if (ms < 1) return "<1ms";
if (ms < 1000) return `${ms}ms`;
if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`;
return `${Math.floor(ms / 60000)}m ${Math.round((ms % 60000) / 1000)}s`;
}