mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: rename A2A extension methods (#915)
* rename * simplify via agentProxy * Update dotnet/src/Microsoft.Extensions.AI.Agents.Hosting.A2A.AspNetCore/WebApplicationExtensions.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/src/Microsoft.Extensions.AI.Agents.Hosting.A2A/Microsoft.Extensions.AI.Agents.Hosting.A2A.csproj --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
9e8f5e62e7
commit
fe6492ce60
@@ -94,8 +94,8 @@ app.UseExceptionHandler();
|
||||
app.MapActors();
|
||||
|
||||
// attach a2a with simple message communication
|
||||
app.AttachA2A(agentName: "pirate", path: "/a2a/pirate");
|
||||
app.AttachA2A(agentName: "knights-and-knaves", path: "/a2a/knights-and-knaves", agentCard: new()
|
||||
app.MapA2A(agentName: "pirate", path: "/a2a/pirate");
|
||||
app.MapA2A(agentName: "knights-and-knaves", path: "/a2a/knights-and-knaves", agentCard: new()
|
||||
{
|
||||
Name = "Knights and Knaves",
|
||||
Description = "An agent that helps you solve the knights and knaves puzzle.",
|
||||
|
||||
@@ -20,14 +20,14 @@ public static class WebApplicationExtensions
|
||||
/// <param name="app">The web application used to configure the pipeline and routes.</param>
|
||||
/// <param name="agentName">The name of the agent to use for A2A protocol integration.</param>
|
||||
/// <param name="path">The route group to use for A2A endpoints.</param>
|
||||
public static void AttachA2A(this WebApplication app, string agentName, string path)
|
||||
public static void MapA2A(this WebApplication app, string agentName, string path)
|
||||
{
|
||||
var agent = app.Services.GetRequiredKeyedService<AIAgent>(agentName);
|
||||
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
|
||||
var actorClient = app.Services.GetRequiredService<IActorClient>();
|
||||
|
||||
var taskManager = agent.AttachA2A(actorClient, loggerFactory: loggerFactory);
|
||||
app.AttachA2A(taskManager, path);
|
||||
var taskManager = agent.MapA2A(actorClient, loggerFactory: loggerFactory);
|
||||
app.MapA2A(taskManager, path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -37,7 +37,7 @@ public static class WebApplicationExtensions
|
||||
/// <param name="agentName">The name of the agent to use for A2A protocol integration.</param>
|
||||
/// <param name="path">The route group to use for A2A endpoints.</param>
|
||||
/// <param name="agentCard">Agent card info to return on query.</param>
|
||||
public static void AttachA2A(
|
||||
public static void MapA2A(
|
||||
this WebApplication app,
|
||||
string agentName,
|
||||
string path,
|
||||
@@ -47,8 +47,8 @@ public static class WebApplicationExtensions
|
||||
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
|
||||
var actorClient = app.Services.GetRequiredService<IActorClient>();
|
||||
|
||||
var taskManager = agent.AttachA2A(actorClient, agentCard: agentCard, loggerFactory: loggerFactory);
|
||||
app.AttachA2A(taskManager, path);
|
||||
var taskManager = agent.MapA2A(actorClient, agentCard: agentCard, loggerFactory: loggerFactory);
|
||||
app.MapA2A(taskManager, path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -58,7 +58,7 @@ public static class WebApplicationExtensions
|
||||
/// <param name="app">The web application used to configure the pipeline and routes.</param>
|
||||
/// <param name="taskManager">Pre-configured A2A TaskManager to use for A2A endpoints handling.</param>
|
||||
/// <param name="path">The route group to use for A2A endpoints.</param>
|
||||
public static void AttachA2A(this WebApplication app, TaskManager taskManager, string path)
|
||||
public static void MapA2A(this WebApplication app, TaskManager taskManager, string path)
|
||||
{
|
||||
// note: current SDK version registers multiple `.well-known/agent.json` handlers here.
|
||||
// it makes app return HTTP 500, but will be fixed once new A2A SDK is released.
|
||||
|
||||
@@ -22,13 +22,14 @@ public static class AIAgentExtensions
|
||||
/// <param name="taskManager">Instance of <see cref="TaskManager"/> to configure for A2A messaging. New instance will be created if not passed.</param>
|
||||
/// <param name="loggerFactory">The logger factory to use for creating <see cref="ILogger"/> instances.</param>
|
||||
/// <returns>The configured <see cref="TaskManager"/>.</returns>
|
||||
public static TaskManager AttachA2A(
|
||||
public static TaskManager MapA2A(
|
||||
this AIAgent agent,
|
||||
IActorClient actorClient,
|
||||
TaskManager? taskManager = null,
|
||||
ILoggerFactory? loggerFactory = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(agent, nameof(agent));
|
||||
ArgumentNullException.ThrowIfNull(agent.Name, nameof(agent.Name));
|
||||
ArgumentNullException.ThrowIfNull(actorClient, nameof(actorClient));
|
||||
|
||||
taskManager ??= new();
|
||||
@@ -49,14 +50,14 @@ public static class AIAgentExtensions
|
||||
/// <param name="taskManager">Instance of <see cref="TaskManager"/> to configure for A2A messaging. New instance will be created if not passed.</param>
|
||||
/// <param name="loggerFactory">The logger factory to use for creating <see cref="ILogger"/> instances.</param>
|
||||
/// <returns>The configured <see cref="TaskManager"/>.</returns>
|
||||
public static TaskManager AttachA2A(
|
||||
public static TaskManager MapA2A(
|
||||
this AIAgent agent,
|
||||
IActorClient actorClient,
|
||||
AgentCard agentCard,
|
||||
TaskManager? taskManager = null,
|
||||
ILoggerFactory? loggerFactory = null)
|
||||
{
|
||||
taskManager = agent.AttachA2A(actorClient, taskManager, loggerFactory);
|
||||
taskManager = agent.MapA2A(actorClient, taskManager, loggerFactory);
|
||||
|
||||
taskManager.OnAgentCardQuery += (context, query) =>
|
||||
{
|
||||
|
||||
@@ -10,18 +10,13 @@ namespace Microsoft.Agents.AI.Hosting.A2A.Converters;
|
||||
|
||||
internal static class ActorEntitiesConverter
|
||||
{
|
||||
public static Message ToMessage(this ActorResponse response)
|
||||
public static Message ToMessage(this AgentRunResponse response, string contextId)
|
||||
{
|
||||
var agentRunResponse =
|
||||
response.Data.Deserialize(AgentHostingJsonUtilities.DefaultOptions.GetTypeInfo(typeof(AgentRunResponse))) as AgentRunResponse ??
|
||||
throw new ArgumentException("The ActorResponse data could not be deserialized to an AgentRunResponse.", nameof(response));
|
||||
|
||||
var contextId = response.ActorId.Key;
|
||||
var parts = agentRunResponse.Messages.ToParts();
|
||||
var parts = response.Messages.ToParts();
|
||||
|
||||
return new Message
|
||||
{
|
||||
MessageId = response.MessageId ?? Guid.NewGuid().ToString("N"),
|
||||
MessageId = response.ResponseId ?? Guid.NewGuid().ToString("N"),
|
||||
ContextId = contextId,
|
||||
Role = MessageRole.Agent,
|
||||
Parts = parts
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using A2A;
|
||||
using Microsoft.Agents.AI.Hosting.A2A.Converters;
|
||||
using Microsoft.Agents.AI.Runtime;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
|
||||
namespace Microsoft.Agents.AI.Hosting.A2A.Internal;
|
||||
|
||||
@@ -16,51 +16,26 @@ namespace Microsoft.Agents.AI.Hosting.A2A.Internal;
|
||||
/// </summary>
|
||||
internal sealed class A2AAgentWrapper
|
||||
{
|
||||
private readonly AIAgent _innerAgent;
|
||||
private readonly IActorClient _actorClient;
|
||||
private readonly AgentProxy _agentProxy;
|
||||
|
||||
public A2AAgentWrapper(
|
||||
IActorClient actorClient,
|
||||
AIAgent innerAgent,
|
||||
ILoggerFactory? loggerFactory = null)
|
||||
{
|
||||
this._actorClient = actorClient;
|
||||
this._innerAgent = innerAgent ?? throw new ArgumentNullException(nameof(innerAgent));
|
||||
Throw.IfNullOrEmpty(innerAgent.Name);
|
||||
|
||||
this._agentProxy = new AgentProxy(innerAgent.Name, actorClient);
|
||||
}
|
||||
|
||||
public async Task<Message> ProcessMessageAsync(MessageSendParams messageSendParams, CancellationToken cancellationToken)
|
||||
{
|
||||
var contextId = messageSendParams.Message.ContextId ?? Guid.NewGuid().ToString("N");
|
||||
var messageId = messageSendParams.Message.MessageId;
|
||||
|
||||
var actorId = new ActorId(type: this.GetActorType(), key: contextId!);
|
||||
|
||||
// Verify request does not exist already
|
||||
var existingResponseHandle = await this._actorClient.GetResponseAsync(actorId, messageId, cancellationToken).ConfigureAwait(false);
|
||||
var existingResponse = await existingResponseHandle.GetResponseAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (existingResponse.Status is RequestStatus.Completed or RequestStatus.Failed)
|
||||
{
|
||||
return existingResponse.ToMessage();
|
||||
}
|
||||
|
||||
// here we know we did not yet send the request, so lets do it
|
||||
var chatMessages = messageSendParams.ToChatMessages();
|
||||
var runRequest = new AgentRunRequest
|
||||
{
|
||||
Messages = chatMessages
|
||||
};
|
||||
var @params = JsonSerializer.SerializeToElement(runRequest, AgentHostingJsonUtilities.DefaultOptions.GetTypeInfo(typeof(AgentRunRequest)));
|
||||
|
||||
var requestHandle = await this._actorClient.SendRequestAsync(new ActorRequest(actorId, messageId, method: "Run" /* ?refer to const here? */, @params: @params), cancellationToken).ConfigureAwait(false);
|
||||
var response = await requestHandle.GetResponseAsync(cancellationToken).ConfigureAwait(false);
|
||||
var thread = this._agentProxy.GetNewThread(contextId);
|
||||
var response = await this._agentProxy.RunAsync(messages: chatMessages, thread: thread, options: null, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return response.ToMessage();
|
||||
}
|
||||
|
||||
private ActorType GetActorType()
|
||||
{
|
||||
// agent is registered in DI via name
|
||||
ArgumentException.ThrowIfNullOrEmpty(this._innerAgent.Name);
|
||||
return new ActorType(this._innerAgent.Name);
|
||||
return response.ToMessage(contextId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
<VersionSuffix>alpha</VersionSuffix>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<InjectSharedThrow>true</InjectSharedThrow>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="A2A" />
|
||||
|
||||
|
||||
Reference in New Issue
Block a user