Files
beaver_project/app-instance/backend/beaver/team_workflows/executor.py
steven_li 520a21a027 feat(coordinator): 添加团队节点默认最大工具迭代次数配置
添加 DEFAULT_TEAM_NODE_MAX_TOOL_ITERATIONS 配置项以控制团队节点的最大工具迭代次数,
并修改 LocalAgentRunner 中的逻辑来使用此默认值当 envelope 中未指定时。

fix(runtime): 修复团队节点运行成功判断逻辑

更新运行成功判断条件,将 finish_reason 为 "max_tool_iterations_finalized" 的情况
视为运行失败,并添加对原始工具调用输出的检测,避免将其误判为成功完成。

feat(mcp): 添加团队工作流MCP工具类别支持

增加新的本地MCP工具类别 "team_workflow" 及其对应的工具创建功能,
为团队工作流提供本地工具支持。

refactor(engine): 调整AgentLoop最大工具迭代次数设置

将 AgentProfile 中的默认 max_tool_iterations 从 30 增加到 100,
同时移除 TaskExecutionPlanner 构造函数中的重复参数传递。

perf(mcp): 优化MCP连接管理避免重复连接

添加 mcp_connected 标志来跟踪MCP连接状态,确保 connect_all 只执行一次,
提高性能并避免不必要的重复连接。

refactor(skills): 移除技能团队模板相关功能

移除与技能团队模板相关的代码,包括解析、存储和处理逻辑,
简化技能记录结构和加载流程。

feat(process): 增强会话过程投影器功能

添加技能激活快照事件处理,改进团队运行完成消息显示,
并增强技能激活事件的时间戳记录功能。

refactor(tasks): 简化任务尝试编排器团队执行逻辑

移除团队执行相关代码,将所有任务统一按单步执行处理,
简化任务编排器的复杂度并提升执行效率。

fix(evidence): 修复节点证据评估中需求验证逻辑

更新节点证据评估逻辑,跳过自然语言证据需求的确定性验证,
只执行机器可读的需求验证,避免因自然语言需求导致的节点失败。
2026-06-26 16:36:29 +08:00

175 lines
6.0 KiB
Python

"""Runtime bridge for local team workflow MCP tools."""
from __future__ import annotations
import json
from typing import Any, Callable
from beaver.coordinator.models import ExecutionGraph, TeamRunResult
from beaver.tools.base import ToolContext, ToolResult
from . import agent_rearrange, concurrent, graph, mixture_of_agents, sequential
GraphBuilder = Callable[..., ExecutionGraph]
class TeamWorkflowExecutor:
"""Execute workflow MCP calls inside the current Beaver runtime."""
_BUILDERS: dict[str, GraphBuilder] = {
"SequentialWorkflow": sequential.build_graph,
"ConcurrentWorkflow": concurrent.build_graph,
"MixtureOfAgents": mixture_of_agents.build_graph,
"AgentRearrange": agent_rearrange.build_graph,
"GraphWorkflow": graph.build_graph,
}
async def execute(
self,
workflow_name: str,
arguments: dict[str, Any],
context: ToolContext,
*,
tool_name: str | None = None,
) -> ToolResult:
exposed_name = tool_name or workflow_name
try:
if str(context.metadata.get("source") or "").startswith("team:"):
raise ValueError("nested_team_workflow_not_allowed")
builder = self._BUILDERS.get(workflow_name)
if builder is None:
raise ValueError(f"unknown team workflow tool: {workflow_name}")
graph = builder(**dict(arguments or {}))
parent_task_id = _task_id(context)
parent_session_id = _session_id(context)
result = await self._run_team(
context=context,
graph=graph,
parent_task_id=parent_task_id,
parent_session_id=parent_session_id,
)
payload = _success_payload(
workflow_name=workflow_name,
graph=graph,
result=result,
)
return ToolResult(
success=True,
content=json.dumps(payload, ensure_ascii=False),
tool_name=exposed_name,
raw_output=payload,
)
except Exception as exc:
payload = {
"success": False,
"workflow": workflow_name,
"error": str(exc),
}
return ToolResult(
success=False,
content=json.dumps(payload, ensure_ascii=False),
tool_name=exposed_name,
error=str(exc),
raw_output=payload,
)
async def _run_team(
self,
*,
context: ToolContext,
graph: ExecutionGraph,
parent_task_id: str,
parent_session_id: str,
) -> TeamRunResult:
runner = context.services.get("agent_team_runner")
parent_run_id = _run_id(context)
if runner is not None:
return await runner(
graph,
parent_task_id=parent_task_id,
parent_session_id=parent_session_id,
parent_run_id=parent_run_id,
)
agent_loop = context.services.get("agent_loop")
if agent_loop is None:
raise ValueError("team workflow execution requires agent_loop or agent_team_runner")
provider_bundle = context.services.get("provider_bundle")
def provider_bundle_factory(_node: Any) -> Any:
return provider_bundle
from beaver.engine import AgentLoop
from beaver.services.team_service import TeamService
loaded = context.services.get("loaded")
team_loop = AgentLoop(profile=agent_loop.profile, loader=agent_loop.loader)
team_loop.loaded = loaded
return await TeamService(team_loop).run_team(
graph,
parent_task_id=parent_task_id,
parent_session_id=parent_session_id,
parent_run_id=parent_run_id,
provider_bundle_factory=provider_bundle_factory if provider_bundle is not None else None,
allow_candidate_generation=False,
)
def _task_id(context: ToolContext) -> str:
value = str(context.services.get("task_id") or context.metadata.get("task_id") or "").strip()
if not value:
raise ValueError("team workflow execution requires task_id")
return value
def _session_id(context: ToolContext) -> str:
value = str(context.session_id or context.services.get("session_id") or "").strip()
if not value:
raise ValueError("team workflow execution requires session_id")
return value
def _run_id(context: ToolContext) -> str | None:
return str(context.services.get("run_id") or context.metadata.get("run_id") or "").strip() or None
def _success_payload(
*,
workflow_name: str,
graph: ExecutionGraph,
result: TeamRunResult,
) -> dict[str, Any]:
return {
"success": result.success,
"workflow": workflow_name,
"summary": result.summary,
"run_ids": list(result.run_ids),
"session_ids": list(result.session_ids),
"node_results": [item.to_dict() for item in result.node_results],
"graph": _graph_to_dict(graph),
}
def _graph_to_dict(graph: ExecutionGraph) -> dict[str, Any]:
return {
"strategy": graph.strategy,
"nodes": [
{
"node_id": node.node_id,
"task": node.task,
"depends_on": list(node.depends_on),
"allowed_tool_names": (
None if node.allowed_tool_names is None else list(node.allowed_tool_names)
),
"required_evidence": list(node.required_evidence),
"evidence_contract": dict(node.evidence_contract),
"validation_rules": list(node.validation_rules),
"required_for_completion": node.required_for_completion,
"block_downstream_on_partial": node.block_downstream_on_partial,
"max_tool_iterations": node.max_tool_iterations,
"metadata": dict(node.agent.metadata),
}
for node in graph.nodes
],
}