@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
34 lines • 1.55 kB
JavaScript
import { constants } from "node:fs";
import { access, readFile } from "node:fs/promises";
import { splitBom } from "../../utils/text.js";
import { applyEditsToNormalizedContent, generateDiffString, normalizeToLF, } from "./edit-diff.js";
import { resolveToCwd } from "./path-utils.js";
/**
* Compute the diff for one or more edit operations without applying them.
* Used for preview rendering in the TUI before the tool executes.
*/
export async function computeEditsDiff(path, edits, cwd) {
const absolutePath = resolveToCwd(path, cwd);
try {
// Check if file exists and is readable
try {
await access(absolutePath, constants.R_OK);
}
catch (error) {
const errorMessage = error instanceof Error && "code" in error ? `Error code: ${error.code}` : String(error);
return { error: `Could not edit file: ${path}. ${errorMessage}.` };
}
// Read the file
const rawContent = await readFile(absolutePath, "utf-8");
// Strip BOM before matching (LLM won't include invisible BOM in oldText)
const { text: content } = splitBom(rawContent);
const normalizedContent = normalizeToLF(content);
const { baseContent, newContent } = applyEditsToNormalizedContent(normalizedContent, edits, path);
// Generate the diff
return generateDiffString(baseContent, newContent);
}
catch (err) {
return { error: err instanceof Error ? err.message : String(err) };
}
}
//# sourceMappingURL=edit-preview.js.map