@@ -121,51 +121,7 @@ public static IEndpointConventionBuilder MapAGUI(
121121 return Results . BadRequest ( ) ;
122122 }
123123
124- var jsonOptions = context . RequestServices . GetRequiredService < IOptions < Microsoft . AspNetCore . Http . Json . JsonOptions > > ( ) ;
125- var jsonSerializerOptions = jsonOptions . Value . SerializerOptions ;
126-
127- var messages = input . Messages . AsChatMessages ( jsonSerializerOptions ) ;
128- var clientTools = input . Tools ? . AsAITools ( ) . ToList ( ) ;
129-
130- // Create run options with AG-UI context in AdditionalProperties
131- var runOptions = new ChatClientAgentRunOptions
132- {
133- ChatOptions = new ChatOptions
134- {
135- Tools = clientTools ,
136- AdditionalProperties = new AdditionalPropertiesDictionary
137- {
138- [ "ag_ui_state" ] = input . State ,
139- [ "ag_ui_context" ] = input . Context ? . Select ( c => new KeyValuePair < string , string > ( c . Description , c . Value ) ) . ToArray ( ) ,
140- [ "ag_ui_forwarded_properties" ] = input . ForwardedProperties ,
141- [ "ag_ui_thread_id" ] = input . ThreadId ,
142- [ "ag_ui_run_id" ] = input . RunId
143- }
144- }
145- } ;
146-
147- var threadId = string . IsNullOrWhiteSpace ( input . ThreadId ) ? Guid . NewGuid ( ) . ToString ( "N" ) : input . ThreadId ;
148- var session = await hostAgent . GetOrCreateSessionAsync ( threadId , cancellationToken ) . ConfigureAwait ( false ) ;
149-
150- // Run the agent and convert to AG-UI events
151- var events = hostAgent . RunStreamingAsync (
152- messages ,
153- session : session ,
154- options : runOptions ,
155- cancellationToken : cancellationToken )
156- . AsChatResponseUpdatesAsync ( )
157- . FilterServerToolsFromMixedToolInvocationsAsync ( clientTools , cancellationToken )
158- . AsAGUIEventStreamAsync (
159- threadId ,
160- input . RunId ,
161- jsonSerializerOptions ,
162- cancellationToken ) ;
163-
164- // Wrap the event stream to save the session after streaming completes
165- var eventsWithSessionSave = SaveSessionAfterStreamingAsync ( events , hostAgent , threadId , session , cancellationToken ) ;
166-
167- var sseLogger = context . RequestServices . GetRequiredService < ILogger < AGUIServerSentEventsResult > > ( ) ;
168- return new AGUIServerSentEventsResult ( eventsWithSessionSave , sseLogger ) ;
124+ return await ExecuteAgentRequestAsync ( hostAgent , input , context , cancellationToken ) . ConfigureAwait ( false ) ;
169125 } ) ;
170126 }
171127
@@ -230,52 +186,65 @@ public static IEndpointConventionBuilder MapAGUI(
230186
231187 var hostAgent = new AIHostAgent ( aiAgent , agentSessionStore ) ;
232188
233- var jsonOptions = context . RequestServices . GetRequiredService < IOptions < Microsoft . AspNetCore . Http . Json . JsonOptions > > ( ) ;
234- var jsonSerializerOptions = jsonOptions . Value . SerializerOptions ;
189+ return await ExecuteAgentRequestAsync ( hostAgent , input , context , cancellationToken ) . ConfigureAwait ( false ) ;
190+ } ) ;
191+ }
235192
236- var messages = input . Messages . AsChatMessages ( jsonSerializerOptions ) ;
237- var clientTools = input . Tools ? . AsAITools ( ) . ToList ( ) ;
193+ /// <summary>
194+ /// Shared execution pipeline for AG-UI agent requests. Converts the input to chat messages,
195+ /// runs the agent, and returns an SSE result with session persistence.
196+ /// </summary>
197+ private static async Task < IResult > ExecuteAgentRequestAsync (
198+ AIHostAgent hostAgent ,
199+ RunAgentInput input ,
200+ HttpContext context ,
201+ CancellationToken cancellationToken )
202+ {
203+ var jsonOptions = context . RequestServices . GetRequiredService < IOptions < Microsoft . AspNetCore . Http . Json . JsonOptions > > ( ) ;
204+ var jsonSerializerOptions = jsonOptions . Value . SerializerOptions ;
238205
239- // Create run options with AG-UI context in AdditionalProperties
240- var runOptions = new ChatClientAgentRunOptions
206+ var messages = input . Messages . AsChatMessages ( jsonSerializerOptions ) ;
207+ var clientTools = input . Tools ? . AsAITools ( ) . ToList ( ) ;
208+
209+ // Create run options with AG-UI context in AdditionalProperties
210+ var runOptions = new ChatClientAgentRunOptions
211+ {
212+ ChatOptions = new ChatOptions
241213 {
242- ChatOptions = new ChatOptions
214+ Tools = clientTools ,
215+ AdditionalProperties = new AdditionalPropertiesDictionary
243216 {
244- Tools = clientTools ,
245- AdditionalProperties = new AdditionalPropertiesDictionary
246- {
247- [ "ag_ui_state" ] = input . State ,
248- [ "ag_ui_context" ] = input . Context ? . Select ( c => new KeyValuePair < string , string > ( c . Description , c . Value ) ) . ToArray ( ) ,
249- [ "ag_ui_forwarded_properties" ] = input . ForwardedProperties ,
250- [ "ag_ui_thread_id" ] = input . ThreadId ,
251- [ "ag_ui_run_id" ] = input . RunId
252- }
217+ [ "ag_ui_state" ] = input . State ,
218+ [ "ag_ui_context" ] = input . Context ? . Select ( c => new KeyValuePair < string , string > ( c . Description , c . Value ) ) . ToArray ( ) ,
219+ [ "ag_ui_forwarded_properties" ] = input . ForwardedProperties ,
220+ [ "ag_ui_thread_id" ] = input . ThreadId ,
221+ [ "ag_ui_run_id" ] = input . RunId
253222 }
254- } ;
255-
256- var threadId = string . IsNullOrWhiteSpace ( input . ThreadId ) ? Guid . NewGuid ( ) . ToString ( "N" ) : input . ThreadId ;
257- var session = await hostAgent . GetOrCreateSessionAsync ( threadId , cancellationToken ) . ConfigureAwait ( false ) ;
258-
259- // Run the agent and convert to AG-UI events
260- var events = hostAgent . RunStreamingAsync (
261- messages ,
262- session : session ,
263- options : runOptions ,
264- cancellationToken : cancellationToken )
265- . AsChatResponseUpdatesAsync ( )
266- . FilterServerToolsFromMixedToolInvocationsAsync ( clientTools , cancellationToken )
267- . AsAGUIEventStreamAsync (
268- threadId ,
269- input . RunId ,
270- jsonSerializerOptions ,
271- cancellationToken ) ;
272-
273- // Wrap the event stream to save the session after streaming completes
274- var eventsWithSessionSave = SaveSessionAfterStreamingAsync ( events , hostAgent , threadId , session , cancellationToken ) ;
275-
276- var sseLogger = context . RequestServices . GetRequiredService < ILogger < AGUIServerSentEventsResult > > ( ) ;
277- return new AGUIServerSentEventsResult ( eventsWithSessionSave , sseLogger ) ;
278- } ) ;
223+ }
224+ } ;
225+
226+ var threadId = string . IsNullOrWhiteSpace ( input . ThreadId ) ? Guid . NewGuid ( ) . ToString ( "N" ) : input . ThreadId ;
227+ var session = await hostAgent . GetOrCreateSessionAsync ( threadId , cancellationToken ) . ConfigureAwait ( false ) ;
228+
229+ // Run the agent and convert to AG-UI events
230+ var events = hostAgent . RunStreamingAsync (
231+ messages ,
232+ session : session ,
233+ options : runOptions ,
234+ cancellationToken : cancellationToken )
235+ . AsChatResponseUpdatesAsync ( )
236+ . FilterServerToolsFromMixedToolInvocationsAsync ( clientTools , cancellationToken )
237+ . AsAGUIEventStreamAsync (
238+ threadId ,
239+ input . RunId ,
240+ jsonSerializerOptions ,
241+ cancellationToken ) ;
242+
243+ // Wrap the event stream to save the session after streaming completes
244+ var eventsWithSessionSave = SaveSessionAfterStreamingAsync ( events , hostAgent , threadId , session , cancellationToken ) ;
245+
246+ var sseLogger = context . RequestServices . GetRequiredService < ILogger < AGUIServerSentEventsResult > > ( ) ;
247+ return new AGUIServerSentEventsResult ( eventsWithSessionSave , sseLogger ) ;
279248 }
280249
281250 private static async IAsyncEnumerable < BaseEvent > SaveSessionAfterStreamingAsync (
0 commit comments