Skip to main content

Module memory

Module memory 

Source
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§

MemoryConfig
Configuration for the in-memory session store.
MemorySession
A tenant-scoped conversation session stored in memory.
MemoryStore
In-memory LRU session store with per-tenant isolation and TTL.

Enums§

MemoryError
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 key to most-recently-used (end of order); returns whether it was present.
lru_put
Insert or promote key to 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 when ttl_secs == 0).