goal.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """
  2. Goal 工具 - 执行计划管理
  3. 提供 LLM 可调用的 goal 工具,用于管理执行计划(GoalTree)。
  4. """
  5. from typing import Optional
  6. from agent.tools import tool
  7. # 全局 GoalTree 引用(由 AgentRunner 注入)
  8. _current_goal_tree = None
  9. def set_goal_tree(tree):
  10. """设置当前 GoalTree(由 AgentRunner 调用)"""
  11. global _current_goal_tree
  12. _current_goal_tree = tree
  13. def get_goal_tree():
  14. """获取当前 GoalTree"""
  15. return _current_goal_tree
  16. @tool(description="管理执行计划,添加/完成/放弃目标,切换焦点")
  17. def goal(
  18. add: Optional[str] = None,
  19. done: Optional[str] = None,
  20. abandon: Optional[str] = None,
  21. focus: Optional[str] = None,
  22. uid: str = ""
  23. ) -> str:
  24. """
  25. 管理执行计划。
  26. 参数:
  27. add: 添加目标(逗号分隔多个)。添加到当前 focus 的 goal 下作为子目标。
  28. done: 完成当前目标,值为 summary
  29. abandon: 放弃当前目标,值为原因(会触发 context 压缩)
  30. focus: 切换焦点到指定 id(可以是内部 ID 或显示 ID)
  31. 示例:
  32. goal(add="分析代码, 实现功能, 测试") - 添加顶层目标
  33. goal(focus="2", add="设计接口, 实现代码") - 切换到目标2,并添加子目标
  34. goal(done="发现用户模型在 models/user.py") - 完成当前目标
  35. goal(abandon="方案A需要Redis,环境没有", add="实现方案B") - 放弃当前并添加新目标
  36. 注意: 内部 ID 是纯自增数字("1", "2", "3"),显示 ID 是带层级的("1", "2.1", "2.2")。
  37. focus 参数可以使用任意格式的 ID。
  38. 返回:
  39. str: 更新后的计划状态文本
  40. """
  41. from agent.goal.tool import goal_tool
  42. tree = get_goal_tree()
  43. if tree is None:
  44. return "错误:GoalTree 未初始化"
  45. return goal_tool(
  46. tree=tree,
  47. add=add,
  48. done=done,
  49. abandon=abandon,
  50. focus=focus
  51. )