/** * The shared tool-transcript kit: a call title, the call line a renderer draws * with it, the failure line a result shows before its body, the expand hint, and * the running timer. * * Every extension's `renderCall` / `renderResult` used to repeat the same four * idioms — a bold `toolTitle`, a raw `result.content[0]`, a hand-built * `… more` hint, and a local elapsed format. This module is the one place those * live, so a rejection is sanitized everywhere, a long call line clips instead * of wrapping into the railed body, and a duration reads the same in every * block. * * Value-only: pure functions, constants, and a component class whose mutable * state is the string the caller passes in. The running timer writes only the * caller's `context.state`, never a module variable — Pi loads each extension * with an isolated module cache, so module state would be one copy per * extension. */ import type { Theme } from "@earendil-works/pi-coding-agent"; import type { Component } from "@earendil-works/pi-tui"; import { formatDuration, sanitizeLine, stripControlChars } from "../format.ts"; import { clip, clipLine, type LineClip } from "./columns.ts"; import { expandHint, SEPARATORS } from "./glyphs.ts"; import { RenderCache } from "./render-cache.ts"; import { paint } from "./tokens.ts"; /** The parts of a tool result a renderer reads. */ export interface ToolMessageResult { content: ReadonlyArray<{ type: string; text?: string }>; isError?: boolean; } /** The bold tool name in the toolTitle role, the block's only accent. */ export function toolCallTitle(theme: Theme, name: string): string { return paint(theme, "toolTitle", theme.bold(name)); } /** * One call line: the bold tool name, then the subject in the meta role. * * The subject is flattened to one line here, because a call line is a label: a * stored note, a task, or a path carrying a newline cannot wrap it into the body * column, where it would read as a result. Pass the clip the subject deserves — * `middle` for a path or a URL whose tail is the identity, `end` for prose. * * A subject drawn in another role (an argument value in `label`, a suffix in * `faint`) stays hand-built at its call site; this is the plain headline. */ export function callLine( theme: Theme, last: unknown, name: string, subject: string, clip: CallClip = "end", ): ToolCallText { const text = reuseCallText(last, "", clip); text.setText(toolCallTitle(theme, `${name} `) + paint(theme, "meta", sanitizeLine(subject))); return text; } /** * A result resolved to its details, or to the one line a failure shows. */ export type DetailsOrLine
= { line: string } | { details: Details }; /** * Resolve a result to its details, or to the line to show instead. * * The failure is one branch whether the call carried no details at all (Pi's * `{ details: {}, isError: true }` envelope) or the extension reported its own * `details.error`, and it goes through `errorLine`: a multi-line message keeps * its lines and `Error:` is prefixed exactly once, which a hand-built * `paint(theme, "bad", \`Error: ${sanitizeLine(...)}\`)` does not. */ export function detailsOrLine
( theme: Theme, result: ToolMessageResult, expanded: boolean, details: Details | undefined, ): DetailsOrLine
{ if (!details) return { line: rejectionLine(theme, result, expanded) }; // The one property this reads is the extension's own report field; a details // block without it (a plan summary, a check report) simply has no failure. if ("error" in details && typeof details.error === "string" && details.error !== "") { return { line: errorLine(theme, details.error, expanded) }; } return { details }; } /** How a call line gives up columns when the terminal is narrow. */ export type CallClip = LineClip; /** * A tool call line: the same mutable string as Pi's `Text`, but clipped to the * render width instead of wrapped. * * A call is a label, so it must stay one physical line: a wrap would push the * rest of the subject into the body column, where it looks like a result (and, * under bytefly's frame, could be railed). `end` preserves the start of prose; * `middle` keeps both the head and the tail of a path or URL. It exposes `.text` * and `.setText` because bytefly's block resolver rewrites the headline in place. * * `render` runs on every frame Pi draws, so the clipped lines are memoized on the * text and the width, the way Pi's own `Text` does; `setText` and `invalidate` * drop them. */ export class ToolCallText implements Component { text: string; readonly clip: CallClip; private cachedWidth: number | null = null; private cachedText = ""; private cachedLines: string[] = []; constructor(initial = "", clip: CallClip = "end") { this.text = initial; this.clip = clip; } setText(text: string): void { this.text = text; this.cachedWidth = null; } invalidate(): void { this.cachedWidth = null; } render(width: number): string[] { const w = Math.max(1, Math.floor(width)); if (this.cachedWidth === w && this.cachedText === this.text) return this.cachedLines; this.cachedLines = this.text.split("\n").map((line) => clipLine(line, w, this.clip)); this.cachedWidth = w; this.cachedText = this.text; return this.cachedLines; } } /** One line for a {@link LinesAtWidth}: its text, and how it gives up columns. */ export interface WidthLine { text: string; clip?: LineClip; } /** * A result built at the render width: identity lines middle-clipped, prose end-clipped, and an * optional body laid out below them. * * A result line handed to Pi's `Text` as a string can only wrap, and a path is one token, so it * hard-breaks mid-segment: at 40 columns a saved plan's path is four lines, and a URL splits * mid-word. This is the call line's rule one rung down the transcript — the same `clipLine`, * memoized the same way — for the results that carry a path or a URL. * * `body` is whatever the result already drew below the identity (a plan's text, a row list): it * is a component, so it keeps its own wrapping, and the memo compares it by identity. */ export class LinesAtWidth implements Component { private lines: readonly WidthLine[]; private body: Component | undefined; private readonly cache = new RenderCache(); constructor(lines: readonly WidthLine[], body?: Component) { this.lines = lines; this.body = body; } setLines(lines: readonly WidthLine[], body?: Component): void { this.lines = lines; this.body = body; this.cache.invalidate(); } invalidate(): void { this.cache.invalidate(); } render(width: number): string[] { const w = Math.max(1, Math.floor(width)); return this.cache.keyed([w, this.lines, this.body], () => [ ...this.lines.map((line) => clipLine(line.text, w, line.clip ?? "end")), ...(this.body?.render(w) ?? []), ]); } } /** Reuse the previous result component when it is one that builds lines at a width. */ export function reuseLines(last: unknown, lines: readonly WidthLine[], body?: Component): LinesAtWidth { return last instanceof LinesAtWidth ? (last.setLines(lines, body), last) : new LinesAtWidth(lines, body); } /** Reuse the previous call component when it is one with the same clip mode. */ export function reuseCallText(last: unknown, initial = "", clip: CallClip = "end"): ToolCallText { return last instanceof ToolCallText && last.clip === clip ? last : new ToolCallText(initial, clip); } /** * Sanitized error lines, painted `bad`, with exactly one `Error:` prefix. * * The raw message is untrusted (a host rejection, a subprocess, a parser), so * every line is neutralized before the theme is applied — otherwise the * sanitizer would strip the theme's own SGR. A message that already opens with * `Error:` is not prefixed twice. * * Collapsed draws the first sentence and the expand affordance; `expanded` draws every * line. What a block shows is not what the model was handed: the result's own text keeps * the instruction, and only the drawing folds. */ export function errorLine(theme: Theme, message: string, expanded = false): string { const lines = messageLines(message); if (lines.length === 0) return paint(theme, "bad", "Error: the tool call failed"); const head = `Error: ${lines[0]?.replace(/^error:\s*/i, "") ?? ""}`; const rest = lines.slice(1); if (expanded) return paint(theme, "bad", [head, ...rest].join("\n")); const sentence = firstSentence(head); // A refusal is often written for the model — the route to the fix is the half a person // already knows when they can read the verdict. The rest is one expand away, and the // result's own text is untouched: what a block draws is not what the model was handed. if (sentence === head && rest.length === 0) return paint(theme, "bad", head); return `${paint(theme, "bad", sentence)} ${paint(theme, "faint", "… ")}${expandHint(theme)}`; } /** * The first sentence of a line: where a collapsed block stops. * * A message with no terminator is its own sentence, which is the common case for a * one-line refusal; the alternative — clipping by columns — would end mid-word. */ function firstSentence(text: string): string { const match = /^(.*?[.!?])(?:\s|$)/.exec(text); return match?.[1] ?? text; } /** * A rejected call: every text block of Pi's `{ details: {}, isError: true }` * envelope, sanitized and painted `bad`. Collapsed output keeps the first * `maxLines` and points at the expand key; expanded shows all of it. */ export function rejectionLine(theme: Theme, result: ToolMessageResult, expanded = false): string { const lines = messageLines(messageText(result)); if (lines.length === 0) return errorLine(theme, "the tool call was rejected"); return errorLine(theme, lines.join("\n"), expanded); } /** Every text block of a result, joined; non-text blocks are ignored. */ export function messageText(result: ToolMessageResult): string { const parts: string[] = []; for (const block of result.content) { if (block.type === "text" && typeof block.text === "string") parts.push(block.text); } return parts.join("\n"); } /** `… N more lines (ctrl+o to expand)`, the collapsed overflow note. */ export function moreHint(theme: Theme, hidden: number, noun = "lines"): string { return `${paint(theme, "faint", `… ${hidden} more ${noun}`)} ${expandHint(theme)}`; } /** `… N more` and the expand affordance, for a list whose rows are items. */ export function overflowHint(theme: Theme, hidden: number): string { return `${paint(theme, "faint", `… ${hidden} more`)} ${expandHint(theme)}`; } /** `… N earlier