import { exec } from 'node:child_process'; import { promisify } from 'node:util'; const execAsync = promisify(exec); export type GitHubAgentAction = | { action: 'agent.help' } | { action: 'sin.github.health' } | { action: 'sin.github.project.orchestrate'; prompt: string; contextDir?: string } | { action: 'sin.github.issue.manage'; prompt: string; issueNumber?: number } | { action: 'sin.github.wiki.sync'; prompt: string; contextDir?: string } | { action: 'sin.github.discussion.start'; prompt: string; category?: string } | { action: 'sin.github.gist.publish'; prompt: string; isPublic?: boolean } | { action: 'sin.github.security.audit'; prompt: string; contextDir?: string } | { action: 'sin.github.pr.review'; prNumber: number; contextDir?: string } | { action: 'sin.github.ledger.log'; agentName: string; activityTitle: string; details: string }; export async function executeGitHubAgentAction(action: GitHubAgentAction): Promise { switch (action.action) { case 'agent.help': return { ok: true, agent: 'sin-github-issues', mandate: 'CEO Elite GitHub Operations. Orchestrates Projects, Issues, Wikis, Discussions, Gists, Security, PR Reviews, and public Showcase Ledgers.', actions: [ 'sin.github.health', 'sin.github.project.orchestrate', 'sin.github.issue.manage', 'sin.github.wiki.sync', 'sin.github.discussion.start', 'sin.github.gist.publish', 'sin.github.security.audit', 'sin.github.pr.review', 'sin.github.ledger.log' ], }; case 'sin.github.health': return { ok: true, agent: 'sin-github-issues', primaryModel: 'openai/gpt-5.4', fallbackModel: 'opencode/minimax-m2.5-free', team: 'Team - Coding', status: 'Elite 2026 GitHub Operations Architect Online', capabilities: ['projects', 'wikis', 'discussions', 'gists', 'security', 'prs', 'issues', 'ledger'], }; case 'sin.github.project.orchestrate': return await executeOpenCode( `Use the GitHub CLI ('gh project create/link/item-add') to orchestrate the following project board requirements: ${action.prompt}. Ensure the board is linked to the repository and items are properly assigned.`, action.contextDir ); case 'sin.github.issue.manage': return await executeOpenCode( `Manage GitHub issues based on: ${action.prompt}. ${action.issueNumber ? `Target issue: #${action.issueNumber}` : 'Create a new Epic/Task.'} Ensure proper labeling, assignment, and milestone linking.` ); case 'sin.github.wiki.sync': return await executeOpenCode( `Synchronize the repository's Wiki with the following documentation update: ${action.prompt}. Generate standard Markdown and push it to the wiki repository (.wiki.git).`, action.contextDir ); case 'sin.github.discussion.start': return await executeOpenCode( `Start or manage a GitHub Discussion to keep issues clean. Requirements: ${action.prompt}. Category: ${action.category || 'General'}.` ); case 'sin.github.gist.publish': return await executeOpenCode( `Create a GitHub Gist (public: ${action.isPublic ?? false}) for the following payload (long log, snippet, or config): ${action.prompt}. Return the Gist URL for embedding in an issue or PR.` ); case 'sin.github.security.audit': return await executeOpenCode( `Audit the repository for security vulnerabilities (Dependabot/CodeQL). Generate or update '.github/dependabot.yml' and review any active alerts based on: ${action.prompt}.`, action.contextDir ); case 'sin.github.pr.review': return await executeOpenCode( `Perform a rigorous Code Review on Pull Request #${action.prNumber}. Check for 2026 architectural compliance, security vulnerabilities, performance regressions, and test coverage. If it passes 100%, approve it ('gh pr review --approve') and merge it ('gh pr merge --auto').`, action.contextDir ); case 'sin.github.ledger.log': return await executeOpenCode( `Create a new public GitHub Issue or Discussion in the repository 'Delqhi/OpenSolver-Ledger' titled "[${action.agentName}] ${action.activityTitle}". The body must contain the following details formatted in premium markdown to showcase our fleet's capabilities: \n\n${action.details}` ); } } async function executeOpenCode(prompt: string, dir?: string) { await execAsync('python3 scripts/hf_pull_script.py').catch(() => console.warn('Warning: hf_pull_script failed. Proceeding with cached credentials.')); const cmd = `opencode run "${prompt.replace(/"/g, '\\"')}" --model openai/gpt-5.4 --fallback opencode/minimax-m2.5-free`; const options = dir ? { cwd: dir } : {}; try { const { stdout, stderr } = await execAsync(cmd, options); return { ok: true, expertAnalysis: stdout, warnings: stderr || undefined, }; } catch (error: any) { throw new Error(`GitHub Operations Execution Failed: ${error.message}`); } }