Class TruncatingCompactor

java.lang.Object
com.tnsai.context.compaction.TruncatingCompactor
All Implemented Interfaces:
ContextCompactor

public class TruncatingCompactor extends Object implements ContextCompactor
Phase-1 compactor that truncates large tool-call arguments in older messages.

This compactor reduces context size without making any LLM calls by finding messages with large tool-call argument strings and replacing them with a truncated placeholder. Only messages outside the "preserve" window are modified.

What Gets Truncated

  • Tool-call argument values exceeding maxArgLength characters
  • Tool-result content exceeding maxArgLength characters
  • Only messages outside the preserved tail (last N messages)

Usage


 TruncatingCompactor compactor = new TruncatingCompactor(200);
 CompactionConfig config = CompactionConfig.builder()
     .preserveLastN(5)
     .minMessagesForCompaction(5)
     .build();
 CompactionResult result = compactor.compact(messages, config);
 
Since:
0.2.3
Author:
TnsAI Team
See Also:
  • Field Details

    • DEFAULT_MAX_ARG_LENGTH

      public static final int DEFAULT_MAX_ARG_LENGTH
      Default maximum argument length before truncation.
      See Also:
  • Constructor Details

    • TruncatingCompactor

      public TruncatingCompactor()
      Creates a truncating compactor with the default max argument length (200 chars).
    • TruncatingCompactor

      public TruncatingCompactor(int maxArgLength)
      Creates a truncating compactor with the given max argument length.
      Parameters:
      maxArgLength - maximum characters before truncation
      Throws:
      IllegalArgumentException - if maxArgLength is less than 1
  • Method Details

    • compact

      public CompactionResult compact(List<Map<String,Object>> messages, CompactionConfig config)
      Description copied from interface: ContextCompactor
      Compacts the given messages according to configuration.
      Specified by:
      compact in interface ContextCompactor
      Parameters:
      messages - the conversation messages to compact
      config - the compaction configuration
      Returns:
      the compaction result
    • estimateTokens

      public int estimateTokens(List<Map<String,Object>> messages)
      Description copied from interface: ContextCompactor
      Estimates token count for a list of messages.
      Specified by:
      estimateTokens in interface ContextCompactor
      Parameters:
      messages - the messages to estimate
      Returns:
      estimated token count
    • getMaxArgLength

      public int getMaxArgLength()
      Returns the configured max argument length.
      Returns:
      the max argument length threshold