run.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. """
  2. 示例(简化版 - 使用框架交互功能)
  3. 使用 Agent 模式 + Skills + 框架交互控制器
  4. 新功能:
  5. 1. 使用框架提供的 InteractiveController
  6. 2. 使用配置文件管理运行参数
  7. 3. 支持命令行随时打断(输入 'p' 暂停,'q' 退出)
  8. 4. 暂停后可插入干预消息
  9. 5. 支持触发经验总结
  10. 6. 查看当前 GoalTree
  11. 7. 支持通过 --trace <ID> 恢复已有 Trace 继续执行
  12. """
  13. import argparse
  14. import os
  15. import sys
  16. import asyncio
  17. from pathlib import Path
  18. # Clash Verge TUN 模式兼容:禁止 httpx/urllib 自动检测系统 HTTP 代理
  19. os.environ.setdefault("no_proxy", "*")
  20. # 添加项目根目录到 Python 路径
  21. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  22. from dotenv import load_dotenv
  23. load_dotenv()
  24. from agent.llm.prompts import SimplePrompt
  25. from agent.core.runner import AgentRunner, RunConfig
  26. from agent.trace import (
  27. FileSystemTraceStore,
  28. Trace,
  29. Message,
  30. )
  31. from agent.llm import create_qwen_llm_call
  32. from agent.cli import InteractiveController
  33. from agent.utils import setup_logging
  34. from agent.tools.builtin.browser.baseClass import init_browser_session, kill_browser_session
  35. # 导入自定义工具(触发 @tool 注册)
  36. # from .tools.reflect import reflect
  37. # 导入项目配置
  38. from config import RUN_CONFIG, SKILLS_DIR, TRACE_STORE_PATH, DEBUG, LOG_LEVEL, LOG_FILE, BROWSER_TYPE, HEADLESS, OUTPUT_DIR
  39. from config import IM_ENABLED, IM_CONTACT_ID, IM_SERVER_URL, IM_WINDOW_MODE, IM_NOTIFY_INTERVAL
  40. from config import KNOWLEDGE_MANAGER_ENABLED, KNOWLEDGE_MANAGER_CONTACT_ID, KNOWLEDGE_MANAGER_ENABLE_DB_COMMIT
  41. async def main():
  42. # 解析命令行参数
  43. parser = argparse.ArgumentParser(description="任务 (Agent 模式 + 交互增强)")
  44. parser.add_argument(
  45. "--trace", type=str, default=None,
  46. help="已有的 Trace ID,用于恢复继续执行(不指定则新建)",
  47. )
  48. args = parser.parse_args()
  49. # 路径配置
  50. base_dir = Path(__file__).parent
  51. project_root = base_dir.parent.parent
  52. prompt_path = base_dir / "tool_research.prompt"
  53. output_dir = project_root / OUTPUT_DIR
  54. output_dir.mkdir(parents=True, exist_ok=True)
  55. # 1. 配置日志
  56. setup_logging(level=LOG_LEVEL, file=LOG_FILE)
  57. # 2. 加载项目级 presets
  58. print("2. 加载 presets...")
  59. presets_path = base_dir / "presets.json"
  60. if presets_path.exists():
  61. from agent.core.presets import load_presets_from_json
  62. load_presets_from_json(str(presets_path))
  63. print(f" - 已加载项目 presets")
  64. else:
  65. print(f" - 未找到 presets.json,跳过")
  66. # 3. 加载 prompt
  67. print("3. 加载 prompt...")
  68. prompt = SimplePrompt(prompt_path)
  69. # 4. 构建任务消息
  70. print("4. 构建任务消息...")
  71. print(f" - 输出目录: {output_dir}")
  72. messages = prompt.build_messages(output_dir=str(output_dir))
  73. # 5. 初始化浏览器
  74. browser_mode_names = {"cloud": "云浏览器", "local": "本地浏览器", "container": "容器浏览器"}
  75. browser_mode_name = browser_mode_names.get(BROWSER_TYPE, BROWSER_TYPE)
  76. print(f"5. 正在初始化{browser_mode_name}...")
  77. await init_browser_session(
  78. browser_type=BROWSER_TYPE,
  79. headless=HEADLESS,
  80. url="https://www.google.com/",
  81. profile_name=""
  82. )
  83. print(f" ✅ {browser_mode_name}初始化完成\n")
  84. # 5.5 初始化 IM Client(可选)
  85. km_task = None
  86. if IM_ENABLED:
  87. from agent.tools.builtin.im.chat import im_setup, im_open_window
  88. print("5.5 初始化 IM Client...")
  89. print(f" - 身份: {IM_CONTACT_ID}, 服务器: {IM_SERVER_URL}")
  90. result = await im_setup(
  91. contact_id=IM_CONTACT_ID,
  92. server_url=IM_SERVER_URL,
  93. notify_interval=IM_NOTIFY_INTERVAL,
  94. )
  95. print(f" ✅ {result.output}")
  96. # 如果启用窗口模式,打开一个窗口
  97. if IM_WINDOW_MODE:
  98. window_result = await im_open_window(contact_id=IM_CONTACT_ID)
  99. print(f" ✅ {window_result.output}\n")
  100. else:
  101. print()
  102. # 启动 Knowledge Manager(如果启用)
  103. if KNOWLEDGE_MANAGER_ENABLED:
  104. print("5.6 启动 Knowledge Manager...")
  105. print(f" - Contact ID: {KNOWLEDGE_MANAGER_CONTACT_ID}")
  106. try:
  107. sys.path.insert(0, str(Path(__file__).parent.parent.parent / "knowhub"))
  108. from agents.knowledge_manager import start_knowledge_manager
  109. km_task = asyncio.create_task(start_knowledge_manager(
  110. contact_id=KNOWLEDGE_MANAGER_CONTACT_ID,
  111. server_url=IM_SERVER_URL,
  112. chat_id="main",
  113. enable_db_commit=KNOWLEDGE_MANAGER_ENABLE_DB_COMMIT
  114. ))
  115. # 等待 2 秒,让 KM 有时间连接和初始化
  116. await asyncio.sleep(2)
  117. # 检查 task 是否出错
  118. if km_task.done():
  119. exc = km_task.exception()
  120. if exc:
  121. print(f" ⚠️ 启动失败: {exc}\n")
  122. else:
  123. print(f" ✅ Knowledge Manager 已启动(后台运行)\n")
  124. else:
  125. print(f" ✅ Knowledge Manager 已启动(后台运行)\n")
  126. except Exception as e:
  127. print(f" ⚠️ 启动失败: {e}\n")
  128. import traceback
  129. traceback.print_exc()
  130. # 6. 创建 Agent Runner
  131. print("6. 创建 Agent Runner...")
  132. print(f" - Skills 目录: {SKILLS_DIR}")
  133. # 从 prompt 的 frontmatter 中提取模型配置(优先于 config.py)
  134. prompt_model = prompt.config.get("model", None)
  135. if prompt_model:
  136. model_for_llm = prompt_model
  137. print(f" - 模型 (from prompt): {model_for_llm}")
  138. else:
  139. model_for_llm = RUN_CONFIG.model
  140. print(f" - 模型 (from config): {model_for_llm}")
  141. store = FileSystemTraceStore(base_path=TRACE_STORE_PATH)
  142. runner = AgentRunner(
  143. trace_store=store,
  144. llm_call=create_qwen_llm_call(model=model_for_llm),
  145. skills_dir=SKILLS_DIR,
  146. debug=DEBUG
  147. )
  148. # 7. 创建交互控制器
  149. interactive = InteractiveController(
  150. runner=runner,
  151. store=store,
  152. enable_stdin_check=True
  153. )
  154. # 将 stdin 检查回调注入 runner,供子 agent 执行期间使用
  155. runner.stdin_check = interactive.check_stdin
  156. # 8. 任务信息
  157. task_name = RUN_CONFIG.name or base_dir.name
  158. print("=" * 60)
  159. print(f"{task_name}")
  160. print("=" * 60)
  161. print("💡 交互提示:")
  162. print(" - 执行过程中输入 'p' 或 'pause' 暂停并进入交互模式")
  163. print(" - 执行过程中输入 'q' 或 'quit' 停止执行")
  164. print("=" * 60)
  165. print()
  166. # 9. 判断是新建还是恢复
  167. resume_trace_id = args.trace
  168. if resume_trace_id:
  169. existing_trace = await store.get_trace(resume_trace_id)
  170. if not existing_trace:
  171. print(f"\n错误: Trace 不存在: {resume_trace_id}")
  172. sys.exit(1)
  173. print(f"恢复已有 Trace: {resume_trace_id[:8]}...")
  174. print(f" - 状态: {existing_trace.status}")
  175. print(f" - 消息数: {existing_trace.total_messages}")
  176. print(f"\n💡 提示:恢复 Trace 时会先进入交互菜单,您可以选择从指定消息续跑")
  177. else:
  178. print(f"启动新 Agent...")
  179. print()
  180. final_response = ""
  181. current_trace_id = resume_trace_id
  182. current_sequence = 0
  183. should_exit = False
  184. try:
  185. # 配置
  186. run_config = RUN_CONFIG
  187. if resume_trace_id:
  188. initial_messages = None
  189. run_config.trace_id = resume_trace_id
  190. else:
  191. initial_messages = messages
  192. run_config.name = f"{task_name}:工具调研"
  193. while not should_exit:
  194. if current_trace_id:
  195. run_config.trace_id = current_trace_id
  196. final_response = ""
  197. # 如果是恢复 trace 或 trace 已完成/失败且没有新消息,进入交互菜单
  198. if current_trace_id and initial_messages is None:
  199. check_trace = await store.get_trace(current_trace_id)
  200. if check_trace:
  201. # 显示 trace 状态
  202. if check_trace.status == "completed":
  203. print(f"\n[Trace] ✅ 已完成")
  204. print(f" - Total messages: {check_trace.total_messages}")
  205. print(f" - Total cost: ${check_trace.total_cost:.4f}")
  206. elif check_trace.status == "failed":
  207. print(f"\n[Trace] ❌ 已失败: {check_trace.error_message}")
  208. elif check_trace.status == "stopped":
  209. print(f"\n[Trace] ⏸️ 已停止")
  210. print(f" - Total messages: {check_trace.total_messages}")
  211. else:
  212. print(f"\n[Trace] 📊 状态: {check_trace.status}")
  213. print(f" - Total messages: {check_trace.total_messages}")
  214. current_sequence = check_trace.head_sequence
  215. menu_result = await interactive.show_menu(current_trace_id, current_sequence)
  216. if menu_result["action"] == "stop":
  217. break
  218. elif menu_result["action"] == "continue":
  219. new_messages = menu_result.get("messages", [])
  220. if new_messages:
  221. initial_messages = new_messages
  222. run_config.after_sequence = menu_result.get("after_sequence")
  223. else:
  224. initial_messages = []
  225. run_config.after_sequence = None
  226. continue
  227. break
  228. # 如果没有进入菜单(新建 trace),设置初始消息
  229. if initial_messages is None:
  230. initial_messages = []
  231. print(f"{'▶️ 开始执行...' if not current_trace_id else '▶️ 继续执行...'}")
  232. # 执行 Agent
  233. paused = False
  234. try:
  235. async for item in runner.run(messages=initial_messages, config=run_config):
  236. # 检查用户中断
  237. cmd = interactive.check_stdin()
  238. if cmd == 'pause':
  239. print("\n⏸️ 正在暂停执行...")
  240. if current_trace_id:
  241. await runner.stop(current_trace_id)
  242. await asyncio.sleep(0.5)
  243. menu_result = await interactive.show_menu(current_trace_id, current_sequence)
  244. if menu_result["action"] == "stop":
  245. should_exit = True
  246. paused = True
  247. break
  248. elif menu_result["action"] == "continue":
  249. new_messages = menu_result.get("messages", [])
  250. if new_messages:
  251. initial_messages = new_messages
  252. after_seq = menu_result.get("after_sequence")
  253. if after_seq is not None:
  254. run_config.after_sequence = after_seq
  255. paused = True
  256. break
  257. else:
  258. initial_messages = []
  259. run_config.after_sequence = None
  260. paused = True
  261. break
  262. elif cmd == 'quit':
  263. print("\n🛑 用户请求停止...")
  264. if current_trace_id:
  265. await runner.stop(current_trace_id)
  266. should_exit = True
  267. break
  268. # 处理 Trace 对象
  269. if isinstance(item, Trace):
  270. current_trace_id = item.trace_id
  271. if item.status == "running":
  272. print(f"[Trace] 开始: {item.trace_id[:8]}...")
  273. elif item.status == "completed":
  274. print(f"\n[Trace] ✅ 完成")
  275. print(f" - Total messages: {item.total_messages}")
  276. print(f" - Total cost: ${item.total_cost:.4f}")
  277. elif item.status == "failed":
  278. print(f"\n[Trace] ❌ 失败: {item.error_message}")
  279. elif item.status == "stopped":
  280. print(f"\n[Trace] ⏸️ 已停止")
  281. # 处理 Message 对象
  282. elif isinstance(item, Message):
  283. current_sequence = item.sequence
  284. if item.role == "assistant":
  285. content = item.content
  286. if isinstance(content, dict):
  287. text = content.get("text", "")
  288. tool_calls = content.get("tool_calls")
  289. if text and not tool_calls:
  290. final_response = text
  291. print(f"\n[Response] Agent 回复:")
  292. print(text)
  293. elif text:
  294. preview = text[:150] + "..." if len(text) > 150 else text
  295. print(f"[Assistant] {preview}")
  296. elif item.role == "tool":
  297. content = item.content
  298. tool_name = "unknown"
  299. if isinstance(content, dict):
  300. tool_name = content.get("tool_name", "unknown")
  301. if item.description and item.description != tool_name:
  302. desc = item.description[:80] if len(item.description) > 80 else item.description
  303. print(f"[Tool Result] ✅ {tool_name}: {desc}...")
  304. else:
  305. print(f"[Tool Result] ✅ {tool_name}")
  306. except Exception as e:
  307. print(f"\n执行出错: {e}")
  308. import traceback
  309. traceback.print_exc()
  310. if paused:
  311. if should_exit:
  312. break
  313. continue
  314. if should_exit:
  315. break
  316. # Runner 退出后显示交互菜单
  317. if current_trace_id:
  318. menu_result = await interactive.show_menu(current_trace_id, current_sequence)
  319. if menu_result["action"] == "stop":
  320. break
  321. elif menu_result["action"] == "continue":
  322. new_messages = menu_result.get("messages", [])
  323. if new_messages:
  324. initial_messages = new_messages
  325. run_config.after_sequence = menu_result.get("after_sequence")
  326. else:
  327. initial_messages = []
  328. run_config.after_sequence = None
  329. continue
  330. break
  331. except KeyboardInterrupt:
  332. print("\n\n用户中断 (Ctrl+C)")
  333. if current_trace_id:
  334. await runner.stop(current_trace_id)
  335. finally:
  336. # 清理 Knowledge Manager
  337. if km_task and not km_task.done():
  338. print("正在关闭 Knowledge Manager...")
  339. km_task.cancel()
  340. try:
  341. await km_task
  342. except asyncio.CancelledError:
  343. pass
  344. # 清理浏览器会话
  345. try:
  346. await kill_browser_session()
  347. except Exception:
  348. pass
  349. # 7. 输出结果
  350. if final_response:
  351. print()
  352. print("=" * 60)
  353. print("Agent 响应:")
  354. print("=" * 60)
  355. print(final_response)
  356. print("=" * 60)
  357. print()
  358. output_file = output_dir / "result.txt"
  359. with open(output_file, 'w', encoding='utf-8') as f:
  360. f.write(final_response)
  361. print(f"✓ 结果已保存到: {output_file}")
  362. print()
  363. # 可视化提示
  364. if current_trace_id:
  365. print("=" * 60)
  366. print("可视化 Step Tree:")
  367. print("=" * 60)
  368. print("1. 启动 API Server:")
  369. print(" python3 api_server.py")
  370. print()
  371. print("2. 浏览器访问:")
  372. print(" http://localhost:8000/api/traces")
  373. print()
  374. print(f"3. Trace ID: {current_trace_id}")
  375. print("=" * 60)
  376. if __name__ == "__main__":
  377. asyncio.run(main())