runner.py 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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. # 自动判断行为:after_sequence 为 None 或 == head → 续跑;< head → 回溯
  405. after_seq = config.after_sequence
  406. # 如果 after_seq > head_sequence,说明 generator 被强制关闭时 store 的
  407. # head_sequence 未来得及更新(仍停在 Phase 2 写入的初始值)。
  408. # 用 last_sequence 修正 head_sequence,确保续跑时能看到完整历史。
  409. if after_seq is not None and after_seq > trace_obj.head_sequence:
  410. trace_obj.head_sequence = trace_obj.last_sequence
  411. await self.trace_store.update_trace(
  412. config.trace_id, head_sequence=trace_obj.head_sequence
  413. )
  414. if after_seq is not None and after_seq < trace_obj.head_sequence:
  415. # 回溯模式
  416. sequence = await self._rewind(config.trace_id, after_seq, goal_tree)
  417. else:
  418. # 续跑模式:从 last_sequence + 1 开始
  419. sequence = trace_obj.last_sequence + 1
  420. # 状态置为 running
  421. await self.trace_store.update_trace(
  422. config.trace_id,
  423. status="running",
  424. completed_at=None,
  425. )
  426. trace_obj.status = "running"
  427. return trace_obj, goal_tree, sequence
  428. # ===== Phase 2: BUILD HISTORY =====
  429. async def _build_history(
  430. self,
  431. trace_id: str,
  432. new_messages: List[Dict],
  433. goal_tree: Optional[GoalTree],
  434. config: RunConfig,
  435. sequence: int,
  436. ) -> Tuple[List[Dict], int, List[Message]]:
  437. """
  438. 构建完整的 LLM 消息历史
  439. 1. 从 head_sequence 沿 parent chain 加载主路径消息(续跑/回溯场景)
  440. 2. 构建 system prompt(新建时注入 skills)
  441. 3. 新建时:在第一条 user message 末尾注入当前经验
  442. 4. 追加 input messages(设置 parent_sequence 链接到当前 head)
  443. Returns:
  444. (history, next_sequence, created_messages, head_sequence)
  445. created_messages: 本次新创建并持久化的 Message 列表,供 run() yield 给调用方
  446. head_sequence: 当前主路径头节点的 sequence
  447. """
  448. history: List[Dict] = []
  449. created_messages: List[Message] = []
  450. head_seq: Optional[int] = None # 当前主路径的头节点 sequence
  451. # 1. 加载已有 messages(通过主路径遍历)
  452. if config.trace_id and self.trace_store:
  453. trace_obj = await self.trace_store.get_trace(trace_id)
  454. if trace_obj and trace_obj.head_sequence > 0:
  455. main_path = await self.trace_store.get_main_path_messages(
  456. trace_id, trace_obj.head_sequence
  457. )
  458. # 修复 orphaned tool_calls(中断导致的 tool_call 无 tool_result)
  459. main_path, sequence = await self._heal_orphaned_tool_calls(
  460. main_path, trace_id, goal_tree, sequence,
  461. )
  462. history = [msg.to_llm_dict() for msg in main_path]
  463. if main_path:
  464. head_seq = main_path[-1].sequence
  465. # 2. 构建 system prompt(如果历史中没有 system message)
  466. has_system = any(m.get("role") == "system" for m in history)
  467. has_system_in_new = any(m.get("role") == "system" for m in new_messages)
  468. if not has_system and not has_system_in_new:
  469. system_prompt = await self._build_system_prompt(config)
  470. if system_prompt:
  471. history = [{"role": "system", "content": system_prompt}] + history
  472. if self.trace_store:
  473. system_msg = Message.create(
  474. trace_id=trace_id, role="system", sequence=sequence,
  475. goal_id=None, content=system_prompt,
  476. parent_sequence=None, # system message 是 root
  477. )
  478. await self.trace_store.add_message(system_msg)
  479. created_messages.append(system_msg)
  480. head_seq = sequence
  481. sequence += 1
  482. # 3. 新建时:在第一条 user message 末尾注入当前经验
  483. if not config.trace_id: # 新建模式
  484. experiences_text = self._load_experiences()
  485. if experiences_text:
  486. for msg in new_messages:
  487. if msg.get("role") == "user" and isinstance(msg.get("content"), str):
  488. msg["content"] += f"\n\n## 参考经验\n\n{experiences_text}"
  489. break
  490. # 4. 追加新 messages(设置 parent_sequence 链接到当前 head)
  491. for msg_dict in new_messages:
  492. history.append(msg_dict)
  493. if self.trace_store:
  494. stored_msg = Message.from_llm_dict(
  495. msg_dict, trace_id=trace_id, sequence=sequence,
  496. goal_id=None, parent_sequence=head_seq,
  497. )
  498. await self.trace_store.add_message(stored_msg)
  499. created_messages.append(stored_msg)
  500. head_seq = sequence
  501. sequence += 1
  502. # 5. 更新 trace 的 head_sequence
  503. if self.trace_store and head_seq is not None:
  504. await self.trace_store.update_trace(trace_id, head_sequence=head_seq)
  505. return history, sequence, created_messages, head_seq or 0
  506. # ===== Phase 3: AGENT LOOP =====
  507. async def _agent_loop(
  508. self,
  509. trace: Trace,
  510. history: List[Dict],
  511. goal_tree: Optional[GoalTree],
  512. config: RunConfig,
  513. sequence: int,
  514. ) -> AsyncIterator[Union[Trace, Message]]:
  515. """ReAct 循环"""
  516. trace_id = trace.trace_id
  517. tool_schemas = self._get_tool_schemas(config.tools)
  518. # 当前主路径头节点的 sequence(用于设置 parent_sequence)
  519. head_seq = trace.head_sequence
  520. # 设置 goal_tree 到 goal 工具
  521. if goal_tree and self.trace_store:
  522. from agent.trace.goal_tool import set_goal_tree
  523. set_goal_tree(goal_tree)
  524. for iteration in range(config.max_iterations):
  525. # 检查取消信号
  526. cancel_event = self._cancel_events.get(trace_id)
  527. if cancel_event and cancel_event.is_set():
  528. logger.info(f"Trace {trace_id} stopped by user")
  529. if self.trace_store:
  530. await self.trace_store.update_trace(
  531. trace_id,
  532. status="stopped",
  533. head_sequence=head_seq,
  534. completed_at=datetime.now(),
  535. )
  536. trace_obj = await self.trace_store.get_trace(trace_id)
  537. if trace_obj:
  538. yield trace_obj
  539. return
  540. # Level 1 压缩:GoalTree 过滤(当消息超过阈值时触发)
  541. compression_config = CompressionConfig()
  542. token_count = estimate_tokens(history)
  543. max_tokens = compression_config.get_max_tokens(config.model)
  544. if token_count > max_tokens and self.trace_store and goal_tree:
  545. # 使用本地 head_seq(store 中的 head_sequence 在 loop 期间未更新,是过时的)
  546. if head_seq > 0:
  547. main_path_msgs = await self.trace_store.get_main_path_messages(
  548. trace_id, head_seq
  549. )
  550. filtered_msgs = filter_by_goal_status(main_path_msgs, goal_tree)
  551. if len(filtered_msgs) < len(main_path_msgs):
  552. logger.info(
  553. "Level 1 压缩: %d -> %d 条消息 (tokens ~%d, 阈值 %d)",
  554. len(main_path_msgs), len(filtered_msgs), token_count, max_tokens,
  555. )
  556. history = [msg.to_llm_dict() for msg in filtered_msgs]
  557. else:
  558. logger.info(
  559. "Level 1 压缩: 无可过滤消息 (%d 条全部保留, completed/abandoned goals=%d)",
  560. len(main_path_msgs),
  561. sum(1 for g in goal_tree.goals
  562. if g.status in ("completed", "abandoned")),
  563. )
  564. elif token_count > max_tokens:
  565. logger.warning(
  566. "消息 token 数 (%d) 超过阈值 (%d),但无法执行 Level 1 压缩(缺少 store 或 goal_tree)",
  567. token_count, max_tokens,
  568. )
  569. # Level 2 压缩:LLM 总结(Level 1 后仍超阈值时触发)
  570. token_count_after = estimate_tokens(history)
  571. if token_count_after > max_tokens:
  572. logger.info(
  573. "Level 1 后 token 仍超阈值 (%d > %d),触发 Level 2 压缩",
  574. token_count_after, max_tokens,
  575. )
  576. history, head_seq, sequence = await self._compress_history(
  577. trace_id, history, goal_tree, config, sequence, head_seq,
  578. )
  579. # 构建 LLM messages(注入上下文)
  580. llm_messages = list(history)
  581. # 周期性注入 GoalTree + Collaborators
  582. if iteration % CONTEXT_INJECTION_INTERVAL == 0:
  583. context_injection = self._build_context_injection(trace, goal_tree)
  584. if context_injection:
  585. llm_messages.append({"role": "system", "content": context_injection})
  586. # 应用 Prompt Caching(不修改原始 history,只在发送给 LLM 时添加缓存标记)
  587. llm_messages = self._add_cache_control(
  588. llm_messages,
  589. config.model,
  590. config.enable_prompt_caching
  591. )
  592. # 调用 LLM
  593. result = await self.llm_call(
  594. messages=llm_messages,
  595. model=config.model,
  596. tools=tool_schemas,
  597. temperature=config.temperature,
  598. **config.extra_llm_params,
  599. )
  600. response_content = result.get("content", "")
  601. tool_calls = result.get("tool_calls")
  602. finish_reason = result.get("finish_reason")
  603. prompt_tokens = result.get("prompt_tokens", 0)
  604. completion_tokens = result.get("completion_tokens", 0)
  605. step_cost = result.get("cost", 0)
  606. cache_creation_tokens = result.get("cache_creation_tokens")
  607. cache_read_tokens = result.get("cache_read_tokens")
  608. # 按需自动创建 root goal
  609. if goal_tree and not goal_tree.goals and tool_calls:
  610. has_goal_call = any(
  611. tc.get("function", {}).get("name") == "goal"
  612. for tc in tool_calls
  613. )
  614. if not has_goal_call:
  615. mission = goal_tree.mission
  616. root_desc = mission[:200] if len(mission) > 200 else mission
  617. goal_tree.add_goals(
  618. descriptions=[root_desc],
  619. reasons=["系统自动创建:Agent 未显式创建目标"],
  620. parent_id=None
  621. )
  622. goal_tree.focus(goal_tree.goals[0].id)
  623. if self.trace_store:
  624. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  625. await self.trace_store.add_goal(trace_id, goal_tree.goals[0])
  626. logger.info(f"自动创建 root goal: {goal_tree.goals[0].id}")
  627. # 获取当前 goal_id
  628. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  629. # 记录 assistant Message(parent_sequence 指向当前 head)
  630. assistant_msg = Message.create(
  631. trace_id=trace_id,
  632. role="assistant",
  633. sequence=sequence,
  634. goal_id=current_goal_id,
  635. parent_sequence=head_seq if head_seq > 0 else None,
  636. content={"text": response_content, "tool_calls": tool_calls},
  637. prompt_tokens=prompt_tokens,
  638. completion_tokens=completion_tokens,
  639. cache_creation_tokens=cache_creation_tokens,
  640. cache_read_tokens=cache_read_tokens,
  641. finish_reason=finish_reason,
  642. cost=step_cost,
  643. )
  644. if self.trace_store:
  645. await self.trace_store.add_message(assistant_msg)
  646. yield assistant_msg
  647. head_seq = sequence
  648. sequence += 1
  649. # 处理工具调用
  650. # 截断兜底:finish_reason == "length" 说明响应被 max_tokens 截断,
  651. # tool call 参数很可能不完整,不应执行,改为提示模型分批操作
  652. if tool_calls and finish_reason == "length":
  653. logger.warning(
  654. "[Runner] 响应被 max_tokens 截断,跳过 %d 个不完整的 tool calls",
  655. len(tool_calls),
  656. )
  657. truncation_hint = (
  658. "你的响应因为 max_tokens 限制被截断,tool call 参数不完整,未执行。"
  659. "请将大内容拆分为多次小的工具调用(例如用 write_file 的 append 模式分批写入)。"
  660. )
  661. history.append({
  662. "role": "assistant",
  663. "content": response_content,
  664. "tool_calls": tool_calls,
  665. })
  666. # 为每个被截断的 tool call 返回错误结果
  667. for tc in tool_calls:
  668. history.append({
  669. "role": "tool",
  670. "tool_call_id": tc["id"],
  671. "content": truncation_hint,
  672. })
  673. continue
  674. if tool_calls and config.auto_execute_tools:
  675. history.append({
  676. "role": "assistant",
  677. "content": response_content,
  678. "tool_calls": tool_calls,
  679. })
  680. for tc in tool_calls:
  681. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  682. tool_name = tc["function"]["name"]
  683. tool_args = tc["function"]["arguments"]
  684. if isinstance(tool_args, str):
  685. tool_args = json.loads(tool_args) if tool_args.strip() else {}
  686. elif tool_args is None:
  687. tool_args = {}
  688. tool_result = await self.tools.execute(
  689. tool_name,
  690. tool_args,
  691. uid=config.uid or "",
  692. context={
  693. "store": self.trace_store,
  694. "trace_id": trace_id,
  695. "goal_id": current_goal_id,
  696. "runner": self,
  697. }
  698. )
  699. # --- 支持多模态工具反馈 ---
  700. # execute() 返回 dict{"text","images"} 或 str
  701. if isinstance(tool_result, dict) and tool_result.get("images"):
  702. tool_result_text = tool_result["text"]
  703. # 构建多模态消息格式
  704. tool_content_for_llm = [{"type": "text", "text": tool_result_text}]
  705. for img in tool_result["images"]:
  706. if img.get("type") == "base64" and img.get("data"):
  707. media_type = img.get("media_type", "image/png")
  708. tool_content_for_llm.append({
  709. "type": "image_url",
  710. "image_url": {
  711. "url": f"data:{media_type};base64,{img['data']}"
  712. }
  713. })
  714. img_count = len(tool_content_for_llm) - 1 # 减去 text 块
  715. print(f"[Runner] 多模态工具反馈: tool={tool_name}, images={img_count}, text_len={len(tool_result_text)}")
  716. else:
  717. tool_result_text = str(tool_result)
  718. tool_content_for_llm = tool_result_text
  719. tool_msg = Message.create(
  720. trace_id=trace_id,
  721. role="tool",
  722. sequence=sequence,
  723. goal_id=current_goal_id,
  724. parent_sequence=head_seq,
  725. tool_call_id=tc["id"],
  726. content={"tool_name": tool_name, "result": tool_result_text},
  727. )
  728. if self.trace_store:
  729. await self.trace_store.add_message(tool_msg)
  730. # 截图单独存为同名 PNG 文件
  731. if isinstance(tool_result, dict) and tool_result.get("images"):
  732. import base64 as b64mod
  733. for img in tool_result["images"]:
  734. if img.get("data"):
  735. png_path = self.trace_store._get_messages_dir(trace_id) / f"{tool_msg.message_id}.png"
  736. png_path.write_bytes(b64mod.b64decode(img["data"]))
  737. print(f"[Runner] 截图已保存: {png_path.name}")
  738. break # 只存第一张
  739. yield tool_msg
  740. head_seq = sequence
  741. sequence += 1
  742. history.append({
  743. "role": "tool",
  744. "tool_call_id": tc["id"],
  745. "name": tool_name,
  746. "content": tool_content_for_llm, # 这里传入 list 即可触发模型的视觉能力
  747. })
  748. # ------------------------------------------
  749. continue # 继续循环
  750. # 无工具调用,任务完成
  751. break
  752. # 更新 head_sequence 并完成 Trace
  753. if self.trace_store:
  754. await self.trace_store.update_trace(
  755. trace_id,
  756. status="completed",
  757. head_sequence=head_seq,
  758. completed_at=datetime.now(),
  759. )
  760. trace_obj = await self.trace_store.get_trace(trace_id)
  761. if trace_obj:
  762. yield trace_obj
  763. # ===== Level 2: LLM 压缩 =====
  764. async def _compress_history(
  765. self,
  766. trace_id: str,
  767. history: List[Dict],
  768. goal_tree: Optional[GoalTree],
  769. config: RunConfig,
  770. sequence: int,
  771. head_seq: int,
  772. ) -> Tuple[List[Dict], int, int]:
  773. """
  774. Level 2 压缩:LLM 总结
  775. Step 1: 经验提取(reflect)— 纯内存 LLM 调用 + 文件追加,不影响 trace
  776. Step 2: 压缩总结 — LLM 生成 summary
  777. Step 3: 存储 summary 为新消息,parent_sequence 跳到 system msg
  778. Step 4: 重建 history
  779. Returns:
  780. (new_history, new_head_seq, next_sequence)
  781. """
  782. logger.info("Level 2 压缩开始: trace=%s, 当前 history 长度=%d", trace_id, len(history))
  783. # 找到 system message 的 sequence(主路径第一条消息)
  784. system_msg_seq = None
  785. system_msg_dict = None
  786. if self.trace_store:
  787. trace_obj = await self.trace_store.get_trace(trace_id)
  788. if trace_obj and trace_obj.head_sequence > 0:
  789. main_path = await self.trace_store.get_main_path_messages(
  790. trace_id, trace_obj.head_sequence
  791. )
  792. for msg in main_path:
  793. if msg.role == "system":
  794. system_msg_seq = msg.sequence
  795. system_msg_dict = msg.to_llm_dict()
  796. break
  797. # Fallback: 从 history 中找 system message
  798. if system_msg_dict is None:
  799. for msg_dict in history:
  800. if msg_dict.get("role") == "system":
  801. system_msg_dict = msg_dict
  802. break
  803. if system_msg_dict is None:
  804. logger.warning("Level 2 压缩跳过:未找到 system message")
  805. return history, head_seq, sequence
  806. # --- Step 1: 经验提取(reflect)---
  807. try:
  808. reflect_prompt = build_reflect_prompt()
  809. reflect_messages = list(history) + [{"role": "user", "content": reflect_prompt}]
  810. # 应用 Prompt Caching
  811. reflect_messages = self._add_cache_control(
  812. reflect_messages,
  813. config.model,
  814. config.enable_prompt_caching
  815. )
  816. reflect_result = await self.llm_call(
  817. messages=reflect_messages,
  818. model=config.model,
  819. tools=[],
  820. temperature=config.temperature,
  821. **config.extra_llm_params,
  822. )
  823. reflect_content = reflect_result.get("content", "").strip()
  824. if reflect_content and self.experiences_path:
  825. try:
  826. os.makedirs(os.path.dirname(self.experiences_path), exist_ok=True)
  827. with open(self.experiences_path, "a", encoding="utf-8") as f:
  828. f.write(f"\n\n---\n\n{reflect_content}")
  829. logger.info("经验已追加到 %s", self.experiences_path)
  830. except Exception as e:
  831. logger.warning("写入经验文件失败: %s", e)
  832. except Exception as e:
  833. logger.warning("Level 2 经验提取失败(不影响压缩): %s", e)
  834. # --- Step 2: 压缩总结 ---
  835. compress_prompt = build_compression_prompt(goal_tree)
  836. compress_messages = list(history) + [{"role": "user", "content": compress_prompt}]
  837. # 应用 Prompt Caching
  838. compress_messages = self._add_cache_control(
  839. compress_messages,
  840. config.model,
  841. config.enable_prompt_caching
  842. )
  843. compress_result = await self.llm_call(
  844. messages=compress_messages,
  845. model=config.model,
  846. tools=[],
  847. temperature=config.temperature,
  848. **config.extra_llm_params,
  849. )
  850. summary_text = compress_result.get("content", "").strip()
  851. if not summary_text:
  852. logger.warning("Level 2 压缩跳过:LLM 未返回 summary")
  853. return history, head_seq, sequence
  854. # --- Step 3: 存储 summary 消息 ---
  855. summary_with_header = (
  856. f"## 对话历史摘要(自动压缩)\n\n{summary_text}\n\n"
  857. "---\n请基于以上摘要和当前 GoalTree 继续执行任务。"
  858. )
  859. summary_msg = Message.create(
  860. trace_id=trace_id,
  861. role="user",
  862. sequence=sequence,
  863. goal_id=None,
  864. parent_sequence=system_msg_seq, # 跳到 system msg,跳过所有中间消息
  865. content=summary_with_header,
  866. )
  867. if self.trace_store:
  868. await self.trace_store.add_message(summary_msg)
  869. new_head_seq = sequence
  870. sequence += 1
  871. # --- Step 4: 重建 history ---
  872. new_history = [system_msg_dict, summary_msg.to_llm_dict()]
  873. # 更新 trace head_sequence
  874. if self.trace_store:
  875. await self.trace_store.update_trace(
  876. trace_id,
  877. head_sequence=new_head_seq,
  878. )
  879. logger.info(
  880. "Level 2 压缩完成: 旧 history %d 条 → 新 history %d 条, summary 长度=%d",
  881. len(history), len(new_history), len(summary_text),
  882. )
  883. return new_history, new_head_seq, sequence
  884. # ===== 回溯(Rewind)=====
  885. async def _rewind(
  886. self,
  887. trace_id: str,
  888. after_sequence: int,
  889. goal_tree: Optional[GoalTree],
  890. ) -> int:
  891. """
  892. 执行回溯:快照 GoalTree,重建干净树,设置 head_sequence
  893. 新消息的 parent_sequence 将指向 rewind 点,旧消息通过树结构自然脱离主路径。
  894. Returns:
  895. 下一个可用的 sequence 号
  896. """
  897. if not self.trace_store:
  898. raise ValueError("trace_store required for rewind")
  899. # 1. 加载所有 messages(用于 safe cutoff 和 max sequence)
  900. all_messages = await self.trace_store.get_trace_messages(trace_id)
  901. if not all_messages:
  902. return 1
  903. # 2. 找到安全截断点(确保不截断在 tool_call 和 tool response 之间)
  904. cutoff = self._find_safe_cutoff(all_messages, after_sequence)
  905. # 3. 快照并重建 GoalTree
  906. if goal_tree:
  907. # 获取截断点消息的 created_at 作为时间界限
  908. cutoff_msg = None
  909. for msg in all_messages:
  910. if msg.sequence == cutoff:
  911. cutoff_msg = msg
  912. break
  913. cutoff_time = cutoff_msg.created_at if cutoff_msg else datetime.now()
  914. # 快照到 events(含 head_sequence 供前端感知分支切换)
  915. await self.trace_store.append_event(trace_id, "rewind", {
  916. "after_sequence": cutoff,
  917. "head_sequence": cutoff,
  918. "goal_tree_snapshot": goal_tree.to_dict(),
  919. })
  920. # 按时间重建干净的 GoalTree
  921. new_tree = goal_tree.rebuild_for_rewind(cutoff_time)
  922. await self.trace_store.update_goal_tree(trace_id, new_tree)
  923. # 更新内存中的引用
  924. goal_tree.goals = new_tree.goals
  925. goal_tree.current_id = new_tree.current_id
  926. # 4. 更新 head_sequence 到 rewind 点
  927. await self.trace_store.update_trace(trace_id, head_sequence=cutoff)
  928. # 5. 返回 next sequence(全局递增,不复用)
  929. max_seq = max((m.sequence for m in all_messages), default=0)
  930. return max_seq + 1
  931. def _find_safe_cutoff(self, messages: List[Message], after_sequence: int) -> int:
  932. """
  933. 找到安全的截断点。
  934. 如果 after_sequence 指向一条带 tool_calls 的 assistant message,
  935. 则自动扩展到其所有对应的 tool response 之后。
  936. """
  937. cutoff = after_sequence
  938. # 找到 after_sequence 对应的 message
  939. target_msg = None
  940. for msg in messages:
  941. if msg.sequence == after_sequence:
  942. target_msg = msg
  943. break
  944. if not target_msg:
  945. return cutoff
  946. # 如果是 assistant 且有 tool_calls,找到所有对应的 tool responses
  947. if target_msg.role == "assistant":
  948. content = target_msg.content
  949. if isinstance(content, dict) and content.get("tool_calls"):
  950. tool_call_ids = set()
  951. for tc in content["tool_calls"]:
  952. if isinstance(tc, dict) and tc.get("id"):
  953. tool_call_ids.add(tc["id"])
  954. # 找到这些 tool_call 对应的 tool messages
  955. for msg in messages:
  956. if (msg.role == "tool" and msg.tool_call_id
  957. and msg.tool_call_id in tool_call_ids):
  958. cutoff = max(cutoff, msg.sequence)
  959. return cutoff
  960. async def _heal_orphaned_tool_calls(
  961. self,
  962. messages: List[Message],
  963. trace_id: str,
  964. goal_tree: Optional[GoalTree],
  965. sequence: int,
  966. ) -> tuple:
  967. """
  968. 检测并修复消息历史中的 orphaned tool_calls。
  969. 当 agent 被 stop/crash 中断时,可能有 assistant 的 tool_calls 没有对应的
  970. tool results(包括多 tool_call 部分完成的情况)。直接发给 LLM 会导致 400。
  971. 修复策略:为每个缺失的 tool_result 插入合成的"中断通知"消息,而非裁剪。
  972. - 普通工具:简短中断提示
  973. - agent/evaluate:包含 sub_trace_id、执行统计、continue_from 指引
  974. 合成消息持久化到 store,确保幂等(下次续跑不再触发)。
  975. Returns:
  976. (healed_messages, next_sequence)
  977. """
  978. if not messages:
  979. return messages, sequence
  980. # 收集所有 tool_call IDs → (assistant_msg, tool_call_dict)
  981. tc_map: Dict[str, tuple] = {}
  982. result_ids: set = set()
  983. for msg in messages:
  984. if msg.role == "assistant":
  985. content = msg.content
  986. if isinstance(content, dict) and content.get("tool_calls"):
  987. for tc in content["tool_calls"]:
  988. tc_id = tc.get("id")
  989. if tc_id:
  990. tc_map[tc_id] = (msg, tc)
  991. elif msg.role == "tool" and msg.tool_call_id:
  992. result_ids.add(msg.tool_call_id)
  993. orphaned_ids = [tc_id for tc_id in tc_map if tc_id not in result_ids]
  994. if not orphaned_ids:
  995. return messages, sequence
  996. logger.info(
  997. "检测到 %d 个 orphaned tool_calls,生成合成中断通知",
  998. len(orphaned_ids),
  999. )
  1000. healed = list(messages)
  1001. head_seq = messages[-1].sequence
  1002. for tc_id in orphaned_ids:
  1003. assistant_msg, tc = tc_map[tc_id]
  1004. tool_name = tc.get("function", {}).get("name", "unknown")
  1005. if tool_name in ("agent", "evaluate"):
  1006. result_text = self._build_agent_interrupted_result(
  1007. tc, goal_tree, assistant_msg,
  1008. )
  1009. else:
  1010. result_text = (
  1011. f"⚠️ 工具 {tool_name} 执行被中断(进程异常退出),"
  1012. "未获得执行结果。请根据需要重新调用。"
  1013. )
  1014. synthetic_msg = Message.create(
  1015. trace_id=trace_id,
  1016. role="tool",
  1017. sequence=sequence,
  1018. goal_id=assistant_msg.goal_id,
  1019. parent_sequence=head_seq,
  1020. tool_call_id=tc_id,
  1021. content={"tool_name": tool_name, "result": result_text},
  1022. )
  1023. if self.trace_store:
  1024. await self.trace_store.add_message(synthetic_msg)
  1025. healed.append(synthetic_msg)
  1026. head_seq = sequence
  1027. sequence += 1
  1028. # 更新 trace head/last sequence
  1029. if self.trace_store:
  1030. await self.trace_store.update_trace(
  1031. trace_id,
  1032. head_sequence=head_seq,
  1033. last_sequence=max(head_seq, sequence - 1),
  1034. )
  1035. return healed, sequence
  1036. def _build_agent_interrupted_result(
  1037. self,
  1038. tc: Dict,
  1039. goal_tree: Optional[GoalTree],
  1040. assistant_msg: Message,
  1041. ) -> str:
  1042. """为中断的 agent/evaluate 工具调用构建合成结果(对齐正常返回值格式)"""
  1043. args_str = tc.get("function", {}).get("arguments", "{}")
  1044. try:
  1045. args = json.loads(args_str) if isinstance(args_str, str) else args_str
  1046. except json.JSONDecodeError:
  1047. args = {}
  1048. task = args.get("task", "未知任务")
  1049. if isinstance(task, list):
  1050. task = "; ".join(task)
  1051. tool_name = tc.get("function", {}).get("name", "agent")
  1052. mode = "evaluate" if tool_name == "evaluate" else "delegate"
  1053. # 从 goal_tree 查找 sub_trace 信息
  1054. sub_trace_id = None
  1055. stats = None
  1056. if goal_tree and assistant_msg.goal_id:
  1057. goal = goal_tree.find(assistant_msg.goal_id)
  1058. if goal and goal.sub_trace_ids:
  1059. first = goal.sub_trace_ids[0]
  1060. if isinstance(first, dict):
  1061. sub_trace_id = first.get("trace_id")
  1062. elif isinstance(first, str):
  1063. sub_trace_id = first
  1064. if goal.cumulative_stats:
  1065. s = goal.cumulative_stats
  1066. if s.message_count > 0:
  1067. stats = {
  1068. "message_count": s.message_count,
  1069. "total_tokens": s.total_tokens,
  1070. "total_cost": round(s.total_cost, 4),
  1071. }
  1072. result: Dict[str, Any] = {
  1073. "mode": mode,
  1074. "status": "interrupted",
  1075. "summary": "⚠️ 子Agent执行被中断(进程异常退出)",
  1076. "task": task,
  1077. }
  1078. if sub_trace_id:
  1079. result["sub_trace_id"] = sub_trace_id
  1080. result["hint"] = (
  1081. f'使用 continue_from="{sub_trace_id}" 可继续执行,保留已有进度'
  1082. )
  1083. if stats:
  1084. result["stats"] = stats
  1085. return json.dumps(result, ensure_ascii=False, indent=2)
  1086. # ===== 上下文注入 =====
  1087. def _build_context_injection(
  1088. self,
  1089. trace: Trace,
  1090. goal_tree: Optional[GoalTree],
  1091. ) -> str:
  1092. """构建周期性注入的上下文(GoalTree + Active Collaborators + Focus 提醒)"""
  1093. parts = []
  1094. # GoalTree
  1095. if goal_tree and goal_tree.goals:
  1096. parts.append(f"## Current Plan\n\n{goal_tree.to_prompt()}")
  1097. # 检测 focus 在有子节点的父目标上:提醒模型 focus 到具体子目标
  1098. if goal_tree.current_id:
  1099. children = goal_tree.get_children(goal_tree.current_id)
  1100. pending_children = [c for c in children if c.status in ("pending", "in_progress")]
  1101. if pending_children:
  1102. child_ids = ", ".join(
  1103. goal_tree._generate_display_id(c) for c in pending_children[:3]
  1104. )
  1105. parts.append(
  1106. f"**提醒**:当前焦点在父目标上,建议用 `goal(focus=\"...\")` "
  1107. f"切换到具体子目标(如 {child_ids})再执行。"
  1108. )
  1109. # Active Collaborators
  1110. collaborators = trace.context.get("collaborators", [])
  1111. if collaborators:
  1112. lines = ["## Active Collaborators"]
  1113. for c in collaborators:
  1114. status_str = c.get("status", "unknown")
  1115. ctype = c.get("type", "agent")
  1116. summary = c.get("summary", "")
  1117. name = c.get("name", "unnamed")
  1118. lines.append(f"- {name} [{ctype}, {status_str}]: {summary}")
  1119. parts.append("\n".join(lines))
  1120. return "\n\n".join(parts)
  1121. # ===== 辅助方法 =====
  1122. def _add_cache_control(
  1123. self,
  1124. messages: List[Dict],
  1125. model: str,
  1126. enable: bool
  1127. ) -> List[Dict]:
  1128. """
  1129. 为支持的模型添加 Prompt Caching 标记
  1130. 策略:
  1131. 1. system message 添加缓存(如果存在且足够长)
  1132. 2. 倒数第 3-5 条 user/assistant 消息添加缓存点
  1133. Args:
  1134. messages: 原始消息列表
  1135. model: 模型名称
  1136. enable: 是否启用缓存
  1137. Returns:
  1138. 添加了 cache_control 的消息列表(深拷贝)
  1139. """
  1140. if not enable:
  1141. return messages
  1142. # 只对 Claude 模型启用
  1143. if "claude" not in model.lower():
  1144. return messages
  1145. # 深拷贝避免修改原始数据
  1146. import copy
  1147. messages = copy.deepcopy(messages)
  1148. # 策略 1: 为 system message 添加缓存
  1149. for msg in messages:
  1150. if msg.get("role") == "system":
  1151. content = msg.get("content", "")
  1152. # 只有足够长的 system prompt 才值得缓存(>1024 tokens 约 4000 字符)
  1153. if isinstance(content, str) and len(content) > 1000:
  1154. # Anthropic API 格式:在 content 的最后一个 block 添加 cache_control
  1155. # 如果 content 是 string,需要转换为 list 格式
  1156. msg["content"] = [
  1157. {
  1158. "type": "text",
  1159. "text": content,
  1160. "cache_control": {"type": "ephemeral"}
  1161. }
  1162. ]
  1163. logger.debug(f"[Cache] 为 system message 添加缓存标记 (len={len(content)})")
  1164. break
  1165. # 策略 2: 为倒数第 3-5 条消息添加缓存点
  1166. # 这样可以缓存大部分历史对话,只有最新的几条消息是新的
  1167. cache_positions = []
  1168. user_assistant_msgs = [
  1169. (i, msg) for i, msg in enumerate(messages)
  1170. if msg.get("role") in ("user", "assistant")
  1171. ]
  1172. if len(user_assistant_msgs) >= 5:
  1173. # 在倒数第 5 条添加缓存点
  1174. cache_positions.append(user_assistant_msgs[-5][0])
  1175. elif len(user_assistant_msgs) >= 3:
  1176. # 在倒数第 3 条添加缓存点
  1177. cache_positions.append(user_assistant_msgs[-3][0])
  1178. for idx in cache_positions:
  1179. msg = messages[idx]
  1180. content = msg.get("content", "")
  1181. # 处理 string content
  1182. if isinstance(content, str):
  1183. msg["content"] = [
  1184. {
  1185. "type": "text",
  1186. "text": content,
  1187. "cache_control": {"type": "ephemeral"}
  1188. }
  1189. ]
  1190. logger.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  1191. # 处理 list content(多模态消息)
  1192. elif isinstance(content, list) and len(content) > 0:
  1193. # 在最后一个 text block 添加 cache_control
  1194. for i in range(len(content) - 1, -1, -1):
  1195. if isinstance(content[i], dict) and content[i].get("type") == "text":
  1196. content[i]["cache_control"] = {"type": "ephemeral"}
  1197. logger.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 的 content[{i}] 添加缓存标记")
  1198. break
  1199. return messages
  1200. def _get_tool_schemas(self, tools: Optional[List[str]]) -> List[Dict]:
  1201. """
  1202. 获取工具 Schema
  1203. - tools=None: 使用 registry 中全部已注册工具(含内置 + 外部注册的)
  1204. - tools=["a", "b"]: 在 BUILTIN_TOOLS 基础上追加指定工具
  1205. """
  1206. if tools is None:
  1207. # 全部已注册工具
  1208. tool_names = self.tools.get_tool_names()
  1209. else:
  1210. # BUILTIN_TOOLS + 显式指定的额外工具
  1211. tool_names = BUILTIN_TOOLS.copy()
  1212. for t in tools:
  1213. if t not in tool_names:
  1214. tool_names.append(t)
  1215. return self.tools.get_schemas(tool_names)
  1216. # 默认 system prompt 前缀(当 config.system_prompt 和前端都未提供 system message 时使用)
  1217. DEFAULT_SYSTEM_PREFIX = "你是最顶尖的AI助手,可以拆分并调用工具逐步解决复杂问题。"
  1218. async def _build_system_prompt(self, config: RunConfig) -> Optional[str]:
  1219. """构建 system prompt(注入 skills)
  1220. 优先级:
  1221. 1. config.skills 显式指定 → 按名称过滤
  1222. 2. config.skills 为 None → 查 preset 的默认 skills 列表
  1223. 3. preset 也无 skills(None)→ 加载全部(向后兼容)
  1224. """
  1225. from agent.core.presets import AGENT_PRESETS
  1226. system_prompt = config.system_prompt
  1227. # 确定要加载哪些 skills
  1228. skills_filter: Optional[List[str]] = config.skills
  1229. if skills_filter is None:
  1230. preset = AGENT_PRESETS.get(config.agent_type)
  1231. if preset is not None:
  1232. skills_filter = preset.skills # 可能仍为 None(加载全部)
  1233. # 加载并过滤
  1234. all_skills = load_skills_from_dir(self.skills_dir)
  1235. if skills_filter is not None:
  1236. skills = [s for s in all_skills if s.name in skills_filter]
  1237. else:
  1238. skills = all_skills
  1239. skills_text = self._format_skills(skills) if skills else ""
  1240. if system_prompt:
  1241. if skills_text:
  1242. system_prompt += f"\n\n## Skills\n{skills_text}"
  1243. else:
  1244. system_prompt = self.DEFAULT_SYSTEM_PREFIX
  1245. if skills_text:
  1246. system_prompt += f"\n\n## Skills\n{skills_text}"
  1247. return system_prompt
  1248. async def _generate_task_name(self, messages: List[Dict]) -> str:
  1249. """生成任务名称:优先使用 utility_llm,fallback 到文本截取"""
  1250. # 提取 messages 中的文本内容
  1251. text_parts = []
  1252. for msg in messages:
  1253. content = msg.get("content", "")
  1254. if isinstance(content, str):
  1255. text_parts.append(content)
  1256. elif isinstance(content, list):
  1257. for part in content:
  1258. if isinstance(part, dict) and part.get("type") == "text":
  1259. text_parts.append(part.get("text", ""))
  1260. raw_text = " ".join(text_parts).strip()
  1261. if not raw_text:
  1262. return "未命名任务"
  1263. # 尝试使用 utility_llm 生成标题
  1264. if self.utility_llm_call:
  1265. try:
  1266. result = await self.utility_llm_call(
  1267. messages=[
  1268. {"role": "system", "content": "用中文为以下任务生成一个简短标题(10-30字),只输出标题本身:"},
  1269. {"role": "user", "content": raw_text[:2000]},
  1270. ],
  1271. model="gpt-4o-mini", # 使用便宜模型
  1272. )
  1273. title = result.get("content", "").strip()
  1274. if title and len(title) < 100:
  1275. return title
  1276. except Exception:
  1277. pass
  1278. # Fallback: 截取前 50 字符
  1279. return raw_text[:50] + ("..." if len(raw_text) > 50 else "")
  1280. def _format_skills(self, skills: List[Skill]) -> str:
  1281. if not skills:
  1282. return ""
  1283. return "\n\n".join(s.to_prompt_text() for s in skills)
  1284. def _load_experiences(self) -> str:
  1285. """从文件加载经验(./.cache/experiences.md)"""
  1286. if not self.experiences_path:
  1287. return ""
  1288. try:
  1289. if os.path.exists(self.experiences_path):
  1290. with open(self.experiences_path, "r", encoding="utf-8") as f:
  1291. return f.read().strip()
  1292. except Exception as e:
  1293. logger.warning(f"Failed to load experiences from {self.experiences_path}: {e}")
  1294. return ""