File tree Expand file tree Collapse file tree 3 files changed +15
-7
lines changed
src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore Expand file tree Collapse file tree 3 files changed +15
-7
lines changed Original file line number Diff line number Diff line change 4040var jsonOptions = app . Services . GetRequiredService < IOptions < Microsoft . AspNetCore . Http . Json . JsonOptions > > ( ) ;
4141app . MapAGUI ( "/shared_state" , ChatClientAgentFactory . CreateSharedState ( jsonOptions . Value . SerializerOptions ) ) ;
4242
43- app . MapAGUI ( "/agents/{agentId}" , async ( context ) =>
43+ // Map an AG-UI agent endpoint with per-request agent selection based on route parameter
44+ app . MapAGUI ( "/agents/{agentId}" , ( context ) =>
4445{
45- var agentId = context . Request . RouteValues [ "agentId" ] ? . ToString ( ) ?? string . Empty ;
46- return agentId switch
46+ string agentId = context . Request . RouteValues [ "agentId" ] ? . ToString ( ) ?? string . Empty ;
47+ return ValueTask . FromResult ( agentId switch
4748 {
4849 "agentic_chat" => ChatClientAgentFactory . CreateAgenticChat ( ) ,
4950 "backend_tool_rendering" => ChatClientAgentFactory . CreateBackendToolRendering ( ) ,
5253 "agentic_generative_ui" => ChatClientAgentFactory . CreateAgenticUI ( ) ,
5354 "shared_state" => ChatClientAgentFactory . CreateSharedState ( jsonOptions . Value . SerializerOptions ) ,
5455 _ => throw new ArgumentException ( $ "Unknown agent ID: { agentId } ") ,
55- } ;
56+ } ) ;
5657} ) ;
5758
5859await app . RunAsync ( ) ;
Original file line number Diff line number Diff line change 44using AGUIServer ;
55using Azure . AI . OpenAI ;
66using Azure . Identity ;
7+ using Microsoft . Agents . AI ;
78using Microsoft . Agents . AI . Hosting . AGUI . AspNetCore ;
89using Microsoft . Extensions . AI ;
910using OpenAI ;
4849// Map the AG-UI agent endpoint
4950app . MapAGUI ( "/" , agent ) ;
5051
51- app . MapAGUI ( "/agents/{agentId}" , async ( context ) =>
52+ // Map an AG-UI agent endpoint with per-request agent selection based on route parameter
53+ app . MapAGUI ( "/agents/{agentId}" , ( context ) =>
5254{
53- var agentId = context . Request . RouteValues [ "agentId" ] ? . ToString ( ) ?? string . Empty ;
54- return agentId switch
55+ string agentId = context . Request . RouteValues [ "agentId" ] ? . ToString ( ) ?? string . Empty ;
56+ AIAgent selectedAgent = agentId switch
5557 {
5658 "0" => agent ,
5759 _ => throw new ArgumentException ( $ "Unknown agent ID: { agentId } ") ,
5860 } ;
61+ return ValueTask . FromResult ( selectedAgent ) ;
5962} ) ;
6063
6164await app . RunAsync ( ) ;
Original file line number Diff line number Diff line change @@ -82,6 +82,10 @@ public static IEndpointConventionBuilder MapAGUI(
8282
8383 // Determine the agent to use
8484 var aiAgent = await aiAgentSelector ( context ) . ConfigureAwait ( false ) ;
85+ if ( aiAgent is null )
86+ {
87+ return Results . BadRequest ( "Agent could not be determined." ) ;
88+ }
8589
8690 // Run the agent and convert to AG-UI events
8791 var events = aiAgent . RunStreamingAsync (
You can’t perform that action at this time.
0 commit comments