import { inspect } from "node:util"; import { keyHint, type ToolRenderers } from "@earendil-works/pi-coding-agent"; import { Text } from "@earendil-works/pi-tui"; function object(value: unknown): Record | undefined { return value !== null && typeof value === "object" && !Array.isArray(value) ? (value as Record) : undefined; } function label(value: unknown, limit = 80): string { return typeof value === "string" ? value.replace(/\p{Cc}/gu, " ").slice(0, limit) : ""; } function details(value: unknown): string { const text = inspect(value, { depth: 6, maxArrayLength: 32, maxStringLength: 16384, colors: false, customInspect: false, getters: false, }); return text.length > 16384 ? `${text.slice(0, 16384)}\n[Expanded display truncated]` : text; } function summary(value: unknown): string { const data = object(value); if (!data) return "finished"; if (Array.isArray(data["ephemlets"])) return `${data["ephemlets"].length} ephemlet${data["ephemlets"].length === 1 ? "" : "s"}`; if (typeof data["deleted"] === "string") return `deleted ${label(data["deleted"])}`; if (typeof data["visible"] === "boolean") return `${data["visible"] ? "shown" : "hidden"}${data["selected"] ? ` · ${label(data["selected"])}` : ""}`; return ( [ label(data["title"]), label(data["id"]), label(data["status"]), typeof data["revision"] === "number" ? `r${data["revision"]}` : "", data["jobId"] ? `job ${label(data["jobId"])}` : "", ] .filter(Boolean) .join(" · ") || "finished" ); } export const ephemletRenderers: ToolRenderers = { renderCall(args, theme, context) { const text = context.lastComponent instanceof Text ? context.lastComponent : new Text("", 0, 0); const data = object(args); let output = `${theme.fg("toolTitle", theme.bold("ephemlet"))} ${theme.fg("toolOutput", [label(data?.["action"]), label(data?.["title"] ?? data?.["id"])].filter(Boolean).join(" · "))}`; output += context.expanded ? `\n${theme.fg("toolOutput", details(args))}` : theme.fg("dim", ` (${keyHint("app.tools.expand", "to expand")})`); text.setText(output); return text; }, renderResult(result, { expanded, isPartial }, theme, context) { const text = context.lastComponent instanceof Text ? context.lastComponent : new Text("", 0, 0); const status = context.isError ? "failed" : isPartial ? "working" : summary(result.details); let output = theme.fg(context.isError ? "error" : "toolOutput", status); output += expanded ? `\n${theme.fg("toolOutput", details(context.isError ? result.content : (result.details ?? result.content)))}` : theme.fg("dim", ` (${keyHint("app.tools.expand", "to expand")})`); text.setText(output); return text; }, };