mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
66e02c10e3
* update a2a agent to the latest a2a sdk (#5257) * Move A2A samples from 04-hosting to 02-agents (#5267) Move the A2A sample projects (A2AAgent_AsFunctionTools and A2AAgent_PollingForTaskCompletion) from samples/04-hosting/A2A/ to samples/02-agents/A2A/ to better align with the sample directory structure. Update solution file and samples README accordingly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * .NET: Fix stream reconnection for A2AAgent (#5275) * Add SSE stream reconnection support to A2AAgent Implement automatic reconnection for SSE streams that disconnect mid-task, using the Last-Event-ID header to resume from where the stream left off. Changes: - Add InvokeStreamingWithReconnectAsync method to A2AAgent with configurable max retries and delay between attempts - Add new log messages for reconnection events - Add A2AAgent_StreamReconnection sample demonstrating the feature - Update existing polling sample to use simplified SendMessageAsync API - Add unit tests for stream reconnection logic Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * address comments * Address PR review feedback - Dispose SSE enumerator before GetTaskAsync fallback to release HTTP connection - Wrap StreamWriter in using blocks with leaveOpen:true and explicit UTF-8 encoding - Print update.Text instead of update object in stream reconnection sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * .NET: Use IA2AClientFactory to create A2AClient (#5277) * Refactor A2A extensions to use IA2AClientFactory and add ProtocolSelection sample - Update A2AAgentCardExtensions to accept IA2AClientFactory instead of A2AClientOptions - Update A2ACardResolverExtensions to accept IA2AClientFactory - Update A2AClientExtensions to accept IA2AClientFactory - Update A2AAgent to use IA2AClientFactory for client creation - Add A2AAgent_ProtocolSelection sample demonstrating protocol selection - Add comprehensive unit tests for all changes - Update README files with new sample reference Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Reorder params: options before loggerFactory in A2A extensions Move A2AClientOptions parameter before ILoggerFactory in AsAIAgent and GetAIAgentAsync extension methods to follow the repo convention of keeping LoggerFactory and CancellationToken as the last parameters. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * .NET: Migrate A2A hosting to A2A SDK v1 (#5363) * .NET: Migrate A2A hosting to A2A SDK v1 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * remove unused agent card --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * .NET: Split A2A endpoint mapping into protocol-specific methods (#5413) * .NET: Refactor A2A hosting registration into A2AServerServiceCollectionExtensions - Rename A2AHostingOptions to A2AServerRegistrationOptions - Move server registration logic from A2AEndpointRouteBuilderExtensions and AIAgentExtensions into new A2AServerServiceCollectionExtensions - Remove A2AProtocolBinding and AIAgentExtensions (consolidated) - Update samples and tests to use the new registration API Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * address copilot comments --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Remove unnecessary using directive in AgentWebChat.AgentHost Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * restore AsyncEnumerable package version * address copilot initial feedback * address automated code review and formatting issues * fix formatting issues * Add DI wiring verification tests for AddA2AServer Add three tests to A2AServerServiceCollectionExtensionsTests that verify custom keyed services are actually wired through to the A2AServer, not just that the server resolves non-null: - Custom IAgentHandler: verifies the keyed handler is invoked when processing a SendMessageRequest instead of the default A2AAgentHandler. - Custom AgentSessionStore (no handler): verifies the keyed session store's GetSessionAsync is called during request processing when no custom handler is registered. - Default stores end-to-end: verifies the InMemoryAgentSessionStore and InMemoryTaskStore defaults successfully process a request. Uses a new CreateAgentMockForRequests helper that includes SerializeSessionCoreAsync setup needed by InMemoryAgentSessionStore. All tests call A2AServer.SendMessageAsync directly (no HTTP layer needed) and use CancellationToken timeouts to guard against hangs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
167 lines
5.7 KiB
C#
167 lines
5.7 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using A2A;
|
|
|
|
namespace Microsoft.Agents.AI.A2A.UnitTests;
|
|
|
|
/// <summary>
|
|
/// Unit tests for the <see cref="A2ACardResolverExtensions"/> class.
|
|
/// </summary>
|
|
public sealed class A2ACardResolverExtensionsTests : IDisposable
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly HttpMessageHandlerStub _handler;
|
|
private readonly A2ACardResolver _resolver;
|
|
|
|
public A2ACardResolverExtensionsTests()
|
|
{
|
|
this._handler = new HttpMessageHandlerStub();
|
|
this._httpClient = new HttpClient(this._handler, false);
|
|
this._resolver = new A2ACardResolver(new Uri("http://test-host"), httpClient: this._httpClient);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAIAgentAsync_WithValidAgentCard_ReturnsAIAgentAsync()
|
|
{
|
|
// Arrange
|
|
this._handler.ResponsesToReturn.Enqueue(new AgentCard
|
|
{
|
|
Name = "Test Agent",
|
|
Description = "A test agent for unit testing",
|
|
SupportedInterfaces = [new AgentInterface { Url = "http://test-endpoint/agent" }]
|
|
});
|
|
|
|
// Act
|
|
var agent = await this._resolver.GetAIAgentAsync();
|
|
|
|
// Assert
|
|
Assert.NotNull(agent);
|
|
Assert.IsType<A2AAgent>(agent);
|
|
Assert.Equal("Test Agent", agent.Name);
|
|
Assert.Equal("A test agent for unit testing", agent.Description);
|
|
|
|
// Verify that there was only one request made to retrieve the agent card
|
|
Assert.Single(this._handler.CapturedUris);
|
|
Assert.StartsWith("http://test-host/", this._handler.CapturedUris[0].ToString());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RunIAgentAsync_WithUrlFromAgentCard_SendsRequestToTheUrlAsync()
|
|
{
|
|
// Arrange
|
|
this._handler.ResponsesToReturn.Enqueue(new AgentCard
|
|
{
|
|
SupportedInterfaces = [new AgentInterface { Url = "http://test-endpoint/agent" }]
|
|
});
|
|
this._handler.ResponsesToReturn.Enqueue(new Message
|
|
{
|
|
Role = Role.Agent,
|
|
Parts = [Part.FromText("Response")],
|
|
});
|
|
|
|
var agent = await this._resolver.GetAIAgentAsync(httpClient: this._httpClient);
|
|
|
|
// Act
|
|
await agent.RunAsync("Test input");
|
|
|
|
// Assert
|
|
Assert.Equal(2, this._handler.CapturedUris.Count); // One for getting the card, one for sending the message to the agent
|
|
Assert.Equal(new Uri("http://test-endpoint/agent"), this._handler.CapturedUris[1]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAIAgentAsync_WithOptions_PassesOptionsToFactoryAsync()
|
|
{
|
|
// Arrange
|
|
this._handler.ResponsesToReturn.Enqueue(new AgentCard
|
|
{
|
|
Name = "Options Agent",
|
|
Description = "Agent with multiple interfaces",
|
|
SupportedInterfaces =
|
|
[
|
|
new AgentInterface { Url = "http://httpjson/agent", ProtocolBinding = ProtocolBindingNames.HttpJson },
|
|
new AgentInterface { Url = "http://jsonrpc/agent", ProtocolBinding = ProtocolBindingNames.JsonRpc },
|
|
]
|
|
});
|
|
this._handler.ResponsesToReturn.Enqueue(new Message
|
|
{
|
|
Role = Role.Agent,
|
|
Parts = [Part.FromText("Response")],
|
|
});
|
|
|
|
var options = new A2AClientOptions
|
|
{
|
|
PreferredBindings = [ProtocolBindingNames.JsonRpc]
|
|
};
|
|
|
|
var agent = await this._resolver.GetAIAgentAsync(httpClient: this._httpClient, options: options);
|
|
|
|
// Act
|
|
await agent.RunAsync("Test input");
|
|
|
|
// Assert
|
|
Assert.Equal(2, this._handler.CapturedUris.Count);
|
|
Assert.Equal(new Uri("http://jsonrpc/agent"), this._handler.CapturedUris[1]);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
this._handler.Dispose();
|
|
this._httpClient.Dispose();
|
|
}
|
|
|
|
internal sealed class HttpMessageHandlerStub : HttpMessageHandler
|
|
{
|
|
public Queue ResponsesToReturn { get; } = new();
|
|
|
|
public List<Uri> CapturedUris { get; } = [];
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
this.CapturedUris.Add(request.RequestUri!);
|
|
|
|
var response = this.ResponsesToReturn.Dequeue();
|
|
|
|
if (response is AgentCard agentCard)
|
|
{
|
|
var json = JsonSerializer.Serialize(agentCard);
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent(json, Encoding.UTF8, "application/json")
|
|
};
|
|
}
|
|
else if (response is Message message)
|
|
{
|
|
var sendMessageResponse = new SendMessageResponse { Message = message };
|
|
var jsonRpcResponse = new JsonRpcResponse
|
|
{
|
|
Id = "response-id",
|
|
Result = JsonSerializer.SerializeToNode(sendMessageResponse, A2AJsonUtilities.DefaultOptions)
|
|
};
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent(JsonSerializer.Serialize(jsonRpcResponse, A2AJsonUtilities.DefaultOptions), Encoding.UTF8, "application/json")
|
|
};
|
|
}
|
|
|
|
// Return empty agent card if none specified
|
|
var emptyCard = new AgentCard();
|
|
var emptyJson = JsonSerializer.Serialize(emptyCard);
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent(emptyJson, Encoding.UTF8, "application/json")
|
|
};
|
|
}
|
|
}
|
|
}
|