@@ -116,10 +116,6 @@ export const defaultApplyEvents = (
116
116
}
117
117
118
118
case EventType . TEXT_MESSAGE_CONTENT : {
119
- const { delta } = event as TextMessageContentEvent ;
120
- const currentContent = messages [ messages . length - 1 ] . content ?? "" ;
121
- const newTextMessageBuffer = currentContent + delta ;
122
-
123
119
const mutation = await runSubscribersWithMutation (
124
120
subscribers ,
125
121
messages ,
@@ -131,15 +127,17 @@ export const defaultApplyEvents = (
131
127
state,
132
128
agent,
133
129
input,
134
- textMessageBuffer : newTextMessageBuffer ,
130
+ textMessageBuffer : messages [ messages . length - 1 ] . content ?? "" ,
135
131
} ) ,
136
132
) ;
137
133
applyMutation ( mutation ) ;
138
134
139
135
if ( mutation . stopPropagation !== true ) {
136
+ const { delta } = event as TextMessageContentEvent ;
137
+
140
138
// Get the last message and append the content
141
139
const lastMessage = messages [ messages . length - 1 ] ;
142
- lastMessage . content = newTextMessageBuffer ;
140
+ lastMessage . content = lastMessage . content ! + delta ;
143
141
applyMutation ( { messages } ) ;
144
142
}
145
143
@@ -235,31 +233,30 @@ export const defaultApplyEvents = (
235
233
}
236
234
237
235
case EventType . TOOL_CALL_ARGS : {
238
- const { delta } = event as ToolCallArgsEvent ;
239
- const toolCalls = ( messages [ messages . length - 1 ] as AssistantMessage ) ?. toolCalls ?? [ ] ;
240
- const currentToolCallBuffer =
241
- toolCalls . length > 0 ? toolCalls [ toolCalls . length - 1 ] . function . arguments : "" ;
242
- const newToolCallBuffer = currentToolCallBuffer + delta ;
243
- const toolCallName =
244
- toolCalls . length > 0 ? toolCalls [ toolCalls . length - 1 ] . function . name : "" ;
245
-
246
- let partialToolCallArgs = { } ;
247
- try {
248
- partialToolCallArgs = untruncateJson ( newToolCallBuffer ) ;
249
- } catch ( error ) { }
250
-
251
236
const mutation = await runSubscribersWithMutation (
252
237
subscribers ,
253
238
messages ,
254
239
state ,
255
240
( subscriber , messages , state ) => {
241
+ const toolCalls =
242
+ ( messages [ messages . length - 1 ] as AssistantMessage ) ?. toolCalls ?? [ ] ;
243
+ const toolCallBuffer =
244
+ toolCalls . length > 0 ? toolCalls [ toolCalls . length - 1 ] . function . arguments : "" ;
245
+ const toolCallName =
246
+ toolCalls . length > 0 ? toolCalls [ toolCalls . length - 1 ] . function . name : "" ;
247
+ let partialToolCallArgs = { } ;
248
+ try {
249
+ // Parse from toolCallBuffer only (before current delta is applied)
250
+ partialToolCallArgs = untruncateJson ( toolCallBuffer ) ;
251
+ } catch ( error ) { }
252
+
256
253
return subscriber . onToolCallArgsEvent ?.( {
257
254
event : event as ToolCallArgsEvent ,
258
255
messages,
259
256
state,
260
257
agent,
261
258
input,
262
- toolCallBuffer : newToolCallBuffer ,
259
+ toolCallBuffer,
263
260
toolCallName,
264
261
partialToolCallArgs,
265
262
} ) ;
@@ -268,14 +265,16 @@ export const defaultApplyEvents = (
268
265
applyMutation ( mutation ) ;
269
266
270
267
if ( mutation . stopPropagation !== true ) {
268
+ const { delta } = event as ToolCallArgsEvent ;
269
+
271
270
// Get the last message
272
271
const lastMessage = messages [ messages . length - 1 ] ;
273
272
274
273
// Get the last tool call
275
274
const lastToolCall = lastMessage . toolCalls [ lastMessage . toolCalls . length - 1 ] ;
276
275
277
- // Set the complete new arguments
278
- lastToolCall . function . arguments = newToolCallBuffer ;
276
+ // Append the arguments
277
+ lastToolCall . function . arguments += delta ;
279
278
280
279
applyMutation ( { messages } ) ;
281
280
}
0 commit comments