UNPKG

@webdevtoday/grok-cli

Version:

A sophisticated CLI tool for interacting with xAI Grok 4, featuring conversation history, file reference, custom commands, memory system, and genetic development workflows

121 lines 4.42 kB
"use strict"; /** * File reading tool */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ReadTool = void 0; const base_1 = require("./base"); const fs_extra_1 = require("fs-extra"); const path_1 = require("path"); /** * Tool for reading files from the filesystem */ class ReadTool extends base_1.BaseTool { constructor() { super('Read', 'Read files from the filesystem'); } validateParams(params) { return (typeof params['filePath'] === 'string' && params['filePath'].length > 0 && (params['encoding'] === undefined || typeof params['encoding'] === 'string') && (params['offset'] === undefined || typeof params['offset'] === 'number') && (params['limit'] === undefined || typeof params['limit'] === 'number')); } async execute(params) { const { filePath, encoding = 'utf-8', offset = 0, limit = 2000 } = params; try { const resolvedPath = (0, path_1.resolve)(filePath); // Check if file exists if (!(await (0, fs_extra_1.pathExists)(resolvedPath))) { return { success: false, output: '', error: `File not found: ${filePath}`, metadata: { filePath: resolvedPath }, }; } // Check if it's a file (not directory) const stats = await (0, fs_extra_1.stat)(resolvedPath); if (!stats.isFile()) { return { success: false, output: '', error: `Path is not a file: ${filePath}`, metadata: { filePath: resolvedPath, isDirectory: stats.isDirectory() }, }; } // Read file content const content = await (0, fs_extra_1.readFile)(resolvedPath, encoding); // Apply offset and limit if specified let processedContent = content; if (offset > 0 || limit < content.length) { const lines = content.split('\n'); const startLine = Math.max(0, offset); const endLine = Math.min(lines.length, startLine + limit); processedContent = lines.slice(startLine, endLine).join('\n'); } // Add line numbers for better readability const numberedContent = processedContent .split('\n') .map((line, index) => `${String(offset + index + 1).padStart(4)}${line}`) .join('\n'); return { success: true, output: numberedContent, metadata: { filePath: resolvedPath, size: stats.size, encoding, linesRead: processedContent.split('\n').length, totalLines: content.split('\n').length, modified: stats.mtime, }, }; } catch (error) { return { success: false, output: '', error: error instanceof Error ? error.message : String(error), metadata: { filePath }, }; } } getParameterSchema() { return { type: 'object', properties: { filePath: { type: 'string', description: 'Path to the file to read', }, encoding: { type: 'string', description: 'File encoding (default: utf-8)', default: 'utf-8', }, offset: { type: 'number', description: 'Line number to start reading from (default: 0)', default: 0, }, limit: { type: 'number', description: 'Maximum number of lines to read (default: 2000)', default: 2000, }, }, required: ['filePath'], }; } getUsageExamples() { return [ 'package.json', 'src/index.ts', 'README.md', '/etc/hosts', ]; } } exports.ReadTool = ReadTool; //# sourceMappingURL=read.js.map