UNPKG

pyb-ts

Version:

PYB-CLI - Minimal AI Agent with multi-model support and CLI interface

43 lines (42 loc) 1.35 kB
import { isAbsolute, resolve } from "path"; import { getCwd } from "@utils/state"; import { readFileSync } from "fs"; import { detectFileEncoding } from "@utils/file"; import { getPatch } from "@utils/diff"; function applyEdit(file_path, old_string, new_string) { const fullFilePath = isAbsolute(file_path) ? file_path : resolve(getCwd(), file_path); let originalFile; let updatedFile; if (old_string === "") { originalFile = ""; updatedFile = new_string; } else { const enc = detectFileEncoding(fullFilePath); originalFile = readFileSync(fullFilePath, enc); if (new_string === "") { if (!old_string.endsWith("\n") && originalFile.includes(old_string + "\n")) { updatedFile = originalFile.replace(old_string + "\n", () => new_string); } else { updatedFile = originalFile.replace(old_string, () => new_string); } } else { updatedFile = originalFile.replace(old_string, () => new_string); } if (updatedFile === originalFile) { throw new Error( "Original and edited file match exactly. Failed to apply edit." ); } } const patch = getPatch({ filePath: file_path, fileContents: originalFile, oldStr: originalFile, newStr: updatedFile }); return { patch, updatedFile }; } export { applyEdit }; //# sourceMappingURL=utils.js.map