Skip to content

Commit 6819297

Browse files
committed
Cleanup MapAGUI API
1 parent 5d8b9ac commit 6819297

File tree

5 files changed

+28
-34
lines changed

5 files changed

+28
-34
lines changed

dotnet/samples/AGUIClientServer/AGUIServer/Program.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Azure.AI.OpenAI;
44
using Azure.Identity;
55
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
6+
using Microsoft.Extensions.AI;
67
using OpenAI;
78

89
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
@@ -12,14 +13,14 @@
1213
string endpoint = builder.Configuration["AZURE_OPENAI_ENDPOINT"] ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
1314
string deploymentName = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME is not set.");
1415

16+
// Create the AI agent
17+
var agent = new AzureOpenAIClient(
18+
new Uri(endpoint),
19+
new DefaultAzureCredential())
20+
.GetChatClient(deploymentName)
21+
.CreateAIAgent(name: "AGUIAssistant");
22+
1523
// Map the AG-UI agent endpoint
16-
app.MapAGUIAgent("/", (messages) =>
17-
{
18-
return new AzureOpenAIClient(
19-
new Uri(endpoint),
20-
new DefaultAzureCredential())
21-
.GetChatClient(deploymentName)
22-
.CreateAIAgent(name: "AGUIAssistant");
23-
});
24+
app.MapAGUI("/", agent);
2425

2526
await app.RunAsync();

dotnet/samples/AGUIClientServer/README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,16 @@ User (:q or quit to exit): :q
114114

115115
### Server Side
116116

117-
The `AGUIServer` uses the `MapAGUIAgent` extension method to expose an agent through the AG-UI protocol:
117+
The `AGUIServer` uses the `MapAGUI` extension method to expose an agent through the AG-UI protocol:
118118

119119
```csharp
120-
app.MapAGUIAgent("/", (messages, tools, context, forwardedProps) =>
121-
{
122-
AIAgent agent = new OpenAIClient(apiKey)
123-
.GetChatClient(model)
124-
.CreateAIAgent(
125-
instructions: "You are a helpful assistant.",
126-
name: "AGUIAssistant");
127-
return agent;
128-
});
120+
AIAgent agent = new OpenAIClient(apiKey)
121+
.GetChatClient(model)
122+
.CreateAIAgent(
123+
instructions: "You are a helpful assistant.",
124+
name: "AGUIAssistant");
125+
126+
app.MapAGUI("/", agent);
129127
```
130128

131129
This automatically handles:

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public static class AGUIEndpointRouteBuilderExtensions
2424
/// </summary>
2525
/// <param name="endpoints">The endpoint route builder.</param>
2626
/// <param name="pattern">The URL pattern for the endpoint.</param>
27-
/// <param name="agentFactory">Factory function to create an agent instance.</param>
27+
/// <param name="aiAgent">The agent instance.</param>
2828
/// <returns>An <see cref="IEndpointConventionBuilder"/> for the mapped endpoint.</returns>
29-
public static IEndpointConventionBuilder MapAGUIAgent(
29+
public static IEndpointConventionBuilder MapAGUI(
3030
this IEndpointRouteBuilder endpoints,
3131
string pattern,
32-
Func<IEnumerable<ChatMessage>, AIAgent> agentFactory)
32+
AIAgent aiAgent)
3333
{
3434
return endpoints.MapPost(pattern, async ([FromBody] RunAgentInput? input, HttpContext context, CancellationToken cancellationToken) =>
3535
{
@@ -39,7 +39,7 @@ public static IEndpointConventionBuilder MapAGUIAgent(
3939
}
4040

4141
var messages = input.Messages.AsChatMessages();
42-
var agent = agentFactory(messages);
42+
var agent = aiAgent;
4343

4444
var events = agent.RunStreamingAsync(
4545
messages,

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/BasicStreamingTests.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,11 @@ private async Task SetupTestServerAsync(bool useMultiMessageAgent = false)
258258

259259
this._app = builder.Build();
260260

261-
if (useMultiMessageAgent)
262-
{
263-
this._app.MapAGUIAgent("/agent", (IEnumerable<ChatMessage> messages) =>
264-
this._app.Services.GetRequiredService<FakeMultiMessageAgent>());
265-
}
266-
else
267-
{
268-
this._app.MapAGUIAgent("/agent", (IEnumerable<ChatMessage> messages) =>
269-
this._app.Services.GetRequiredService<FakeChatClientAgent>());
270-
}
261+
AIAgent agent = useMultiMessageAgent
262+
? this._app.Services.GetRequiredService<FakeMultiMessageAgent>()
263+
: this._app.Services.GetRequiredService<FakeChatClientAgent>();
264+
265+
this._app.MapAGUI("/agent", agent);
271266

272267
await this._app.StartAsync();
273268

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests/AGUIEndpointRouteBuilderExtensionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public void MapAGUIAgent_MapsEndpoint_AtSpecifiedPattern()
3535
endpointsMock.Setup(e => e.DataSources).Returns([]);
3636

3737
const string Pattern = "/api/agent";
38-
AIAgent factory(IEnumerable<ChatMessage> messages) => new TestAgent();
38+
AIAgent agent = new TestAgent();
3939

4040
// Act
41-
IEndpointConventionBuilder? result = AGUIEndpointRouteBuilderExtensions.MapAGUIAgent(endpointsMock.Object, Pattern, factory);
41+
IEndpointConventionBuilder? result = AGUIEndpointRouteBuilderExtensions.MapAGUI(endpointsMock.Object, Pattern, agent);
4242

4343
// Assert
4444
Assert.NotNull(result);

0 commit comments

Comments
 (0)