Class DefaultLLMMemoryManager

java.lang.Object
dev.agenor.runtime.memory.llm.DefaultLLMMemoryManager
All Implemented Interfaces:
LLMMemoryManager

public class DefaultLLMMemoryManager extends Object implements LLMMemoryManager
Default implementation of LLMMemoryManager.
Since:
0.6.0
  • Constructor Details

  • Method Details

    • addMessage

      public CompletableFuture<Void> addMessage(LLMMessage message)
      Description copied from interface: LLMMemoryManager
      Add a message to the conversation history.

      The message is stored in both short-term (conversation) and optionally long-term (MemoryStore) memory depending on importance.

      Example:

      
       llmMemory.addMessage(LLMMessage.user("What's the weather?"))
           .thenRun(() -> log.info("Message added"));
       
      Specified by:
      addMessage in interface LLMMemoryManager
      Parameters:
      message - the LLM message to add
      Returns:
      future that completes when message is stored
    • addMessages

      public CompletableFuture<Void> addMessages(List<LLMMessage> messages)
      Description copied from interface: LLMMemoryManager
      Add multiple messages to the conversation history.

      More efficient than calling addMessage() multiple times.

      Specified by:
      addMessages in interface LLMMemoryManager
      Parameters:
      messages - the messages to add
      Returns:
      future that completes when all messages are stored
    • getConversationHistory

      public CompletableFuture<List<LLMMessage>> getConversationHistory(int maxTokens, ContextWindowStrategy strategy)
      Description copied from interface: LLMMemoryManager
      Get conversation history for LLM prompt with token budget.

      Uses the specified strategy to select which messages to include within the token budget. Older messages may be summarized or excluded.

      Strategies:

      Strategy implementations determine which messages to include:

      • Fixed window - Last N messages that fit in budget
      • Sliding window - Most recent + important messages
      • Summarized - Recent messages + summary of old ones

      Example:

      
       // Obtain strategy from runtime (e.g., via dependency injection)
       ContextWindowStrategy strategy = ...; // Provided by runtime
      
       // Get conversation with token budget and strategy
       List<LLMMessage> history = llmMemory.getConversationHistory(
           2000,      // Max tokens
           strategy   // Selection strategy
       ).join();
       
      Specified by:
      getConversationHistory in interface LLMMemoryManager
      Parameters:
      maxTokens - maximum tokens for the context
      strategy - strategy for selecting messages
      Returns:
      future with list of messages that fit in budget
    • getAllMessages

      public CompletableFuture<List<LLMMessage>> getAllMessages()
      Description copied from interface: LLMMemoryManager
      Get all messages in conversation history (no token limit).

      Warning: This may return many messages. Use with caution and prefer token-limited methods for LLM prompts.

      Specified by:
      getAllMessages in interface LLMMemoryManager
      Returns:
      future with all conversation messages
    • remember

      public CompletableFuture<Void> remember(String key, String content, Map<String,Object> metadata)
      Description copied from interface: LLMMemoryManager
      Store important context in long-term memory.

      This stores a key-value pair in the MemoryStore for later retrieval. Use this for facts, preferences, or important information that should persist beyond the current conversation.

      Example:

      
       llmMemory.remember("user-name", "Alice", Map.of(
           "category", "profile",
           "confidence", "high"
       ));
       
      Specified by:
      remember in interface LLMMemoryManager
      Parameters:
      key - the memory key
      content - the content to remember
      metadata - optional metadata for the memory
      Returns:
      future that completes when stored
    • retrieveRelevantContext

      public CompletableFuture<List<MemoryEntry>> retrieveRelevantContext(String query, int maxTokens)
      Description copied from interface: LLMMemoryManager
      Retrieve relevant context from long-term memory.

      Searches the MemoryStore for entries relevant to the query and returns them formatted for LLM context. Results are limited by token budget.

      Example:

      
       // Get user preferences (up to 500 tokens)
       List<MemoryEntry> context = llmMemory.retrieveRelevantContext(
           "user preferences",
           500
       ).join();
       
      Specified by:
      retrieveRelevantContext in interface LLMMemoryManager
      Parameters:
      query - the search query
      maxTokens - maximum tokens for retrieved context
      Returns:
      future with relevant memory entries
    • summarizeOldMessages

      public CompletableFuture<String> summarizeOldMessages(int messagesToSummarize)
      Description copied from interface: LLMMemoryManager
      Summarize old messages to save tokens.

      Takes the oldest N messages and creates a summary using the LLM. The original messages are replaced with a single summary message.

      Example:

      
       // Summarize oldest 20 messages
       String summary = llmMemory.summarizeOldMessages(20).join();
       System.out.println("Summary: " + summary);
       
      Specified by:
      summarizeOldMessages in interface LLMMemoryManager
      Parameters:
      messagesToSummarize - number of oldest messages to summarize
      Returns:
      future with summary text
    • clearConversationHistory

      public CompletableFuture<Void> clearConversationHistory()
      Description copied from interface: LLMMemoryManager
      Clear conversation history (short-term memory).

      Removes all messages from the current conversation. Long-term memories (from LLMMemoryManager.remember(java.lang.String, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>)) are not affected.

      Specified by:
      clearConversationHistory in interface LLMMemoryManager
      Returns:
      future that completes when history is cleared
    • getCurrentTokenCount

      public int getCurrentTokenCount()
      Description copied from interface: LLMMemoryManager
      Get current token count of conversation history.

      Returns the estimated total tokens used by all messages in the conversation history.

      Specified by:
      getCurrentTokenCount in interface LLMMemoryManager
      Returns:
      current token count
    • getMessageCount

      public int getMessageCount()
      Description copied from interface: LLMMemoryManager
      Get number of messages in conversation history.
      Specified by:
      getMessageCount in interface LLMMemoryManager
      Returns:
      message count
    • getTokenEstimator

      public TokenEstimator getTokenEstimator()
      Description copied from interface: LLMMemoryManager
      Get the token estimator used by this manager.
      Specified by:
      getTokenEstimator in interface LLMMemoryManager
      Returns:
      the token estimator
    • getAgentId

      public String getAgentId()
      Description copied from interface: LLMMemoryManager
      Get the agent ID this manager is associated with.
      Specified by:
      getAgentId in interface LLMMemoryManager
      Returns:
      the agent ID
    • toString

      public String toString()
      Overrides:
      toString in class Object