Package com.tnsai.context.compaction
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 Summary
Modifier and TypeMethodDescriptionCompacts the given messages according to configuration.intestimateTokens(List<Map<String, Object>> messages) Estimates token count for a list of messages.default booleanshouldCompact(int currentTokens, int maxTokens, CompactionConfig config) Checks if compaction is needed based on current token count.
-
Method Details
-
compact
Compacts the given messages according to configuration.- Parameters:
messages- the conversation messages to compactconfig- the compaction configuration- Returns:
- the compaction result
-
shouldCompact
Checks if compaction is needed based on current token count.- Parameters:
currentTokens- current token count in contextmaxTokens- maximum allowed tokensconfig- the compaction configuration- Returns:
- true if compaction should be triggered
-
estimateTokens
Estimates token count for a list of messages.- Parameters:
messages- the messages to estimate- Returns:
- estimated token count
-