mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Remove AgentThread.GetMessagesAsync (#668)
* Remove AgentThread.GetMessagesAsync * Remove unecessary using
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
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.");
|
throw new InvalidOperationException("An agent service managed thread is not supported by this agent.");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ChatMessage> messagesList = [];
|
List<ChatMessage> messagesList = (await thread.MessageStore.GetMessagesAsync(cancellationToken).ConfigureAwait(false)).ToList();
|
||||||
await foreach (var threadMessage in thread.GetMessagesAsync(cancellationToken).ConfigureAwait(false))
|
|
||||||
{
|
|
||||||
messagesList.Add(threadMessage);
|
|
||||||
}
|
|
||||||
messagesList.AddRange(messages);
|
messagesList.AddRange(messages);
|
||||||
messages = messagesList;
|
messages = messagesList;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
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>
|
/// <summary>
|
||||||
/// Serializes the current object's state to a <see cref="JsonElement"/> using the specified serialization options.
|
/// Serializes the current object's state to a <see cref="JsonElement"/> using the specified serialization options.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public interface IChatMessageStore
|
|||||||
/// since they may contain state that is specific to a thread.
|
/// since they may contain state that is specific to a thread.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
Task<IEnumerable<ChatMessage>> GetMessagesAsync(CancellationToken cancellationToken);
|
Task<IEnumerable<ChatMessage>> GetMessagesAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds messages to the store.
|
/// Adds messages to the store.
|
||||||
@@ -42,7 +42,7 @@ public interface IChatMessageStore
|
|||||||
/// <param name="messages">The messages to add.</param>
|
/// <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>
|
/// <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>
|
/// <returns>An async task.</returns>
|
||||||
Task AddMessagesAsync(IReadOnlyCollection<ChatMessage> messages, CancellationToken cancellationToken);
|
Task AddMessagesAsync(IReadOnlyCollection<ChatMessage> messages, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deserializes the state contained in the provided <see cref="JsonElement"/> into the properties on this store.
|
/// 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.
|
// Add any existing messages from the thread to the messages to be sent to the chat client.
|
||||||
List<ChatMessage> threadMessages = [];
|
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.
|
// Add the input messages to the end of thread messages.
|
||||||
|
|||||||
-50
@@ -88,56 +88,6 @@ public class AgentThreadTests
|
|||||||
|
|
||||||
#endregion Constructor and Property Tests
|
#endregion Constructor and Property Tests
|
||||||
|
|
||||||
#region GetMessagesAsync Tests
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetMessagesAsyncReturnsEmptyListWhenNoStoreAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var thread = new AgentThread();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var messages = await ToListAsync(thread.GetMessagesAsync(CancellationToken.None));
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Empty(messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetMessagesAsyncReturnsEmptyListWhenAgentServiceIdAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var thread = new AgentThread { ConversationId = "thread-123" };
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var messages = await ToListAsync(thread.GetMessagesAsync(CancellationToken.None));
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Empty(messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetMessagesAsyncReturnsMessagesFromStoreAsync()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var store = new InMemoryChatMessageStore
|
|
||||||
{
|
|
||||||
new ChatMessage(ChatRole.User, "Hello"),
|
|
||||||
new ChatMessage(ChatRole.Assistant, "Hi there!")
|
|
||||||
};
|
|
||||||
var thread = new AgentThread { MessageStore = store };
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var messages = await ToListAsync(thread.GetMessagesAsync(CancellationToken.None));
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Equal(2, messages.Count);
|
|
||||||
Assert.Equal("Hello", messages[0].Text);
|
|
||||||
Assert.Equal("Hi there!", messages[1].Text);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion GetMessagesAsync Tests
|
|
||||||
|
|
||||||
#region OnNewMessagesAsync Tests
|
#region OnNewMessagesAsync Tests
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class OpenAIChatCompletionFixture : IChatClientAgentFixture
|
|||||||
|
|
||||||
public async Task<List<ChatMessage>> GetChatHistoryAsync(AgentThread thread)
|
public async Task<List<ChatMessage>> GetChatHistoryAsync(AgentThread thread)
|
||||||
{
|
{
|
||||||
return await thread.GetMessagesAsync().ToListAsync();
|
return thread.MessageStore is null ? [] : (await thread.MessageStore.GetMessagesAsync()).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<ChatClientAgent> CreateChatClientAgentAsync(
|
public Task<ChatClientAgent> CreateChatClientAgentAsync(
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ public class OpenAIResponseFixture(bool store) : IChatClientAgentFixture
|
|||||||
return [.. previousMessages, responseMessage];
|
return [.. previousMessages, responseMessage];
|
||||||
}
|
}
|
||||||
|
|
||||||
return await thread.GetMessagesAsync().ToListAsync();
|
return thread.MessageStore is null ? [] : (await thread.MessageStore.GetMessagesAsync()).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ChatMessage ConvertToChatMessage(ResponseItem item)
|
private static ChatMessage ConvertToChatMessage(ResponseItem item)
|
||||||
|
|||||||
Reference in New Issue
Block a user