runner.py 59 KB

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