Interface ContextCompactor

All Known Implementing Classes:
LLMContextCompactor, TruncatingCompactor, TwoPhaseCompactor

public interface ContextCompactor
Interface for context compaction strategies.

Compactors reduce the conversation context size when it approaches the token limit, enabling long-running sessions.

Implementation Example


 public class LLMContextCompactor implements ContextCompactor {
     private final LLMClient llm;

     @Override
     public CompactionResult compact(List<Map<String, Object>> messages, CompactionConfig config) {
         // Separate preserved and compactable messages
         // Summarize compactable messages using LLM
         // Return result with summary and preserved messages
     }
 }
 
Author:
TnsAI Team
See Also:
  • Method Details

    • compact

      CompactionResult compact(List<Map<String,Object>> messages, CompactionConfig config)
      Compacts the given messages according to configuration.
      Parameters:
      messages - the conversation messages to compact
      config - the compaction configuration
      Returns:
      the compaction result
    • shouldCompact

      default boolean shouldCompact(int currentTokens, int maxTokens, CompactionConfig config)
      Checks if compaction is needed based on current token count.
      Parameters:
      currentTokens - current token count in context
      maxTokens - maximum allowed tokens
      config - the compaction configuration
      Returns:
      true if compaction should be triggered
    • estimateTokens

      int estimateTokens(List<Map<String,Object>> messages)
      Estimates token count for a list of messages.
      Parameters:
      messages - the messages to estimate
      Returns:
      estimated token count