@@ -51,59 +51,31 @@ protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowC
5151
5252 protected override async ValueTask TakeTurnAsync ( List < ChatMessage > messages , IWorkflowContext context , bool ? emitEvents , CancellationToken cancellationToken = default )
5353 {
54- emitEvents ??= this . _emitEvents ;
55- IAsyncEnumerable < AgentRunResponseUpdate > agentStream = this . _agent . RunStreamingAsync ( messages , this . EnsureThread ( context ) , cancellationToken : cancellationToken ) ;
56-
57- List < AIContent > updates = [ ] ;
58- ChatMessage ? currentStreamingMessage = null ;
59-
60- await foreach ( AgentRunResponseUpdate update in agentStream . ConfigureAwait ( false ) )
54+ if ( emitEvents ?? this . _emitEvents )
6155 {
62- if ( string . IsNullOrEmpty ( update . MessageId ) )
63- {
64- // Ignore updates that don't have a message ID.
65- continue ;
66- }
56+ // Run the agent in streaming mode only when agent run update events are to be emitted.
57+ IAsyncEnumerable < AgentRunResponseUpdate > agentStream = this . _agent . RunStreamingAsync ( messages , this . EnsureThread ( context ) , cancellationToken : cancellationToken ) ;
6758
68- if ( emitEvents ?? this . _emitEvents )
59+ List < AgentRunResponseUpdate > updates = [ ] ;
60+
61+ await foreach ( AgentRunResponseUpdate update in agentStream . ConfigureAwait ( false ) )
6962 {
7063 await context . AddEventAsync ( new AgentRunUpdateEvent ( this . Id , update ) , cancellationToken ) . ConfigureAwait ( false ) ;
71- }
72-
73- // TODO: FunctionCall request handling, and user info request handling.
74- // In some sense: We should just let it be handled as a ChatMessage, though we should consider
75- // providing some mechanisms to help the user complete the request, or route it out of the
76- // workflow.
7764
78- if ( currentStreamingMessage is null || currentStreamingMessage . MessageId != update . MessageId )
79- {
80- await PublishCurrentMessageAsync ( ) . ConfigureAwait ( false ) ;
81- currentStreamingMessage = new ( update . Role ?? ChatRole . Assistant , update . Contents )
82- {
83- AuthorName = update . AuthorName ,
84- CreatedAt = update . CreatedAt ,
85- MessageId = update . MessageId ,
86- RawRepresentation = update . RawRepresentation ,
87- AdditionalProperties = update . AdditionalProperties
88- } ;
65+ // TODO: FunctionCall request handling, and user info request handling.
66+ // In some sense: We should just let it be handled as a ChatMessage, though we should consider
67+ // providing some mechanisms to help the user complete the request, or route it out of the
68+ // workflow.
69+ updates . Add ( update ) ;
8970 }
9071
91- updates . AddRange ( update . Contents ) ;
72+ await context . SendMessageAsync ( updates . ToAgentRunResponse ( ) . Messages , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
9273 }
93-
94- await PublishCurrentMessageAsync ( ) . ConfigureAwait ( false ) ;
95-
96- async ValueTask PublishCurrentMessageAsync ( )
74+ else
9775 {
98- if ( currentStreamingMessage is not null && updates . Count > 0 )
99- {
100- currentStreamingMessage . Contents = updates ;
101- updates = [ ] ;
102-
103- await context . SendMessageAsync ( currentStreamingMessage , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
104- }
105-
106- currentStreamingMessage = null ;
76+ // Otherwise, run the agent in non-streaming mode.
77+ AgentRunResponse response = await this . _agent . RunAsync ( messages , this . EnsureThread ( context ) , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
78+ await context . SendMessageAsync ( response . Messages , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
10779 }
10880 }
10981}
0 commit comments