Package com.tnsai.memory
Interface MemoryStore
- All Known Implementing Classes:
AbstractMemoryStore,FileMemoryStore,InMemoryStore,ManagedConversationMemory,NamespacedMemoryStore,SharedMemoryStore,VectorSearchMemoryStore
public interface MemoryStore
Abstraction for agent memory (conversation history).
Supports storage, retrieval, semantic search, and pruning.
-
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.voidclear()Clears the memory.Retrieves the full conversation history.getRecentHistory(int limit) Retrieves the last N messages.voidInitializes the memory store for a specific agent.voidprune(int maxTokens) Prunes the memory to keep it within a specific token limit.default voidreplaceHistory(List<Map<String, Object>> newHistory) Atomically replaces the entire conversation history with the given list.Performs a semantic search on the memory (Optional).
-
Method Details
-
init
Initializes the memory store for a specific agent.- Parameters:
agentId- The unique ID of the agent
-
addMessage
Adds a message to the memory.- Parameters:
role- The role of the message sender (user, assistant, system)content- The text content of the message
-
addMessage
Adds a structured message (e.g. with tool calls) to memory.- Parameters:
message- The full message object
-
getHistory
Retrieves the full conversation history.- Returns:
- List of message maps
-
getRecentHistory
Retrieves the last N messages.- Parameters:
limit- Number of messages to retrieve- Returns:
- List of message maps
-
clear
void clear()Clears the memory. -
prune
void prune(int maxTokens) Prunes the memory to keep it within a specific token limit. Removes oldest messages first, preserving system prompts if possible.- Parameters:
maxTokens- The maximum estimated tokens allowed
-
replaceHistory
Atomically replaces the entire conversation history with the given list.Used by
MemoryConsolidationPipelineto swap in a consolidated history (summary + preserved messages) in a single call, minimizing the window in which concurrently added messages could be lost (TAN-5180).The default implementation falls back to
clear()followed byaddMessage(Map)for each message — behaviorally identical to the legacy rewrite, so existing stores remain source- and behavior-compatible. Implementations with an internal lock (e.g. those based onAbstractMemoryStore) SHOULD override this to perform the swap under a single lock hold, making the rewrite atomic with respect to concurrent writers.- Parameters:
newHistory- the complete new history, in order (must not be null)- Since:
- 0.13.0
-
search
Performs a semantic search on the memory (Optional). Useful for RAG (Retrieval-Augmented Generation).- Parameters:
query- The search querylimit- Max results- Returns:
- List of relevant memory segments
-