Skip to content

Commit d964b05

Browse files
stephentoubCopilot
andauthored
Add an AgentRunResponse ctor accepting a ChatResponse (#204)
* Add an AgentRunResponse ctor accepting a ChatResponse * Update dotnet/src/Microsoft.Extensions.AI.Agents.Abstractions/AgentRunResponseUpdate.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a8fadbc commit d964b05

6 files changed

Lines changed: 85 additions & 62 deletions

File tree

dotnet/src/Microsoft.Extensions.AI.Agents.Abstractions/AgentRunResponse.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public AgentRunResponse(ChatMessage message)
4444
this.Messages.Add(message);
4545
}
4646

47+
/// <summary>Initializes a new instance of the <see cref="AgentRunResponse"/> class.</summary>
48+
/// <param name="response">The <see cref="ChatResponse"/> from which to seed this <see cref="AgentRunResponse"/>.</param>
49+
/// <exception cref="ArgumentNullException"><paramref name="response"/> is <see langword="null"/>.</exception>
50+
public AgentRunResponse(ChatResponse response)
51+
{
52+
_ = Throw.IfNull(response);
53+
54+
this.AdditionalProperties = response.AdditionalProperties;
55+
this.CreatedAt = response.CreatedAt;
56+
this.Messages = response.Messages;
57+
this.RawRepresentation = response.RawRepresentation;
58+
this.ResponseId = response.ResponseId;
59+
this.Usage = response.Usage;
60+
}
61+
4762
/// <summary>Initializes a new instance of the <see cref="AgentRunResponse"/> class.</summary>
4863
/// <param name="messages">The response messages.</param>
4964
public AgentRunResponse(IList<ChatMessage>? messages)

dotnet/src/Microsoft.Extensions.AI.Agents.Abstractions/AgentRunResponseUpdate.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Text.Json.Serialization;
8+
using Microsoft.Shared.Diagnostics;
89

910
namespace Microsoft.Extensions.AI.Agents;
1011

@@ -58,6 +59,22 @@ public AgentRunResponseUpdate(ChatRole? role, IList<AIContent>? contents)
5859
this._contents = contents;
5960
}
6061

62+
/// <summary>Initializes a new instance of the <see cref="AgentRunResponseUpdate"/> class.</summary>
63+
/// <param name="chatResponseUpdate">The <see cref="ChatResponseUpdate"/> from which to seed this <see cref="AgentRunResponseUpdate"/>.</param>
64+
public AgentRunResponseUpdate(ChatResponseUpdate chatResponseUpdate)
65+
{
66+
_ = Throw.IfNull(chatResponseUpdate);
67+
68+
this.AdditionalProperties = chatResponseUpdate.AdditionalProperties;
69+
this.AuthorName = chatResponseUpdate.AuthorName;
70+
this.Contents = chatResponseUpdate.Contents;
71+
this.CreatedAt = chatResponseUpdate.CreatedAt;
72+
this.MessageId = chatResponseUpdate.MessageId;
73+
this.RawRepresentation = chatResponseUpdate.RawRepresentation;
74+
this.ResponseId = chatResponseUpdate.ResponseId;
75+
this.Role = chatResponseUpdate.Role;
76+
}
77+
6178
/// <summary>Gets or sets the name of the author of the response update.</summary>
6279
public string? AuthorName
6380
{

dotnet/src/Microsoft.Extensions.AI.Agents/ChatCompletion/ChatClientAgent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public override async Task<AgentRunResponse> RunAsync(
130130

131131
await this.NotifyThreadOfNewMessagesAsync(chatClientThread, chatResponseMessages, cancellationToken).ConfigureAwait(false);
132132

133-
return chatResponse.ToAgentRunResponse(this.Id);
133+
return new(chatResponse) { AgentId = this.Id };
134134
}
135135

136136
/// <inheritdoc/>
@@ -167,7 +167,7 @@ public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync
167167
{
168168
responseUpdates.Add(update);
169169
update.AuthorName ??= this.Name;
170-
yield return update.ToAgentRunResponseUpdate(this.Id);
170+
yield return new(update) { AgentId = this.Id };
171171
}
172172

173173
hasUpdates = await responseUpdatesEnumerator.MoveNextAsync().ConfigureAwait(false);

dotnet/src/Microsoft.Extensions.AI.Agents/ChatCompletion/ChatResponseExtensions.cs

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

dotnet/tests/Microsoft.Extensions.AI.Agents.Abstractions.UnitTests/AgentRunResponseTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ public void ConstructorWithMessagesRoundtrips()
4343
Assert.Same(messages, response.Messages);
4444
}
4545

46+
[Fact]
47+
public void ConstructorWithChatResponseRoundtrips()
48+
{
49+
ChatResponse chatResponse = new()
50+
{
51+
AdditionalProperties = new(),
52+
CreatedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero),
53+
Messages = [new(ChatRole.Assistant, "This is a test message.")],
54+
RawRepresentation = new object(),
55+
ResponseId = "responseId",
56+
Usage = new UsageDetails(),
57+
};
58+
59+
AgentRunResponse response = new(chatResponse);
60+
Assert.Same(chatResponse.AdditionalProperties, response.AdditionalProperties);
61+
Assert.Equal(chatResponse.CreatedAt, response.CreatedAt);
62+
Assert.Same(chatResponse.Messages, response.Messages);
63+
Assert.Equal(chatResponse.ResponseId, response.ResponseId);
64+
Assert.Same(chatResponse.RawRepresentation, response.RawRepresentation);
65+
Assert.Same(chatResponse.Usage, response.Usage);
66+
}
67+
4668
[Fact]
4769
public void PropertiesRoundtrip()
4870
{

dotnet/tests/Microsoft.Extensions.AI.Agents.Abstractions.UnitTests/AgentRunResponseUpdatesTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@ public void ConstructorPropsDefaulted()
2424
Assert.Equal(string.Empty, update.ToString());
2525
}
2626

27+
[Fact]
28+
public void ConstructorWithChatResponseUpdateRoundtrips()
29+
{
30+
ChatResponseUpdate chatResponseUpdate = new()
31+
{
32+
AdditionalProperties = new(),
33+
AuthorName = "author",
34+
Contents = [new TextContent("hello")],
35+
ConversationId = "conversationId",
36+
CreatedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero),
37+
FinishReason = ChatFinishReason.Length,
38+
MessageId = "messageId",
39+
ModelId = "modelId",
40+
RawRepresentation = new object(),
41+
ResponseId = "responseId",
42+
Role = ChatRole.Assistant,
43+
};
44+
45+
AgentRunResponseUpdate response = new(chatResponseUpdate);
46+
Assert.Same(chatResponseUpdate.AdditionalProperties, response.AdditionalProperties);
47+
Assert.Equal(chatResponseUpdate.AuthorName, response.AuthorName);
48+
Assert.Same(chatResponseUpdate.Contents, response.Contents);
49+
Assert.Equal(chatResponseUpdate.CreatedAt, response.CreatedAt);
50+
Assert.Equal(chatResponseUpdate.MessageId, response.MessageId);
51+
Assert.Same(chatResponseUpdate.RawRepresentation, response.RawRepresentation);
52+
Assert.Equal(chatResponseUpdate.ResponseId, response.ResponseId);
53+
Assert.Equal(chatResponseUpdate.Role, response.Role);
54+
}
55+
2756
[Fact]
2857
public void PropertiesRoundtrip()
2958
{

0 commit comments

Comments
 (0)