Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions codegen/generator/src/Visitors/PaginationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ private bool TryHandleGetRawPagesAsyncMethod(MethodProvider method)
binaryExpr.Right is KeywordExpression rightKeyword &&
rightKeyword.Keyword == "null")
{
// Create "hasMore == null" condition
var hasMoreNullCheck = new BinaryOperatorExpression(
"==",
// Create "!hasMore" condition
var hasMoreNullCheck = new UnaryOperatorExpression(
"!",
new MemberExpression(null, "hasMore"),
Snippet.False);
OperandOnTheLeft: false);

// Return "nextToken == null || hasMore == null"
// Return "nextToken == null || !hasMore"
return new BinaryOperatorExpression("||", binaryExpr, hasMoreNullCheck);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override async IAsyncEnumerable<ClientResult> GetRawPagesAsync()
bool hasMore = ((InternalChatCompletionMessageList)result).HasMore;
nextToken = ((InternalChatCompletionMessageList)result).LastId;
// Plugin customization: add hasMore == false check to pagination condition
if (nextToken == null || hasMore == false)
if (nextToken == null || !hasMore)
{
yield break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override async IAsyncEnumerable<ClientResult> GetRawPagesAsync()
bool hasMore = ((InternalChatCompletionMessageList)result).HasMore;
nextToken = ((InternalChatCompletionMessageList)result).LastId;
// Plugin customization: add hasMore == false check to pagination condition
if (nextToken == null || hasMore == false)
if (nextToken == null || !hasMore)
{
yield break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override IEnumerable<ClientResult> GetRawPages()
bool hasMore = ((InternalChatCompletionMessageList)result).HasMore;
nextToken = ((InternalChatCompletionMessageList)result).LastId;
// Plugin customization: add hasMore == false check to pagination condition
if (nextToken == null || hasMore == false)
if (nextToken == null || !hasMore)
{
yield break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override IEnumerable<ClientResult> GetRawPages()
bool hasMore = ((InternalChatCompletionMessageList)result).HasMore;
nextToken = ((InternalChatCompletionMessageList)result).LastId;
// Plugin customization: add hasMore == false check to pagination condition
if (nextToken == null || hasMore == false)
if (nextToken == null || !hasMore)
{
yield break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override async IAsyncEnumerable<ClientResult> GetRawPagesAsync()
bool hasMore = ((InternalChatCompletionList)result).HasMore;
nextToken = ((InternalChatCompletionList)result).LastId;
// Plugin customization: add hasMore == false check to pagination condition
if (nextToken == null || hasMore == false)
if (nextToken == null || !hasMore)
{
yield break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override async IAsyncEnumerable<ClientResult> GetRawPagesAsync()
bool hasMore = ((InternalChatCompletionList)result).HasMore;
nextToken = ((InternalChatCompletionList)result).LastId;
// Plugin customization: add hasMore == false check to pagination condition
if (nextToken == null || hasMore == false)
if (nextToken == null || !hasMore)
{
yield break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override IEnumerable<ClientResult> GetRawPages()
bool hasMore = ((InternalChatCompletionList)result).HasMore;
nextToken = ((InternalChatCompletionList)result).LastId;
// Plugin customization: add hasMore == false check to pagination condition
if (nextToken == null || hasMore == false)
if (nextToken == null || !hasMore)
{
yield break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override IEnumerable<ClientResult> GetRawPages()
bool hasMore = ((InternalChatCompletionList)result).HasMore;
nextToken = ((InternalChatCompletionList)result).LastId;
// Plugin customization: add hasMore == false check to pagination condition
if (nextToken == null || hasMore == false)
if (nextToken == null || !hasMore)
{
yield break;
}
Expand Down
106 changes: 21 additions & 85 deletions tests/Chat/ChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public async Task StreamingChatCanBeCancelledAsync()
Assert.That(firstUpdate, Is.Not.Null);
Assert.That(cancellationTokenSource.IsCancellationRequested, Is.False);

Thread.Sleep(1000);
await Task.Delay(1000);

Assert.ThrowsAsync<OperationCanceledException>(async () =>
{
Expand Down Expand Up @@ -1090,7 +1090,7 @@ public async Task GetChatCompletionsWithPagination()
completionIds.Add(completion.Id);
}

Thread.Sleep(5000); // Wait for completions to be stored
await Task.Delay(5000); // Wait for completions to be stored

// Test pagination with limit
ChatCompletionCollectionOptions paginationOptions = new()
Expand Down Expand Up @@ -1146,7 +1146,7 @@ public async Task GetChatCompletionsWithAfterIdPagination()
completionIds.Add(completion.Id);
}

Thread.Sleep(5000); // Wait for completions to be stored
await Task.Delay(5000); // Wait for completions to be stored

// Get first completion to use as afterId
string afterId = null;
Expand Down Expand Up @@ -1205,10 +1205,10 @@ public async Task GetChatCompletionsWithOrderFiltering()
createOptions);

completionIds.Add(completion.Id);
Thread.Sleep(1000); // Ensure different timestamps
await Task.Delay(1000); // Ensure different timestamps
}

Thread.Sleep(5000); // Wait for completions to be stored
await Task.Delay(5000); // Wait for completions to be stored

// Test ascending order
ChatCompletionCollectionOptions ascOptions = new()
Expand Down Expand Up @@ -1290,7 +1290,7 @@ public async Task GetChatCompletionsWithMetadataFiltering()
options2);
completionIds.Add(otherCompletion.Id);

Thread.Sleep(5000); // Wait for completions to be stored
await Task.Delay(5000); // Wait for completions to be stored

// Filter by specific metadata
ChatCompletionCollectionOptions filterOptions = new()
Expand Down Expand Up @@ -1338,7 +1338,7 @@ public async Task GetChatCompletionsWithModelFiltering()
["Model filter test: Say 'Hello'"],
createOptions);

Thread.Sleep(5000); // Wait for completion to be stored
await Task.Delay(5000); // Wait for completion to be stored

// Filter by the model used by the test client
ChatCompletionCollectionOptions filterOptions = new()
Expand Down Expand Up @@ -1381,7 +1381,7 @@ public async Task GetChatCompletionsWithEmptyOptions()
["Empty options test: Say 'Hello'"],
createOptions);

Thread.Sleep(5000); // Wait for completion to be stored
await Task.Delay(5000); // Wait for completion to be stored

// Test with default/empty options
int count = 0;
Expand Down Expand Up @@ -1424,7 +1424,7 @@ public async Task GetChatCompletionsWithCombinedFilters()
["Combined filters test: Say 'Combined test'"],
createOptions);

Thread.Sleep(5000); // Wait for completion to be stored
await Task.Delay(6000); // Wait for completion to be stored

// Test with combined filters
ChatCompletionCollectionOptions combinedOptions = new()
Expand Down Expand Up @@ -1522,18 +1522,20 @@ public async Task StoredChatCompletionsWork()
[new UserChatMessage("Say `this is a test`.")],
options);

Thread.Sleep(5000);
await RetryWithExponentialBackoffAsync(async () =>
{

ChatCompletion storedCompletion = await client.GetChatCompletionAsync(completion.Id);
ChatCompletion storedCompletion = await client.GetChatCompletionAsync(completion.Id);

Assert.That(storedCompletion.Id, Is.EqualTo(completion.Id));
Assert.That(storedCompletion.Content[0].Text, Is.EqualTo(completion.Content[0].Text));
Assert.That(storedCompletion.Id, Is.EqualTo(completion.Id));
Assert.That(storedCompletion.Content[0].Text, Is.EqualTo(completion.Content[0].Text));

ChatCompletionDeletionResult deletionResult = await client.DeleteChatCompletionAsync(completion.Id);
ChatCompletionDeletionResult deletionResult = await client.DeleteChatCompletionAsync(completion.Id);

Assert.That(deletionResult.Deleted, Is.True);
Assert.That(deletionResult.Deleted, Is.True);
});

Thread.Sleep(5000);
await Task.Delay(5000);

Assert.ThrowsAsync<ClientResultException>(async () =>
{
Expand Down Expand Up @@ -1571,7 +1573,7 @@ public async Task GetChatCompletionsValidatesCollectionEnumeration()
["Enumeration test: Say 'Test enumeration'"],
createOptions);

Thread.Sleep(5000); // Wait for completion to be stored
await Task.Delay(5000); // Wait for completion to be stored

// Test that we can enumerate multiple times
ChatCompletionCollectionOptions collectionOptions = new()
Expand Down Expand Up @@ -1625,7 +1627,7 @@ public async Task GetChatCompletionsHandlesLargeLimits()
["Large limit test: Say 'Testing large limits'"],
createOptions);

Thread.Sleep(5000); // Wait for completion to be stored
await Task.Delay(5000); // Wait for completion to be stored

// Test with a large page size limit
ChatCompletionCollectionOptions largeOptions = new()
Expand Down Expand Up @@ -1666,7 +1668,7 @@ public async Task GetChatCompletionsWithMinimalLimits()
["Minimal limit test: Say 'Testing minimal limits'"],
createOptions);

