@@ -116,6 +116,15 @@ export const defaultApplyEvents = (
116
116
}
117
117
118
118
case EventType . TEXT_MESSAGE_CONTENT : {
119
+ const { messageId, delta } = event as TextMessageContentEvent ;
120
+
121
+ // Find the target message by ID
122
+ const targetMessage = messages . find ( ( m ) => m . id === messageId ) ;
123
+ if ( ! targetMessage ) {
124
+ console . warn ( `TEXT_MESSAGE_CONTENT: No message found with ID '${ messageId } '` ) ;
125
+ return emitUpdates ( ) ;
126
+ }
127
+
119
128
const mutation = await runSubscribersWithMutation (
120
129
subscribers ,
121
130
messages ,
@@ -127,24 +136,30 @@ export const defaultApplyEvents = (
127
136
state,
128
137
agent,
129
138
input,
130
- textMessageBuffer : messages [ messages . length - 1 ] . content ?? "" ,
139
+ textMessageBuffer : targetMessage . content ?? "" ,
131
140
} ) ,
132
141
) ;
133
142
applyMutation ( mutation ) ;
134
143
135
144
if ( mutation . stopPropagation !== true ) {
136
- const { delta } = event as TextMessageContentEvent ;
137
-
138
- // Get the last message and append the content
139
- const lastMessage = messages [ messages . length - 1 ] ;
140
- lastMessage . content = lastMessage . content ! + delta ;
145
+ // Append content to the correct message by ID
146
+ targetMessage . content = ( targetMessage . content || "" ) + delta ;
141
147
applyMutation ( { messages } ) ;
142
148
}
143
149
144
150
return emitUpdates ( ) ;
145
151
}
146
152
147
153
case EventType . TEXT_MESSAGE_END : {
154
+ const { messageId } = event as TextMessageEndEvent ;
155
+
156
+ // Find the target message by ID
157
+ const targetMessage = messages . find ( ( m ) => m . id === messageId ) ;
158
+ if ( ! targetMessage ) {
159
+ console . warn ( `TEXT_MESSAGE_END: No message found with ID '${ messageId } '` ) ;
160
+ return emitUpdates ( ) ;
161
+ }
162
+
148
163
const mutation = await runSubscribersWithMutation (
149
164
subscribers ,
150
165
messages ,
@@ -156,15 +171,15 @@ export const defaultApplyEvents = (
156
171
state,
157
172
agent,
158
173
input,
159
- textMessageBuffer : messages [ messages . length - 1 ] . content ?? "" ,
174
+ textMessageBuffer : targetMessage . content ?? "" ,
160
175
} ) ,
161
176
) ;
162
177
applyMutation ( mutation ) ;
163
178
164
179
await Promise . all (
165
180
subscribers . map ( ( subscriber ) => {
166
181
subscriber . onNewMessage ?.( {
167
- message : messages [ messages . length - 1 ] ,
182
+ message : targetMessage ,
168
183
messages,
169
184
state,
170
185
agent,
@@ -233,17 +248,34 @@ export const defaultApplyEvents = (
233
248
}
234
249
235
250
case EventType . TOOL_CALL_ARGS : {
251
+ const { toolCallId, delta } = event as ToolCallArgsEvent ;
252
+
253
+ // Find the message containing this tool call
254
+ const targetMessage = messages . find ( ( m ) =>
255
+ ( m as AssistantMessage ) . toolCalls ?. some ( ( tc ) => tc . id === toolCallId ) ,
256
+ ) as AssistantMessage ;
257
+
258
+ if ( ! targetMessage ) {
259
+ console . warn (
260
+ `TOOL_CALL_ARGS: No message found containing tool call with ID '${ toolCallId } '` ,
261
+ ) ;
262
+ return emitUpdates ( ) ;
263
+ }
264
+
265
+ // Find the specific tool call
266
+ const targetToolCall = targetMessage . toolCalls ! . find ( ( tc ) => tc . id === toolCallId ) ;
267
+ if ( ! targetToolCall ) {
268
+ console . warn ( `TOOL_CALL_ARGS: No tool call found with ID '${ toolCallId } '` ) ;
269
+ return emitUpdates ( ) ;
270
+ }
271
+
236
272
const mutation = await runSubscribersWithMutation (
237
273
subscribers ,
238
274
messages ,
239
275
state ,
240
276
( 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 : "" ;
277
+ const toolCallBuffer = targetToolCall . function . arguments ;
278
+ const toolCallName = targetToolCall . function . name ;
247
279
let partialToolCallArgs = { } ;
248
280
try {
249
281
// Parse from toolCallBuffer only (before current delta is applied)
@@ -265,35 +297,43 @@ export const defaultApplyEvents = (
265
297
applyMutation ( mutation ) ;
266
298
267
299
if ( mutation . stopPropagation !== true ) {
268
- const { delta } = event as ToolCallArgsEvent ;
269
-
270
- // Get the last message
271
- const lastMessage = messages [ messages . length - 1 ] as AssistantMessage ;
272
-
273
- // Get the last tool call
274
- const lastToolCall = lastMessage . toolCalls ! [ lastMessage . toolCalls ! . length - 1 ] ;
275
-
276
- // Append the arguments
277
- lastToolCall . function . arguments += delta ;
278
-
300
+ // Append the arguments to the correct tool call by ID
301
+ targetToolCall . function . arguments += delta ;
279
302
applyMutation ( { messages } ) ;
280
303
}
281
304
282
305
return emitUpdates ( ) ;
283
306
}
284
307
285
308
case EventType . TOOL_CALL_END : {
309
+ const { toolCallId } = event as ToolCallEndEvent ;
310
+
311
+ // Find the message containing this tool call
312
+ const targetMessage = messages . find ( ( m ) =>
313
+ ( m as AssistantMessage ) . toolCalls ?. some ( ( tc ) => tc . id === toolCallId ) ,
314
+ ) as AssistantMessage ;
315
+
316
+ if ( ! targetMessage ) {
317
+ console . warn (
318
+ `TOOL_CALL_END: No message found containing tool call with ID '${ toolCallId } '` ,
319
+ ) ;
320
+ return emitUpdates ( ) ;
321
+ }
322
+
323
+ // Find the specific tool call
324
+ const targetToolCall = targetMessage . toolCalls ! . find ( ( tc ) => tc . id === toolCallId ) ;
325
+ if ( ! targetToolCall ) {
326
+ console . warn ( `TOOL_CALL_END: No tool call found with ID '${ toolCallId } '` ) ;
327
+ return emitUpdates ( ) ;
328
+ }
329
+
286
330
const mutation = await runSubscribersWithMutation (
287
331
subscribers ,
288
332
messages ,
289
333
state ,
290
334
( subscriber , messages , state ) => {
291
- const toolCalls =
292
- ( messages [ messages . length - 1 ] as AssistantMessage ) ?. toolCalls ?? [ ] ;
293
- const toolCallArgsString =
294
- toolCalls . length > 0 ? toolCalls [ toolCalls . length - 1 ] . function . arguments : "" ;
295
- const toolCallName =
296
- toolCalls . length > 0 ? toolCalls [ toolCalls . length - 1 ] . function . name : "" ;
335
+ const toolCallArgsString = targetToolCall . function . arguments ;
336
+ const toolCallName = targetToolCall . function . name ;
297
337
let toolCallArgs = { } ;
298
338
try {
299
339
toolCallArgs = JSON . parse ( toolCallArgsString ) ;
@@ -314,9 +354,7 @@ export const defaultApplyEvents = (
314
354
await Promise . all (
315
355
subscribers . map ( ( subscriber ) => {
316
356
subscriber . onNewToolCall ?.( {
317
- toolCall : ( messages [ messages . length - 1 ] as AssistantMessage ) . toolCalls ! [
318
- ( messages [ messages . length - 1 ] as AssistantMessage ) . toolCalls ! . length - 1
319
- ] ,
357
+ toolCall : targetToolCall ,
320
358
messages,
321
359
state,
322
360
agent,
0 commit comments