Package com.tnsai.memory.strategy
Class HybridStrategy
java.lang.Object
com.tnsai.memory.strategy.HybridStrategy
- All Implemented Interfaces:
MemoryStrategy
Hybrid memory strategy combining sliding window and summarization.
This strategy uses a two-tier approach:
- Recent messages are kept in full (sliding window)
- Older messages are summarized (summary strategy)
How It Works
Before: [M1, M2, M3, M4, M5, M6, M7, M8, M9, M10] (10 messages)
After: [Summary(M1-M4), M5, M6, M7, M8, M9, M10] (7 items)
└─ Summarized ─┘ └── Recent (kept) ──┘
Strategy Phases
1. Try to fit all messages
2. If over budget:
a. Keep recent N messages (recentWindowSize)
b. Summarize messages before the window
c. If still over budget, apply sliding window
Usage Example
MemoryStrategy strategy = HybridStrategy.builder()
.recentWindowSize(6)
.summaryStrategy(SummaryStrategy.builder()
.summarizer(llmClient)
.build())
.build();
List<Map<String, Object>> optimized = strategy.apply(history, budget);
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionApplies the strategy to the conversation history.static HybridStrategy.Builderbuilder()Returns a description of what this strategy does.getName()Returns the name of this strategy.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.memory.strategy.MemoryStrategy
estimateTokens, estimateTokens
-
Method Details
-
builder
-
apply
Description copied from interface:MemoryStrategyApplies the strategy to the conversation history.- Specified by:
applyin interfaceMemoryStrategy- Parameters:
history- The full conversation historybudget- The token budget configuration- Returns:
- Optimized history within the token budget
-
getName
Description copied from interface:MemoryStrategyReturns the name of this strategy.- Specified by:
getNamein interfaceMemoryStrategy
-
getDescription
Description copied from interface:MemoryStrategyReturns a description of what this strategy does.- Specified by:
getDescriptionin interfaceMemoryStrategy
-