59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
import traceback
|
|
from typing import (
|
|
TypeAlias,
|
|
Dict,
|
|
Tuple,
|
|
)
|
|
import importlib
|
|
import pathlib
|
|
import inspect
|
|
|
|
from .base import Agent, get_or_create_agent
|
|
from logger import logger
|
|
|
|
# Type alias for Agent or any subclass
|
|
AnyAgent: TypeAlias = Agent # BaseModel covers Agent and subclasses
|
|
|
|
# Maps class_name to (module_name, class_name)
|
|
class_registry: Dict[str, Tuple[str, str]] = (
|
|
{}
|
|
)
|
|
|
|
__all__ = ['get_or_create_agent']
|
|
|
|
package_dir = pathlib.Path(__file__).parent
|
|
package_name = __name__
|
|
|
|
for path in package_dir.glob("*.py"):
|
|
if path.name in ("__init__.py", "base.py") or path.name.startswith("_"):
|
|
continue
|
|
|
|
module_name = path.stem
|
|
full_module_name = f"{package_name}.{module_name}"
|
|
|
|
try:
|
|
module = importlib.import_module(full_module_name)
|
|
|
|
# Find all Agent subclasses in the module
|
|
for name, obj in inspect.getmembers(module, inspect.isclass):
|
|
if (
|
|
issubclass(obj, AnyAgent)
|
|
and obj is not AnyAgent
|
|
and obj is not Agent
|
|
and name not in class_registry
|
|
):
|
|
class_registry[name] = (full_module_name, name)
|
|
globals()[name] = obj
|
|
logger.info(f"Adding agent: {name}")
|
|
__all__.append(name) # type: ignore
|
|
except ImportError as e:
|
|
logger.error(traceback.format_exc())
|
|
logger.error(f"Error importing {full_module_name}: {e}")
|
|
raise e
|
|
except Exception as e:
|
|
logger.error(traceback.format_exc())
|
|
logger.error(f"Error processing {full_module_name}: {e}")
|
|
raise e
|