Replaces the single 800-line tools.ts and its switch dispatcher with a Theia-inspired registry pattern — each tool domain is its own file, and dispatch is a plain Map.get() call with no central routing function. New structure in src/tools/: registry.ts — ToolDefinition (with handler), registerTool(), executeTool(), ALL_TOOLS context.ts — ToolContext, MemoryUpdate interfaces security.ts — PROTECTED_* constants + assertGiteaWritable/assertCoolifyDeployable utils.ts — safeResolve(), EXCLUDED set file.ts — read_file, write_file, replace_in_file, list_directory, find_files, search_code shell.ts — execute_command git.ts — git_commit_and_push coolify.ts — coolify_*, list_all_apps, get_app_status, deploy_app gitea.ts — gitea_*, list_repos, list_all_issues, read_repo_file agent.ts — spawn_agent, get_job_status memory.ts — save_memory index.ts — barrel with side-effect imports + re-exports Adding a new tool now requires only a new file + registerTool() call. No switch statement, no shared array to edit. External API unchanged. Made-with: Cursor
216 lines
8.3 KiB
JavaScript
216 lines
8.3 KiB
JavaScript
"use strict";
|
|
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;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const fs = __importStar(require("fs"));
|
|
const path = __importStar(require("path"));
|
|
const cp = __importStar(require("child_process"));
|
|
const util = __importStar(require("util"));
|
|
const minimatch_1 = require("minimatch");
|
|
const registry_1 = require("./registry");
|
|
const utils_1 = require("./utils");
|
|
const execAsync = util.promisify(cp.exec);
|
|
(0, registry_1.registerTool)({
|
|
name: 'read_file',
|
|
description: 'Read the complete content of a file in the workspace. Always read before editing.',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
path: { type: 'string', description: 'Relative path from workspace root (e.g. "src/index.ts")' }
|
|
},
|
|
required: ['path']
|
|
},
|
|
async handler(args, ctx) {
|
|
const abs = (0, utils_1.safeResolve)(ctx.workspaceRoot, String(args.path));
|
|
try {
|
|
return fs.readFileSync(abs, 'utf8');
|
|
}
|
|
catch {
|
|
return { error: `File not found: ${args.path}` };
|
|
}
|
|
}
|
|
});
|
|
(0, registry_1.registerTool)({
|
|
name: 'write_file',
|
|
description: 'Write complete content to a file. Creates parent directories if needed. Overwrites existing files.',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
path: { type: 'string', description: 'Relative path from workspace root' },
|
|
content: { type: 'string', description: 'Complete new file content' }
|
|
},
|
|
required: ['path', 'content']
|
|
},
|
|
async handler(args, ctx) {
|
|
const abs = (0, utils_1.safeResolve)(ctx.workspaceRoot, String(args.path));
|
|
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
fs.writeFileSync(abs, String(args.content), 'utf8');
|
|
return { success: true, path: args.path, bytes: Buffer.byteLength(String(args.content)) };
|
|
}
|
|
});
|
|
(0, registry_1.registerTool)({
|
|
name: 'replace_in_file',
|
|
description: 'Replace an exact string in a file. The old_content must match character-for-character. Read the file first.',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
path: { type: 'string', description: 'Relative path from workspace root' },
|
|
old_content: { type: 'string', description: 'Exact text to replace' },
|
|
new_content: { type: 'string', description: 'Replacement text' }
|
|
},
|
|
required: ['path', 'old_content', 'new_content']
|
|
},
|
|
async handler(args, ctx) {
|
|
const abs = (0, utils_1.safeResolve)(ctx.workspaceRoot, String(args.path));
|
|
const current = fs.readFileSync(abs, 'utf8');
|
|
if (!current.includes(String(args.old_content))) {
|
|
return { error: 'old_content not found in file. Read the file again to get the current content.' };
|
|
}
|
|
fs.writeFileSync(abs, current.replace(String(args.old_content), String(args.new_content)), 'utf8');
|
|
return { success: true, path: args.path };
|
|
}
|
|
});
|
|
(0, registry_1.registerTool)({
|
|
name: 'list_directory',
|
|
description: 'List files and subdirectories in a directory. Directories have trailing "/".',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
path: { type: 'string', description: 'Relative path from workspace root. Use "." for root.' }
|
|
},
|
|
required: ['path']
|
|
},
|
|
async handler(args, ctx) {
|
|
const abs = (0, utils_1.safeResolve)(ctx.workspaceRoot, String(args.path));
|
|
try {
|
|
const entries = fs.readdirSync(abs, { withFileTypes: true });
|
|
return entries
|
|
.filter(e => !utils_1.EXCLUDED.has(e.name))
|
|
.map(e => e.isDirectory() ? `${e.name}/` : e.name);
|
|
}
|
|
catch {
|
|
return { error: `Directory not found: ${args.path}` };
|
|
}
|
|
}
|
|
});
|
|
(0, registry_1.registerTool)({
|
|
name: 'find_files',
|
|
description: 'Find files matching a glob pattern in the workspace. Returns up to 200 relative paths.',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
pattern: { type: 'string', description: 'Glob pattern e.g. "**/*.ts", "src/**/*.test.js"' }
|
|
},
|
|
required: ['pattern']
|
|
},
|
|
async handler(args, ctx) {
|
|
const matcher = new minimatch_1.Minimatch(String(args.pattern), { dot: false });
|
|
const results = [];
|
|
function walk(dir) {
|
|
if (results.length >= 200)
|
|
return;
|
|
let entries;
|
|
try {
|
|
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
}
|
|
catch {
|
|
return;
|
|
}
|
|
for (const e of entries) {
|
|
if (utils_1.EXCLUDED.has(e.name))
|
|
continue;
|
|
const abs = path.join(dir, e.name);
|
|
const rel = path.relative(ctx.workspaceRoot, abs).split(path.sep).join('/');
|
|
if (e.isDirectory()) {
|
|
walk(abs);
|
|
}
|
|
else if (matcher.match(rel)) {
|
|
results.push(rel);
|
|
}
|
|
}
|
|
}
|
|
walk(ctx.workspaceRoot);
|
|
return { files: results, truncated: results.length >= 200 };
|
|
}
|
|
});
|
|
(0, registry_1.registerTool)({
|
|
name: 'search_code',
|
|
description: 'Search file contents for a string or regex pattern. Returns file path, line number, and matching line.',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
query: { type: 'string', description: 'Search term or regex' },
|
|
file_extensions: {
|
|
type: 'array',
|
|
items: { type: 'string' },
|
|
description: 'Optional: limit to these extensions e.g. ["ts","js"]'
|
|
}
|
|
},
|
|
required: ['query']
|
|
},
|
|
async handler(args, ctx) {
|
|
const extensions = args.file_extensions;
|
|
const globPatterns = extensions?.map(e => `*.${e}`) || [];
|
|
const rgArgs = ['--line-number', '--no-heading', '--color=never', '--max-count=30'];
|
|
for (const ex of utils_1.EXCLUDED) {
|
|
rgArgs.push('--glob', `!${ex}`);
|
|
}
|
|
if (globPatterns.length > 0) {
|
|
for (const g of globPatterns)
|
|
rgArgs.push('--glob', g);
|
|
}
|
|
rgArgs.push('--fixed-strings', String(args.query), ctx.workspaceRoot);
|
|
try {
|
|
const { stdout } = await execAsync(`rg ${rgArgs.map(a => `'${a}'`).join(' ')}`, {
|
|
cwd: ctx.workspaceRoot, timeout: 15000
|
|
});
|
|
return stdout.trim().split('\n').filter(Boolean).map(line => {
|
|
const m = line.match(/^(.+?):(\d+):(.*)$/);
|
|
if (!m)
|
|
return null;
|
|
return {
|
|
file: path.relative(ctx.workspaceRoot, m[1]).split(path.sep).join('/'),
|
|
line: parseInt(m[2]),
|
|
content: m[3].trim()
|
|
};
|
|
}).filter(Boolean);
|
|
}
|
|
catch (err) {
|
|
if (err.code === 1)
|
|
return []; // ripgrep exit 1 = no matches
|
|
return { error: `Search failed: ${err.message}` };
|
|
}
|
|
}
|
|
});
|