Skip to content

Commit 7e86057

Browse files
authored
Merge pull request #1345 from SciSharp/a2aupgrade
Refactor A2A client API and migrate to AIAgent integration
2 parents 82ab7a1 + 8204065 commit 7e86057

11 files changed

Lines changed: 476 additions & 138 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"aspire.enableSettingsFileCreationPromptOnStartup": false
3+
}

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
</PropertyGroup>
55
<ItemGroup>
6-
<PackageVersion Include="A2A" Version="0.3.3-preview" />
6+
<PackageVersion Include="A2A" Version="1.0.0-preview2" />
7+
<PackageVersion Include="Microsoft.Agents.AI.A2A" Version="1.3.0-preview.260423.1" />
78
<PackageVersion Include="CsvHelper" Version="33.1.0" />
89
<PackageVersion Include="FuzzySharp" Version="2.0.2" />
910
<PackageVersion Include="Google_GenerativeAI" Version="3.6.4" />

src/Infrastructure/BotSharp.Core.A2A/BotSharp.Core.A2A.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="A2A" />
15+
<PackageReference Include="Microsoft.Agents.AI.A2A" />
1516
</ItemGroup>
1617

1718
<ItemGroup>

src/Infrastructure/BotSharp.Core.A2A/Functions/A2ADelegationFn.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ public A2ADelegationFn(IA2AService a2aService, A2ASettings settings, IConversati
2525

2626
public async Task<bool> Execute(RoleDialogModel message)
2727
{
28-
var args = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
28+
var rawArgs = string.IsNullOrWhiteSpace(message.FunctionArgs) ? "{}" : message.FunctionArgs;
29+
var args = JsonSerializer.Deserialize<JsonElement>(rawArgs);
2930
string queryText = string.Empty;
3031
if (args.TryGetProperty("user_query", out var queryProp))
3132
{
32-
queryText = queryProp.GetString();
33+
queryText = queryProp.GetString() ?? string.Empty;
3334
}
3435

3536
var agentId = message.CurrentAgentId;
@@ -43,6 +44,12 @@ public async Task<bool> Execute(RoleDialogModel message)
4344
}
4445

4546
var conversationId = _stateService.GetConversationId();
47+
if (string.IsNullOrWhiteSpace(conversationId))
48+
{
49+
message.Content = "System Error: Conversation context is unavailable for A2A session continuation.";
50+
message.StopCompletion = true;
51+
return false;
52+
}
4653

4754
try
4855
{
@@ -54,6 +61,7 @@ public async Task<bool> Execute(RoleDialogModel message)
5461
);
5562

5663
message.Content = responseText;
64+
message.StopCompletion = true;
5765
return true;
5866
}
5967
catch (Exception ex)

src/Infrastructure/BotSharp.Core.A2A/Hooks/A2AAgentHook.cs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
using A2A;
12
using BotSharp.Abstraction.Agents;
23
using BotSharp.Abstraction.Agents.Enums;
34
using BotSharp.Abstraction.Agents.Models;
45
using BotSharp.Abstraction.Agents.Settings;
56
using BotSharp.Abstraction.Functions.Models;
67
using BotSharp.Core.A2A.Services;
78
using BotSharp.Core.A2A.Settings;
9+
using Microsoft.Extensions.Logging;
810
using System.Text.Json;
911

1012
namespace BotSharp.Core.A2A.Hooks;
@@ -15,12 +17,14 @@ public class A2AAgentHook : AgentHookBase
1517

1618
private readonly A2ASettings _a2aSettings;
1719
private readonly IA2AService _a2aService;
20+
private readonly ILogger<A2AAgentHook> _logger;
1821

19-
public A2AAgentHook(IServiceProvider services, IA2AService a2aService, A2ASettings a2aSettings, AgentSettings agentSettings)
22+
public A2AAgentHook(IServiceProvider services, IA2AService a2aService, A2ASettings a2aSettings, AgentSettings agentSettings, ILogger<A2AAgentHook> logger)
2023
: base(services, agentSettings)
2124
{
2225
_a2aService = a2aService;
2326
_a2aSettings = a2aSettings;
27+
_logger = logger;
2428
}
2529

2630
public override async Task<string?> OnAgentLoading(string id)
@@ -45,7 +49,16 @@ public override async Task OnAgentLoaded(Agent agent)
4549
var remoteConfig = _a2aSettings.Agents?.FirstOrDefault(x => x.Id == agent.Id);
4650
if (remoteConfig != null)
4751
{
48-
var agentCard = await _a2aService.GetCapabilitiesAsync(remoteConfig.Endpoint);
52+
AgentCard? agentCard = null;
53+
try
54+
{
55+
agentCard = await _a2aService.GetCapabilitiesAsync(remoteConfig.Endpoint);
56+
}
57+
catch (Exception ex)
58+
{
59+
_logger.LogWarning(ex, "Failed to resolve A2A agent card for endpoint {AgentEndpoint}. Using configured metadata.", remoteConfig.Endpoint);
60+
}
61+
4962
if (agentCard != null)
5063
{
5164
agent.Name = agentCard.Name;
@@ -54,34 +67,34 @@ public override async Task OnAgentLoaded(Agent agent)
5467
$"Your ONLY goal is to forward the user's request verbatim to the external service. " +
5568
$"You must use the function 'delegate_to_a2a' to communicate with it. " +
5669
$"Do not attempt to answer the question yourself.";
70+
}
5771

58-
var properties = new Dictionary<string, object>
72+
var properties = new Dictionary<string, object>
73+
{
5974
{
75+
"user_query",
76+
new
6077
{
61-
"user_query",
62-
new
63-
{
64-
type = "string",
65-
description = "The exact user request or task description to be forwarded."
66-
}
78+
type = "string",
79+
description = "The exact user request or task description to be forwarded."
6780
}
68-
};
81+
}
82+
};
6983

70-
var propertiesJson = JsonSerializer.Serialize(properties);
71-
var propertiesDocument = JsonDocument.Parse(propertiesJson);
84+
var propertiesJson = JsonSerializer.Serialize(properties);
85+
var propertiesDocument = JsonDocument.Parse(propertiesJson);
7286

73-
agent.Functions.Add(new FunctionDef
87+
agent.Functions.Add(new FunctionDef
88+
{
89+
Name = "delegate_to_a2a",
90+
Description = $"Delegates the task to the external {remoteConfig.Name} via A2A protocol.",
91+
Parameters = new FunctionParametersDef()
7492
{
75-
Name = "delegate_to_a2a",
76-
Description = $"Delegates the task to the external {remoteConfig.Name} via A2A protocol.",
77-
Parameters = new FunctionParametersDef()
78-
{
79-
Type = "object",
80-
Properties = propertiesDocument,
81-
Required = new List<string> { "user_query" }
82-
}
83-
});
84-
}
93+
Type = "object",
94+
Properties = propertiesDocument,
95+
Required = new List<string> { "user_query" }
96+
}
97+
});
8598
}
8699
await base.OnAgentLoaded(agent);
87100
}

0 commit comments

Comments
 (0)