11import { getToolCallString } from '@codebuff/common/tools/utils'
22import { getErrorObject } from '@codebuff/common/util/error'
3+ import { cloneDeep } from 'lodash'
34
45import { addAgentStep } from './agent-run'
56import { executeToolCall } from './tools/tool-executor'
@@ -142,9 +143,9 @@ export async function runProgrammaticStep(
142143 ...data ,
143144 } )
144145 } ,
145- agentState : { ... agentState } ,
146- agentContext : agentState . agentContext ,
147- messages : agentState . messageHistory . map ( ( msg ) => ( { ... msg } ) ) ,
146+ agentState : cloneDeep ( agentState ) ,
147+ agentContext : cloneDeep ( agentState . agentContext ) ,
148+ messages : cloneDeep ( agentState . messageHistory ) ,
148149 }
149150
150151 let toolResult : ToolResultOutput [ ] = [ ]
@@ -158,8 +159,8 @@ export async function runProgrammaticStep(
158159 // Execute tools synchronously as the generator yields them
159160 do {
160161 startTime = new Date ( )
161- creditsBefore = agentState . directCreditsUsed
162- childrenBefore = agentState . childRunIds . length
162+ creditsBefore = state . agentState . directCreditsUsed
163+ childrenBefore = state . agentState . childRunIds . length
163164
164165 const result = sandbox
165166 ? await sandbox . executeStep ( {
@@ -181,7 +182,7 @@ export async function runProgrammaticStep(
181182 break
182183 }
183184 if ( result . value === 'STEP_ALL' ) {
184- agentIdToStepAll . add ( agentState . agentId )
185+ agentIdToStepAll . add ( state . agentState . agentId )
185186 break
186187 }
187188
@@ -216,8 +217,8 @@ export async function runProgrammaticStep(
216217 } )
217218 state . sendSubagentChunk ( {
218219 userInputId,
219- agentId : agentState . agentId ,
220- agentType : agentState . agentType ! ,
220+ agentId : state . agentState . agentId ,
221+ agentType : state . agentState . agentType ! ,
221222 chunk : toolCallString ,
222223 } )
223224 }
@@ -250,13 +251,13 @@ export async function runProgrammaticStep(
250251 // Get the latest tool result
251252 toolResult = toolResults [ toolResults . length - 1 ] ?. output
252253
253- if ( agentState . runId ) {
254+ if ( state . agentState . runId ) {
254255 await addAgentStep ( {
255256 userId,
256- agentRunId : agentState . runId ,
257+ agentRunId : state . agentState . runId ,
257258 stepNumber,
258- credits : agentState . directCreditsUsed - creditsBefore ,
259- childRunIds : agentState . childRunIds . slice ( childrenBefore ) ,
259+ credits : state . agentState . directCreditsUsed - creditsBefore ,
260+ childRunIds : state . agentState . childRunIds . slice ( childrenBefore ) ,
260261 status : 'completed' ,
261262 startTime,
262263 } )
0 commit comments