Class ManagedConversationMemory

java.lang.Object
com.tnsai.memory.strategy.ManagedConversationMemory
All Implemented Interfaces:
MemoryStore

public class ManagedConversationMemory extends Object implements MemoryStore
Conversation memory with automatic strategy-based management.

ManagedConversationMemory wraps the standard MemoryStore interface and automatically applies a memory strategy when the token budget is exceeded.

Features

  • Automatic token budget management
  • Pluggable memory strategies
  • Thread-safe operations
  • Statistics tracking

Usage Example


 ManagedConversationMemory memory = ManagedConversationMemory.builder()
     .strategy(HybridStrategy.builder()
         .recentWindowSize(6)
         .build())
     .tokenBudget(TokenBudget.forModel(8192))
     .autoOptimize(true)
     .build();

 // Use like regular memory
 memory.addMessage("user", "Hello!");
 memory.addMessage("assistant", "Hi there!");

 // Get optimized history
 List<Map<String, Object>> history = memory.getHistory();
 
  • Method Details

    • builder

      public static ManagedConversationMemory.Builder builder()
    • init

      public void init(String agentId)
      Description copied from interface: MemoryStore
      Initializes the memory store for a specific agent.
      Specified by:
      init in interface MemoryStore
      Parameters:
      agentId - The unique ID of the agent
    • addMessage

      public void addMessage(String role, String content)
      Description copied from interface: MemoryStore
      Adds a message to the memory.
      Specified by:
      addMessage in interface MemoryStore
      Parameters:
      role - The role of the message sender (user, assistant, system)
      content - The text content of the message
    • addMessage

      public void addMessage(Map<String,Object> message)
      Description copied from interface: MemoryStore
      Adds a structured message (e.g. with tool calls) to memory.
      Specified by:
      addMessage in interface MemoryStore
      Parameters:
      message - The full message object
    • getHistory

      public List<Map<String,Object>> getHistory()
      Description copied from interface: MemoryStore
      Retrieves the full conversation history.
      Specified by:
      getHistory in interface MemoryStore
      Returns:
      List of message maps
    • getRecentHistory

      public List<Map<String,Object>> getRecentHistory(int limit)
      Description copied from interface: MemoryStore
      Retrieves the last N messages.
      Specified by:
      getRecentHistory in interface MemoryStore
      Parameters:
      limit - Number of messages to retrieve
      Returns:
      List of message maps
    • clear

      public void clear()
      Description copied from interface: MemoryStore
      Clears the memory.
      Specified by:
      clear in interface MemoryStore
    • prune

      public void prune(int maxTokens)
      Description copied from interface: MemoryStore
      Prunes the memory to keep it within a specific token limit. Removes oldest messages first, preserving system prompts if possible.
      Specified by:
      prune in interface MemoryStore
      Parameters:
      maxTokens - The maximum estimated tokens allowed
    • optimize

      public void optimize()
      Manually triggers optimization.
    • getRawHistory

      public List<Map<String,Object>> getRawHistory()
      Returns the raw (unoptimized) history.
    • getStrategy

      public MemoryStrategy getStrategy()
      Returns the current memory strategy.
    • getBudget

      public TokenBudget getBudget()
      Returns the token budget configuration.
    • getStats

      Returns memory statistics.