Skip to content

Commit 777a00f

Browse files
committed
Make sure we always have the AgentId in the ChatResponse
When converting to ChatResponse, the AgentRunResponse.AgentId should be set as an additional property. Fixes microsoft/agent-framework#1574
1 parent 32ca484 commit 777a00f

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/Agents/AgentExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.ComponentModel;
2+
using System.Runtime.CompilerServices;
3+
using Microsoft.Agents.AI;
4+
using Microsoft.Extensions.AI;
5+
6+
namespace Devlooped.Agents.AI;
7+
8+
/// <summary>
9+
/// Miscenalleous extension methods for agents.
10+
/// </summary>
11+
[EditorBrowsable(EditorBrowsableState.Never)]
12+
public static class AgentExtensions
13+
{
14+
/// <summary>Ensures the returned <see cref="ChatResponse"/> contains the <see cref="AgentRunResponse.AgentId"/> as an additional property.</summary>
15+
/// <devdoc>Change priority to -10 and make EditorBrowsable.Never when https://github.com/microsoft/agent-framework/issues/1574 is fixed.</devdoc>
16+
[OverloadResolutionPriority(10)]
17+
public static ChatResponse AsChatResponse(this AgentRunResponse response)
18+
{
19+
var chatResponse = AgentRunResponseExtensions.AsChatResponse(response);
20+
21+
chatResponse.AdditionalProperties ??= [];
22+
chatResponse.AdditionalProperties[nameof(response.AgentId)] = response.AgentId;
23+
24+
return chatResponse;
25+
}
26+
}

src/Agents/Agents.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0-preview.251009.1" />
2828
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.9" />
2929
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.9" />
30+
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" />
3031
</ItemGroup>
3132

3233
<ItemGroup>

src/Tests/ConfigurableAgentTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,14 @@ public void CanSetGrokOptions()
249249
Assert.Equal(ReasoningEffort.High, grok.ReasoningEffort);
250250
Assert.Equal(GrokSearch.Off, grok.Search);
251251
}
252+
253+
[Fact]
254+
public void Task()
255+
{
256+
var agent = new AgentRunResponse() { AgentId = "agent-123" };
257+
var chat = agent.AsChatResponse();
258+
259+
Assert.Equal("agent-123", chat.AdditionalProperties!["AgentId"]);
260+
}
252261
}
253262

0 commit comments

Comments
 (0)