Package com.tnsai.context.compaction
Class TruncatingCompactor
java.lang.Object
com.tnsai.context.compaction.TruncatingCompactor
- All Implemented Interfaces:
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
maxArgLengthcharacters - Tool-result content exceeding
maxArgLengthcharacters - 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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum argument length before truncation. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a truncating compactor with the default max argument length (200 chars).TruncatingCompactor(int maxArgLength) Creates a truncating compactor with the given max argument length. -
Method Summary
Modifier and TypeMethodDescriptionCompacts the given messages according to configuration.intestimateTokens(List<Map<String, Object>> messages) Estimates token count for a list of messages.intReturns the configured max argument length.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.context.compaction.ContextCompactor
shouldCompact
-
Field Details
-
DEFAULT_MAX_ARG_LENGTH
public static final int DEFAULT_MAX_ARG_LENGTHDefault 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
Description copied from interface:ContextCompactorCompacts the given messages according to configuration.- Specified by:
compactin interfaceContextCompactor- Parameters:
messages- the conversation messages to compactconfig- the compaction configuration- Returns:
- the compaction result
-
estimateTokens
Description copied from interface:ContextCompactorEstimates token count for a list of messages.- Specified by:
estimateTokensin interfaceContextCompactor- 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
-