Interface SessionMemory

All Known Implementing Classes:
SessionMemoryClient

public interface SessionMemory
Interface for managing contextual session history between users and AI models.

SessionMemory provides functionality to store, retrieve, and manage messages exchanged during interactions in an agent system. It enables agents to maintain context across multiple interactions within the same session.

Default Implementation: The default implementation is backed by SessionMemoryEntity, a built-in Event Sourced Entity that automatically stores contextual history. This provides durability and allows for session memory to be shared between multiple agents using the same session id.

Custom Implementation: You can provide a custom implementation using MemoryProvider.custom(SessionMemory) to store session memory in external databases or services.

Memory Management: Session memory can be configured to limit the amount of history retained, either by message count or total size, to control token usage and performance.

  • Method Details

    • addInteraction

      void addInteraction(String sessionId, SessionMessage.UserMessage userMessage, List<SessionMessage> messages)
      Adds an interaction to the session history for the specified session.

      When userMessage is non-null, the user message is persisted followed by the response messages. When userMessage is null, only the response messages are persisted (a partial interaction, such as tool call responses completing a previous AI message).

      Parameters:
      sessionId - The unique identifier for the contextual session
      userMessage - The content of the user message, or null for partial interactions
      messages - All other messages generated during this interaction, typically AiMessage but also Tool Call responses.
    • addInteraction

      void addInteraction(String sessionId, SessionMessage.MultimodalUserMessage userMessage, List<SessionMessage> messages)
      Adds an interaction between a user and an AI model to the session history for the specified session, supporting multimodal content.

      This overload accepts a SessionMessage.MultimodalUserMessage which can contain multiple content types including text and images, enabling multimodal interactions.

      Parameters:
      sessionId - The unique identifier for the contextual session
      userMessage - The user message containing multimodal content (text, images, etc.)
      messages - All other messages generated during this interaction, typically AiMessage but also Tool Call responses.
    • getHistory

      SessionHistory getHistory(String sessionId)
      Retrieves the complete session history for the specified session. For very long sessions, this might return a compacted version of the history.
      Parameters:
      sessionId - The unique identifier for the contextual session
      Returns:
      The complete session history containing all messages