import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { readOverflowSettings } from "./settings.ts"; import { getStash, stashResult } from "./src/stash.ts"; /** * Pre-bound capture: loaded FIRST in pi.extensions, before RTK or any other * result-rewriting extension. It only stashes the untouched original text — * it never modifies the result. The bound stage (`index.ts`, loaded last) * consumes the stash after all other handlers have run. */ export default function overflowCapture(pi: ExtensionAPI): void { const settings = readOverflowSettings(); pi.on("tool_result", (event) => { if (!settings.enabled) return undefined; if (settings.exemptTools.includes(event.toolName)) return undefined; if (event.parentToolCallId) return undefined; const rawText = event.content .filter((part): part is { type: "text"; text: string } => part.type === "text") .map((part) => part.text) .join("\n"); const fullOutputPath = typeof event.details?.fullOutputPath === "string" ? event.details.fullOutputPath : undefined; if (rawText.length <= settings.thresholdChars && !fullOutputPath) return undefined; stashResult(getStash(), event.toolCallId, { rawText, ...(fullOutputPath ? { fullOutputPath } : {}), }); return undefined; }); }