Package com.tnsai.memory
Class AbstractMemoryStore
java.lang.Object
com.tnsai.memory.AbstractMemoryStore
- All Implemented Interfaces:
MemoryStore
- Direct Known Subclasses:
FileMemoryStore,InMemoryStore,NamespacedMemoryStore
Abstract base class for memory stores.
Provides common functionality for history management, reducing code duplication.
Uses LinkedList for O(1) removal at head during pruning operations, which is more efficient than CopyOnWriteArrayList.remove(0) which is O(n).
Thread safety notes:
- All mutating operations are guarded by a
ReentrantLock(Virtual Thread friendly, avoids carrier thread pinning) - Read operations return unmodifiable views
- Subclasses should call super methods and handle persistence in overridden methods
Subclasses only need to implement:
onHistoryChanged()- Called after any history modificationloadHistory()- Called during init to load persisted history
-
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.Performs TF-IDF based semantic search over conversation history.intsize()Returns the current size of the history.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.memory.MemoryStore
replaceHistory
-
Method Details
-
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
-
search
Performs TF-IDF based semantic search over conversation history.This implementation uses term frequency-inverse document frequency (TF-IDF) scoring to find the most relevant messages matching the query. Each message in history is treated as a document, and the query terms are matched against all documents to produce a relevance-ranked result.
No external dependencies are required -- this is a pure Java implementation.
- Specified by:
searchin interfaceMemoryStore- Parameters:
query- the search querylimit- maximum number of results to return- Returns:
- list of message content strings, ranked by relevance (most relevant first)
-
size
public int size()Returns the current size of the history. Useful for monitoring and debugging.- Returns:
- number of messages in history
-