UNPKG

editcodewithai

Version:
67 lines (66 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.shouldDeleteFile = shouldDeleteFile; exports.prepareFilesForPrompt = prepareFilesForPrompt; exports.mergeFileChanges = mergeFileChanges; const viz_utils_1 = require("@vizhub/viz-utils"); /** * If the LLM outputs empty text for a file, we interpret this * as a request to delete the file. */ function shouldDeleteFile(file) { if (!file) return false; return file.text.trim() === ""; } /** * Processes files for the prompt by truncating large files */ function prepareFilesForPrompt(files) { const result = {}; Object.values(files).forEach((file) => { // Example: truncate large files, etc. result[file.name] = file.text .split("\n") .slice(0, file.name.endsWith(".csv") || file.name.endsWith(".json") ? 50 : 500) .map((line) => line.slice(0, 200)) .join("\n"); }); return result; } /** * Merges original files with changes from the LLM */ function mergeFileChanges(originalFiles, parsedFiles) { // Start with existing files let changedFiles = Object.keys(originalFiles).reduce((acc, fileId) => { const original = originalFiles[fileId]; const changedText = parsedFiles[original.name]; const changedFile = changedText !== undefined ? { name: original.name, text: changedText } : undefined; if (shouldDeleteFile(changedFile)) { // Exclude from new set return acc; } // If changedFile is present, use it; otherwise use original acc[fileId] = { ...original, text: changedFile ? changedFile.text : original.text, }; return acc; }, {}); // Handle newly-created files Object.entries(parsedFiles).forEach(([fileName, fileText]) => { const existingFile = Object.values(changedFiles).find((file) => file.name === fileName); // If no existing file and not empty => it's a new file if (!existingFile && fileText.trim() !== "") { const newFileId = (0, viz_utils_1.generateVizFileId)(); changedFiles[newFileId] = { name: fileName, text: fileText, }; } }); return changedFiles; }