UNPKG

@code-yeongyu/senpi

Version:

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

179 lines • 6.99 kB
import { readFile } from "node:fs/promises"; import { createUnifiedPatch } from "../../../tools/unified-diff.js"; import { parsePatch } from "./parser.js"; import { createPatchDiff } from "./patch-diff.js"; import { replaceChunks } from "./patch-replace.js"; import { formatPatchPreview, formatPendingPatchPaths } from "./preview-format.js"; import { resolvePatchPath } from "./workspace.js"; export async function readPatchFileSnapshot(absolutePath) { try { const bytes = await readFile(absolutePath); if (bytes.includes(0)) return { exists: true, content: "", binary: true, bytes }; try { return { exists: true, content: new TextDecoder("utf-8", { fatal: true }).decode(bytes), }; } catch { return { exists: true, content: "", binary: true, bytes }; } } catch (error) { if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") { return { exists: false, content: "" }; } throw error; } } function createMovePatch(input) { const { hunk, oldContent, newContent, moveDestination } = input; if (!hunk.movePath || hunk.movePath === hunk.filePath) { return createUnifiedPatch(hunk.filePath, hunk.movePath ?? hunk.filePath, oldContent, newContent); } const sourcePatch = createUnifiedPatch(`a/${hunk.filePath}`, "/dev/null", oldContent, ""); const destinationPatch = moveDestination?.exists ? moveDestination.content === newContent ? "" : createUnifiedPatch(`a/${hunk.movePath}`, `b/${hunk.movePath}`, moveDestination.content, newContent) : createUnifiedPatch("/dev/null", `b/${hunk.movePath}`, "", newContent); return `${sourcePatch}${destinationPatch}`; } export function buildPatchPreviewFile(input) { const { hunk, source, newContent, moveDestination } = input; if (source.binary || moveDestination?.binary) { return { filePath: hunk.filePath, ...(hunk.type === "update" && hunk.movePath ? { movePath: hunk.movePath } : {}), operation: hunk.type === "add" && source.exists ? "update" : hunk.type, binary: true, diff: "", added: 0, removed: 0, }; } switch (hunk.type) { case "add": { const operation = source.exists ? "update" : "add"; return { filePath: hunk.filePath, operation, patch: createUnifiedPatch(operation === "add" ? "/dev/null" : hunk.filePath, hunk.filePath, source.content, newContent), ...createPatchDiff(source.content, newContent), }; } case "delete": return { filePath: hunk.filePath, operation: "delete", patch: createUnifiedPatch(hunk.filePath, "/dev/null", source.content, ""), ...createPatchDiff(source.content, ""), }; case "update": return { filePath: hunk.filePath, movePath: hunk.movePath, operation: "update", patch: createMovePatch({ hunk, oldContent: source.content, newContent, moveDestination }), ...createPatchDiff(source.content, newContent), }; default: { const exhaustive = hunk; return exhaustive; } } } async function createPatchPreviewFile(cwd, hunk) { const absolutePath = resolvePatchPath(cwd, hunk.filePath); switch (hunk.type) { case "add": { const source = await readPatchFileSnapshot(absolutePath); return buildPatchPreviewFile({ hunk, source, newContent: hunk.content }); } case "delete": { const source = await readPatchFileSnapshot(absolutePath); return buildPatchPreviewFile({ hunk, source, newContent: "", }); } case "update": { const source = await readPatchFileSnapshot(absolutePath); const moveDestination = hunk.movePath && hunk.movePath !== hunk.filePath ? await readPatchFileSnapshot(resolvePatchPath(cwd, hunk.movePath)) : undefined; if (source.binary || moveDestination?.binary) { return buildPatchPreviewFile({ hunk, source, newContent: "", ...(moveDestination ? { moveDestination } : {}), }); } const newContent = hunk.chunks.length === 0 ? source.content : replaceChunks(source.content, hunk.filePath, hunk.chunks).content; return buildPatchPreviewFile({ hunk, source, newContent, ...(moveDestination ? { moveDestination } : {}), }); } default: { const exhaustive = hunk; return exhaustive; } } } function hasPreviewChange(file) { return (file.binary === true || file.operation !== "update" || file.movePath !== undefined || file.diff.trim().length > 0); } export async function createPatchPreview(cwd, hunks) { const files = []; for (const hunk of hunks) { try { files.push(await createPatchPreviewFile(cwd, hunk)); } catch (error) { if (error instanceof Error) continue; throw error; } } return { files, added: files.reduce((sum, file) => sum + file.added, 0), removed: files.reduce((sum, file) => sum + file.removed, 0), }; } export async function createPendingPatchUpdate(cwd, patchText, progress, previewOverride) { const title = progress ? `Applying patch (${progress.applied + progress.failed}/${progress.total})...` : "Applying patch..."; if (previewOverride) { return { text: `${title}\n${formatPatchPreview(previewOverride)}`, details: { preview: previewOverride, progress }, }; } try { const hunks = parsePatch(patchText); if (hunks.length === 0) return { text: title, details: progress ? { progress } : undefined }; const preview = await createPatchPreview(cwd, hunks); if (preview.files.some(hasPreviewChange)) { return { text: `${title}\n${formatPatchPreview(preview)}`, details: { preview, progress } }; } } catch { return { text: progress ? title : formatPendingPatchPaths(patchText), details: progress ? { progress } : undefined, }; } return { text: progress ? title : formatPendingPatchPaths(patchText), details: progress ? { progress } : undefined }; } //# sourceMappingURL=preview.js.map