6
6
} from '@codebuff/common/tools/constants'
7
7
import { buildArray } from '@codebuff/common/util/array'
8
8
import { generateCompactId } from '@codebuff/common/util/string'
9
+ import { cloneDeep } from 'lodash'
9
10
10
11
import { expireMessages } from '../util/messages'
11
12
import { sendAction } from '../websockets/websocket-action'
@@ -17,7 +18,10 @@ import type { StreamChunk } from '../llm-apis/vercel-ai-sdk/ai-sdk'
17
18
import type { AgentTemplate } from '../templates/types'
18
19
import type { ToolName } from '@codebuff/common/tools/constants'
19
20
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
20
- import type { Message } from '@codebuff/common/types/messages/codebuff-message'
21
+ import type {
22
+ Message ,
23
+ ToolMessage ,
24
+ } from '@codebuff/common/types/messages/codebuff-message'
21
25
import type { ToolResultPart } from '@codebuff/common/types/messages/content-part'
22
26
import type { PrintModeEvent } from '@codebuff/common/types/print-mode'
23
27
import type { AgentState , Subgoal } from '@codebuff/common/types/session-state'
@@ -70,6 +74,7 @@ export async function processStreamWithTools(options: {
70
74
const messages = [ ...options . messages ]
71
75
72
76
const toolResults : ToolResultPart [ ] = [ ]
77
+ const toolResultsToAddAfterStream : ToolResultPart [ ] = [ ]
73
78
const toolCalls : ( CodebuffToolCall | CustomToolCall ) [ ] = [ ]
74
79
const { promise : streamDonePromise , resolve : resolveStreamDonePromise } =
75
80
Promise . withResolvers < void > ( )
@@ -160,12 +165,14 @@ export async function processStreamWithTools(options: {
160
165
] ) ,
161
166
] ) ,
162
167
( toolName , error ) => {
163
- toolResults . push ( {
168
+ const toolResult : ToolResultPart = {
164
169
type : 'tool-result' ,
165
170
toolName,
166
171
toolCallId : generateCompactId ( ) ,
167
172
output : [ { type : 'json' , value : { errorMessage : error } } ] ,
168
- } )
173
+ }
174
+ toolResults . push ( cloneDeep ( toolResult ) )
175
+ toolResultsToAddAfterStream . push ( cloneDeep ( toolResult ) )
169
176
} ,
170
177
onResponseChunk ,
171
178
{
@@ -212,6 +219,12 @@ export async function processStreamWithTools(options: {
212
219
role : 'assistant' as const ,
213
220
content : fullResponseChunks . join ( '' ) ,
214
221
} ,
222
+ ...toolResultsToAddAfterStream . map ( ( toolResult ) => {
223
+ return {
224
+ role : 'tool' ,
225
+ content : toolResult ,
226
+ } satisfies ToolMessage
227
+ } ) ,
215
228
] )
216
229
217
230
resolveStreamDonePromise ( )
0 commit comments