Skip to content

Commit edcc786

Browse files
.NET: Preserve and propagate CreatedAt through workflows (#3930)
* Preserve per-message CreatedAt attribute if it's available * Add unit test --------- Co-authored-by: Sam Chang <changsam@microsoft.com> Co-authored-by: samchang-msft <samchang.msft@gmail.com>
1 parent 07a1e83 commit edcc786

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

dotnet/src/Microsoft.Agents.AI.Abstractions/AgentResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public AgentResponseUpdate[] ToAgentResponseUpdates()
297297
AgentId = this.AgentId,
298298
ResponseId = this.ResponseId,
299299
MessageId = message.MessageId,
300-
CreatedAt = this.CreatedAt,
300+
CreatedAt = message.CreatedAt ?? this.CreatedAt,
301301
};
302302
}
303303

dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentResponseTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,42 @@ public void ToAgentResponseUpdatesProducesUpdates()
222222
Assert.Equal(100, usageContent.Details.TotalTokenCount);
223223
}
224224

225+
[Fact]
226+
public void ToAgentResponseUpdatesPropagatesCreatedAt()
227+
{
228+
// Sets different CreatedAt values on the AgentResponse and the ChatMessage to verify that the ChatMessage.CreatedAt is the one that gets propagated to the AgentResponseUpdate
229+
AgentResponse response = new(new ChatMessage(new ChatRole("customRole"), "Text") { MessageId = "someMessage", CreatedAt = new DateTimeOffset(2024, 11, 11, 9, 20, 0, TimeSpan.Zero) })
230+
{
231+
AgentId = "agentId",
232+
ResponseId = "12345",
233+
CreatedAt = new DateTimeOffset(2024, 11, 10, 9, 20, 0, TimeSpan.Zero),
234+
AdditionalProperties = new() { ["key1"] = "value1", ["key2"] = 42 },
235+
Usage = new UsageDetails
236+
{
237+
TotalTokenCount = 100
238+
},
239+
};
240+
241+
AgentResponseUpdate[] updates = response.ToAgentResponseUpdates();
242+
Assert.NotNull(updates);
243+
Assert.Equal(2, updates.Length);
244+
245+
AgentResponseUpdate update0 = updates[0];
246+
Assert.Equal("agentId", update0.AgentId);
247+
Assert.Equal("12345", update0.ResponseId);
248+
Assert.Equal("someMessage", update0.MessageId);
249+
Assert.Equal(new DateTimeOffset(2024, 11, 11, 9, 20, 0, TimeSpan.Zero), update0.CreatedAt);
250+
Assert.Equal("customRole", update0.Role?.Value);
251+
Assert.Equal("Text", update0.Text);
252+
253+
AgentResponseUpdate update1 = updates[1];
254+
Assert.Equal("value1", update1.AdditionalProperties?["key1"]);
255+
Assert.Equal(42, update1.AdditionalProperties?["key2"]);
256+
Assert.IsType<UsageContent>(update1.Contents[0]);
257+
UsageContent usageContent = (UsageContent)update1.Contents[0];
258+
Assert.Equal(100, usageContent.Details.TotalTokenCount);
259+
}
260+
225261
[Fact]
226262
public void ParseAsStructuredOutputWithJSOSuccess()
227263
{

0 commit comments

Comments
 (0)