Class NamespacedMemoryStore
- All Implemented Interfaces:
MemoryStore
Enables scoped retrieval across a workspace > session hierarchy. Each namespace maintains its own isolated message history, and cross-namespace merged retrieval allows combining, for example, global context with session-local conversation history.
Standard MemoryStore methods delegate to a configurable default namespace,
preserving full backward compatibility with code that is namespace-unaware.
Thread safety: Each namespace has its own ReentrantLock for fine-grained
concurrency. Cross-namespace operations acquire locks in deterministic order to
prevent deadlocks.
- Since:
- 0.1.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a NamespacedMemoryStore withMemoryNamespace.GLOBALas the default.NamespacedMemoryStore(MemoryNamespace defaultNs) Creates a NamespacedMemoryStore with the given default namespace. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessage(String role, String content) Adds a message to the memory.voidaddMessage(String namespace, String role, String content) Adds a message to a specific namespace.voidaddMessage(String namespace, Map<String, Object> message) Adds a structured message to a specific namespace.voidaddMessage(Map<String, Object> message) Adds a structured message (e.g. with tool calls) to memory.voidclear()Clears the memory.voidClears a specific namespace.Retrieves the full conversation history.getHistory(String namespace) Retrieves the full history from a specific namespace.getMergedHistory(String... namespaceKeys) Retrieves history merged from multiple namespaces, in the order provided.Returns all active namespace keys.getRecentHistory(int limit) Retrieves the last N messages.getRecentHistory(String namespace, int limit) Retrieves the last N messages from a specific namespace.voidprune(int maxTokens) Prunes the memory to keep it within a specific token limit.voidPrunes a specific namespace to fit within the token budget.Performs TF-IDF based semantic search over conversation history.Searches across specified namespaces (or all if none specified) using TF-IDF.intReturns the number of messages in a specific namespace.Methods inherited from class com.tnsai.memory.AbstractMemoryStore
init, sizeMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.memory.MemoryStore
replaceHistory
-
Constructor Details
-
NamespacedMemoryStore
public NamespacedMemoryStore()Creates a NamespacedMemoryStore withMemoryNamespace.GLOBALas the default. -
NamespacedMemoryStore
Creates a NamespacedMemoryStore with the given default namespace.- Parameters:
defaultNs- the namespace used when standard MemoryStore methods are called
-
-
Method Details
-
addMessage
Adds a message to a specific namespace.- Parameters:
namespace- the target namespace key (e.g. "workspace:session")role- the message role (user, assistant, system)content- the message content
-
addMessage
Adds a structured message to a specific namespace.- Parameters:
namespace- the target namespace keymessage- the full message object
-
getHistory
Retrieves the full history from a specific namespace.- Parameters:
namespace- the namespace key- Returns:
- unmodifiable list of messages, or empty list if namespace does not exist
-
getRecentHistory
Retrieves the last N messages from a specific namespace.- Parameters:
namespace- the namespace keylimit- number of messages to retrieve- Returns:
- unmodifiable list of the most recent messages
-
getMergedHistory
Retrieves history merged from multiple namespaces, in the order provided. Messages from each namespace are appended sequentially.- Parameters:
namespaceKeys- the namespace keys to merge- Returns:
- unmodifiable merged list of messages
-
search
Searches across specified namespaces (or all if none specified) using TF-IDF.- Parameters:
query- the search querylimit- max resultsnamespaceKeys- namespaces to search (empty = all)- Returns:
- list of relevant content strings, ranked by relevance
-
clear
Clears a specific namespace.- Parameters:
namespace- the namespace key to clear
-
prune
Prunes a specific namespace to fit within the token budget.- Parameters:
namespace- the namespace keymaxTokens- the token budget
-
size
Returns the number of messages in a specific namespace.- Parameters:
namespace- the namespace key- Returns:
- the message count, or 0 if namespace does not exist
-
getNamespaceKeys
Returns all active namespace keys.- Returns:
- unmodifiable set of namespace keys
-
addMessage
Description copied from interface:MemoryStoreAdds a message to the memory.- Specified by:
addMessagein interfaceMemoryStore- Overrides:
addMessagein classAbstractMemoryStore- 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- Overrides:
addMessagein classAbstractMemoryStore- Parameters:
message- The full message object
-
getHistory
Description copied from interface:MemoryStoreRetrieves the full conversation history.- Specified by:
getHistoryin interfaceMemoryStore- Overrides:
getHistoryin classAbstractMemoryStore- Returns:
- List of message maps
-
getRecentHistory
Description copied from interface:MemoryStoreRetrieves the last N messages.- Specified by:
getRecentHistoryin interfaceMemoryStore- Overrides:
getRecentHistoryin classAbstractMemoryStore- 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- Overrides:
clearin classAbstractMemoryStore
-
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- Overrides:
prunein classAbstractMemoryStore- Parameters:
maxTokens- The maximum estimated tokens allowed
-
search
Description copied from class:AbstractMemoryStorePerforms 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- Overrides:
searchin classAbstractMemoryStore- Parameters:
query- the search querylimit- maximum number of results to return- Returns:
- list of message content strings, ranked by relevance (most relevant first)
-