UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

257 lines • 10.5 kB
import { Container, Spacer } from "@earendil-works/pi-tui"; import { isModelOnlyText } from "../../../core/tools/model-only-text.js"; import { GrokToolRow } from "../grok/tool-row.js"; import { createBoundedRenderSignature } from "./render-signature.js"; import { ToolExecutionAnimation, toolCardSpins, toolCardStrikes } from "./tool-execution-animation.js"; import { serializedToolResultBytes, ToolExecutionRenderCache } from "./tool-execution-cache.js"; import { collapseFallbackResult } from "./tool-execution-fallback-preview.js"; import { ToolExecutionImages } from "./tool-execution-images.js"; import { ToolExecutionRenderer } from "./tool-execution-renderer.js"; export class ToolExecutionComponent extends Container { constructor(toolName, toolCallId, args, options = {}, toolDefinition, ui, cwd, presentation = "classic") { super(); this.expanded = false; this.isPartial = true; this.executionStarted = false; this.argsComplete = false; /** Built content was dropped by `invalidate`; the next render rebuilds it. */ this.displayReleased = false; this.renderCache = new ToolExecutionRenderCache(); this.animation = new ToolExecutionAnimation({ invalidate: () => this.invalidateRenderCache(), redraw: () => { this.updateDisplay(); this.ui.requestRender(); }, }); this.identity = { toolName, toolCallId, cwd, toolDefinition }; this.args = args; this.showImages = options.showImages ?? true; this.imageWidthCells = options.imageWidthCells ?? 60; this.ui = ui; this.presentation = presentation; const initialState = this.createRenderState(); if (this.presentation === "grok") { this.grokRow = new GrokToolRow({ toolName: this.identity.toolName, isPartial: initialState.isPartial, result: initialState.result, }); this.addChild(new Spacer(1)); this.addChild(this.grokRow); } else { this.renderer = new ToolExecutionRenderer(this.identity, initialState, // A renderer asking for a rerender (`context.invalidate()`, e.g. after an async load it cached on its // own component) rebuilds now and keeps that component as `lastComponent`; only invalidation from // the TUI tree releases the content. () => { this.rebuildNow(); this.ui.requestRender(); }, // Left-clicking a finished tool card toggles it like the expand keybinding does. () => { this.setExpanded(!this.expanded); this.ui.requestRender(); }); this.images = new ToolExecutionImages(() => { this.invalidateRenderCache(); this.ui.requestRender(); }); this.addChild(new Spacer(1)); this.addChild(this.renderer); this.addChild(this.images); } this.updateSpinnerAnimation(); this.updateDisplay(); } updateArgs(args) { this.args = args; this.lastDisplaySignature = undefined; this.updateSpinnerAnimation(); this.updateDisplay(); } /** Read-only presentation state; execution routing continues to own this original card. */ get presentationSnapshot() { return { identity: this.identity, state: this.createRenderState(), presentation: this.presentation }; } markExecutionStarted() { this.executionStarted = true; this.updateSpinnerAnimation(); this.updateDisplay(); this.ui.requestRender(); } setArgsComplete() { this.argsComplete = true; this.updateSpinnerAnimation(); this.updateDisplay(); this.ui.requestRender(); } updateResult(result, isPartial = false) { this.result = result; this.isPartial = isPartial; // senpi#1960: a finished card's retained result is measured once at finalize; a streaming card keeps its last figure. if (!isPartial) { this.argsComplete = true; this.renderCache.finalizeResult(serializedToolResultBytes(result)); } this.lastDisplaySignature = undefined; this.updateSpinnerAnimation(); this.updateTodoStrikeAnimation(); this.updateDisplay(); this.images?.updateResult(result); this.renderCache.setImages(result.content.filter((part) => part.type === "image").length); this.invalidateRenderCache(); } stopAnimation() { this.animation.stop(); } dispose() { this.stopAnimation(); this.renderCache.dispose(); super.dispose(); } setExpanded(expanded) { this.expanded = expanded; this.updateDisplay(); } setShowImages(show) { this.showImages = show; this.updateDisplay(); } setImageWidthCells(width) { this.imageWidthCells = Math.max(1, Math.floor(width)); this.updateDisplay(); } /** * Drops the built call and result content (and its styled output) instead of rebuilding it here; * `render` rebuilds it from the card's state the next time the card is painted (#2537). Every card * in the transcript is invalidated together (theme or terminal-capability changes, and the cards * that scroll above the kept main-screen history), so an eager rebuild kept every finished card's * output for the rest of the session. Grok rows and animating cards are rebuilt at once, as before. */ rebuildNow() { this.invalidateRenderCache(); super.invalidate(); this.lastDisplaySignature = undefined; this.updateDisplay(); } invalidate() { this.invalidateRenderCache(); super.invalidate(); this.lastDisplaySignature = undefined; const renderer = this.renderer; if (renderer !== undefined && !this.animation.running) { renderer.releaseContent(); this.displayReleased = true; return; } this.updateDisplay(); } /** * A finished card renders from state that only changes through this class's setters, each of which * moves the revision. Cards still streaming arguments, running, animating, or in the grok * presentation keep rendering every frame because their output can change between setter calls. */ getRenderRevision() { if (this.presentation === "grok" || this.isPartial || !this.argsComplete || this.result === undefined || this.animation.running) { return undefined; } return this.renderCache.revision; } render(width) { if (this.presentation === "grok") return super.render(width); if (this.displayReleased) { this.displayReleased = false; this.updateDisplay(); } const signature = this.createRenderSignature(); const cached = this.renderCache.read(width, signature); if (cached) return cached; let lines; const renderer = this.renderer; const images = this.images; if (renderer.hasRendererDefinition && renderer.renderShell === "self") { const contentLines = renderer.render(width); const imageLines = images.render(width); if (contentLines.length === 0 && imageLines.length === 0) return []; lines = contentLines.length > 0 ? ["", ...contentLines, ...imageLines] : imageLines; } else { lines = super.render(width); } this.renderCache.store(width, signature, lines); return lines; } updateDisplay() { // A released card (expand-all, show-images, a new image width) only records the change; `render` // rebuilds it if the card is painted again, so cards far above the kept history stay released. if (this.displayReleased) { this.invalidateRenderCache(); return; } const displaySignature = this.createRenderSignature(); if (this.lastDisplaySignature === displaySignature) return; this.lastDisplaySignature = displaySignature; this.invalidateRenderCache(); const state = this.createRenderState(); if (this.grokRow) { this.grokRow.update({ toolName: this.identity.toolName, isPartial: state.isPartial, result: state.result, }); return; } const renderer = this.renderer; const renderState = renderer.hasRendererDefinition && !renderer.hasResultRenderer ? { ...state, result: collapseFallbackResult(state.result, state.showImages, state.expanded) } : state; renderer.update(renderState); this.images.updateOptions({ showImages: state.showImages, maxWidthCells: this.imageWidthCells, showRendererFallback: renderer.hasResultRenderer, }); } createRenderState() { return { args: this.args, executionStarted: this.executionStarted, argsComplete: this.argsComplete, isPartial: this.isPartial, expanded: this.expanded, showImages: this.showImages, spinnerFrame: this.animation.frame, result: this.result ? { ...this.result, content: this.result.content.filter((part) => !isModelOnlyText(part)) } : undefined, }; } createRenderSignature() { return createBoundedRenderSignature({ ...this.createRenderState(), imageWidthCells: this.imageWidthCells, toolCallId: this.identity.toolCallId, toolName: this.identity.toolName, }); } updateSpinnerAnimation() { this.animation.spin(toolCardSpins(this.identity.toolName, this.createRenderState())); } updateTodoStrikeAnimation() { this.animation.strike(toolCardStrikes(this.identity.toolName, this.createRenderState())); } invalidateRenderCache() { this.renderCache.invalidate(); } } //# sourceMappingURL=tool-execution.js.map