Implementation:24kchengYe MemoMind load extension
| Knowledge Sources | |
|---|---|
| Domains | Software_Architecture, Extensibility |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
Dynamic extension loader that instantiates extension classes from environment variable configuration, enabling zero-code customization.
Description
The load_extension function reads an environment variable (e.g., `HINDSIGHT_API_OPERATION_VALIDATOR_EXTENSION`), imports the class via dotted path (e.g., `mypackage.validators:MyValidator`), instantiates it with config from matching env vars, and calls `set_context()`. Returns None if the extension is not configured. Enables users to add features purely via environment variables.
Usage
Call this function at startup to load optional extensions. Returns None if no extension is configured.
Code Reference
Source Location
- Repository: MemoMind
- File: engine/hindsight_api/extensions/loader.py
- Lines: L1-125
Signature
def load_extension(
prefix: str,
base_class: type[T],
env_prefix: str = "HINDSIGHT_API",
context: ExtensionContext | None = None,
) -> T | None: ...
Import
from hindsight_api.extensions.loader import load_extension
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| prefix | str | Yes | Extension prefix (e.g., "OPERATION_VALIDATOR") |
| base_class | type | Yes | Base class the extension must inherit |
| context | ExtensionContext | No | Context to provide to extension |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | None | Extension instance or None if not configured |
Usage Examples
Loading an Operation Validator
from hindsight_api.extensions.loader import load_extension
from hindsight_api.extensions.operation_validator import OperationValidatorExtension
# Env: HINDSIGHT_API_OPERATION_VALIDATOR_EXTENSION=mypackage:CreditValidator
# Env: HINDSIGHT_API_OPERATION_VALIDATOR_MAX_CREDITS=1000
validator = load_extension("OPERATION_VALIDATOR", OperationValidatorExtension)
# validator.config == {"max_credits": "1000"}