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,51 @@
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

Check failure on line 374 in test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs#L374

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs(374,13): error SA1515: (NETCORE_ENGINEERING_TELEMETRY=Build) Single-line comment should be preceded by blank line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1515.md)

Check failure on line 374 in test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs#L374

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs(374,13): error SA1515: (NETCORE_ENGINEERING_TELEMETRY=Build) Single-line comment should be preceded by blank line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1515.md)

Check failure on line 374 in test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs

View check run for this annotation

Azure Pipelines / extensions-ci

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs#L374

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs(374,13): error SA1515: (NETCORE_ENGINEERING_TELEMETRY=Build) Single-line comment should be preceded by blank line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1515.md)

Check failure on line 374 in test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs

View check run for this annotation

Azure Pipelines / extensions-ci

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs#L374

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs(374,13): error SA1515: (NETCORE_ENGINEERING_TELEMETRY=Build) Single-line comment should be preceded by blank line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1515.md)
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 },

// null should not overwrite
new(null, "g") { CreatedAt = null },
];

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

Assert.Equal("abcdefg", 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