@@ -287,52 +287,119 @@ export abstract class AbstractAgent {
287
287
// Add message to the messages array
288
288
this . messages . push ( message ) ;
289
289
290
- // Create minimal input for subscriber context
291
- const input : RunAgentInput = {
292
- threadId : this . threadId ,
293
- runId : uuidv4 ( ) ,
294
- tools : [ ] ,
295
- context : [ ] ,
296
- forwardedProps : { } ,
297
- state : structuredClone_ ( this . state ) ,
298
- messages : structuredClone_ ( this . messages ) ,
299
- } ;
290
+ // Notify subscribers sequentially in the background
291
+ ( async ( ) => {
292
+ // Fire onNewMessage sequentially
293
+ for ( const subscriber of this . subscribers ) {
294
+ await subscriber . onNewMessage ?.( {
295
+ message,
296
+ messages : this . messages ,
297
+ state : this . state ,
298
+ agent : this ,
299
+ } ) ;
300
+ }
301
+
302
+ // Fire onNewToolCall if the message is from assistant and contains tool calls
303
+ if ( message . role === "assistant" && message . toolCalls ) {
304
+ for ( const toolCall of message . toolCalls ) {
305
+ for ( const subscriber of this . subscribers ) {
306
+ await subscriber . onNewToolCall ?.( {
307
+ toolCall,
308
+ messages : this . messages ,
309
+ state : this . state ,
310
+ agent : this ,
311
+ } ) ;
312
+ }
313
+ }
314
+ }
300
315
301
- // Fire onMessagesChanged
302
- this . subscribers . forEach ( ( subscriber ) => {
303
- subscriber . onMessagesChanged ?.( {
304
- messages : this . messages ,
305
- state : this . state ,
306
- agent : this ,
307
- input,
308
- } ) ;
309
- } ) ;
310
-
311
- // Fire onNewMessage
312
- this . subscribers . forEach ( ( subscriber ) => {
313
- subscriber . onNewMessage ?.( {
314
- message,
315
- messages : this . messages ,
316
- state : this . state ,
317
- agent : this ,
318
- input,
319
- } ) ;
320
- } ) ;
321
-
322
- // Fire onNewToolCall if the message is from assistant and contains tool calls
323
- if ( message . role === "assistant" && message . toolCalls ) {
324
- message . toolCalls . forEach ( ( toolCall : ToolCall ) => {
325
- this . subscribers . forEach ( ( subscriber ) => {
326
- subscriber . onNewToolCall ?.( {
327
- toolCall,
316
+ // Fire onMessagesChanged sequentially
317
+ for ( const subscriber of this . subscribers ) {
318
+ await subscriber . onMessagesChanged ?.( {
319
+ messages : this . messages ,
320
+ state : this . state ,
321
+ agent : this ,
322
+ } ) ;
323
+ }
324
+ } ) ( ) ;
325
+ }
326
+
327
+ public addMessages ( messages : Message [ ] ) {
328
+ // Add all messages to the messages array
329
+ this . messages . push ( ...messages ) ;
330
+
331
+ // Notify subscribers sequentially in the background
332
+ ( async ( ) => {
333
+ // Fire onNewMessage and onNewToolCall for each message sequentially
334
+ for ( const message of messages ) {
335
+ // Fire onNewMessage sequentially
336
+ for ( const subscriber of this . subscribers ) {
337
+ await subscriber . onNewMessage ?.( {
338
+ message,
328
339
messages : this . messages ,
329
340
state : this . state ,
330
341
agent : this ,
331
- input,
332
342
} ) ;
343
+ }
344
+
345
+ // Fire onNewToolCall if the message is from assistant and contains tool calls
346
+ if ( message . role === "assistant" && message . toolCalls ) {
347
+ for ( const toolCall of message . toolCalls ) {
348
+ for ( const subscriber of this . subscribers ) {
349
+ await subscriber . onNewToolCall ?.( {
350
+ toolCall,
351
+ messages : this . messages ,
352
+ state : this . state ,
353
+ agent : this ,
354
+ } ) ;
355
+ }
356
+ }
357
+ }
358
+ }
359
+
360
+ // Fire onMessagesChanged once at the end sequentially
361
+ for ( const subscriber of this . subscribers ) {
362
+ await subscriber . onMessagesChanged ?.( {
363
+ messages : this . messages ,
364
+ state : this . state ,
365
+ agent : this ,
333
366
} ) ;
334
- } ) ;
335
- }
367
+ }
368
+ } ) ( ) ;
369
+ }
370
+
371
+ public setMessages ( messages : Message [ ] ) {
372
+ // Replace the entire messages array
373
+ this . messages = structuredClone_ ( messages ) ;
374
+
375
+ // Notify subscribers sequentially in the background
376
+ ( async ( ) => {
377
+ // Fire onMessagesChanged sequentially
378
+ for ( const subscriber of this . subscribers ) {
379
+ await subscriber . onMessagesChanged ?.( {
380
+ messages : this . messages ,
381
+ state : this . state ,
382
+ agent : this ,
383
+ } ) ;
384
+ }
385
+ } ) ( ) ;
386
+ }
387
+
388
+ public setState ( state : State ) {
389
+ // Replace the entire state
390
+ this . state = structuredClone_ ( state ) ;
391
+
392
+ // Notify subscribers sequentially in the background
393
+ ( async ( ) => {
394
+ // Fire onStateChanged sequentially
395
+ for ( const subscriber of this . subscribers ) {
396
+ await subscriber . onStateChanged ?.( {
397
+ messages : this . messages ,
398
+ state : this . state ,
399
+ agent : this ,
400
+ } ) ;
401
+ }
402
+ } ) ( ) ;
336
403
}
337
404
338
405
public legacy_to_be_removed_runAgentBridged (
0 commit comments