Files
vibn-agent-runner/dist/tools/gitea.js
mawkone e91e5e0e37 refactor: split tools.ts into registry-based domain files
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
2026-03-01 15:27:29 -08:00

167 lines
6.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const registry_1 = require("./registry");
const security_1 = require("./security");
async function giteaFetch(path, ctx, method = 'GET', body) {
const res = await fetch(`${ctx.gitea.apiUrl}/api/v1${path}`, {
method,
headers: {
'Authorization': `token ${ctx.gitea.apiToken}`,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: body ? JSON.stringify(body) : undefined
});
if (!res.ok)
return { error: `Gitea API error: ${res.status} ${res.statusText}` };
return res.json();
}
(0, registry_1.registerTool)({
name: 'gitea_create_issue',
description: 'Create a new issue in a Gitea repository.',
parameters: {
type: 'object',
properties: {
repo: { type: 'string', description: 'Repository in "owner/name" format' },
title: { type: 'string', description: 'Issue title' },
body: { type: 'string', description: 'Issue body (markdown)' },
labels: { type: 'array', items: { type: 'string' }, description: 'Optional label names' }
},
required: ['repo', 'title', 'body']
},
async handler(args, ctx) {
const repo = String(args.repo);
(0, security_1.assertGiteaWritable)(repo);
return giteaFetch(`/repos/${repo}/issues`, ctx, 'POST', {
title: args.title,
body: args.body,
labels: args.labels
});
}
});
(0, registry_1.registerTool)({
name: 'gitea_list_issues',
description: 'List open issues in a Gitea repository.',
parameters: {
type: 'object',
properties: {
repo: { type: 'string', description: 'Repository in "owner/name" format' },
state: { type: 'string', description: '"open", "closed", or "all". Default: "open"' }
},
required: ['repo']
},
async handler(args, ctx) {
const state = String(args.state || 'open');
return giteaFetch(`/repos/${String(args.repo)}/issues?state=${state}&limit=20`, ctx);
}
});
(0, registry_1.registerTool)({
name: 'gitea_close_issue',
description: 'Close an issue in a Gitea repository.',
parameters: {
type: 'object',
properties: {
repo: { type: 'string', description: 'Repository in "owner/name" format' },
issue_number: { type: 'number', description: 'Issue number to close' }
},
required: ['repo', 'issue_number']
},
async handler(args, ctx) {
const repo = String(args.repo);
(0, security_1.assertGiteaWritable)(repo);
return giteaFetch(`/repos/${repo}/issues/${Number(args.issue_number)}`, ctx, 'PATCH', { state: 'closed' });
}
});
(0, registry_1.registerTool)({
name: 'list_repos',
description: 'List all Git repositories in the Gitea organization. Returns repo names, descriptions, and last update time.',
parameters: { type: 'object', properties: {} },
async handler(_args, ctx) {
const res = await fetch(`${ctx.gitea.apiUrl}/api/v1/repos/search?limit=50`, {
headers: { 'Authorization': `token ${ctx.gitea.apiToken}` }
});
const data = await res.json();
return (data.data || [])
.filter((r) => !security_1.PROTECTED_GITEA_REPOS.has(r.full_name))
.map((r) => ({
name: r.full_name,
description: r.description,
default_branch: r.default_branch,
updated: r.updated,
stars: r.stars_count,
open_issues: r.open_issues_count
}));
}
});
(0, registry_1.registerTool)({
name: 'list_all_issues',
description: 'List open issues across all repos or a specific repo. Use this to understand what work is queued or in progress.',
parameters: {
type: 'object',
properties: {
repo: { type: 'string', description: 'Optional: "owner/name" to scope to one repo. Omit for all repos.' },
state: { type: 'string', description: '"open", "closed", or "all". Default: "open"' }
}
},
async handler(args, ctx) {
const state = String(args.state || 'open');
if (args.repo) {
const repo = String(args.repo);
if (security_1.PROTECTED_GITEA_REPOS.has(repo)) {
return { error: `SECURITY: "${repo}" is a protected Vibn platform repo. Agents cannot access its issues.` };
}
return giteaFetch(`/repos/${repo}/issues?state=${state}&limit=20`, ctx);
}
// Fetch across all non-protected repos
const reposRes = await fetch(`${ctx.gitea.apiUrl}/api/v1/repos/search?limit=50`, {
headers: { 'Authorization': `token ${ctx.gitea.apiToken}` }
});
const reposData = await reposRes.json();
const repos = (reposData.data || []).filter((r) => !security_1.PROTECTED_GITEA_REPOS.has(r.full_name));
const allIssues = [];
for (const r of repos.slice(0, 10)) {
const issues = await giteaFetch(`/repos/${r.full_name}/issues?state=${state}&limit=10`, ctx);
if (Array.isArray(issues)) {
allIssues.push(...issues.map((i) => ({
repo: r.full_name,
number: i.number,
title: i.title,
state: i.state,
labels: i.labels?.map((l) => l.name),
created: i.created_at
})));
}
}
return allIssues;
}
});
(0, registry_1.registerTool)({
name: 'read_repo_file',
description: 'Read a file from any Gitea repository without cloning it. Useful for understanding project structure.',
parameters: {
type: 'object',
properties: {
repo: { type: 'string', description: 'Repo in "owner/name" format' },
path: { type: 'string', description: 'File path within the repo (e.g. "src/app/page.tsx")' }
},
required: ['repo', 'path']
},
async handler(args, ctx) {
const repo = String(args.repo);
const filePath = String(args.path);
try {
const res = await fetch(`${ctx.gitea.apiUrl}/api/v1/repos/${repo}/contents/${filePath}`, {
headers: { 'Authorization': `token ${ctx.gitea.apiToken}` }
});
if (!res.ok)
return { error: `File not found: ${filePath} in ${repo}` };
const data = await res.json();
const content = Buffer.from(data.content, 'base64').toString('utf8');
return { repo, path: filePath, content };
}
catch (err) {
return { error: `Failed to read ${filePath}: ${err instanceof Error ? err.message : String(err)}` };
}
}
});