ChatHistoryProvider.StoreChatHistoryAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
When overridden in a derived class, adds new messages to the chat history at the end of the agent invocation.
protected virtual System.Threading.Tasks.ValueTask StoreChatHistoryAsync(Microsoft.Agents.AI.ChatHistoryProvider.InvokedContext context, System.Threading.CancellationToken cancellationToken = default);
abstract member StoreChatHistoryAsync : Microsoft.Agents.AI.ChatHistoryProvider.InvokedContext * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
override this.StoreChatHistoryAsync : Microsoft.Agents.AI.ChatHistoryProvider.InvokedContext * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
Protected Overridable Function StoreChatHistoryAsync (context As ChatHistoryProvider.InvokedContext, Optional cancellationToken As CancellationToken = Nothing) As ValueTask
Parameters
Contains the invocation context including request messages, response messages, and any exception that occurred.
- cancellationToken
- CancellationToken
The CancellationToken to monitor for cancellation requests. The default is None.
Returns
A task that represents the asynchronous add operation.
Remarks
Messages should be added in the order they were generated to maintain proper chronological sequence. The ChatHistoryProvider is responsible for preserving message ordering and ensuring that subsequent calls to InvokingCoreAsync(ChatHistoryProvider+InvokingContext, CancellationToken) return messages in the correct chronological order.
Implementations may perform additional processing during message addition, such as:
- Validating message content and metadata
- Applying storage optimizations or compression
- Triggering background maintenance operations
This method is called from InvokedCoreAsync(ChatHistoryProvider+InvokedContext, CancellationToken). Note that InvokedCoreAsync(ChatHistoryProvider+InvokedContext, CancellationToken) can be overridden to directly control message filtering and error handling, in which case it is up to the implementer to call this method as needed to store messages.
In contrast with InvokedCoreAsync(ChatHistoryProvider+InvokedContext, CancellationToken), this method only stores messages, while InvokedCoreAsync(ChatHistoryProvider+InvokedContext, CancellationToken) is also responsible for messages filtering and error handling.
The default implementation of InvokedCoreAsync(ChatHistoryProvider+InvokedContext, CancellationToken) only calls this method if the invocation succeeded.