goal.py 1.8 KB

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