BaseModule
What it is
A base class and supporting models for defining “modules” in naas_abi_core. A module:
- Is initialized with an
EngineProxyand a validatedModuleConfiguration - Can declare dependencies on other modules/services
- Can auto-discover ontology
.ttlfiles from anontologies/folder - Can load agents and orchestrations via loader helpers
- Exposes lifecycle hooks (
on_load,on_initialized,on_unloaded) and an optional FastAPIapi()hook
Public API
class ModuleDependencies
Holds dependency declarations for a module.
- Constructor
ModuleDependencies(modules: List[str], services: List[type])
- Properties
modules: List[str]— module dependency names/identifiersservices: List[type]— service types required by the module
class ModuleConfiguration(pydantic.BaseModel)
Base configuration model for modules.
- Fields
global_config: GlobalConfig
- Pydantic behavior
extra="forbid"(unknown fields rejected)
class BaseModule(Generic[TConfig])
Base interface for modules (generic over a configuration type TConfig bound to ModuleConfiguration).
Class attributes
dependencies: ModuleDependencies— default isModuleDependencies(modules=[], services=[])_instances: Dict[type, BaseModule]— internal registry forget_instance()
Constructor
BaseModule(engine: EngineProxy, configuration: TConfig)- Asserts
configurationis aModuleConfiguration - Asserts subclass defines
Configurationclass and that it is a subclass ofModuleConfiguration - Sets:
module_path(derived fromself.__module__)module_root_path(viafind_class_module_root_path())
- Asserts
Class methods
get_dependencies() -> List[str]- Returns
getattr(cls, "dependencies", []). - Note:
dependenciesis aModuleDependenciesby default, despite the return type annotation.
- Returns
get_instance() -> Self- Returns the stored instance for the module class.
- Raises
ValueErrorif the class was not initialized.
Properties (read-only)
engine -> EngineProxyconfiguration -> TConfigontologies -> List[str]— populated byon_load()via filesystem scanagents -> List[type[Agent]]— set byon_load()viaModuleAgentLoaderintegrations -> List[Integration]workflows -> List[Workflow]pipelines -> List[Pipeline]orchestrations -> List[type[Orchestrations]]— set byon_load()viaModuleOrchestrationLoader
Lifecycle hooks
on_load()- Loads ontologies from
<module_root_path>/ontologies/**/*.ttl - Loads agents and orchestrations via loader helpers
- Loads ontologies from
on_initialized()- Called after all modules are loaded and the engine is fully initialized (default: logs only)
on_unloaded()- Default: no-op
api(app: fastapi.FastAPI) -> None- Override to register FastAPI endpoints (default: no-op)
Configuration/Dependencies
- Configuration model: subclasses must define an inner class attribute
Configurationthat subclassesModuleConfiguration. - Dependencies declaration: via the class attribute
dependencies(aModuleDependenciesinstance). - Filesystem:
- Ontologies are discovered only if an
ontologies/directory exists under the module root path. - Only
*.ttlfiles are collected recursively.
- Ontologies are discovered only if an
Key imports used:
fastapi.FastAPIpydantic.BaseModelnaas_abi_core.engine.EngineProxy.EngineProxynaas_abi_core.engine.engine_configuration.EngineConfiguration.GlobalConfigModuleAgentLoader,ModuleOrchestrationLoaderfind_class_module_root_path
Usage
from naas_abi_core.module.Module import BaseModule, ModuleConfiguration, ModuleDependencies
# EngineProxy is provided by naas_abi_core at runtime.
# Here it's typed as "object" just to keep the example minimal/runnable.
EngineProxy = object
class MyConfig(ModuleConfiguration):
pass
class MyModule(BaseModule[MyConfig]):
Configuration = MyConfig
dependencies = ModuleDependencies(modules=["other_module"], services=[])
def on_initialized(self):
# Called after engine and all modules are initialized
print("Module initialized")
engine = EngineProxy()
cfg = MyConfig(global_config=None) # GlobalConfig instance is expected in real usage.
m = MyModule(engine=engine, configuration=cfg)
m.on_load()
# Access singleton-like instance (after initialization)
same = MyModule.get_instance()Caveats
BaseModule.__init__enforces that subclasses defineConfigurationand that it subclassesModuleConfiguration; otherwise it raises viaassert.get_instance()only works after at least one instance of that module class has been constructed; otherwise it raisesValueError.get_dependencies()is annotated to returnList[str], but the defaultdependenciesvalue is aModuleDependenciesinstance; callers should not rely on the annotation alone.