import type { SourceInfo } from "@earendil-works/pi-coding-agent"; import type { JsonValue } from "./json.js"; import { estimateTokens, stableStringify, toBoundedJson } from "./json.js"; export const PLANNED_ACTION_TOKEN_CAP = 16_000; export interface ReviewableToolCall { toolName: string; input: unknown; } export interface PlannedAction { json: string; truncated: boolean; tooLarge: boolean; } export function buildPlannedAction( call: ReviewableToolCall, cwd: string, sourceInfo: SourceInfo, ): PlannedAction { const source: JsonValue = { path: sourceInfo.path, source: sourceInfo.source, scope: sourceInfo.scope, origin: sourceInfo.origin, }; const action: Record = { kind: "pi_tool_call", tool: call.toolName, cwd, source, arguments: call.input, }; if (call.toolName === "bash") { action["execution_scope_note"] = "Bash may launch subprocesses whose effects are not visible as separate Pi tool calls. Review the complete command as authorization for those effects."; } const bounded = toBoundedJson(action, PLANNED_ACTION_TOKEN_CAP); const json = stableStringify(bounded.value); return { json, truncated: bounded.truncated, tooLarge: estimateTokens(json) > PLANNED_ACTION_TOKEN_CAP, }; }