subagent.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """
  2. Subagent 工具 - 统一的 Sub-Agent 创建工具
  3. 统一 evaluate、delegate、explore 三个工具的功能
  4. """
  5. from typing import Optional, Dict, Any, List
  6. from agent.tools import tool
  7. @tool(description="创建 Sub-Agent 执行任务(评估/委托/探索)")
  8. async def subagent(
  9. mode: str, # "evaluate" | "delegate" | "explore"
  10. # 通用参数
  11. task: Optional[str] = None,
  12. # evaluate 专用参数
  13. target_goal_id: Optional[str] = None,
  14. evaluation_input: Optional[Dict] = None,
  15. requirements: Optional[str] = None,
  16. # explore 专用参数
  17. branches: Optional[List[str]] = None,
  18. background: Optional[str] = None,
  19. # 通用选项
  20. continue_from: Optional[str] = None,
  21. wait: bool = True,
  22. context: Optional[dict] = None
  23. ) -> Dict[str, Any]:
  24. """
  25. 创建 Sub-Agent 执行任务
  26. Args:
  27. mode: 模式 - "evaluate"(评估)、"delegate"(委托)、"explore"(探索)
  28. task: 任务描述(delegate/explore 使用)
  29. target_goal_id: 被评估的 Goal ID(evaluate 使用)
  30. evaluation_input: 评估输入(evaluate 使用)
  31. requirements: 评估要求(evaluate 使用)
  32. branches: 探索分支列表(explore 使用)
  33. background: 背景信息(explore 使用)
  34. continue_from: 继承的 trace ID(连续记忆)
  35. wait: 是否等待结果(默认 True)
  36. context: 工具执行上下文
  37. Returns:
  38. 根据 mode 返回不同格式的结果
  39. Examples:
  40. # 评估
  41. subagent(
  42. mode="evaluate",
  43. target_goal_id="3",
  44. evaluation_input={"actual_result": "已实现登录功能"}
  45. )
  46. # 委托
  47. subagent(mode="delegate", task="实现用户注册功能")
  48. # 探索
  49. subagent(mode="explore", branches=["JWT 方案", "Session 方案"])
  50. """
  51. from agent.services.subagent.manager import SubAgentManager
  52. if not context:
  53. return {"error": "context is required"}
  54. # 提取 context 参数
  55. store = context.get("store")
  56. trace_id = context.get("trace_id")
  57. goal_id = context.get("goal_id")
  58. run_agent = context.get("run_agent")
  59. # 验证必需参数
  60. missing = []
  61. if not store: missing.append("store")
  62. if not trace_id: missing.append("trace_id")
  63. if not run_agent: missing.append("run_agent")
  64. if missing:
  65. return {"error": f"Missing required context: {', '.join(missing)}"}
  66. # 验证 mode 参数
  67. if mode not in ["evaluate", "delegate", "explore"]:
  68. return {"error": f"Invalid mode: {mode}. Must be 'evaluate', 'delegate', or 'explore'"}
  69. # 构建 options
  70. options = {}
  71. if mode == "evaluate":
  72. if not target_goal_id or not evaluation_input:
  73. return {"error": "evaluate mode requires target_goal_id and evaluation_input"}
  74. options = {
  75. "target_goal_id": target_goal_id,
  76. "evaluation_input": evaluation_input,
  77. "requirements": requirements
  78. }
  79. elif mode == "delegate":
  80. if not task:
  81. return {"error": "delegate mode requires task"}
  82. options = {"task": task}
  83. elif mode == "explore":
  84. if not branches:
  85. return {"error": "explore mode requires branches"}
  86. options = {
  87. "branches": branches,
  88. "background": background
  89. }
  90. # 使用 SubAgentManager 执行
  91. manager = SubAgentManager(store, signal_bus=context.get("signal_bus"))
  92. result = await manager.execute(
  93. mode=mode,
  94. current_trace_id=trace_id,
  95. current_goal_id=goal_id,
  96. options=options,
  97. continue_from=continue_from,
  98. wait=wait,
  99. run_agent=run_agent
  100. )
  101. return result