UNPKG

spaider

Version:

Deterministic-first AI code assistant that crawls your codebase to implement changes using open source LLMs

61 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyChanges = void 0; const ai_1 = require("../services/ai"); const utils_1 = require("../lib/utils"); const logger_1 = require("../services/logger"); const applyChanges = async (ctx) => { if (!ctx.changes) { throw new Error("Changes must be generated before applying"); } const changesPerFile = ctx.changes.reduce((acc, curr) => { acc[curr.filePath] = acc[curr.filePath] || []; acc[curr.filePath].push(curr); return acc; }, {}); const filePaths = Object.keys(changesPerFile); await Promise.all(filePaths.map(async (filePath) => { const changes = changesPerFile[filePath]; if (changes[0].operation === "delete_file") { logger_1.Logger.info("Deleting file", filePath); try { await (0, utils_1.safeDeleteFile)(filePath, ctx.projectRoot); ctx.result.deletedFiles.push(filePath); logger_1.Logger.debug("File deleted successfully:", filePath); } catch (error) { logger_1.Logger.error("Error deleting file:", error); } return; } let fileContent = ""; if (changes[0].operation !== "new_file") { logger_1.Logger.info("Updating file", filePath); fileContent = await (0, utils_1.readFile)((0, utils_1.resolveFilePath)(filePath, ctx.projectRoot)); if (!fileContent) { throw new Error(`Could not read file ${filePath}`); } } else { logger_1.Logger.info("Creating file", filePath); } const newFileContent = await ai_1.AI.applyFileChanges(changes, fileContent); try { await (0, utils_1.safeWriteFile)(filePath, newFileContent, ctx.projectRoot); if (changes[0].operation === "new_file") { ctx.result.createdFiles.push(filePath); } else { ctx.result.modifiedFiles.push(filePath); } logger_1.Logger.debug("File written successfully:", filePath); } catch (error) { logger_1.Logger.error("Error writing file:", error); throw error; // Re-throw to properly handle the error } })); return ctx; }; exports.applyChanges = applyChanges; //# sourceMappingURL=apply.js.map