-
Notifications
You must be signed in to change notification settings - Fork 1.1k
.NET: assign AgentCard's URL to mapped-endpoint if not defined explicitly #2047
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7d9bdcb
fix serialization in chat completions on tools
DeagleGross fa68b4d
nit
DeagleGross 28818ff
write e2e test for agent card resolve + adjust behavior
DeagleGross 14ec29a
nit
DeagleGross 0ec8d54
Merge branch 'main' into dmkorolev/a2acard-fix
DeagleGross 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
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
87 changes: 87 additions & 0 deletions
87
dotnet/tests/Microsoft.Agents.AI.Hosting.A2A.UnitTests/A2AIntegrationTests.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,87 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using A2A; | ||
| using Microsoft.Agents.AI.Hosting.A2A.UnitTests.Internal; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting.Server; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Microsoft.Agents.AI.Hosting.A2A.UnitTests; | ||
|
|
||
| public sealed class A2AIntegrationTests | ||
| { | ||
| /// <summary> | ||
| /// Verifies that calling the A2A card endpoint with MapA2A returns an agent card with a URL populated. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task MapA2A_WithAgentCard_CardEndpointReturnsCardWithUrlAsync() | ||
| { | ||
| // Arrange | ||
| WebApplicationBuilder builder = WebApplication.CreateBuilder(); | ||
| builder.WebHost.UseTestServer(); | ||
|
|
||
| IChatClient mockChatClient = new DummyChatClient(); | ||
| builder.Services.AddKeyedSingleton("chat-client", mockChatClient); | ||
| IHostedAgentBuilder agentBuilder = builder.AddAIAgent("test-agent", "Test instructions", chatClientServiceKey: "chat-client"); | ||
| builder.Services.AddLogging(); | ||
|
|
||
| using WebApplication app = builder.Build(); | ||
|
|
||
| var agentCard = new AgentCard | ||
| { | ||
| Name = "Test Agent", | ||
| Description = "A test agent for A2A communication", | ||
| Version = "1.0" | ||
| }; | ||
|
|
||
| // Map A2A with the agent card | ||
| app.MapA2A(agentBuilder, "/a2a/test-agent", agentCard); | ||
|
|
||
| await app.StartAsync(); | ||
|
|
||
| try | ||
| { | ||
| // Get the test server client | ||
| TestServer testServer = app.Services.GetRequiredService<IServer>() as TestServer | ||
| ?? throw new InvalidOperationException("TestServer not found"); | ||
| var httpClient = testServer.CreateClient(); | ||
|
|
||
| // Act - Query the agent card endpoint | ||
| var requestUri = new Uri("/a2a/test-agent/v1/card", UriKind.Relative); | ||
| var response = await httpClient.GetAsync(requestUri); | ||
|
|
||
| // Assert | ||
| Assert.True(response.IsSuccessStatusCode, $"Expected successful response but got {response.StatusCode}"); | ||
|
|
||
| var content = await response.Content.ReadAsStringAsync(); | ||
| var jsonDoc = JsonDocument.Parse(content); | ||
| var root = jsonDoc.RootElement; | ||
|
|
||
| // Verify the card has expected properties | ||
| Assert.True(root.TryGetProperty("name", out var nameProperty)); | ||
| Assert.Equal("Test Agent", nameProperty.GetString()); | ||
|
|
||
| Assert.True(root.TryGetProperty("description", out var descProperty)); | ||
| Assert.Equal("A test agent for A2A communication", descProperty.GetString()); | ||
|
|
||
| // Verify the card has a URL property and it's not null/empty | ||
| Assert.True(root.TryGetProperty("url", out var urlProperty)); | ||
| Assert.NotEqual(JsonValueKind.Null, urlProperty.ValueKind); | ||
|
|
||
| var url = urlProperty.GetString(); | ||
| Assert.NotNull(url); | ||
| Assert.NotEmpty(url); | ||
| Assert.StartsWith("http", url, StringComparison.OrdinalIgnoreCase); | ||
| Assert.Equal($"{testServer.BaseAddress.ToString().TrimEnd('/')}/a2a/test-agent/v1/card", url); | ||
| } | ||
| finally | ||
| { | ||
| await app.StopAsync(); | ||
| } | ||
| } | ||
| } |
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
30 changes: 30 additions & 0 deletions
30
dotnet/tests/Microsoft.Agents.AI.Hosting.A2A.UnitTests/Internal/DummyChatClient.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,30 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace Microsoft.Agents.AI.Hosting.A2A.UnitTests.Internal; | ||
|
|
||
| internal sealed class DummyChatClient : IChatClient | ||
| { | ||
| public void Dispose() | ||
| { | ||
| throw new NotImplementedException(); | ||
DeagleGross marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public object? GetService(Type serviceType, object? serviceKey = null) => | ||
| serviceType.IsInstanceOfType(this) ? this : null; | ||
|
|
||
| public IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
5 changes: 4 additions & 1 deletion
5
...icrosoft.Agents.AI.Hosting.A2A.UnitTests/Microsoft.Agents.AI.Hosting.A2A.UnitTests.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
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.