ModuleAgentLoader
What it is
- Utility loader that discovers and imports “agent” classes from an
agents/package located next to a given class’s module root. - Returns agent classes that subclass
naas_abi_core.utils.Expose.Expose.
Public API
class ModuleAgentLoader@classmethod load_agents(class_: type) -> List[type[Expose]]- Scans
<module_root>/agents/*.py(excluding*test.py), imports each module, and collects classes that:- are
typeobjects, - are subclasses of
Expose, - belong to the same top-level package as
class_(same first segment of__module__).
- are
- If an agent module defines
create_agent, it is attached to each discovered agent class asNew(viasetattr) when the (incorrectly implemented) guard passes.
- Scans
Configuration/Dependencies
- Filesystem structure:
- Requires an
agents/directory under the module root returned byfind_class_module_root_path(class_).
- Requires an
- Dependencies:
naas_abi_core.module.ModuleUtils.find_class_module_root_pathnaas_abi_core.utils.Expose.Exposenaas_abi_core.utils.Logger.logger- Standard library:
importlib,os
Usage
from naas_abi_core.module.ModuleAgentLoader import ModuleAgentLoader
# Any class within your package/module tree
from mypkg.some_module import SomeClass
agents = ModuleAgentLoader.load_agents(SomeClass)
for AgentCls in agents:
print(AgentCls.__name__, AgentCls)
Expected package layout (example):
mypkg/
some_module.py # defines SomeClass
agents/
agent_a.py # defines class AgentA(Expose)
agent_b.py # defines class AgentB(Expose)
Caveats
- Only
.pyfiles inagents/are considered; files ending withtest.pyare skipped. - Imported agents are filtered to the same top-level package as
class_(based on__module__.split(".")[0]). - The guard
if not hasattr(key, "New") ...checks the string name (key), not the class; as written it is effectively always true for"New"and does not correctly detect whether the class already hasNew.