Skip to content

Commit b99c781

Browse files
.NET: Use extension methods from A2A package for converting between MEAI & A2A model classes (microsoft#1600)
* use extension methods from A2A package for converting between MEAI and A2A model classes. * Update dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs Co-authored-by: Stephen Toub <[email protected]> * remove unused using --------- Co-authored-by: Stephen Toub <[email protected]>
1 parent 1760593 commit b99c781

File tree

14 files changed

+25
-1073
lines changed

14 files changed

+25
-1073
lines changed

dotnet/Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
<!-- Agent SDKs -->
7373
<PackageVersion Include="Microsoft.Agents.CopilotStudio.Client" Version="1.2.41" />
7474
<!-- A2A -->
75-
<PackageVersion Include="A2A" Version="0.3.1-preview" />
76-
<PackageVersion Include="A2A.AspNetCore" Version="0.3.1-preview" />
75+
<PackageVersion Include="A2A" Version="0.3.3-preview" />
76+
<PackageVersion Include="A2A.AspNetCore" Version="0.3.3-preview" />
7777
<!-- MCP -->
7878
<PackageVersion Include="ModelContextProtocol" Version="0.4.0-preview.2" />
7979
<!-- Inference SDKs -->

dotnet/samples/AgentWebChat/AgentWebChat.Web/A2AAgentClient.cs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public async override IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync
5858
if (a2aResponse is AgentMessage message)
5959
{
6060
var responseMessage = message.ToChatMessage();
61-
if (responseMessage is not null)
61+
if (responseMessage is { Contents.Count: > 0 })
6262
{
6363
results.Add(new AgentRunResponseUpdate(responseMessage.Role, responseMessage.Contents)
6464
{
@@ -78,11 +78,7 @@ public async override IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync
7878

7979
foreach (var part in artifact.Parts)
8080
{
81-
var aiContent = ConvertPartToAIContent(part);
82-
if (aiContent != null)
83-
{
84-
(aiContents ??= []).Add(aiContent);
85-
}
81+
(aiContents ??= []).Add(part.ToAIContent());
8682
}
8783

8884
if (aiContents is not null)
@@ -155,20 +151,6 @@ public async override IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync
155151
return (a2aClient, a2aCardResolver);
156152
});
157153

158-
private static AIContent? ConvertPartToAIContent(Part part) =>
159-
part switch
160-
{
161-
TextPart textPart => new TextContent(textPart.Text)
162-
{
163-
RawRepresentation = textPart
164-
},
165-
FilePart filePart when filePart.File is FileWithUri fileWithUrl => new HostedFileContent(fileWithUrl.Uri)
166-
{
167-
RawRepresentation = filePart
168-
},
169-
_ => null
170-
};
171-
172154
private static AdditionalPropertiesDictionary? ConvertMetadataToAdditionalProperties(Dictionary<string, JsonElement>? metadata)
173155
{
174156
if (metadata is not { Count: > 0 })
@@ -184,22 +166,3 @@ public async override IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync
184166
return additionalProperties;
185167
}
186168
}
187-
188-
// Extension method to convert multiple chat messages to A2A messages
189-
internal static class ChatMessageExtensions
190-
{
191-
public static List<AgentMessage> ToA2AMessages(this IList<ChatMessage> chatMessages)
192-
{
193-
if (chatMessages is null || chatMessages.Count == 0)
194-
{
195-
return [];
196-
}
197-
198-
var result = new List<AgentMessage>();
199-
foreach (var chatMessage in chatMessages)
200-
{
201-
result.Add(chatMessage.ToA2AMessage());
202-
}
203-
return result;
204-
}
205-
}

dotnet/src/Microsoft.Agents.AI.A2A/A2AHostAgent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66
using A2A;
7+
using Microsoft.Extensions.AI;
78
using Microsoft.Shared.Diagnostics;
89

910
namespace Microsoft.Agents.AI.A2A;

dotnet/src/Microsoft.Agents.AI.A2A/Extensions/A2AAIContentExtensions.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ internal static class A2AAIContentExtensions
1515
/// </summary>
1616
/// <param name="contents">The collection of AI contents to convert.</param>"
1717
/// <returns>The list of A2A <see cref="Part"/> objects.</returns>
18-
internal static List<Part>? ToA2AParts(this IEnumerable<AIContent> contents)
18+
internal static List<Part>? ToParts(this IEnumerable<AIContent> contents)
1919
{
2020
List<Part>? parts = null;
2121

2222
foreach (var content in contents)
2323
{
24-
var part = content.ToA2APart();
24+
var part = content.ToPart();
2525
if (part is not null)
2626
{
2727
(parts ??= []).Add(part);
@@ -30,18 +30,4 @@ internal static class A2AAIContentExtensions
3030

3131
return parts;
3232
}
33-
34-
/// <summary>
35-
/// Converts a <see cref="AIContent"/> to a <see cref="Part"/> object."/>
36-
/// </summary>
37-
/// <param name="content">AI content to convert.</param>
38-
/// <returns>The corresponding A2A <see cref="Part"/> object, or null if the content type is not supported.</returns>
39-
internal static Part? ToA2APart(this AIContent content) =>
40-
content switch
41-
{
42-
TextContent textContent => new TextPart { Text = textContent.Text },
43-
HostedFileContent hostedFileContent => new FilePart { File = new FileWithUri { Uri = hostedFileContent.FileId } },
44-
// Ignore unknown content types (FunctionCallContent, FunctionResultContent, etc.)
45-
_ => null,
46-
};
4733
}

dotnet/src/Microsoft.Agents.AI.A2A/Extensions/A2AMessageExtensions.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

dotnet/src/Microsoft.Agents.AI.A2A/Extensions/A2APartExtensions.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

dotnet/src/Microsoft.Agents.AI.A2A/Extensions/ChatMessageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal static AgentMessage ToA2AMessage(this IEnumerable<ChatMessage> messages
1717

1818
foreach (var message in messages)
1919
{
20-
if (message.Contents.ToA2AParts() is { Count: > 0 } ps)
20+
if (message.Contents.ToParts() is { Count: > 0 } ps)
2121
{
2222
allParts.AddRange(ps);
2323
}

0 commit comments

Comments
 (0)