runner.py 31 KB

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