AgentSessionExtensions.SetInMemoryChatHistory Method

Definition

Sets the in-memory chat message history for the specified agent session, replacing any existing messages.

public static void SetInMemoryChatHistory(this Microsoft.Agents.AI.AgentSession session, System.Collections.Generic.List<Microsoft.Extensions.AI.ChatMessage> messages, string? stateKey = default, System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = default);
static member SetInMemoryChatHistory : Microsoft.Agents.AI.AgentSession * System.Collections.Generic.List<Microsoft.Extensions.AI.ChatMessage> * string * System.Text.Json.JsonSerializerOptions -> unit
<Extension()>
Public Sub SetInMemoryChatHistory (session As AgentSession, messages As List(Of ChatMessage), Optional stateKey As String = Nothing, Optional jsonSerializerOptions As JsonSerializerOptions = Nothing)

Parameters

session
AgentSession

The agent session whose in-memory chat history will be updated.

messages
List<ChatMessage>

The list of chat messages to store in memory for the session. Replaces any existing messages for the specified state key.

stateKey
String

The key used to identify the in-memory chat history within the session's state bag. If null, a default key is used.

jsonSerializerOptions
JsonSerializerOptions

The serializer options used when accessing or storing the state. If null, default options are applied.

Remarks

This method is only applicable when using InMemoryChatHistoryProvider and if the service does not require in-service chat history storage. If messages are set, but a different ChatHistoryProvider is used, or if chat history is stored in the underlying AI service, the messages will be ignored.

Applies to