.NET: Remove AgentThread.GetMessagesAsync (#668)

* Remove AgentThread.GetMessagesAsync

* Remove unecessary using
This commit is contained in:
westey
2025-09-09 17:40:07 +01:00
committed by GitHub
Unverified
parent 5c3c2fe634
commit ec5ea3c8a8
7 changed files with 8 additions and 79 deletions
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading;
@@ -75,11 +76,7 @@ public abstract partial class OrchestratingAgent : AIAgent
throw new InvalidOperationException("An agent service managed thread is not supported by this agent.");
}
List<ChatMessage> messagesList = [];
await foreach (var threadMessage in thread.GetMessagesAsync(cancellationToken).ConfigureAwait(false))
{
messagesList.Add(threadMessage);
}
List<ChatMessage> messagesList = (await thread.MessageStore.GetMessagesAsync(cancellationToken).ConfigureAwait(false)).ToList();
messagesList.AddRange(messages);
messages = messagesList;
}
@@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
@@ -109,23 +108,6 @@ public class AgentThread
}
}
/// <summary>
/// Retrieves any messages stored in the <see cref="IChatMessageStore"/> of the thread, otherwise returns an empty collection.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The messages from the <see cref="IChatMessageStore"/> in ascending chronological order, with the oldest message first.</returns>
public virtual async IAsyncEnumerable<ChatMessage> GetMessagesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (this._messageStore is not null)
{
var messages = await this._messageStore!.GetMessagesAsync(cancellationToken).ConfigureAwait(false);
foreach (var message in messages)
{
yield return message;
}
}
}
/// <summary>
/// Serializes the current object's state to a <see cref="JsonElement"/> using the specified serialization options.
/// </summary>
@@ -34,7 +34,7 @@ public interface IChatMessageStore
/// since they may contain state that is specific to a thread.
/// </para>
/// </remarks>
Task<IEnumerable<ChatMessage>> GetMessagesAsync(CancellationToken cancellationToken);
Task<IEnumerable<ChatMessage>> GetMessagesAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Adds messages to the store.
@@ -42,7 +42,7 @@ public interface IChatMessageStore
/// <param name="messages">The messages to add.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An async task.</returns>
Task AddMessagesAsync(IReadOnlyCollection<ChatMessage> messages, CancellationToken cancellationToken);
Task AddMessagesAsync(IReadOnlyCollection<ChatMessage> messages, CancellationToken cancellationToken = default);
/// <summary>
/// Deserializes the state contained in the provided <see cref="JsonElement"/> into the properties on this store.
@@ -344,9 +344,9 @@ public sealed class ChatClientAgent : AIAgent
// Add any existing messages from the thread to the messages to be sent to the chat client.
List<ChatMessage> threadMessages = [];
await foreach (ChatMessage message in thread.GetMessagesAsync(cancellationToken).ConfigureAwait(false))
if (thread.MessageStore is not null)
{
threadMessages.Add(message);
threadMessages.AddRange(await thread.MessageStore.GetMessagesAsync(cancellationToken).ConfigureAwait(false));
}
// Add the input messages to the end of thread messages.