UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

128 lines (127 loc) 4.79 kB
import { BaseTool } from '../base/BaseTool.js'; import { IntelligentFileService } from '../../services/IntelligentFileService.js'; /** * Tool for intelligent file reading with context awareness */ export class ReadFileTool extends BaseTool { constructor() { const definition = { name: 'read_file', description: 'Read file content with optional partial reading and context analysis', category: 'file', parameters: [ { name: 'path', type: 'string', description: 'Path to the file to read', required: true }, { name: 'startLine', type: 'number', description: 'Starting line number for partial read', required: false }, { name: 'endLine', type: 'number', description: 'Ending line number for partial read', required: false }, { name: 'searchTerm', type: 'string', description: 'Search for specific term and return context', required: false }, { name: 'contextLines', type: 'number', description: 'Number of context lines around search term', required: false, default: 3 }, { name: 'includeAnalysis', type: 'boolean', description: 'Include intelligent analysis of the file', required: false, default: false } ] }; super(definition); this.fileService = new IntelligentFileService(); } async execute(parameters, context) { try { const { path, startLine, endLine, searchTerm, contextLines, includeAnalysis } = parameters; // Validate file exists and is accessible if (!this.isFileAccessible(path, context)) { return { success: false, error: `File ${path} is not accessible or outside project scope` }; } let result = {}; // Read file content (partial or full) if (startLine || endLine || searchTerm) { const partialResult = await this.fileService.readFilePartial(path, { startLine, endLine, searchTerm, contextLines }); result.content = partialResult.content; result.metadata = partialResult.metadata; } else { result.content = await this.fileService.readFile(path); result.metadata = { totalLines: result.content.split('\n').length, selectedLines: result.content.split('\n').length }; } // Include analysis if requested if (includeAnalysis) { try { result.analysis = await this.fileService.analyzeFile(path); } catch (error) { result.analysisError = error instanceof Error ? error.message : 'Analysis failed'; } } return { success: true, data: result, metadata: { filePath: path, operation: 'read', timestamp: Date.now() } }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error occurred' }; } } isFileAccessible(filePath, context) { // Check if file is within project scope const normalizedPath = filePath.startsWith('/') ? filePath : filePath; return normalizedPath.startsWith(context.projectPath) || !filePath.includes('..'); } getExamples() { return [ 'read_file({"path": "README.md"})', 'read_file({"path": "src/index.ts", "includeAnalysis": true})', 'read_file({"path": "config.json", "startLine": 10, "endLine": 20})', 'read_file({"path": "app.js", "searchTerm": "function", "contextLines": 5})' ]; } getEstimatedExecutionTime() { return 2000; // 2 seconds for file reading and analysis } }