ModuleAgentLoader
What it is
- A small utility for discovering and importing
Agentsubclasses from anagents/folder adjacent to a given module/class. - Returns a list of agent classes found in
*.pyfiles (excluding files ending intest.py).
Public API
class ModuleAgentLoader@classmethod load_agents(class_: type) -> List[type[Agent]]- Locates the module root for
class_, then scans<module_root>/agents. - Imports each agent module and collects classes that:
- are Python classes (
isinstance(value, type)), - are subclasses of
naas_abi_core.services.agent.Agent.Agent, - belong to the same top-level package as
class_(by comparing__module__.split(".")[0]).
- are Python classes (
- If the agent module defines
create_agent, it sets aNewattribute on each discovered agent class to referencecreate_agent.
- Locates the module root for
Configuration/Dependencies
- Filesystem expectations:
- An
agents/directory exists at the module root path returned byfind_class_module_root_path(class_). - Agent modules are importable as:
"{class_.__module__}.agents.<filename_without_py>".
- An
- Dependencies:
naas_abi_core.module.ModuleUtils.find_class_module_root_pathnaas_abi_core.services.agent.Agent.Agent(base class for agents)naas_abi_core.utils.Logger.logger(debug logging)- Standard library:
importlib,os
Usage
from naas_abi_core.module.ModuleAgentLoader import ModuleAgentLoader
# Any class from the target package/module; used to locate <module_root>/agents
from my_package.my_module import SomeClass
agents = ModuleAgentLoader.load_agents(SomeClass)
for agent_cls in agents:
print(agent_cls.__name__)
# If the agent module defined create_agent, agent_cls.New will be set.
Caveats
- Only loads from
.pyfiles in theagents/directory; ignores files ending withtest.py. - Only agent classes whose top-level package matches
class_are collected. - The
Newattribute injection depends oncreate_agentexisting in the agent module; no validation of its signature is performed.