Thread.Sleep(5000); // Wait for completion to be stored
await Task.Delay(5000); // Wait for completion to be stored

// Test with minimal page size
ChatCompletionCollectionOptions minimalOptions = new()
Expand Down Expand Up @@ -1974,72 +1976,6 @@ await RetryWithExponentialBackoffAsync(async () =>
catch { /* Ignore cleanup errors */ }
}

[Test]
public async Task GetChatCompletionMessagesWithMessageProperties()
{
ChatClient client = GetTestClient();

// Create completion with function calling to test various message properties
ChatTool calculatorTool = ChatTool.CreateFunctionTool(
"calculate",
"Perform basic calculations",
BinaryData.FromString("""
{
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Mathematical expression to evaluate"
}
},
"required": ["expression"]
}
"""));

ChatCompletionOptions createOptions = new()
{
StoredOutputEnabled = true,
Tools = { calculatorTool },
Metadata = { ["test_scenario"] = "message_properties" }
};

ChatCompletion completion = await client.CompleteChatAsync(
["Message properties test: Calculate 2 + 2"],
createOptions);

await RetryWithExponentialBackoffAsync(async () =>
{
// Test message properties
bool foundUserMessage = false;
bool foundAssistantMessage = false;

await foreach (var message in client.GetChatCompletionMessagesAsync(completion.Id))
{
// Validate required properties
Assert.That(message.Id, Is.Not.Null.And.Not.Empty);

// Test collections are properly initialized
Assert.That(message.ToolCalls, Is.Not.Null);
Assert.That(message.Annotations, Is.Not.Null);

// Refusal should be null or a string
if (message.Refusal != null)
{
Assert.That(message.Refusal, Is.TypeOf<string>());
}
}

Assert.That(foundUserMessage || foundAssistantMessage, Is.True, "Should find at least one message");
});

// Clean up
try
{
await client.DeleteChatCompletionAsync(completion.Id);
}
catch { /* Ignore cleanup errors */ }
}

[Test]
public async Task GetChatCompletionMessagesWithCombinedOptions()
{
Expand Down