55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
|
|
"""Agent-Loader: scannt agents/ Ordner und lädt alle BaseAgent-Subklassen."""
|
||
|
|
import importlib
|
||
|
|
import inspect
|
||
|
|
import logging
|
||
|
|
from pathlib import Path
|
||
|
|
from typing import Dict, List
|
||
|
|
|
||
|
|
from agents.base import BaseAgent, AgentConfig
|
||
|
|
|
||
|
|
|
||
|
|
logger = logging.getLogger("agents.loader")
|
||
|
|
|
||
|
|
|
||
|
|
def discover_agents() -> Dict[str, BaseAgent]:
|
||
|
|
"""Scannt das agents/ Verzeichnis und gibt alle Agent-Instanzen zurück."""
|
||
|
|
agents: Dict[str, BaseAgent] = {}
|
||
|
|
agents_dir = Path(__file__).parent
|
||
|
|
|
||
|
|
for py_file in agents_dir.glob("*.py"):
|
||
|
|
if py_file.name.startswith("_") or py_file.name in ("base.py", "__init__.py"):
|
||
|
|
continue
|
||
|
|
module_name = f"agents.{py_file.stem}"
|
||
|
|
try:
|
||
|
|
module = importlib.import_module(module_name)
|
||
|
|
except Exception as e:
|
||
|
|
logger.warning(f"Konnte {module_name} nicht laden: {e}")
|
||
|
|
continue
|
||
|
|
|
||
|
|
for name, obj in inspect.getmembers(module, inspect.isclass):
|
||
|
|
if obj is BaseAgent:
|
||
|
|
continue
|
||
|
|
if issubclass(obj, BaseAgent) and obj.__module__ == module_name:
|
||
|
|
try:
|
||
|
|
instance = obj()
|
||
|
|
if hasattr(instance, "config"):
|
||
|
|
agents[instance.config.id] = instance
|
||
|
|
logger.info(f"Agent geladen: {instance.config.id} ({instance.config.name})")
|
||
|
|
except Exception as e:
|
||
|
|
logger.warning(f"Konnte Agent {name} aus {module_name} nicht instanziieren: {e}")
|
||
|
|
|
||
|
|
return agents
|
||
|
|
|
||
|
|
|
||
|
|
def get_agent(agent_id: str) -> BaseAgent:
|
||
|
|
"""Holt einen Agent by ID."""
|
||
|
|
agents = discover_agents()
|
||
|
|
if agent_id not in agents:
|
||
|
|
raise KeyError(f"Agent '{agent_id}' nicht gefunden. Verfügbar: {list(agents.keys())}")
|
||
|
|
return agents[agent_id]
|
||
|
|
|
||
|
|
|
||
|
|
def list_agents() -> List[AgentConfig]:
|
||
|
|
"""Gibt Configs aller Agents zurück."""
|
||
|
|
return [a.config for a in discover_agents().values()]
|