UNPKG

capsule-ai-cli

Version:

The AI Model Orchestrator - Intelligent multi-model workflows with device-locked licensing

86 lines 3.17 kB
import { writeFile, mkdir } from 'fs/promises'; import path from 'path'; import { BaseTool } from '../base.js'; export class FileWriteTool extends BaseTool { name = 'file_write'; displayName = '✍️ Write File'; description = 'Write content to a file. Requires "path" and "content". Example: {"path": "/src/new.ts", "content": "console.log(\'hello\');"}.'; category = 'file'; icon = '✍️'; parameters = [ { name: 'path', type: 'string', description: 'Path where to write the file (absolute or relative to working directory)', required: true }, { name: 'content', type: 'string', description: 'Content to write to the file', required: true }, { name: 'encoding', type: 'string', description: 'File encoding (default: utf8)', default: 'utf8' }, { name: 'createDirectories', type: 'boolean', description: 'Create parent directories if they don\'t exist', default: true } ]; permissions = { fileSystem: 'write' }; ui = { showProgress: false, collapsible: true, dangerous: true }; async run(params, context) { const { path: filePath, content, encoding = 'utf8', createDirectories = true } = params; if (!filePath) { throw new Error('Missing required parameter: path. Example usage: {"path": "/src/new-file.js", "content": "// Your code here"}'); } if (content === undefined || content === null) { throw new Error('Missing required parameter: content. Example usage: {"path": "/src/file.txt", "content": "File contents here"}'); } const resolvedPath = path.isAbsolute(filePath) ? filePath : path.join(context.workingDirectory || process.cwd(), filePath); this.reportProgress(context, `Writing to file: ${resolvedPath}`); try { if (createDirectories) { const dir = path.dirname(resolvedPath); await mkdir(dir, { recursive: true }); } await writeFile(resolvedPath, content, encoding); const lines = content.split('\n').length; const size = Buffer.byteLength(content, encoding); return { path: resolvedPath, encoding, lines, size, created: true }; } catch (error) { if (error.code === 'ENOENT') { throw new Error(`Parent directory does not exist: ${path.dirname(resolvedPath)}. Set createDirectories to true to create it.`); } else if (error.code === 'EACCES') { throw new Error(`Permission denied: ${resolvedPath}`); } else if (error.code === 'EISDIR') { throw new Error(`Path is a directory: ${resolvedPath}`); } throw error; } } } //# sourceMappingURL=file-write.js.map