Files
LLeMbas-CLI/src/tui/transcript.ts
T
HomerandClaude Fable 5.2 2f35b877bf TUI: full-height display pane, narrow fallback, images tab, compact transcript
The display pane is a column of the root now: it stands at the right over the
terminal's full height, beside the prompt and the status bar, from 80 columns
(layout.ts decides both columns' widths from the terminal's width alone). Below
that it is no longer silently absent: the status bar names the key, and alt+d
puts the display in the transcript's place and back.

An images tab shows what view_image viewed, what show showed and what was
attached, drawn by OpenTUI's own image renderable (kitty graphics where the
terminal is found to support them, sixel, else blocks); `images: auto|on|off`.

The transcript has two views over the same items: full, and compact, where a
round of tool calls is one line and a failure or a refusal always has its own.
`transcript: auto|full|compact`, alt+t, /transcript.

The pane gets a session header, a tab bar that fits its width, clickable tabs
and rows of known width; the status bar has a short form for a narrow column,
and the welcome box's hints and the prompt's placeholder no longer wrap inside
a phrase.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-01 19:15:17 +00:00

152 lines
6.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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<Item, { kind: "tool" }>
export type ReasoningItem = Extract<Item, { kind: "reasoning" }>
/** 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<string, string> {
const map = new Map<string, string>()
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<Round, "tools">): 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) }))
}