runner.py 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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.experience import _get_structured_experiences, _batch_update_experiences
  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. enable_memory: bool = True
  56. auto_execute_tools: bool = True
  57. name: Optional[str] = None # 显示名称(空则由 utility_llm 自动生成)
  58. # --- Trace 控制 ---
  59. trace_id: Optional[str] = None # None = 新建
  60. parent_trace_id: Optional[str] = None # 子 Agent 专用
  61. parent_goal_id: Optional[str] = None
  62. # --- 续跑控制 ---
  63. after_sequence: Optional[int] = None # 从哪条消息后续跑(message sequence)
  64. # --- 额外 LLM 参数(传给 llm_call 的 **kwargs)---
  65. extra_llm_params: Dict[str, Any] = field(default_factory=dict)
  66. # 内置工具列表(始终自动加载)
  67. BUILTIN_TOOLS = [
  68. # 文件操作工具
  69. "read_file",
  70. "edit_file",
  71. "write_file",
  72. "glob_files",
  73. "grep_content",
  74. # 系统工具
  75. "bash_command",
  76. # 技能和目标管理
  77. "skill",
  78. "list_skills",
  79. "goal",
  80. "agent",
  81. "evaluate",
  82. # 搜索工具
  83. "search_posts",
  84. "get_experience",
  85. "get_search_suggestions",
  86. # 沙箱工具
  87. "sandbox_create_environment",
  88. "sandbox_run_shell",
  89. "sandbox_rebuild_with_ports",
  90. "sandbox_destroy_environment",
  91. # 浏览器工具
  92. "browser_navigate_to_url",
  93. "browser_search_web",
  94. "browser_go_back",
  95. "browser_wait",
  96. "browser_click_element",
  97. "browser_input_text",
  98. "browser_send_keys",
  99. "browser_upload_file",
  100. "browser_scroll_page",
  101. "browser_find_text",
  102. "browser_screenshot",
  103. "browser_switch_tab",
  104. "browser_close_tab",
  105. "browser_get_dropdown_options",
  106. "browser_select_dropdown_option",
  107. "browser_extract_content",
  108. "browser_read_long_content",
  109. "browser_download_direct_url",
  110. "browser_get_page_html",
  111. "browser_get_visual_selector_map",
  112. "browser_evaluate",
  113. "browser_ensure_login_with_cookies",
  114. "browser_wait_for_user_action",
  115. "browser_done",
  116. "browser_export_cookies",
  117. "browser_load_cookies"
  118. ]
  119. # ===== 向后兼容 =====
  120. @dataclass
  121. class AgentConfig:
  122. """[向后兼容] Agent 配置,新代码请使用 RunConfig"""
  123. agent_type: str = "default"
  124. max_iterations: int = 200
  125. enable_memory: bool = True
  126. auto_execute_tools: bool = True
  127. @dataclass
  128. class CallResult:
  129. """单次调用结果"""
  130. reply: str
  131. tool_calls: Optional[List[Dict]] = None
  132. trace_id: Optional[str] = None
  133. step_id: Optional[str] = None
  134. tokens: Optional[Dict[str, int]] = None
  135. cost: float = 0.0
  136. # ===== 执行引擎 =====
  137. CONTEXT_INJECTION_INTERVAL = 10 # 每 N 轮注入一次 GoalTree + Collaborators
  138. class AgentRunner:
  139. """
  140. Agent 执行引擎
  141. 支持三种运行模式(通过 RunConfig 区分):
  142. 1. 新建:trace_id=None
  143. 2. 续跑:trace_id=已有ID, after_sequence=None 或 == head
  144. 3. 回溯:trace_id=已有ID, after_sequence=N(N < head_sequence)
  145. """
  146. def __init__(
  147. self,
  148. trace_store: Optional[TraceStore] = None,
  149. memory_store: Optional[MemoryStore] = None,
  150. state_store: Optional[StateStore] = None,
  151. tool_registry: Optional[ToolRegistry] = None,
  152. llm_call: Optional[Callable] = None,
  153. utility_llm_call: Optional[Callable] = None,
  154. embedding_call: Optional[Callable] = None,
  155. config: Optional[AgentConfig] = None,
  156. skills_dir: Optional[str] = None,
  157. experiences_path: Optional[str] = "./.cache/experiences.md",
  158. goal_tree: Optional[GoalTree] = None,
  159. debug: bool = False,
  160. ):
  161. """
  162. 初始化 AgentRunner
  163. Args:
  164. trace_store: Trace 存储
  165. memory_store: Memory 存储(可选)
  166. state_store: State 存储(可选)
  167. tool_registry: 工具注册表(默认使用全局注册表)
  168. llm_call: 主 LLM 调用函数
  169. embedding_call: 语义嵌入向量LLM
  170. utility_llm_call: 轻量 LLM(用于生成任务标题等),可选
  171. config: [向后兼容] AgentConfig
  172. skills_dir: Skills 目录路径
  173. experiences_path: 经验文件路径(默认 ./.cache/experiences.md)
  174. goal_tree: 初始 GoalTree(可选)
  175. debug: 保留参数(已废弃)
  176. """
  177. self.trace_store = trace_store
  178. self.memory_store = memory_store
  179. self.state_store = state_store
  180. self.tools = tool_registry or get_tool_registry()
  181. self.llm_call = llm_call
  182. self.embedding_call = embedding_call
  183. self.utility_llm_call = utility_llm_call
  184. self.config = config or AgentConfig()
  185. self.skills_dir = skills_dir
  186. self.experiences_path = experiences_path
  187. self.goal_tree = goal_tree
  188. self.debug = debug
  189. self._cancel_events: Dict[str, asyncio.Event] = {} # trace_id → cancel event
  190. self.used_ex_ids: List[str] = [] # 当前运行中使用过的经验 ID
  191. # ===== 核心公开方法 =====
  192. async def run(
  193. self,
  194. messages: List[Dict],
  195. config: Optional[RunConfig] = None,
  196. ) -> AsyncIterator[Union[Trace, Message]]:
  197. """
  198. Agent 模式执行(核心方法)
  199. Args:
  200. messages: OpenAI SDK 格式的输入消息
  201. 新建: 初始任务消息 [{"role": "user", "content": "..."}]
  202. 续跑: 追加的新消息
  203. 回溯: 在插入点之后追加的消息
  204. config: 运行配置
  205. Yields:
  206. Union[Trace, Message]: Trace 对象(状态变化)或 Message 对象(执行过程)
  207. """
  208. if not self.llm_call:
  209. raise ValueError("llm_call function not provided")
  210. config = config or RunConfig()
  211. trace = None
  212. try:
  213. # Phase 1: PREPARE TRACE
  214. trace, goal_tree, sequence = await self._prepare_trace(messages, config)
  215. # 注册取消事件
  216. self._cancel_events[trace.trace_id] = asyncio.Event()
  217. yield trace
  218. # Phase 2: BUILD HISTORY
  219. history, sequence, created_messages, head_seq = await self._build_history(
  220. trace.trace_id, messages, goal_tree, config, sequence
  221. )
  222. # Update trace's head_sequence in memory
  223. trace.head_sequence = head_seq
  224. for msg in created_messages:
  225. yield msg
  226. # Phase 3: AGENT LOOP
  227. async for event in self._agent_loop(trace, history, goal_tree, config, sequence):
  228. yield event
  229. except Exception as e:
  230. logger.error(f"Agent run failed: {e}")
  231. tid = config.trace_id or (trace.trace_id if trace else None)
  232. if self.trace_store and tid:
  233. # 读取当前 last_sequence 作为 head_sequence,确保续跑时能加载完整历史
  234. current = await self.trace_store.get_trace(tid)
  235. head_seq = current.last_sequence if current else None
  236. await self.trace_store.update_trace(
  237. tid,
  238. status="failed",
  239. head_sequence=head_seq,
  240. error_message=str(e),
  241. completed_at=datetime.now()
  242. )
  243. trace_obj = await self.trace_store.get_trace(tid)
  244. if trace_obj:
  245. yield trace_obj
  246. raise
  247. finally:
  248. # 清理取消事件
  249. if trace:
  250. self._cancel_events.pop(trace.trace_id, None)
  251. async def run_result(
  252. self,
  253. messages: List[Dict],
  254. config: Optional[RunConfig] = None,
  255. ) -> Dict[str, Any]:
  256. """
  257. 结果模式 — 消费 run(),返回结构化结果。
  258. 主要用于 agent/evaluate 工具内部。
  259. """
  260. last_assistant_text = ""
  261. final_trace: Optional[Trace] = None
  262. async for item in self.run(messages=messages, config=config):
  263. if isinstance(item, Message) and item.role == "assistant":
  264. content = item.content
  265. text = ""
  266. if isinstance(content, dict):
  267. text = content.get("text", "") or ""
  268. elif isinstance(content, str):
  269. text = content
  270. if text and text.strip():
  271. last_assistant_text = text
  272. elif isinstance(item, Trace):
  273. final_trace = item
  274. config = config or RunConfig()
  275. if not final_trace and config.trace_id and self.trace_store:
  276. final_trace = await self.trace_store.get_trace(config.trace_id)
  277. status = final_trace.status if final_trace else "unknown"
  278. error = final_trace.error_message if final_trace else None
  279. summary = last_assistant_text
  280. if not summary:
  281. status = "failed"
  282. error = error or "Agent 没有产生 assistant 文本结果"
  283. return {
  284. "status": status,
  285. "summary": summary,
  286. "trace_id": final_trace.trace_id if final_trace else config.trace_id,
  287. "error": error,
  288. "stats": {
  289. "total_messages": final_trace.total_messages if final_trace else 0,
  290. "total_tokens": final_trace.total_tokens if final_trace else 0,
  291. "total_cost": final_trace.total_cost if final_trace else 0.0,
  292. },
  293. }
  294. async def stop(self, trace_id: str) -> bool:
  295. """
  296. 停止运行中的 Trace
  297. 设置取消信号,agent loop 在下一个 LLM 调用前检查并退出。
  298. Trace 状态置为 "stopped"。
  299. Returns:
  300. True 如果成功发送停止信号,False 如果该 trace 不在运行中
  301. """
  302. cancel_event = self._cancel_events.get(trace_id)
  303. if cancel_event is None:
  304. return False
  305. cancel_event.set()
  306. return True
  307. # ===== 单次调用(保留)=====
  308. async def call(
  309. self,
  310. messages: List[Dict],
  311. model: str = "gpt-4o",
  312. tools: Optional[List[str]] = None,
  313. uid: Optional[str] = None,
  314. trace: bool = True,
  315. **kwargs
  316. ) -> CallResult:
  317. """
  318. 单次 LLM 调用(无 Agent Loop)
  319. """
  320. if not self.llm_call:
  321. raise ValueError("llm_call function not provided")
  322. trace_id = None
  323. message_id = None
  324. tool_schemas = self._get_tool_schemas(tools)
  325. if trace and self.trace_store:
  326. trace_obj = Trace.create(mode="call", uid=uid, model=model, tools=tool_schemas, llm_params=kwargs)
  327. trace_id = await self.trace_store.create_trace(trace_obj)
  328. result = await self.llm_call(messages=messages, model=model, tools=tool_schemas, **kwargs)
  329. if trace and self.trace_store and trace_id:
  330. msg = Message.create(
  331. trace_id=trace_id, role="assistant", sequence=1, goal_id=None,
  332. content={"text": result.get("content", ""), "tool_calls": result.get("tool_calls")},
  333. prompt_tokens=result.get("prompt_tokens", 0),
  334. completion_tokens=result.get("completion_tokens", 0),
  335. finish_reason=result.get("finish_reason"),
  336. cost=result.get("cost", 0),
  337. )
  338. message_id = await self.trace_store.add_message(msg)
  339. await self.trace_store.update_trace(trace_id, status="completed", completed_at=datetime.now())
  340. return CallResult(
  341. reply=result.get("content", ""),
  342. tool_calls=result.get("tool_calls"),
  343. trace_id=trace_id,
  344. step_id=message_id,
  345. tokens={"prompt": result.get("prompt_tokens", 0), "completion": result.get("completion_tokens", 0)},
  346. cost=result.get("cost", 0)
  347. )
  348. # ===== Phase 1: PREPARE TRACE =====
  349. async def _prepare_trace(
  350. self,
  351. messages: List[Dict],
  352. config: RunConfig,
  353. ) -> Tuple[Trace, Optional[GoalTree], int]:
  354. """
  355. 准备 Trace:创建新的或加载已有的
  356. Returns:
  357. (trace, goal_tree, next_sequence)
  358. """
  359. if config.trace_id:
  360. return await self._prepare_existing_trace(config)
  361. else:
  362. return await self._prepare_new_trace(messages, config)
  363. async def _prepare_new_trace(
  364. self,
  365. messages: List[Dict],
  366. config: RunConfig,
  367. ) -> Tuple[Trace, Optional[GoalTree], int]:
  368. """创建新 Trace"""
  369. trace_id = str(uuid.uuid4())
  370. # 生成任务名称
  371. task_name = config.name or await self._generate_task_name(messages)
  372. # 准备工具 Schema
  373. tool_schemas = self._get_tool_schemas(config.tools)
  374. trace_obj = Trace(
  375. trace_id=trace_id,
  376. mode="agent",
  377. task=task_name,
  378. agent_type=config.agent_type,
  379. parent_trace_id=config.parent_trace_id,
  380. parent_goal_id=config.parent_goal_id,
  381. uid=config.uid,
  382. model=config.model,
  383. tools=tool_schemas,
  384. llm_params={"temperature": config.temperature, **config.extra_llm_params},
  385. status="running",
  386. )
  387. goal_tree = self.goal_tree or GoalTree(mission=task_name)
  388. if self.trace_store:
  389. await self.trace_store.create_trace(trace_obj)
  390. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  391. return trace_obj, goal_tree, 1
  392. async def _prepare_existing_trace(
  393. self,
  394. config: RunConfig,
  395. ) -> Tuple[Trace, Optional[GoalTree], int]:
  396. """加载已有 Trace(续跑或回溯)"""
  397. if not self.trace_store:
  398. raise ValueError("trace_store required for continue/rewind")
  399. trace_obj = await self.trace_store.get_trace(config.trace_id)
  400. if not trace_obj:
  401. raise ValueError(f"Trace not found: {config.trace_id}")
  402. goal_tree = await self.trace_store.get_goal_tree(config.trace_id)
  403. # 自动判断行为:after_sequence 为 None 或 == head → 续跑;< head → 回溯
  404. after_seq = config.after_sequence
  405. # 如果 after_seq > head_sequence,说明 generator 被强制关闭时 store 的
  406. # head_sequence 未来得及更新(仍停在 Phase 2 写入的初始值)。
  407. # 用 last_sequence 修正 head_sequence,确保续跑时能看到完整历史。
  408. if after_seq is not None and after_seq > trace_obj.head_sequence:
  409. trace_obj.head_sequence = trace_obj.last_sequence
  410. await self.trace_store.update_trace(
  411. config.trace_id, head_sequence=trace_obj.head_sequence
  412. )
  413. if after_seq is not None and after_seq < trace_obj.head_sequence:
  414. # 回溯模式
  415. sequence = await self._rewind(config.trace_id, after_seq, goal_tree)
  416. else:
  417. # 续跑模式:从 last_sequence + 1 开始
  418. sequence = trace_obj.last_sequence + 1
  419. # 状态置为 running
  420. await self.trace_store.update_trace(
  421. config.trace_id,
  422. status="running",
  423. completed_at=None,
  424. )
  425. trace_obj.status = "running"
  426. return trace_obj, goal_tree, sequence
  427. # ===== Phase 2: BUILD HISTORY =====
  428. async def _get_embedding(self, text: str) -> List[float]:
  429. """
  430. 获取文本的嵌入向量(Embedding)
  431. Args:
  432. text: 需要向量化的文本
  433. Returns:
  434. List[float]: 嵌入向量
  435. """
  436. if not text or not text.strip():
  437. return []
  438. # 优先使用注入的 embedding_call
  439. if self.embedding_call:
  440. try:
  441. return await self.embedding_call(text)
  442. except Exception as e:
  443. logger.error(f"Error in embedding_call: {e}")
  444. raise
  445. # 兜底方案:如果没有注入 embedding_call,但有 llm_call,
  446. # 某些 SDK 封装可能支持通过 llm_call 的客户端直接获取
  447. # 这里建议强制要求基础设施层提供该函数以保证分层清晰
  448. raise ValueError("embedding_call function not provided to AgentRunner")
  449. async def _build_history(
  450. self,
  451. trace_id: str,
  452. new_messages: List[Dict],
  453. goal_tree: Optional[GoalTree],
  454. config: RunConfig,
  455. sequence: int,
  456. ) -> Tuple[List[Dict], int, List[Message]]:
  457. """
  458. 构建完整的 LLM 消息历史
  459. 1. 从 head_sequence 沿 parent chain 加载主路径消息(续跑/回溯场景)
  460. 2. 构建 system prompt(新建时注入 skills)
  461. 3. 新建时:在第一条 user message 末尾注入当前经验
  462. 4. 追加 input messages(设置 parent_sequence 链接到当前 head)
  463. Returns:
  464. (history, next_sequence, created_messages, head_sequence)
  465. created_messages: 本次新创建并持久化的 Message 列表,供 run() yield 给调用方
  466. head_sequence: 当前主路径头节点的 sequence
  467. """
  468. history: List[Dict] = []
  469. created_messages: List[Message] = []
  470. head_seq: Optional[int] = None # 当前主路径的头节点 sequence
  471. # 1. 加载已有 messages(通过主路径遍历)
  472. if config.trace_id and self.trace_store:
  473. trace_obj = await self.trace_store.get_trace(trace_id)
  474. if trace_obj and trace_obj.head_sequence > 0:
  475. main_path = await self.trace_store.get_main_path_messages(
  476. trace_id, trace_obj.head_sequence
  477. )
  478. # 修复 orphaned tool_calls(中断导致的 tool_call 无 tool_result)
  479. main_path, sequence = await self._heal_orphaned_tool_calls(
  480. main_path, trace_id, goal_tree, sequence,
  481. )
  482. history = [msg.to_llm_dict() for msg in main_path]
  483. if main_path:
  484. head_seq = main_path[-1].sequence
  485. # 2. 构建 system prompt(如果历史中没有 system message)
  486. has_system = any(m.get("role") == "system" for m in history)
  487. has_system_in_new = any(m.get("role") == "system" for m in new_messages)
  488. if not has_system and not has_system_in_new:
  489. system_prompt = await self._build_system_prompt(config)
  490. if system_prompt:
  491. history = [{"role": "system", "content": system_prompt}] + history
  492. if self.trace_store:
  493. system_msg = Message.create(
  494. trace_id=trace_id, role="system", sequence=sequence,
  495. goal_id=None, content=system_prompt,
  496. parent_sequence=None, # system message 是 root
  497. )
  498. await self.trace_store.add_message(system_msg)
  499. created_messages.append(system_msg)
  500. head_seq = sequence
  501. sequence += 1
  502. # 3. 追加新 messages(设置 parent_sequence 链接到当前 head)
  503. for msg_dict in new_messages:
  504. history.append(msg_dict)
  505. if self.trace_store:
  506. stored_msg = Message.from_llm_dict(
  507. msg_dict, trace_id=trace_id, sequence=sequence,
  508. goal_id=None, parent_sequence=head_seq,
  509. )
  510. await self.trace_store.add_message(stored_msg)
  511. created_messages.append(stored_msg)
  512. head_seq = sequence
  513. sequence += 1
  514. # 5. 更新 trace 的 head_sequence
  515. if self.trace_store and head_seq is not None:
  516. await self.trace_store.update_trace(trace_id, head_sequence=head_seq)
  517. return history, sequence, created_messages, head_seq or 0
  518. # ===== Phase 3: AGENT LOOP =====
  519. async def _agent_loop(
  520. self,
  521. trace: Trace,
  522. history: List[Dict],
  523. goal_tree: Optional[GoalTree],
  524. config: RunConfig,
  525. sequence: int,
  526. ) -> AsyncIterator[Union[Trace, Message]]:
  527. """ReAct 循环"""
  528. trace_id = trace.trace_id
  529. tool_schemas = self._get_tool_schemas(config.tools)
  530. # 当前主路径头节点的 sequence(用于设置 parent_sequence)
  531. head_seq = trace.head_sequence
  532. # 设置 goal_tree 到 goal 工具
  533. if goal_tree and self.trace_store:
  534. from agent.trace.goal_tool import set_goal_tree
  535. set_goal_tree(goal_tree)
  536. # 经验检索缓存:只在 goal 切换时重新检索
  537. _last_goal_id = None
  538. _cached_exp_text = ""
  539. for iteration in range(config.max_iterations):
  540. # 检查取消信号
  541. cancel_event = self._cancel_events.get(trace_id)
  542. if cancel_event and cancel_event.is_set():
  543. logger.info(f"Trace {trace_id} stopped by user")
  544. if self.trace_store:
  545. await self.trace_store.update_trace(
  546. trace_id,
  547. status="stopped",
  548. head_sequence=head_seq,
  549. completed_at=datetime.now(),
  550. )
  551. trace_obj = await self.trace_store.get_trace(trace_id)
  552. if trace_obj:
  553. yield trace_obj
  554. return
  555. # Level 1 压缩:GoalTree 过滤(当消息超过阈值时触发)
  556. compression_config = CompressionConfig()
  557. token_count = estimate_tokens(history)
  558. max_tokens = compression_config.get_max_tokens(config.model)
  559. if token_count > max_tokens and self.trace_store and goal_tree:
  560. # 使用本地 head_seq(store 中的 head_sequence 在 loop 期间未更新,是过时的)
  561. if head_seq > 0:
  562. main_path_msgs = await self.trace_store.get_main_path_messages(
  563. trace_id, head_seq
  564. )
  565. filtered_msgs = filter_by_goal_status(main_path_msgs, goal_tree)
  566. if len(filtered_msgs) < len(main_path_msgs):
  567. logger.info(
  568. "Level 1 压缩: %d -> %d 条消息 (tokens ~%d, 阈值 %d)",
  569. len(main_path_msgs), len(filtered_msgs), token_count, max_tokens,
  570. )
  571. history = [msg.to_llm_dict() for msg in filtered_msgs]
  572. else:
  573. logger.info(
  574. "Level 1 压缩: 无可过滤消息 (%d 条全部保留, completed/abandoned goals=%d)",
  575. len(main_path_msgs),
  576. sum(1 for g in goal_tree.goals
  577. if g.status in ("completed", "abandoned")),
  578. )
  579. elif token_count > max_tokens:
  580. logger.warning(
  581. "消息 token 数 (%d) 超过阈值 (%d),但无法执行 Level 1 压缩(缺少 store 或 goal_tree)",
  582. token_count, max_tokens,
  583. )
  584. # Level 2 压缩:LLM 总结(Level 1 后仍超阈值时触发)
  585. token_count_after = estimate_tokens(history)
  586. if token_count_after > max_tokens:
  587. logger.info(
  588. "Level 1 后 token 仍超阈值 (%d > %d),触发 Level 2 压缩",
  589. token_count_after, max_tokens,
  590. )
  591. history, head_seq, sequence = await self._compress_history(
  592. trace_id, history, goal_tree, config, sequence, head_seq,
  593. )
  594. # 构建 LLM messages(注入上下文)
  595. llm_messages = list(history)
  596. # 周期性注入 GoalTree + Collaborators
  597. if iteration % CONTEXT_INJECTION_INTERVAL == 0:
  598. context_injection = self._build_context_injection(trace, goal_tree)
  599. if context_injection:
  600. llm_messages.append({"role": "system", "content": context_injection})
  601. # 经验检索:goal 切换时重新检索,注入为 system message
  602. current_goal_id = goal_tree.current_id if goal_tree else None
  603. if current_goal_id and current_goal_id != _last_goal_id:
  604. _last_goal_id = current_goal_id
  605. current_goal = goal_tree.find(current_goal_id)
  606. if current_goal:
  607. try:
  608. relevant_exps = await _get_structured_experiences(
  609. query_text=current_goal.description,
  610. top_k=3
  611. )
  612. if relevant_exps:
  613. self.used_ex_ids = [exp['id'] for exp in relevant_exps]
  614. parts = [f"[{exp['id']}] {exp['content']}" for exp in relevant_exps]
  615. _cached_exp_text = "## 参考历史经验\n" + "\n\n".join(parts)
  616. logger.info(
  617. "经验检索: goal='%s', 命中 %d 条 %s",
  618. current_goal.description[:40],
  619. len(relevant_exps),
  620. self.used_ex_ids,
  621. )
  622. else:
  623. _cached_exp_text = ""
  624. except Exception as e:
  625. logger.warning("经验检索失败: %s", e)
  626. _cached_exp_text = ""
  627. if _cached_exp_text:
  628. llm_messages.append({"role": "system", "content": _cached_exp_text})
  629. # 调用 LLM
  630. result = await self.llm_call(
  631. messages=llm_messages,
  632. model=config.model,
  633. tools=tool_schemas,
  634. temperature=config.temperature,
  635. **config.extra_llm_params,
  636. )
  637. response_content = result.get("content", "")
  638. tool_calls = result.get("tool_calls")
  639. finish_reason = result.get("finish_reason")
  640. prompt_tokens = result.get("prompt_tokens", 0)
  641. completion_tokens = result.get("completion_tokens", 0)
  642. step_cost = result.get("cost", 0)
  643. # 按需自动创建 root goal
  644. if goal_tree and not goal_tree.goals and tool_calls:
  645. has_goal_call = any(
  646. tc.get("function", {}).get("name") == "goal"
  647. for tc in tool_calls
  648. )
  649. if not has_goal_call:
  650. mission = goal_tree.mission
  651. root_desc = mission[:200] if len(mission) > 200 else mission
  652. goal_tree.add_goals(
  653. descriptions=[root_desc],
  654. reasons=["系统自动创建:Agent 未显式创建目标"],
  655. parent_id=None
  656. )
  657. goal_tree.focus(goal_tree.goals[0].id)
  658. if self.trace_store:
  659. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  660. await self.trace_store.add_goal(trace_id, goal_tree.goals[0])
  661. logger.info(f"自动创建 root goal: {goal_tree.goals[0].id}")
  662. # 获取当前 goal_id
  663. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  664. # 记录 assistant Message(parent_sequence 指向当前 head)
  665. assistant_msg = Message.create(
  666. trace_id=trace_id,
  667. role="assistant",
  668. sequence=sequence,
  669. goal_id=current_goal_id,
  670. parent_sequence=head_seq if head_seq > 0 else None,
  671. content={"text": response_content, "tool_calls": tool_calls},
  672. prompt_tokens=prompt_tokens,
  673. completion_tokens=completion_tokens,
  674. finish_reason=finish_reason,
  675. cost=step_cost,
  676. )
  677. if self.trace_store:
  678. await self.trace_store.add_message(assistant_msg)
  679. yield assistant_msg
  680. head_seq = sequence
  681. sequence += 1
  682. # 处理工具调用
  683. # 截断兜底:finish_reason == "length" 说明响应被 max_tokens 截断,
  684. # tool call 参数很可能不完整,不应执行,改为提示模型分批操作
  685. if tool_calls and finish_reason == "length":
  686. logger.warning(
  687. "[Runner] 响应被 max_tokens 截断,跳过 %d 个不完整的 tool calls",
  688. len(tool_calls),
  689. )
  690. truncation_hint = (
  691. "你的响应因为 max_tokens 限制被截断,tool call 参数不完整,未执行。"
  692. "请将大内容拆分为多次小的工具调用(例如用 write_file 的 append 模式分批写入)。"
  693. )
  694. history.append({
  695. "role": "assistant",
  696. "content": response_content,
  697. "tool_calls": tool_calls,
  698. })
  699. # 为每个被截断的 tool call 返回错误结果
  700. for tc in tool_calls:
  701. history.append({
  702. "role": "tool",
  703. "tool_call_id": tc["id"],
  704. "content": truncation_hint,
  705. })
  706. continue
  707. if tool_calls and config.auto_execute_tools:
  708. history.append({
  709. "role": "assistant",
  710. "content": response_content,
  711. "tool_calls": tool_calls,
  712. })
  713. for tc in tool_calls:
  714. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  715. tool_name = tc["function"]["name"]
  716. tool_args = tc["function"]["arguments"]
  717. if isinstance(tool_args, str):
  718. tool_args = json.loads(tool_args) if tool_args.strip() else {}
  719. elif tool_args is None:
  720. tool_args = {}
  721. tool_result = await self.tools.execute(
  722. tool_name,
  723. tool_args,
  724. uid=config.uid or "",
  725. context={
  726. "store": self.trace_store,
  727. "trace_id": trace_id,
  728. "goal_id": current_goal_id,
  729. "runner": self,
  730. }
  731. )
  732. # --- 支持多模态工具反馈 ---
  733. # execute() 返回 dict{"text","images"} 或 str
  734. if isinstance(tool_result, dict) and tool_result.get("images"):
  735. tool_result_text = tool_result["text"]
  736. # 构建多模态消息格式
  737. tool_content_for_llm = [{"type": "text", "text": tool_result_text}]
  738. for img in tool_result["images"]:
  739. if img.get("type") == "base64" and img.get("data"):
  740. media_type = img.get("media_type", "image/png")
  741. tool_content_for_llm.append({
  742. "type": "image_url",
  743. "image_url": {
  744. "url": f"data:{media_type};base64,{img['data']}"
  745. }
  746. })
  747. img_count = len(tool_content_for_llm) - 1 # 减去 text 块
  748. print(f"[Runner] 多模态工具反馈: tool={tool_name}, images={img_count}, text_len={len(tool_result_text)}")
  749. else:
  750. tool_result_text = str(tool_result)
  751. tool_content_for_llm = tool_result_text
  752. tool_msg = Message.create(
  753. trace_id=trace_id,
  754. role="tool",
  755. sequence=sequence,
  756. goal_id=current_goal_id,
  757. parent_sequence=head_seq,
  758. tool_call_id=tc["id"],
  759. content={"tool_name": tool_name, "result": tool_result_text},
  760. )
  761. if self.trace_store:
  762. await self.trace_store.add_message(tool_msg)
  763. # 截图单独存为同名 PNG 文件
  764. if isinstance(tool_result, dict) and tool_result.get("images"):
  765. import base64 as b64mod
  766. for img in tool_result["images"]:
  767. if img.get("data"):
  768. png_path = self.trace_store._get_messages_dir(trace_id) / f"{tool_msg.message_id}.png"
  769. png_path.write_bytes(b64mod.b64decode(img["data"]))
  770. print(f"[Runner] 截图已保存: {png_path.name}")
  771. break # 只存第一张
  772. yield tool_msg
  773. head_seq = sequence
  774. sequence += 1
  775. history.append({
  776. "role": "tool",
  777. "tool_call_id": tc["id"],
  778. "name": tool_name,
  779. "content": tool_content_for_llm, # 这里传入 list 即可触发模型的视觉能力
  780. })
  781. # ------------------------------------------
  782. continue # 继续循环
  783. # 无工具调用,任务完成
  784. break
  785. # 更新 head_sequence 并完成 Trace
  786. if self.trace_store:
  787. await self.trace_store.update_trace(
  788. trace_id,
  789. status="completed",
  790. head_sequence=head_seq,
  791. completed_at=datetime.now(),
  792. )
  793. trace_obj = await self.trace_store.get_trace(trace_id)
  794. if trace_obj:
  795. yield trace_obj
  796. # ===== Level 2: LLM 压缩 =====
  797. async def _compress_history(
  798. self,
  799. trace_id: str,
  800. history: List[Dict],
  801. goal_tree: Optional[GoalTree],
  802. config: RunConfig,
  803. sequence: int,
  804. head_seq: int,
  805. ) -> Tuple[List[Dict], int, int]:
  806. """
  807. Level 2 压缩:LLM 总结
  808. Step 1: 经验提取(reflect)— 纯内存 LLM 调用 + 文件追加,不影响 trace
  809. Step 2: 压缩总结 — LLM 生成 summary
  810. Step 3: 存储 summary 为新消息,parent_sequence 跳到 system msg
  811. Step 4: 重建 history
  812. Returns:
  813. (new_history, new_head_seq, next_sequence)
  814. """
  815. logger.info("Level 2 压缩开始: trace=%s, 当前 history 长度=%d", trace_id, len(history))
  816. # 找到 system message 的 sequence(主路径第一条消息)
  817. system_msg_seq = None
  818. system_msg_dict = None
  819. if self.trace_store:
  820. trace_obj = await self.trace_store.get_trace(trace_id)
  821. if trace_obj and trace_obj.head_sequence > 0:
  822. main_path = await self.trace_store.get_main_path_messages(
  823. trace_id, trace_obj.head_sequence
  824. )
  825. for msg in main_path:
  826. if msg.role == "system":
  827. system_msg_seq = msg.sequence
  828. system_msg_dict = msg.to_llm_dict()
  829. break
  830. # Fallback: 从 history 中找 system message
  831. if system_msg_dict is None:
  832. for msg_dict in history:
  833. if msg_dict.get("role") == "system":
  834. system_msg_dict = msg_dict
  835. break
  836. if system_msg_dict is None:
  837. logger.warning("Level 2 压缩跳过:未找到 system message")
  838. return history, head_seq, sequence
  839. # --- Step 1: 经验提取(reflect)---
  840. try:
  841. # 1. 构造 Reflect Prompt(确保包含格式要求)
  842. # 建议在 build_reflect_prompt() 里加入:
  843. # "请使用格式:- [intent: 意图, state: 状态描述] 具体的经验内容"
  844. reflect_prompt = build_reflect_prompt()
  845. reflect_messages = list(history) + [{"role": "user", "content": reflect_prompt}]
  846. reflect_result = await self.llm_call(
  847. messages=reflect_messages,
  848. model=config.model,
  849. tools=[],
  850. temperature=0.2, # 略微保持一点发散性
  851. **config.extra_llm_params,
  852. )
  853. reflection_text = reflect_result.get("content", "").strip()
  854. if reflection_text:
  855. import re as _re2
  856. import uuid as _uuid2
  857. pattern = r"-\s*\[(?P<tags>.*?)\]\s*(?P<content>.*)"
  858. matches = list(_re2.finditer(pattern, reflection_text))
  859. structured_entries = []
  860. for match in matches:
  861. tags_str = match.group("tags")
  862. content = match.group("content")
  863. intent_match = _re2.search(r"intent:\s*(.*?)(?:,|$)", tags_str, _re2.IGNORECASE)
  864. state_match = _re2.search(r"state:\s*(.*?)(?:,|$)", tags_str, _re2.IGNORECASE)
  865. intents = [i.strip() for i in intent_match.group(1).split(",")] if intent_match and intent_match.group(1) else []
  866. states = [s.strip() for s in state_match.group(1).split(",")] if state_match and state_match.group(1) else []
  867. ex_id = f"ex_{datetime.now().strftime('%m%d%H%M')}_{_uuid2.uuid4().hex[:4]}"
  868. entry = f"""---
  869. id: {ex_id}
  870. trace_id: {trace_id}
  871. tags: {{intent: {intents}, state: {states}}}
  872. metrics: {{helpful: 1, harmful: 0}}
  873. created_at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
  874. ---
  875. - {content}
  876. - 经验ID: [{ex_id}]"""
  877. structured_entries.append(entry)
  878. if structured_entries:
  879. os.makedirs(os.path.dirname(self.experiences_path), exist_ok=True)
  880. with open(self.experiences_path, "a", encoding="utf-8") as f:
  881. f.write("\n\n" + "\n\n".join(structured_entries))
  882. logger.info(f"已提取并保存 {len(structured_entries)} 条结构化经验")
  883. else:
  884. logger.warning("未能解析出符合格式的经验条目,请检查 REFLECT_PROMPT。")
  885. logger.debug(f"LLM Raw Output:\n{reflection_text}")
  886. else:
  887. logger.warning("LLM 未生成反思内容")
  888. except Exception as e:
  889. logger.error(f"Level 2 经验提取失败: {e}")
  890. # --- Step 2: 压缩总结 + 经验评估 ---
  891. compress_prompt = build_compression_prompt(goal_tree, used_ex_ids=self.used_ex_ids)
  892. compress_messages = list(history) + [{"role": "user", "content": compress_prompt}]
  893. compress_result = await self.llm_call(
  894. messages=compress_messages,
  895. model=config.model,
  896. tools=[],
  897. temperature=config.temperature,
  898. **config.extra_llm_params,
  899. )
  900. raw_output = compress_result.get("content", "").strip()
  901. if not raw_output:
  902. logger.warning("Level 2 压缩跳过:LLM 未返回内容")
  903. return history, head_seq, sequence
  904. # 解析 [[EVALUATION]] 块并更新经验
  905. if self.used_ex_ids:
  906. try:
  907. eval_block = ""
  908. if "[[EVALUATION]]" in raw_output:
  909. eval_start = raw_output.index("[[EVALUATION]]") + len("[[EVALUATION]]")
  910. eval_end = raw_output.index("[[SUMMARY]]") if "[[SUMMARY]]" in raw_output else len(raw_output)
  911. eval_block = raw_output[eval_start:eval_end].strip()
  912. if eval_block:
  913. import re as _re
  914. update_map = {}
  915. for line in eval_block.splitlines():
  916. m = _re.search(r"ID:\s*(ex_\S+)\s*\|\s*Result:\s*(\w+)", line)
  917. if m:
  918. ex_id, result = m.group(1), m.group(2).lower()
  919. if result in ("helpful", "harmful"):
  920. update_map[ex_id] = {"action": result, "feedback": ""}
  921. elif result == "mixed":
  922. update_map[ex_id] = {"action": "helpful", "feedback": ""}
  923. if update_map:
  924. count = await _batch_update_experiences(update_map)
  925. logger.info("经验评估完成,更新了 %s 条经验", count)
  926. except Exception as e:
  927. logger.warning("经验评估解析失败(不影响压缩): %s", e)
  928. # 提取 [[SUMMARY]] 块
  929. summary_text = raw_output
  930. if "[[SUMMARY]]" in raw_output:
  931. summary_text = raw_output[raw_output.index("[[SUMMARY]]") + len("[[SUMMARY]]"):].strip()
  932. # 压缩完成后清空 used_ex_ids
  933. self.used_ex_ids = []
  934. if not summary_text:
  935. logger.warning("Level 2 压缩跳过:LLM 未返回 summary")
  936. return history, head_seq, sequence
  937. # --- Step 3: 存储 summary 消息 ---
  938. summary_with_header = (
  939. f"## 对话历史摘要(自动压缩)\n\n{summary_text}\n\n"
  940. "---\n请基于以上摘要和当前 GoalTree 继续执行任务。"
  941. )
  942. summary_msg = Message.create(
  943. trace_id=trace_id,
  944. role="user",
  945. sequence=sequence,
  946. goal_id=None,
  947. parent_sequence=system_msg_seq, # 跳到 system msg,跳过所有中间消息
  948. content=summary_with_header,
  949. )
  950. if self.trace_store:
  951. await self.trace_store.add_message(summary_msg)
  952. new_head_seq = sequence
  953. sequence += 1
  954. # --- Step 4: 重建 history ---
  955. new_history = [system_msg_dict, summary_msg.to_llm_dict()]
  956. # 更新 trace head_sequence
  957. if self.trace_store:
  958. await self.trace_store.update_trace(
  959. trace_id,
  960. head_sequence=new_head_seq,
  961. )
  962. logger.info(
  963. "Level 2 压缩完成: 旧 history %d 条 → 新 history %d 条, summary 长度=%d",
  964. len(history), len(new_history), len(summary_text),
  965. )
  966. return new_history, new_head_seq, sequence
  967. # ===== 回溯(Rewind)=====
  968. async def _rewind(
  969. self,
  970. trace_id: str,
  971. after_sequence: int,
  972. goal_tree: Optional[GoalTree],
  973. ) -> int:
  974. """
  975. 执行回溯:快照 GoalTree,重建干净树,设置 head_sequence
  976. 新消息的 parent_sequence 将指向 rewind 点,旧消息通过树结构自然脱离主路径。
  977. Returns:
  978. 下一个可用的 sequence 号
  979. """
  980. if not self.trace_store:
  981. raise ValueError("trace_store required for rewind")
  982. # 1. 加载所有 messages(用于 safe cutoff 和 max sequence)
  983. all_messages = await self.trace_store.get_trace_messages(trace_id)
  984. if not all_messages:
  985. return 1
  986. # 2. 找到安全截断点(确保不截断在 tool_call 和 tool response 之间)
  987. cutoff = self._find_safe_cutoff(all_messages, after_sequence)
  988. # 3. 快照并重建 GoalTree
  989. if goal_tree:
  990. # 获取截断点消息的 created_at 作为时间界限
  991. cutoff_msg = None
  992. for msg in all_messages:
  993. if msg.sequence == cutoff:
  994. cutoff_msg = msg
  995. break
  996. cutoff_time = cutoff_msg.created_at if cutoff_msg else datetime.now()
  997. # 快照到 events(含 head_sequence 供前端感知分支切换)
  998. await self.trace_store.append_event(trace_id, "rewind", {
  999. "after_sequence": cutoff,
  1000. "head_sequence": cutoff,
  1001. "goal_tree_snapshot": goal_tree.to_dict(),
  1002. })
  1003. # 按时间重建干净的 GoalTree
  1004. new_tree = goal_tree.rebuild_for_rewind(cutoff_time)
  1005. await self.trace_store.update_goal_tree(trace_id, new_tree)
  1006. # 更新内存中的引用
  1007. goal_tree.goals = new_tree.goals
  1008. goal_tree.current_id = new_tree.current_id
  1009. # 4. 更新 head_sequence 到 rewind 点
  1010. await self.trace_store.update_trace(trace_id, head_sequence=cutoff)
  1011. # 5. 返回 next sequence(全局递增,不复用)
  1012. max_seq = max((m.sequence for m in all_messages), default=0)
  1013. return max_seq + 1
  1014. def _find_safe_cutoff(self, messages: List[Message], after_sequence: int) -> int:
  1015. """
  1016. 找到安全的截断点。
  1017. 如果 after_sequence 指向一条带 tool_calls 的 assistant message,
  1018. 则自动扩展到其所有对应的 tool response 之后。
  1019. """
  1020. cutoff = after_sequence
  1021. # 找到 after_sequence 对应的 message
  1022. target_msg = None
  1023. for msg in messages:
  1024. if msg.sequence == after_sequence:
  1025. target_msg = msg
  1026. break
  1027. if not target_msg:
  1028. return cutoff
  1029. # 如果是 assistant 且有 tool_calls,找到所有对应的 tool responses
  1030. if target_msg.role == "assistant":
  1031. content = target_msg.content
  1032. if isinstance(content, dict) and content.get("tool_calls"):
  1033. tool_call_ids = set()
  1034. for tc in content["tool_calls"]:
  1035. if isinstance(tc, dict) and tc.get("id"):
  1036. tool_call_ids.add(tc["id"])
  1037. # 找到这些 tool_call 对应的 tool messages
  1038. for msg in messages:
  1039. if (msg.role == "tool" and msg.tool_call_id
  1040. and msg.tool_call_id in tool_call_ids):
  1041. cutoff = max(cutoff, msg.sequence)
  1042. return cutoff
  1043. async def _heal_orphaned_tool_calls(
  1044. self,
  1045. messages: List[Message],
  1046. trace_id: str,
  1047. goal_tree: Optional[GoalTree],
  1048. sequence: int,
  1049. ) -> tuple:
  1050. """
  1051. 检测并修复消息历史中的 orphaned tool_calls。
  1052. 当 agent 被 stop/crash 中断时,可能有 assistant 的 tool_calls 没有对应的
  1053. tool results(包括多 tool_call 部分完成的情况)。直接发给 LLM 会导致 400。
  1054. 修复策略:为每个缺失的 tool_result 插入合成的"中断通知"消息,而非裁剪。
  1055. - 普通工具:简短中断提示
  1056. - agent/evaluate:包含 sub_trace_id、执行统计、continue_from 指引
  1057. 合成消息持久化到 store,确保幂等(下次续跑不再触发)。
  1058. Returns:
  1059. (healed_messages, next_sequence)
  1060. """
  1061. if not messages:
  1062. return messages, sequence
  1063. # 收集所有 tool_call IDs → (assistant_msg, tool_call_dict)
  1064. tc_map: Dict[str, tuple] = {}
  1065. result_ids: set = set()
  1066. for msg in messages:
  1067. if msg.role == "assistant":
  1068. content = msg.content
  1069. if isinstance(content, dict) and content.get("tool_calls"):
  1070. for tc in content["tool_calls"]:
  1071. tc_id = tc.get("id")
  1072. if tc_id:
  1073. tc_map[tc_id] = (msg, tc)
  1074. elif msg.role == "tool" and msg.tool_call_id:
  1075. result_ids.add(msg.tool_call_id)
  1076. orphaned_ids = [tc_id for tc_id in tc_map if tc_id not in result_ids]
  1077. if not orphaned_ids:
  1078. return messages, sequence
  1079. logger.info(
  1080. "检测到 %d 个 orphaned tool_calls,生成合成中断通知",
  1081. len(orphaned_ids),
  1082. )
  1083. healed = list(messages)
  1084. head_seq = messages[-1].sequence
  1085. for tc_id in orphaned_ids:
  1086. assistant_msg, tc = tc_map[tc_id]
  1087. tool_name = tc.get("function", {}).get("name", "unknown")
  1088. if tool_name in ("agent", "evaluate"):
  1089. result_text = self._build_agent_interrupted_result(
  1090. tc, goal_tree, assistant_msg,
  1091. )
  1092. else:
  1093. result_text = (
  1094. f"⚠️ 工具 {tool_name} 执行被中断(进程异常退出),"
  1095. "未获得执行结果。请根据需要重新调用。"
  1096. )
  1097. synthetic_msg = Message.create(
  1098. trace_id=trace_id,
  1099. role="tool",
  1100. sequence=sequence,
  1101. goal_id=assistant_msg.goal_id,
  1102. parent_sequence=head_seq,
  1103. tool_call_id=tc_id,
  1104. content={"tool_name": tool_name, "result": result_text},
  1105. )
  1106. if self.trace_store:
  1107. await self.trace_store.add_message(synthetic_msg)
  1108. healed.append(synthetic_msg)
  1109. head_seq = sequence
  1110. sequence += 1
  1111. # 更新 trace head/last sequence
  1112. if self.trace_store:
  1113. await self.trace_store.update_trace(
  1114. trace_id,
  1115. head_sequence=head_seq,
  1116. last_sequence=max(head_seq, sequence - 1),
  1117. )
  1118. return healed, sequence
  1119. def _build_agent_interrupted_result(
  1120. self,
  1121. tc: Dict,
  1122. goal_tree: Optional[GoalTree],
  1123. assistant_msg: Message,
  1124. ) -> str:
  1125. """为中断的 agent/evaluate 工具调用构建合成结果(对齐正常返回值格式)"""
  1126. args_str = tc.get("function", {}).get("arguments", "{}")
  1127. try:
  1128. args = json.loads(args_str) if isinstance(args_str, str) else args_str
  1129. except json.JSONDecodeError:
  1130. args = {}
  1131. task = args.get("task", "未知任务")
  1132. if isinstance(task, list):
  1133. task = "; ".join(task)
  1134. tool_name = tc.get("function", {}).get("name", "agent")
  1135. mode = "evaluate" if tool_name == "evaluate" else "delegate"
  1136. # 从 goal_tree 查找 sub_trace 信息
  1137. sub_trace_id = None
  1138. stats = None
  1139. if goal_tree and assistant_msg.goal_id:
  1140. goal = goal_tree.find(assistant_msg.goal_id)
  1141. if goal and goal.sub_trace_ids:
  1142. first = goal.sub_trace_ids[0]
  1143. if isinstance(first, dict):
  1144. sub_trace_id = first.get("trace_id")
  1145. elif isinstance(first, str):
  1146. sub_trace_id = first
  1147. if goal.cumulative_stats:
  1148. s = goal.cumulative_stats
  1149. if s.message_count > 0:
  1150. stats = {
  1151. "message_count": s.message_count,
  1152. "total_tokens": s.total_tokens,
  1153. "total_cost": round(s.total_cost, 4),
  1154. }
  1155. result: Dict[str, Any] = {
  1156. "mode": mode,
  1157. "status": "interrupted",
  1158. "summary": "⚠️ 子Agent执行被中断(进程异常退出)",
  1159. "task": task,
  1160. }
  1161. if sub_trace_id:
  1162. result["sub_trace_id"] = sub_trace_id
  1163. result["hint"] = (
  1164. f'使用 continue_from="{sub_trace_id}" 可继续执行,保留已有进度'
  1165. )
  1166. if stats:
  1167. result["stats"] = stats
  1168. return json.dumps(result, ensure_ascii=False, indent=2)
  1169. # ===== 上下文注入 =====
  1170. def _build_context_injection(
  1171. self,
  1172. trace: Trace,
  1173. goal_tree: Optional[GoalTree],
  1174. ) -> str:
  1175. """构建周期性注入的上下文(GoalTree + Active Collaborators + Focus 提醒)"""
  1176. parts = []
  1177. # GoalTree
  1178. if goal_tree and goal_tree.goals:
  1179. parts.append(f"## Current Plan\n\n{goal_tree.to_prompt()}")
  1180. # 检测 focus 在有子节点的父目标上:提醒模型 focus 到具体子目标
  1181. if goal_tree.current_id:
  1182. children = goal_tree.get_children(goal_tree.current_id)
  1183. pending_children = [c for c in children if c.status in ("pending", "in_progress")]
  1184. if pending_children:
  1185. child_ids = ", ".join(
  1186. goal_tree._generate_display_id(c) for c in pending_children[:3]
  1187. )
  1188. parts.append(
  1189. f"**提醒**:当前焦点在父目标上,建议用 `goal(focus=\"...\")` "
  1190. f"切换到具体子目标(如 {child_ids})再执行。"
  1191. )
  1192. # Active Collaborators
  1193. collaborators = trace.context.get("collaborators", [])
  1194. if collaborators:
  1195. lines = ["## Active Collaborators"]
  1196. for c in collaborators:
  1197. status_str = c.get("status", "unknown")
  1198. ctype = c.get("type", "agent")
  1199. summary = c.get("summary", "")
  1200. name = c.get("name", "unnamed")
  1201. lines.append(f"- {name} [{ctype}, {status_str}]: {summary}")
  1202. parts.append("\n".join(lines))
  1203. return "\n\n".join(parts)
  1204. # ===== 辅助方法 =====
  1205. def _get_tool_schemas(self, tools: Optional[List[str]]) -> List[Dict]:
  1206. """
  1207. 获取工具 Schema
  1208. - tools=None: 使用 registry 中全部已注册工具(含内置 + 外部注册的)
  1209. - tools=["a", "b"]: 在 BUILTIN_TOOLS 基础上追加指定工具
  1210. """
  1211. if tools is None:
  1212. # 全部已注册工具
  1213. tool_names = self.tools.get_tool_names()
  1214. else:
  1215. # BUILTIN_TOOLS + 显式指定的额外工具
  1216. tool_names = BUILTIN_TOOLS.copy()
  1217. for t in tools:
  1218. if t not in tool_names:
  1219. tool_names.append(t)
  1220. return self.tools.get_schemas(tool_names)
  1221. # 默认 system prompt 前缀(当 config.system_prompt 和前端都未提供 system message 时使用)
  1222. DEFAULT_SYSTEM_PREFIX = "你是最顶尖的AI助手,可以拆分并调用工具逐步解决复杂问题。"
  1223. async def _build_system_prompt(self, config: RunConfig) -> Optional[str]:
  1224. """构建 system prompt(注入 skills)"""
  1225. system_prompt = config.system_prompt
  1226. # 加载 Skills
  1227. skills_text = ""
  1228. skills = load_skills_from_dir(self.skills_dir)
  1229. if skills:
  1230. skills_text = self._format_skills(skills)
  1231. # 拼装:有自定义 system_prompt 则用它,否则用默认前缀
  1232. if system_prompt:
  1233. if skills_text:
  1234. system_prompt += f"\n\n## Skills\n{skills_text}"
  1235. else:
  1236. system_prompt = self.DEFAULT_SYSTEM_PREFIX
  1237. if skills_text:
  1238. system_prompt += f"\n\n## Skills\n{skills_text}"
  1239. return system_prompt
  1240. async def _generate_task_name(self, messages: List[Dict]) -> str:
  1241. """生成任务名称:优先使用 utility_llm,fallback 到文本截取"""
  1242. # 提取 messages 中的文本内容
  1243. text_parts = []
  1244. for msg in messages:
  1245. content = msg.get("content", "")
  1246. if isinstance(content, str):
  1247. text_parts.append(content)
  1248. elif isinstance(content, list):
  1249. for part in content:
  1250. if isinstance(part, dict) and part.get("type") == "text":
  1251. text_parts.append(part.get("text", ""))
  1252. raw_text = " ".join(text_parts).strip()
  1253. if not raw_text:
  1254. return "未命名任务"
  1255. # 尝试使用 utility_llm 生成标题
  1256. if self.utility_llm_call:
  1257. try:
  1258. result = await self.utility_llm_call(
  1259. messages=[
  1260. {"role": "system", "content": "用中文为以下任务生成一个简短标题(10-30字),只输出标题本身:"},
  1261. {"role": "user", "content": raw_text[:2000]},
  1262. ],
  1263. model="gpt-4o-mini", # 使用便宜模型
  1264. )
  1265. title = result.get("content", "").strip()
  1266. if title and len(title) < 100:
  1267. return title
  1268. except Exception:
  1269. pass
  1270. # Fallback: 截取前 50 字符
  1271. return raw_text[:50] + ("..." if len(raw_text) > 50 else "")
  1272. def _format_skills(self, skills: List[Skill]) -> str:
  1273. if not skills:
  1274. return ""
  1275. return "\n\n".join(s.to_prompt_text() for s in skills)
  1276. def _load_experiences(self) -> str:
  1277. """从文件加载经验(./.cache/experiences.md)"""
  1278. if not self.experiences_path:
  1279. return ""
  1280. try:
  1281. if os.path.exists(self.experiences_path):
  1282. with open(self.experiences_path, "r", encoding="utf-8") as f:
  1283. return f.read().strip()
  1284. except Exception as e:
  1285. logger.warning(f"Failed to load experiences from {self.experiences_path}: {e}")
  1286. return ""