Expand description
Memory management module for conversation context and user memory.
This module provides utilities for:
- Building agent context with memory
- Formatting memory for LLM prompts
- Managing conversation history windows
User memory facts and preferences are stored in the database (PostgresClient). This module provides utilities for working with that stored memory.
Structs§
- Memory
Config - Configuration for the in-memory session store.
- Memory
Session - A tenant-scoped conversation session stored in memory.
- Memory
Store - In-memory LRU session store with per-tenant isolation and TTL.
Enums§
- Memory
Error - Errors from session store operations.
Constants§
- DEFAULT_
HISTORY_ WINDOW - Default number of recent messages to include in context.
- MAX_
FACTS_ IN_ PROMPT - Maximum number of facts to include in a prompt to avoid token overflow.
- MAX_
PREFERENCES_ IN_ PROMPT - Maximum number of preferences to include in a prompt.
Functions§
- build_
context - Builds an agent context from components.
- cleanup_
expired - Remove expired sessions; returns the number removed.
- estimate_
tokens - Estimates token count for a message.
- filter_
facts_ by_ category - Filters memory facts by category.
- filter_
preferences_ by_ category - Filters preferences by category.
- format_
memory_ for_ prompt - Formats user memory into a string suitable for inclusion in system prompts.
- format_
preferences_ compact - Formats user preferences into a compact string for prompt inclusion.
- lru_
evict - Evict least-recently-used entries until
order.len() <= max_sessions. - lru_get
- Promote
keyto most-recently-used (end oforder); returns whether it was present. - lru_put
- Insert or promote
keyto most-recently-used. - session_
key - Composite map key for tenant-scoped sessions.
- truncate_
history - Truncates conversation history to a window of recent messages.
- truncate_
history_ to_ tokens - Truncates history to fit within a token budget.
- ttl_
expired - Returns true when
now - created_at > ttl_secs(TTL disabled whenttl_secs == 0).