runner.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. """
  2. Agent Runner - Agent 执行引擎
  3. 核心职责:
  4. 1. 执行 Agent 任务(循环调用 LLM + 工具)
  5. 2. 记录执行图(Trace + Steps)
  6. 3. 检索和注入记忆(Experience + Skill)
  7. 4. 管理执行计划(Goal Tree)
  8. 5. 收集反馈,提取经验
  9. """
  10. import logging
  11. from dataclasses import field
  12. from datetime import datetime
  13. from typing import AsyncIterator, Optional, Dict, Any, List, Callable, Literal, Union
  14. from agent.core.config import AgentConfig, CallResult
  15. from agent.execution import Trace, Step, TraceStore
  16. from agent.goal import GoalTree, goal_tool, compress_messages_for_goal
  17. from agent.memory.models import Experience, Skill
  18. from agent.memory.protocols import MemoryStore, StateStore
  19. from agent.memory.skill_loader import load_skills_from_dir
  20. from agent.tools import ToolRegistry, get_tool_registry
  21. logger = logging.getLogger(__name__)
  22. # 内置工具列表(始终自动加载)
  23. BUILTIN_TOOLS = [
  24. "read_file",
  25. "edit_file",
  26. "write_file",
  27. "glob_files",
  28. "grep_content",
  29. "bash_command",
  30. "skill",
  31. "list_skills",
  32. "goal",
  33. ]
  34. class AgentRunner:
  35. """
  36. Agent 执行引擎
  37. 支持两种模式:
  38. 1. call(): 单次 LLM 调用(简洁 API)
  39. 2. run(): Agent 模式(循环 + 记忆 + 追踪)
  40. """
  41. def __init__(
  42. self,
  43. trace_store: Optional[TraceStore] = None,
  44. memory_store: Optional[MemoryStore] = None,
  45. state_store: Optional[StateStore] = None,
  46. tool_registry: Optional[ToolRegistry] = None,
  47. llm_call: Optional[Callable] = None,
  48. config: Optional[AgentConfig] = None,
  49. skills_dir: Optional[str] = None,
  50. goal_tree: Optional[GoalTree] = None,
  51. debug: bool = False,
  52. ):
  53. """
  54. 初始化 AgentRunner
  55. Args:
  56. trace_store: Trace 存储(可选,不提供则不记录)
  57. memory_store: Memory 存储(可选,不提供则不使用记忆)
  58. state_store: State 存储(可选,用于任务状态)
  59. tool_registry: 工具注册表(可选,默认使用全局注册表)
  60. llm_call: LLM 调用函数(必须提供,用于实际调用 LLM)
  61. config: Agent 配置
  62. skills_dir: Skills 目录路径(可选,不提供则不加载 skills)
  63. goal_tree: 执行计划(可选,不提供则在运行时按需创建)
  64. debug: 保留参数(已废弃,请使用 API Server 可视化)
  65. """
  66. self.trace_store = trace_store
  67. self.memory_store = memory_store
  68. self.state_store = state_store
  69. self.tools = tool_registry or get_tool_registry()
  70. self.llm_call = llm_call
  71. self.config = config or AgentConfig()
  72. self.skills_dir = skills_dir
  73. self.goal_tree = goal_tree
  74. self.debug = debug
  75. def _generate_id(self) -> str:
  76. """生成唯一 ID"""
  77. import uuid
  78. return str(uuid.uuid4())
  79. async def _dump_debug(self, trace_id: str) -> None:
  80. """Debug 模式(已废弃 - 使用 API 可视化替代)"""
  81. # 不再自动生成 tree.txt/tree.md/tree.json
  82. # 请使用 API Server 进行可视化:python3 api_server.py
  83. pass
  84. # ===== 单次调用 =====
  85. async def call(
  86. self,
  87. messages: List[Dict],
  88. model: str = "gpt-4o",
  89. tools: Optional[List[str]] = None,
  90. uid: Optional[str] = None,
  91. trace: bool = True,
  92. **kwargs
  93. ) -> CallResult:
  94. """
  95. 单次 LLM 调用
  96. Args:
  97. messages: 消息列表
  98. model: 模型名称
  99. tools: 工具名称列表
  100. uid: 用户 ID
  101. trace: 是否记录 Trace
  102. **kwargs: 其他参数传递给 LLM
  103. Returns:
  104. CallResult
  105. """
  106. if not self.llm_call:
  107. raise ValueError("llm_call function not provided")
  108. trace_id = None
  109. step_id = None
  110. # 创建 Trace
  111. if trace and self.trace_store:
  112. trace_obj = Trace.create(
  113. mode="call",
  114. uid=uid,
  115. context={"model": model}
  116. )
  117. trace_id = await self.trace_store.create_trace(trace_obj)
  118. # 准备工具 Schema
  119. # 合并内置工具 + 用户指定工具
  120. tool_names = BUILTIN_TOOLS.copy()
  121. if tools:
  122. # 添加用户指定的工具(去重)
  123. for tool in tools:
  124. if tool not in tool_names:
  125. tool_names.append(tool)
  126. tool_schemas = self.tools.get_schemas(tool_names)
  127. # 调用 LLM
  128. result = await self.llm_call(
  129. messages=messages,
  130. model=model,
  131. tools=tool_schemas,
  132. **kwargs
  133. )
  134. # 记录 Step
  135. if trace and self.trace_store and trace_id:
  136. step = Step.create(
  137. trace_id=trace_id,
  138. step_type="thought",
  139. sequence=0,
  140. status="completed",
  141. description=f"LLM 调用 ({model})",
  142. data={
  143. "messages": messages,
  144. "response": result.get("content", ""),
  145. "model": model,
  146. "tools": tool_schemas, # 记录传给模型的 tools schema
  147. "tool_calls": result.get("tool_calls"),
  148. },
  149. tokens=result.get("prompt_tokens", 0) + result.get("completion_tokens", 0),
  150. cost=result.get("cost", 0),
  151. )
  152. step_id = await self.trace_store.add_step(step)
  153. await self._dump_debug(trace_id)
  154. # 完成 Trace
  155. await self.trace_store.update_trace(
  156. trace_id,
  157. status="completed",
  158. completed_at=datetime.now(),
  159. total_tokens=result.get("prompt_tokens", 0) + result.get("completion_tokens", 0),
  160. total_cost=result.get("cost", 0)
  161. )
  162. return CallResult(
  163. reply=result.get("content", ""),
  164. tool_calls=result.get("tool_calls"),
  165. trace_id=trace_id,
  166. step_id=step_id,
  167. tokens={
  168. "prompt": result.get("prompt_tokens", 0),
  169. "completion": result.get("completion_tokens", 0),
  170. },
  171. cost=result.get("cost", 0)
  172. )
  173. # ===== Agent 模式 =====
  174. async def run(
  175. self,
  176. task: str,
  177. messages: Optional[List[Dict]] = None,
  178. system_prompt: Optional[str] = None,
  179. model: str = "gpt-4o",
  180. tools: Optional[List[str]] = None,
  181. agent_type: Optional[str] = None,
  182. uid: Optional[str] = None,
  183. max_iterations: Optional[int] = None,
  184. enable_memory: Optional[bool] = None,
  185. auto_execute_tools: Optional[bool] = None,
  186. **kwargs
  187. ) -> AsyncIterator[Union[Trace, Step]]:
  188. """
  189. Agent 模式执行
  190. Args:
  191. task: 任务描述
  192. messages: 初始消息(可选)
  193. system_prompt: 系统提示(可选)
  194. model: 模型名称
  195. tools: 工具名称列表
  196. agent_type: Agent 类型
  197. uid: 用户 ID
  198. max_iterations: 最大迭代次数
  199. enable_memory: 是否启用记忆
  200. auto_execute_tools: 是否自动执行工具
  201. **kwargs: 其他参数
  202. Yields:
  203. Union[Trace, Step]: Trace 对象(状态变化)或 Step 对象(执行过程)
  204. """
  205. if not self.llm_call:
  206. raise ValueError("llm_call function not provided")
  207. # 使用配置默认值
  208. agent_type = agent_type or self.config.agent_type
  209. max_iterations = max_iterations or self.config.max_iterations
  210. enable_memory = enable_memory if enable_memory is not None else self.config.enable_memory
  211. auto_execute_tools = auto_execute_tools if auto_execute_tools is not None else self.config.auto_execute_tools
  212. # 创建 Trace
  213. trace_id = self._generate_id()
  214. trace_obj = None
  215. if self.trace_store:
  216. trace_obj = Trace(
  217. trace_id=trace_id,
  218. mode="agent",
  219. task=task,
  220. agent_type=agent_type,
  221. uid=uid,
  222. context={"model": model, **kwargs}
  223. )
  224. await self.trace_store.create_trace(trace_obj)
  225. # 返回 Trace(表示开始)
  226. yield trace_obj
  227. try:
  228. # 加载记忆(Experience 和 Skill)
  229. experiences_text = ""
  230. skills_text = ""
  231. if enable_memory and self.memory_store:
  232. scope = f"agent:{agent_type}"
  233. experiences = await self.memory_store.search_experiences(scope, task)
  234. experiences_text = self._format_experiences(experiences)
  235. # 记录 memory_read Step
  236. if self.trace_store:
  237. mem_step = Step.create(
  238. trace_id=trace_id,
  239. step_type="memory_read",
  240. sequence=0,
  241. status="completed",
  242. description=f"加载 {len(experiences)} 条经验",
  243. data={
  244. "experiences_count": len(experiences),
  245. "experiences": [e.to_dict() for e in experiences],
  246. }
  247. )
  248. await self.trace_store.add_step(mem_step)
  249. await self._dump_debug(trace_id)
  250. # 返回 Step(表示记忆加载完成)
  251. yield mem_step
  252. # 加载 Skills(内置 + 用户自定义)
  253. # load_skills_from_dir() 会自动加载 agent/skills/ 中的内置 skills
  254. # 如果提供了 skills_dir,会额外加载用户自定义的 skills
  255. skills = load_skills_from_dir(self.skills_dir)
  256. if skills:
  257. skills_text = self._format_skills(skills)
  258. if self.skills_dir:
  259. logger.info(f"加载 {len(skills)} 个 skills (内置 + 自定义: {self.skills_dir})")
  260. else:
  261. logger.info(f"加载 {len(skills)} 个内置 skills")
  262. # 构建初始消息
  263. if messages is None:
  264. messages = []
  265. if system_prompt:
  266. # 注入记忆和 skills 到 system prompt
  267. full_system = system_prompt
  268. if skills_text:
  269. full_system += f"\n\n## Skills\n{skills_text}"
  270. if experiences_text:
  271. full_system += f"\n\n## 相关经验\n{experiences_text}"
  272. messages = [{"role": "system", "content": full_system}] + messages
  273. # 添加任务描述
  274. messages.append({"role": "user", "content": task})
  275. # 初始化 GoalTree
  276. goal_tree = self.goal_tree or GoalTree(mission=task)
  277. # 准备工具 Schema
  278. # 合并内置工具 + 用户指定工具
  279. tool_names = BUILTIN_TOOLS.copy()
  280. if tools:
  281. # 添加用户指定的工具(去重)
  282. for tool in tools:
  283. if tool not in tool_names:
  284. tool_names.append(tool)
  285. tool_schemas = self.tools.get_schemas(tool_names)
  286. # 执行循环
  287. current_goal_id = None # 当前焦点 goal
  288. sequence = 1
  289. total_tokens = 0
  290. total_cost = 0.0
  291. for iteration in range(max_iterations):
  292. # 注入当前计划到 messages(如果有 goals)
  293. llm_messages = list(messages)
  294. if goal_tree.goals:
  295. plan_text = f"\n## Current Plan\n\n{goal_tree.to_prompt()}"
  296. # 作为最后一条 system 消息注入
  297. llm_messages.append({"role": "system", "content": plan_text})
  298. # 调用 LLM
  299. result = await self.llm_call(
  300. messages=llm_messages,
  301. model=model,
  302. tools=tool_schemas,
  303. **kwargs
  304. )
  305. response_content = result.get("content", "")
  306. tool_calls = result.get("tool_calls")
  307. step_tokens = result.get("prompt_tokens", 0) + result.get("completion_tokens", 0)
  308. step_cost = result.get("cost", 0)
  309. total_tokens += step_tokens
  310. total_cost += step_cost
  311. # 记录 LLM 调用 Step
  312. llm_step_id = self._generate_id()
  313. llm_step = None
  314. if self.trace_store:
  315. # 推断 step_type
  316. step_type = "thought"
  317. if tool_calls:
  318. step_type = "thought" # 有工具调用的思考
  319. elif not tool_calls and iteration > 0:
  320. step_type = "response" # 无工具调用,可能是最终回复
  321. llm_step = Step(
  322. step_id=llm_step_id,
  323. trace_id=trace_id,
  324. step_type=step_type,
  325. status="completed",
  326. sequence=sequence,
  327. parent_id=current_goal_id,
  328. description=response_content[:100] + "..." if len(response_content) > 100 else response_content,
  329. data={
  330. "messages": messages, # 记录完整的 messages(包含 system prompt)
  331. "content": response_content,
  332. "model": model,
  333. "tools": tool_schemas, # 记录传给模型的 tools schema
  334. "tool_calls": tool_calls,
  335. },
  336. tokens=step_tokens,
  337. cost=step_cost,
  338. )
  339. await self.trace_store.add_step(llm_step)
  340. await self._dump_debug(trace_id)
  341. # 返回 Step(LLM 思考完成)
  342. yield llm_step
  343. sequence += 1
  344. # 处理工具调用
  345. if tool_calls and auto_execute_tools:
  346. # 检查是否需要用户确认
  347. if self.tools.check_confirmation_required(tool_calls):
  348. # 创建等待确认的 Step
  349. await_step = Step.create(
  350. trace_id=trace_id,
  351. step_type="action",
  352. status="awaiting_approval",
  353. sequence=sequence,
  354. parent_id=llm_step_id,
  355. description="等待用户确认工具调用",
  356. data={
  357. "tool_calls": tool_calls,
  358. "confirmation_flags": self.tools.get_confirmation_flags(tool_calls),
  359. "editable_params": self.tools.get_editable_params_map(tool_calls)
  360. }
  361. )
  362. if self.trace_store:
  363. await self.trace_store.add_step(await_step)
  364. await self._dump_debug(trace_id)
  365. yield await_step
  366. # TODO: 等待用户确认
  367. break
  368. # 执行工具
  369. messages.append({
  370. "role": "assistant",
  371. "content": response_content,
  372. "tool_calls": tool_calls,
  373. "goal_id": goal_tree.current_id,
  374. })
  375. for tc in tool_calls:
  376. tool_name = tc["function"]["name"]
  377. tool_args = tc["function"]["arguments"]
  378. if isinstance(tool_args, str):
  379. import json
  380. tool_args = json.loads(tool_args)
  381. # 拦截 goal 工具调用
  382. if tool_name == "goal":
  383. prev_goal_id = goal_tree.current_id
  384. prev_goal = goal_tree.get_current()
  385. tool_result = goal_tool(tree=goal_tree, **tool_args)
  386. # 如果 done/abandon 触发了压缩
  387. if prev_goal_id and prev_goal:
  388. if prev_goal.status in ("completed", "abandoned") and prev_goal.summary:
  389. messages = compress_messages_for_goal(
  390. messages, prev_goal_id, prev_goal.summary
  391. )
  392. else:
  393. # 执行普通工具
  394. tool_result = await self.tools.execute(
  395. tool_name,
  396. tool_args,
  397. uid=uid or ""
  398. )
  399. # 记录 action Step
  400. action_step_id = self._generate_id()
  401. action_step = None
  402. if self.trace_store:
  403. action_step = Step(
  404. step_id=action_step_id,
  405. trace_id=trace_id,
  406. step_type="action",
  407. status="completed",
  408. sequence=sequence,
  409. parent_id=llm_step_id,
  410. description=f"{tool_name}({', '.join(f'{k}={v}' for k, v in list(tool_args.items())[:2])})",
  411. data={
  412. "tool_name": tool_name,
  413. "arguments": tool_args,
  414. }
  415. )
  416. await self.trace_store.add_step(action_step)
  417. await self._dump_debug(trace_id)
  418. # 返回 Step(工具调用)
  419. yield action_step
  420. sequence += 1
  421. # 记录 result Step
  422. result_step_id = self._generate_id()
  423. result_step = None
  424. if self.trace_store:
  425. result_step = Step(
  426. step_id=result_step_id,
  427. trace_id=trace_id,
  428. step_type="result",
  429. status="completed",
  430. sequence=sequence,
  431. parent_id=action_step_id,
  432. description=str(tool_result)[:100] if tool_result else "",
  433. data={
  434. "tool_name": tool_name,
  435. "output": tool_result,
  436. }
  437. )
  438. await self.trace_store.add_step(result_step)
  439. await self._dump_debug(trace_id)
  440. # 返回 Step(工具结果)
  441. yield result_step
  442. sequence += 1
  443. # 添加到消息(Gemini 需要 name 字段!)
  444. messages.append({
  445. "role": "tool",
  446. "tool_call_id": tc["id"],
  447. "name": tool_name,
  448. "content": tool_result,
  449. "goal_id": goal_tree.current_id,
  450. })
  451. continue # 继续循环
  452. # 无工具调用,任务完成
  453. # 记录 response Step
  454. response_step_id = self._generate_id()
  455. response_step = None
  456. if self.trace_store:
  457. response_step = Step(
  458. step_id=response_step_id,
  459. trace_id=trace_id,
  460. step_type="response",
  461. status="completed",
  462. sequence=sequence,
  463. parent_id=current_goal_id,
  464. description=response_content[:100] + "..." if len(response_content) > 100 else response_content,
  465. data={
  466. "content": response_content,
  467. "is_final": True
  468. }
  469. )
  470. await self.trace_store.add_step(response_step)
  471. await self._dump_debug(trace_id)
  472. # 返回 Step(最终回复)
  473. yield response_step
  474. break
  475. # 完成 Trace
  476. if self.trace_store:
  477. await self.trace_store.update_trace(
  478. trace_id,
  479. status="completed",
  480. completed_at=datetime.now(),
  481. total_tokens=total_tokens,
  482. total_cost=total_cost
  483. )
  484. # 重新获取更新后的 Trace 并返回
  485. trace_obj = await self.trace_store.get_trace(trace_id)
  486. if trace_obj:
  487. yield trace_obj
  488. except Exception as e:
  489. logger.error(f"Agent run failed: {e}")
  490. if self.trace_store:
  491. await self.trace_store.update_trace(
  492. trace_id,
  493. status="failed",
  494. completed_at=datetime.now()
  495. )
  496. # 重新获取更新后的 Trace 并返回
  497. trace_obj = await self.trace_store.get_trace(trace_id)
  498. if trace_obj:
  499. yield trace_obj
  500. raise
  501. # ===== 辅助方法 =====
  502. def _format_skills(self, skills: List[Skill]) -> str:
  503. """格式化技能为 Prompt 文本"""
  504. if not skills:
  505. return ""
  506. return "\n\n".join(s.to_prompt_text() for s in skills)
  507. def _format_experiences(self, experiences: List[Experience]) -> str:
  508. """格式化经验为 Prompt 文本"""
  509. if not experiences:
  510. return ""
  511. return "\n".join(f"- {e.to_prompt_text()}" for e in experiences)
  512. def _format_skills(self, skills: List[Skill]) -> str:
  513. """格式化 Skills 为 Prompt 文本"""
  514. if not skills:
  515. return ""
  516. return "\n\n".join(s.to_prompt_text() for s in skills)