Package com.tnsai.memory.strategy
Class ManagedConversationMemory
java.lang.Object
com.tnsai.memory.strategy.ManagedConversationMemory
- All Implemented Interfaces:
MemoryStore
Conversation memory with automatic strategy-based management.
ManagedConversationMemory wraps the standard MemoryStore interface and automatically applies a memory strategy when the token budget is exceeded.
Features
- Automatic token budget management
- Pluggable memory strategies
- Thread-safe operations
- Statistics tracking
Usage Example
ManagedConversationMemory memory = ManagedConversationMemory.builder()
.strategy(HybridStrategy.builder()
.recentWindowSize(6)
.build())
.tokenBudget(TokenBudget.forModel(8192))
.autoOptimize(true)
.build();
// Use like regular memory
memory.addMessage("user", "Hello!");
memory.addMessage("assistant", "Hi there!");
// Get optimized history
List<Map<String, Object>> history = memory.getHistory();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for ManagedConversationMemory.static final recordMemory statistics. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessage(String role, String content) Adds a message to the memory.voidaddMessage(Map<String, Object> message) Adds a structured message (e.g. with tool calls) to memory.builder()voidclear()Clears the memory.Returns the token budget configuration.Retrieves the full conversation history.Returns the raw (unoptimized) history.getRecentHistory(int limit) Retrieves the last N messages.getStats()Returns memory statistics.Returns the current memory strategy.voidInitializes the memory store for a specific agent.voidoptimize()Manually triggers optimization.voidprune(int maxTokens) Prunes the memory to keep it within a specific token limit.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.memory.MemoryStore
replaceHistory, search
-
Method Details
-
builder
-
init
Description copied from interface:MemoryStoreInitializes the memory store for a specific agent.- Specified by:
initin interfaceMemoryStore- Parameters:
agentId- The unique ID of the agent
-
addMessage
Description copied from interface:MemoryStoreAdds a message to the memory.- Specified by:
addMessagein interfaceMemoryStore- Parameters:
role- The role of the message sender (user, assistant, system)content- The text content of the message
-
addMessage
Description copied from interface:MemoryStoreAdds a structured message (e.g. with tool calls) to memory.- Specified by:
addMessagein interfaceMemoryStore- Parameters:
message- The full message object
-
getHistory
Description copied from interface:MemoryStoreRetrieves the full conversation history.- Specified by:
getHistoryin interfaceMemoryStore- Returns:
- List of message maps
-
getRecentHistory
Description copied from interface:MemoryStoreRetrieves the last N messages.- Specified by:
getRecentHistoryin interfaceMemoryStore- Parameters:
limit- Number of messages to retrieve- Returns:
- List of message maps
-
clear
public void clear()Description copied from interface:MemoryStoreClears the memory.- Specified by:
clearin interfaceMemoryStore
-
prune
public void prune(int maxTokens) Description copied from interface:MemoryStorePrunes the memory to keep it within a specific token limit. Removes oldest messages first, preserving system prompts if possible.- Specified by:
prunein interfaceMemoryStore- Parameters:
maxTokens- The maximum estimated tokens allowed
-
optimize
public void optimize()Manually triggers optimization. -
getRawHistory
Returns the raw (unoptimized) history. -
getStrategy
Returns the current memory strategy. -
getBudget
Returns the token budget configuration. -
getStats
Returns memory statistics.
-