run.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. """
  2. 示例(增强版)
  3. 使用 Agent 模式 + Skills
  4. 新增功能:
  5. 1. 支持命令行随时打断(输入 'p' 暂停,'q' 退出)
  6. 2. 暂停后可插入干预消息
  7. 3. 支持触发经验总结
  8. 4. 查看当前 GoalTree
  9. 5. 框架层自动清理不完整的工具调用
  10. 6. 支持通过 --trace <ID> 恢复已有 Trace 继续执行
  11. """
  12. import argparse
  13. import os
  14. import sys
  15. import select
  16. import asyncio
  17. from pathlib import Path
  18. # Clash Verge TUN 模式兼容:禁止 httpx/urllib 自动检测系统 HTTP 代理
  19. # TUN 虚拟网卡已在网络层接管所有流量,不需要应用层再走 HTTP 代理,
  20. # 否则 httpx 检测到 macOS 系统代理 (127.0.0.1:7897) 会导致 ConnectError
  21. os.environ.setdefault("no_proxy", "*")
  22. # 添加项目根目录到 Python 路径
  23. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  24. from dotenv import load_dotenv
  25. load_dotenv()
  26. from agent.llm.prompts import SimplePrompt
  27. from agent.core.runner import AgentRunner, RunConfig
  28. from agent.core.presets import AgentPreset, register_preset
  29. from agent.trace import (
  30. FileSystemTraceStore,
  31. Trace,
  32. Message,
  33. )
  34. from agent.llm import create_openrouter_llm_call
  35. from agent.tools import get_tool_registry
  36. # ===== 非阻塞 stdin 检测 =====
  37. if sys.platform == 'win32':
  38. import msvcrt
  39. def check_stdin() -> str | None:
  40. """
  41. 跨平台非阻塞检查 stdin 输入。
  42. Windows: 使用 msvcrt.kbhit()
  43. macOS/Linux: 使用 select.select()
  44. """
  45. if sys.platform == 'win32':
  46. # 检查是否有按键按下
  47. if msvcrt.kbhit():
  48. # 读取按下的字符(msvcrt.getwch 是非阻塞读取宽字符)
  49. ch = msvcrt.getwch().lower()
  50. if ch == 'p':
  51. return 'pause'
  52. if ch == 'q':
  53. return 'quit'
  54. # 如果是其他按键,可以选择消耗掉或者忽略
  55. return None
  56. else:
  57. # Unix/Mac 逻辑
  58. ready, _, _ = select.select([sys.stdin], [], [], 0)
  59. if ready:
  60. line = sys.stdin.readline().strip().lower()
  61. if line in ('p', 'pause'):
  62. return 'pause'
  63. if line in ('q', 'quit'):
  64. return 'quit'
  65. return None
  66. # ===== 交互菜单 =====
  67. def _read_multiline() -> str:
  68. """
  69. 读取多行输入,以连续两次回车(空行)结束。
  70. 单次回车只是换行,不会提前终止输入。
  71. """
  72. print("\n请输入干预消息(连续输入两次回车结束):")
  73. lines: list[str] = []
  74. blank_count = 0
  75. while True:
  76. line = input()
  77. if line == "":
  78. blank_count += 1
  79. if blank_count >= 2:
  80. break
  81. lines.append("") # 保留单个空行
  82. else:
  83. blank_count = 0
  84. lines.append(line)
  85. # 去掉尾部多余空行
  86. while lines and lines[-1] == "":
  87. lines.pop()
  88. return "\n".join(lines)
  89. async def show_interactive_menu(
  90. runner: AgentRunner,
  91. trace_id: str,
  92. current_sequence: int,
  93. store: FileSystemTraceStore,
  94. ):
  95. """
  96. 显示交互式菜单,让用户选择操作。
  97. 进入本函数前不再有后台线程占用 stdin,所以 input() 能正常工作。
  98. """
  99. print("\n" + "=" * 60)
  100. print(" 执行已暂停")
  101. print("=" * 60)
  102. print("请选择操作:")
  103. print(" 1. 插入干预消息并继续")
  104. print(" 2. 触发经验总结(reflect)")
  105. print(" 3. 查看当前 GoalTree")
  106. print(" 4. 手动压缩上下文(compact)")
  107. print(" 5. 继续执行")
  108. print(" 6. 停止执行")
  109. print("=" * 60)
  110. while True:
  111. choice = input("请输入选项 (1-6): ").strip()
  112. if choice == "1":
  113. text = _read_multiline()
  114. if not text:
  115. print("未输入任何内容,取消操作")
  116. continue
  117. print(f"\n将插入干预消息并继续执行...")
  118. # 从 store 读取实际的 last_sequence,避免本地 current_sequence 过时
  119. live_trace = await store.get_trace(trace_id)
  120. actual_sequence = live_trace.last_sequence if live_trace and live_trace.last_sequence else current_sequence
  121. return {
  122. "action": "continue",
  123. "messages": [{"role": "user", "content": text}],
  124. "after_sequence": actual_sequence,
  125. }
  126. elif choice == "2":
  127. # 触发经验总结
  128. print("\n触发经验总结...")
  129. focus = input("请输入反思重点(可选,直接回车跳过): ").strip()
  130. # 触发反思
  131. await perform_reflection(runner, store, trace_id, focus=focus)
  132. continue
  133. elif choice == "3":
  134. goal_tree = await store.get_goal_tree(trace_id)
  135. if goal_tree and goal_tree.goals:
  136. print("\n当前 GoalTree:")
  137. print(goal_tree.to_prompt())
  138. else:
  139. print("\n当前没有 Goal")
  140. continue
  141. elif choice == "4":
  142. # 手动压缩上下文
  143. print("\n正在执行上下文压缩(compact)...")
  144. try:
  145. goal_tree = await store.get_goal_tree(trace_id)
  146. trace = await store.get_trace(trace_id)
  147. if not trace:
  148. print("未找到 Trace,无法压缩")
  149. continue
  150. # 重建当前 history
  151. main_path = await store.get_main_path_messages(trace_id, trace.head_sequence)
  152. history = [msg.to_llm_dict() for msg in main_path]
  153. head_seq = main_path[-1].sequence if main_path else 0
  154. next_seq = head_seq + 1
  155. compact_config = RunConfig(trace_id=trace_id)
  156. new_history, new_head, new_seq = await runner._compress_history(
  157. trace_id=trace_id,
  158. history=history,
  159. goal_tree=goal_tree,
  160. config=compact_config,
  161. sequence=next_seq,
  162. head_seq=head_seq,
  163. )
  164. print(f"\n✅ 压缩完成: {len(history)} 条消息 → {len(new_history)} 条")
  165. except Exception as e:
  166. print(f"\n❌ 压缩失败: {e}")
  167. continue
  168. elif choice == "5":
  169. print("\n继续执行...")
  170. return {"action": "continue"}
  171. elif choice == "6":
  172. print("\n停止执行...")
  173. return {"action": "stop"}
  174. else:
  175. print("无效选项,请重新输入")
  176. async def perform_reflection(runner: AgentRunner, store: FileSystemTraceStore, trace_id: str, focus: str = ""):
  177. """执行经验总结并保存(带结构化 YAML 解析)"""
  178. from agent.trace.compaction import build_reflect_prompt
  179. import re as _re2
  180. import uuid as _uuid2
  181. from datetime import datetime
  182. trace = await store.get_trace(trace_id)
  183. if not trace:
  184. return
  185. saved_head = trace.head_sequence
  186. prompt = build_reflect_prompt()
  187. if focus:
  188. prompt += f"\n\n请特别关注:{focus}"
  189. print("正在生成反思...")
  190. reflect_cfg = RunConfig(trace_id=trace_id, max_iterations=1, tools=[])
  191. reflection_text = ""
  192. try:
  193. result = await runner.run_result(
  194. messages=[{"role": "user", "content": prompt}],
  195. config=reflect_cfg,
  196. )
  197. reflection_text = result.get("summary", "")
  198. finally:
  199. # 恢复 head_sequence(反思消息成为侧枝,不污染主对话)
  200. await store.update_trace(trace_id, head_sequence=saved_head)
  201. # 追加到 experiences 文件
  202. if reflection_text:
  203. # experiences_path = runner.experiences_path # 已废弃,使用知识系统 or "./.cache/experiences_how.md"
  204. os.makedirs(os.path.dirname(experiences_path), exist_ok=True)
  205. pattern = r"-\s*\[(?P<tags>.*?)\]\s*(?P<content>.*)"
  206. matches = list(_re2.finditer(pattern, reflection_text))
  207. structured_entries = []
  208. for match in matches:
  209. tags_str = match.group("tags")
  210. content = match.group("content")
  211. intent_match = _re2.search(r"intent:\s*(.*?)(?:,|$)", tags_str, _re2.IGNORECASE)
  212. state_match = _re2.search(r"state:\s*(.*?)(?:,|$)", tags_str, _re2.IGNORECASE)
  213. intents = [i.strip() for i in intent_match.group(1).split(",")] if intent_match and intent_match.group(1) else []
  214. states = [s.strip() for s in state_match.group(1).split(",")] if state_match and state_match.group(1) else []
  215. ex_id = f"ex_{datetime.now().strftime('%m%d%H%M')}_{_uuid2.uuid4().hex[:4]}"
  216. entry = f"---\nid: {ex_id}\ntrace_id: {trace_id}\ntags: {{intent: {intents}, state: {states}}}\nmetrics: {{helpful: 1, harmful: 0}}\ncreated_at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n---\n- {content}\n- 经验ID: [{ex_id}]"
  217. structured_entries.append(entry)
  218. if structured_entries:
  219. final_output = "\n\n" + "\n\n".join(structured_entries)
  220. with open(experiences_path, "a", encoding="utf-8") as f:
  221. f.write(final_output)
  222. print(f"\n✅ 提取了 {len(structured_entries)} 条经验,已结构化并保存到: {experiences_path}")
  223. print("\n--- 反思内容(结构化后) ---")
  224. print(final_output.strip())
  225. print("--- 结束 ---\n")
  226. else:
  227. print("\n⚠️ 未能解析出符合格式的经验条目,已保存原始纯文本以供检查。")
  228. header = f"\n\n---\n\n## [Raw] {trace_id} ({datetime.now().strftime('%Y-%m-%d %H:%M')})\n\n"
  229. with open(experiences_path, "a", encoding="utf-8") as f:
  230. f.write(header + reflection_text + "\n")
  231. print(reflection_text)
  232. else:
  233. print("未生成反思内容")
  234. async def main():
  235. # 解析命令行参数
  236. parser = argparse.ArgumentParser(description="任务 (Agent 模式 + 交互增强)")
  237. parser.add_argument(
  238. "--trace", type=str, default=None,
  239. help="已有的 Trace ID,用于恢复继续执行(不指定则新建)",
  240. )
  241. args = parser.parse_args()
  242. # 路径配置
  243. base_dir = Path(__file__).parent
  244. project_root = base_dir.parent.parent
  245. prompt_path = base_dir / "production.prompt"
  246. output_dir = base_dir / "output_1"
  247. output_dir.mkdir(exist_ok=True)
  248. # 加载项目级 presets(examples/how/presets.json)
  249. presets_path = base_dir / "presets.json"
  250. if presets_path.exists():
  251. import json
  252. with open(presets_path, "r", encoding="utf-8") as f:
  253. project_presets = json.load(f)
  254. for name, cfg in project_presets.items():
  255. register_preset(name, AgentPreset(**cfg))
  256. print(f" - 已加载项目 presets: {list(project_presets.keys())}")
  257. # Skills 目录(可选:用户自定义 skills)
  258. # 注意:内置 skills(agent/memory/skills/)会自动加载
  259. skills_dir = str(base_dir / "skills")
  260. print("=" * 60)
  261. print("mcp/skills 发现、获取、评价 分析任务 (Agent 模式 + 交互增强)")
  262. print("=" * 60)
  263. print()
  264. print("💡 交互提示:")
  265. print(" - 执行过程中输入 'p' 或 'pause' 暂停并进入交互模式")
  266. print(" - 执行过程中输入 'q' 或 'quit' 停止执行")
  267. print("=" * 60)
  268. print()
  269. # 1. 加载 prompt
  270. print("1. 加载 prompt 配置...")
  271. prompt = SimplePrompt(prompt_path)
  272. # 2. 构建消息(仅新建时使用,恢复时消息已在 trace 中)
  273. print("2. 构建任务消息...")
  274. messages = prompt.build_messages()
  275. # 3. 创建 Agent Runner(配置 skills)
  276. print("3. 创建 Agent Runner...")
  277. print(f" - Skills 目录: {skills_dir}")
  278. print(f" - 模型: {prompt.config.get('model', 'sonnet-4.5')}")
  279. # 加载自定义工具
  280. print(" - 加载自定义工具: nanobanana")
  281. import examples.how.tool # 导入自定义工具模块,触发 @tool 装饰器注册
  282. store = FileSystemTraceStore(base_path=".trace")
  283. runner = AgentRunner(
  284. trace_store=store,
  285. llm_call=create_openrouter_llm_call(model=f"anthropic/claude-{prompt.config.get('model', 'sonnet-4.5')}"),
  286. skills_dir=skills_dir,
  287. debug=True
  288. )
  289. # 4. 判断是新建还是恢复
  290. resume_trace_id = args.trace
  291. if resume_trace_id:
  292. # 验证 trace 存在
  293. existing_trace = await store.get_trace(resume_trace_id)
  294. if not existing_trace:
  295. print(f"\n错误: Trace 不存在: {resume_trace_id}")
  296. sys.exit(1)
  297. print(f"4. 恢复已有 Trace: {resume_trace_id[:8]}...")
  298. print(f" - 状态: {existing_trace.status}")
  299. print(f" - 消息数: {existing_trace.total_messages}")
  300. print(f" - 任务: {existing_trace.task}")
  301. else:
  302. print(f"4. 启动新 Agent 模式...")
  303. print()
  304. final_response = ""
  305. current_trace_id = resume_trace_id
  306. current_sequence = 0
  307. should_exit = False
  308. try:
  309. # 恢复模式:不发送初始消息,只指定 trace_id 续跑
  310. if resume_trace_id:
  311. initial_messages = None # None = 未设置,触发早期菜单检查
  312. config = RunConfig(
  313. model=f"claude-{prompt.config.get('model', 'sonnet-4.5')}",
  314. temperature=float(prompt.config.get('temperature', 0.3)),
  315. max_iterations=1000,
  316. trace_id=resume_trace_id,
  317. )
  318. else:
  319. initial_messages = messages
  320. config = RunConfig(
  321. model=f"claude-{prompt.config.get('model', 'sonnet-4.5')}",
  322. temperature=float(prompt.config.get('temperature', 0.3)),
  323. max_iterations=1000,
  324. name="社交媒体内容解构、建构、评估任务",
  325. )
  326. while not should_exit:
  327. # 如果是续跑,需要指定 trace_id
  328. if current_trace_id:
  329. config.trace_id = current_trace_id
  330. # 清理上一轮的响应,避免失败后显示旧内容
  331. final_response = ""
  332. # 如果 trace 已完成/失败且没有新消息,直接进入交互菜单
  333. # 注意:initial_messages 为 None 表示未设置(首次加载),[] 表示有意为空(用户选择"继续")
  334. if current_trace_id and initial_messages is None:
  335. check_trace = await store.get_trace(current_trace_id)
  336. if check_trace and check_trace.status in ("completed", "failed"):
  337. if check_trace.status == "completed":
  338. print(f"\n[Trace] ✅ 已完成")
  339. print(f" - Total messages: {check_trace.total_messages}")
  340. print(f" - Total cost: ${check_trace.total_cost:.4f}")
  341. else:
  342. print(f"\n[Trace] ❌ 已失败: {check_trace.error_message}")
  343. current_sequence = check_trace.head_sequence
  344. menu_result = await show_interactive_menu(
  345. runner, current_trace_id, current_sequence, store
  346. )
  347. if menu_result["action"] == "stop":
  348. break
  349. elif menu_result["action"] == "continue":
  350. new_messages = menu_result.get("messages", [])
  351. if new_messages:
  352. initial_messages = new_messages
  353. config.after_sequence = menu_result.get("after_sequence")
  354. else:
  355. # 无新消息:对 failed trace 意味着重试,对 completed 意味着继续
  356. initial_messages = []
  357. config.after_sequence = None
  358. continue
  359. break
  360. # 对 stopped/running 等非终态的 trace,直接续跑
  361. initial_messages = []
  362. print(f"{'▶️ 开始执行...' if not current_trace_id else '▶️ 继续执行...'}")
  363. # 执行 Agent
  364. paused = False
  365. try:
  366. async for item in runner.run(messages=initial_messages, config=config):
  367. # 检查用户中断
  368. cmd = check_stdin()
  369. if cmd == 'pause':
  370. # 暂停执行
  371. print("\n⏸️ 正在暂停执行...")
  372. if current_trace_id:
  373. await runner.stop(current_trace_id)
  374. # 等待一小段时间让 runner 处理 stop 信号
  375. await asyncio.sleep(0.5)
  376. # 显示交互菜单
  377. menu_result = await show_interactive_menu(
  378. runner, current_trace_id, current_sequence, store
  379. )
  380. if menu_result["action"] == "stop":
  381. should_exit = True
  382. paused = True
  383. break
  384. elif menu_result["action"] == "continue":
  385. # 检查是否有新消息需要插入
  386. new_messages = menu_result.get("messages", [])
  387. if new_messages:
  388. # 有干预消息,需要重新启动循环
  389. initial_messages = new_messages
  390. after_seq = menu_result.get("after_sequence")
  391. if after_seq is not None:
  392. config.after_sequence = after_seq
  393. paused = True
  394. break
  395. else:
  396. # 没有新消息,需要重启执行
  397. initial_messages = []
  398. config.after_sequence = None
  399. paused = True
  400. break
  401. elif cmd == 'quit':
  402. print("\n🛑 用户请求停止...")
  403. if current_trace_id:
  404. await runner.stop(current_trace_id)
  405. should_exit = True
  406. break
  407. # 处理 Trace 对象(整体状态变化)
  408. if isinstance(item, Trace):
  409. current_trace_id = item.trace_id
  410. if item.status == "running":
  411. print(f"[Trace] 开始: {item.trace_id[:8]}...")
  412. elif item.status == "completed":
  413. print(f"\n[Trace] ✅ 完成")
  414. print(f" - Total messages: {item.total_messages}")
  415. print(f" - Total tokens: {item.total_tokens}")
  416. print(f" - Total cost: ${item.total_cost:.4f}")
  417. elif item.status == "failed":
  418. print(f"\n[Trace] ❌ 失败: {item.error_message}")
  419. elif item.status == "stopped":
  420. print(f"\n[Trace] ⏸️ 已停止")
  421. # 处理 Message 对象(执行过程)
  422. elif isinstance(item, Message):
  423. current_sequence = item.sequence
  424. if item.role == "assistant":
  425. content = item.content
  426. if isinstance(content, dict):
  427. text = content.get("text", "")
  428. tool_calls = content.get("tool_calls")
  429. if text and not tool_calls:
  430. # 纯文本回复(最终响应)
  431. final_response = text
  432. print(f"\n[Response] Agent 回复:")
  433. print(text)
  434. elif text:
  435. preview = text[:150] + "..." if len(text) > 150 else text
  436. print(f"[Assistant] {preview}")
  437. if tool_calls:
  438. for tc in tool_calls:
  439. tool_name = tc.get("function", {}).get("name", "unknown")
  440. print(f"[Tool Call] 🛠️ {tool_name}")
  441. elif item.role == "tool":
  442. content = item.content
  443. if isinstance(content, dict):
  444. tool_name = content.get("tool_name", "unknown")
  445. print(f"[Tool Result] ✅ {tool_name}")
  446. if item.description:
  447. desc = item.description[:80] if len(item.description) > 80 else item.description
  448. print(f" {desc}...")
  449. except Exception as e:
  450. print(f"\n执行出错: {e}")
  451. import traceback
  452. traceback.print_exc()
  453. # paused → 菜单已在暂停时内联显示过
  454. if paused:
  455. if should_exit:
  456. break
  457. continue
  458. # quit → 直接退出
  459. if should_exit:
  460. break
  461. # Runner 退出(完成/失败/停止/异常)→ 显示交互菜单
  462. if current_trace_id:
  463. # 🌟 新增:自动触发反思的生命周期钩子
  464. check_trace = await store.get_trace(current_trace_id)
  465. if check_trace and check_trace.status in ("completed", "failed"):
  466. print(f"\n⚙️ 任务已结束 (状态: {check_trace.status}),正在自动触发经验总结...")
  467. # 如果是失败状态,自动带上针对性的 focus 提示
  468. auto_focus = "本次任务执行失败了,请重点反思失败的原因、踩坑点以及未来应如何避免。" if check_trace.status == "failed" else ""
  469. await perform_reflection(runner, store, current_trace_id, focus=auto_focus)
  470. # 自动反思结束后,依然弹出菜单,让用户决定是彻底退出(6)还是查看总结(3)
  471. menu_result = await show_interactive_menu(
  472. runner, current_trace_id, current_sequence, store
  473. )
  474. if menu_result["action"] == "stop":
  475. break
  476. elif menu_result["action"] == "continue":
  477. new_messages = menu_result.get("messages", [])
  478. if new_messages:
  479. initial_messages = new_messages
  480. config.after_sequence = menu_result.get("after_sequence")
  481. else:
  482. initial_messages = []
  483. config.after_sequence = None
  484. continue
  485. break
  486. except KeyboardInterrupt:
  487. print("\n\n用户中断 (Ctrl+C)")
  488. if current_trace_id:
  489. await runner.stop(current_trace_id)
  490. # 6. 输出结果
  491. if final_response:
  492. print()
  493. print("=" * 60)
  494. print("Agent 响应:")
  495. print("=" * 60)
  496. print(final_response)
  497. print("=" * 60)
  498. print()
  499. # 7. 保存结果
  500. output_file = output_dir / "result.txt"
  501. with open(output_file, 'w', encoding='utf-8') as f:
  502. f.write(final_response)
  503. print(f"✓ 结果已保存到: {output_file}")
  504. print()
  505. # 可视化提示
  506. if current_trace_id:
  507. print("=" * 60)
  508. print("可视化 Step Tree:")
  509. print("=" * 60)
  510. print("1. 启动 API Server:")
  511. print(" python3 api_server.py")
  512. print()
  513. print("2. 浏览器访问:")
  514. print(" http://43.106.118.91:8000/api/traces")
  515. print()
  516. print(f"3. Trace ID: {current_trace_id}")
  517. print("=" * 60)
  518. if __name__ == "__main__":
  519. asyncio.run(main())