UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

122 lines (121 loc) 4.83 kB
import { BaseTool } from './BaseTool.js'; import { FileService } from '../fileService.js'; import path from 'path'; export class FileReadTool extends BaseTool { constructor(baseDir) { super(); this.supportedExtensions = ['.txt', '.md', '.mdx', '.js', '.ts', '.jsx', '.tsx', '.json', '.yaml', '.yml', '.css', '.scss', '.html', '.xml', '.py', '.java', '.cpp', '.c', '.h', '.go', '.rs', '.php', '.rb', '.sh', '.sql']; this.binaryExtensions = ['.wav', '.mp3', '.mp4', '.avi', '.mov', '.jpg', '.jpeg', '.png', '.gif', '.pdf', '.zip', '.tar', '.gz']; this.fileService = new FileService(baseDir); } getName() { return 'read_file'; } getDescription() { return 'Read the contents of text files, code files, or get information about binary files (audio, video, images). Supports text files and provides metadata for binary files.'; } getParameters() { return [ { name: 'file_path', type: 'string', description: 'The path to the file to read (relative to the current working directory)', required: true }, { name: 'encoding', type: 'string', description: 'File encoding (default: utf-8)', required: false, default: 'utf-8' } ]; } async execute(parameters) { // Validate parameters const validationError = this.validateParameters(parameters); if (validationError) { return validationError; } const { file_path, encoding = 'utf-8' } = parameters; try { // Check if file extension is supported const fileExtension = path.extname(file_path).toLowerCase(); // Handle binary files (provide metadata only) if (this.binaryExtensions.includes(fileExtension)) { const fs = await import('fs/promises'); const fullPath = path.resolve(process.cwd(), file_path); try { const stats = await fs.stat(fullPath); const fileInfo = { path: file_path, extension: fileExtension, size: stats.size, type: 'binary', created: stats.birthtime, modified: stats.mtime, encoding: 'binary' }; return { success: true, data: { content: `[Binary file: ${fileExtension}]`, fileInfo, isBinary: true }, message: `Binary file info: ${file_path} (${this.formatFileSize(stats.size)}, ${fileExtension.slice(1).toUpperCase()} format)` }; } catch (error) { return { success: false, error: `File not found: ${file_path}` }; } } // Handle text files if (!this.supportedExtensions.includes(fileExtension)) { return { success: false, error: `Unsupported file type: ${fileExtension}. Supported text types: ${this.supportedExtensions.join(', ')}. Binary types (info only): ${this.binaryExtensions.join(', ')}` }; } // Read the file const content = await this.fileService.readFile(file_path); // Get file stats for additional info const fileInfo = { path: file_path, extension: fileExtension, size: content.length, lines: content.split('\n').length, encoding: encoding, type: 'text' }; return { success: true, data: { content, fileInfo, isBinary: false }, message: `Successfully read file: ${file_path} (${fileInfo.lines} lines, ${fileInfo.size} characters)` }; } catch (error) { return { success: false, error: `Failed to read file: ${error.message}` }; } } /** * Format file size for display */ formatFileSize(bytes) { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } }