Skip to content

.Net: Add more options to getmessages for each agentthread and improve xml docs. #11138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions dotnet/src/Agents/Abstractions/AgentThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public abstract class AgentThread
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task that completes when the thread has been created.</returns>
/// <exception cref="InvalidOperationException">The thread has been deleted.</exception>
protected virtual async Task CreateAsync(CancellationToken cancellationToken = default)
{
if (this.IsDeleted)
Expand All @@ -51,6 +52,7 @@ protected virtual async Task CreateAsync(CancellationToken cancellationToken = d
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task that completes when the thread has been deleted.</returns>
/// <exception cref="InvalidOperationException">The thread was never created.</exception>
public virtual async Task DeleteAsync(CancellationToken cancellationToken = default)
{
if (this.IsDeleted)
Expand All @@ -77,6 +79,7 @@ public virtual async Task DeleteAsync(CancellationToken cancellationToken = defa
/// <param name="newMessage">The new message.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task that completes when the context has been updated.</returns>
/// <exception cref="InvalidOperationException">The thread has been deleted.</exception>
public virtual async Task OnNewMessageAsync(ChatMessageContent newMessage, CancellationToken cancellationToken = default)
{
if (this.IsDeleted)
Expand Down
12 changes: 9 additions & 3 deletions dotnet/src/Agents/AzureAI/AzureAIAgentThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,15 @@ protected override async Task OnNewMessageInternalAsync(ChatMessageContent newMe
}
}

/// <inheritdoc />
/// <summary>
/// Asynchronously retrieves all messages in the thread.
/// </summary>
/// <param name="sortOrder">The order to return messages in.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The messages in the thread.</returns>
/// <exception cref="InvalidOperationException">The thread has been deleted.</exception>
[Experimental("SKEXP0110")]
public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync(ListSortOrder? sortOrder = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (this.IsDeleted)
{
Expand All @@ -155,7 +161,7 @@ public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync([EnumeratorCa
await this.CreateAsync(cancellationToken).ConfigureAwait(false);
}

await foreach (var message in AgentThreadActions.GetMessagesAsync(this._client, this.Id!, ListSortOrder.Ascending, cancellationToken).ConfigureAwait(false))
await foreach (var message in AgentThreadActions.GetMessagesAsync(this._client, this.Id!, sortOrder, cancellationToken).ConfigureAwait(false))
{
yield return message;
}
Expand Down
10 changes: 9 additions & 1 deletion dotnet/src/Agents/Core/ChatHistoryAgentThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ protected override Task OnNewMessageInternalAsync(ChatMessageContent newMessage,
return Task.CompletedTask;
}

/// <inheritdoc />
/// <summary>
/// Asynchronously retrieves all messages in the thread.
/// </summary>
/// <remarks>
/// Messages will be returned in ascending chronological order.
/// </remarks>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The messages in the thread.</returns>
/// <exception cref="InvalidOperationException">The thread has been deleted.</exception>
[Experimental("SKEXP0110")]
public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Expand Down
12 changes: 9 additions & 3 deletions dotnet/src/Agents/OpenAI/OpenAIAssistantAgentThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,15 @@ protected override async Task OnNewMessageInternalAsync(ChatMessageContent newMe
}
}

/// <inheritdoc />
/// <summary>
/// Asynchronously retrieves all messages in the thread.
/// </summary>
/// <param name="sortOrder">The order to return messages in.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The messages in the thread.</returns>
/// <exception cref="InvalidOperationException">The thread has been deleted.</exception>
[Experimental("SKEXP0110")]
public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync(MessageCollectionOrder? sortOrder = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (this.IsDeleted)
{
Expand All @@ -137,7 +143,7 @@ public async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync([EnumeratorCa
await this.CreateAsync(cancellationToken).ConfigureAwait(false);
}

await foreach (var message in AssistantThreadActions.GetMessagesAsync(this._client, this.Id!, MessageCollectionOrder.Ascending, cancellationToken).ConfigureAwait(false))
await foreach (var message in AssistantThreadActions.GetMessagesAsync(this._client, this.Id!, sortOrder, cancellationToken).ConfigureAwait(false))
{
yield return message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class AzureAIAgentFixture : AgentFixture
public override async Task<ChatHistory> GetChatHistory()
{
var chatHistory = new ChatHistory();
await foreach (var existingMessage in this._thread!.GetMessagesAsync().ConfigureAwait(false))
await foreach (var existingMessage in this._thread!.GetMessagesAsync(AAIP.ListSortOrder.Ascending).ConfigureAwait(false))
{
chatHistory.Add(existingMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class OpenAIAssistantAgentFixture : AgentFixture
public override async Task<ChatHistory> GetChatHistory()
{
var chatHistory = new ChatHistory();
await foreach (var existingMessage in this._thread!.GetMessagesAsync().ConfigureAwait(false))
await foreach (var existingMessage in this._thread!.GetMessagesAsync(MessageCollectionOrder.Ascending).ConfigureAwait(false))
{
chatHistory.Add(existingMessage);
}
Expand Down
Loading