// The compact transcript: the same items as the full one, grouped. A tool round — the calls // between two pieces of text — is one row; what the calls returned is in the display pane. // Pure: a view over the items, which it never changes. import { duration } from "../duration.ts" import { blockOf, callDuration, callTitle, diffCounts, drawnBlock } from "../session/display.ts" import type { Item } from "./state.ts" export type ToolItem = Extract export type ReasoningItem = Extract /** A call the transcript does not draw: a plan change (the panel follows it) or a tool the spec * marks hidden — under its former name too. */ export const undrawn = (name: string) => !drawnBlock(blockOf(name)) /** One round: its calls in order, and the finished thoughts between them (folded into the row). */ export interface Round { kind: "round" /** The first call's id: the same from the round's first call to its last. */ id: string tools: ToolItem[] thoughts: ReasoningItem[] } export type Row = { kind: "item"; item: Item } | Round /** The items as rows. Consecutive drawn calls are one round; a finished thought between two calls * of a round is folded into it. A thought still streaming, one before the round's first call, and * every thought while `showReasoning` is on, is a row of its own — and ends the round before it. * Undrawn calls are no row and end nothing. */ export function groupRounds(items: readonly Item[], o: { showReasoning?: boolean } = {}): Row[] { const rows: Row[] = [] let round: Round | undefined // Thoughts after the round's last call so far: folded in only if another call follows. let trailing: ReasoningItem[] = [] const close = () => { for (const t of trailing) rows.push({ kind: "item", item: t }) trailing = [] round = undefined } for (const item of items) { if (item.kind === "tool") { if (undrawn(item.name)) continue if (!round) { round = { kind: "round", id: item.id, tools: [], thoughts: [] } rows.push(round) } round.thoughts.push(...trailing) trailing = [] round.tools.push(item) continue } if (item.kind === "reasoning" && round && !item.streaming && !o.showReasoning) { trailing.push(item) continue } close() rows.push({ kind: "item", item }) } close() return rows } /** Which row an item is drawn in: its own id, or its round's — to find the same place again * after the view changes. */ export function rowIdOf(rows: readonly Row[]): Map { const map = new Map() for (const r of rows) { if (r.kind === "item") map.set(r.item.id, r.item.id) else for (const it of [...r.tools, ...r.thoughts]) map.set(it.id, r.id) } return map } export interface RoundSummary { count: number /** The tools by name, in the order each first appears. */ kinds: { name: string; n: number }[] /** Every finished call's time together; undefined when none was measured (a stored session). */ ms?: number running: ToolItem[] failed: ToolItem[] denied: ToolItem[] /** Finished changes, one per call that has a diff: `path`, `+added −removed`. */ changes: { id: string; path: string; added: number; removed: number }[] } const titleOf = (t: ToolItem) => t.title ?? callTitle(t.args) ?? "" export function summarize(round: Pick): RoundSummary { const kinds: { name: string; n: number }[] = [] let ms: number | undefined const changes: RoundSummary["changes"] = [] for (const t of round.tools) { const k = kinds.find((x) => x.name === t.name) if (k) k.n++ else kinds.push({ name: t.name, n: 1 }) if (t.ms !== undefined && t.status !== "running") ms = (ms ?? 0) + t.ms if (t.diff && t.status === "done") { const stat = diffCounts(t.diff) changes.push({ id: t.id, path: titleOf(t) || t.name, added: stat.added, removed: stat.removed }) } } return { count: round.tools.length, kinds, ...(ms !== undefined ? { ms } : {}), running: round.tools.filter((t) => t.status === "running"), failed: round.tools.filter((t) => t.status === "error"), denied: round.tools.filter((t) => t.status === "denied"), changes, } } /** What is said beside the round's line in its own colour: `2 failed`, `1 denied`. */ export function roundTrouble(s: RoundSummary): { failed: string; denied: string } { return { failed: s.failed.length ? `${s.failed.length} failed` : "", denied: s.denied.length ? `${s.denied.length} denied` : "" } } /** The round's one line, without its mark: `7 tools · read ×3 · bash ×2 · edit ×2 · 14.0s`. * Failures and refusals are not in it: they are said beside it (`roundTrouble`), and each gets a * line of its own under it — never only a number. `width`: the columns the line may take; the * count and the time always stay, and of the kinds as many as fit, then `…`. */ export function roundLine(s: RoundSummary, width = Infinity): string { const sep = " · " const count = `${s.count} tool${s.count === 1 ? "" : "s"}` // The same writing as a single call's time (display.md §2): durations are not locale numbers. const took = s.ms !== undefined && !s.running.length ? duration(s.ms) : "" const kinds = s.kinds.map((k) => (k.n > 1 ? `${k.name} ×${k.n}` : k.name)) const line = (shown: string[], more: boolean) => [count, ...shown, ...(more ? ["…"] : []), took].filter(Boolean).join(sep) for (let n = kinds.length; n > 0; n--) { const text = line(kinds.slice(0, n), n < kinds.length) if (Bun.stringWidth(text) <= width) return text } return line([], kinds.length > 0 && Bun.stringWidth(line([], true)) <= width) } /** What a call adds after its title in one line: its facts, a failure's or refusal's first line, * how long it took (never for a call that is running or was denied). */ export function callAfter(t: ToolItem): string { if (t.status === "running") return "…" const state = t.status === "error" ? "failed" : t.status return [...(t.facts ?? []), ...(t.note ? [t.note] : []), ...[callDuration(state, t.ms)].filter((x): x is string => !!x)].join(" · ") } /** A multi-file patch (/diff) as stat lines, one per file. */ export function patchStats(patch: string): { path: string; added: number; removed: number }[] { return patch .split(/^(?=diff --git )/m) .filter((f) => f.startsWith("diff --git")) .map((f) => ({ path: /^diff --git a\/(.+?) b\//.exec(f)?.[1] ?? "?", ...diffCounts(f) })) }