UNPKG

@code-yeongyu/senpi

Version:

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

130 lines • 5.45 kB
import { Text } from "@earendil-works/pi-tui"; import { formatToolCallWithArgs, getTextOutput as getRenderedTextOutput } from "../../../core/tools/render-utils.js"; import { stripAnsi } from "../../../utils/ansi.js"; import { theme } from "../theme/theme.js"; const FALLBACK_STRING_MAX_LENGTH = 160; const FALLBACK_JSON_MAX_LENGTH = 2000; const JSON_VIEW_MAX_DEPTH = 3; const JSON_VIEW_MAX_ROWS = 24; const JSON_VIEW_MAX_VALUE_LENGTH = 100; function sanitizeFallbackString(value, maxLength = FALLBACK_STRING_MAX_LENGTH) { const sanitized = stripAnsi(value) .replace(/[\u0000-\u001f\u007f-\u009f]+/g, " ") .replace(/\s+/g, " ") .trim(); if (sanitized.length <= maxLength) { return sanitized; } return `${sanitized.slice(0, Math.max(0, maxLength - 3))}...`; } function sanitizeFallbackJsonValue(_key, value) { return typeof value === "string" ? sanitizeFallbackString(value) : value; } function parseRenderableJson(text) { const trimmed = text.trim(); if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return undefined; try { const parsed = JSON.parse(trimmed); if (typeof parsed !== "object" || parsed === null) return undefined; return parsed; } catch { return undefined; } } function truncateJsonViewValue(value) { return value.length > JSON_VIEW_MAX_VALUE_LENGTH ? `${value.slice(0, JSON_VIEW_MAX_VALUE_LENGTH)}\u2026` : value; } function styleJsonPrimitive(value) { if (typeof value === "string") return theme.fg("toolOutput", truncateJsonViewValue(value)); if (typeof value === "number" || typeof value === "boolean") return theme.fg("accent", String(value)); if (value === null) return theme.fg("dim", "null"); return theme.fg("toolOutput", truncateJsonViewValue(String(value))); } function styleJsonCompact(value) { return theme.fg("toolOutput", truncateJsonViewValue(JSON.stringify(value) ?? String(value))); } function isJsonContainer(value) { return typeof value === "object" && value !== null; } /** * Flattens a parsed JSON value into bounded `key: value` rows. Returns the rendered rows plus the * number of entries that were dropped so the caller can append a single truncation line. */ function collectJsonViewRows(value) { const rows = []; let omitted = 0; const walk = (node, depth) => { const entries = Array.isArray(node) ? node.map((item, index) => [String(index), item]) : Object.entries(node); const indent = " ".repeat(depth); for (const [key, entryValue] of entries) { if (rows.length >= JSON_VIEW_MAX_ROWS) { omitted += 1; continue; } const label = `${indent}${theme.fg("muted", `${key}:`)}`; if (isJsonContainer(entryValue)) { if (depth + 1 >= JSON_VIEW_MAX_DEPTH) { rows.push(`${label} ${styleJsonCompact(entryValue)}`); continue; } rows.push(label); walk(entryValue, depth + 1); continue; } rows.push(`${label} ${styleJsonPrimitive(entryValue)}`); } }; walk(value, 0); return { rows, omitted }; } function renderJsonView(value) { const { rows, omitted } = collectJsonViewRows(value); if (rows.length === 0) return theme.fg("dim", "(empty)"); if (omitted > 0) rows.push(theme.fg("dim", `\u2026 ${omitted} more`)); return rows.join("\n"); } /** Terminal escapes and control characters in argument strings never reach the terminal; line breaks stay. */ function sanitizeCallArgValue(_key, value) { return typeof value === "string" ? stripAnsi(value).replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g, "") : value; } /** Tools without a call renderer show their arguments on the title line (key=value; one line per key expanded). */ export function createToolCallFallback(toolName, args, expanded = false) { // Tool-call arguments are JSON values (ToolCall.arguments: JsonObject), so the round trip cannot throw. const safeArgs = args === undefined ? undefined : JSON.parse(JSON.stringify(args, sanitizeCallArgValue)); return new Text(formatToolCallWithArgs(sanitizeFallbackString(toolName), safeArgs, theme, expanded), 0, 0); } export function createToolResultFallback(result, showImages) { const output = getRenderedTextOutput(result, showImages); if (!output) return undefined; const parsed = parseRenderableJson(output); if (parsed) return new Text(renderJsonView(parsed), 0, 0); return new Text(theme.fg("toolOutput", output), 0, 0); } export function formatToolExecutionFallback(toolName, args, result, showImages) { let text = theme.fg("toolTitle", theme.bold(sanitizeFallbackString(toolName))); const content = JSON.stringify(args, sanitizeFallbackJsonValue, 2); if (content) { const boundedContent = content.length > FALLBACK_JSON_MAX_LENGTH ? `${content.slice(0, FALLBACK_JSON_MAX_LENGTH - 3)}...` : content; text += `\n\n${boundedContent}`; } const output = getRenderedTextOutput(result, showImages); if (output) { text += `\n${sanitizeFallbackString(output, FALLBACK_JSON_MAX_LENGTH)}`; } return text; } //# sourceMappingURL=tool-execution-fallback.js.map