Skip to content

Commit 7ec1c13

Browse files
committed
dotnet: MapAGUI supports on-demand agent selection (addressing copilot comments)
1 parent 16e45f0 commit 7ec1c13

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

dotnet/samples/AGUIClientServer/AGUIDojoServer/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
var jsonOptions = app.Services.GetRequiredService<IOptions<Microsoft.AspNetCore.Http.Json.JsonOptions>>();
4141
app.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(),
@@ -52,7 +53,7 @@
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

5859
await app.RunAsync();

dotnet/samples/AGUIClientServer/AGUIServer/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using AGUIServer;
55
using Azure.AI.OpenAI;
66
using Azure.Identity;
7+
using Microsoft.Agents.AI;
78
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
89
using Microsoft.Extensions.AI;
910
using OpenAI;
@@ -48,14 +49,16 @@
4849
// Map the AG-UI agent endpoint
4950
app.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

6164
await app.RunAsync();

dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/AGUIEndpointRouteBuilderExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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(

0 commit comments

Comments
 (0)