UNPKG

@code-yeongyu/senpi

Version:

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

174 lines • 8.8 kB
import { Box, Container, Spacer, Text } from "@earendil-works/pi-tui"; import { defineTool } from "../../types.js"; import { applyPatchDetailed, buildPartialFailureText, compactApplyPatchResult } from "./apply.js"; import { APPLY_PATCH_FREEFORM_DESCRIPTION, APPLY_PATCH_JSON_DESCRIPTION, APPLY_PATCH_LARK_GRAMMAR, APPLY_PATCH_PARAMS, } from "./constants.js"; import { normalizeApplyPatchArguments } from "./params.js"; import { parsePatch } from "./parser.js"; import { createPendingPatchUpdate } from "./preview.js"; import { getApplyPatchRenderState, renderPatchPreview, truncatePreview } from "./preview-format.js"; import { renderStreamingPatchCall } from "./streaming-render.js"; export const APPLY_PATCH_RESULT_PATCH_MAX_BYTES = 16 * 1024; function retainedPatch(patch) { if (patch === undefined || Buffer.byteLength(patch, "utf8") > APPLY_PATCH_RESULT_PATCH_MAX_BYTES) { return undefined; } return patch; } function appliedPreview(result) { const files = [...result.details.appliedOperations] .sort((left, right) => left.operationIndex - right.operationIndex) .map((operation) => { const { diff, patch, ...metadata } = operation.preview; const boundedPatch = retainedPatch(patch); return { ...metadata, diff: truncatePreview(diff), ...(boundedPatch === undefined ? {} : { patch: boundedPatch }), }; }); if (files.length === 0) return undefined; return { files, added: files.reduce((total, file) => total + file.added, 0), removed: files.reduce((total, file) => total + file.removed, 0), }; } function renderPreviewBox(title, details, isPartial, cwd, expanded, theme) { const component = new Container(); if (!details.preview) return component; const bgName = isPartial ? "toolPendingBg" : "toolSuccessBg"; const progress = details.progress; const renderedTitle = progress ? `Applying patch (${progress.applied + progress.failed}/${progress.total})` : title; const box = new Box(1, 1, (text) => theme.bg(bgName, text)); box.addChild(new Text(theme.fg("toolTitle", theme.bold(renderedTitle)), 0, 0)); box.addChild(new Spacer(1)); box.addChild(new Text(renderPatchPreview(details.preview, cwd, theme, expanded), 0, 0)); component.addChild(box); return component; } function renderTextResult(result, theme) { const component = new Container(); const text = resultText(result); if (text) component.addChild(new Text(theme.fg("toolOutput", text), 0, 0)); return component; } function resultText(result) { return result.content .filter((block) => block.type === "text") .map((block) => block.text) .filter((value) => typeof value === "string" && value.length > 0) .join("\n"); } function renderFailureBox(result, cwd, expanded, theme) { const patchResult = result.details?.result; if (!patchResult) return renderTextResult(result, theme); const component = new Container(); const appliedCount = patchResult.appliedFiles.length; const title = appliedCount > 0 ? "Patch partially failed" : "Patch failed"; const box = new Box(1, 1, (text) => theme.bg("toolErrorBg", text)); box.addChild(new Text(theme.fg("toolTitle", theme.bold(title)), 0, 0)); if (result.details.preview) { box.addChild(new Spacer(1)); box.addChild(new Text(renderPatchPreview(result.details.preview, cwd, theme, expanded), 0, 0)); } const text = resultText(result); if (text) { box.addChild(new Spacer(1)); box.addChild(new Text(theme.fg("toolOutput", text), 0, 0)); } component.addChild(box); return component; } export function createApplyPatchTool(variant = "freeform") { const tool = defineTool({ name: "apply_patch", label: "ApplyPatch", description: variant === "json" ? APPLY_PATCH_JSON_DESCRIPTION : APPLY_PATCH_FREEFORM_DESCRIPTION, parameters: APPLY_PATCH_PARAMS, prepareArguments: normalizeApplyPatchArguments, promptSnippet: "Apply Codex-format file patches with apply_patch", promptGuidelines: [ "Use apply_patch for file edits instead of mutating files through bash, Python scripts, heredocs, or shell redirection.", "After apply_patch succeeds, do not re-read the edited files just to confirm the patch applied.", ], async execute(_toolCallId, params, _signal, onUpdate, ctx) { const normalizedParams = normalizeApplyPatchArguments(params); if (!normalizedParams.input) throw new Error("input is required"); let totalOperations = 0; try { totalOperations = parsePatch(normalizedParams.input).length; } catch { // createPendingPatchUpdate keeps incomplete or invalid patch text renderable. } const initialProgress = totalOperations > 0 ? { applied: 0, failed: 0, total: totalOperations } : undefined; const pendingUpdate = await createPendingPatchUpdate(ctx.cwd, normalizedParams.input, initialProgress); onUpdate?.({ content: [{ type: "text", text: pendingUpdate.text }], details: pendingUpdate.details }); const preview = pendingUpdate.details?.preview; const result = await applyPatchDetailed(ctx.cwd, normalizedParams.input, async (progress) => { const progressUpdate = await createPendingPatchUpdate(ctx.cwd, normalizedParams.input, progress, preview); onUpdate?.({ content: [{ type: "text", text: progressUpdate.text }], details: progressUpdate.details }); }); const resultPreview = appliedPreview(result); const persistedResult = compactApplyPatchResult(result); if (result.failures.length > 0) { return { content: [{ type: "text", text: buildPartialFailureText(result) }], details: resultPreview ? { preview: resultPreview, result: persistedResult } : { result: persistedResult }, }; } return { content: [{ type: "text", text: result.summaries.join("\n") }], details: resultPreview ? { preview: resultPreview, result: persistedResult } : { result: persistedResult }, }; }, renderCall(args, theme, context) { if (!context.executionStarted) { const streaming = renderStreamingPatchCall(normalizeApplyPatchArguments(args), theme, context.state); if (streaming) return streaming; } if (!context.argsComplete) return new Text(theme.fg("toolTitle", theme.bold("apply_patch: Patching")), 0, 0); const normalizedArgs = normalizeApplyPatchArguments(args); const renderState = getApplyPatchRenderState(context.toolCallId, context.cwd, normalizedArgs.input); const text = renderState.callText?.length ? `apply_patch: ${renderState.callText}` : "apply_patch"; return new Text(theme.fg("toolTitle", theme.bold(text)), 0, 0); }, renderResult(result, options, theme, context) { if (context.isError && result.details?.result?.failures.length) { return renderFailureBox(result, context.cwd, true, theme); } if (result.details?.preview) { const expanded = true; return renderPreviewBox(options.isPartial ? "Applying patch" : "Applied patch", result.details, options.isPartial, context.cwd, expanded, theme); } if (result.details?.result) { const component = new Container(); const box = new Box(1, 1, (text) => theme.bg(options.isPartial ? "toolPendingBg" : "toolSuccessBg", text)); box.addChild(new Text(theme.fg("toolTitle", theme.bold("Applying patch")), 0, 0)); box.addChild(new Spacer(1)); if (result.details.preview) { const expanded = options.isPartial ? true : (options.expanded ?? true); box.addChild(new Text(renderPatchPreview(result.details.preview, context.cwd, theme, expanded), 0, 0)); } component.addChild(box); return component; } return renderTextResult(result, theme); }, }); if (variant === "json") return tool; return Object.assign(tool, { freeform: { type: "grammar", syntax: "lark", definition: APPLY_PATCH_LARK_GRAMMAR }, }); } //# sourceMappingURL=tool.js.map