Global context manager for all memory systems with lazy loading support.
| 24 | from src.registry import MEMORY_SYSTEM |
| 25 | |
| 26 | class MemoryContextManager(BaseModel): |
| 27 | """Global context manager for all memory systems with lazy loading support.""" |
| 28 | model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow") |
| 29 | |
| 30 | base_dir: str = Field(default=None, description="The base directory to use for the memory systems") |
| 31 | save_path: str = Field(default=None, description="The path to save the memory systems") |
| 32 | contract_path: str = Field(default=None, description="The path to save the memory contract") |
| 33 | |
| 34 | def __init__(self, |
| 35 | base_dir: Optional[str] = None, |
| 36 | save_path: Optional[str] = None, |
| 37 | contract_path: Optional[str] = None, |
| 38 | **kwargs): |
| 39 | """Initialize the memory context manager. |
| 40 | |
| 41 | Args: |
| 42 | base_dir: Base directory for storing memory data |
| 43 | save_path: Path to save memory configurations |
| 44 | contract_path: Path to save memory contract |
| 45 | """ |
| 46 | super().__init__(**kwargs) |
| 47 | |
| 48 | if base_dir is not None: |
| 49 | self.base_dir = assemble_project_path(base_dir) |
| 50 | else: |
| 51 | self.base_dir = assemble_project_path(os.path.join(config.workdir, "memory")) |
| 52 | logger.info(f"| 📁 Memory context manager base directory: {self.base_dir}.") |
| 53 | os.makedirs(self.base_dir, exist_ok=True) |
| 54 | if save_path is not None: |
| 55 | self.save_path = assemble_project_path(save_path) |
| 56 | else: |
| 57 | self.save_path = os.path.join(self.base_dir, "memory.json") |
| 58 | logger.info(f"| 📁 Memory context manager save path: {self.save_path}.") |
| 59 | if contract_path is not None: |
| 60 | self.contract_path = assemble_project_path(contract_path) |
| 61 | else: |
| 62 | self.contract_path = os.path.join(self.base_dir, "contract.md") |
| 63 | logger.info(f"| 📁 Memory context manager contract path: {self.contract_path}.") |
| 64 | |
| 65 | self._memory_configs: Dict[str, MemoryConfig] = {} # Current active configs (latest version) |
| 66 | # Memory version history, e.g., {"memory_name": {"1.0.0": MemoryConfig, "1.0.1": MemoryConfig}} |
| 67 | self._memory_history_versions: Dict[str, Dict[str, MemoryConfig]] = {} |
| 68 | |
| 69 | self._cleanup_registered = False |
| 70 | self._variables_lock = asyncio.Lock() # Lock for get/set trainable variables |
| 71 | |
| 72 | async def initialize(self, memory_names: Optional[List[str]] = None): |
| 73 | """Initialize the memory context manager.""" |
| 74 | # Register memory-related symbols for auto-injection in dynamic code |
| 75 | dynamic_manager.register_symbol("MEMORY_SYSTEM", MEMORY_SYSTEM) |
| 76 | dynamic_manager.register_symbol("Memory", Memory) |
| 77 | |
| 78 | # Register memory context provider for automatic import injection |
| 79 | def memory_context_provider(): |
| 80 | """Provide memory-related imports for dynamic memory classes.""" |
| 81 | return { |
| 82 | "MEMORY_SYSTEM": MEMORY_SYSTEM, |
| 83 | "Memory": Memory, |