runner.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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, insert_after 等)
  12. - Messages: OpenAI SDK 格式的任务消息
  13. """
  14. import json
  15. import logging
  16. import uuid
  17. from dataclasses import dataclass, field
  18. from datetime import datetime
  19. from typing import AsyncIterator, Optional, Dict, Any, List, Callable, Literal, Tuple, Union
  20. from agent.trace.models import Trace, Message
  21. from agent.trace.protocols import TraceStore
  22. from agent.trace.goal_models import GoalTree
  23. from agent.memory.models import Experience, Skill
  24. from agent.memory.protocols import MemoryStore, StateStore
  25. from agent.memory.skill_loader import load_skills_from_dir
  26. from agent.tools import ToolRegistry, get_tool_registry
  27. logger = logging.getLogger(__name__)
  28. # ===== 运行配置 =====
  29. @dataclass
  30. class RunConfig:
  31. """
  32. 运行参数 — 控制 Agent 如何执行
  33. 分为模型层参数(由上游 agent 或用户决定)和框架层参数(由系统注入)。
  34. """
  35. # --- 模型层参数 ---
  36. model: str = "gpt-4o"
  37. temperature: float = 0.3
  38. max_iterations: int = 200
  39. tools: Optional[List[str]] = None # None = 全部内置工具
  40. # --- 框架层参数 ---
  41. agent_type: str = "default"
  42. uid: Optional[str] = None
  43. system_prompt: Optional[str] = None # None = 从 skills 自动构建
  44. enable_memory: bool = True
  45. auto_execute_tools: bool = True
  46. name: Optional[str] = None # 显示名称(空则由 utility_llm 自动生成)
  47. # --- Trace 控制 ---
  48. trace_id: Optional[str] = None # None = 新建
  49. parent_trace_id: Optional[str] = None # 子 Agent 专用
  50. parent_goal_id: Optional[str] = None
  51. # --- 续跑控制 ---
  52. insert_after: Optional[int] = None # 回溯插入点(message sequence)
  53. # --- 额外 LLM 参数(传给 llm_call 的 **kwargs)---
  54. extra_llm_params: Dict[str, Any] = field(default_factory=dict)
  55. # 内置工具列表(始终自动加载)
  56. BUILTIN_TOOLS = [
  57. # 文件操作工具
  58. "read_file",
  59. "edit_file",
  60. "write_file",
  61. "glob_files",
  62. "grep_content",
  63. # 系统工具
  64. "bash_command",
  65. # 技能和目标管理
  66. "skill",
  67. "list_skills",
  68. "goal",
  69. "subagent",
  70. # 搜索工具
  71. "search_posts",
  72. "get_search_suggestions",
  73. # 沙箱工具
  74. "sandbox_create_environment",
  75. "sandbox_run_shell",
  76. "sandbox_rebuild_with_ports",
  77. "sandbox_destroy_environment",
  78. # 浏览器工具
  79. "browser_navigate_to_url",
  80. "browser_search_web",
  81. "browser_go_back",
  82. "browser_wait",
  83. "browser_click_element",
  84. "browser_input_text",
  85. "browser_send_keys",
  86. "browser_upload_file",
  87. "browser_scroll_page",
  88. "browser_find_text",
  89. "browser_screenshot",
  90. "browser_switch_tab",
  91. "browser_close_tab",
  92. "browser_get_dropdown_options",
  93. "browser_select_dropdown_option",
  94. "browser_extract_content",
  95. "browser_read_long_content",
  96. "browser_get_page_html",
  97. "browser_get_selector_map",
  98. "browser_get_visual_selector_map",
  99. "browser_evaluate",
  100. "browser_ensure_login_with_cookies",
  101. "browser_wait_for_user_action",
  102. "browser_done",
  103. "browser_export_cookies",
  104. "browser_load_cookies"
  105. ]
  106. # ===== 向后兼容 =====
  107. @dataclass
  108. class AgentConfig:
  109. """[向后兼容] Agent 配置,新代码请使用 RunConfig"""
  110. agent_type: str = "default"
  111. max_iterations: int = 200
  112. enable_memory: bool = True
  113. auto_execute_tools: bool = True
  114. @dataclass
  115. class CallResult:
  116. """单次调用结果"""
  117. reply: str
  118. tool_calls: Optional[List[Dict]] = None
  119. trace_id: Optional[str] = None
  120. step_id: Optional[str] = None
  121. tokens: Optional[Dict[str, int]] = None
  122. cost: float = 0.0
  123. # ===== 执行引擎 =====
  124. CONTEXT_INJECTION_INTERVAL = 10 # 每 N 轮注入一次 GoalTree + Collaborators
  125. class AgentRunner:
  126. """
  127. Agent 执行引擎
  128. 支持三种运行模式(通过 RunConfig 区分):
  129. 1. 新建:trace_id=None
  130. 2. 续跑:trace_id=已有ID, insert_after=None
  131. 3. 回溯:trace_id=已有ID, insert_after=N
  132. """
  133. def __init__(
  134. self,
  135. trace_store: Optional[TraceStore] = None,
  136. memory_store: Optional[MemoryStore] = None,
  137. state_store: Optional[StateStore] = None,
  138. tool_registry: Optional[ToolRegistry] = None,
  139. llm_call: Optional[Callable] = None,
  140. utility_llm_call: Optional[Callable] = None,
  141. config: Optional[AgentConfig] = None,
  142. skills_dir: Optional[str] = None,
  143. goal_tree: Optional[GoalTree] = None,
  144. debug: bool = False,
  145. ):
  146. """
  147. 初始化 AgentRunner
  148. Args:
  149. trace_store: Trace 存储
  150. memory_store: Memory 存储(可选)
  151. state_store: State 存储(可选)
  152. tool_registry: 工具注册表(默认使用全局注册表)
  153. llm_call: 主 LLM 调用函数
  154. utility_llm_call: 轻量 LLM(用于生成任务标题等),可选
  155. config: [向后兼容] AgentConfig
  156. skills_dir: Skills 目录路径
  157. goal_tree: 初始 GoalTree(可选)
  158. debug: 保留参数(已废弃)
  159. """
  160. self.trace_store = trace_store
  161. self.memory_store = memory_store
  162. self.state_store = state_store
  163. self.tools = tool_registry or get_tool_registry()
  164. self.llm_call = llm_call
  165. self.utility_llm_call = utility_llm_call
  166. self.config = config or AgentConfig()
  167. self.skills_dir = skills_dir
  168. self.goal_tree = goal_tree
  169. self.debug = debug
  170. # ===== 核心公开方法 =====
  171. async def run(
  172. self,
  173. messages: List[Dict],
  174. config: Optional[RunConfig] = None,
  175. ) -> AsyncIterator[Union[Trace, Message]]:
  176. """
  177. Agent 模式执行(核心方法)
  178. Args:
  179. messages: OpenAI SDK 格式的输入消息
  180. 新建: 初始任务消息 [{"role": "user", "content": "..."}]
  181. 续跑: 追加的新消息
  182. 回溯: 在插入点之后追加的消息
  183. config: 运行配置
  184. Yields:
  185. Union[Trace, Message]: Trace 对象(状态变化)或 Message 对象(执行过程)
  186. """
  187. if not self.llm_call:
  188. raise ValueError("llm_call function not provided")
  189. config = config or RunConfig()
  190. trace = None
  191. try:
  192. # Phase 1: PREPARE TRACE
  193. trace, goal_tree, sequence = await self._prepare_trace(messages, config)
  194. yield trace
  195. # Phase 2: BUILD HISTORY
  196. history, sequence, created_messages = await self._build_history(
  197. trace.trace_id, messages, goal_tree, config, sequence
  198. )
  199. for msg in created_messages:
  200. yield msg
  201. # Phase 3: AGENT LOOP
  202. async for event in self._agent_loop(trace, history, goal_tree, config, sequence):
  203. yield event
  204. except Exception as e:
  205. logger.error(f"Agent run failed: {e}")
  206. tid = config.trace_id or (trace.trace_id if trace else None)
  207. if self.trace_store and tid:
  208. await self.trace_store.update_trace(
  209. tid,
  210. status="failed",
  211. error_message=str(e),
  212. completed_at=datetime.now()
  213. )
  214. trace_obj = await self.trace_store.get_trace(tid)
  215. if trace_obj:
  216. yield trace_obj
  217. raise
  218. async def run_result(
  219. self,
  220. messages: List[Dict],
  221. config: Optional[RunConfig] = None,
  222. ) -> Dict[str, Any]:
  223. """
  224. 结果模式 — 消费 run(),返回结构化结果。
  225. 主要用于 subagent 工具内部。
  226. """
  227. last_assistant_text = ""
  228. final_trace: Optional[Trace] = None
  229. async for item in self.run(messages=messages, config=config):
  230. if isinstance(item, Message) and item.role == "assistant":
  231. content = item.content
  232. text = ""
  233. if isinstance(content, dict):
  234. text = content.get("text", "") or ""
  235. elif isinstance(content, str):
  236. text = content
  237. if text and text.strip():
  238. last_assistant_text = text
  239. elif isinstance(item, Trace):
  240. final_trace = item
  241. config = config or RunConfig()
  242. if not final_trace and config.trace_id and self.trace_store:
  243. final_trace = await self.trace_store.get_trace(config.trace_id)
  244. status = final_trace.status if final_trace else "unknown"
  245. error = final_trace.error_message if final_trace else None
  246. summary = last_assistant_text
  247. if not summary:
  248. status = "failed"
  249. error = error or "Agent 没有产生 assistant 文本结果"
  250. return {
  251. "status": status,
  252. "summary": summary,
  253. "trace_id": final_trace.trace_id if final_trace else config.trace_id,
  254. "error": error,
  255. "stats": {
  256. "total_messages": final_trace.total_messages if final_trace else 0,
  257. "total_tokens": final_trace.total_tokens if final_trace else 0,
  258. "total_cost": final_trace.total_cost if final_trace else 0.0,
  259. },
  260. }
  261. # ===== 单次调用(保留)=====
  262. async def call(
  263. self,
  264. messages: List[Dict],
  265. model: str = "gpt-4o",
  266. tools: Optional[List[str]] = None,
  267. uid: Optional[str] = None,
  268. trace: bool = True,
  269. **kwargs
  270. ) -> CallResult:
  271. """
  272. 单次 LLM 调用(无 Agent Loop)
  273. """
  274. if not self.llm_call:
  275. raise ValueError("llm_call function not provided")
  276. trace_id = None
  277. message_id = None
  278. tool_names = BUILTIN_TOOLS.copy()
  279. if tools:
  280. for tool in tools:
  281. if tool not in tool_names:
  282. tool_names.append(tool)
  283. tool_schemas = self.tools.get_schemas(tool_names)
  284. if trace and self.trace_store:
  285. trace_obj = Trace.create(mode="call", uid=uid, model=model, tools=tool_schemas, llm_params=kwargs)
  286. trace_id = await self.trace_store.create_trace(trace_obj)
  287. result = await self.llm_call(messages=messages, model=model, tools=tool_schemas, **kwargs)
  288. if trace and self.trace_store and trace_id:
  289. msg = Message.create(
  290. trace_id=trace_id, role="assistant", sequence=1, goal_id=None,
  291. content={"text": result.get("content", ""), "tool_calls": result.get("tool_calls")},
  292. prompt_tokens=result.get("prompt_tokens", 0),
  293. completion_tokens=result.get("completion_tokens", 0),
  294. finish_reason=result.get("finish_reason"),
  295. cost=result.get("cost", 0),
  296. )
  297. message_id = await self.trace_store.add_message(msg)
  298. await self.trace_store.update_trace(trace_id, status="completed", completed_at=datetime.now())
  299. return CallResult(
  300. reply=result.get("content", ""),
  301. tool_calls=result.get("tool_calls"),
  302. trace_id=trace_id,
  303. step_id=message_id,
  304. tokens={"prompt": result.get("prompt_tokens", 0), "completion": result.get("completion_tokens", 0)},
  305. cost=result.get("cost", 0)
  306. )
  307. # ===== Phase 1: PREPARE TRACE =====
  308. async def _prepare_trace(
  309. self,
  310. messages: List[Dict],
  311. config: RunConfig,
  312. ) -> Tuple[Trace, Optional[GoalTree], int]:
  313. """
  314. 准备 Trace:创建新的或加载已有的
  315. Returns:
  316. (trace, goal_tree, next_sequence)
  317. """
  318. if config.trace_id:
  319. return await self._prepare_existing_trace(config)
  320. else:
  321. return await self._prepare_new_trace(messages, config)
  322. async def _prepare_new_trace(
  323. self,
  324. messages: List[Dict],
  325. config: RunConfig,
  326. ) -> Tuple[Trace, Optional[GoalTree], int]:
  327. """创建新 Trace"""
  328. trace_id = str(uuid.uuid4())
  329. # 生成任务名称
  330. task_name = config.name or await self._generate_task_name(messages)
  331. # 准备工具 Schema
  332. tool_schemas = self._get_tool_schemas(config.tools)
  333. trace_obj = Trace(
  334. trace_id=trace_id,
  335. mode="agent",
  336. task=task_name,
  337. agent_type=config.agent_type,
  338. parent_trace_id=config.parent_trace_id,
  339. parent_goal_id=config.parent_goal_id,
  340. uid=config.uid,
  341. model=config.model,
  342. tools=tool_schemas,
  343. llm_params={"temperature": config.temperature, **config.extra_llm_params},
  344. status="running",
  345. )
  346. goal_tree = self.goal_tree or GoalTree(mission=task_name)
  347. if self.trace_store:
  348. await self.trace_store.create_trace(trace_obj)
  349. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  350. return trace_obj, goal_tree, 1
  351. async def _prepare_existing_trace(
  352. self,
  353. config: RunConfig,
  354. ) -> Tuple[Trace, Optional[GoalTree], int]:
  355. """加载已有 Trace(续跑或回溯)"""
  356. if not self.trace_store:
  357. raise ValueError("trace_store required for continue/rewind")
  358. trace_obj = await self.trace_store.get_trace(config.trace_id)
  359. if not trace_obj:
  360. raise ValueError(f"Trace not found: {config.trace_id}")
  361. goal_tree = await self.trace_store.get_goal_tree(config.trace_id)
  362. if config.insert_after is not None:
  363. # 回溯模式
  364. sequence = await self._rewind(config.trace_id, config.insert_after, goal_tree)
  365. else:
  366. # 续跑模式:从最大 sequence + 1 开始
  367. all_messages = await self.trace_store.get_trace_messages(
  368. config.trace_id, include_abandoned=True
  369. )
  370. sequence = max((m.sequence for m in all_messages), default=0) + 1
  371. # 状态置为 running
  372. await self.trace_store.update_trace(
  373. config.trace_id,
  374. status="running",
  375. completed_at=None,
  376. )
  377. trace_obj.status = "running"
  378. return trace_obj, goal_tree, sequence
  379. # ===== Phase 2: BUILD HISTORY =====
  380. async def _build_history(
  381. self,
  382. trace_id: str,
  383. new_messages: List[Dict],
  384. goal_tree: Optional[GoalTree],
  385. config: RunConfig,
  386. sequence: int,
  387. ) -> Tuple[List[Dict], int, List[Message]]:
  388. """
  389. 构建完整的 LLM 消息历史
  390. 1. 加载已有 active messages(续跑/回溯场景)
  391. 2. 构建 system prompt(新建时注入 skills/experiences)
  392. 3. 追加 input messages
  393. Returns:
  394. (history, next_sequence, created_messages)
  395. created_messages: 本次新创建并持久化的 Message 列表,供 run() yield 给调用方
  396. """
  397. history: List[Dict] = []
  398. created_messages: List[Message] = []
  399. # 1. 加载已有 messages
  400. if config.trace_id and self.trace_store:
  401. existing_messages = await self.trace_store.get_trace_messages(trace_id)
  402. history = [msg.to_llm_dict() for msg in existing_messages]
  403. # 2. 构建 system prompt(如果历史中没有 system message)
  404. has_system = any(m.get("role") == "system" for m in history)
  405. has_system_in_new = any(m.get("role") == "system" for m in new_messages)
  406. if not has_system and not has_system_in_new:
  407. system_prompt = await self._build_system_prompt(config)
  408. if system_prompt:
  409. history = [{"role": "system", "content": system_prompt}] + history
  410. if self.trace_store:
  411. system_msg = Message.create(
  412. trace_id=trace_id, role="system", sequence=sequence,
  413. goal_id=None, content=system_prompt,
  414. )
  415. await self.trace_store.add_message(system_msg)
  416. created_messages.append(system_msg)
  417. sequence += 1
  418. # 3. 追加新 messages
  419. for msg_dict in new_messages:
  420. history.append(msg_dict)
  421. if self.trace_store:
  422. stored_msg = Message.from_llm_dict(
  423. msg_dict, trace_id=trace_id, sequence=sequence, goal_id=None
  424. )
  425. await self.trace_store.add_message(stored_msg)
  426. created_messages.append(stored_msg)
  427. sequence += 1
  428. return history, sequence, created_messages
  429. # ===== Phase 3: AGENT LOOP =====
  430. async def _agent_loop(
  431. self,
  432. trace: Trace,
  433. history: List[Dict],
  434. goal_tree: Optional[GoalTree],
  435. config: RunConfig,
  436. sequence: int,
  437. ) -> AsyncIterator[Union[Trace, Message]]:
  438. """ReAct 循环"""
  439. trace_id = trace.trace_id
  440. tool_schemas = self._get_tool_schemas(config.tools)
  441. # 设置 goal_tree 到 goal 工具
  442. if goal_tree and self.trace_store:
  443. from agent.trace.goal_tool import set_goal_tree
  444. set_goal_tree(goal_tree)
  445. for iteration in range(config.max_iterations):
  446. # 构建 LLM messages(注入上下文)
  447. llm_messages = list(history)
  448. # 周期性注入 GoalTree + Collaborators
  449. if iteration % CONTEXT_INJECTION_INTERVAL == 0:
  450. context_injection = self._build_context_injection(trace, goal_tree)
  451. if context_injection:
  452. llm_messages.append({"role": "system", "content": context_injection})
  453. # 调用 LLM
  454. result = await self.llm_call(
  455. messages=llm_messages,
  456. model=config.model,
  457. tools=tool_schemas,
  458. temperature=config.temperature,
  459. **config.extra_llm_params,
  460. )
  461. response_content = result.get("content", "")
  462. tool_calls = result.get("tool_calls")
  463. finish_reason = result.get("finish_reason")
  464. prompt_tokens = result.get("prompt_tokens", 0)
  465. completion_tokens = result.get("completion_tokens", 0)
  466. step_cost = result.get("cost", 0)
  467. # 按需自动创建 root goal
  468. if goal_tree and not goal_tree.goals and tool_calls:
  469. has_goal_call = any(
  470. tc.get("function", {}).get("name") == "goal"
  471. for tc in tool_calls
  472. )
  473. if not has_goal_call:
  474. mission = goal_tree.mission
  475. root_desc = mission[:200] if len(mission) > 200 else mission
  476. goal_tree.add_goals(
  477. descriptions=[root_desc],
  478. reasons=["系统自动创建:Agent 未显式创建目标"],
  479. parent_id=None
  480. )
  481. goal_tree.focus(goal_tree.goals[0].id)
  482. if self.trace_store:
  483. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  484. await self.trace_store.add_goal(trace_id, goal_tree.goals[0])
  485. logger.info(f"自动创建 root goal: {goal_tree.goals[0].id}")
  486. # 获取当前 goal_id
  487. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  488. # 记录 assistant Message
  489. assistant_msg = Message.create(
  490. trace_id=trace_id,
  491. role="assistant",
  492. sequence=sequence,
  493. goal_id=current_goal_id,
  494. content={"text": response_content, "tool_calls": tool_calls},
  495. prompt_tokens=prompt_tokens,
  496. completion_tokens=completion_tokens,
  497. finish_reason=finish_reason,
  498. cost=step_cost,
  499. )
  500. if self.trace_store:
  501. await self.trace_store.add_message(assistant_msg)
  502. yield assistant_msg
  503. sequence += 1
  504. # 处理工具调用
  505. if tool_calls and config.auto_execute_tools:
  506. history.append({
  507. "role": "assistant",
  508. "content": response_content,
  509. "tool_calls": tool_calls,
  510. })
  511. for tc in tool_calls:
  512. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  513. tool_name = tc["function"]["name"]
  514. tool_args = tc["function"]["arguments"]
  515. if isinstance(tool_args, str):
  516. tool_args = json.loads(tool_args) if tool_args.strip() else {}
  517. elif tool_args is None:
  518. tool_args = {}
  519. tool_result = await self.tools.execute(
  520. tool_name,
  521. tool_args,
  522. uid=config.uid or "",
  523. context={
  524. "store": self.trace_store,
  525. "trace_id": trace_id,
  526. "goal_id": current_goal_id,
  527. "runner": self,
  528. }
  529. )
  530. # --- 支持多模态工具反馈 ---
  531. # execute() 返回 dict{"text","images"} 或 str
  532. if isinstance(tool_result, dict) and tool_result.get("images"):
  533. tool_result_text = tool_result["text"]
  534. # 构建多模态消息格式
  535. tool_content_for_llm = [{"type": "text", "text": tool_result_text}]
  536. for img in tool_result["images"]:
  537. if img.get("type") == "base64" and img.get("data"):
  538. media_type = img.get("media_type", "image/png")
  539. tool_content_for_llm.append({
  540. "type": "image_url",
  541. "image_url": {
  542. "url": f"data:{media_type};base64,{img['data']}"
  543. }
  544. })
  545. img_count = len(tool_content_for_llm) - 1 # 减去 text 块
  546. print(f"[Runner] 多模态工具反馈: tool={tool_name}, images={img_count}, text_len={len(tool_result_text)}")
  547. else:
  548. tool_result_text = str(tool_result)
  549. tool_content_for_llm = tool_result_text
  550. tool_msg = Message.create(
  551. trace_id=trace_id,
  552. role="tool",
  553. sequence=sequence,
  554. goal_id=current_goal_id,
  555. tool_call_id=tc["id"],
  556. content={"tool_name": tool_name, "result": tool_result_text},
  557. )
  558. if self.trace_store:
  559. await self.trace_store.add_message(tool_msg)
  560. # 截图单独存为同名 PNG 文件
  561. if isinstance(tool_result, dict) and tool_result.get("images"):
  562. import base64 as b64mod
  563. for img in tool_result["images"]:
  564. if img.get("data"):
  565. png_path = self.trace_store._get_messages_dir(trace_id) / f"{tool_msg.message_id}.png"
  566. png_path.write_bytes(b64mod.b64decode(img["data"]))
  567. print(f"[Runner] 截图已保存: {png_path.name}")
  568. break # 只存第一张
  569. yield tool_msg
  570. sequence += 1
  571. history.append({
  572. "role": "tool",
  573. "tool_call_id": tc["id"],
  574. "name": tool_name,
  575. "content": tool_content_for_llm, # 这里传入 list 即可触发模型的视觉能力
  576. })
  577. # ------------------------------------------
  578. continue # 继续循环
  579. # 无工具调用,任务完成
  580. break
  581. # 完成 Trace
  582. if self.trace_store:
  583. await self.trace_store.update_trace(
  584. trace_id,
  585. status="completed",
  586. completed_at=datetime.now(),
  587. )
  588. trace_obj = await self.trace_store.get_trace(trace_id)
  589. if trace_obj:
  590. yield trace_obj
  591. # ===== 回溯(Rewind)=====
  592. async def _rewind(
  593. self,
  594. trace_id: str,
  595. insert_after: int,
  596. goal_tree: Optional[GoalTree],
  597. ) -> int:
  598. """
  599. 执行回溯:标记 insert_after 之后的 messages 和 goals 为 abandoned
  600. Returns:
  601. 下一个可用的 sequence 号
  602. """
  603. if not self.trace_store:
  604. raise ValueError("trace_store required for rewind")
  605. # 1. 加载所有 messages(含已 abandoned 的)
  606. all_messages = await self.trace_store.get_trace_messages(
  607. trace_id, include_abandoned=True
  608. )
  609. if not all_messages:
  610. return 1
  611. # 2. 找到安全截断点(确保不截断在 tool_call 和 tool response 之间)
  612. cutoff = self._find_safe_cutoff(all_messages, insert_after)
  613. # 3. 批量标记 messages 为 abandoned
  614. abandoned_ids = await self.trace_store.abandon_messages_after(trace_id, cutoff)
  615. # 4. 处理 Goals
  616. if goal_tree:
  617. active_messages = [m for m in all_messages if m.sequence <= cutoff]
  618. active_goal_ids = {m.goal_id for m in active_messages if m.goal_id}
  619. for goal in goal_tree.goals:
  620. if goal.status == "abandoned":
  621. continue # 已 abandoned,跳过
  622. if goal.status == "completed" and goal.id in active_goal_ids:
  623. continue # 已完成且有截断点之前的 messages → 保留
  624. # 其余全部 abandon(含无 active messages 的 completed goal)
  625. goal.status = "abandoned"
  626. goal.summary = "回溯导致放弃"
  627. # 重置 current_id
  628. goal_tree._current_id = None
  629. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  630. # 5. 记录 rewind 事件
  631. abandoned_sequences = [
  632. m.sequence for m in all_messages
  633. if m.sequence > cutoff and m.status != "abandoned" # 本次新 abandon 的
  634. ]
  635. await self.trace_store.append_event(trace_id, "rewind", {
  636. "insert_after_sequence": cutoff,
  637. "abandoned_message_count": len(abandoned_ids),
  638. "abandoned_sequences": abandoned_sequences[:20], # 只记前 20 条
  639. })
  640. # 6. 返回 next sequence
  641. max_seq = max((m.sequence for m in all_messages), default=0)
  642. return max_seq + 1
  643. def _find_safe_cutoff(self, messages: List[Message], insert_after: int) -> int:
  644. """
  645. 找到安全的截断点。
  646. 如果 insert_after 指向一条带 tool_calls 的 assistant message,
  647. 则自动扩展到其所有对应的 tool response 之后。
  648. """
  649. cutoff = insert_after
  650. # 找到 insert_after 对应的 message
  651. target_msg = None
  652. for msg in messages:
  653. if msg.sequence == insert_after:
  654. target_msg = msg
  655. break
  656. if not target_msg:
  657. return cutoff
  658. # 如果是 assistant 且有 tool_calls,找到所有对应的 tool responses
  659. if target_msg.role == "assistant":
  660. content = target_msg.content
  661. if isinstance(content, dict) and content.get("tool_calls"):
  662. tool_call_ids = set()
  663. for tc in content["tool_calls"]:
  664. if isinstance(tc, dict) and tc.get("id"):
  665. tool_call_ids.add(tc["id"])
  666. # 找到这些 tool_call 对应的 tool messages
  667. for msg in messages:
  668. if (msg.role == "tool" and msg.tool_call_id
  669. and msg.tool_call_id in tool_call_ids):
  670. cutoff = max(cutoff, msg.sequence)
  671. return cutoff
  672. # ===== 上下文注入 =====
  673. def _build_context_injection(
  674. self,
  675. trace: Trace,
  676. goal_tree: Optional[GoalTree],
  677. ) -> str:
  678. """构建周期性注入的上下文(GoalTree + Active Collaborators)"""
  679. parts = []
  680. # GoalTree
  681. if goal_tree and goal_tree.goals:
  682. parts.append(f"## Current Plan\n\n{goal_tree.to_prompt()}")
  683. # Active Collaborators
  684. collaborators = trace.context.get("collaborators", [])
  685. if collaborators:
  686. lines = ["## Active Collaborators"]
  687. for c in collaborators:
  688. status_str = c.get("status", "unknown")
  689. ctype = c.get("type", "agent")
  690. summary = c.get("summary", "")
  691. name = c.get("name", "unnamed")
  692. lines.append(f"- {name} [{ctype}, {status_str}]: {summary}")
  693. parts.append("\n".join(lines))
  694. return "\n\n".join(parts)
  695. # ===== 辅助方法 =====
  696. def _get_tool_schemas(self, tools: Optional[List[str]]) -> List[Dict]:
  697. """获取工具 Schema"""
  698. tool_names = BUILTIN_TOOLS.copy()
  699. if tools:
  700. for tool in tools:
  701. if tool not in tool_names:
  702. tool_names.append(tool)
  703. return self.tools.get_schemas(tool_names)
  704. async def _build_system_prompt(self, config: RunConfig) -> Optional[str]:
  705. """构建 system prompt(注入 skills 和 experiences)"""
  706. system_prompt = config.system_prompt
  707. # 加载 Skills
  708. skills_text = ""
  709. skills = load_skills_from_dir(self.skills_dir)
  710. if skills:
  711. skills_text = self._format_skills(skills)
  712. # 加载 Experiences
  713. experiences_text = ""
  714. if config.enable_memory and self.memory_store:
  715. scope = f"agent:{config.agent_type}"
  716. # 从 messages 提取文本作为查询
  717. experiences = await self.memory_store.search_experiences(scope, system_prompt or "")
  718. experiences_text = self._format_experiences(experiences)
  719. # 拼装
  720. if system_prompt:
  721. if skills_text:
  722. system_prompt += f"\n\n## Skills\n{skills_text}"
  723. if experiences_text:
  724. system_prompt += f"\n\n## 相关经验\n{experiences_text}"
  725. elif skills_text or experiences_text:
  726. parts = []
  727. if skills_text:
  728. parts.append(f"## Skills\n{skills_text}")
  729. if experiences_text:
  730. parts.append(f"## 相关经验\n{experiences_text}")
  731. system_prompt = "\n\n".join(parts)
  732. return system_prompt
  733. async def _generate_task_name(self, messages: List[Dict]) -> str:
  734. """生成任务名称:优先使用 utility_llm,fallback 到文本截取"""
  735. # 提取 messages 中的文本内容
  736. text_parts = []
  737. for msg in messages:
  738. content = msg.get("content", "")
  739. if isinstance(content, str):
  740. text_parts.append(content)
  741. elif isinstance(content, list):
  742. for part in content:
  743. if isinstance(part, dict) and part.get("type") == "text":
  744. text_parts.append(part.get("text", ""))
  745. raw_text = " ".join(text_parts).strip()
  746. if not raw_text:
  747. return "未命名任务"
  748. # 尝试使用 utility_llm 生成标题
  749. if self.utility_llm_call:
  750. try:
  751. result = await self.utility_llm_call(
  752. messages=[
  753. {"role": "system", "content": "用中文为以下任务生成一个简短标题(10-30字),只输出标题本身:"},
  754. {"role": "user", "content": raw_text[:2000]},
  755. ],
  756. model="gpt-4o-mini", # 使用便宜模型
  757. )
  758. title = result.get("content", "").strip()
  759. if title and len(title) < 100:
  760. return title
  761. except Exception:
  762. pass
  763. # Fallback: 截取前 50 字符
  764. return raw_text[:50] + ("..." if len(raw_text) > 50 else "")
  765. def _format_skills(self, skills: List[Skill]) -> str:
  766. if not skills:
  767. return ""
  768. return "\n\n".join(s.to_prompt_text() for s in skills)
  769. def _format_experiences(self, experiences: List[Experience]) -> str:
  770. if not experiences:
  771. return ""
  772. return "\n".join(f"- {e.to_prompt_text()}" for e in experiences)