Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private static void ProcessUpdate(ChatResponseUpdate update, ChatResponse respon
message.AuthorName = update.AuthorName;
}

if (update.CreatedAt is not null)
if (message.CreatedAt is null || (update.CreatedAt is not null && update.CreatedAt > message.CreatedAt))
{
message.CreatedAt = update.CreatedAt;
}
Expand Down Expand Up @@ -391,7 +391,7 @@ private static void ProcessUpdate(ChatResponseUpdate update, ChatResponse respon
response.ConversationId = update.ConversationId;
}

if (update.CreatedAt is not null)
if (response.CreatedAt is null || (update.CreatedAt is not null && update.CreatedAt > response.CreatedAt))
{
response.CreatedAt = update.CreatedAt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,49 @@ public async Task ToChatResponse_UsageContentExtractedFromContents()
Assert.Equal("Hello, world!", Assert.IsType<TextContent>(Assert.Single(Assert.Single(response.Messages).Contents)).Text);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ToChatResponse_AlternativeTimestamps(bool useAsync)
{
DateTimeOffset early = new(2024, 1, 1, 10, 0, 0, TimeSpan.Zero);
DateTimeOffset middle = new(2024, 1, 1, 11, 0, 0, TimeSpan.Zero);
DateTimeOffset late = new(2024, 1, 1, 12, 0, 0, TimeSpan.Zero);
DateTimeOffset unixEpoch = new(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

ChatResponseUpdate[] updates =
[

// Start with an early timestamp
new(ChatRole.Tool, "a") { MessageId = "4", CreatedAt = early },

// Unix epoch (as "null") should not overwrite
new(null, "b") { CreatedAt = unixEpoch },

// Newer timestamp should overwrite
new(null, "c") { CreatedAt = middle },

// Older timestamp should not overwrite
new(null, "d") { CreatedAt = early },

// Even newer timestamp should overwrite
new(null, "e") { CreatedAt = late },

// Unix epoch should not overwrite again
new(null, "f") { CreatedAt = unixEpoch },
];

ChatResponse response = useAsync ?
updates.ToChatResponse() :
await YieldAsync(updates).ToChatResponseAsync();
Assert.Single(response.Messages);

Assert.Equal("abcdef", response.Messages[0].Text);
Assert.Equal(ChatRole.Tool, response.Messages[0].Role);
Assert.Equal(late, response.Messages[0].CreatedAt);
Assert.Equal(late, response.CreatedAt);
}

private static async IAsyncEnumerable<ChatResponseUpdate> YieldAsync(IEnumerable<ChatResponseUpdate> updates)
{
foreach (ChatResponseUpdate update in updates)
Expand Down
Loading