| 7 | } from '@codebuff/common/types/print-mode' |
| 8 | |
| 9 | export class ClaudeRunner implements Runner { |
| 10 | private cwd: string |
| 11 | private env: Record<string, string> |
| 12 | |
| 13 | constructor(cwd: string, env: Record<string, string> = {}) { |
| 14 | this.cwd = cwd |
| 15 | this.env = env |
| 16 | } |
| 17 | |
| 18 | async run(prompt: string): Promise<RunnerResult> { |
| 19 | const steps: AgentStep[] = [] |
| 20 | let totalCostUsd = 0 |
| 21 | |
| 22 | return new Promise((resolve, reject) => { |
| 23 | const args = [ |
| 24 | '-p', |
| 25 | prompt, |
| 26 | '--output-format', |
| 27 | 'stream-json', |
| 28 | '--verbose', |
| 29 | '--dangerously-skip-permissions', |
| 30 | '--model', |
| 31 | 'claude-opus-4-5-20251101', |
| 32 | ] |
| 33 | |
| 34 | console.log(`[ClaudeRunner] Running: claude ${args.join(' ')}`) |
| 35 | |
| 36 | const child = spawn('claude', args, { |
| 37 | cwd: this.cwd, |
| 38 | env: { |
| 39 | ...process.env, |
| 40 | ...this.env, |
| 41 | // Ensure ANTHROPIC_API_KEY is set from CLAUDE_CODE_KEY if available |
| 42 | ANTHROPIC_API_KEY: |
| 43 | process.env.CLAUDE_CODE_KEY || process.env.ANTHROPIC_API_KEY, |
| 44 | }, |
| 45 | // Use 'ignore' for stdin to prevent the CLI from waiting for input |
| 46 | stdio: ['ignore', 'pipe', 'pipe'], |
| 47 | }) |
| 48 | |
| 49 | let _stdout = '' |
| 50 | let stderr = '' |
| 51 | let responseText = '' |
| 52 | let toolCalls: PrintModeToolCall[] = [] |
| 53 | let toolResults: PrintModeToolResult[] = [] |
| 54 | |
| 55 | function flushStep() { |
| 56 | if (responseText.length > 0) { |
| 57 | steps.push({ type: 'text', text: responseText }) |
| 58 | } |
| 59 | for (const call of toolCalls) { |
| 60 | steps.push(call) |
| 61 | } |
| 62 | for (const result of toolResults) { |
| 63 | steps.push(result) |
| 64 | } |
| 65 | responseText = '' |
| 66 | toolCalls = [] |
nothing calls this directly
no outgoing calls
no test coverage detected