runner.py 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. """
  2. Agent Runner - Agent 执行引擎
  3. 核心职责:
  4. 1. 执行 Agent 任务(循环调用 LLM + 工具)
  5. 2. 记录执行轨迹(Trace + Messages + GoalTree)
  6. 3. 检索和注入记忆(Experience + Skill)
  7. 4. 管理执行计划(GoalTree)
  8. 5. 支持续跑(continue)和回溯重跑(rewind)
  9. 参数分层:
  10. - Infrastructure: AgentRunner 构造时设置(trace_store, llm_call 等)
  11. - RunConfig: 每次 run 时指定(model, trace_id, after_sequence 等)
  12. - Messages: OpenAI SDK 格式的任务消息
  13. """
  14. import asyncio
  15. import json
  16. import logging
  17. import os
  18. import uuid
  19. from dataclasses import dataclass, field
  20. from datetime import datetime
  21. from typing import AsyncIterator, Optional, Dict, Any, List, Callable, Literal, Tuple, Union
  22. from agent.trace.models import Trace, Message
  23. from agent.trace.protocols import TraceStore
  24. from agent.trace.goal_models import GoalTree
  25. from agent.tools.builtin.knowledge import knowledge_save, knowledge_batch_update
  26. from agent.trace.compaction import (
  27. CompressionConfig,
  28. filter_by_goal_status,
  29. estimate_tokens,
  30. needs_level2_compression,
  31. build_compression_prompt,
  32. build_reflect_prompt,
  33. )
  34. from agent.memory.models import Skill
  35. from agent.memory.protocols import MemoryStore, StateStore
  36. from agent.memory.skill_loader import load_skills_from_dir
  37. from agent.tools import ToolRegistry, get_tool_registry
  38. logger = logging.getLogger(__name__)
  39. # ===== 运行配置 =====
  40. @dataclass
  41. class RunConfig:
  42. """
  43. 运行参数 — 控制 Agent 如何执行
  44. 分为模型层参数(由上游 agent 或用户决定)和框架层参数(由系统注入)。
  45. """
  46. # --- 模型层参数 ---
  47. model: str = "gpt-4o"
  48. temperature: float = 0.3
  49. max_iterations: int = 200
  50. tools: Optional[List[str]] = None # None = 全部已注册工具
  51. # --- 框架层参数 ---
  52. agent_type: str = "default"
  53. uid: Optional[str] = None
  54. system_prompt: Optional[str] = None # None = 从 skills 自动构建
  55. skills: Optional[List[str]] = None # 注入 system prompt 的 skill 名称列表;None = 按 preset 决定
  56. enable_memory: bool = True
  57. auto_execute_tools: bool = True
  58. name: Optional[str] = None # 显示名称(空则由 utility_llm 自动生成)
  59. enable_prompt_caching: bool = True # 启用 Anthropic Prompt Caching(仅 Claude 模型有效)
  60. # --- Trace 控制 ---
  61. trace_id: Optional[str] = None # None = 新建
  62. parent_trace_id: Optional[str] = None # 子 Agent 专用
  63. parent_goal_id: Optional[str] = None
  64. # --- 续跑控制 ---
  65. after_sequence: Optional[int] = None # 从哪条消息后续跑(message sequence)
  66. # --- 额外 LLM 参数(传给 llm_call 的 **kwargs)---
  67. extra_llm_params: Dict[str, Any] = field(default_factory=dict)
  68. # --- 研究流程控制 ---
  69. enable_research_flow: bool = True # 是否启用自动研究流程(知识检索→经验检索→调研→计划)
  70. # 内置工具列表(始终自动加载)
  71. BUILTIN_TOOLS = [
  72. # 文件操作工具
  73. "read_file",
  74. "edit_file",
  75. "write_file",
  76. "glob_files",
  77. "grep_content",
  78. # 系统工具
  79. "bash_command",
  80. # 技能和目标管理
  81. "skill",
  82. "list_skills",
  83. "goal",
  84. "agent",
  85. "evaluate",
  86. # 搜索工具
  87. "search_posts",
  88. "get_search_suggestions",
  89. # 知识管理工具
  90. "knowledge_search",
  91. "knowledge_save",
  92. "knowledge_update",
  93. "knowledge_batch_update",
  94. "knowledge_list",
  95. "knowledge_slim",
  96. # 沙箱工具
  97. # "sandbox_create_environment",
  98. # "sandbox_run_shell",
  99. # "sandbox_rebuild_with_ports",
  100. # "sandbox_destroy_environment",
  101. # 浏览器工具
  102. "browser_navigate_to_url",
  103. "browser_search_web",
  104. "browser_go_back",
  105. "browser_wait",
  106. "browser_click_element",
  107. "browser_input_text",
  108. "browser_send_keys",
  109. "browser_upload_file",
  110. "browser_scroll_page",
  111. "browser_find_text",
  112. "browser_screenshot",
  113. "browser_switch_tab",
  114. "browser_close_tab",
  115. "browser_get_dropdown_options",
  116. "browser_select_dropdown_option",
  117. "browser_extract_content",
  118. "browser_read_long_content",
  119. "browser_download_direct_url",
  120. "browser_get_page_html",
  121. "browser_get_visual_selector_map",
  122. "browser_evaluate",
  123. "browser_ensure_login_with_cookies",
  124. "browser_wait_for_user_action",
  125. "browser_done",
  126. "browser_export_cookies",
  127. "browser_load_cookies"
  128. ]
  129. # ===== 向后兼容 =====
  130. @dataclass
  131. class AgentConfig:
  132. """[向后兼容] Agent 配置,新代码请使用 RunConfig"""
  133. agent_type: str = "default"
  134. max_iterations: int = 200
  135. enable_memory: bool = True
  136. auto_execute_tools: bool = True
  137. @dataclass
  138. class CallResult:
  139. """单次调用结果"""
  140. reply: str
  141. tool_calls: Optional[List[Dict]] = None
  142. trace_id: Optional[str] = None
  143. step_id: Optional[str] = None
  144. tokens: Optional[Dict[str, int]] = None
  145. cost: float = 0.0
  146. # ===== 执行引擎 =====
  147. CONTEXT_INJECTION_INTERVAL = 10 # 每 N 轮注入一次 GoalTree + Collaborators
  148. class AgentRunner:
  149. """
  150. Agent 执行引擎
  151. 支持三种运行模式(通过 RunConfig 区分):
  152. 1. 新建:trace_id=None
  153. 2. 续跑:trace_id=已有ID, after_sequence=None 或 == head
  154. 3. 回溯:trace_id=已有ID, after_sequence=N(N < head_sequence)
  155. """
  156. def __init__(
  157. self,
  158. trace_store: Optional[TraceStore] = None,
  159. memory_store: Optional[MemoryStore] = None,
  160. state_store: Optional[StateStore] = None,
  161. tool_registry: Optional[ToolRegistry] = None,
  162. llm_call: Optional[Callable] = None,
  163. utility_llm_call: Optional[Callable] = None,
  164. embedding_call: Optional[Callable] = None,
  165. config: Optional[AgentConfig] = None,
  166. skills_dir: Optional[str] = None,
  167. goal_tree: Optional[GoalTree] = None,
  168. debug: bool = False,
  169. ):
  170. """
  171. 初始化 AgentRunner
  172. Args:
  173. trace_store: Trace 存储
  174. memory_store: Memory 存储(可选)
  175. state_store: State 存储(可选)
  176. tool_registry: 工具注册表(默认使用全局注册表)
  177. llm_call: 主 LLM 调用函数
  178. embedding_call: 语义嵌入向量LLM
  179. utility_llm_call: 轻量 LLM(用于生成任务标题等),可选
  180. config: [向后兼容] AgentConfig
  181. skills_dir: Skills 目录路径
  182. goal_tree: 初始 GoalTree(可选)
  183. debug: 保留参数(已废弃)
  184. """
  185. self.trace_store = trace_store
  186. self.memory_store = memory_store
  187. self.state_store = state_store
  188. self.tools = tool_registry or get_tool_registry()
  189. self.llm_call = llm_call
  190. self.embedding_call = embedding_call
  191. self.utility_llm_call = utility_llm_call
  192. self.config = config or AgentConfig()
  193. self.skills_dir = skills_dir
  194. self.goal_tree = goal_tree
  195. self.debug = debug
  196. self._cancel_events: Dict[str, asyncio.Event] = {} # trace_id → cancel event
  197. self.used_ex_ids: List[str] = [] # 当前运行中使用过的经验 ID
  198. # 研究流程状态管理(每个 trace 独立)
  199. self._research_states: Dict[str, Dict[str, Any]] = {} # trace_id → research_state
  200. # 知识保存跟踪(每个 trace 独立)
  201. self._saved_knowledge_ids: Dict[str, List[str]] = {} # trace_id → [knowledge_ids]
  202. # ===== 核心公开方法 =====
  203. async def run(
  204. self,
  205. messages: List[Dict],
  206. config: Optional[RunConfig] = None,
  207. ) -> AsyncIterator[Union[Trace, Message]]:
  208. """
  209. Agent 模式执行(核心方法)
  210. Args:
  211. messages: OpenAI SDK 格式的输入消息
  212. 新建: 初始任务消息 [{"role": "user", "content": "..."}]
  213. 续跑: 追加的新消息
  214. 回溯: 在插入点之后追加的消息
  215. config: 运行配置
  216. Yields:
  217. Union[Trace, Message]: Trace 对象(状态变化)或 Message 对象(执行过程)
  218. """
  219. if not self.llm_call:
  220. raise ValueError("llm_call function not provided")
  221. config = config or RunConfig()
  222. trace = None
  223. try:
  224. # Phase 1: PREPARE TRACE
  225. trace, goal_tree, sequence = await self._prepare_trace(messages, config)
  226. # 注册取消事件
  227. self._cancel_events[trace.trace_id] = asyncio.Event()
  228. yield trace
  229. # Phase 2: BUILD HISTORY
  230. history, sequence, created_messages, head_seq = await self._build_history(
  231. trace.trace_id, messages, goal_tree, config, sequence
  232. )
  233. # Update trace's head_sequence in memory
  234. trace.head_sequence = head_seq
  235. for msg in created_messages:
  236. yield msg
  237. # Phase 3: AGENT LOOP
  238. async for event in self._agent_loop(trace, history, goal_tree, config, sequence):
  239. yield event
  240. except Exception as e:
  241. logger.error(f"Agent run failed: {e}")
  242. tid = config.trace_id or (trace.trace_id if trace else None)
  243. if self.trace_store and tid:
  244. # 读取当前 last_sequence 作为 head_sequence,确保续跑时能加载完整历史
  245. current = await self.trace_store.get_trace(tid)
  246. head_seq = current.last_sequence if current else None
  247. await self.trace_store.update_trace(
  248. tid,
  249. status="failed",
  250. head_sequence=head_seq,
  251. error_message=str(e),
  252. completed_at=datetime.now()
  253. )
  254. trace_obj = await self.trace_store.get_trace(tid)
  255. if trace_obj:
  256. yield trace_obj
  257. raise
  258. finally:
  259. # 清理取消事件
  260. if trace:
  261. self._cancel_events.pop(trace.trace_id, None)
  262. async def run_result(
  263. self,
  264. messages: List[Dict],
  265. config: Optional[RunConfig] = None,
  266. on_event: Optional[Callable] = None,
  267. ) -> Dict[str, Any]:
  268. """
  269. 结果模式 — 消费 run(),返回结构化结果。
  270. 主要用于 agent/evaluate 工具内部。
  271. Args:
  272. on_event: 可选回调,每个 Trace/Message 事件触发一次,用于实时输出子 Agent 执行过程。
  273. """
  274. last_assistant_text = ""
  275. final_trace: Optional[Trace] = None
  276. async for item in self.run(messages=messages, config=config):
  277. if on_event:
  278. on_event(item)
  279. if isinstance(item, Message) and item.role == "assistant":
  280. content = item.content
  281. text = ""
  282. if isinstance(content, dict):
  283. text = content.get("text", "") or ""
  284. elif isinstance(content, str):
  285. text = content
  286. if text and text.strip():
  287. last_assistant_text = text
  288. elif isinstance(item, Trace):
  289. final_trace = item
  290. config = config or RunConfig()
  291. if not final_trace and config.trace_id and self.trace_store:
  292. final_trace = await self.trace_store.get_trace(config.trace_id)
  293. status = final_trace.status if final_trace else "unknown"
  294. error = final_trace.error_message if final_trace else None
  295. summary = last_assistant_text
  296. if not summary:
  297. status = "failed"
  298. error = error or "Agent 没有产生 assistant 文本结果"
  299. # 获取保存的知识 ID
  300. trace_id = final_trace.trace_id if final_trace else config.trace_id
  301. saved_knowledge_ids = self._saved_knowledge_ids.get(trace_id, [])
  302. return {
  303. "status": status,
  304. "summary": summary,
  305. "trace_id": trace_id,
  306. "error": error,
  307. "saved_knowledge_ids": saved_knowledge_ids, # 新增:返回保存的知识 ID
  308. "stats": {
  309. "total_messages": final_trace.total_messages if final_trace else 0,
  310. "total_tokens": final_trace.total_tokens if final_trace else 0,
  311. "total_cost": final_trace.total_cost if final_trace else 0.0,
  312. },
  313. }
  314. async def stop(self, trace_id: str) -> bool:
  315. """
  316. 停止运行中的 Trace
  317. 设置取消信号,agent loop 在下一个 LLM 调用前检查并退出。
  318. Trace 状态置为 "stopped"。
  319. Returns:
  320. True 如果成功发送停止信号,False 如果该 trace 不在运行中
  321. """
  322. cancel_event = self._cancel_events.get(trace_id)
  323. if cancel_event is None:
  324. return False
  325. cancel_event.set()
  326. return True
  327. # ===== 单次调用(保留)=====
  328. async def call(
  329. self,
  330. messages: List[Dict],
  331. model: str = "gpt-4o",
  332. tools: Optional[List[str]] = None,
  333. uid: Optional[str] = None,
  334. trace: bool = True,
  335. **kwargs
  336. ) -> CallResult:
  337. """
  338. 单次 LLM 调用(无 Agent Loop)
  339. """
  340. if not self.llm_call:
  341. raise ValueError("llm_call function not provided")
  342. trace_id = None
  343. message_id = None
  344. tool_schemas = self._get_tool_schemas(tools)
  345. if trace and self.trace_store:
  346. trace_obj = Trace.create(mode="call", uid=uid, model=model, tools=tool_schemas, llm_params=kwargs)
  347. trace_id = await self.trace_store.create_trace(trace_obj)
  348. result = await self.llm_call(messages=messages, model=model, tools=tool_schemas, **kwargs)
  349. if trace and self.trace_store and trace_id:
  350. msg = Message.create(
  351. trace_id=trace_id, role="assistant", sequence=1, goal_id=None,
  352. content={"text": result.get("content", ""), "tool_calls": result.get("tool_calls")},
  353. prompt_tokens=result.get("prompt_tokens", 0),
  354. completion_tokens=result.get("completion_tokens", 0),
  355. finish_reason=result.get("finish_reason"),
  356. cost=result.get("cost", 0),
  357. )
  358. message_id = await self.trace_store.add_message(msg)
  359. await self.trace_store.update_trace(trace_id, status="completed", completed_at=datetime.now())
  360. return CallResult(
  361. reply=result.get("content", ""),
  362. tool_calls=result.get("tool_calls"),
  363. trace_id=trace_id,
  364. step_id=message_id,
  365. tokens={"prompt": result.get("prompt_tokens", 0), "completion": result.get("completion_tokens", 0)},
  366. cost=result.get("cost", 0)
  367. )
  368. # ===== Phase 1: PREPARE TRACE =====
  369. async def _prepare_trace(
  370. self,
  371. messages: List[Dict],
  372. config: RunConfig,
  373. ) -> Tuple[Trace, Optional[GoalTree], int]:
  374. """
  375. 准备 Trace:创建新的或加载已有的
  376. Returns:
  377. (trace, goal_tree, next_sequence)
  378. """
  379. if config.trace_id:
  380. return await self._prepare_existing_trace(config)
  381. else:
  382. return await self._prepare_new_trace(messages, config)
  383. async def _prepare_new_trace(
  384. self,
  385. messages: List[Dict],
  386. config: RunConfig,
  387. ) -> Tuple[Trace, Optional[GoalTree], int]:
  388. """创建新 Trace"""
  389. trace_id = str(uuid.uuid4())
  390. # 生成任务名称
  391. task_name = config.name or await self._generate_task_name(messages)
  392. # 准备工具 Schema
  393. tool_schemas = self._get_tool_schemas(config.tools)
  394. trace_obj = Trace(
  395. trace_id=trace_id,
  396. mode="agent",
  397. task=task_name,
  398. agent_type=config.agent_type,
  399. parent_trace_id=config.parent_trace_id,
  400. parent_goal_id=config.parent_goal_id,
  401. uid=config.uid,
  402. model=config.model,
  403. tools=tool_schemas,
  404. llm_params={"temperature": config.temperature, **config.extra_llm_params},
  405. status="running",
  406. )
  407. goal_tree = self.goal_tree or GoalTree(mission=task_name)
  408. if self.trace_store:
  409. await self.trace_store.create_trace(trace_obj)
  410. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  411. return trace_obj, goal_tree, 1
  412. async def _prepare_existing_trace(
  413. self,
  414. config: RunConfig,
  415. ) -> Tuple[Trace, Optional[GoalTree], int]:
  416. """加载已有 Trace(续跑或回溯)"""
  417. if not self.trace_store:
  418. raise ValueError("trace_store required for continue/rewind")
  419. trace_obj = await self.trace_store.get_trace(config.trace_id)
  420. if not trace_obj:
  421. raise ValueError(f"Trace not found: {config.trace_id}")
  422. goal_tree = await self.trace_store.get_goal_tree(config.trace_id)
  423. if goal_tree is None:
  424. # 防御性兜底:trace 存在但 goal.json 丢失时,创建空树
  425. goal_tree = GoalTree(mission=trace_obj.task or "Agent task")
  426. await self.trace_store.update_goal_tree(config.trace_id, goal_tree)
  427. # 自动判断行为:after_sequence 为 None 或 == head → 续跑;< head → 回溯
  428. after_seq = config.after_sequence
  429. # 如果 after_seq > head_sequence,说明 generator 被强制关闭时 store 的
  430. # head_sequence 未来得及更新(仍停在 Phase 2 写入的初始值)。
  431. # 用 last_sequence 修正 head_sequence,确保续跑时能看到完整历史。
  432. if after_seq is not None and after_seq > trace_obj.head_sequence:
  433. trace_obj.head_sequence = trace_obj.last_sequence
  434. await self.trace_store.update_trace(
  435. config.trace_id, head_sequence=trace_obj.head_sequence
  436. )
  437. if after_seq is not None and after_seq < trace_obj.head_sequence:
  438. # 回溯模式
  439. sequence = await self._rewind(config.trace_id, after_seq, goal_tree)
  440. else:
  441. # 续跑模式:从 last_sequence + 1 开始
  442. sequence = trace_obj.last_sequence + 1
  443. # 状态置为 running
  444. await self.trace_store.update_trace(
  445. config.trace_id,
  446. status="running",
  447. completed_at=None,
  448. )
  449. trace_obj.status = "running"
  450. return trace_obj, goal_tree, sequence
  451. # ===== Phase 2: BUILD HISTORY =====
  452. async def _get_embedding(self, text: str) -> List[float]:
  453. """
  454. 获取文本的嵌入向量(Embedding)
  455. Args:
  456. text: 需要向量化的文本
  457. Returns:
  458. List[float]: 嵌入向量
  459. """
  460. if not text or not text.strip():
  461. return []
  462. # 优先使用注入的 embedding_call
  463. if self.embedding_call:
  464. try:
  465. return await self.embedding_call(text)
  466. except Exception as e:
  467. logger.error(f"Error in embedding_call: {e}")
  468. raise
  469. # 兜底方案:如果没有注入 embedding_call,但有 llm_call,
  470. # 某些 SDK 封装可能支持通过 llm_call 的客户端直接获取
  471. # 这里建议强制要求基础设施层提供该函数以保证分层清晰
  472. raise ValueError("embedding_call function not provided to AgentRunner")
  473. async def _build_history(
  474. self,
  475. trace_id: str,
  476. new_messages: List[Dict],
  477. goal_tree: Optional[GoalTree],
  478. config: RunConfig,
  479. sequence: int,
  480. ) -> Tuple[List[Dict], int, List[Message]]:
  481. """
  482. 构建完整的 LLM 消息历史
  483. 1. 从 head_sequence 沿 parent chain 加载主路径消息(续跑/回溯场景)
  484. 2. 构建 system prompt(新建时注入 skills)
  485. 3. 新建时:在第一条 user message 末尾注入当前经验
  486. 4. 追加 input messages(设置 parent_sequence 链接到当前 head)
  487. Returns:
  488. (history, next_sequence, created_messages, head_sequence)
  489. created_messages: 本次新创建并持久化的 Message 列表,供 run() yield 给调用方
  490. head_sequence: 当前主路径头节点的 sequence
  491. """
  492. history: List[Dict] = []
  493. created_messages: List[Message] = []
  494. head_seq: Optional[int] = None # 当前主路径的头节点 sequence
  495. # 1. 加载已有 messages(通过主路径遍历)
  496. if config.trace_id and self.trace_store:
  497. trace_obj = await self.trace_store.get_trace(trace_id)
  498. if trace_obj and trace_obj.head_sequence > 0:
  499. main_path = await self.trace_store.get_main_path_messages(
  500. trace_id, trace_obj.head_sequence
  501. )
  502. # 修复 orphaned tool_calls(中断导致的 tool_call 无 tool_result)
  503. main_path, sequence = await self._heal_orphaned_tool_calls(
  504. main_path, trace_id, goal_tree, sequence,
  505. )
  506. history = [msg.to_llm_dict() for msg in main_path]
  507. if main_path:
  508. head_seq = main_path[-1].sequence
  509. # 2. 构建/注入 skills 到 system prompt
  510. has_system = any(m.get("role") == "system" for m in history)
  511. has_system_in_new = any(m.get("role") == "system" for m in new_messages)
  512. if not has_system:
  513. if has_system_in_new:
  514. # 入参消息已含 system,将 skills 注入其中(在 step 4 持久化之前)
  515. augmented = []
  516. for msg in new_messages:
  517. if msg.get("role") == "system":
  518. base = msg.get("content") or ""
  519. enriched = await self._build_system_prompt(config, base_prompt=base)
  520. augmented.append({**msg, "content": enriched or base})
  521. else:
  522. augmented.append(msg)
  523. new_messages = augmented
  524. else:
  525. # 没有 system,自动构建并插入历史
  526. system_prompt = await self._build_system_prompt(config)
  527. if system_prompt:
  528. history = [{"role": "system", "content": system_prompt}] + history
  529. if self.trace_store:
  530. system_msg = Message.create(
  531. trace_id=trace_id, role="system", sequence=sequence,
  532. goal_id=None, content=system_prompt,
  533. parent_sequence=None, # system message 是 root
  534. )
  535. await self.trace_store.add_message(system_msg)
  536. created_messages.append(system_msg)
  537. head_seq = sequence
  538. sequence += 1
  539. # 3. 追加新 messages(设置 parent_sequence 链接到当前 head)
  540. for msg_dict in new_messages:
  541. history.append(msg_dict)
  542. if self.trace_store:
  543. stored_msg = Message.from_llm_dict(
  544. msg_dict, trace_id=trace_id, sequence=sequence,
  545. goal_id=None, parent_sequence=head_seq,
  546. )
  547. await self.trace_store.add_message(stored_msg)
  548. created_messages.append(stored_msg)
  549. head_seq = sequence
  550. sequence += 1
  551. # 5. 更新 trace 的 head_sequence
  552. if self.trace_store and head_seq is not None:
  553. await self.trace_store.update_trace(trace_id, head_sequence=head_seq)
  554. # 6. 初始化研究流程(已废弃,知识注入现在在 goal_tool.py 中实现)
  555. # if config.enable_research_flow and not config.trace_id:
  556. # await self._init_research_flow(trace_id, new_messages, goal_tree, config)
  557. return history, sequence, created_messages, head_seq or 0
  558. async def _init_research_flow(
  559. self,
  560. trace_id: str,
  561. messages: List[Dict],
  562. goal_tree: Optional[GoalTree],
  563. config: RunConfig,
  564. ):
  565. """
  566. 初始化研究流程状态
  567. 研究流程阶段(已简化):
  568. 1. research_decision: 决定是否需要调研(知识和经验已自动注入到 GoalTree)
  569. 2. research: 执行调研(如果需要)
  570. 3. planning: 制定计划
  571. 4. execution: 正常执行
  572. """
  573. # 提取任务描述
  574. task_desc = self._extract_task_description(messages)
  575. # 初始化研究状态(直接从 research_decision 开始,因为知识已自动注入)
  576. self._research_states[trace_id] = {
  577. "stage": "research_decision", # 直接进入决策阶段
  578. "task_desc": task_desc,
  579. "knowledge_found": False,
  580. "experience_found": False,
  581. "need_research": False,
  582. "research_completed": False,
  583. "planning_completed": False,
  584. "knowledge_results": [],
  585. "experience_results": [],
  586. "decision_guide_injected": False, # 防止重复注入决策引导
  587. }
  588. logger.info(f"[Research Flow] 初始化研究流程(知识已自动注入): {task_desc[:50]}...")
  589. def _extract_task_description(self, messages: List[Dict]) -> str:
  590. """从消息中提取任务描述"""
  591. for msg in messages:
  592. if msg.get("role") == "user":
  593. content = msg.get("content", "")
  594. if isinstance(content, str):
  595. return content
  596. elif isinstance(content, list):
  597. for part in content:
  598. if isinstance(part, dict) and part.get("type") == "text":
  599. return part.get("text", "")
  600. return "未知任务"
  601. def _get_research_state(self, trace_id: str) -> Optional[Dict[str, Any]]:
  602. """获取研究流程状态"""
  603. return self._research_states.get(trace_id)
  604. def _update_research_stage(self, trace_id: str, stage: str, **kwargs):
  605. """更新研究流程阶段"""
  606. if trace_id in self._research_states:
  607. self._research_states[trace_id]["stage"] = stage
  608. self._research_states[trace_id].update(kwargs)
  609. logger.info(f"[Research Flow] 阶段切换: {stage}")
  610. def _build_research_guide(self, research_state: Dict[str, Any]) -> str:
  611. """根据研究流程状态构建引导消息"""
  612. stage = research_state["stage"]
  613. task_desc = research_state["task_desc"]
  614. if stage == "research":
  615. # 读取 research.md 的内容
  616. research_skill_content = ""
  617. research_skill_path = os.path.join(
  618. os.path.dirname(__file__),
  619. "..", "memory", "skills", "research.md"
  620. )
  621. try:
  622. with open(research_skill_path, "r", encoding="utf-8") as f:
  623. research_skill_content = f.read()
  624. except Exception as e:
  625. logger.warning(f"无法读取 research.md: {e}")
  626. research_skill_content = "(无法加载 research.md 内容)"
  627. return f"""
  628. ## 📚 研究流程 - 执行调研
  629. 现有信息不足,需要进行调研。
  630. {research_skill_content}
  631. **重要提示**:
  632. - 调研完成后,请使用 `knowledge_save` 工具保存调研结果
  633. - 系统会自动检测到 knowledge_save 调用,并进入下一阶段(计划)
  634. """
  635. elif stage == "planning":
  636. return f"""
  637. ## 📋 研究流程 - 制定计划
  638. 调研已完成(或无需调研),现在请制定执行计划。
  639. **请立即执行以下操作**:
  640. 1. 使用 `goal` 工具创建目标树
  641. 2. 将任务分解为可执行的子目标
  642. 3. 为每个子目标设置合理的优先级
  643. 注意:这是强制步骤,必须创建 goal tree 才能进入执行阶段。
  644. """
  645. # research_decision 阶段的引导消息已移到 _build_research_decision_guide
  646. return ""
  647. def _build_research_decision_guide(self, research_state: Dict[str, Any]) -> str:
  648. """构建调研决策阶段的引导消息(基于已自动注入的知识和经验)"""
  649. experience_results = research_state.get("experience_results", [])
  650. task_desc = research_state.get("task_desc", "")
  651. # 构建经验摘要
  652. experience_summary = ""
  653. if experience_results:
  654. experience_summary = f"✅ 已自动检索到 {len(experience_results)} 条相关经验(见上方 GoalTree 中的「📚 相关知识」)\n"
  655. else:
  656. experience_summary = "❌ 未找到相关经验\n"
  657. return f"""
  658. ---
  659. ## 🤔 调研决策
  660. {experience_summary}
  661. ### 决策指南
  662. **当前状态**:系统已自动检索知识库和经验库,相关内容已注入到上方的 GoalTree 中(查看 Current Goal 下的「📚 相关知识」部分)。
  663. **请根据已注入的知识和经验,选择下一步行动**:
  664. **选项 1: 知识充足,直接制定计划**
  665. - 如果上方显示的知识和经验已经足够完成任务
  666. - 直接使用 `goal` 工具制定执行计划
  667. **选项 2: 知识不足,需要调研** ⭐
  668. - 如果上方没有显示相关知识,或现有知识不足以完成任务
  669. - **立即调用 `agent` 工具启动调研子任务**:
  670. ```python
  671. agent(
  672. task=\"\"\"针对任务「{task_desc[:100]}」进行深入调研:
  673. 1. 使用 web_search 工具搜索相关技术文档、教程、最佳实践
  674. 2. 搜索关键词建议:
  675. - 核心技术名称 + "教程"
  676. - 核心技术名称 + "最佳实践"
  677. - 核心技术名称 + "示例代码"
  678. 3. 使用 read_file 工具查看项目中的相关文件
  679. 4. 对每条有价值的信息,使用 knowledge_save 工具保存,标签类型选择:
  680. - tool: 工具使用方法
  681. - definition: 概念定义
  682. - usercase: 使用案例
  683. - strategy: 策略经验
  684. 调研完成后,系统会自动进入计划阶段。
  685. \"\"\",
  686. skills=["research"] # 注入调研指南
  687. )
  688. ```
  689. **重要提示**:
  690. - 如果 GoalTree 中没有显示「📚 相关知识」,说明知识库为空,必须先调研
  691. - 调研应该简洁高效,最多设立两个 goal
  692. """
  693. async def _handle_research_flow_transition(
  694. self,
  695. trace_id: str,
  696. tool_name: str,
  697. tool_result: Any,
  698. goal_tree: Optional[GoalTree],
  699. ):
  700. """处理研究流程的状态转换"""
  701. research_state = self._get_research_state(trace_id)
  702. if not research_state:
  703. return
  704. stage = research_state["stage"]
  705. # 阶段 1: 调研决策(通过 assistant 的文本回复或 agent 工具调用判断)
  706. # 这个阶段的转换在 assistant 回复后处理,或检测到 agent 工具调用
  707. # 阶段 2: 调研完成
  708. # 情况 1: 检测到 knowledge_save 调用(直接调研)
  709. # 情况 2: 检测到 agent 工具执行完成(子 agent 调研)
  710. if stage == "research":
  711. if tool_name == "knowledge_save":
  712. # 直接调研:检测到 knowledge_save 调用
  713. self._update_research_stage(
  714. trace_id,
  715. "planning",
  716. research_completed=True
  717. )
  718. logger.info(f"[Research Flow] 调研完成(直接调研),进入计划阶段")
  719. elif tool_name == "agent":
  720. # 子 agent 调研:agent 工具执行完成
  721. self._update_research_stage(
  722. trace_id,
  723. "planning",
  724. research_completed=True
  725. )
  726. logger.info(f"[Research Flow] 调研完成(子 agent 调研),进入计划阶段")
  727. # 阶段 3: 计划完成(检测到 goal 工具调用)
  728. elif stage == "planning" and tool_name == "goal":
  729. # 检查是否创建了 goal tree
  730. if goal_tree and goal_tree.goals:
  731. self._update_research_stage(
  732. trace_id,
  733. "execution",
  734. planning_completed=True
  735. )
  736. logger.info(f"[Research Flow] 计划完成,进入执行阶段")
  737. # 打印 goal tree
  738. print("\n" + "="*60)
  739. print("📋 Goal Tree 已创建:")
  740. print("="*60)
  741. print(goal_tree.to_prompt())
  742. print("="*60 + "\n")
  743. # ===== Phase 3: AGENT LOOP =====
  744. async def _agent_loop(
  745. self,
  746. trace: Trace,
  747. history: List[Dict],
  748. goal_tree: Optional[GoalTree],
  749. config: RunConfig,
  750. sequence: int,
  751. ) -> AsyncIterator[Union[Trace, Message]]:
  752. """ReAct 循环"""
  753. trace_id = trace.trace_id
  754. tool_schemas = self._get_tool_schemas(config.tools)
  755. # 当前主路径头节点的 sequence(用于设置 parent_sequence)
  756. head_seq = trace.head_sequence
  757. # 经验检索缓存:只在 goal 切换时重新检索
  758. _last_goal_id = None
  759. _cached_exp_text = ""
  760. for iteration in range(config.max_iterations):
  761. # 检查取消信号
  762. cancel_event = self._cancel_events.get(trace_id)
  763. if cancel_event and cancel_event.is_set():
  764. logger.info(f"Trace {trace_id} stopped by user")
  765. if self.trace_store:
  766. await self.trace_store.update_trace(
  767. trace_id,
  768. status="stopped",
  769. head_sequence=head_seq,
  770. completed_at=datetime.now(),
  771. )
  772. trace_obj = await self.trace_store.get_trace(trace_id)
  773. if trace_obj:
  774. yield trace_obj
  775. return
  776. # Level 1 压缩:GoalTree 过滤(当消息超过阈值时触发)
  777. compression_config = CompressionConfig()
  778. token_count = estimate_tokens(history)
  779. max_tokens = compression_config.get_max_tokens(config.model)
  780. # 压缩评估日志
  781. progress_pct = (token_count / max_tokens * 100) if max_tokens > 0 else 0
  782. msg_count = len(history)
  783. img_count = sum(
  784. 1 for msg in history
  785. if isinstance(msg.get("content"), list)
  786. for part in msg["content"]
  787. if isinstance(part, dict) and part.get("type") in ("image", "image_url")
  788. )
  789. # 检查是否需要压缩(token 或消息数量超限)
  790. needs_compression_by_tokens = token_count > max_tokens
  791. needs_compression_by_count = (
  792. compression_config.max_messages > 0 and
  793. msg_count > compression_config.max_messages
  794. )
  795. needs_compression = needs_compression_by_tokens or needs_compression_by_count
  796. print(f"\n[压缩评估] 消息数: {msg_count} / {compression_config.max_messages} | 图片数: {img_count} | Token: {token_count:,} / {max_tokens:,} ({progress_pct:.1f}%)")
  797. if needs_compression:
  798. if needs_compression_by_count:
  799. print(f"[压缩评估] ⚠️ 消息数超过阈值 ({msg_count} > {compression_config.max_messages}),触发压缩流程")
  800. if needs_compression_by_tokens:
  801. print(f"[压缩评估] ⚠️ Token 数超过阈值,触发压缩流程")
  802. else:
  803. print(f"[压缩评估] ✅ 未超阈值,无需压缩")
  804. if needs_compression and self.trace_store and goal_tree:
  805. # 使用本地 head_seq(store 中的 head_sequence 在 loop 期间未更新,是过时的)
  806. if head_seq > 0:
  807. main_path_msgs = await self.trace_store.get_main_path_messages(
  808. trace_id, head_seq
  809. )
  810. filtered_msgs = filter_by_goal_status(main_path_msgs, goal_tree)
  811. if len(filtered_msgs) < len(main_path_msgs):
  812. filtered_tokens = estimate_tokens([msg.to_llm_dict() for msg in filtered_msgs])
  813. print(
  814. f"[Level 1 压缩] 消息: {len(main_path_msgs)} → {len(filtered_msgs)} 条 | "
  815. f"Token: {token_count:,} → ~{filtered_tokens:,}"
  816. )
  817. logger.info(
  818. "Level 1 压缩: %d -> %d 条消息 (tokens ~%d, 阈值 %d)",
  819. len(main_path_msgs), len(filtered_msgs), token_count, max_tokens,
  820. )
  821. history = [msg.to_llm_dict() for msg in filtered_msgs]
  822. else:
  823. print(
  824. f"[Level 1 压缩] 无可过滤消息 ({len(main_path_msgs)} 条全部保留, "
  825. f"completed/abandoned goals={sum(1 for g in goal_tree.goals if g.status in ('completed', 'abandoned'))})"
  826. )
  827. logger.info(
  828. "Level 1 压缩: 无可过滤消息 (%d 条全部保留, completed/abandoned goals=%d)",
  829. len(main_path_msgs),
  830. sum(1 for g in goal_tree.goals
  831. if g.status in ("completed", "abandoned")),
  832. )
  833. elif needs_compression:
  834. print("[压缩评估] ⚠️ 无法执行 Level 1 压缩(缺少 store 或 goal_tree)")
  835. logger.warning(
  836. "消息数 (%d) 或 token 数 (%d) 超过阈值 (max_messages=%d, max_tokens=%d),但无法执行 Level 1 压缩(缺少 store 或 goal_tree)",
  837. msg_count, token_count, compression_config.max_messages, max_tokens,
  838. )
  839. # Level 2 压缩:LLM 总结(Level 1 后仍超阈值时触发)
  840. token_count_after = estimate_tokens(history)
  841. msg_count_after = len(history)
  842. needs_level2_by_tokens = token_count_after > max_tokens
  843. needs_level2_by_count = (
  844. compression_config.max_messages > 0 and
  845. msg_count_after > compression_config.max_messages
  846. )
  847. needs_level2 = needs_level2_by_tokens or needs_level2_by_count
  848. if needs_level2:
  849. progress_pct_after = (token_count_after / max_tokens * 100) if max_tokens > 0 else 0
  850. reason = []
  851. if needs_level2_by_count:
  852. reason.append(f"消息数 {msg_count_after} > {compression_config.max_messages}")
  853. if needs_level2_by_tokens:
  854. reason.append(f"Token {token_count_after:,} > {max_tokens:,} ({progress_pct_after:.1f}%)")
  855. print(
  856. f"[Level 2 压缩] Level 1 后仍超阈值: {' | '.join(reason)} "
  857. f"→ 触发 LLM 总结"
  858. )
  859. logger.info(
  860. "Level 1 后仍超阈值 (消息数=%d/%d, token=%d/%d),触发 Level 2 压缩",
  861. msg_count_after, compression_config.max_messages, token_count_after, max_tokens,
  862. )
  863. history, head_seq, sequence = await self._compress_history(
  864. trace_id, history, goal_tree, config, sequence, head_seq,
  865. )
  866. final_tokens = estimate_tokens(history)
  867. print(f"[Level 2 压缩] 完成: Token {token_count_after:,} → {final_tokens:,}")
  868. elif needs_compression:
  869. # Level 1 压缩成功,未触发 Level 2
  870. print(f"[压缩评估] ✅ Level 1 压缩后达标: 消息数 {msg_count_after} | Token {token_count_after:,} / {max_tokens:,}")
  871. print() # 空行分隔
  872. # 构建 LLM messages(注入上下文)
  873. llm_messages = list(history)
  874. # 收集需要持久化的消息
  875. user_messages_to_persist = [] # 研究流程引导和经验检索改为 user 消息
  876. system_messages_to_persist = [] # 上下文注入保持为 system 消息
  877. # 研究流程引导(仅在启用且处于研究阶段时)- 改为 user 消息
  878. research_state = self._get_research_state(trace_id)
  879. if research_state and research_state["stage"] != "execution":
  880. research_guide = self._build_research_guide(research_state)
  881. if research_guide:
  882. user_msg = {"role": "user", "content": research_guide}
  883. llm_messages.append(user_msg)
  884. user_messages_to_persist.append(("研究流程引导", user_msg))
  885. # 先对历史消息应用 Prompt Caching(在注入动态内容之前)
  886. # 这样可以确保历史消息的缓存点固定,不受动态注入影响
  887. llm_messages = self._add_cache_control(
  888. llm_messages,
  889. config.model,
  890. config.enable_prompt_caching
  891. )
  892. # 然后追加动态注入的内容(不影响已缓存的历史消息)
  893. # 周期性注入 GoalTree + Collaborators
  894. if iteration % CONTEXT_INJECTION_INTERVAL == 0:
  895. context_injection = self._build_context_injection(trace, goal_tree)
  896. if context_injection:
  897. system_msg = {"role": "system", "content": context_injection}
  898. llm_messages.append(system_msg)
  899. system_messages_to_persist.append(("上下文注入", system_msg))
  900. # 经验检索:已废弃,知识注入现在在 goal_tool.py 的 focus 操作中自动执行
  901. # current_goal_id = goal_tree.current_id if goal_tree else None
  902. # if current_goal_id and current_goal_id != _last_goal_id:
  903. # ... (已移除)
  904. # # 经验注入:goal切换时注入相关历史经验 - 改为 user 消息
  905. # 或者在 research_decision 阶段注入调研决策引导
  906. # if _cached_exp_text or (research_state and research_state["stage"] == "research_decision" and not research_state.get("decision_guide_injected", False)):
  907. # exp_content = _cached_exp_text if _cached_exp_text else ""
  908. # # 如果处于 research_decision 阶段,追加引导消息
  909. # if research_state and research_state["stage"] == "research_decision" and not research_state.get("decision_guide_injected", False):
  910. # if exp_content:
  911. # exp_content += "\n\n"
  912. # exp_content += self._build_research_decision_guide(research_state)
  913. # 标记已注入,防止重复
  914. # research_state["decision_guide_injected"] = True
  915. # logger.info("[Research Flow] 已注入调研决策引导消息")
  916. # # if exp_content: # 确保有内容才注入
  917. # user_msg = {"role": "user", "content": exp_content}
  918. # llm_messages.append(user_msg)
  919. # user_messages_to_persist.append(("经验检索", user_msg))
  920. # # 持久化 user 消息到 trace 和 history
  921. # for label, usr_msg in user_messages_to_persist:
  922. # 添加到 history(这样会被包含在后续的对话中)
  923. # history.append(usr_msg)
  924. # # 保存到 trace store
  925. # if self.trace_store:
  926. # 在 content 前添加标签,这样会自动出现在 description 中
  927. # labeled_content = f"[{label}]\n{usr_msg['content']}"
  928. # user_message = Message.create(
  929. # trace_id=trace_id,
  930. # role="user",
  931. # sequence=sequence,
  932. # goal_id=current_goal_id,
  933. # parent_sequence=head_seq if head_seq > 0 else None,
  934. # content=labeled_content,
  935. # )
  936. # await self.trace_store.add_message(user_message)
  937. yield user_message
  938. head_seq = sequence
  939. sequence += 1
  940. # 持久化 system 消息到 trace 和 history
  941. for label, sys_msg in system_messages_to_persist:
  942. # 添加到 history(这样会被包含在后续的对话中)
  943. history.append(sys_msg)
  944. # 保存到 trace store
  945. if self.trace_store:
  946. # 在 content 前添加标签,这样会自动出现在 description 中
  947. labeled_content = f"[{label}]\n{sys_msg['content']}"
  948. system_message = Message.create(
  949. trace_id=trace_id,
  950. role="system",
  951. sequence=sequence,
  952. goal_id=current_goal_id,
  953. parent_sequence=head_seq if head_seq > 0 else None,
  954. content=labeled_content,
  955. )
  956. await self.trace_store.add_message(system_message)
  957. yield system_message
  958. head_seq = sequence
  959. sequence += 1
  960. # 调用 LLM
  961. result = await self.llm_call(
  962. messages=llm_messages,
  963. model=config.model,
  964. tools=tool_schemas,
  965. temperature=config.temperature,
  966. **config.extra_llm_params,
  967. )
  968. response_content = result.get("content", "")
  969. tool_calls = result.get("tool_calls")
  970. finish_reason = result.get("finish_reason")
  971. prompt_tokens = result.get("prompt_tokens", 0)
  972. completion_tokens = result.get("completion_tokens", 0)
  973. step_cost = result.get("cost", 0)
  974. cache_creation_tokens = result.get("cache_creation_tokens")
  975. cache_read_tokens = result.get("cache_read_tokens")
  976. # 研究流程:处理 research_decision 阶段的转换
  977. research_state = self._get_research_state(trace_id)
  978. research_decision_handled = False
  979. if research_state and research_state["stage"] == "research_decision":
  980. # 检查是否调用了 agent 工具进行调研
  981. if tool_calls:
  982. has_agent_call = any(
  983. tc.get("function", {}).get("name") == "agent"
  984. for tc in tool_calls
  985. )
  986. if has_agent_call:
  987. # LLM 决定使用子 agent 进行调研
  988. self._update_research_stage(trace_id, "research", need_research=True)
  989. logger.info(f"[Research Flow] LLM 决定使用子 agent 进行调研,进入调研阶段")
  990. research_decision_handled = True
  991. # 继续执行 agent 工具调用
  992. else:
  993. # 检查是否调用了 goal 工具(直接进入计划)
  994. has_goal_call = any(
  995. tc.get("function", {}).get("name") == "goal"
  996. for tc in tool_calls
  997. )
  998. if has_goal_call:
  999. self._update_research_stage(trace_id, "planning", need_research=False)
  1000. logger.info(f"[Research Flow] LLM 决定无需调研,直接进入计划阶段")
  1001. research_decision_handled = True
  1002. else:
  1003. # 根据 assistant 的文本回复判断
  1004. response_lower = response_content.lower()
  1005. if "无需调研" in response_content or "不需要调研" in response_content or "信息充足" in response_content:
  1006. self._update_research_stage(trace_id, "planning", need_research=False)
  1007. logger.info(f"[Research Flow] LLM 决定无需调研,直接进入计划阶段")
  1008. research_decision_handled = True
  1009. # 按需自动创建 root goal
  1010. if goal_tree and not goal_tree.goals and tool_calls:
  1011. has_goal_call = any(
  1012. tc.get("function", {}).get("name") == "goal"
  1013. for tc in tool_calls
  1014. )
  1015. if not has_goal_call:
  1016. mission = goal_tree.mission
  1017. root_desc = mission[:200] if len(mission) > 200 else mission
  1018. goal_tree.add_goals(
  1019. descriptions=[root_desc],
  1020. reasons=["系统自动创建:Agent 未显式创建目标"],
  1021. parent_id=None
  1022. )
  1023. goal_tree.focus(goal_tree.goals[0].id)
  1024. if self.trace_store:
  1025. await self.trace_store.add_goal(trace_id, goal_tree.goals[0])
  1026. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  1027. logger.info(f"自动创建 root goal: {goal_tree.goals[0].id}")
  1028. # 获取当前 goal_id
  1029. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  1030. # 记录 assistant Message(parent_sequence 指向当前 head)
  1031. assistant_msg = Message.create(
  1032. trace_id=trace_id,
  1033. role="assistant",
  1034. sequence=sequence,
  1035. goal_id=current_goal_id,
  1036. parent_sequence=head_seq if head_seq > 0 else None,
  1037. content={"text": response_content, "tool_calls": tool_calls},
  1038. prompt_tokens=prompt_tokens,
  1039. completion_tokens=completion_tokens,
  1040. cache_creation_tokens=cache_creation_tokens,
  1041. cache_read_tokens=cache_read_tokens,
  1042. finish_reason=finish_reason,
  1043. cost=step_cost,
  1044. )
  1045. if self.trace_store:
  1046. await self.trace_store.add_message(assistant_msg)
  1047. # 记录模型使用
  1048. await self.trace_store.record_model_usage(
  1049. trace_id=trace_id,
  1050. sequence=sequence - 1, # assistant_msg的sequence
  1051. role="assistant",
  1052. model=config.model,
  1053. prompt_tokens=prompt_tokens,
  1054. completion_tokens=completion_tokens,
  1055. cache_read_tokens=cache_read_tokens or 0,
  1056. )
  1057. yield assistant_msg
  1058. head_seq = sequence
  1059. sequence += 1
  1060. # 处理工具调用
  1061. # 截断兜底:finish_reason == "length" 说明响应被 max_tokens 截断,
  1062. # tool call 参数很可能不完整,不应执行,改为提示模型分批操作
  1063. if tool_calls and finish_reason == "length":
  1064. logger.warning(
  1065. "[Runner] 响应被 max_tokens 截断,跳过 %d 个不完整的 tool calls",
  1066. len(tool_calls),
  1067. )
  1068. truncation_hint = (
  1069. "你的响应因为 max_tokens 限制被截断,tool call 参数不完整,未执行。"
  1070. "请将大内容拆分为多次小的工具调用(例如用 write_file 的 append 模式分批写入)。"
  1071. )
  1072. history.append({
  1073. "role": "assistant",
  1074. "content": response_content,
  1075. "tool_calls": tool_calls,
  1076. })
  1077. # 为每个被截断的 tool call 返回错误结果
  1078. for tc in tool_calls:
  1079. history.append({
  1080. "role": "tool",
  1081. "tool_call_id": tc["id"],
  1082. "content": truncation_hint,
  1083. })
  1084. continue
  1085. if tool_calls and config.auto_execute_tools:
  1086. history.append({
  1087. "role": "assistant",
  1088. "content": response_content,
  1089. "tool_calls": tool_calls,
  1090. })
  1091. for tc in tool_calls:
  1092. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  1093. tool_name = tc["function"]["name"]
  1094. tool_args = tc["function"]["arguments"]
  1095. if isinstance(tool_args, str):
  1096. tool_args = json.loads(tool_args) if tool_args.strip() else {}
  1097. elif tool_args is None:
  1098. tool_args = {}
  1099. tool_result = await self.tools.execute(
  1100. tool_name,
  1101. tool_args,
  1102. uid=config.uid or "",
  1103. context={
  1104. "store": self.trace_store,
  1105. "trace_id": trace_id,
  1106. "goal_id": current_goal_id,
  1107. "runner": self,
  1108. "goal_tree": goal_tree,
  1109. }
  1110. )
  1111. # 跟踪保存的知识 ID
  1112. if tool_name == "knowledge_save" and isinstance(tool_result, dict):
  1113. metadata = tool_result.get("metadata", {})
  1114. knowledge_id = metadata.get("knowledge_id")
  1115. if knowledge_id:
  1116. if trace_id not in self._saved_knowledge_ids:
  1117. self._saved_knowledge_ids[trace_id] = []
  1118. self._saved_knowledge_ids[trace_id].append(knowledge_id)
  1119. logger.info(f"[Knowledge Tracking] 记录保存的知识 ID: {knowledge_id}")
  1120. # --- 支持多模态工具反馈 ---
  1121. # execute() 返回 dict{"text","images","tool_usage"} 或 str
  1122. # 统一为dict格式
  1123. if isinstance(tool_result, str):
  1124. tool_result = {"text": tool_result}
  1125. tool_text = tool_result.get("text", str(tool_result))
  1126. tool_images = tool_result.get("images", [])
  1127. tool_usage = tool_result.get("tool_usage") # 新增:提取tool_usage
  1128. # 处理多模态消息
  1129. if tool_images:
  1130. tool_result_text = tool_text
  1131. # 构建多模态消息格式
  1132. tool_content_for_llm = [{"type": "text", "text": tool_text}]
  1133. for img in tool_images:
  1134. if img.get("type") == "base64" and img.get("data"):
  1135. media_type = img.get("media_type", "image/png")
  1136. tool_content_for_llm.append({
  1137. "type": "image_url",
  1138. "image_url": {
  1139. "url": f"data:{media_type};base64,{img['data']}"
  1140. }
  1141. })
  1142. img_count = len(tool_content_for_llm) - 1 # 减去 text 块
  1143. print(f"[Runner] 多模态工具反馈: tool={tool_name}, images={img_count}, text_len={len(tool_result_text)}")
  1144. else:
  1145. tool_result_text = tool_text
  1146. tool_content_for_llm = tool_text
  1147. tool_msg = Message.create(
  1148. trace_id=trace_id,
  1149. role="tool",
  1150. sequence=sequence,
  1151. goal_id=current_goal_id,
  1152. parent_sequence=head_seq,
  1153. tool_call_id=tc["id"],
  1154. # 存储完整内容:有图片时保留 list(含 image_url),纯文本时存字符串
  1155. content={"tool_name": tool_name, "result": tool_content_for_llm},
  1156. )
  1157. if self.trace_store:
  1158. await self.trace_store.add_message(tool_msg)
  1159. # 记录工具的模型使用
  1160. if tool_usage:
  1161. await self.trace_store.record_model_usage(
  1162. trace_id=trace_id,
  1163. sequence=sequence,
  1164. role="tool",
  1165. tool_name=tool_name,
  1166. model=tool_usage.get("model"),
  1167. prompt_tokens=tool_usage.get("prompt_tokens", 0),
  1168. completion_tokens=tool_usage.get("completion_tokens", 0),
  1169. cache_read_tokens=tool_usage.get("cache_read_tokens", 0),
  1170. )
  1171. # 截图单独存为同名 PNG 文件
  1172. if tool_images:
  1173. import base64 as b64mod
  1174. for img in tool_images:
  1175. if img.get("data"):
  1176. png_path = self.trace_store._get_messages_dir(trace_id) / f"{tool_msg.message_id}.png"
  1177. png_path.write_bytes(b64mod.b64decode(img["data"]))
  1178. print(f"[Runner] 截图已保存: {png_path.name}")
  1179. break # 只存第一张
  1180. yield tool_msg
  1181. head_seq = sequence
  1182. sequence += 1
  1183. history.append({
  1184. "role": "tool",
  1185. "tool_call_id": tc["id"],
  1186. "name": tool_name,
  1187. "content": tool_content_for_llm, # 这里传入 list 即可触发模型的视觉能力
  1188. })
  1189. # ------------------------------------------
  1190. # 研究流程状态转换
  1191. await self._handle_research_flow_transition(
  1192. trace_id, tool_name, tool_result, goal_tree
  1193. )
  1194. continue # 继续循环
  1195. # 研究流程决策阶段:即使没有工具调用也要继续
  1196. if research_decision_handled:
  1197. logger.info(f"[Research Flow] 研究决策完成,继续下一轮循环")
  1198. continue
  1199. # 无工具调用,任务完成
  1200. break
  1201. # 更新 head_sequence 并完成 Trace
  1202. if self.trace_store:
  1203. await self.trace_store.update_trace(
  1204. trace_id,
  1205. status="completed",
  1206. head_sequence=head_seq,
  1207. completed_at=datetime.now(),
  1208. )
  1209. trace_obj = await self.trace_store.get_trace(trace_id)
  1210. if trace_obj:
  1211. yield trace_obj
  1212. # ===== Level 2: LLM 压缩 =====
  1213. async def _compress_history(
  1214. self,
  1215. trace_id: str,
  1216. history: List[Dict],
  1217. goal_tree: Optional[GoalTree],
  1218. config: RunConfig,
  1219. sequence: int,
  1220. head_seq: int,
  1221. ) -> Tuple[List[Dict], int, int]:
  1222. """
  1223. Level 2 压缩:LLM 总结
  1224. Step 1: 经验提取(reflect)— 纯内存 LLM 调用 + 文件追加,不影响 trace
  1225. Step 2: 压缩总结 — LLM 生成 summary
  1226. Step 3: 存储 summary 为新消息,parent_sequence 跳到 system msg
  1227. Step 4: 重建 history
  1228. Returns:
  1229. (new_history, new_head_seq, next_sequence)
  1230. """
  1231. logger.info("Level 2 压缩开始: trace=%s, 当前 history 长度=%d", trace_id, len(history))
  1232. # 找到 system message 的 sequence(主路径第一条消息)
  1233. system_msg_seq = None
  1234. system_msg_dict = None
  1235. if self.trace_store:
  1236. trace_obj = await self.trace_store.get_trace(trace_id)
  1237. if trace_obj and trace_obj.head_sequence > 0:
  1238. main_path = await self.trace_store.get_main_path_messages(
  1239. trace_id, trace_obj.head_sequence
  1240. )
  1241. for msg in main_path:
  1242. if msg.role == "system":
  1243. system_msg_seq = msg.sequence
  1244. system_msg_dict = msg.to_llm_dict()
  1245. break
  1246. # Fallback: 从 history 中找 system message
  1247. if system_msg_dict is None:
  1248. for msg_dict in history:
  1249. if msg_dict.get("role") == "system":
  1250. system_msg_dict = msg_dict
  1251. break
  1252. if system_msg_dict is None:
  1253. logger.warning("Level 2 压缩跳过:未找到 system message")
  1254. return history, head_seq, sequence
  1255. # --- Step 1: 经验提取(reflect)---
  1256. try:
  1257. # 1. 构造 Reflect Prompt(确保包含格式要求)
  1258. # 建议在 build_reflect_prompt() 里加入:
  1259. # "请使用格式:- [intent: 意图, state: 状态描述] 具体的经验内容"
  1260. reflect_prompt = build_reflect_prompt()
  1261. reflect_messages = list(history) + [{"role": "user", "content": reflect_prompt}]
  1262. # 应用 Prompt Caching
  1263. reflect_messages = self._add_cache_control(
  1264. reflect_messages,
  1265. config.model,
  1266. config.enable_prompt_caching
  1267. )
  1268. reflect_result = await self.llm_call(
  1269. messages=reflect_messages,
  1270. model=config.model,
  1271. tools=[],
  1272. temperature=0.2, # 略微保持一点发散性
  1273. **config.extra_llm_params,
  1274. )
  1275. reflection_text = reflect_result.get("content", "").strip()
  1276. if reflection_text:
  1277. import re as _re2
  1278. import uuid as _uuid2
  1279. pattern = r"-\s*\[(?P<tags>.*?)\]\s*(?P<content>.*)"
  1280. matches = list(_re2.finditer(pattern, reflection_text))
  1281. structured_entries = []
  1282. for match in matches:
  1283. tags_str = match.group("tags")
  1284. content = match.group("content")
  1285. intent_match = _re2.search(r"intent:\s*(.*?)(?:,|$)", tags_str, _re2.IGNORECASE)
  1286. state_match = _re2.search(r"state:\s*(.*?)(?:,|$)", tags_str, _re2.IGNORECASE)
  1287. intents = [i.strip() for i in intent_match.group(1).split(",")] if intent_match and intent_match.group(1) else []
  1288. states = [s.strip() for s in state_match.group(1).split(",")] if state_match and state_match.group(1) else []
  1289. ex_id = f"ex_{datetime.now().strftime('%m%d%H%M')}_{_uuid2.uuid4().hex[:4]}"
  1290. entry = f"""---
  1291. id: {ex_id}
  1292. trace_id: {trace_id}
  1293. tags: {{intent: {intents}, state: {states}}}
  1294. metrics: {{helpful: 1, harmful: 0}}
  1295. created_at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
  1296. ---
  1297. - {content}
  1298. - 经验ID: [{ex_id}]"""
  1299. structured_entries.append(entry)
  1300. if structured_entries:
  1301. # 保存经验为知识(strategy 标签)
  1302. saved_count = 0
  1303. for entry in structured_entries:
  1304. try:
  1305. # 从 entry 中提取信息
  1306. lines = entry.split("\n")
  1307. ex_id = ""
  1308. intents = []
  1309. states = []
  1310. content = ""
  1311. for line in lines:
  1312. if line.startswith("id:"):
  1313. ex_id = line.split(":", 1)[1].strip()
  1314. elif line.startswith("tags:"):
  1315. tags_match = _re2.search(r"intent:\s*\[(.*?)\].*state:\s*\[(.*?)\]", line)
  1316. if tags_match:
  1317. intents_str = tags_match.group(1).strip("'\"")
  1318. states_str = tags_match.group(2).strip("'\"")
  1319. intents = [i.strip().strip("'\"") for i in intents_str.split(",") if i.strip()]
  1320. states = [s.strip().strip("'\"") for s in states_str.split(",") if s.strip()]
  1321. elif line.startswith("- ") and not line.startswith("- 经验ID:"):
  1322. content = line[2:].strip()
  1323. # 构建 scenario(从 intent 和 state 生成)
  1324. scenario_parts = []
  1325. if intents:
  1326. scenario_parts.append(f"意图: {', '.join(intents)}")
  1327. if states:
  1328. scenario_parts.append(f"状态: {', '.join(states)}")
  1329. scenario = " | ".join(scenario_parts) if scenario_parts else "通用经验"
  1330. # 调用 knowledge_save 保存为 strategy 标签的知识
  1331. result = await knowledge_save(
  1332. scenario=scenario,
  1333. content=content,
  1334. tags_type=["strategy"],
  1335. urls=[],
  1336. agent_id="runner",
  1337. score=3,
  1338. message_id=trace_id # 使用 trace_id 作为 message_id
  1339. )
  1340. saved_count += 1
  1341. except Exception as e:
  1342. logger.warning(f"保存经验失败: {e}")
  1343. continue
  1344. logger.info(f"已提取并保存 {saved_count}/{len(structured_entries)} 条结构化经验到知识库")
  1345. else:
  1346. logger.warning("未能解析出符合格式的经验条目,请检查 REFLECT_PROMPT。")
  1347. logger.debug(f"LLM Raw Output:\n{reflection_text}")
  1348. else:
  1349. logger.warning("LLM 未生成反思内容")
  1350. except Exception as e:
  1351. logger.error(f"Level 2 经验提取失败: {e}")
  1352. # --- Step 2: 压缩总结 + 经验评估 ---
  1353. compress_prompt = build_compression_prompt(goal_tree, used_ex_ids=self.used_ex_ids)
  1354. compress_messages = list(history) + [{"role": "user", "content": compress_prompt}]
  1355. # 应用 Prompt Caching
  1356. compress_messages = self._add_cache_control(
  1357. compress_messages,
  1358. config.model,
  1359. config.enable_prompt_caching
  1360. )
  1361. compress_result = await self.llm_call(
  1362. messages=compress_messages,
  1363. model=config.model,
  1364. tools=[],
  1365. temperature=config.temperature,
  1366. **config.extra_llm_params,
  1367. )
  1368. raw_output = compress_result.get("content", "").strip()
  1369. if not raw_output:
  1370. logger.warning("Level 2 压缩跳过:LLM 未返回内容")
  1371. return history, head_seq, sequence
  1372. # 解析 [[EVALUATION]] 块并更新经验
  1373. if self.used_ex_ids:
  1374. try:
  1375. eval_block = ""
  1376. if "[[EVALUATION]]" in raw_output:
  1377. eval_start = raw_output.index("[[EVALUATION]]") + len("[[EVALUATION]]")
  1378. eval_end = raw_output.index("[[SUMMARY]]") if "[[SUMMARY]]" in raw_output else len(raw_output)
  1379. eval_block = raw_output[eval_start:eval_end].strip()
  1380. if eval_block:
  1381. import re as _re
  1382. update_map = {}
  1383. for line in eval_block.splitlines():
  1384. # 匹配新的知识 ID 格式:knowledge-xxx 或 research-xxx
  1385. m = _re.search(r"ID:\s*((?:knowledge|research)-\S+)\s*\|\s*Result:\s*(\w+)", line)
  1386. if m:
  1387. knowledge_id, result = m.group(1), m.group(2).lower()
  1388. if result in ("helpful", "harmful"):
  1389. update_map[knowledge_id] = {"action": result, "feedback": ""}
  1390. elif result == "mixed":
  1391. update_map[knowledge_id] = {"action": "helpful", "feedback": ""}
  1392. if update_map:
  1393. # 转换为 knowledge_batch_update 的格式
  1394. feedback_list = []
  1395. for kid, action_data in update_map.items():
  1396. feedback_list.append({
  1397. "knowledge_id": kid,
  1398. "is_effective": action_data["action"] == "helpful",
  1399. "feedback": action_data.get("feedback", "")
  1400. })
  1401. result = await knowledge_batch_update(feedback_list=feedback_list)
  1402. logger.info("知识评估完成,更新了知识")
  1403. except Exception as e:
  1404. logger.warning("经验评估解析失败(不影响压缩): %s", e)
  1405. # 提取 [[SUMMARY]] 块
  1406. summary_text = raw_output
  1407. if "[[SUMMARY]]" in raw_output:
  1408. summary_text = raw_output[raw_output.index("[[SUMMARY]]") + len("[[SUMMARY]]"):].strip()
  1409. # 压缩完成后清空 used_ex_ids
  1410. self.used_ex_ids = []
  1411. if not summary_text:
  1412. logger.warning("Level 2 压缩跳过:LLM 未返回 summary")
  1413. return history, head_seq, sequence
  1414. # --- Step 3: 存储 summary 消息 ---
  1415. summary_with_header = (
  1416. f"## 对话历史摘要(自动压缩)\n\n{summary_text}\n\n"
  1417. "---\n请基于以上摘要和当前 GoalTree 继续执行任务。"
  1418. )
  1419. summary_msg = Message.create(
  1420. trace_id=trace_id,
  1421. role="user",
  1422. sequence=sequence,
  1423. goal_id=None,
  1424. parent_sequence=system_msg_seq, # 跳到 system msg,跳过所有中间消息
  1425. content=summary_with_header,
  1426. )
  1427. if self.trace_store:
  1428. await self.trace_store.add_message(summary_msg)
  1429. new_head_seq = sequence
  1430. sequence += 1
  1431. # --- Step 4: 重建 history ---
  1432. new_history = [system_msg_dict, summary_msg.to_llm_dict()]
  1433. # 更新 trace head_sequence
  1434. if self.trace_store:
  1435. await self.trace_store.update_trace(
  1436. trace_id,
  1437. head_sequence=new_head_seq,
  1438. )
  1439. logger.info(
  1440. "Level 2 压缩完成: 旧 history %d 条 → 新 history %d 条, summary 长度=%d",
  1441. len(history), len(new_history), len(summary_text),
  1442. )
  1443. return new_history, new_head_seq, sequence
  1444. # ===== 回溯(Rewind)=====
  1445. async def _rewind(
  1446. self,
  1447. trace_id: str,
  1448. after_sequence: int,
  1449. goal_tree: Optional[GoalTree],
  1450. ) -> int:
  1451. """
  1452. 执行回溯:快照 GoalTree,重建干净树,设置 head_sequence
  1453. 新消息的 parent_sequence 将指向 rewind 点,旧消息通过树结构自然脱离主路径。
  1454. Returns:
  1455. 下一个可用的 sequence 号
  1456. """
  1457. if not self.trace_store:
  1458. raise ValueError("trace_store required for rewind")
  1459. # 1. 加载所有 messages(用于 safe cutoff 和 max sequence)
  1460. all_messages = await self.trace_store.get_trace_messages(trace_id)
  1461. if not all_messages:
  1462. return 1
  1463. # 2. 找到安全截断点(确保不截断在 tool_call 和 tool response 之间)
  1464. cutoff = self._find_safe_cutoff(all_messages, after_sequence)
  1465. # 3. 快照并重建 GoalTree
  1466. if goal_tree:
  1467. # 获取截断点消息的 created_at 作为时间界限
  1468. cutoff_msg = None
  1469. for msg in all_messages:
  1470. if msg.sequence == cutoff:
  1471. cutoff_msg = msg
  1472. break
  1473. cutoff_time = cutoff_msg.created_at if cutoff_msg else datetime.now()
  1474. # 快照到 events(含 head_sequence 供前端感知分支切换)
  1475. await self.trace_store.append_event(trace_id, "rewind", {
  1476. "after_sequence": cutoff,
  1477. "head_sequence": cutoff,
  1478. "goal_tree_snapshot": goal_tree.to_dict(),
  1479. })
  1480. # 按时间重建干净的 GoalTree
  1481. new_tree = goal_tree.rebuild_for_rewind(cutoff_time)
  1482. await self.trace_store.update_goal_tree(trace_id, new_tree)
  1483. # 更新内存中的引用
  1484. goal_tree.goals = new_tree.goals
  1485. goal_tree.current_id = new_tree.current_id
  1486. # 4. 更新 head_sequence 到 rewind 点
  1487. await self.trace_store.update_trace(trace_id, head_sequence=cutoff)
  1488. # 5. 返回 next sequence(全局递增,不复用)
  1489. max_seq = max((m.sequence for m in all_messages), default=0)
  1490. return max_seq + 1
  1491. def _find_safe_cutoff(self, messages: List[Message], after_sequence: int) -> int:
  1492. """
  1493. 找到安全的截断点。
  1494. 如果 after_sequence 指向一条带 tool_calls 的 assistant message,
  1495. 则自动扩展到其所有对应的 tool response 之后。
  1496. """
  1497. cutoff = after_sequence
  1498. # 找到 after_sequence 对应的 message
  1499. target_msg = None
  1500. for msg in messages:
  1501. if msg.sequence == after_sequence:
  1502. target_msg = msg
  1503. break
  1504. if not target_msg:
  1505. return cutoff
  1506. # 如果是 assistant 且有 tool_calls,找到所有对应的 tool responses
  1507. if target_msg.role == "assistant":
  1508. content = target_msg.content
  1509. if isinstance(content, dict) and content.get("tool_calls"):
  1510. tool_call_ids = set()
  1511. for tc in content["tool_calls"]:
  1512. if isinstance(tc, dict) and tc.get("id"):
  1513. tool_call_ids.add(tc["id"])
  1514. # 找到这些 tool_call 对应的 tool messages
  1515. for msg in messages:
  1516. if (msg.role == "tool" and msg.tool_call_id
  1517. and msg.tool_call_id in tool_call_ids):
  1518. cutoff = max(cutoff, msg.sequence)
  1519. return cutoff
  1520. async def _heal_orphaned_tool_calls(
  1521. self,
  1522. messages: List[Message],
  1523. trace_id: str,
  1524. goal_tree: Optional[GoalTree],
  1525. sequence: int,
  1526. ) -> tuple:
  1527. """
  1528. 检测并修复消息历史中的 orphaned tool_calls。
  1529. 当 agent 被 stop/crash 中断时,可能有 assistant 的 tool_calls 没有对应的
  1530. tool results(包括多 tool_call 部分完成的情况)。直接发给 LLM 会导致 400。
  1531. 修复策略:为每个缺失的 tool_result 插入合成的"中断通知"消息,而非裁剪。
  1532. - 普通工具:简短中断提示
  1533. - agent/evaluate:包含 sub_trace_id、执行统计、continue_from 指引
  1534. 合成消息持久化到 store,确保幂等(下次续跑不再触发)。
  1535. Returns:
  1536. (healed_messages, next_sequence)
  1537. """
  1538. if not messages:
  1539. return messages, sequence
  1540. # 收集所有 tool_call IDs → (assistant_msg, tool_call_dict)
  1541. tc_map: Dict[str, tuple] = {}
  1542. result_ids: set = set()
  1543. for msg in messages:
  1544. if msg.role == "assistant":
  1545. content = msg.content
  1546. if isinstance(content, dict) and content.get("tool_calls"):
  1547. for tc in content["tool_calls"]:
  1548. tc_id = tc.get("id")
  1549. if tc_id:
  1550. tc_map[tc_id] = (msg, tc)
  1551. elif msg.role == "tool" and msg.tool_call_id:
  1552. result_ids.add(msg.tool_call_id)
  1553. orphaned_ids = [tc_id for tc_id in tc_map if tc_id not in result_ids]
  1554. if not orphaned_ids:
  1555. return messages, sequence
  1556. logger.info(
  1557. "检测到 %d 个 orphaned tool_calls,生成合成中断通知",
  1558. len(orphaned_ids),
  1559. )
  1560. healed = list(messages)
  1561. head_seq = messages[-1].sequence
  1562. for tc_id in orphaned_ids:
  1563. assistant_msg, tc = tc_map[tc_id]
  1564. tool_name = tc.get("function", {}).get("name", "unknown")
  1565. if tool_name in ("agent", "evaluate"):
  1566. result_text = self._build_agent_interrupted_result(
  1567. tc, goal_tree, assistant_msg,
  1568. )
  1569. else:
  1570. result_text = (
  1571. f"⚠️ 工具 {tool_name} 执行被中断(进程异常退出),"
  1572. "未获得执行结果。请根据需要重新调用。"
  1573. )
  1574. synthetic_msg = Message.create(
  1575. trace_id=trace_id,
  1576. role="tool",
  1577. sequence=sequence,
  1578. goal_id=assistant_msg.goal_id,
  1579. parent_sequence=head_seq,
  1580. tool_call_id=tc_id,
  1581. content={"tool_name": tool_name, "result": result_text},
  1582. )
  1583. if self.trace_store:
  1584. await self.trace_store.add_message(synthetic_msg)
  1585. healed.append(synthetic_msg)
  1586. head_seq = sequence
  1587. sequence += 1
  1588. # 更新 trace head/last sequence
  1589. if self.trace_store:
  1590. await self.trace_store.update_trace(
  1591. trace_id,
  1592. head_sequence=head_seq,
  1593. last_sequence=max(head_seq, sequence - 1),
  1594. )
  1595. return healed, sequence
  1596. def _build_agent_interrupted_result(
  1597. self,
  1598. tc: Dict,
  1599. goal_tree: Optional[GoalTree],
  1600. assistant_msg: Message,
  1601. ) -> str:
  1602. """为中断的 agent/evaluate 工具调用构建合成结果(对齐正常返回值格式)"""
  1603. args_str = tc.get("function", {}).get("arguments", "{}")
  1604. try:
  1605. args = json.loads(args_str) if isinstance(args_str, str) else args_str
  1606. except json.JSONDecodeError:
  1607. args = {}
  1608. task = args.get("task", "未知任务")
  1609. if isinstance(task, list):
  1610. task = "; ".join(task)
  1611. tool_name = tc.get("function", {}).get("name", "agent")
  1612. mode = "evaluate" if tool_name == "evaluate" else "delegate"
  1613. # 从 goal_tree 查找 sub_trace 信息
  1614. sub_trace_id = None
  1615. stats = None
  1616. if goal_tree and assistant_msg.goal_id:
  1617. goal = goal_tree.find(assistant_msg.goal_id)
  1618. if goal and goal.sub_trace_ids:
  1619. first = goal.sub_trace_ids[0]
  1620. if isinstance(first, dict):
  1621. sub_trace_id = first.get("trace_id")
  1622. elif isinstance(first, str):
  1623. sub_trace_id = first
  1624. if goal.cumulative_stats:
  1625. s = goal.cumulative_stats
  1626. if s.message_count > 0:
  1627. stats = {
  1628. "message_count": s.message_count,
  1629. "total_tokens": s.total_tokens,
  1630. "total_cost": round(s.total_cost, 4),
  1631. }
  1632. result: Dict[str, Any] = {
  1633. "mode": mode,
  1634. "status": "interrupted",
  1635. "summary": "⚠️ 子Agent执行被中断(进程异常退出)",
  1636. "task": task,
  1637. }
  1638. if sub_trace_id:
  1639. result["sub_trace_id"] = sub_trace_id
  1640. result["hint"] = (
  1641. f'使用 continue_from="{sub_trace_id}" 可继续执行,保留已有进度'
  1642. )
  1643. if stats:
  1644. result["stats"] = stats
  1645. return json.dumps(result, ensure_ascii=False, indent=2)
  1646. # ===== 上下文注入 =====
  1647. def _build_context_injection(
  1648. self,
  1649. trace: Trace,
  1650. goal_tree: Optional[GoalTree],
  1651. ) -> str:
  1652. """构建周期性注入的上下文(GoalTree + Active Collaborators + Focus 提醒)"""
  1653. parts = []
  1654. # GoalTree
  1655. if goal_tree and goal_tree.goals:
  1656. parts.append(f"## Current Plan\n\n{goal_tree.to_prompt()}")
  1657. # 检测 focus 在有子节点的父目标上:提醒模型 focus 到具体子目标
  1658. if goal_tree.current_id:
  1659. children = goal_tree.get_children(goal_tree.current_id)
  1660. pending_children = [c for c in children if c.status in ("pending", "in_progress")]
  1661. if pending_children:
  1662. child_ids = ", ".join(
  1663. goal_tree._generate_display_id(c) for c in pending_children[:3]
  1664. )
  1665. parts.append(
  1666. f"**提醒**:当前焦点在父目标上,建议用 `goal(focus=\"...\")` "
  1667. f"切换到具体子目标(如 {child_ids})再执行。"
  1668. )
  1669. # Active Collaborators
  1670. collaborators = trace.context.get("collaborators", [])
  1671. if collaborators:
  1672. lines = ["## Active Collaborators"]
  1673. for c in collaborators:
  1674. status_str = c.get("status", "unknown")
  1675. ctype = c.get("type", "agent")
  1676. summary = c.get("summary", "")
  1677. name = c.get("name", "unnamed")
  1678. lines.append(f"- {name} [{ctype}, {status_str}]: {summary}")
  1679. parts.append("\n".join(lines))
  1680. return "\n\n".join(parts)
  1681. # ===== 辅助方法 =====
  1682. def _add_cache_control(
  1683. self,
  1684. messages: List[Dict],
  1685. model: str,
  1686. enable: bool
  1687. ) -> List[Dict]:
  1688. """
  1689. 为支持的模型添加 Prompt Caching 标记
  1690. 策略:固定位置 + 延迟查找
  1691. 1. system message 添加缓存(如果足够长)
  1692. 2. 固定位置缓存点(20, 40, 60, 80),确保每个缓存点间隔 >= 1024 tokens
  1693. 3. 最多使用 4 个缓存点(含 system)
  1694. Args:
  1695. messages: 原始消息列表
  1696. model: 模型名称
  1697. enable: 是否启用缓存
  1698. Returns:
  1699. 添加了 cache_control 的消息列表(深拷贝)
  1700. """
  1701. if not enable:
  1702. return messages
  1703. # 只对 Claude 模型启用
  1704. if "claude" not in model.lower():
  1705. return messages
  1706. # 深拷贝避免修改原始数据
  1707. import copy
  1708. messages = copy.deepcopy(messages)
  1709. # 策略 1: 为 system message 添加缓存
  1710. system_cached = False
  1711. for msg in messages:
  1712. if msg.get("role") == "system":
  1713. content = msg.get("content", "")
  1714. if isinstance(content, str) and len(content) > 1000:
  1715. msg["content"] = [{
  1716. "type": "text",
  1717. "text": content,
  1718. "cache_control": {"type": "ephemeral"}
  1719. }]
  1720. system_cached = True
  1721. logger.debug(f"[Cache] 为 system message 添加缓存标记 (len={len(content)})")
  1722. break
  1723. # 策略 2: 固定位置缓存点
  1724. CACHE_INTERVAL = 20
  1725. MAX_POINTS = 3 if system_cached else 4
  1726. MIN_TOKENS = 1024
  1727. AVG_TOKENS_PER_MSG = 70
  1728. total_msgs = len(messages)
  1729. if total_msgs == 0:
  1730. return messages
  1731. cache_positions = []
  1732. last_cache_pos = 0
  1733. for i in range(1, MAX_POINTS + 1):
  1734. target_pos = i * CACHE_INTERVAL - 1 # 19, 39, 59, 79
  1735. if target_pos >= total_msgs:
  1736. break
  1737. # 从目标位置开始查找合适的 user/assistant 消息
  1738. for j in range(target_pos, total_msgs):
  1739. msg = messages[j]
  1740. if msg.get("role") not in ("user", "assistant"):
  1741. continue
  1742. content = msg.get("content", "")
  1743. if not content:
  1744. continue
  1745. # 检查 content 是否非空
  1746. is_valid = False
  1747. if isinstance(content, str):
  1748. is_valid = len(content) > 0
  1749. elif isinstance(content, list):
  1750. is_valid = any(
  1751. isinstance(block, dict) and
  1752. block.get("type") == "text" and
  1753. len(block.get("text", "")) > 0
  1754. for block in content
  1755. )
  1756. if not is_valid:
  1757. continue
  1758. # 检查 token 距离
  1759. msg_count = j - last_cache_pos
  1760. estimated_tokens = msg_count * AVG_TOKENS_PER_MSG
  1761. if estimated_tokens >= MIN_TOKENS:
  1762. cache_positions.append(j)
  1763. last_cache_pos = j
  1764. logger.debug(f"[Cache] 在位置 {j} 添加缓存点 (估算 {estimated_tokens} tokens)")
  1765. break
  1766. # 应用缓存标记
  1767. for idx in cache_positions:
  1768. msg = messages[idx]
  1769. content = msg.get("content", "")
  1770. if isinstance(content, str):
  1771. msg["content"] = [{
  1772. "type": "text",
  1773. "text": content,
  1774. "cache_control": {"type": "ephemeral"}
  1775. }]
  1776. logger.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  1777. elif isinstance(content, list):
  1778. # 在最后一个 text block 添加 cache_control
  1779. for block in reversed(content):
  1780. if isinstance(block, dict) and block.get("type") == "text":
  1781. block["cache_control"] = {"type": "ephemeral"}
  1782. logger.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  1783. break
  1784. logger.debug(
  1785. f"[Cache] 总消息: {total_msgs}, "
  1786. f"缓存点: {len(cache_positions)} at {cache_positions}"
  1787. )
  1788. return messages
  1789. def _get_tool_schemas(self, tools: Optional[List[str]]) -> List[Dict]:
  1790. """
  1791. 获取工具 Schema
  1792. - tools=None: 使用 registry 中全部已注册工具(含内置 + 外部注册的)
  1793. - tools=["a", "b"]: 在 BUILTIN_TOOLS 基础上追加指定工具
  1794. """
  1795. if tools is None:
  1796. # 全部已注册工具
  1797. tool_names = self.tools.get_tool_names()
  1798. else:
  1799. # BUILTIN_TOOLS + 显式指定的额外工具
  1800. tool_names = BUILTIN_TOOLS.copy()
  1801. for t in tools:
  1802. if t not in tool_names:
  1803. tool_names.append(t)
  1804. return self.tools.get_schemas(tool_names)
  1805. # 默认 system prompt 前缀(当 config.system_prompt 和前端都未提供 system message 时使用)
  1806. DEFAULT_SYSTEM_PREFIX = "你是最顶尖的AI助手,可以拆分并调用工具逐步解决复杂问题。"
  1807. async def _build_system_prompt(self, config: RunConfig, base_prompt: Optional[str] = None) -> Optional[str]:
  1808. """构建 system prompt(注入 skills)
  1809. 优先级:
  1810. 1. config.skills 显式指定 → 按名称过滤
  1811. 2. config.skills 为 None → 查 preset 的默认 skills 列表
  1812. 3. preset 也无 skills(None)→ 加载全部(向后兼容)
  1813. Args:
  1814. base_prompt: 已有 system 内容(来自消息或 config.system_prompt),
  1815. None 时使用 config.system_prompt
  1816. """
  1817. from agent.core.presets import AGENT_PRESETS
  1818. system_prompt = base_prompt if base_prompt is not None else config.system_prompt
  1819. # 确定要加载哪些 skills
  1820. skills_filter: Optional[List[str]] = config.skills
  1821. if skills_filter is None:
  1822. preset = AGENT_PRESETS.get(config.agent_type)
  1823. if preset is not None:
  1824. skills_filter = preset.skills # 可能仍为 None(加载全部)
  1825. # 加载并过滤
  1826. all_skills = load_skills_from_dir(self.skills_dir)
  1827. if skills_filter is not None:
  1828. skills = [s for s in all_skills if s.name in skills_filter]
  1829. else:
  1830. skills = all_skills
  1831. skills_text = self._format_skills(skills) if skills else ""
  1832. if system_prompt:
  1833. if skills_text:
  1834. system_prompt += f"\n\n## Skills\n{skills_text}"
  1835. else:
  1836. system_prompt = self.DEFAULT_SYSTEM_PREFIX
  1837. if skills_text:
  1838. system_prompt += f"\n\n## Skills\n{skills_text}"
  1839. return system_prompt
  1840. async def _generate_task_name(self, messages: List[Dict]) -> str:
  1841. """生成任务名称:优先使用 utility_llm,fallback 到文本截取"""
  1842. # 提取 messages 中的文本内容
  1843. text_parts = []
  1844. for msg in messages:
  1845. content = msg.get("content", "")
  1846. if isinstance(content, str):
  1847. text_parts.append(content)
  1848. elif isinstance(content, list):
  1849. for part in content:
  1850. if isinstance(part, dict) and part.get("type") == "text":
  1851. text_parts.append(part.get("text", ""))
  1852. raw_text = " ".join(text_parts).strip()
  1853. if not raw_text:
  1854. return "未命名任务"
  1855. # 尝试使用 utility_llm 生成标题
  1856. if self.utility_llm_call:
  1857. try:
  1858. result = await self.utility_llm_call(
  1859. messages=[
  1860. {"role": "system", "content": "用中文为以下任务生成一个简短标题(10-30字),只输出标题本身:"},
  1861. {"role": "user", "content": raw_text[:2000]},
  1862. ],
  1863. model="gpt-4o-mini", # 使用便宜模型
  1864. )
  1865. title = result.get("content", "").strip()
  1866. if title and len(title) < 100:
  1867. return title
  1868. except Exception:
  1869. pass
  1870. # Fallback: 截取前 50 字符
  1871. return raw_text[:50] + ("..." if len(raw_text) > 50 else "")
  1872. def _format_skills(self, skills: List[Skill]) -> str:
  1873. if not skills:
  1874. return ""
  1875. return "\n\n".join(s.to_prompt_text() for s in skills)