runner.py 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. """
  2. Agent Runner - Agent 执行引擎
  3. 核心职责:
  4. 1. 执行 Agent 任务(循环调用 LLM + 工具)
  5. 2. 记录执行轨迹(Trace + Messages + GoalTree)
  6. 3. 加载和注入技能(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.skill_loader import load_skills_from_dir
  35. from agent.tools import ToolRegistry, get_tool_registry
  36. from agent.core.prompts import (
  37. DEFAULT_SYSTEM_PREFIX,
  38. TRUNCATION_HINT,
  39. TOOL_INTERRUPTED_MESSAGE,
  40. AGENT_INTERRUPTED_SUMMARY,
  41. AGENT_CONTINUE_HINT_TEMPLATE,
  42. TASK_NAME_GENERATION_SYSTEM_PROMPT,
  43. TASK_NAME_FALLBACK,
  44. SUMMARY_HEADER_TEMPLATE,
  45. COMPLETION_REFLECT_PROMPT,
  46. build_summary_header,
  47. build_tool_interrupted_message,
  48. build_agent_continue_hint,
  49. )
  50. logger = logging.getLogger(__name__)
  51. # ===== 知识管理配置 =====
  52. @dataclass
  53. class KnowledgeConfig:
  54. """知识提取与注入的配置"""
  55. # 压缩时提取(消息量超阈值触发压缩时,在 Level 1 过滤前用完整 history 反思)
  56. enable_extraction: bool = True # 是否在压缩触发时提取知识
  57. reflect_prompt: str = "" # 自定义反思 prompt;空则使用默认,见 agent/core/prompts/knowledge.py:REFLECT_PROMPT
  58. # agent运行完成后提取(不代表任务完成,agent 可能中途退出等待人工评估)
  59. enable_completion_extraction: bool = True # 是否在运行完成后提取知识
  60. completion_reflect_prompt: str = "" # 自定义复盘 prompt;空则使用默认,见 agent/core/prompts/knowledge.py:COMPLETION_REFLECT_PROMPT
  61. # 知识注入(agent切换当前工作的goal时,自动注入相关知识)
  62. enable_injection: bool = True # 是否在 focus goal 时自动注入相关知识
  63. def get_reflect_prompt(self) -> str:
  64. """压缩时反思 prompt"""
  65. return self.reflect_prompt if self.reflect_prompt else build_reflect_prompt()
  66. def get_completion_reflect_prompt(self) -> str:
  67. """任务完成后复盘 prompt"""
  68. return self.completion_reflect_prompt if self.completion_reflect_prompt else COMPLETION_REFLECT_PROMPT
  69. # ===== 运行配置 =====
  70. @dataclass
  71. class RunConfig:
  72. """
  73. 运行参数 — 控制 Agent 如何执行
  74. 分为模型层参数(由上游 agent 或用户决定)和框架层参数(由系统注入)。
  75. """
  76. # --- 模型层参数 ---
  77. model: str = "gpt-4o"
  78. temperature: float = 0.3
  79. max_iterations: int = 200
  80. tools: Optional[List[str]] = None # None = 全部已注册工具
  81. # --- 框架层参数 ---
  82. agent_type: str = "default"
  83. uid: Optional[str] = None
  84. system_prompt: Optional[str] = None # None = 从 skills 自动构建
  85. skills: Optional[List[str]] = None # 注入 system prompt 的 skill 名称列表;None = 按 preset 决定
  86. enable_memory: bool = True
  87. auto_execute_tools: bool = True
  88. name: Optional[str] = None # 显示名称(空则由 utility_llm 自动生成)
  89. enable_prompt_caching: bool = True # 启用 Anthropic Prompt Caching(仅 Claude 模型有效)
  90. # --- Trace 控制 ---
  91. trace_id: Optional[str] = None # None = 新建
  92. parent_trace_id: Optional[str] = None # 子 Agent 专用
  93. parent_goal_id: Optional[str] = None
  94. # --- 续跑控制 ---
  95. after_sequence: Optional[int] = None # 从哪条消息后续跑(message sequence)
  96. # --- 额外 LLM 参数(传给 llm_call 的 **kwargs)---
  97. extra_llm_params: Dict[str, Any] = field(default_factory=dict)
  98. # --- 知识管理配置 ---
  99. knowledge: KnowledgeConfig = field(default_factory=KnowledgeConfig)
  100. # 内置工具列表(始终自动加载)
  101. BUILTIN_TOOLS = [
  102. # 文件操作工具
  103. "read_file",
  104. "edit_file",
  105. "write_file",
  106. "glob_files",
  107. "grep_content",
  108. # 系统工具
  109. "bash_command",
  110. # 技能和目标管理
  111. "skill",
  112. "list_skills",
  113. "goal",
  114. "agent",
  115. "evaluate",
  116. # 搜索工具
  117. "search_posts",
  118. "get_search_suggestions",
  119. # 知识管理工具
  120. "knowledge_search",
  121. "knowledge_save",
  122. "knowledge_update",
  123. "knowledge_batch_update",
  124. "knowledge_list",
  125. "knowledge_slim",
  126. # 沙箱工具
  127. # "sandbox_create_environment",
  128. # "sandbox_run_shell",
  129. # "sandbox_rebuild_with_ports",
  130. # "sandbox_destroy_environment",
  131. # 浏览器工具
  132. "browser_navigate_to_url",
  133. "browser_search_web",
  134. "browser_go_back",
  135. "browser_wait",
  136. "browser_click_element",
  137. "browser_input_text",
  138. "browser_send_keys",
  139. "browser_upload_file",
  140. "browser_scroll_page",
  141. "browser_find_text",
  142. "browser_screenshot",
  143. "browser_switch_tab",
  144. "browser_close_tab",
  145. "browser_get_dropdown_options",
  146. "browser_select_dropdown_option",
  147. "browser_extract_content",
  148. "browser_read_long_content",
  149. "browser_download_direct_url",
  150. "browser_get_page_html",
  151. "browser_get_visual_selector_map",
  152. "browser_evaluate",
  153. "browser_ensure_login_with_cookies",
  154. "browser_wait_for_user_action",
  155. "browser_done",
  156. "browser_export_cookies",
  157. "browser_load_cookies"
  158. ]
  159. @dataclass
  160. class CallResult:
  161. """单次调用结果"""
  162. reply: str
  163. tool_calls: Optional[List[Dict]] = None
  164. trace_id: Optional[str] = None
  165. step_id: Optional[str] = None
  166. tokens: Optional[Dict[str, int]] = None
  167. cost: float = 0.0
  168. # ===== 执行引擎 =====
  169. CONTEXT_INJECTION_INTERVAL = 10 # 每 N 轮注入一次 GoalTree + Collaborators
  170. class AgentRunner:
  171. """
  172. Agent 执行引擎
  173. 支持三种运行模式(通过 RunConfig 区分):
  174. 1. 新建:trace_id=None
  175. 2. 续跑:trace_id=已有ID, after_sequence=None 或 == head
  176. 3. 回溯:trace_id=已有ID, after_sequence=N(N < head_sequence)
  177. """
  178. def __init__(
  179. self,
  180. trace_store: Optional[TraceStore] = None,
  181. tool_registry: Optional[ToolRegistry] = None,
  182. llm_call: Optional[Callable] = None,
  183. utility_llm_call: Optional[Callable] = None,
  184. skills_dir: Optional[str] = None,
  185. goal_tree: Optional[GoalTree] = None,
  186. debug: bool = False,
  187. ):
  188. """
  189. 初始化 AgentRunner
  190. Args:
  191. trace_store: Trace 存储
  192. tool_registry: 工具注册表(默认使用全局注册表)
  193. llm_call: 主 LLM 调用函数
  194. utility_llm_call: 轻量 LLM(用于生成任务标题等),可选
  195. skills_dir: Skills 目录路径
  196. goal_tree: 初始 GoalTree(可选)
  197. debug: 保留参数(已废弃)
  198. """
  199. self.trace_store = trace_store
  200. self.tools = tool_registry or get_tool_registry()
  201. self.llm_call = llm_call
  202. self.utility_llm_call = utility_llm_call
  203. self.skills_dir = skills_dir
  204. self.goal_tree = goal_tree
  205. self.debug = debug
  206. self._cancel_events: Dict[str, asyncio.Event] = {} # trace_id → cancel event
  207. # 知识保存跟踪(每个 trace 独立)
  208. self._saved_knowledge_ids: Dict[str, List[str]] = {} # trace_id → [knowledge_ids]
  209. # ===== 核心公开方法 =====
  210. async def run(
  211. self,
  212. messages: List[Dict],
  213. config: Optional[RunConfig] = None,
  214. ) -> AsyncIterator[Union[Trace, Message]]:
  215. """
  216. Agent 模式执行(核心方法)
  217. Args:
  218. messages: OpenAI SDK 格式的输入消息
  219. 新建: 初始任务消息 [{"role": "user", "content": "..."}]
  220. 续跑: 追加的新消息
  221. 回溯: 在插入点之后追加的消息
  222. config: 运行配置
  223. Yields:
  224. Union[Trace, Message]: Trace 对象(状态变化)或 Message 对象(执行过程)
  225. """
  226. if not self.llm_call:
  227. raise ValueError("llm_call function not provided")
  228. config = config or RunConfig()
  229. trace = None
  230. try:
  231. # Phase 1: PREPARE TRACE
  232. trace, goal_tree, sequence = await self._prepare_trace(messages, config)
  233. # 注册取消事件
  234. self._cancel_events[trace.trace_id] = asyncio.Event()
  235. yield trace
  236. # Phase 2: BUILD HISTORY
  237. history, sequence, created_messages, head_seq = await self._build_history(
  238. trace.trace_id, messages, goal_tree, config, sequence
  239. )
  240. # Update trace's head_sequence in memory
  241. trace.head_sequence = head_seq
  242. for msg in created_messages:
  243. yield msg
  244. # Phase 3: AGENT LOOP
  245. async for event in self._agent_loop(trace, history, goal_tree, config, sequence):
  246. yield event
  247. except Exception as e:
  248. logger.error(f"Agent run failed: {e}")
  249. tid = config.trace_id or (trace.trace_id if trace else None)
  250. if self.trace_store and tid:
  251. # 读取当前 last_sequence 作为 head_sequence,确保续跑时能加载完整历史
  252. current = await self.trace_store.get_trace(tid)
  253. head_seq = current.last_sequence if current else None
  254. await self.trace_store.update_trace(
  255. tid,
  256. status="failed",
  257. head_sequence=head_seq,
  258. error_message=str(e),
  259. completed_at=datetime.now()
  260. )
  261. trace_obj = await self.trace_store.get_trace(tid)
  262. if trace_obj:
  263. yield trace_obj
  264. raise
  265. finally:
  266. # 清理取消事件
  267. if trace:
  268. self._cancel_events.pop(trace.trace_id, None)
  269. async def run_result(
  270. self,
  271. messages: List[Dict],
  272. config: Optional[RunConfig] = None,
  273. on_event: Optional[Callable] = None,
  274. ) -> Dict[str, Any]:
  275. """
  276. 结果模式 — 消费 run(),返回结构化结果。
  277. 主要用于 agent/evaluate 工具内部。
  278. Args:
  279. on_event: 可选回调,每个 Trace/Message 事件触发一次,用于实时输出子 Agent 执行过程。
  280. """
  281. last_assistant_text = ""
  282. final_trace: Optional[Trace] = None
  283. async for item in self.run(messages=messages, config=config):
  284. if on_event:
  285. on_event(item)
  286. if isinstance(item, Message) and item.role == "assistant":
  287. content = item.content
  288. text = ""
  289. if isinstance(content, dict):
  290. text = content.get("text", "") or ""
  291. elif isinstance(content, str):
  292. text = content
  293. if text and text.strip():
  294. last_assistant_text = text
  295. elif isinstance(item, Trace):
  296. final_trace = item
  297. config = config or RunConfig()
  298. if not final_trace and config.trace_id and self.trace_store:
  299. final_trace = await self.trace_store.get_trace(config.trace_id)
  300. status = final_trace.status if final_trace else "unknown"
  301. error = final_trace.error_message if final_trace else None
  302. summary = last_assistant_text
  303. if not summary:
  304. status = "failed"
  305. error = error or "Agent 没有产生 assistant 文本结果"
  306. # 获取保存的知识 ID
  307. trace_id = final_trace.trace_id if final_trace else config.trace_id
  308. saved_knowledge_ids = self._saved_knowledge_ids.get(trace_id, [])
  309. return {
  310. "status": status,
  311. "summary": summary,
  312. "trace_id": trace_id,
  313. "error": error,
  314. "saved_knowledge_ids": saved_knowledge_ids, # 新增:返回保存的知识 ID
  315. "stats": {
  316. "total_messages": final_trace.total_messages if final_trace else 0,
  317. "total_tokens": final_trace.total_tokens if final_trace else 0,
  318. "total_cost": final_trace.total_cost if final_trace else 0.0,
  319. },
  320. }
  321. async def stop(self, trace_id: str) -> bool:
  322. """
  323. 停止运行中的 Trace
  324. 设置取消信号,agent loop 在下一个 LLM 调用前检查并退出。
  325. Trace 状态置为 "stopped"。
  326. Returns:
  327. True 如果成功发送停止信号,False 如果该 trace 不在运行中
  328. """
  329. cancel_event = self._cancel_events.get(trace_id)
  330. if cancel_event is None:
  331. return False
  332. cancel_event.set()
  333. return True
  334. # ===== 单次调用(保留)=====
  335. async def call(
  336. self,
  337. messages: List[Dict],
  338. model: str = "gpt-4o",
  339. tools: Optional[List[str]] = None,
  340. uid: Optional[str] = None,
  341. trace: bool = True,
  342. **kwargs
  343. ) -> CallResult:
  344. """
  345. 单次 LLM 调用(无 Agent Loop)
  346. """
  347. if not self.llm_call:
  348. raise ValueError("llm_call function not provided")
  349. trace_id = None
  350. message_id = None
  351. tool_schemas = self._get_tool_schemas(tools)
  352. if trace and self.trace_store:
  353. trace_obj = Trace.create(mode="call", uid=uid, model=model, tools=tool_schemas, llm_params=kwargs)
  354. trace_id = await self.trace_store.create_trace(trace_obj)
  355. result = await self.llm_call(messages=messages, model=model, tools=tool_schemas, **kwargs)
  356. if trace and self.trace_store and trace_id:
  357. msg = Message.create(
  358. trace_id=trace_id, role="assistant", sequence=1, goal_id=None,
  359. content={"text": result.get("content", ""), "tool_calls": result.get("tool_calls")},
  360. prompt_tokens=result.get("prompt_tokens", 0),
  361. completion_tokens=result.get("completion_tokens", 0),
  362. finish_reason=result.get("finish_reason"),
  363. cost=result.get("cost", 0),
  364. )
  365. message_id = await self.trace_store.add_message(msg)
  366. await self.trace_store.update_trace(trace_id, status="completed", completed_at=datetime.now())
  367. return CallResult(
  368. reply=result.get("content", ""),
  369. tool_calls=result.get("tool_calls"),
  370. trace_id=trace_id,
  371. step_id=message_id,
  372. tokens={"prompt": result.get("prompt_tokens", 0), "completion": result.get("completion_tokens", 0)},
  373. cost=result.get("cost", 0)
  374. )
  375. # ===== Phase 1: PREPARE TRACE =====
  376. async def _prepare_trace(
  377. self,
  378. messages: List[Dict],
  379. config: RunConfig,
  380. ) -> Tuple[Trace, Optional[GoalTree], int]:
  381. """
  382. 准备 Trace:创建新的或加载已有的
  383. Returns:
  384. (trace, goal_tree, next_sequence)
  385. """
  386. if config.trace_id:
  387. return await self._prepare_existing_trace(config)
  388. else:
  389. return await self._prepare_new_trace(messages, config)
  390. async def _prepare_new_trace(
  391. self,
  392. messages: List[Dict],
  393. config: RunConfig,
  394. ) -> Tuple[Trace, Optional[GoalTree], int]:
  395. """创建新 Trace"""
  396. trace_id = str(uuid.uuid4())
  397. # 生成任务名称
  398. task_name = config.name or await self._generate_task_name(messages)
  399. # 准备工具 Schema
  400. tool_schemas = self._get_tool_schemas(config.tools)
  401. trace_obj = Trace(
  402. trace_id=trace_id,
  403. mode="agent",
  404. task=task_name,
  405. agent_type=config.agent_type,
  406. parent_trace_id=config.parent_trace_id,
  407. parent_goal_id=config.parent_goal_id,
  408. uid=config.uid,
  409. model=config.model,
  410. tools=tool_schemas,
  411. llm_params={"temperature": config.temperature, **config.extra_llm_params},
  412. status="running",
  413. )
  414. goal_tree = self.goal_tree or GoalTree(mission=task_name)
  415. if self.trace_store:
  416. await self.trace_store.create_trace(trace_obj)
  417. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  418. return trace_obj, goal_tree, 1
  419. async def _prepare_existing_trace(
  420. self,
  421. config: RunConfig,
  422. ) -> Tuple[Trace, Optional[GoalTree], int]:
  423. """加载已有 Trace(续跑或回溯)"""
  424. if not self.trace_store:
  425. raise ValueError("trace_store required for continue/rewind")
  426. trace_obj = await self.trace_store.get_trace(config.trace_id)
  427. if not trace_obj:
  428. raise ValueError(f"Trace not found: {config.trace_id}")
  429. goal_tree = await self.trace_store.get_goal_tree(config.trace_id)
  430. if goal_tree is None:
  431. # 防御性兜底:trace 存在但 goal.json 丢失时,创建空树
  432. goal_tree = GoalTree(mission=trace_obj.task or "Agent task")
  433. await self.trace_store.update_goal_tree(config.trace_id, goal_tree)
  434. # 自动判断行为:after_sequence 为 None 或 == head → 续跑;< head → 回溯
  435. after_seq = config.after_sequence
  436. # 如果 after_seq > head_sequence,说明 generator 被强制关闭时 store 的
  437. # head_sequence 未来得及更新(仍停在 Phase 2 写入的初始值)。
  438. # 用 last_sequence 修正 head_sequence,确保续跑时能看到完整历史。
  439. if after_seq is not None and after_seq > trace_obj.head_sequence:
  440. trace_obj.head_sequence = trace_obj.last_sequence
  441. await self.trace_store.update_trace(
  442. config.trace_id, head_sequence=trace_obj.head_sequence
  443. )
  444. if after_seq is not None and after_seq < trace_obj.head_sequence:
  445. # 回溯模式
  446. sequence = await self._rewind(config.trace_id, after_seq, goal_tree)
  447. else:
  448. # 续跑模式:从 last_sequence + 1 开始
  449. sequence = trace_obj.last_sequence + 1
  450. # 状态置为 running
  451. await self.trace_store.update_trace(
  452. config.trace_id,
  453. status="running",
  454. completed_at=None,
  455. )
  456. trace_obj.status = "running"
  457. return trace_obj, goal_tree, sequence
  458. # ===== Phase 2: BUILD HISTORY =====
  459. async def _build_history(
  460. self,
  461. trace_id: str,
  462. new_messages: List[Dict],
  463. goal_tree: Optional[GoalTree],
  464. config: RunConfig,
  465. sequence: int,
  466. ) -> Tuple[List[Dict], int, List[Message]]:
  467. """
  468. 构建完整的 LLM 消息历史
  469. 1. 从 head_sequence 沿 parent chain 加载主路径消息(续跑/回溯场景)
  470. 2. 构建 system prompt(新建时注入 skills)
  471. 3. 新建时:在第一条 user message 末尾注入当前经验
  472. 4. 追加 input messages(设置 parent_sequence 链接到当前 head)
  473. Returns:
  474. (history, next_sequence, created_messages, head_sequence)
  475. created_messages: 本次新创建并持久化的 Message 列表,供 run() yield 给调用方
  476. head_sequence: 当前主路径头节点的 sequence
  477. """
  478. history: List[Dict] = []
  479. created_messages: List[Message] = []
  480. head_seq: Optional[int] = None # 当前主路径的头节点 sequence
  481. # 1. 加载已有 messages(通过主路径遍历)
  482. if config.trace_id and self.trace_store:
  483. trace_obj = await self.trace_store.get_trace(trace_id)
  484. if trace_obj and trace_obj.head_sequence > 0:
  485. main_path = await self.trace_store.get_main_path_messages(
  486. trace_id, trace_obj.head_sequence
  487. )
  488. # 修复 orphaned tool_calls(中断导致的 tool_call 无 tool_result)
  489. main_path, sequence = await self._heal_orphaned_tool_calls(
  490. main_path, trace_id, goal_tree, sequence,
  491. )
  492. history = [msg.to_llm_dict() for msg in main_path]
  493. if main_path:
  494. head_seq = main_path[-1].sequence
  495. # 2. 构建/注入 skills 到 system prompt
  496. has_system = any(m.get("role") == "system" for m in history)
  497. has_system_in_new = any(m.get("role") == "system" for m in new_messages)
  498. if not has_system:
  499. if has_system_in_new:
  500. # 入参消息已含 system,将 skills 注入其中(在 step 4 持久化之前)
  501. augmented = []
  502. for msg in new_messages:
  503. if msg.get("role") == "system":
  504. base = msg.get("content") or ""
  505. enriched = await self._build_system_prompt(config, base_prompt=base)
  506. augmented.append({**msg, "content": enriched or base})
  507. else:
  508. augmented.append(msg)
  509. new_messages = augmented
  510. else:
  511. # 没有 system,自动构建并插入历史
  512. system_prompt = await self._build_system_prompt(config)
  513. if system_prompt:
  514. history = [{"role": "system", "content": system_prompt}] + history
  515. if self.trace_store:
  516. system_msg = Message.create(
  517. trace_id=trace_id, role="system", sequence=sequence,
  518. goal_id=None, content=system_prompt,
  519. parent_sequence=None, # system message 是 root
  520. )
  521. await self.trace_store.add_message(system_msg)
  522. created_messages.append(system_msg)
  523. head_seq = sequence
  524. sequence += 1
  525. # 3. 追加新 messages(设置 parent_sequence 链接到当前 head)
  526. for msg_dict in new_messages:
  527. history.append(msg_dict)
  528. if self.trace_store:
  529. stored_msg = Message.from_llm_dict(
  530. msg_dict, trace_id=trace_id, sequence=sequence,
  531. goal_id=None, parent_sequence=head_seq,
  532. )
  533. await self.trace_store.add_message(stored_msg)
  534. created_messages.append(stored_msg)
  535. head_seq = sequence
  536. sequence += 1
  537. # 5. 更新 trace 的 head_sequence
  538. if self.trace_store and head_seq is not None:
  539. await self.trace_store.update_trace(trace_id, head_sequence=head_seq)
  540. return history, sequence, created_messages, head_seq or 0
  541. # ===== Phase 3: AGENT LOOP =====
  542. async def _agent_loop(
  543. self,
  544. trace: Trace,
  545. history: List[Dict],
  546. goal_tree: Optional[GoalTree],
  547. config: RunConfig,
  548. sequence: int,
  549. ) -> AsyncIterator[Union[Trace, Message]]:
  550. """ReAct 循环"""
  551. trace_id = trace.trace_id
  552. tool_schemas = self._get_tool_schemas(config.tools)
  553. # 当前主路径头节点的 sequence(用于设置 parent_sequence)
  554. head_seq = trace.head_sequence
  555. for iteration in range(config.max_iterations):
  556. # 检查取消信号
  557. cancel_event = self._cancel_events.get(trace_id)
  558. if cancel_event and cancel_event.is_set():
  559. logger.info(f"Trace {trace_id} stopped by user")
  560. if self.trace_store:
  561. await self.trace_store.update_trace(
  562. trace_id,
  563. status="stopped",
  564. head_sequence=head_seq,
  565. completed_at=datetime.now(),
  566. )
  567. trace_obj = await self.trace_store.get_trace(trace_id)
  568. if trace_obj:
  569. yield trace_obj
  570. return
  571. # Level 1 压缩:GoalTree 过滤(当消息超过阈值时触发)
  572. compression_config = CompressionConfig()
  573. token_count = estimate_tokens(history)
  574. max_tokens = compression_config.get_max_tokens(config.model)
  575. # 压缩评估日志
  576. progress_pct = (token_count / max_tokens * 100) if max_tokens > 0 else 0
  577. msg_count = len(history)
  578. img_count = sum(
  579. 1 for msg in history
  580. if isinstance(msg.get("content"), list)
  581. for part in msg["content"]
  582. if isinstance(part, dict) and part.get("type") in ("image", "image_url")
  583. )
  584. # 检查是否需要压缩(token 或消息数量超限)
  585. needs_compression_by_tokens = token_count > max_tokens
  586. needs_compression_by_count = (
  587. compression_config.max_messages > 0 and
  588. msg_count > compression_config.max_messages
  589. )
  590. needs_compression = needs_compression_by_tokens or needs_compression_by_count
  591. print(f"\n[压缩评估] 消息数: {msg_count} / {compression_config.max_messages} | 图片数: {img_count} | Token: {token_count:,} / {max_tokens:,} ({progress_pct:.1f}%)")
  592. if needs_compression:
  593. if needs_compression_by_count:
  594. print(f"[压缩评估] ⚠️ 消息数超过阈值 ({msg_count} > {compression_config.max_messages}),触发压缩流程")
  595. if needs_compression_by_tokens:
  596. print(f"[压缩评估] ⚠️ Token 数超过阈值,触发压缩流程")
  597. else:
  598. print(f"[压缩评估] ✅ 未超阈值,无需压缩")
  599. # 知识提取:在任何压缩发生前,用完整 history 做反思
  600. if needs_compression and config.knowledge.enable_extraction:
  601. await self._run_reflect(
  602. trace_id, history, config,
  603. reflect_prompt=config.knowledge.get_reflect_prompt(),
  604. source_name="compression_reflection",
  605. )
  606. if needs_compression and self.trace_store and goal_tree:
  607. # 使用本地 head_seq(store 中的 head_sequence 在 loop 期间未更新,是过时的)
  608. if head_seq > 0:
  609. main_path_msgs = await self.trace_store.get_main_path_messages(
  610. trace_id, head_seq
  611. )
  612. filtered_msgs = filter_by_goal_status(main_path_msgs, goal_tree)
  613. if len(filtered_msgs) < len(main_path_msgs):
  614. filtered_tokens = estimate_tokens([msg.to_llm_dict() for msg in filtered_msgs])
  615. print(
  616. f"[Level 1 压缩] 消息: {len(main_path_msgs)} → {len(filtered_msgs)} 条 | "
  617. f"Token: {token_count:,} → ~{filtered_tokens:,}"
  618. )
  619. logger.info(
  620. "Level 1 压缩: %d -> %d 条消息 (tokens ~%d, 阈值 %d)",
  621. len(main_path_msgs), len(filtered_msgs), token_count, max_tokens,
  622. )
  623. history = [msg.to_llm_dict() for msg in filtered_msgs]
  624. else:
  625. print(
  626. f"[Level 1 压缩] 无可过滤消息 ({len(main_path_msgs)} 条全部保留, "
  627. f"completed/abandoned goals={sum(1 for g in goal_tree.goals if g.status in ('completed', 'abandoned'))})"
  628. )
  629. logger.info(
  630. "Level 1 压缩: 无可过滤消息 (%d 条全部保留, completed/abandoned goals=%d)",
  631. len(main_path_msgs),
  632. sum(1 for g in goal_tree.goals
  633. if g.status in ("completed", "abandoned")),
  634. )
  635. elif needs_compression:
  636. print("[压缩评估] ⚠️ 无法执行 Level 1 压缩(缺少 store 或 goal_tree)")
  637. logger.warning(
  638. "消息数 (%d) 或 token 数 (%d) 超过阈值 (max_messages=%d, max_tokens=%d),但无法执行 Level 1 压缩(缺少 store 或 goal_tree)",
  639. msg_count, token_count, compression_config.max_messages, max_tokens,
  640. )
  641. # Level 2 压缩:LLM 总结(Level 1 后仍超阈值时触发)
  642. token_count_after = estimate_tokens(history)
  643. msg_count_after = len(history)
  644. needs_level2_by_tokens = token_count_after > max_tokens
  645. needs_level2_by_count = (
  646. compression_config.max_messages > 0 and
  647. msg_count_after > compression_config.max_messages
  648. )
  649. needs_level2 = needs_level2_by_tokens or needs_level2_by_count
  650. if needs_level2:
  651. progress_pct_after = (token_count_after / max_tokens * 100) if max_tokens > 0 else 0
  652. reason = []
  653. if needs_level2_by_count:
  654. reason.append(f"消息数 {msg_count_after} > {compression_config.max_messages}")
  655. if needs_level2_by_tokens:
  656. reason.append(f"Token {token_count_after:,} > {max_tokens:,} ({progress_pct_after:.1f}%)")
  657. print(
  658. f"[Level 2 压缩] Level 1 后仍超阈值: {' | '.join(reason)} "
  659. f"→ 触发 LLM 总结"
  660. )
  661. logger.info(
  662. "Level 1 后仍超阈值 (消息数=%d/%d, token=%d/%d),触发 Level 2 压缩",
  663. msg_count_after, compression_config.max_messages, token_count_after, max_tokens,
  664. )
  665. history, head_seq, sequence = await self._compress_history(
  666. trace_id, history, goal_tree, config, sequence, head_seq,
  667. )
  668. final_tokens = estimate_tokens(history)
  669. print(f"[Level 2 压缩] 完成: Token {token_count_after:,} → {final_tokens:,}")
  670. elif needs_compression:
  671. # Level 1 压缩成功,未触发 Level 2
  672. print(f"[压缩评估] ✅ Level 1 压缩后达标: 消息数 {msg_count_after} | Token {token_count_after:,} / {max_tokens:,}")
  673. print() # 空行分隔
  674. # 构建 LLM messages(注入上下文)
  675. llm_messages = list(history)
  676. # 对历史消息应用 Prompt Caching
  677. llm_messages = self._add_cache_control(
  678. llm_messages,
  679. config.model,
  680. config.enable_prompt_caching
  681. )
  682. # 周期性注入 GoalTree + Collaborators(动态内容追加在缓存点之后)
  683. if iteration % CONTEXT_INJECTION_INTERVAL == 0:
  684. context_injection = self._build_context_injection(trace, goal_tree)
  685. if context_injection:
  686. system_msg = {"role": "system", "content": context_injection}
  687. llm_messages.append(system_msg)
  688. # 持久化上下文注入消息
  689. if self.trace_store:
  690. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  691. system_message = Message.create(
  692. trace_id=trace_id,
  693. role="system",
  694. sequence=sequence,
  695. goal_id=current_goal_id,
  696. parent_sequence=head_seq if head_seq > 0 else None,
  697. content=f"[上下文注入]\n{context_injection}",
  698. )
  699. await self.trace_store.add_message(system_message)
  700. history.append(system_msg)
  701. head_seq = sequence
  702. sequence += 1
  703. # 调用 LLM
  704. result = await self.llm_call(
  705. messages=llm_messages,
  706. model=config.model,
  707. tools=tool_schemas,
  708. temperature=config.temperature,
  709. **config.extra_llm_params,
  710. )
  711. response_content = result.get("content", "")
  712. tool_calls = result.get("tool_calls")
  713. finish_reason = result.get("finish_reason")
  714. prompt_tokens = result.get("prompt_tokens", 0)
  715. completion_tokens = result.get("completion_tokens", 0)
  716. step_cost = result.get("cost", 0)
  717. cache_creation_tokens = result.get("cache_creation_tokens")
  718. cache_read_tokens = result.get("cache_read_tokens")
  719. # 按需自动创建 root goal
  720. if goal_tree and not goal_tree.goals and tool_calls:
  721. has_goal_call = any(
  722. tc.get("function", {}).get("name") == "goal"
  723. for tc in tool_calls
  724. )
  725. if not has_goal_call:
  726. mission = goal_tree.mission
  727. root_desc = mission[:200] if len(mission) > 200 else mission
  728. goal_tree.add_goals(
  729. descriptions=[root_desc],
  730. reasons=["系统自动创建:Agent 未显式创建目标"],
  731. parent_id=None
  732. )
  733. goal_tree.focus(goal_tree.goals[0].id)
  734. if self.trace_store:
  735. await self.trace_store.add_goal(trace_id, goal_tree.goals[0])
  736. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  737. logger.info(f"自动创建 root goal: {goal_tree.goals[0].id}")
  738. # 获取当前 goal_id
  739. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  740. # 记录 assistant Message(parent_sequence 指向当前 head)
  741. assistant_msg = Message.create(
  742. trace_id=trace_id,
  743. role="assistant",
  744. sequence=sequence,
  745. goal_id=current_goal_id,
  746. parent_sequence=head_seq if head_seq > 0 else None,
  747. content={"text": response_content, "tool_calls": tool_calls},
  748. prompt_tokens=prompt_tokens,
  749. completion_tokens=completion_tokens,
  750. cache_creation_tokens=cache_creation_tokens,
  751. cache_read_tokens=cache_read_tokens,
  752. finish_reason=finish_reason,
  753. cost=step_cost,
  754. )
  755. if self.trace_store:
  756. await self.trace_store.add_message(assistant_msg)
  757. # 记录模型使用
  758. await self.trace_store.record_model_usage(
  759. trace_id=trace_id,
  760. sequence=sequence - 1, # assistant_msg的sequence
  761. role="assistant",
  762. model=config.model,
  763. prompt_tokens=prompt_tokens,
  764. completion_tokens=completion_tokens,
  765. cache_read_tokens=cache_read_tokens or 0,
  766. )
  767. yield assistant_msg
  768. head_seq = sequence
  769. sequence += 1
  770. # 处理工具调用
  771. # 截断兜底:finish_reason == "length" 说明响应被 max_tokens 截断,
  772. # tool call 参数很可能不完整,不应执行,改为提示模型分批操作
  773. if tool_calls and finish_reason == "length":
  774. logger.warning(
  775. "[Runner] 响应被 max_tokens 截断,跳过 %d 个不完整的 tool calls",
  776. len(tool_calls),
  777. )
  778. truncation_hint = TRUNCATION_HINT
  779. history.append({
  780. "role": "assistant",
  781. "content": response_content,
  782. "tool_calls": tool_calls,
  783. })
  784. # 为每个被截断的 tool call 返回错误结果
  785. for tc in tool_calls:
  786. history.append({
  787. "role": "tool",
  788. "tool_call_id": tc["id"],
  789. "content": truncation_hint,
  790. })
  791. continue
  792. if tool_calls and config.auto_execute_tools:
  793. history.append({
  794. "role": "assistant",
  795. "content": response_content,
  796. "tool_calls": tool_calls,
  797. })
  798. for tc in tool_calls:
  799. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  800. tool_name = tc["function"]["name"]
  801. tool_args = tc["function"]["arguments"]
  802. if isinstance(tool_args, str):
  803. tool_args = json.loads(tool_args) if tool_args.strip() else {}
  804. elif tool_args is None:
  805. tool_args = {}
  806. tool_result = await self.tools.execute(
  807. tool_name,
  808. tool_args,
  809. uid=config.uid or "",
  810. context={
  811. "store": self.trace_store,
  812. "trace_id": trace_id,
  813. "goal_id": current_goal_id,
  814. "runner": self,
  815. "goal_tree": goal_tree,
  816. }
  817. )
  818. # 跟踪保存的知识 ID
  819. if tool_name == "knowledge_save" and isinstance(tool_result, dict):
  820. metadata = tool_result.get("metadata", {})
  821. knowledge_id = metadata.get("knowledge_id")
  822. if knowledge_id:
  823. if trace_id not in self._saved_knowledge_ids:
  824. self._saved_knowledge_ids[trace_id] = []
  825. self._saved_knowledge_ids[trace_id].append(knowledge_id)
  826. logger.info(f"[Knowledge Tracking] 记录保存的知识 ID: {knowledge_id}")
  827. # --- 支持多模态工具反馈 ---
  828. # execute() 返回 dict{"text","images","tool_usage"} 或 str
  829. # 统一为dict格式
  830. if isinstance(tool_result, str):
  831. tool_result = {"text": tool_result}
  832. tool_text = tool_result.get("text", str(tool_result))
  833. tool_images = tool_result.get("images", [])
  834. tool_usage = tool_result.get("tool_usage") # 新增:提取tool_usage
  835. # 处理多模态消息
  836. if tool_images:
  837. tool_result_text = tool_text
  838. # 构建多模态消息格式
  839. tool_content_for_llm = [{"type": "text", "text": tool_text}]
  840. for img in tool_images:
  841. if img.get("type") == "base64" and img.get("data"):
  842. media_type = img.get("media_type", "image/png")
  843. tool_content_for_llm.append({
  844. "type": "image_url",
  845. "image_url": {
  846. "url": f"data:{media_type};base64,{img['data']}"
  847. }
  848. })
  849. img_count = len(tool_content_for_llm) - 1 # 减去 text 块
  850. print(f"[Runner] 多模态工具反馈: tool={tool_name}, images={img_count}, text_len={len(tool_result_text)}")
  851. else:
  852. tool_result_text = tool_text
  853. tool_content_for_llm = tool_text
  854. tool_msg = Message.create(
  855. trace_id=trace_id,
  856. role="tool",
  857. sequence=sequence,
  858. goal_id=current_goal_id,
  859. parent_sequence=head_seq,
  860. tool_call_id=tc["id"],
  861. # 存储完整内容:有图片时保留 list(含 image_url),纯文本时存字符串
  862. content={"tool_name": tool_name, "result": tool_content_for_llm},
  863. )
  864. if self.trace_store:
  865. await self.trace_store.add_message(tool_msg)
  866. # 记录工具的模型使用
  867. if tool_usage:
  868. await self.trace_store.record_model_usage(
  869. trace_id=trace_id,
  870. sequence=sequence,
  871. role="tool",
  872. tool_name=tool_name,
  873. model=tool_usage.get("model"),
  874. prompt_tokens=tool_usage.get("prompt_tokens", 0),
  875. completion_tokens=tool_usage.get("completion_tokens", 0),
  876. cache_read_tokens=tool_usage.get("cache_read_tokens", 0),
  877. )
  878. # 截图单独存为同名 PNG 文件
  879. if tool_images:
  880. import base64 as b64mod
  881. for img in tool_images:
  882. if img.get("data"):
  883. png_path = self.trace_store._get_messages_dir(trace_id) / f"{tool_msg.message_id}.png"
  884. png_path.write_bytes(b64mod.b64decode(img["data"]))
  885. print(f"[Runner] 截图已保存: {png_path.name}")
  886. break # 只存第一张
  887. yield tool_msg
  888. head_seq = sequence
  889. sequence += 1
  890. history.append({
  891. "role": "tool",
  892. "tool_call_id": tc["id"],
  893. "name": tool_name,
  894. "content": tool_content_for_llm,
  895. })
  896. continue # 继续循环
  897. # 无工具调用,任务完成
  898. break
  899. # 任务完成后复盘提取
  900. if config.knowledge.enable_completion_extraction:
  901. await self._extract_knowledge_on_completion(trace_id, history, config)
  902. # 更新 head_sequence 并完成 Trace
  903. if self.trace_store:
  904. await self.trace_store.update_trace(
  905. trace_id,
  906. status="completed",
  907. head_sequence=head_seq,
  908. completed_at=datetime.now(),
  909. )
  910. trace_obj = await self.trace_store.get_trace(trace_id)
  911. if trace_obj:
  912. yield trace_obj
  913. # ===== Level 2: LLM 压缩 =====
  914. async def _compress_history(
  915. self,
  916. trace_id: str,
  917. history: List[Dict],
  918. goal_tree: Optional[GoalTree],
  919. config: RunConfig,
  920. sequence: int,
  921. head_seq: int,
  922. ) -> Tuple[List[Dict], int, int]:
  923. """
  924. Level 2 压缩:LLM 总结
  925. Step 1: 压缩总结 — LLM 生成 summary
  926. Step 2: 存储 summary 为新消息,parent_sequence 跳到 system msg
  927. Step 3: 重建 history
  928. Returns:
  929. (new_history, new_head_seq, next_sequence)
  930. """
  931. logger.info("Level 2 压缩开始: trace=%s, 当前 history 长度=%d", trace_id, len(history))
  932. # 找到 system message 的 sequence(主路径第一条消息)
  933. system_msg_seq = None
  934. system_msg_dict = None
  935. if self.trace_store:
  936. trace_obj = await self.trace_store.get_trace(trace_id)
  937. if trace_obj and trace_obj.head_sequence > 0:
  938. main_path = await self.trace_store.get_main_path_messages(
  939. trace_id, trace_obj.head_sequence
  940. )
  941. for msg in main_path:
  942. if msg.role == "system":
  943. system_msg_seq = msg.sequence
  944. system_msg_dict = msg.to_llm_dict()
  945. break
  946. # Fallback: 从 history 中找 system message
  947. if system_msg_dict is None:
  948. for msg_dict in history:
  949. if msg_dict.get("role") == "system":
  950. system_msg_dict = msg_dict
  951. break
  952. if system_msg_dict is None:
  953. logger.warning("Level 2 压缩跳过:未找到 system message")
  954. return history, head_seq, sequence
  955. # --- Step 1: 压缩总结 ---
  956. compress_prompt = build_compression_prompt(goal_tree)
  957. compress_messages = list(history) + [{"role": "user", "content": compress_prompt}]
  958. # 应用 Prompt Caching
  959. compress_messages = self._add_cache_control(
  960. compress_messages,
  961. config.model,
  962. config.enable_prompt_caching
  963. )
  964. compress_result = await self.llm_call(
  965. messages=compress_messages,
  966. model=config.model,
  967. tools=[],
  968. temperature=config.temperature,
  969. **config.extra_llm_params,
  970. )
  971. raw_output = compress_result.get("content", "").strip()
  972. if not raw_output:
  973. logger.warning("Level 2 压缩跳过:LLM 未返回内容")
  974. return history, head_seq, sequence
  975. # 提取 [[SUMMARY]] 块
  976. summary_text = raw_output
  977. if "[[SUMMARY]]" in raw_output:
  978. summary_text = raw_output[raw_output.index("[[SUMMARY]]") + len("[[SUMMARY]]"):].strip()
  979. if not summary_text:
  980. logger.warning("Level 2 压缩跳过:LLM 未返回 summary")
  981. return history, head_seq, sequence
  982. # --- Step 3: 存储 summary 消息 ---
  983. summary_with_header = build_summary_header(summary_text)
  984. summary_msg = Message.create(
  985. trace_id=trace_id,
  986. role="user",
  987. sequence=sequence,
  988. goal_id=None,
  989. parent_sequence=system_msg_seq, # 跳到 system msg,跳过所有中间消息
  990. content=summary_with_header,
  991. )
  992. if self.trace_store:
  993. await self.trace_store.add_message(summary_msg)
  994. new_head_seq = sequence
  995. sequence += 1
  996. # --- Step 4: 重建 history ---
  997. new_history = [system_msg_dict, summary_msg.to_llm_dict()]
  998. # 更新 trace head_sequence
  999. if self.trace_store:
  1000. await self.trace_store.update_trace(
  1001. trace_id,
  1002. head_sequence=new_head_seq,
  1003. )
  1004. logger.info(
  1005. "Level 2 压缩完成: 旧 history %d 条 → 新 history %d 条, summary 长度=%d",
  1006. len(history), len(new_history), len(summary_text),
  1007. )
  1008. return new_history, new_head_seq, sequence
  1009. async def _run_reflect(
  1010. self,
  1011. trace_id: str,
  1012. history: List[Dict],
  1013. config: RunConfig,
  1014. reflect_prompt: str,
  1015. source_name: str,
  1016. ) -> None:
  1017. """
  1018. 执行反思提取:LLM 对历史消息进行反思,直接调用 knowledge_save 工具保存经验。
  1019. Args:
  1020. trace_id: Trace ID(作为知识的 message_id)
  1021. history: 当前对话历史
  1022. config: 运行配置
  1023. reflect_prompt: 反思 prompt
  1024. source_name: 来源名称(用于区分压缩时/完成时)
  1025. """
  1026. try:
  1027. reflect_messages = list(history) + [{"role": "user", "content": reflect_prompt}]
  1028. reflect_messages = self._add_cache_control(
  1029. reflect_messages, config.model, config.enable_prompt_caching
  1030. )
  1031. # 只暴露 knowledge_save 工具,让 LLM 直接调用
  1032. knowledge_save_schema = self._get_tool_schemas(["knowledge_save"])
  1033. reflect_result = await self.llm_call(
  1034. messages=reflect_messages,
  1035. model=config.model,
  1036. tools=knowledge_save_schema,
  1037. temperature=0.2,
  1038. **config.extra_llm_params,
  1039. )
  1040. tool_calls = reflect_result.get("tool_calls") or []
  1041. if not tool_calls:
  1042. logger.info("反思阶段无经验保存 (source=%s)", source_name)
  1043. return
  1044. saved_count = 0
  1045. for tc in tool_calls:
  1046. tool_name = tc.get("function", {}).get("name")
  1047. if tool_name != "knowledge_save":
  1048. continue
  1049. tool_args = tc.get("function", {}).get("arguments") or {}
  1050. if isinstance(tool_args, str):
  1051. tool_args = json.loads(tool_args) if tool_args.strip() else {}
  1052. # 注入来源信息(LLM 不需要填写这些字段)
  1053. tool_args.setdefault("source_name", source_name)
  1054. tool_args.setdefault("source_category", "exp")
  1055. tool_args.setdefault("message_id", trace_id)
  1056. try:
  1057. await self.tools.execute(
  1058. "knowledge_save",
  1059. tool_args,
  1060. uid=config.uid or "",
  1061. context={"store": self.trace_store, "trace_id": trace_id},
  1062. )
  1063. saved_count += 1
  1064. except Exception as e:
  1065. logger.warning("保存经验失败: %s", e)
  1066. logger.info("已提取并保存 %d 条经验 (source=%s)", saved_count, source_name)
  1067. except Exception as e:
  1068. logger.error("知识反思提取失败 (source=%s): %s", source_name, e)
  1069. async def _extract_knowledge_on_completion(
  1070. self,
  1071. trace_id: str,
  1072. history: List[Dict],
  1073. config: RunConfig,
  1074. ) -> None:
  1075. """任务完成后执行全局复盘,提取经验保存到知识库。"""
  1076. logger.info("任务完成后复盘提取: trace=%s", trace_id)
  1077. await self._run_reflect(
  1078. trace_id, history, config,
  1079. reflect_prompt=config.knowledge.get_completion_reflect_prompt(),
  1080. source_name="completion_reflection",
  1081. )
  1082. # ===== 回溯(Rewind)=====
  1083. async def _rewind(
  1084. self,
  1085. trace_id: str,
  1086. after_sequence: int,
  1087. goal_tree: Optional[GoalTree],
  1088. ) -> int:
  1089. """
  1090. 执行回溯:快照 GoalTree,重建干净树,设置 head_sequence
  1091. 新消息的 parent_sequence 将指向 rewind 点,旧消息通过树结构自然脱离主路径。
  1092. Returns:
  1093. 下一个可用的 sequence 号
  1094. """
  1095. if not self.trace_store:
  1096. raise ValueError("trace_store required for rewind")
  1097. # 1. 加载所有 messages(用于 safe cutoff 和 max sequence)
  1098. all_messages = await self.trace_store.get_trace_messages(trace_id)
  1099. if not all_messages:
  1100. return 1
  1101. # 2. 找到安全截断点(确保不截断在 tool_call 和 tool response 之间)
  1102. cutoff = self._find_safe_cutoff(all_messages, after_sequence)
  1103. # 3. 快照并重建 GoalTree
  1104. if goal_tree:
  1105. # 获取截断点消息的 created_at 作为时间界限
  1106. cutoff_msg = None
  1107. for msg in all_messages:
  1108. if msg.sequence == cutoff:
  1109. cutoff_msg = msg
  1110. break
  1111. cutoff_time = cutoff_msg.created_at if cutoff_msg else datetime.now()
  1112. # 快照到 events(含 head_sequence 供前端感知分支切换)
  1113. await self.trace_store.append_event(trace_id, "rewind", {
  1114. "after_sequence": cutoff,
  1115. "head_sequence": cutoff,
  1116. "goal_tree_snapshot": goal_tree.to_dict(),
  1117. })
  1118. # 按时间重建干净的 GoalTree
  1119. new_tree = goal_tree.rebuild_for_rewind(cutoff_time)
  1120. await self.trace_store.update_goal_tree(trace_id, new_tree)
  1121. # 更新内存中的引用
  1122. goal_tree.goals = new_tree.goals
  1123. goal_tree.current_id = new_tree.current_id
  1124. # 4. 更新 head_sequence 到 rewind 点
  1125. await self.trace_store.update_trace(trace_id, head_sequence=cutoff)
  1126. # 5. 返回 next sequence(全局递增,不复用)
  1127. max_seq = max((m.sequence for m in all_messages), default=0)
  1128. return max_seq + 1
  1129. def _find_safe_cutoff(self, messages: List[Message], after_sequence: int) -> int:
  1130. """
  1131. 找到安全的截断点。
  1132. 如果 after_sequence 指向一条带 tool_calls 的 assistant message,
  1133. 则自动扩展到其所有对应的 tool response 之后。
  1134. """
  1135. cutoff = after_sequence
  1136. # 找到 after_sequence 对应的 message
  1137. target_msg = None
  1138. for msg in messages:
  1139. if msg.sequence == after_sequence:
  1140. target_msg = msg
  1141. break
  1142. if not target_msg:
  1143. return cutoff
  1144. # 如果是 assistant 且有 tool_calls,找到所有对应的 tool responses
  1145. if target_msg.role == "assistant":
  1146. content = target_msg.content
  1147. if isinstance(content, dict) and content.get("tool_calls"):
  1148. tool_call_ids = set()
  1149. for tc in content["tool_calls"]:
  1150. if isinstance(tc, dict) and tc.get("id"):
  1151. tool_call_ids.add(tc["id"])
  1152. # 找到这些 tool_call 对应的 tool messages
  1153. for msg in messages:
  1154. if (msg.role == "tool" and msg.tool_call_id
  1155. and msg.tool_call_id in tool_call_ids):
  1156. cutoff = max(cutoff, msg.sequence)
  1157. return cutoff
  1158. async def _heal_orphaned_tool_calls(
  1159. self,
  1160. messages: List[Message],
  1161. trace_id: str,
  1162. goal_tree: Optional[GoalTree],
  1163. sequence: int,
  1164. ) -> tuple:
  1165. """
  1166. 检测并修复消息历史中的 orphaned tool_calls。
  1167. 当 agent 被 stop/crash 中断时,可能有 assistant 的 tool_calls 没有对应的
  1168. tool results(包括多 tool_call 部分完成的情况)。直接发给 LLM 会导致 400。
  1169. 修复策略:为每个缺失的 tool_result 插入合成的"中断通知"消息,而非裁剪。
  1170. - 普通工具:简短中断提示
  1171. - agent/evaluate:包含 sub_trace_id、执行统计、continue_from 指引
  1172. 合成消息持久化到 store,确保幂等(下次续跑不再触发)。
  1173. Returns:
  1174. (healed_messages, next_sequence)
  1175. """
  1176. if not messages:
  1177. return messages, sequence
  1178. # 收集所有 tool_call IDs → (assistant_msg, tool_call_dict)
  1179. tc_map: Dict[str, tuple] = {}
  1180. result_ids: set = set()
  1181. for msg in messages:
  1182. if msg.role == "assistant":
  1183. content = msg.content
  1184. if isinstance(content, dict) and content.get("tool_calls"):
  1185. for tc in content["tool_calls"]:
  1186. tc_id = tc.get("id")
  1187. if tc_id:
  1188. tc_map[tc_id] = (msg, tc)
  1189. elif msg.role == "tool" and msg.tool_call_id:
  1190. result_ids.add(msg.tool_call_id)
  1191. orphaned_ids = [tc_id for tc_id in tc_map if tc_id not in result_ids]
  1192. if not orphaned_ids:
  1193. return messages, sequence
  1194. logger.info(
  1195. "检测到 %d 个 orphaned tool_calls,生成合成中断通知",
  1196. len(orphaned_ids),
  1197. )
  1198. healed = list(messages)
  1199. head_seq = messages[-1].sequence
  1200. for tc_id in orphaned_ids:
  1201. assistant_msg, tc = tc_map[tc_id]
  1202. tool_name = tc.get("function", {}).get("name", "unknown")
  1203. if tool_name in ("agent", "evaluate"):
  1204. result_text = self._build_agent_interrupted_result(
  1205. tc, goal_tree, assistant_msg,
  1206. )
  1207. else:
  1208. result_text = build_tool_interrupted_message(tool_name)
  1209. synthetic_msg = Message.create(
  1210. trace_id=trace_id,
  1211. role="tool",
  1212. sequence=sequence,
  1213. goal_id=assistant_msg.goal_id,
  1214. parent_sequence=head_seq,
  1215. tool_call_id=tc_id,
  1216. content={"tool_name": tool_name, "result": result_text},
  1217. )
  1218. if self.trace_store:
  1219. await self.trace_store.add_message(synthetic_msg)
  1220. healed.append(synthetic_msg)
  1221. head_seq = sequence
  1222. sequence += 1
  1223. # 更新 trace head/last sequence
  1224. if self.trace_store:
  1225. await self.trace_store.update_trace(
  1226. trace_id,
  1227. head_sequence=head_seq,
  1228. last_sequence=max(head_seq, sequence - 1),
  1229. )
  1230. return healed, sequence
  1231. def _build_agent_interrupted_result(
  1232. self,
  1233. tc: Dict,
  1234. goal_tree: Optional[GoalTree],
  1235. assistant_msg: Message,
  1236. ) -> str:
  1237. """为中断的 agent/evaluate 工具调用构建合成结果(对齐正常返回值格式)"""
  1238. args_str = tc.get("function", {}).get("arguments", "{}")
  1239. try:
  1240. args = json.loads(args_str) if isinstance(args_str, str) else args_str
  1241. except json.JSONDecodeError:
  1242. args = {}
  1243. task = args.get("task", "未知任务")
  1244. if isinstance(task, list):
  1245. task = "; ".join(task)
  1246. tool_name = tc.get("function", {}).get("name", "agent")
  1247. mode = "evaluate" if tool_name == "evaluate" else "delegate"
  1248. # 从 goal_tree 查找 sub_trace 信息
  1249. sub_trace_id = None
  1250. stats = None
  1251. if goal_tree and assistant_msg.goal_id:
  1252. goal = goal_tree.find(assistant_msg.goal_id)
  1253. if goal and goal.sub_trace_ids:
  1254. first = goal.sub_trace_ids[0]
  1255. if isinstance(first, dict):
  1256. sub_trace_id = first.get("trace_id")
  1257. elif isinstance(first, str):
  1258. sub_trace_id = first
  1259. if goal.cumulative_stats:
  1260. s = goal.cumulative_stats
  1261. if s.message_count > 0:
  1262. stats = {
  1263. "message_count": s.message_count,
  1264. "total_tokens": s.total_tokens,
  1265. "total_cost": round(s.total_cost, 4),
  1266. }
  1267. result: Dict[str, Any] = {
  1268. "mode": mode,
  1269. "status": "interrupted",
  1270. "summary": AGENT_INTERRUPTED_SUMMARY,
  1271. "task": task,
  1272. }
  1273. if sub_trace_id:
  1274. result["sub_trace_id"] = sub_trace_id
  1275. result["hint"] = build_agent_continue_hint(sub_trace_id)
  1276. if stats:
  1277. result["stats"] = stats
  1278. return json.dumps(result, ensure_ascii=False, indent=2)
  1279. # ===== 上下文注入 =====
  1280. def _build_context_injection(
  1281. self,
  1282. trace: Trace,
  1283. goal_tree: Optional[GoalTree],
  1284. ) -> str:
  1285. """构建周期性注入的上下文(GoalTree + Active Collaborators + Focus 提醒)"""
  1286. parts = []
  1287. # GoalTree
  1288. if goal_tree and goal_tree.goals:
  1289. parts.append(f"## Current Plan\n\n{goal_tree.to_prompt()}")
  1290. # 检测 focus 在有子节点的父目标上:提醒模型 focus 到具体子目标
  1291. if goal_tree.current_id:
  1292. children = goal_tree.get_children(goal_tree.current_id)
  1293. pending_children = [c for c in children if c.status in ("pending", "in_progress")]
  1294. if pending_children:
  1295. child_ids = ", ".join(
  1296. goal_tree._generate_display_id(c) for c in pending_children[:3]
  1297. )
  1298. parts.append(
  1299. f"**提醒**:当前焦点在父目标上,建议用 `goal(focus=\"...\")` "
  1300. f"切换到具体子目标(如 {child_ids})再执行。"
  1301. )
  1302. # Active Collaborators
  1303. collaborators = trace.context.get("collaborators", [])
  1304. if collaborators:
  1305. lines = ["## Active Collaborators"]
  1306. for c in collaborators:
  1307. status_str = c.get("status", "unknown")
  1308. ctype = c.get("type", "agent")
  1309. summary = c.get("summary", "")
  1310. name = c.get("name", "unnamed")
  1311. lines.append(f"- {name} [{ctype}, {status_str}]: {summary}")
  1312. parts.append("\n".join(lines))
  1313. return "\n\n".join(parts)
  1314. # ===== 辅助方法 =====
  1315. def _add_cache_control(
  1316. self,
  1317. messages: List[Dict],
  1318. model: str,
  1319. enable: bool
  1320. ) -> List[Dict]:
  1321. """
  1322. 为支持的模型添加 Prompt Caching 标记
  1323. 策略:固定位置 + 延迟查找
  1324. 1. system message 添加缓存(如果足够长)
  1325. 2. 固定位置缓存点(20, 40, 60, 80),确保每个缓存点间隔 >= 1024 tokens
  1326. 3. 最多使用 4 个缓存点(含 system)
  1327. Args:
  1328. messages: 原始消息列表
  1329. model: 模型名称
  1330. enable: 是否启用缓存
  1331. Returns:
  1332. 添加了 cache_control 的消息列表(深拷贝)
  1333. """
  1334. if not enable:
  1335. return messages
  1336. # 只对 Claude 模型启用
  1337. if "claude" not in model.lower():
  1338. return messages
  1339. # 深拷贝避免修改原始数据
  1340. import copy
  1341. messages = copy.deepcopy(messages)
  1342. # 策略 1: 为 system message 添加缓存
  1343. system_cached = False
  1344. for msg in messages:
  1345. if msg.get("role") == "system":
  1346. content = msg.get("content", "")
  1347. if isinstance(content, str) and len(content) > 1000:
  1348. msg["content"] = [{
  1349. "type": "text",
  1350. "text": content,
  1351. "cache_control": {"type": "ephemeral"}
  1352. }]
  1353. system_cached = True
  1354. logger.debug(f"[Cache] 为 system message 添加缓存标记 (len={len(content)})")
  1355. break
  1356. # 策略 2: 固定位置缓存点
  1357. CACHE_INTERVAL = 20
  1358. MAX_POINTS = 3 if system_cached else 4
  1359. MIN_TOKENS = 1024
  1360. AVG_TOKENS_PER_MSG = 70
  1361. total_msgs = len(messages)
  1362. if total_msgs == 0:
  1363. return messages
  1364. cache_positions = []
  1365. last_cache_pos = 0
  1366. for i in range(1, MAX_POINTS + 1):
  1367. target_pos = i * CACHE_INTERVAL - 1 # 19, 39, 59, 79
  1368. if target_pos >= total_msgs:
  1369. break
  1370. # 从目标位置开始查找合适的 user/assistant 消息
  1371. for j in range(target_pos, total_msgs):
  1372. msg = messages[j]
  1373. if msg.get("role") not in ("user", "assistant"):
  1374. continue
  1375. content = msg.get("content", "")
  1376. if not content:
  1377. continue
  1378. # 检查 content 是否非空
  1379. is_valid = False
  1380. if isinstance(content, str):
  1381. is_valid = len(content) > 0
  1382. elif isinstance(content, list):
  1383. is_valid = any(
  1384. isinstance(block, dict) and
  1385. block.get("type") == "text" and
  1386. len(block.get("text", "")) > 0
  1387. for block in content
  1388. )
  1389. if not is_valid:
  1390. continue
  1391. # 检查 token 距离
  1392. msg_count = j - last_cache_pos
  1393. estimated_tokens = msg_count * AVG_TOKENS_PER_MSG
  1394. if estimated_tokens >= MIN_TOKENS:
  1395. cache_positions.append(j)
  1396. last_cache_pos = j
  1397. logger.debug(f"[Cache] 在位置 {j} 添加缓存点 (估算 {estimated_tokens} tokens)")
  1398. break
  1399. # 应用缓存标记
  1400. for idx in cache_positions:
  1401. msg = messages[idx]
  1402. content = msg.get("content", "")
  1403. if isinstance(content, str):
  1404. msg["content"] = [{
  1405. "type": "text",
  1406. "text": content,
  1407. "cache_control": {"type": "ephemeral"}
  1408. }]
  1409. logger.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  1410. elif isinstance(content, list):
  1411. # 在最后一个 text block 添加 cache_control
  1412. for block in reversed(content):
  1413. if isinstance(block, dict) and block.get("type") == "text":
  1414. block["cache_control"] = {"type": "ephemeral"}
  1415. logger.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  1416. break
  1417. logger.debug(
  1418. f"[Cache] 总消息: {total_msgs}, "
  1419. f"缓存点: {len(cache_positions)} at {cache_positions}"
  1420. )
  1421. return messages
  1422. def _get_tool_schemas(self, tools: Optional[List[str]]) -> List[Dict]:
  1423. """
  1424. 获取工具 Schema
  1425. - tools=None: 使用 registry 中全部已注册工具(含内置 + 外部注册的)
  1426. - tools=["a", "b"]: 在 BUILTIN_TOOLS 基础上追加指定工具
  1427. """
  1428. if tools is None:
  1429. # 全部已注册工具
  1430. tool_names = self.tools.get_tool_names()
  1431. else:
  1432. # BUILTIN_TOOLS + 显式指定的额外工具
  1433. tool_names = BUILTIN_TOOLS.copy()
  1434. for t in tools:
  1435. if t not in tool_names:
  1436. tool_names.append(t)
  1437. return self.tools.get_schemas(tool_names)
  1438. # 默认 system prompt 前缀(当 config.system_prompt 和前端都未提供 system message 时使用)
  1439. # 注意:此常量已迁移到 agent.core.prompts,这里保留引用以保持向后兼容
  1440. async def _build_system_prompt(self, config: RunConfig, base_prompt: Optional[str] = None) -> Optional[str]:
  1441. """构建 system prompt(注入 skills)
  1442. 优先级:
  1443. 1. config.skills 显式指定 → 按名称过滤
  1444. 2. config.skills 为 None → 查 preset 的默认 skills 列表
  1445. 3. preset 也无 skills(None)→ 加载全部(向后兼容)
  1446. Args:
  1447. base_prompt: 已有 system 内容(来自消息或 config.system_prompt),
  1448. None 时使用 config.system_prompt
  1449. """
  1450. from agent.core.presets import AGENT_PRESETS
  1451. system_prompt = base_prompt if base_prompt is not None else config.system_prompt
  1452. # 确定要加载哪些 skills
  1453. skills_filter: Optional[List[str]] = config.skills
  1454. if skills_filter is None:
  1455. preset = AGENT_PRESETS.get(config.agent_type)
  1456. if preset is not None:
  1457. skills_filter = preset.skills # 可能仍为 None(加载全部)
  1458. # 加载并过滤
  1459. all_skills = load_skills_from_dir(self.skills_dir)
  1460. if skills_filter is not None:
  1461. skills = [s for s in all_skills if s.name in skills_filter]
  1462. else:
  1463. skills = all_skills
  1464. skills_text = self._format_skills(skills) if skills else ""
  1465. if system_prompt:
  1466. if skills_text:
  1467. system_prompt += f"\n\n## Skills\n{skills_text}"
  1468. else:
  1469. system_prompt = DEFAULT_SYSTEM_PREFIX
  1470. if skills_text:
  1471. system_prompt += f"\n\n## Skills\n{skills_text}"
  1472. return system_prompt
  1473. async def _generate_task_name(self, messages: List[Dict]) -> str:
  1474. """生成任务名称:优先使用 utility_llm,fallback 到文本截取"""
  1475. # 提取 messages 中的文本内容
  1476. text_parts = []
  1477. for msg in messages:
  1478. content = msg.get("content", "")
  1479. if isinstance(content, str):
  1480. text_parts.append(content)
  1481. elif isinstance(content, list):
  1482. for part in content:
  1483. if isinstance(part, dict) and part.get("type") == "text":
  1484. text_parts.append(part.get("text", ""))
  1485. raw_text = " ".join(text_parts).strip()
  1486. if not raw_text:
  1487. return TASK_NAME_FALLBACK
  1488. # 尝试使用 utility_llm 生成标题
  1489. if self.utility_llm_call:
  1490. try:
  1491. result = await self.utility_llm_call(
  1492. messages=[
  1493. {"role": "system", "content": TASK_NAME_GENERATION_SYSTEM_PROMPT},
  1494. {"role": "user", "content": raw_text[:2000]},
  1495. ],
  1496. model="gpt-4o-mini", # 使用便宜模型
  1497. )
  1498. title = result.get("content", "").strip()
  1499. if title and len(title) < 100:
  1500. return title
  1501. except Exception:
  1502. pass
  1503. # Fallback: 截取前 50 字符
  1504. return raw_text[:50] + ("..." if len(raw_text) > 50 else "")
  1505. def _format_skills(self, skills: List[Skill]) -> str:
  1506. if not skills:
  1507. return ""
  1508. return "\n\n".join(s.to_prompt_text() for s in skills)