Memory System - Memory Manager¶
File: cogs/memory/memory_manager.py
The MemoryManager is the central nervous system of the entire memory system. It acts as the primary entry point and orchestrator, initializing, coordinating, and providing a unified API for all other memory-related components.
MemoryManager Class¶
__init__(self, bot, ...)¶
The constructor initializes the manager and all its subordinate components.
- Initialization Process:
- Loads the memory system configuration from
optimization_config.json. - Initializes the
DatabaseManagerto handle all SQLite operations. - Initializes the
EmbeddingServiceto manage the loading and use of text embedding models. - Initializes the
VectorManagerto handle the storage and searching of vector indices. - Initializes the
SearchEngine, which combines the embedding service and vector manager to perform searches. - Initializes the
TextSegmentationServiceto group messages into logical conversation segments.
- Loads the memory system configuration from
Key Methods¶
async initialize(self) -> bool¶
Performs the main, asynchronous initialization of the memory system. This method is called once at bot startup. It loads configurations, sets up database connections, and prepares the vector search components.
async store_message(self, message: discord.Message) -> bool¶
This is the primary method for ingesting new information. It's called for each new message in a channel where memory is active.
- Process:
- It prepares the message data, cleaning and formatting the content.
- It saves the message to the SQLite database via the
DatabaseManager. - It passes the message to the
TextSegmentationServiceto determine if a new conversation segment should be created or if the message belongs to an existing one. - If a segment is completed, it triggers the process to embed the segment's content and store the resulting vector.
async search_memory(self, search_query: SearchQuery) -> SearchResult¶
The main public method for retrieving memories.
- Parameters:
search_query(SearchQuery): A data class containing the search text, channel ID, search type, and other parameters.
- Process:
- It first checks the
SearchCachefor a recent, identical query. - If no cached result is found, it passes the query to the
SearchEngine. - The
SearchEngineperforms the requested search (semantic, keyword, or hybrid). - The results are returned as a
SearchResultobject. - The new result is stored in the
SearchCache.
- It first checks the
- Returns: A
SearchResultdata class containing the retrieved messages, relevance scores, and metadata about the search.
async get_stats(self) -> MemoryStats¶
Retrieves performance and usage statistics for the entire memory system, such as the total number of indexed messages, average query time, and cache hit rate.