repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

use native Pi tool rendering in ultra transcript

a82f049aa8239cc0da2f2ed28833293f4760a0d6

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

extensions/ultra/index.ts

Modified
217 217 onRunAbort: () => controller.abort(),
218 218 workflowName: spec.name,
219 219 workflowDescription: spec.description,
220 + toolUi: tui,
221 + cwd: ctx.cwd,
221 223 // P3: return the Component SYNCHRONOUSLY; run the engine DETACHED, or
222 224 // the overlay never renders. `done` resolves the custom() promise.

extensions/ultra/progress.ts

Modified
43 43 kind: ProgressKind;
44 44 /** Tool name for `action` events (tool start/end). */
45 45 toolName?: string;
46 + /** Tool call id for native tool rendering. */
47 + toolCallId?: string;
48 + /** Raw tool args for native tool rendering. */
49 + args?: unknown;
50 + /** Raw tool result for native tool rendering. */
51 + result?: unknown;
52 + isError?: boolean;
46 53 /** Streamed delta text, a tool action/result preview, or a coarse status line. */
47 54 text?: string;
48 55 /** Terminal/error marker: `end` carries the run outcome; `action` may carry `"error"`. */
116 123 ...base,
117 124 kind: "action",
118 125 toolName: event.toolName,
126 + toolCallId: event.toolCallId,
127 + args: event.args,
119 128 text: previewArgs(event.args),
124 133 ...base,
125 134 kind: "action",
126 135 toolName: event.toolName,
136 + toolCallId: event.toolCallId,
137 + result: event.result,
138 + isError: event.isError,
127 139 text: previewResult(event.result),
128 140 status: event.isError ? "error" : undefined,
174 186 workflowDescription?: string;
189 +export type TranscriptItem =
190 + | { kind: "text"; text: string }
191 + | {
192 + kind: "tool";
193 + toolName: string;
194 + toolCallId?: string;
195 + args?: unknown;
196 + result?: unknown;
197 + isError?: boolean;
198 + fallback: string;
199 + };
177 201 export interface ReducerState extends ProgressMeta {
178 202 rows: readonly AgentRow[];
179 203 /** agentId -> accumulated transcript (text/thinking deltas + tool action lines). */
180 204 transcripts: Readonly<Record<string, string>>;
205 + transcriptItems: Readonly<Record<string, readonly TranscriptItem[]>>;
181 206 /** Distinct phase ids in arrival order — the phase counter (`phase N/len`). */
182 207 phases: readonly string[];
183 208 /** Current (latest-seen) phase id. */
197 222 workflowDescription: meta.workflowDescription,
198 223 rows: [],
199 224 transcripts: {},
225 + transcriptItems: {},
200 226 phases: [],
201 227 phase: undefined,
202 228 total: 0,
270 +function transcriptItem(event: UltraProgressEvent): TranscriptItem | undefined {
271 + if (event.kind === "delta") return { kind: "text", text: event.text ?? "" };
272 + if (event.kind !== "action" || !event.toolName) return undefined;
273 + const fallback = actionLabel(event);
274 + return {
275 + kind: "tool",
276 + toolName: event.toolName,
277 + toolCallId: event.toolCallId,
278 + args: event.args,
279 + result: event.result,
280 + isError: event.isError,
281 + fallback,
282 + };
244 285 function nextRow(
245 286 prev: AgentRow | undefined,
246 287 event: UltraProgressEvent,
304 345 : state.transcripts;
347 + const item = transcriptItem(event);
348 + const transcriptItems = item
349 + ? {
350 + ...state.transcriptItems,
351 + [event.agentId]: [...(state.transcriptItems[event.agentId] ?? []), item],
353 + : state.transcriptItems;
306 355 const phases = state.phases.includes(event.phase)
307 356 ? state.phases
308 357 : [...state.phases, event.phase];
319 368 workflowDescription: state.workflowDescription,
321 370 transcripts,
371 + transcriptItems,
322 372 phases,
323 373 phase: event.phase,
324 374 total: rows.length,

extensions/ultra/ui.ts

Modified
20 20 Theme,
21 21 ToolRenderResultOptions,
22 22 } from "@earendil-works/pi-coding-agent";
23 -import { getMarkdownTheme, keyHint } from "@earendil-works/pi-coding-agent";
23 +import {
24 + getMarkdownTheme,
25 + keyHint,
26 + ToolExecutionComponent,
27 +} from "@earendil-works/pi-coding-agent";
24 28 import {
25 29 type Component,
26 30 Markdown,
27 31 matchesKey,
28 32 truncateToWidth,
33 + type TUI,
29 34 } from "@earendil-works/pi-tui";
30 35 import {
31 36 type AgentControls,
33 38 initialReducerState,
34 39 type ReducerState,
35 40 reduceProgress,
41 + type TranscriptItem,
36 42 type UltraProgressEvent,
37 43 } from "./progress.ts";
228 234 throttleMs?: number;
229 235 workflowName?: string;
230 236 workflowDescription?: string;
237 + /** Optional Pi TUI handle; when present, tool transcript rows use Pi's native tool renderer. */
238 + toolUi?: TUI;
239 + cwd?: string;
233 242 const DEFAULT_THROTTLE_MS = 80;
238 247 return new Markdown(text, 0, 0, getMarkdownTheme()).render(Math.max(1, width));
250 +function normalizeToolResult(item: TranscriptItem & { kind: "tool" }) {
251 + const r = item.result as
252 + | { content?: unknown; details?: unknown; isError?: boolean }
253 + | undefined;
254 + const content = Array.isArray(r?.content)
255 + ? (r.content as Array<{ type: string; text?: string; data?: string; mimeType?: string }>)
256 + : item.fallback
257 + ? [{ type: "text", text: item.fallback }]
258 + : [];
259 + return {
260 + content,
261 + details: r?.details,
262 + isError: item.isError ?? r?.isError ?? false,
263 + };
242 267 * The `ctx.ui.custom` overlay (command surface only). Folds the `onProgress`
243 268 * stream with the shared reducer, renders the board + a focused-agent drill-in
261 286 private readonly onRunAbort: () => void;
262 287 private readonly now: () => number;
263 288 private readonly throttleMs: number;
289 + private readonly toolUi: TUI | undefined;
290 + private readonly cwd: string | undefined;
265 292 constructor(options: UltraOverlayOptions) {
266 293 this.state = initialReducerState({
272 299 this.onRunAbort = options.onRunAbort;
273 300 this.now = options.now ?? Date.now;
274 301 this.throttleMs = options.throttleMs ?? DEFAULT_THROTTLE_MS;
302 + this.toolUi = options.toolUi;
303 + this.cwd = options.cwd;
277 306 /** True once the human has submitted a steer this run (enqueue-time taint). */
451 + private transcriptBodyLines(agentId: string | undefined, width: number): string[] {
452 + if (!agentId) return [];
453 + const items = this.state.transcriptItems[agentId];
454 + if (!items || items.length === 0) {
455 + return transcriptLines(this.state.transcripts[agentId] ?? "", width);
457 + const lines: string[] = [];
458 + let text = "";
459 + const flushText = () => {
460 + if (!text) return;
461 + lines.push(...transcriptLines(text, width));
462 + text = "";
463 + };
464 + for (const item of items) {
465 + if (item.kind === "text") {
466 + text += item.text;
467 + continue;
469 + flushText();
470 + if (this.toolUi && this.cwd) {
471 + const tool = new ToolExecutionComponent(
472 + item.toolName,
473 + item.toolCallId ?? `${agentId}-${lines.length}`,
474 + item.args ?? {},
475 + undefined,
476 + undefined,
477 + this.toolUi,
478 + this.cwd,
479 + );
480 + tool.markExecutionStarted();
481 + tool.setArgsComplete();
482 + if (item.result !== undefined) tool.updateResult(normalizeToolResult(item));
483 + lines.push(...tool.render(width));
484 + } else if (item.fallback) {
485 + lines.push(...transcriptLines(`> ${item.fallback}`, width));
488 + flushText();
489 + return lines;
422 492 // ---- render -----------------------------------------------------------
424 494 render(width: number): string[] {
466 - const transcript = focusedRow
467 - ? (state.transcripts[focusedRow.agentId] ?? "")
468 - : "";
469 - const body = transcriptLines(transcript, w - 2);
536 + const body = this.transcriptBodyLines(focusedRow?.agentId, w - 2);
471 538 // Reserve the footer and keep a 3-row terminal safety margin.
472 539 const maxVisible = Math.max(0, maxLines - lines.length - 1);