UNPKG

@code-yeongyu/senpi

Version:

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

130 lines • 5.86 kB
/** * Presentation for the shell tools. * * Renderers live apart from the implementation so a process that only displays tool output does not * load the execution path or its typebox parameter schema. `bash.ts` spreads these into the shell * tool definition, so the tool's public shape is unchanged. */ import { Container, Text, truncateToWidth } from "@earendil-works/pi-tui"; import { keyHint } from "../../../modes/interactive/components/keybinding-hints.js"; import { truncateToVisualLines } from "../../../modes/interactive/components/visual-truncate.js"; import { highlightCode, theme } from "../../../modes/interactive/theme/theme.js"; import { getTextOutput, invalidArgText, normalizeDisplayText, replaceTabs, str } from "../render-utils.js"; const BASH_PREVIEW_LINES = 5; class BashResultRenderComponent extends Container { constructor() { super(...arguments); this.state = { cachedWidth: undefined, cachedLines: undefined, }; } } /** Whole-second elapsed/took display (fork): sub-second runs read `<1s`, longer ones roll up to m/h. */ function formatDuration(ms) { const totalSeconds = Math.floor(Math.max(0, ms) / 1000); if (totalSeconds < 1) { return "<1s"; } const seconds = totalSeconds % 60; const totalMinutes = Math.floor(totalSeconds / 60); if (totalMinutes < 1) { return `${seconds}s`; } const minutes = totalMinutes % 60; const hours = Math.floor(totalMinutes / 60); if (hours < 1) { return seconds === 0 ? `${minutes}m` : `${minutes}m ${seconds}s`; } return minutes === 0 ? `${hours}h` : `${hours}h ${minutes}m`; } function highlightBashCommand(command) { return highlightCode(replaceTabs(normalizeDisplayText(command)), "bash").join("\n"); } /** The fork renders every shell call with a bold `$ ` prompt and a syntax-highlighted command body. */ function formatShellCall(args, _prompt) { const command = str(args?.command); const timeout = args?.timeout; const timeoutSuffix = timeout ? theme.fg("muted", ` (timeout ${timeout}s)`) : ""; const commandDisplay = command === null ? invalidArgText(theme) : command ? highlightBashCommand(command) : theme.fg("toolOutput", "..."); return theme.fg("toolTitle", theme.bold("$ ")) + commandDisplay + timeoutSuffix; } function rebuildBashResultRenderComponent(component, result, options, showImages, startedAt, endedAt) { const state = component.state; component.detachAll(); const output = getTextOutput(result, showImages).trim(); if (output) { const styledOutput = output .split("\n") .map((line) => theme.fg("toolOutput", line)) .join("\n"); if (options.expanded) { component.addChild(new Text(`\n${styledOutput}`, 0, 0)); } else { component.addChild({ render: (width) => { // Cache the complete output: this renders on every frame for every bash result in the transcript. if (state.cachedLines === undefined || state.cachedWidth !== width) { const preview = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, width); const hintLines = []; if (preview.skippedCount > 0) { const hint = theme.fg("muted", `... (${preview.skippedCount} earlier lines,`) + ` ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; hintLines.push(truncateToWidth(hint, width, "...")); } state.cachedLines = ["", ...hintLines, ...preview.visualLines]; state.cachedWidth = width; } return state.cachedLines; }, invalidate: () => { state.cachedWidth = undefined; state.cachedLines = undefined; }, }); } } if (startedAt !== undefined) { const label = options.isPartial ? "Elapsed" : "Took"; const endTime = endedAt ?? Date.now(); component.addChild(new Text(`\n${theme.fg("muted", `${label} ${formatDuration(endTime - startedAt)}`)}`, 0, 0)); } } /** Shell renderers are shared by bash and powershell, which differ only in the prompt they display. */ export function createShellRenderers(prompt) { return { renderCall(args, _theme, context) { const state = context.state; if (context.executionStarted && state.startedAt === undefined) { state.startedAt = Date.now(); state.endedAt = undefined; } const text = context.lastComponent ?? new Text("", 0, 0); text.setText(formatShellCall(args, prompt)); return text; }, renderResult(result, options, _theme, context) { const state = context.state; if (state.startedAt !== undefined && options.isPartial && !state.interval) { state.interval = setInterval(() => context.invalidate(), 1000); } if (!options.isPartial || context.isError) { state.endedAt ??= Date.now(); if (state.interval) { clearInterval(state.interval); state.interval = undefined; } } const component = context.lastComponent ?? new BashResultRenderComponent(); rebuildBashResultRenderComponent(component, result, options, context.showImages, state.startedAt, state.endedAt); component.invalidate(); return component; }, }; } //# sourceMappingURL=bash.js.map