1- import fs from 'fs'
2- import path from 'path'
31import { execSync } from 'child_process'
42import { promisify } from 'util'
53import { exec } from 'child_process'
@@ -9,13 +7,16 @@ const execAsync = promisify(exec)
97import { withTimeout } from '@codebuff/common/util/promise'
108import { CodebuffClient } from '@codebuff/sdk'
119import { withTestRepo } from '../subagents/test-repo-utils'
10+ import { ClaudeRunner } from './runners/claude'
11+ import { CodexRunner } from './runners/codex'
12+ import { CodebuffRunner } from './runners/codebuff'
1213
13- import type { PrintModeEvent } from '@codebuff/common/types/print-mode'
1414import type { EvalCommitV2 , FinalCheckOutput } from './types'
15+ import type { Runner , AgentStep } from './runners/runner'
1516
16- export type AgentStep = PrintModeEvent
17+ export type { AgentStep }
1718
18- const DEBUG_ERROR = true
19+ export type ExternalAgentType = 'claude' | 'codex'
1920
2021export async function runAgentOnCommit ( {
2122 client,
@@ -27,6 +28,7 @@ export async function runAgentOnCommit({
2728 localAgentDefinitions,
2829 printEvents,
2930 finalCheckCommands,
31+ externalAgentType,
3032} : {
3133 client : CodebuffClient
3234 agentId : string
@@ -37,6 +39,7 @@ export async function runAgentOnCommit({
3739 localAgentDefinitions : any [ ]
3840 printEvents : boolean
3941 finalCheckCommands ?: string [ ]
42+ externalAgentType ?: ExternalAgentType
4043} ) : Promise < {
4144 diff : string
4245 contextFiles : Record < string , string >
@@ -66,59 +69,33 @@ export async function runAgentOnCommit({
6669 env,
6770 } ,
6871 async ( repoDir ) => {
69- const maxAgentSteps = 40
70- const result = await client . run ( {
71- agent : agentId ,
72- prompt : commit . prompt ,
73- agentDefinitions : localAgentDefinitions ,
74- cwd : repoDir ,
75- env,
76- maxAgentSteps,
77- handleEvent : ( event ) => {
78- if (
79- ( event . type === 'tool_call' || event . type === 'tool_result' ) &&
80- event . toolName === 'set_messages'
81- ) {
82- return
83- }
84- if ( event . type === 'error' ) {
85- console . error (
86- `[${ commit . id } :${ agentId } ] Error event:` ,
87- event . message ,
88- )
89- if ( DEBUG_ERROR && ! event . message . startsWith ( 'Invalid JSON' ) ) {
90- // Save errors in a file, but not tool calls with invalid json.
91- fs . writeFileSync (
92- path . join (
93- __dirname ,
94- `${ commit . id } -${ agentId } -error-${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 6 ) } .json` ,
95- ) ,
96- JSON . stringify (
97- {
98- error : event . message ,
99- trace : trace ,
100- } ,
101- null ,
102- 2 ,
103- ) ,
104- )
105- }
106- } else if ( printEvents ) {
107- console . log (
108- `[${ commit . id } :${ agentId } ]` ,
109- JSON . stringify ( event , null , 2 ) ,
110- )
111- }
112- trace . push ( event )
113- } ,
114- } )
115- cost = ( result . sessionState ?. mainAgentState . creditsUsed ?? 0 ) / 100
116-
117- execSync ( 'git add .' , { cwd : repoDir , stdio : 'ignore' } )
118- diff = execSync ( `git diff ${ commit . parentSha } ` , {
119- cwd : repoDir ,
120- encoding : 'utf-8' ,
121- } )
72+ // Select the appropriate runner
73+ let runner : Runner
74+ if ( externalAgentType === 'claude' ) {
75+ runner = new ClaudeRunner ( repoDir , env )
76+ } else if ( externalAgentType === 'codex' ) {
77+ runner = new CodexRunner ( repoDir , env )
78+ } else {
79+ runner = new CodebuffRunner ( {
80+ cwd : repoDir ,
81+ env,
82+ client,
83+ agentId,
84+ localAgentDefinitions,
85+ printEvents,
86+ commitId : commit . id ,
87+ parentSha : commit . parentSha ,
88+ } )
89+ }
90+
91+ console . log (
92+ `[${ commit . id } ] Running agent: ${ externalAgentType || 'codebuff' } ` ,
93+ )
94+
95+ const result = await runner . run ( commit . prompt )
96+ trace . push ( ...result . steps )
97+ cost = result . totalCostUsd
98+ diff = result . diff
12299
123100 const contextFilePaths = new Set < string > ( [
124101 ...commit . supplementalFiles ,
0 commit comments