Skip to content

Commit 9d4b076

Browse files
committed
Attempt one more fix for pushing errors after all tool results
1 parent ad9ad64 commit 9d4b076

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

packages/agent-runtime/src/tools/stream-parser.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { generateCompactId } from '@codebuff/common/util/string'
99
import { cloneDeep } from 'lodash'
1010

1111
import { processStreamWithTools } from '../tool-stream-parser'
12-
import { executeCustomToolCall, executeToolCall, tryTransformAgentToolCall } from './tool-executor'
12+
import {
13+
executeCustomToolCall,
14+
executeToolCall,
15+
tryTransformAgentToolCall,
16+
} from './tool-executor'
1317
import { expireMessages, withSystemTags } from '../util/messages'
1418

1519
import type { CustomToolCall, ExecuteToolCallParams } from './tool-executor'
@@ -145,14 +149,14 @@ export async function processStream(
145149
return
146150
}
147151
const toolCallId = generateCompactId()
148-
152+
149153
// Check if this is an agent tool call - if so, transform to spawn_agents
150154
const transformed = tryTransformAgentToolCall({
151155
toolName,
152156
input,
153157
spawnableAgents: agentTemplate.spawnableAgents,
154158
})
155-
159+
156160
if (transformed) {
157161
// Use executeToolCall for spawn_agents (a native tool)
158162
previousToolCallFinished = executeToolCall({
@@ -276,7 +280,7 @@ export async function processStream(
276280
fullResponseChunks.push(chunk.text)
277281
} else if (chunk.type === 'error') {
278282
onResponseChunk(chunk)
279-
283+
280284
hadToolCallError = true
281285
// Collect error messages to add AFTER all tool results
282286
// This ensures proper message ordering for Anthropic's API which requires
@@ -300,13 +304,16 @@ export async function processStream(
300304
...expireMessages(agentState.messageHistory, 'agentStep'),
301305
...assistantMessages,
302306
...toolResultsToAddAfterStream,
303-
...errorMessages, // Error messages must come AFTER tool results for proper API ordering
304307
])
305308

306309
if (!signal.aborted) {
307310
resolveStreamDonePromise()
308311
await previousToolCallFinished
309312
}
313+
314+
// Error messages must come AFTER tool results for proper API ordering)
315+
agentState.messageHistory.push(...errorMessages)
316+
310317
return {
311318
fullResponse: fullResponseChunks.join(''),
312319
fullResponseChunks,

0 commit comments

Comments
 (0)