| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- """
- Goal 工具 - 计划管理
- 提供 goal 工具供 LLM 管理执行计划。
- """
- from typing import Optional, List, TYPE_CHECKING
- if TYPE_CHECKING:
- from agent.goal.models import GoalTree
- def goal_tool(
- tree: "GoalTree",
- add: Optional[str] = None,
- done: Optional[str] = None,
- abandon: Optional[str] = None,
- focus: Optional[str] = None,
- ) -> str:
- """
- 管理执行计划。
- Args:
- tree: GoalTree 实例
- add: 添加目标(逗号分隔多个)。添加到当前 focus 的 goal 下作为子目标。
- done: 完成当前目标,值为 summary
- abandon: 放弃当前目标,值为原因
- focus: 切换焦点到指定内部 id
- Returns:
- 更新后的计划状态文本
- """
- changes = []
- # 1. 处理 abandon(先处理,因为可能需要在 add 新目标前放弃旧的)
- if abandon is not None:
- if not tree.current_id:
- return "错误:没有当前目标可以放弃"
- goal = tree.abandon(tree.current_id, abandon)
- display_id = tree._generate_display_id(goal)
- changes.append(f"已放弃: {display_id}. {goal.description}")
- # 2. 处理 done
- if done is not None:
- if not tree.current_id:
- return "错误:没有当前目标可以完成"
- goal = tree.complete(tree.current_id, done)
- display_id = tree._generate_display_id(goal)
- changes.append(f"已完成: {display_id}. {goal.description}")
- # 检查是否有级联完成的父目标
- if goal.parent_id:
- parent = tree.find(goal.parent_id)
- if parent and parent.status == "completed":
- parent_display_id = tree._generate_display_id(parent)
- changes.append(f"自动完成: {parent_display_id}. {parent.description}(所有子目标已完成)")
- # 3. 处理 focus(在 add 之前,这样 add 可以添加到新焦点下)
- if focus is not None:
- # focus 参数可以是内部 ID 或显示 ID
- # 先尝试作为内部 ID 查找
- goal = tree.find(focus)
- # 如果找不到,尝试根据显示 ID 查找
- if not goal:
- # 通过遍历所有 goal 查找匹配的显示 ID
- for g in tree.goals:
- if tree._generate_display_id(g) == focus:
- goal = g
- break
- if not goal:
- return f"错误:找不到目标 {focus}"
- tree.focus(goal.id)
- display_id = tree._generate_display_id(goal)
- changes.append(f"切换焦点: {display_id}. {goal.description}")
- # 4. 处理 add
- if add is not None:
- descriptions = [d.strip() for d in add.split(",") if d.strip()]
- if descriptions:
- # 添加到当前焦点下(如果有焦点),否则添加到顶层
- parent_id = tree.current_id
- new_goals = tree.add_goals(descriptions, parent_id=parent_id)
- if parent_id:
- parent_display_id = tree._generate_display_id(tree.find(parent_id))
- changes.append(f"在 {parent_display_id} 下添加 {len(new_goals)} 个子目标")
- else:
- changes.append(f"添加 {len(new_goals)} 个顶层目标")
- # 如果没有焦点且添加了目标,自动 focus 到第一个新目标
- if not tree.current_id and new_goals:
- tree.focus(new_goals[0].id)
- display_id = tree._generate_display_id(new_goals[0])
- changes.append(f"自动切换焦点: {display_id}")
- # 返回当前状态
- result = []
- if changes:
- result.append("## 更新")
- result.extend(f"- {c}" for c in changes)
- result.append("")
- result.append("## Current Plan")
- result.append(tree.to_prompt())
- return "\n".join(result)
- def create_goal_tool_schema() -> dict:
- """创建 goal 工具的 JSON Schema"""
- return {
- "name": "goal",
- "description": """管理执行计划。
- - add: 添加目标(逗号分隔多个)。添加到当前 focus 的 goal 下作为子目标。
- - done: 完成当前目标,值为 summary
- - abandon: 放弃当前目标,值为原因(会触发 context 压缩)
- - focus: 切换焦点到指定 id(可以是内部 ID 或显示 ID)
- 示例:
- - goal(add="分析代码, 实现功能, 测试") - 添加顶层目标
- - goal(focus="2", add="设计接口, 实现代码") - 切换到目标2,并添加子目标
- - goal(done="发现用户模型在 models/user.py") - 完成当前目标
- - goal(abandon="方案A需要Redis,环境没有", add="实现方案B") - 放弃当前并添加新目标
- 注意:内部 ID 是纯自增数字("1", "2", "3"),显示 ID 是带层级的("1", "2.1", "2.2")。
- focus 参数可以使用任意格式的 ID。
- """,
- "parameters": {
- "type": "object",
- "properties": {
- "add": {
- "type": "string",
- "description": "添加目标(逗号分隔多个)。添加到当前 focus 的 goal 下作为子目标。"
- },
- "done": {
- "type": "string",
- "description": "完成当前目标,值为 summary"
- },
- "abandon": {
- "type": "string",
- "description": "放弃当前目标,值为原因"
- },
- "focus": {
- "type": "string",
- "description": "切换焦点到指定 goal id(可以是内部 ID 或显示 ID)"
- }
- },
- "required": []
- }
- }
|