goal_tool.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. """
  2. Goal 工具 - 计划管理
  3. 提供 goal 工具供 LLM 管理执行计划。
  4. """
  5. import logging
  6. from typing import Optional, List, TYPE_CHECKING
  7. from agent.tools import tool
  8. if TYPE_CHECKING:
  9. from .goal_models import GoalTree, Goal
  10. from .protocols import TraceStore
  11. logger = logging.getLogger(__name__)
  12. # ===== 知识注入 =====
  13. async def inject_knowledge_for_goal(
  14. goal: "Goal",
  15. tree: "GoalTree",
  16. store: Optional["TraceStore"] = None,
  17. trace_id: Optional[str] = None,
  18. knowledge_config: Optional[dict] = None,
  19. sequence: Optional[int] = None,
  20. ) -> Optional[str]:
  21. """
  22. 为指定 goal 注入相关知识。
  23. Args:
  24. goal: 目标对象
  25. tree: GoalTree
  26. store: TraceStore(用于持久化)
  27. trace_id: Trace ID
  28. knowledge_config: 知识管理配置(KnowledgeConfig 对象)
  29. sequence: 当前消息序列号(用于记录注入时机)
  30. Returns:
  31. 注入结果描述(如 "📚 已注入 3 条相关知识"),无结果返回 None
  32. """
  33. # 检查是否启用知识注入
  34. if knowledge_config and not getattr(knowledge_config, 'enable_injection', True):
  35. logger.debug(f"[Knowledge Inject] 知识注入已禁用,跳过")
  36. return None
  37. try:
  38. import re
  39. from agent.tools.builtin.subagent import _run_remote_agent
  40. logger.info(f"[Knowledge Inject] goal: {goal.id}, query: {goal.description[:80]}")
  41. # 通过统一 agent 工具调用远端 Librarian,明确走 ask 策略
  42. result = await _run_remote_agent(
  43. agent_type="remote_librarian",
  44. task=goal.description,
  45. messages=None,
  46. continue_from=None, # 知识注入每次独立查询,不续跑
  47. skills=["ask_strategy"],
  48. )
  49. if result.get("status") != "completed":
  50. logger.warning(f"[Knowledge Inject] 远端 Librarian 调用失败: {result.get('error')}")
  51. goal.knowledge = []
  52. return None
  53. response_text = result.get("summary", "")
  54. # 从 summary 中解析 source_ids(Librarian prompt 约定以 knowledge-xxx 形式引用)
  55. source_ids = re.findall(r'\[?(knowledge-[a-zA-Z0-9_-]+)\]?', response_text)
  56. source_ids = list(dict.fromkeys(source_ids)) # 去重保序
  57. if source_ids:
  58. goal.knowledge = [{"id": sid} for sid in source_ids]
  59. knowledge_count = len(source_ids)
  60. logger.info(f"[Knowledge Inject] 注入 {knowledge_count} 条知识到 goal {goal.id}")
  61. if store and trace_id:
  62. await store.update_goal_tree(trace_id, tree)
  63. if sequence is not None:
  64. await store.append_cognition_event(
  65. trace_id=trace_id,
  66. event={
  67. "type": "query",
  68. "sequence": sequence,
  69. "goal_id": goal.id,
  70. "query": goal.description,
  71. "response": response_text[:2000],
  72. "source_ids": source_ids,
  73. "sources": [],
  74. }
  75. )
  76. logger.info(f"[Knowledge Inject] 已记录 query 事件到 cognition_log")
  77. return f"📚 已注入 {knowledge_count} 条相关知识"
  78. else:
  79. goal.knowledge = []
  80. logger.info(f"[Knowledge Inject] 未找到相关知识")
  81. return None
  82. except Exception as e:
  83. logger.warning(f"[Knowledge Inject] 知识注入失败: {e}")
  84. goal.knowledge = []
  85. return None
  86. # ===== LLM 可调用的 goal 工具 =====
  87. @tool(description="管理执行计划,添加/完成/放弃目标,切换焦点", hidden_params=["context"], groups=["core"])
  88. async def goal(
  89. add: Optional[str] = None,
  90. reason: Optional[str] = None,
  91. after: Optional[str] = None,
  92. under: Optional[str] = None,
  93. done: Optional[str] = None,
  94. abandon: Optional[str] = None,
  95. focus: Optional[str] = None,
  96. context: Optional[dict] = None
  97. ) -> str:
  98. """
  99. 管理执行计划,添加/完成/放弃目标,切换焦点。
  100. Args:
  101. add: 添加目标(逗号分隔多个)
  102. reason: 创建理由(逗号分隔多个,与 add 一一对应)
  103. after: 在指定目标后面添加(同层级)
  104. under: 为指定目标添加子目标
  105. done: 完成当前目标,值为 summary
  106. abandon: 放弃当前目标,值为原因
  107. focus: 切换焦点到指定 ID
  108. context: 工具执行上下文(包含 store、trace_id、goal_tree)
  109. Returns:
  110. str: 更新后的计划状态文本
  111. """
  112. # GoalTree 从 context 获取,每个 agent 实例独立,不再依赖全局变量
  113. tree = context.get("goal_tree") if context else None
  114. if tree is None:
  115. return "错误:GoalTree 未初始化"
  116. # 从 context 获取 store、trace_id 和 knowledge_config
  117. store = context.get("store") if context else None
  118. trace_id = context.get("trace_id") if context else None
  119. knowledge_config = context.get("knowledge_config") if context else None
  120. return await goal_tool(
  121. tree=tree,
  122. store=store,
  123. trace_id=trace_id,
  124. add=add,
  125. reason=reason,
  126. after=after,
  127. under=under,
  128. done=done,
  129. abandon=abandon,
  130. focus=focus,
  131. knowledge_config=knowledge_config,
  132. context=context
  133. )
  134. # ===== 核心逻辑函数 =====
  135. async def goal_tool(
  136. tree: "GoalTree",
  137. store: Optional["TraceStore"] = None,
  138. trace_id: Optional[str] = None,
  139. add: Optional[str] = None,
  140. reason: Optional[str] = None,
  141. after: Optional[str] = None,
  142. under: Optional[str] = None,
  143. done: Optional[str] = None,
  144. abandon: Optional[str] = None,
  145. focus: Optional[str] = None,
  146. knowledge_config: Optional[object] = None,
  147. context: Optional[dict] = None,
  148. ) -> str:
  149. """
  150. 管理执行计划。
  151. Args:
  152. tree: GoalTree 实例
  153. store: TraceStore 实例(用于推送事件)
  154. trace_id: 当前 Trace ID
  155. add: 添加目标(逗号分隔多个)
  156. reason: 创建理由(逗号分隔多个,与 add 一一对应)
  157. after: 在指定目标后面添加(同层级)
  158. under: 为指定目标添加子目标
  159. done: 完成当前目标,值为 summary
  160. abandon: 放弃当前目标,值为原因
  161. focus: 切换焦点到指定 ID
  162. knowledge_config: 知识管理配置(KnowledgeConfig 对象)
  163. Returns:
  164. 更新后的计划状态文本
  165. """
  166. changes = []
  167. # 1. 处理 done(完成当前目标)
  168. if done is not None:
  169. if not tree.current_id:
  170. return f"错误:没有当前目标可以完成。当前焦点为空,请先使用 focus 参数切换到要完成的目标。\n\n当前计划:\n{tree.to_prompt()}"
  171. # 完成当前目标
  172. # 如果同时指定了 focus,则不清空焦点(后面会切换到新目标)
  173. # 如果只有 done,则清空焦点
  174. clear_focus = (focus is None)
  175. goal = tree.complete(tree.current_id, done, clear_focus=clear_focus)
  176. display_id = tree._generate_display_id(goal)
  177. changes.append(f"已完成: {display_id}. {goal.description}")
  178. # 推送事件
  179. if store and trace_id:
  180. await store.update_goal(trace_id, goal.id, status="completed", summary=done)
  181. # 检查是否有级联完成的父目标(complete方法已经处理,这里只需要记录)
  182. if goal.parent_id:
  183. parent = tree.find(goal.parent_id)
  184. if parent and parent.status == "completed":
  185. parent_display_id = tree._generate_display_id(parent)
  186. changes.append(f"自动完成: {parent_display_id}. {parent.description}(所有子目标已完成)")
  187. # 2. 处理 focus(切换焦点到新目标)
  188. if focus is not None:
  189. goal = tree.find_by_display_id(focus)
  190. if not goal:
  191. return f"错误:找不到目标 {focus}\n\n当前计划:\n{tree.to_prompt()}"
  192. tree.focus(goal.id)
  193. display_id = tree._generate_display_id(goal)
  194. changes.append(f"切换焦点: {display_id}. {goal.description}")
  195. # 自动注入知识
  196. inject_msg = await inject_knowledge_for_goal(
  197. goal, tree, store, trace_id, knowledge_config, sequence=context.get("sequence")
  198. )
  199. if inject_msg:
  200. changes.append(inject_msg)
  201. # 3. 处理 abandon(放弃当前目标)
  202. if abandon is not None:
  203. if not tree.current_id:
  204. return f"错误:没有当前目标可以放弃。当前焦点为空。\n\n当前计划:\n{tree.to_prompt()}"
  205. goal = tree.abandon(tree.current_id, abandon)
  206. display_id = tree._generate_display_id(goal)
  207. changes.append(f"已放弃: {display_id}. {goal.description}")
  208. # 推送事件
  209. if store and trace_id:
  210. await store.update_goal(trace_id, goal.id, status="abandoned", summary=abandon)
  211. # 4. 处理 add
  212. if add is not None:
  213. # 检查 after 和 under 互斥
  214. if after is not None and under is not None:
  215. return "错误:after 和 under 参数不能同时指定"
  216. descriptions = [d.strip() for d in add.split(",") if d.strip()]
  217. if descriptions:
  218. # 解析 reasons(与 descriptions 一一对应)
  219. reasons = None
  220. if reason:
  221. reasons = [r.strip() for r in reason.split(",")]
  222. # 如果 reasons 数量少于 descriptions,补空字符串
  223. while len(reasons) < len(descriptions):
  224. reasons.append("")
  225. # 确定添加位置
  226. if after is not None:
  227. # 在指定 goal 后面添加(同层级)
  228. target_goal = tree.find_by_display_id(after)
  229. if not target_goal:
  230. return f"错误:找不到目标 {after}\n\n当前计划:\n{tree.to_prompt()}"
  231. new_goals = tree.add_goals_after(target_goal.id, descriptions, reasons=reasons)
  232. changes.append(f"在 {tree._generate_display_id(target_goal)} 后面添加 {len(new_goals)} 个同级目标")
  233. elif under is not None:
  234. # 为指定 goal 添加子目标
  235. parent_goal = tree.find_by_display_id(under)
  236. if not parent_goal:
  237. return f"错误:找不到目标 {under}\n\n当前计划:\n{tree.to_prompt()}"
  238. new_goals = tree.add_goals(descriptions, reasons=reasons, parent_id=parent_goal.id)
  239. changes.append(f"在 {tree._generate_display_id(parent_goal)} 下添加 {len(new_goals)} 个子目标")
  240. else:
  241. # 默认行为:添加到当前焦点下(如果有焦点),否则添加到顶层
  242. parent_id = tree.current_id
  243. new_goals = tree.add_goals(descriptions, reasons=reasons, parent_id=parent_id)
  244. if parent_id:
  245. parent_display_id = tree._generate_display_id(tree.find(parent_id))
  246. changes.append(f"在 {parent_display_id} 下添加 {len(new_goals)} 个子目标")
  247. else:
  248. changes.append(f"添加 {len(new_goals)} 个顶层目标")
  249. # 推送事件
  250. if store and trace_id:
  251. for goal in new_goals:
  252. await store.add_goal(trace_id, goal)
  253. # 将完整内存树状态(含 current_id)同步到存储,
  254. # 因为 store.add_goal / update_goal 各自从磁盘加载,不包含 focus 等内存变更
  255. if store and trace_id and changes:
  256. await store.update_goal_tree(trace_id, tree)
  257. # 返回当前状态
  258. result = []
  259. if changes:
  260. result.append("## 更新")
  261. result.extend(f"- {c}" for c in changes)
  262. result.append("")
  263. result.append("## Current Plan")
  264. result.append(tree.to_prompt())
  265. return "\n".join(result)
  266. def create_goal_tool_schema() -> dict:
  267. """创建 goal 工具的 JSON Schema"""
  268. return {
  269. "name": "goal",
  270. "description": """管理执行计划。目标工具是灵活的支持系统,帮助你组织和追踪工作进度。
  271. 使用策略(按需选择):
  272. - 全局规划:先规划所有目标,再逐个执行
  273. - 渐进规划:走一步看一步,每次只创建下一个目标
  274. - 动态调整:行动中随时 abandon 不可行的目标,创建新目标
  275. 参数:
  276. - add: 添加目标(逗号分隔多个)
  277. - reason: 创建理由(逗号分隔,与 add 一一对应)
  278. - after: 在指定目标后面添加同级目标。使用目标 ID。
  279. - under: 为指定目标添加子目标。使用目标 ID。如已有子目标,追加到最后。
  280. - done: 完成当前目标,值为 summary(记录关键结论)
  281. - abandon: 放弃当前目标,值为原因
  282. - focus: 切换焦点到指定目标。使用目标 ID。
  283. 位置控制(优先使用 after):
  284. - 不指定 after/under: 添加到当前 focus 下作为子目标(无 focus 时添加到顶层)
  285. - after="X": 在目标 X 后面添加兄弟节点(同层级)
  286. - under="X": 为目标 X 添加子目标
  287. - after 和 under 不能同时指定
  288. 执行顺序:
  289. - done → focus → abandon → add
  290. - 如果同时指定 done 和 focus,会先完成当前目标,再切换焦点到新目标
  291. 示例:
  292. - goal(add="分析代码, 实现功能, 测试") - 添加顶层目标
  293. - goal(add="设计接口, 实现代码", under="2") - 为目标2添加子目标
  294. - goal(add="编写文档", after="3") - 在目标3后面添加同级任务
  295. - goal(add="集成测试", after="2.2") - 在目标2.2后面添加同级任务
  296. - goal(done="发现用户模型在 models/user.py") - 完成当前目标
  297. - goal(done="已完成调研", focus="2") - 完成当前目标,切换到目标2
  298. - goal(abandon="方案A需要Redis,环境没有") - 放弃当前目标
  299. 注意:
  300. - 目标 ID 的格式为 "1", "2", "2.1", "2.2" 等,在计划视图中可以看到
  301. - reason 应该与 add 的目标数量一致,如果数量不一致,缺少的 reason 将为空
  302. """,
  303. "parameters": {
  304. "type": "object",
  305. "properties": {
  306. "add": {
  307. "type": "string",
  308. "description": "添加目标(逗号分隔多个)"
  309. },
  310. "reason": {
  311. "type": "string",
  312. "description": "创建理由(逗号分隔多个,与 add 一一对应)。说明为什么要做这些目标。"
  313. },
  314. "after": {
  315. "type": "string",
  316. "description": "在指定目标后面添加(同层级)。使用目标的 ID,如 \"2\" 或 \"2.1\"。"
  317. },
  318. "under": {
  319. "type": "string",
  320. "description": "为指定目标添加子目标。使用目标的 ID,如 \"2\" 或 \"2.1\"。"
  321. },
  322. "done": {
  323. "type": "string",
  324. "description": "完成当前目标,值为 summary"
  325. },
  326. "abandon": {
  327. "type": "string",
  328. "description": "放弃当前目标,值为原因"
  329. },
  330. "focus": {
  331. "type": "string",
  332. "description": "切换焦点到指定目标。使用目标的 ID,如 \"2\" 或 \"2.1\"。"
  333. }
  334. },
  335. "required": []
  336. }
  337. }