capsule-ai-cli
Version:
The AI Model Orchestrator - Intelligent multi-model workflows with device-locked licensing
82 lines • 2.93 kB
JavaScript
import { readFile } from 'fs/promises';
import path from 'path';
import { FileEditTool } from './file-edit.js';
import { FileWriteTool } from './file-write.js';
import { editConfirmationService } from '../../services/edit-confirmation.js';
import { stateService } from '../../services/state.js';
export class FileEditWithConfirmationTool extends FileEditTool {
ui = {
showProgress: false,
collapsible: true,
dangerous: false
};
async run(params, context) {
const currentMode = stateService.getMode();
if (currentMode === 'auto') {
return super.run(params, context);
}
const { path: filePath, oldText, newText, replaceAll = false } = params;
const resolvedPath = path.isAbsolute(filePath)
? filePath
: path.join(context.workingDirectory || process.cwd(), filePath);
const oldContent = await readFile(resolvedPath, 'utf8');
if (!oldContent.includes(oldText)) {
throw new Error(`Text not found in file: "${oldText.substring(0, 50)}${oldText.length > 50 ? '...' : ''}"`);
}
let newContent;
if (replaceAll) {
newContent = oldContent.split(oldText).join(newText);
}
else {
const index = oldContent.indexOf(oldText);
newContent = oldContent.substring(0, index) + newText + oldContent.substring(index + oldText.length);
}
const approved = await editConfirmationService.confirmEdit({
filePath: resolvedPath,
oldContent,
newContent,
diff: ''
});
if (!approved) {
throw new Error('Edit cancelled by user');
}
return super.run(params, context);
}
}
export class FileWriteWithConfirmationTool extends FileWriteTool {
ui = {
showProgress: false,
collapsible: true,
dangerous: false
};
async run(params, context) {
const { path: filePath, content } = params;
const resolvedPath = path.isAbsolute(filePath)
? filePath
: path.join(context.workingDirectory || process.cwd(), filePath);
let oldContent = '';
let isNewFile = false;
try {
oldContent = await readFile(resolvedPath, 'utf8');
}
catch (error) {
if (error.code === 'ENOENT') {
isNewFile = true;
}
else {
throw error;
}
}
const approved = await editConfirmationService.confirmEdit({
filePath: resolvedPath + (isNewFile ? ' (new file)' : ''),
oldContent,
newContent: content,
diff: ''
});
if (!approved) {
throw new Error('Write cancelled by user');
}
return super.run(params, context);
}
}
//# sourceMappingURL=file-edit-with-confirmation.js.map