-
Notifications
You must be signed in to change notification settings - Fork 2.1k
.NET: Support dynamic agent resolution in AG-UI endpoints #3162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheEagleByte
wants to merge
7
commits into
microsoft:main
Choose a base branch
from
TheEagleByte:feature/dynamic-agent-resolution-2988
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cdb15aa
.NET: Add dynamic agent resolution support for AG-UI endpoints (#2988)
TheEagleByte 8529ad5
Address PR review feedback
TheEagleByte e2c235b
Merge branch 'main' into feature/dynamic-agent-resolution-2988
TheEagleByte b7193a1
Merge branch 'main' into feature/dynamic-agent-resolution-2988
TheEagleByte b4c5b1e
Merge branch 'main' into feature/dynamic-agent-resolution-2988
TheEagleByte 5b71fbf
Merge branch 'main' into feature/dynamic-agent-resolution-2988
TheEagleByte 7238772
Merge branch 'main' into feature/dynamic-agent-resolution-2988
TheEagleByte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
dotnet/samples/GettingStarted/AGUI/Step06_DynamicAgentResolution/Client/Client.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net10.0</TargetFrameworks> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI\Microsoft.Agents.AI.csproj" /> | ||
| <ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.AGUI\Microsoft.Agents.AI.AGUI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
93 changes: 93 additions & 0 deletions
93
dotnet/samples/GettingStarted/AGUI/Step06_DynamicAgentResolution/Client/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Microsoft.Agents.AI; | ||
| using Microsoft.Agents.AI.AGUI; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| string serverUrl = Environment.GetEnvironmentVariable("AGUI_SERVER_URL") ?? "http://localhost:8888"; | ||
|
|
||
| Console.WriteLine("AG-UI Dynamic Agent Resolution Demo"); | ||
| Console.WriteLine("====================================\n"); | ||
| Console.WriteLine("Available agents: general, code, writer"); | ||
| Console.WriteLine($"Server URL: {serverUrl}\n"); | ||
|
|
||
| using HttpClient httpClient = new() | ||
| { | ||
| Timeout = TimeSpan.FromSeconds(60) | ||
| }; | ||
|
|
||
| while (true) | ||
| { | ||
| // Get agent selection | ||
| Console.Write("Select agent (general/code/writer) or 'quit': "); | ||
| string? agentId = Console.ReadLine()?.Trim(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(agentId) || agentId.Equals("quit", StringComparison.OrdinalIgnoreCase) || agentId == ":q") | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| // Create client for specific agent | ||
| string agentUrl = $"{serverUrl.TrimEnd('/')}/agents/{agentId}"; | ||
| AGUIChatClient chatClient = new(httpClient, agentUrl); | ||
|
|
||
| AIAgent agent = chatClient.CreateAIAgent( | ||
| name: $"agui-client-{agentId}", | ||
| description: $"AG-UI Client for {agentId} agent"); | ||
|
|
||
| AgentThread thread = agent.GetNewThread(); | ||
| List<ChatMessage> messages = []; | ||
|
|
||
| Console.WriteLine($"\nConnected to '{agentId}' agent. Type ':back' to switch agents.\n"); | ||
|
|
||
| // Conversation loop for this agent | ||
| while (true) | ||
| { | ||
| Console.Write($"[{agentId}] User: "); | ||
| string? message = Console.ReadLine(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(message)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (message == ":back") | ||
| { | ||
| Console.WriteLine(); | ||
| break; | ||
| } | ||
|
|
||
| if (message == ":q" || message == "quit") | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| messages.Add(new ChatMessage(ChatRole.User, message)); | ||
|
|
||
| // Stream the response | ||
| Console.Write($"[{agentId}] Assistant: "); | ||
|
|
||
| try | ||
| { | ||
| await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync(messages, thread)) | ||
| { | ||
| foreach (AIContent content in update.Contents) | ||
| { | ||
| if (content is TextContent textContent) | ||
| { | ||
| Console.Write(textContent.Text); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Console.WriteLine("\n"); | ||
| } | ||
| catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) | ||
| { | ||
| Console.ForegroundColor = ConsoleColor.Red; | ||
| Console.WriteLine($"\nAgent '{agentId}' not found. Please try a different agent.\n"); | ||
| Console.ResetColor(); | ||
| break; | ||
| } | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
dotnet/samples/GettingStarted/AGUI/Step06_DynamicAgentResolution/Server/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Azure.AI.OpenAI; | ||
| using Azure.Identity; | ||
| using Microsoft.Agents.AI; | ||
| using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; | ||
| using Microsoft.Extensions.AI; | ||
| using OpenAI.Chat; | ||
|
|
||
| WebApplicationBuilder builder = WebApplication.CreateBuilder(args); | ||
| builder.Services.AddHttpClient().AddLogging(); | ||
| builder.Services.AddAGUI(); | ||
|
|
||
| WebApplication app = builder.Build(); | ||
|
|
||
| string endpoint = builder.Configuration["AZURE_OPENAI_ENDPOINT"] | ||
| ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); | ||
| string deploymentName = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] | ||
| ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME is not set."); | ||
|
TheEagleByte marked this conversation as resolved.
|
||
|
|
||
| // Create chat client (shared across agents) | ||
| ChatClient chatClient = new AzureOpenAIClient( | ||
| new Uri(endpoint), | ||
| new DefaultAzureCredential()) | ||
| .GetChatClient(deploymentName); | ||
|
|
||
| // Define available agent configurations | ||
| Dictionary<string, (string Name, string Instructions)> agentConfigs = new(StringComparer.OrdinalIgnoreCase) | ||
| { | ||
| ["general"] = ("GeneralAssistant", "You are a helpful general assistant."), | ||
| ["code"] = ("CodeAssistant", "You are an expert programmer. Help users write, debug, and optimize code."), | ||
| ["writer"] = ("WriterAssistant", "You are a professional writer. Help users with writing, editing, and creative content.") | ||
| }; | ||
|
TheEagleByte marked this conversation as resolved.
|
||
|
|
||
| // Map AG-UI endpoint with dynamic agent resolution | ||
| app.MapAGUI("/agents/{agentId}", async (HttpContext context, CancellationToken cancellationToken) => | ||
| { | ||
| // Extract agent ID from route | ||
| string? agentId = context.GetRouteValue("agentId")?.ToString(); | ||
|
|
||
| if (string.IsNullOrEmpty(agentId)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Look up agent configuration | ||
| if (!agentConfigs.TryGetValue(agentId, out (string Name, string Instructions) config)) | ||
| { | ||
| return null; // Returns 404 | ||
| } | ||
|
|
||
| // Create and return agent (could also cache this) | ||
| IChatClient client = chatClient.AsIChatClient(); | ||
| return client.CreateAIAgent( | ||
| name: config.Name, | ||
| instructions: config.Instructions); | ||
| }); | ||
|
|
||
| // Also map a root endpoint for discovery | ||
| app.MapGet("/", () => Results.Json(new | ||
| { | ||
| availableAgents = agentConfigs.Keys.ToArray(), | ||
| usage = "POST to /agents/{agentId} with AG-UI protocol" | ||
| })); | ||
|
|
||
| await app.RunAsync(); | ||
23 changes: 23 additions & 0 deletions
23
...s/GettingStarted/AGUI/Step06_DynamicAgentResolution/Server/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:8888", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7047;http://localhost:8888", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
dotnet/samples/GettingStarted/AGUI/Step06_DynamicAgentResolution/Server/Server.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net10.0</TargetFrameworks> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Azure.AI.OpenAI" /> | ||
| <PackageReference Include="Azure.Identity" /> | ||
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Hosting.AGUI.AspNetCore\Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.csproj" /> | ||
| <ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
8 changes: 8 additions & 0 deletions
8
...les/GettingStarted/AGUI/Step06_DynamicAgentResolution/Server/appsettings.Development.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
dotnet/samples/GettingStarted/AGUI/Step06_DynamicAgentResolution/Server/appsettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.