Files
X-Agents/agent/app/agent/multi/graph.py
DESKTOP-72TV0V4\caoxiaozhu 5ea6f0d31f feat: 新增多 Agent 协作系统
- 添加多 Agent 图协作框架 (graph, supervisor, workers)
- 添加迭代器和集成模块
- 添加多 Agent 规划文档

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 23:21:37 +08:00

131 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
LangGraph 流程编排
"""
from langgraph.graph import StateGraph, END
from langgraph.graph.graph import CompiledGraph
from .types import AgentState, AgentType
from .supervisor import SupervisorAgent, ResultAggregator
from .workers.research import ResearchWorker
from .workers.coder import CoderWorker
from .workers.review import ReviewWorker
def create_multi_agent_graph(
llm,
tool_registry=None,
max_iterations: int = 3,
max_tasks: int = 10
) -> CompiledGraph:
"""创建多 Agent 流程图
Args:
llm: 语言模型实例
tool_registry: 工具注册表
max_iterations: 最大迭代次数
max_tasks: 最大任务数
Returns:
CompiledGraph: 编译后的 LangGraph
"""
# 初始化组件
supervisor = SupervisorAgent(llm, max_iterations=max_iterations, max_tasks=max_tasks)
research_worker = ResearchWorker(llm, tool_registry)
coder_worker = CoderWorker(llm, tool_registry)
review_worker = ReviewWorker(llm, tool_registry)
aggregator = ResultAggregator(llm)
# 创建图
graph = StateGraph(AgentState)
# 添加节点
graph.add_node("supervisor", supervisor.create_node())
graph.add_node(AgentType.RESEARCH, research_worker.create_node())
graph.add_node(AgentType.CODER, coder_worker.create_node())
graph.add_node(AgentType.REVIEW, review_worker.create_node())
graph.add_node("aggregator", aggregator.create_node())
# 设置入口点
graph.set_entry_point("supervisor")
# 定义条件边函数
def should_continue(state: AgentState) -> str:
"""判断是否继续执行"""
# 获取下一步节点
next_node = state.get("next_node", "aggregator")
# 如果是结束节点
if next_node in ["__end__", "aggregator"]:
return "aggregator"
# 如果是 Worker 节点
if next_node in [AgentType.RESEARCH, AgentType.CODER, AgentType.REVIEW]:
return next_node
# 如果是 supervisor
if next_node == "supervisor":
# 检查迭代次数
iteration = state.get("iteration", 0)
if iteration >= max_iterations:
return "aggregator"
return "supervisor"
# 默认进入汇总
return "aggregator"
# 添加条件边:从 supervisor 出来
graph.add_conditional_edges(
"supervisor",
should_continue,
{
"supervisor": "supervisor",
AgentType.RESEARCH: AgentType.RESEARCH,
AgentType.CODER: AgentType.CODER,
AgentType.REVIEW: AgentType.REVIEW,
"aggregator": "aggregator"
}
)
# 添加边Worker -> Review
graph.add_edge(AgentType.RESEARCH, AgentType.REVIEW)
graph.add_edge(AgentType.CODER, AgentType.REVIEW)
# 添加条件边:从 Review 出来
graph.add_conditional_edges(
AgentType.REVIEW,
should_continue,
{
"supervisor": "supervisor",
"aggregator": "aggregator"
}
)
# 添加边aggregator -> END
graph.add_edge("aggregator", END)
# 编译图
return graph.compile()
def create_simple_graph(llm, tool_registry=None) -> CompiledGraph:
"""创建简单的单 Agent 图(不经过 Supervisor"""
# 创建图
graph = StateGraph(AgentState)
# 直接使用 Coder Worker
coder_worker = CoderWorker(llm, tool_registry)
# 添加节点
graph.add_node("coder", coder_worker.create_node())
# 设置入口
graph.set_entry_point("coder")
# 添加边
graph.add_edge("coder", END)
return graph.compile()