hataraku
Version:
An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.
139 lines • 5.74 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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 zod_1 = require("zod");
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const fast_glob_1 = __importDefault(require("fast-glob"));
async function searchInFile(filePath, regex) {
const content = await fs.readFile(filePath, 'utf-8');
const lines = content.split('\n');
const matches = [];
const contextLines = 2; // Number of lines before and after match
for (let i = 0; i < lines.length; i++) {
if (regex.test(lines[i])) {
const start = Math.max(0, i - contextLines);
const end = Math.min(lines.length, i + contextLines + 1);
matches.push(`File: ${filePath}:${i + 1}`, '```', ...lines.slice(start, end).map((line, index) => {
const lineNum = start + index + 1;
const prefix = start + index === i ? ' > ' : ' ';
return `${lineNum}${prefix}${line}`;
}), '```\n');
}
}
return matches;
}
exports.searchFilesTool = {
description: "Search for patterns in files using regular expressions. Provides context around matches.",
parameters: zod_1.z.object({
path: zod_1.z.string().describe('The path of the directory to search in (relative to the current working directory)'),
regex: zod_1.z.string().describe('The regular expression pattern to search for'),
file_pattern: zod_1.z.string().optional().describe('Glob pattern to filter files (e.g., "*.ts" for TypeScript files)')
}),
execute: async ({ path: dirPath, regex, file_pattern }) => {
try {
const absolutePath = path.resolve(process.cwd(), dirPath);
// Validate regex
try {
regex = new RegExp(regex);
}
catch (error) {
return {
isError: true,
content: [{
type: "text",
text: `Invalid regular expression: ${error instanceof Error ? error.message : 'Unknown error'}`
}]
};
}
// Find files to search
const pattern = path.join(absolutePath, file_pattern || '**/*');
const files = await (0, fast_glob_1.default)(pattern, {
ignore: ['**/node_modules/**', '**/dist/**', '**/build/**'],
onlyFiles: true
});
if (files.length === 0) {
return {
content: [{
type: "text",
text: `No files found matching pattern: ${file_pattern || '*'}`
}]
};
}
// Search in files
const searchRegex = new RegExp(regex);
const allMatches = [];
for (const file of files) {
try {
const matches = await searchInFile(file, searchRegex);
allMatches.push(...matches);
}
catch (error) {
console.warn(`Error searching file ${file}: ${error instanceof Error ? error.message : 'Unknown error'}`);
// Continue with other files instead of returning null
continue;
}
}
if (allMatches.length === 0) {
return {
content: [{
type: "text",
text: `No matches found for pattern: ${regex.source}`
}]
};
}
return {
content: [{
type: "text",
text: allMatches.join('\n')
}]
};
}
catch (error) {
return {
isError: true,
content: [{
type: "text",
text: `Error searching files: ${error instanceof Error ? error.message : 'Unknown error'}`
}]
};
}
}
};
//# sourceMappingURL=search-files.js.map