@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
192 lines • 8.62 kB
JavaScript
import { Box, Container, getCapabilities, MouseRegion, Text } from "@earendil-works/pi-tui";
import { createAllToolDefinitions } from "../../../core/tools/index.js";
import { theme } from "../theme/theme.js";
import { formatToolProgressLine, readToolProgress } from "../tool-progress.js";
import { createToolCallFallback, createToolResultFallback, formatToolExecutionFallback, } from "./tool-execution-fallback.js";
import { isComponent, ToolRendererBoundary } from "./tool-renderer-boundary.js";
/**
* The built-in renderers, one set per working directory. Each card used to build all nine tool
* definitions and keep its own copy of one, descriptions included, for the rest of the session (#2537).
* Deliberately unbounded: a session renders cards for a handful of working directories at most.
*/
const builtInDefinitionsByCwd = new Map();
function builtInDefinitionsFor(cwd) {
let definitions = builtInDefinitionsByCwd.get(cwd);
if (definitions === undefined) {
definitions = createAllToolDefinitions(cwd);
builtInDefinitionsByCwd.set(cwd, definitions);
}
return definitions;
}
/** What a released renderer keeps instead of its last render state; `update` sets the real one before any paint. */
const RELEASED_STATE = {
args: undefined,
executionStarted: false,
argsComplete: false,
isPartial: false,
expanded: false,
showImages: false,
spinnerFrame: 0,
result: undefined,
};
export class ToolExecutionRenderer extends Container {
constructor(identity, state, onInvalidate, onToggleExpanded) {
super();
this.selfRenderContainer = new Container();
this.rendererState = {};
this.identity = identity;
this.state = state;
this.onInvalidate = onInvalidate;
this.onToggleExpanded = onToggleExpanded;
this.builtInDefinition = builtInDefinitionsFor(identity.cwd)[identity.toolName];
this.contentBox = new Box(1, 1, (text) => theme.bg("toolPendingBg", text));
this.contentText = new Text("", 1, 1, (text) => theme.bg("toolPendingBg", text));
this.addChild(this.hasRendererDefinition
? this.renderShell === "self"
? this.selfRenderContainer
: this.contentBox
: this.clickToExpand(this.contentText));
}
/** Left-clicking a finished tool card toggles it (upstream 71026970a); pending cards ignore clicks. */
clickToExpand(component) {
const onToggleExpanded = this.onToggleExpanded;
if (!onToggleExpanded)
return component;
return new MouseRegion(component, (event) => {
if (!this.state.result || event.type !== "click" || event.button !== "left")
return undefined;
onToggleExpanded();
return { handled: true };
});
}
get hasRendererDefinition() {
return this.builtInDefinition !== undefined || this.identity.toolDefinition !== undefined;
}
get hasResultRenderer() {
return this.getResultRenderer() !== undefined;
}
get renderShell() {
const customShell = this.identity.toolDefinition?.renderShell;
return customShell ?? this.builtInDefinition?.renderShell ?? "default";
}
update(state) {
this.state = state;
const background = state.isPartial
? (text) => theme.bg("toolPendingBg", text)
: state.result?.isError
? (text) => theme.bg("toolErrorBg", text)
: (text) => theme.bg("toolSuccessBg", text);
const progress = state.isPartial ? readToolProgress(state.result?.details) : undefined;
if (!this.hasRendererDefinition) {
this.contentText.setCustomBgFn(background);
let text = formatToolExecutionFallback(this.identity.toolName, state.args, state.result, state.showImages);
if (progress)
text += `\n${formatToolProgressLine(progress, Date.now(), state.spinnerFrame)}`;
this.contentText.setText(text);
return;
}
const container = this.renderShell === "self" ? this.selfRenderContainer : this.contentBox;
if (container instanceof Box)
container.setBgFn(background);
container.detachAll();
this.renderCall(container);
if (state.result)
this.renderResult(container, state.result);
if (progress)
container.addChild(new Text(formatToolProgressLine(progress, Date.now(), state.spinnerFrame), 0, 0));
}
/** Drops the built call and result content; the next `update` builds it again from state. */
releaseContent() {
this.contentBox.detachAll();
this.selfRenderContainer.detachAll();
this.contentText.setText("");
this.callRendererComponent = undefined;
this.resultRendererComponent = undefined;
// The last render state held its own filtered copy of the result (about 2 KB per card); the card keeps the
// original and passes a fresh state to `update` when it is painted again (#2537).
this.state = RELEASED_STATE;
}
getCallRenderer() {
return this.identity.toolDefinition?.renderCall ?? this.builtInDefinition?.renderCall;
}
getResultRenderer() {
return this.identity.toolDefinition?.renderResult ?? this.builtInDefinition?.renderResult;
}
getRenderContext(lastComponent) {
return {
args: this.state.args,
toolCallId: this.identity.toolCallId,
invalidate: this.onInvalidate,
lastComponent,
state: this.rendererState,
cwd: this.identity.cwd,
executionStarted: this.state.executionStarted,
argsComplete: this.state.argsComplete,
isPartial: this.state.isPartial,
expanded: this.state.expanded,
showImages: this.state.showImages,
imageProtocol: getCapabilities().images,
isError: this.state.result?.isError ?? false,
hasResult: this.state.result !== undefined,
spinnerFrame: this.state.spinnerFrame,
};
}
renderCall(container) {
const fallback = createToolCallFallback(this.identity.toolName, this.state.args, this.state.expanded);
const renderer = this.getCallRenderer();
if (!renderer) {
container.addChild(this.clickToExpand(fallback));
return;
}
try {
const component = renderer(this.state.args, theme, this.getRenderContext(this.callRendererComponent));
this.addRendererComponent(container, "call", component, fallback);
}
catch {
this.callRendererComponent = undefined;
container.addChild(this.clickToExpand(fallback));
}
}
renderResult(container, result) {
const fallback = createToolResultFallback(result, this.state.showImages);
const renderer = this.getResultRenderer();
if (!renderer) {
if (fallback)
container.addChild(this.clickToExpand(fallback));
return;
}
try {
const agentResult = { content: result.content, details: result.details };
const component = renderer(agentResult, { expanded: this.state.expanded, isPartial: this.state.isPartial }, theme, this.getRenderContext(this.resultRendererComponent));
this.addRendererComponent(container, "result", component, fallback);
}
catch {
this.resultRendererComponent = undefined;
if (fallback)
container.addChild(this.clickToExpand(fallback));
}
}
addRendererComponent(container, slot, value, fallback) {
if (!isComponent(value)) {
this.setRendererComponent(slot, undefined);
if (fallback)
container.addChild(this.clickToExpand(fallback));
return;
}
this.setRendererComponent(slot, value);
container.addChild(this.clickToExpand(new ToolRendererBoundary(value, fallback, () => {
if (this.getRendererComponent(slot) === value)
this.setRendererComponent(slot, undefined);
})));
}
getRendererComponent(slot) {
return slot === "call" ? this.callRendererComponent : this.resultRendererComponent;
}
setRendererComponent(slot, component) {
if (slot === "call")
this.callRendererComponent = component;
else
this.resultRendererComponent = component;
}
}
//# sourceMappingURL=tool-execution-renderer.js.map