goal_tool.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. """
  2. Goal 工具 - 计划管理
  3. 提供 goal 工具供 LLM 管理执行计划。
  4. """
  5. from typing import Optional, List, TYPE_CHECKING
  6. from agent.tools import tool
  7. if TYPE_CHECKING:
  8. from .goal_models import GoalTree
  9. from .protocols import TraceStore
  10. # ===== LLM 可调用的 goal 工具 =====
  11. @tool(description="管理执行计划,添加/完成/放弃目标,切换焦点")
  12. async def goal(
  13. add: Optional[str] = None,
  14. reason: Optional[str] = None,
  15. after: Optional[str] = None,
  16. under: Optional[str] = None,
  17. done: Optional[str] = None,
  18. abandon: Optional[str] = None,
  19. focus: Optional[str] = None,
  20. context: Optional[dict] = None
  21. ) -> str:
  22. """
  23. 管理执行计划,添加/完成/放弃目标,切换焦点。
  24. Args:
  25. add: 添加目标(逗号分隔多个)
  26. reason: 创建理由(逗号分隔多个,与 add 一一对应)
  27. after: 在指定目标后面添加(同层级)
  28. under: 为指定目标添加子目标
  29. done: 完成当前目标,值为 summary
  30. abandon: 放弃当前目标,值为原因
  31. focus: 切换焦点到指定 ID
  32. context: 工具执行上下文(包含 store、trace_id、goal_tree)
  33. Returns:
  34. str: 更新后的计划状态文本
  35. """
  36. # GoalTree 从 context 获取,每个 agent 实例独立,不再依赖全局变量
  37. tree = context.get("goal_tree") if context else None
  38. if tree is None:
  39. return "错误:GoalTree 未初始化"
  40. # 从 context 获取 store 和 trace_id
  41. store = context.get("store") if context else None
  42. trace_id = context.get("trace_id") if context else None
  43. return await goal_tool(
  44. tree=tree,
  45. store=store,
  46. trace_id=trace_id,
  47. add=add,
  48. reason=reason,
  49. after=after,
  50. under=under,
  51. done=done,
  52. abandon=abandon,
  53. focus=focus
  54. )
  55. # ===== 核心逻辑函数 =====
  56. async def goal_tool(
  57. tree: "GoalTree",
  58. store: Optional["TraceStore"] = None,
  59. trace_id: Optional[str] = None,
  60. add: Optional[str] = None,
  61. reason: Optional[str] = None,
  62. after: Optional[str] = None,
  63. under: Optional[str] = None,
  64. done: Optional[str] = None,
  65. abandon: Optional[str] = None,
  66. focus: Optional[str] = None,
  67. ) -> str:
  68. """
  69. 管理执行计划。
  70. Args:
  71. tree: GoalTree 实例
  72. store: TraceStore 实例(用于推送事件)
  73. trace_id: 当前 Trace ID
  74. add: 添加目标(逗号分隔多个)
  75. reason: 创建理由(逗号分隔多个,与 add 一一对应)
  76. after: 在指定目标后面添加(同层级)
  77. under: 为指定目标添加子目标
  78. done: 完成当前目标,值为 summary
  79. abandon: 放弃当前目标,值为原因
  80. focus: 切换焦点到指定 ID
  81. Returns:
  82. 更新后的计划状态文本
  83. """
  84. changes = []
  85. # 1. 处理 done(完成当前目标)
  86. if done is not None:
  87. if not tree.current_id:
  88. return f"错误:没有当前目标可以完成。当前焦点为空,请先使用 focus 参数切换到要完成的目标。\n\n当前计划:\n{tree.to_prompt()}"
  89. # 完成当前目标
  90. # 如果同时指定了 focus,则不清空焦点(后面会切换到新目标)
  91. # 如果只有 done,则清空焦点
  92. clear_focus = (focus is None)
  93. goal = tree.complete(tree.current_id, done, clear_focus=clear_focus)
  94. display_id = tree._generate_display_id(goal)
  95. changes.append(f"已完成: {display_id}. {goal.description}")
  96. # 推送事件
  97. if store and trace_id:
  98. await store.update_goal(trace_id, goal.id, status="completed", summary=done)
  99. # 检查是否有级联完成的父目标(complete方法已经处理,这里只需要记录)
  100. if goal.parent_id:
  101. parent = tree.find(goal.parent_id)
  102. if parent and parent.status == "completed":
  103. parent_display_id = tree._generate_display_id(parent)
  104. changes.append(f"自动完成: {parent_display_id}. {parent.description}(所有子目标已完成)")
  105. # 2. 处理 focus(切换焦点到新目标)
  106. if focus is not None:
  107. goal = tree.find_by_display_id(focus)
  108. if not goal:
  109. return f"错误:找不到目标 {focus}\n\n当前计划:\n{tree.to_prompt()}"
  110. tree.focus(goal.id)
  111. display_id = tree._generate_display_id(goal)
  112. changes.append(f"切换焦点: {display_id}. {goal.description}")
  113. # 3. 处理 abandon(放弃当前目标)
  114. if abandon is not None:
  115. if not tree.current_id:
  116. return f"错误:没有当前目标可以放弃。当前焦点为空。\n\n当前计划:\n{tree.to_prompt()}"
  117. goal = tree.abandon(tree.current_id, abandon)
  118. display_id = tree._generate_display_id(goal)
  119. changes.append(f"已放弃: {display_id}. {goal.description}")
  120. # 推送事件
  121. if store and trace_id:
  122. await store.update_goal(trace_id, goal.id, status="abandoned", summary=abandon)
  123. # 4. 处理 add
  124. if add is not None:
  125. # 检查 after 和 under 互斥
  126. if after is not None and under is not None:
  127. return "错误:after 和 under 参数不能同时指定"
  128. descriptions = [d.strip() for d in add.split(",") if d.strip()]
  129. if descriptions:
  130. # 解析 reasons(与 descriptions 一一对应)
  131. reasons = None
  132. if reason:
  133. reasons = [r.strip() for r in reason.split(",")]
  134. # 如果 reasons 数量少于 descriptions,补空字符串
  135. while len(reasons) < len(descriptions):
  136. reasons.append("")
  137. # 确定添加位置
  138. if after is not None:
  139. # 在指定 goal 后面添加(同层级)
  140. target_goal = tree.find_by_display_id(after)
  141. if not target_goal:
  142. return f"错误:找不到目标 {after}\n\n当前计划:\n{tree.to_prompt()}"
  143. new_goals = tree.add_goals_after(target_goal.id, descriptions, reasons=reasons)
  144. changes.append(f"在 {tree._generate_display_id(target_goal)} 后面添加 {len(new_goals)} 个同级目标")
  145. elif under is not None:
  146. # 为指定 goal 添加子目标
  147. parent_goal = tree.find_by_display_id(under)
  148. if not parent_goal:
  149. return f"错误:找不到目标 {under}\n\n当前计划:\n{tree.to_prompt()}"
  150. new_goals = tree.add_goals(descriptions, reasons=reasons, parent_id=parent_goal.id)
  151. changes.append(f"在 {tree._generate_display_id(parent_goal)} 下添加 {len(new_goals)} 个子目标")
  152. else:
  153. # 默认行为:添加到当前焦点下(如果有焦点),否则添加到顶层
  154. parent_id = tree.current_id
  155. new_goals = tree.add_goals(descriptions, reasons=reasons, parent_id=parent_id)
  156. if parent_id:
  157. parent_display_id = tree._generate_display_id(tree.find(parent_id))
  158. changes.append(f"在 {parent_display_id} 下添加 {len(new_goals)} 个子目标")
  159. else:
  160. changes.append(f"添加 {len(new_goals)} 个顶层目标")
  161. # 推送事件
  162. if store and trace_id:
  163. for goal in new_goals:
  164. await store.add_goal(trace_id, goal)
  165. # 如果没有焦点且添加了目标,自动 focus 到第一个新目标
  166. if not tree.current_id and new_goals:
  167. tree.focus(new_goals[0].id)
  168. display_id = tree._generate_display_id(new_goals[0])
  169. changes.append(f"自动切换焦点: {display_id}")
  170. # 将完整内存树状态(含 current_id)同步到存储,
  171. # 因为 store.add_goal / update_goal 各自从磁盘加载,不包含 focus 等内存变更
  172. if store and trace_id and changes:
  173. await store.update_goal_tree(trace_id, tree)
  174. # 返回当前状态
  175. result = []
  176. if changes:
  177. result.append("## 更新")
  178. result.extend(f"- {c}" for c in changes)
  179. result.append("")
  180. result.append("## Current Plan")
  181. result.append(tree.to_prompt())
  182. return "\n".join(result)
  183. def create_goal_tool_schema() -> dict:
  184. """创建 goal 工具的 JSON Schema"""
  185. return {
  186. "name": "goal",
  187. "description": """管理执行计划。目标工具是灵活的支持系统,帮助你组织和追踪工作进度。
  188. 使用策略(按需选择):
  189. - 全局规划:先规划所有目标,再逐个执行
  190. - 渐进规划:走一步看一步,每次只创建下一个目标
  191. - 动态调整:行动中随时 abandon 不可行的目标,创建新目标
  192. 参数:
  193. - add: 添加目标(逗号分隔多个)
  194. - reason: 创建理由(逗号分隔,与 add 一一对应)
  195. - after: 在指定目标后面添加同级目标。使用目标 ID。
  196. - under: 为指定目标添加子目标。使用目标 ID。如已有子目标,追加到最后。
  197. - done: 完成当前目标,值为 summary(记录关键结论)
  198. - abandon: 放弃当前目标,值为原因
  199. - focus: 切换焦点到指定目标。使用目标 ID。
  200. 位置控制(优先使用 after):
  201. - 不指定 after/under: 添加到当前 focus 下作为子目标(无 focus 时添加到顶层)
  202. - after="X": 在目标 X 后面添加兄弟节点(同层级)
  203. - under="X": 为目标 X 添加子目标
  204. - after 和 under 不能同时指定
  205. 执行顺序:
  206. - done → focus → abandon → add
  207. - 如果同时指定 done 和 focus,会先完成当前目标,再切换焦点到新目标
  208. 示例:
  209. - goal(add="分析代码, 实现功能, 测试") - 添加顶层目标
  210. - goal(add="设计接口, 实现代码", under="2") - 为目标2添加子目标
  211. - goal(add="编写文档", after="3") - 在目标3后面添加同级任务
  212. - goal(add="集成测试", after="2.2") - 在目标2.2后面添加同级任务
  213. - goal(done="发现用户模型在 models/user.py") - 完成当前目标
  214. - goal(done="已完成调研", focus="2") - 完成当前目标,切换到目标2
  215. - goal(abandon="方案A需要Redis,环境没有") - 放弃当前目标
  216. 注意:
  217. - 目标 ID 的格式为 "1", "2", "2.1", "2.2" 等,在计划视图中可以看到
  218. - reason 应该与 add 的目标数量一致,如果数量不一致,缺少的 reason 将为空
  219. """,
  220. "parameters": {
  221. "type": "object",
  222. "properties": {
  223. "add": {
  224. "type": "string",
  225. "description": "添加目标(逗号分隔多个)"
  226. },
  227. "reason": {
  228. "type": "string",
  229. "description": "创建理由(逗号分隔多个,与 add 一一对应)。说明为什么要做这些目标。"
  230. },
  231. "after": {
  232. "type": "string",
  233. "description": "在指定目标后面添加(同层级)。使用目标的 ID,如 \"2\" 或 \"2.1\"。"
  234. },
  235. "under": {
  236. "type": "string",
  237. "description": "为指定目标添加子目标。使用目标的 ID,如 \"2\" 或 \"2.1\"。"
  238. },
  239. "done": {
  240. "type": "string",
  241. "description": "完成当前目标,值为 summary"
  242. },
  243. "abandon": {
  244. "type": "string",
  245. "description": "放弃当前目标,值为原因"
  246. },
  247. "focus": {
  248. "type": "string",
  249. "description": "切换焦点到指定目标。使用目标的 ID,如 \"2\" 或 \"2.1\"。"
  250. }
  251. },
  252. "required": []
  253. }
  254. }