AgentSessionExtensions.TryGetInMemoryChatHistory Method

Definition

Attempts to retrieve the in-memory chat history messages associated with the specified agent session, if the agent is storing memories in the session using the InMemoryChatHistoryProvider

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

Parameters

session
AgentSession

The agent session from which to retrieve in-memory chat history.

messages
List<ChatMessage>

When this method returns, contains the list of chat history messages if available; otherwise, null.

stateKey
String

An optional key used to identify the chat history state in the session's state bag. If null, the default key for in-memory chat history is used.

jsonSerializerOptions
JsonSerializerOptions

Optional JSON serializer options to use when accessing the session state. If null, default options are used.

Returns

true if the in-memory chat history messages were found and retrieved; false otherwise.

Remarks

This method is only applicable when using InMemoryChatHistoryProvider and if the service does not require in-service chat history storage.

Applies to