calute.memory.user_memory#
User-specific memory for personalization.
- class calute.memory.user_memory.UserMemory(storage: Any | None = None)[source]#
Bases:
objectUser-specific memory manager with per-user isolation.
Maintains separate
ContextualMemoryandEntityMemoryinstances for each user, along with per-user preference dictionaries. This allows agents to personalise responses and retain context across sessions on a per-user basis.- storage#
Optional
MemoryStoragebackend used for persisting user preferences and passed to per-user memory instances.
- user_memories#
Dictionary mapping user IDs to their
ContextualMemoryinstances.
- user_entities#
Dictionary mapping user IDs to their
EntityMemoryinstances.
- user_preferences#
Dictionary mapping user IDs to preference dictionaries (response style, verbosity, language, etc.).
Example
>>> from calute.memory import UserMemory >>> um = UserMemory() >>> um.save_memory("user-1", "Prefers dark mode") >>> um.update_user_preferences("user-1", {"theme": "dark"}) >>> ctx = um.get_user_context("user-1")
- clear_user_memory(user_id: str) None[source]#
Clear all data for a user and remove them from the manager.
Clears and deletes the user’s
ContextualMemory,EntityMemory, and preference dictionary. After this call the user is treated as if they never existed; a subsequent call toget_or_create_user_memory()will re-initialise them with fresh defaults.- Parameters
user_id – Unique identifier for the user to clear.
- get_or_create_user_memory(user_id: str) ContextualMemory[source]#
Get or lazily create the memory subsystem for a user.
On first call for a given
user_id, creates:A
ContextualMemory(usingstoragefor long-term).An
EntityMemory(usingstoragefor persistence).A default preferences dictionary (see
_get_default_preferences()).
Subsequent calls return the existing
ContextualMemory.- Parameters
user_id – Unique identifier for the user.
- Returns
The
ContextualMemoryinstance associated with the user.
- get_user_context(user_id: str) str[source]#
Build a formatted context string for a user.
Combines three sections into a single string separated by blank lines:
User preferences – the current preference dictionary.
Context summary – from the user’s
ContextualMemory.Known entities – up to 10 entity names from the user’s
EntityMemory.
- Parameters
user_id – Unique identifier for the user.
- Returns
Multi-line context string suitable for inclusion in an agent’s system prompt or context window.
- get_user_preferences(user_id: str) dict[str, Any][source]#
Retrieve the preferences for a user.
- Parameters
user_id – Unique identifier for the user.
- Returns
Dictionary of user preferences. Returns a fresh set of default preferences if the user has not been registered yet.
- get_user_statistics(user_id: str) dict[str, Any][source]#
Compute aggregate statistics for a user’s memory subsystem.
- Parameters
user_id – Unique identifier for the user.
- Returns
"user_id": the queried user ID."total_memories": combined short-term and long-term count."short_term_memories": short-term item count (if available)."long_term_memories": long-term item count (if available)."entities_known": number of tracked entities."relationships": total relationship pair count."preferences": the user’s current preference dictionary.
- Return type
Dictionary with keys
- save_memory(user_id: str, content: str, metadata: dict[str, Any] | None = None, **kwargs)[source]#
Save a memory item for a specific user.
Stores the content in both the user’s
ContextualMemory(for context-aware retrieval and promotion) andEntityMemory(for entity tracking).- Parameters
user_id – Unique identifier for the user. If the user does not yet exist, their memory subsystem is created automatically.
content – Text content to store.
metadata – Optional key-value metadata. A
"user_id"key is added automatically.**kwargs – Additional keyword arguments forwarded to the underlying
savemethods (e.g.importance,agent_id).
- Returns
The
MemoryItemcreated by the contextual memory store.
- search_user_memory(user_id: str, query: str, limit: int = 10, **kwargs) list[source]#
Search memories for a specific user.
Delegates to the user’s
ContextualMemorysearch, which queries both short-term and long-term stores.- Parameters
user_id – Unique identifier for the user. If the user does not yet exist, their memory subsystem is created automatically.
query – Natural-language or keyword search query string.
limit – Maximum number of results to return.
**kwargs – Additional keyword arguments forwarded to
ContextualMemory.search().
- Returns
List of matching
MemoryIteminstances sorted by relevance.
- update_user_preferences(user_id: str, preferences: dict[str, Any]) None[source]#
Update user preferences by merging new values.
Existing keys are overwritten, new keys are added, and the full preference dictionary is persisted to the storage backend.
- Parameters
user_id – Unique identifier for the user. If no preferences exist yet, defaults are initialised first.
preferences – Dictionary of preference keys and their new values to merge into the existing preferences.