.NET: Rename ChatMessageStore to ChatHistoryProvider (#3375)

* Rename ChatMessageStore to ChatHistoryProvider

* Fix merge issue

* Fixed PR comments

* Fix tests after property rename

* Add unit tests and fix merge issues

* Fix encoding
This commit is contained in:
westey
2026-01-23 15:49:01 +00:00
committed by GitHub
parent 9e57dc7dbe
commit 21e00c054b
45 changed files with 1037 additions and 904 deletions
@@ -9,11 +9,11 @@ using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI;
/// <summary>
/// Provides an abstract base class for agent threads that maintain all conversation state in local memory.
/// Provides an abstract base class for an <see cref="AgentThread"/> that maintain all chat history in local memory.
/// </summary>
/// <remarks>
/// <para>
/// <see cref="InMemoryAgentThread"/> is designed for scenarios where conversation state should be stored locally
/// <see cref="InMemoryAgentThread"/> is designed for scenarios where chat history should be stored locally
/// rather than in external services or databases. This approach provides high performance and simplicity while
/// maintaining full control over the conversation data.
/// </para>
@@ -28,17 +28,17 @@ public abstract class InMemoryAgentThread : AgentThread
/// <summary>
/// Initializes a new instance of the <see cref="InMemoryAgentThread"/> class.
/// </summary>
/// <param name="messageStore">
/// An optional <see cref="InMemoryChatMessageStore"/> instance to use for storing chat messages.
/// If <see langword="null"/>, a new empty message store will be created.
/// <param name="chatHistoryProvider">
/// An optional <see cref="InMemoryChatHistoryProvider"/> instance to use for storing chat messages.
/// If <see langword="null"/>, a new empty <see cref="InMemoryChatHistoryProvider"/> will be created.
/// </param>
/// <remarks>
/// This constructor allows sharing of message stores between threads or providing pre-configured
/// message stores with specific reduction or processing logic.
/// This constructor allows sharing of <see cref="ChatHistoryProvider"/> between threads or providing pre-configured
/// <see cref="ChatHistoryProvider"/> with specific reduction or processing logic.
/// </remarks>
protected InMemoryAgentThread(InMemoryChatMessageStore? messageStore = null)
protected InMemoryAgentThread(InMemoryChatHistoryProvider? chatHistoryProvider = null)
{
this.MessageStore = messageStore ?? [];
this.ChatHistoryProvider = chatHistoryProvider ?? [];
}
/// <summary>
@@ -52,7 +52,7 @@ public abstract class InMemoryAgentThread : AgentThread
/// </remarks>
protected InMemoryAgentThread(IEnumerable<ChatMessage> messages)
{
this.MessageStore = [.. messages];
this.ChatHistoryProvider = [.. messages];
}
/// <summary>
@@ -60,9 +60,9 @@ public abstract class InMemoryAgentThread : AgentThread
/// </summary>
/// <param name="serializedThreadState">A <see cref="JsonElement"/> representing the serialized state of the thread.</param>
/// <param name="jsonSerializerOptions">Optional settings for customizing the JSON deserialization process.</param>
/// <param name="messageStoreFactory">
/// Optional factory function to create the <see cref="InMemoryChatMessageStore"/> from its serialized state.
/// If not provided, a default factory will be used that creates a basic in-memory store.
/// <param name="chatHistoryProviderFactory">
/// Optional factory function to create the <see cref="InMemoryChatHistoryProvider"/> from its serialized state.
/// If not provided, a default factory will be used that creates a basic <see cref="InMemoryChatHistoryProvider"/>.
/// </param>
/// <exception cref="ArgumentException">The <paramref name="serializedThreadState"/> is not a JSON object.</exception>
/// <exception cref="JsonException">The <paramref name="serializedThreadState"/> is invalid or cannot be deserialized to the expected type.</exception>
@@ -73,7 +73,7 @@ public abstract class InMemoryAgentThread : AgentThread
protected InMemoryAgentThread(
JsonElement serializedThreadState,
JsonSerializerOptions? jsonSerializerOptions = null,
Func<JsonElement, JsonSerializerOptions?, InMemoryChatMessageStore>? messageStoreFactory = null)
Func<JsonElement, JsonSerializerOptions?, InMemoryChatHistoryProvider>? chatHistoryProviderFactory = null)
{
if (serializedThreadState.ValueKind != JsonValueKind.Object)
{
@@ -83,15 +83,15 @@ public abstract class InMemoryAgentThread : AgentThread
var state = serializedThreadState.Deserialize(
AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(InMemoryAgentThreadState))) as InMemoryAgentThreadState;
this.MessageStore =
messageStoreFactory?.Invoke(state?.StoreState ?? default, jsonSerializerOptions) ??
new InMemoryChatMessageStore(state?.StoreState ?? default, jsonSerializerOptions);
this.ChatHistoryProvider =
chatHistoryProviderFactory?.Invoke(state?.ChatHistoryProviderState ?? default, jsonSerializerOptions) ??
new InMemoryChatHistoryProvider(state?.ChatHistoryProviderState ?? default, jsonSerializerOptions);
}
/// <summary>
/// Gets or sets the <see cref="InMemoryChatMessageStore"/> used by this thread.
/// Gets or sets the <see cref="InMemoryChatHistoryProvider"/> used by this thread.
/// </summary>
public InMemoryChatMessageStore MessageStore { get; }
public InMemoryChatHistoryProvider ChatHistoryProvider { get; }
/// <summary>
/// Serializes the current object's state to a <see cref="JsonElement"/> using the specified serialization options.
@@ -100,11 +100,11 @@ public abstract class InMemoryAgentThread : AgentThread
/// <returns>A <see cref="JsonElement"/> representation of the object's state.</returns>
public override JsonElement Serialize(JsonSerializerOptions? jsonSerializerOptions = null)
{
var storeState = this.MessageStore.Serialize(jsonSerializerOptions);
var chatHistoryProviderState = this.ChatHistoryProvider.Serialize(jsonSerializerOptions);
var state = new InMemoryAgentThreadState
{
StoreState = storeState,
ChatHistoryProviderState = chatHistoryProviderState,
};
return JsonSerializer.SerializeToElement(state, AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(InMemoryAgentThreadState)));
@@ -112,13 +112,13 @@ public abstract class InMemoryAgentThread : AgentThread
/// <inheritdoc/>
public override object? GetService(Type serviceType, object? serviceKey = null) =>
base.GetService(serviceType, serviceKey) ?? this.MessageStore?.GetService(serviceType, serviceKey);
base.GetService(serviceType, serviceKey) ?? this.ChatHistoryProvider?.GetService(serviceType, serviceKey);
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"Count = {this.MessageStore.Count}";
private string DebuggerDisplay => $"Count = {this.ChatHistoryProvider.Count}";
internal sealed class InMemoryAgentThreadState
{
public JsonElement? StoreState { get; set; }
public JsonElement? ChatHistoryProviderState { get; set; }
}
}