Register a memory class or instance. This will: - Create (or reuse) a memory instance - Create a `MemoryConfig` - Store it as the current config and append to version history - Register the version in `version_manager` Args:
(self,
memory: Union[Memory, Type[Memory]],
memory_config_dict: Optional[Dict[str, Any]] = None,
override: bool = False,
version: Optional[str] = None)
| 338 | return memory_configs |
| 339 | |
| 340 | async def register(self, |
| 341 | memory: Union[Memory, Type[Memory]], |
| 342 | memory_config_dict: Optional[Dict[str, Any]] = None, |
| 343 | override: bool = False, |
| 344 | version: Optional[str] = None) -> MemoryConfig: |
| 345 | """Register a memory class or instance. |
| 346 | |
| 347 | This will: |
| 348 | - Create (or reuse) a memory instance |
| 349 | - Create a `MemoryConfig` |
| 350 | - Store it as the current config and append to version history |
| 351 | - Register the version in `version_manager` |
| 352 | |
| 353 | Args: |
| 354 | memory: Memory instance or class |
| 355 | memory_config_dict: Configuration dict for memory initialization (required when memory is a class) |
| 356 | override: Whether to override existing registration |
| 357 | version: Optional version string |
| 358 | |
| 359 | Returns: |
| 360 | MemoryConfig: Memory configuration |
| 361 | """ |
| 362 | |
| 363 | try: |
| 364 | # Handle both instance and class cases |
| 365 | if isinstance(memory, Memory): |
| 366 | # Registering an instance |
| 367 | memory_instance = memory |
| 368 | memory_cls = type(memory) |
| 369 | if memory_config_dict: |
| 370 | raise ValueError("Extra keyword arguments are not allowed when registering memory instances.") |
| 371 | memory_config_dict = {} |
| 372 | else: |
| 373 | # Registering a class |
| 374 | memory_cls = memory |
| 375 | if memory_config_dict is None: |
| 376 | # Fallback to global config by class name |
| 377 | memory_config_key = inflection.underscore(memory_cls.__name__) |
| 378 | memory_config_dict = config.get(memory_config_key, {}) |
| 379 | |
| 380 | # Instantiate memory immediately (register is a runtime operation) |
| 381 | try: |
| 382 | memory_instance = memory_cls(**memory_config_dict) |
| 383 | except Exception as e: |
| 384 | logger.error(f"| ❌ Failed to create memory instance for {memory_cls.__name__}: {e}") |
| 385 | raise ValueError(f"Failed to instantiate memory {memory_cls.__name__} with provided config: {e}") |
| 386 | |
| 387 | memory_name = memory_instance.name |
| 388 | memory_description = memory_instance.description |
| 389 | memory_metadata = getattr(memory_instance, 'metadata', {}) |
| 390 | # Get require_grad from memory_config_dict if provided, otherwise from memory_instance |
| 391 | memory_require_grad = memory_config_dict.get("require_grad", memory_instance.require_grad) if memory_config_dict and "require_grad" in memory_config_dict else memory_instance.require_grad |
| 392 | |
| 393 | if not memory_name: |
| 394 | raise ValueError("Memory.name cannot be empty.") |
| 395 | |
| 396 | if memory_name in self._memory_configs and not override: |
| 397 | raise ValueError(f"Memory '{memory_name}' already registered. Use override=True to replace it.") |
nothing calls this directly
no test coverage detected