UNPKG

nohandcoder

Version:

An AI agent for code editing, searching, and project analysis

63 lines (62 loc) 2.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SearchFilesTool = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const glob_1 = require("glob"); const chalk_1 = __importDefault(require("chalk")); class SearchFilesTool { constructor(workspaceRoot) { this.workspaceRoot = workspaceRoot; } getDefinition() { return { type: "function", function: { name: "searchFiles", description: "Search for text in files", parameters: { type: "object", properties: { pattern: { type: "string", description: "Glob pattern to match files (e.g. '**/*.ts')", }, text: { type: "string", description: "Text to search for in files", }, }, required: ["pattern", "text"], additionalProperties: false, }, strict: true, }, }; } async execute(args) { console.log(chalk_1.default.blue("\nSearching files...", args.pattern, args.text)); const searchPattern = args.pattern || "**/*"; const results = []; const files = await (0, glob_1.glob)(searchPattern, { cwd: this.workspaceRoot }); for (const file of files) { const fullPath = path_1.default.join(this.workspaceRoot, file); const content = await fs_1.default.promises.readFile(fullPath, "utf-8"); const lines = content.split("\n"); lines.forEach((line, index) => { if (line.includes(args.text)) { results.push({ file, line: index + 1, content: line.trim(), }); } }); } return results; } } exports.SearchFilesTool = SearchFilesTool;