|
| 1 | +import { writeFileSync, mkdirSync, rmSync } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { spawnSync } from 'node:child_process'; |
| 5 | +import { randomBytes } from 'node:crypto'; |
| 6 | +import { exportToGitclaw } from '../adapters/gitclaw.js'; |
| 7 | +import { AgentManifest } from '../utils/loader.js'; |
| 8 | +import { error, info } from '../utils/format.js'; |
| 9 | + |
| 10 | +export interface GitclawRunOptions { |
| 11 | + prompt?: string; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Run a gitagent agent using the gitclaw SDK. |
| 16 | + * |
| 17 | + * Creates a temporary workspace with the full gitclaw directory structure: |
| 18 | + * - agent.yaml (provider:model format) |
| 19 | + * - SOUL.md, RULES.md, DUTIES.md |
| 20 | + * - skills/, tools/, hooks/, knowledge/ |
| 21 | + * |
| 22 | + * Then launches `gitclaw` CLI in that workspace. |
| 23 | + * Supports both interactive mode (no prompt) and single-shot mode (`gitclaw run -p`). |
| 24 | + */ |
| 25 | +export function runWithGitclaw(agentDir: string, manifest: AgentManifest, options: GitclawRunOptions = {}): void { |
| 26 | + const exp = exportToGitclaw(agentDir); |
| 27 | + |
| 28 | + // Create a temporary workspace |
| 29 | + const workspaceDir = join(tmpdir(), `gitagent-gitclaw-${randomBytes(4).toString('hex')}`); |
| 30 | + mkdirSync(workspaceDir, { recursive: true }); |
| 31 | + |
| 32 | + // Write agent.yaml |
| 33 | + writeFileSync(join(workspaceDir, 'agent.yaml'), exp.agentYaml, 'utf-8'); |
| 34 | + |
| 35 | + // Write identity files |
| 36 | + if (exp.soulMd) writeFileSync(join(workspaceDir, 'SOUL.md'), exp.soulMd, 'utf-8'); |
| 37 | + if (exp.rulesMd) writeFileSync(join(workspaceDir, 'RULES.md'), exp.rulesMd, 'utf-8'); |
| 38 | + if (exp.dutiesMd) writeFileSync(join(workspaceDir, 'DUTIES.md'), exp.dutiesMd, 'utf-8'); |
| 39 | + |
| 40 | + // Write knowledge index |
| 41 | + if (exp.knowledgeIndex) { |
| 42 | + mkdirSync(join(workspaceDir, 'knowledge'), { recursive: true }); |
| 43 | + writeFileSync(join(workspaceDir, 'knowledge', 'index.yaml'), exp.knowledgeIndex, 'utf-8'); |
| 44 | + } |
| 45 | + |
| 46 | + // Write skills |
| 47 | + for (const skill of exp.skills) { |
| 48 | + const skillDir = join(workspaceDir, 'skills', skill.name); |
| 49 | + mkdirSync(skillDir, { recursive: true }); |
| 50 | + writeFileSync(join(skillDir, 'SKILL.md'), skill.content, 'utf-8'); |
| 51 | + } |
| 52 | + |
| 53 | + // Write tools |
| 54 | + if (exp.tools.length > 0) { |
| 55 | + mkdirSync(join(workspaceDir, 'tools'), { recursive: true }); |
| 56 | + for (const tool of exp.tools) { |
| 57 | + writeFileSync(join(workspaceDir, 'tools', tool.name), tool.content, 'utf-8'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Write hooks |
| 62 | + if (exp.hooks) { |
| 63 | + mkdirSync(join(workspaceDir, 'hooks'), { recursive: true }); |
| 64 | + writeFileSync(join(workspaceDir, 'hooks', 'hooks.yaml'), exp.hooks, 'utf-8'); |
| 65 | + } |
| 66 | + |
| 67 | + info(`Workspace prepared at ${workspaceDir}`); |
| 68 | + info(` agent.yaml${exp.soulMd ? ', SOUL.md' : ''}${exp.rulesMd ? ', RULES.md' : ''}`); |
| 69 | + if (exp.skills.length > 0) { |
| 70 | + info(` Skills: ${exp.skills.map(s => s.name).join(', ')}`); |
| 71 | + } |
| 72 | + if (manifest.model?.preferred) { |
| 73 | + info(` Model: ${manifest.model.preferred}`); |
| 74 | + } |
| 75 | + |
| 76 | + // Build gitclaw CLI args |
| 77 | + const args: string[] = []; |
| 78 | + |
| 79 | + if (options.prompt) { |
| 80 | + args.push('run', '-p', options.prompt); |
| 81 | + } |
| 82 | + |
| 83 | + info(`Launching gitclaw agent "${manifest.name}"...`); |
| 84 | + if (!options.prompt) { |
| 85 | + info('Starting interactive mode. Type your messages to chat.'); |
| 86 | + } |
| 87 | + |
| 88 | + const result = spawnSync('gitclaw', args, { |
| 89 | + stdio: 'inherit', |
| 90 | + cwd: workspaceDir, |
| 91 | + env: { ...process.env }, |
| 92 | + }); |
| 93 | + |
| 94 | + // Cleanup temp workspace before exiting |
| 95 | + try { rmSync(workspaceDir, { recursive: true, force: true }); } catch { /* ignore */ } |
| 96 | + |
| 97 | + if (result.error) { |
| 98 | + error(`Failed to launch gitclaw: ${result.error.message}`); |
| 99 | + info('Make sure gitclaw is installed: npm install -g gitclaw'); |
| 100 | + process.exitCode = 1; |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + process.exitCode = result.status ?? 0; |
| 105 | +} |
0 commit comments