2025-05-20 22:21:51 +08:00
|
|
|
|
import asyncio
|
2025-10-23 14:08:08 +08:00
|
|
|
|
import importlib
|
|
|
|
|
|
import inspect
|
|
|
|
|
|
from pathlib import Path
|
2025-07-22 04:12:55 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
from server.utils.singleton import SingletonMeta
|
2025-10-23 14:08:08 +08:00
|
|
|
|
from src.agents.common.base import BaseAgent
|
|
|
|
|
|
from src.utils import logger
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
class AgentManager(metaclass=SingletonMeta):
|
2025-03-24 19:07:51 +08:00
|
|
|
|
def __init__(self):
|
2025-05-20 22:21:51 +08:00
|
|
|
|
self._classes = {}
|
|
|
|
|
|
self._instances = {} # 存储已创建的 agent 实例
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-06-29 15:43:40 +08:00
|
|
|
|
def register_agent(self, agent_class):
|
2025-07-22 17:30:00 +08:00
|
|
|
|
self._classes[agent_class.__name__] = agent_class
|
2025-03-24 23:00:14 +08:00
|
|
|
|
|
2025-05-20 22:21:51 +08:00
|
|
|
|
def init_all_agents(self):
|
2025-07-22 17:30:00 +08:00
|
|
|
|
for agent_id in self._classes.keys():
|
|
|
|
|
|
self.get_agent(agent_id)
|
2025-05-20 22:21:51 +08:00
|
|
|
|
|
2025-07-26 03:36:54 +08:00
|
|
|
|
def get_agent(self, agent_id, reload=False, **kwargs):
|
2025-05-15 22:34:32 +08:00
|
|
|
|
# 检查是否已经创建了该 agent 的实例
|
2025-07-26 03:36:54 +08:00
|
|
|
|
if reload or agent_id not in self._instances:
|
2025-07-22 17:30:00 +08:00
|
|
|
|
agent_class = self._classes[agent_id]
|
|
|
|
|
|
self._instances[agent_id] = agent_class()
|
2025-05-20 22:21:51 +08:00
|
|
|
|
|
2025-07-22 17:30:00 +08:00
|
|
|
|
return self._instances[agent_id]
|
2025-05-20 22:21:51 +08:00
|
|
|
|
|
|
|
|
|
|
def get_agents(self):
|
|
|
|
|
|
return list(self._instances.values())
|
2025-03-25 05:40:07 +08:00
|
|
|
|
|
2025-07-26 03:36:54 +08:00
|
|
|
|
async def reload_all(self):
|
|
|
|
|
|
for agent_id in self._classes.keys():
|
|
|
|
|
|
self.get_agent(agent_id, reload=True)
|
|
|
|
|
|
|
2025-05-20 22:21:51 +08:00
|
|
|
|
async def get_agents_info(self):
|
|
|
|
|
|
agents = self.get_agents()
|
|
|
|
|
|
return await asyncio.gather(*[a.get_info() for a in agents])
|
2025-03-25 05:40:07 +08:00
|
|
|
|
|
2025-10-23 14:08:08 +08:00
|
|
|
|
def auto_discover_agents(self):
|
|
|
|
|
|
"""自动发现并注册 src/agents/ 下的所有智能体。
|
|
|
|
|
|
|
|
|
|
|
|
遍历 src/agents/ 目录下的所有子文件夹,如果子文件夹包含 __init__.py,
|
|
|
|
|
|
则尝试从中导入 BaseAgent 的子类并注册。(使用自动导入的方式,支持私有agent)
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 获取 agents 目录的路径
|
|
|
|
|
|
agents_dir = Path(__file__).parent
|
|
|
|
|
|
|
|
|
|
|
|
# 遍历所有子目录
|
|
|
|
|
|
for item in agents_dir.iterdir():
|
|
|
|
|
|
logger.info(f"尝试导入模块:{item}")
|
|
|
|
|
|
# 跳过非目录、common 目录、__pycache__ 等
|
|
|
|
|
|
if not item.is_dir() or item.name.startswith("_") or item.name == "common":
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
# 检查是否有 __init__.py 文件
|
|
|
|
|
|
init_file = item / "__init__.py"
|
|
|
|
|
|
if not init_file.exists():
|
|
|
|
|
|
logger.warning(f"{item} 不是一个有效的模块")
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
# 尝试导入模块
|
|
|
|
|
|
try:
|
|
|
|
|
|
module_name = f"src.agents.{item.name}"
|
|
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
|
|
|
|
|
|
|
|
# 查找模块中所有 BaseAgent 的子类
|
|
|
|
|
|
for name, obj in inspect.getmembers(module):
|
|
|
|
|
|
if (
|
|
|
|
|
|
inspect.isclass(obj)
|
|
|
|
|
|
and issubclass(obj, BaseAgent)
|
|
|
|
|
|
and obj is not BaseAgent
|
|
|
|
|
|
and obj.__module__.startswith(module_name)
|
|
|
|
|
|
):
|
|
|
|
|
|
logger.info(f"自动发现智能体: {obj.__name__} 来自 {item.name}")
|
|
|
|
|
|
self.register_agent(obj)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.warning(f"无法从 {item.name} 加载智能体: {e}")
|
|
|
|
|
|
|
2025-03-25 05:40:07 +08:00
|
|
|
|
|
|
|
|
|
|
agent_manager = AgentManager()
|
2025-10-23 14:08:08 +08:00
|
|
|
|
# 自动发现并注册所有智能体
|
|
|
|
|
|
agent_manager.auto_discover_agents()
|
2025-05-20 22:21:51 +08:00
|
|
|
|
agent_manager.init_all_agents()
|
2025-03-25 05:40:07 +08:00
|
|
|
|
|
|
|
|
|
|
__all__ = ["agent_manager"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-05-24 11:29:45 +08:00
|
|
|
|
pass
|