Class SharedMemoryStore
- All Implemented Interfaces:
MemoryStore
SharedMemoryStore wraps any MemoryStore and provides
thread-safe read/write access across multiple agents. It maintains
an access control list of agent IDs that are permitted to read
from and write to the shared memory.
Usage with @Memory(shared = true, shareWith = {"agent-a", "agent-b"}):
MemoryStore base = new InMemoryStore();
SharedMemoryStore shared = new SharedMemoryStore(base, "namespace-1");
shared.grantAccess("agent-a");
shared.grantAccess("agent-b");
shared.init("agent-a"); // First agent initializes
Viewer-bound views and per-operation ACL
The directly constructed instance is the canonical handle for the namespace: it owns the ACL and performs the once-only delegate initialization. It is a trusted handle — its data operations do not check the ACL.
Agents that resolve shared memory through AgentMemoryResolver receive
a viewer-bound view created via viewFor(String). A view shares
the delegate, ACL and lock with the canonical handle, but every operation
(including init(String)) verifies that the bound viewer is authorized
and throws IllegalStateException otherwise. Views created for a
previously authorized agent start throwing as soon as access is revoked.
Thread safety: All operations are protected by a ReentrantReadWriteLock
for high-throughput concurrent reads with exclusive writes.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSharedMemoryStore(MemoryStore delegate, String namespace) Creates a SharedMemoryStore wrapping the given delegate. -
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.Returns the namespace of this shared memory.getRecentHistory(int limit) Retrieves the last N messages.voidgrantAccess(String agentId) Grants access to an agent ID.booleanChecks whether the given agent has access.voidInitializes the memory store for a specific agent.voidprune(int maxTokens) Prunes the memory to keep it within a specific token limit.voidrevokeAccess(String agentId) Revokes access from an agent ID.Performs a semantic search on the memory (Optional).Creates a view of this shared store bound to the given agent.Methods 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
-
SharedMemoryStore
Creates a SharedMemoryStore wrapping the given delegate.- Parameters:
delegate- the underlying memory storenamespace- the shared memory namespace (isolates different shared contexts)
-
-
Method Details
-
viewFor
Creates a view of this shared store bound to the given agent.The view shares state with this store but enforces the ACL on every operation: all
MemoryStoremethods throwIllegalStateExceptiononce the viewer is not (or no longer) authorized for the namespace.- Parameters:
agentId- the agent ID to bind the view to- Returns:
- a viewer-bound view of this store
-
grantAccess
Grants access to an agent ID.- Parameters:
agentId- the agent ID to authorize
-
revokeAccess
Revokes access from an agent ID.- Parameters:
agentId- the agent ID to deauthorize
-
hasAccess
Checks whether the given agent has access.- Parameters:
agentId- the agent ID to check- Returns:
- true if authorized
-
getNamespace
Returns the namespace of this shared memory.- Returns:
- the namespace
-
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
Description copied from interface:MemoryStorePerforms a semantic search on the memory (Optional). Useful for RAG (Retrieval-Augmented Generation).- Specified by:
searchin interfaceMemoryStore- Parameters:
query- The search querylimit- Max results- Returns:
- List of relevant memory segments
-