UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

76 lines (75 loc) 2.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.fileService = new FileService(baseDir); } getName() { return 'read_file'; } getDescription() { return 'Read the contents of a text file, markdown file, or code file. Supports various file formats including .txt, .md, .mdx, and common programming language 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(); if (!this.supportedExtensions.includes(fileExtension)) { return { success: false, error: `Unsupported file type: ${fileExtension}. Supported types: ${this.supportedExtensions.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 }; return { success: true, data: { content, fileInfo }, message: `Successfully read file: ${file_path} (${fileInfo.lines} lines, ${fileInfo.size} characters)` }; } catch (error) { return { success: false, error: `Failed to read file: ${error.message}` }; } } }