overall_derivation_agent_run.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. """
  2. 选题点整体推导 Agent(增强版)
  3. 参考 examples/how/run.py,提供:
  4. 1. 命令行交互:输入 'p' 暂停、'q' 退出
  5. 2. 暂停后可插入干预消息、触发经验总结、查看 GoalTree、手动压缩上下文
  6. 3. 支持 --trace <ID> 恢复已有 Trace 继续执行
  7. 4. 使用 SimplePrompt 加载 derivation_main.md,支持评估子 agent(agent_type=evaluate_derivation)
  8. """
  9. import argparse
  10. import os
  11. import sys
  12. import select
  13. import asyncio
  14. from datetime import datetime
  15. from pathlib import Path
  16. # 与 examples/how/run.py 一致:禁止 httpx/urllib 自动检测系统 HTTP 代理
  17. # os.environ.setdefault("no_proxy", "*")
  18. # 添加项目根目录到 Python 路径
  19. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  20. from dotenv import load_dotenv
  21. from agent.llm.prompts import SimplePrompt
  22. from agent.core.runner import AgentRunner, RunConfig
  23. from agent.core.presets import AgentPreset, register_preset
  24. from agent.trace import (
  25. FileSystemTraceStore,
  26. Trace,
  27. Message,
  28. )
  29. from agent.llm import create_openrouter_llm_call
  30. from agent.trace.compaction import build_reflect_prompt
  31. load_dotenv()
  32. # ===== 非阻塞 stdin 检测 =====
  33. if sys.platform == 'win32':
  34. import msvcrt
  35. def check_stdin() -> str | None:
  36. """
  37. 跨平台非阻塞检查 stdin 输入。
  38. Windows: msvcrt.kbhit();macOS/Linux: select.select()
  39. """
  40. if sys.platform == 'win32':
  41. if msvcrt.kbhit():
  42. ch = msvcrt.getwch().lower()
  43. if ch == 'p':
  44. return 'pause'
  45. if ch == 'q':
  46. return 'quit'
  47. return None
  48. else:
  49. ready, _, _ = select.select([sys.stdin], [], [], 0)
  50. if ready:
  51. line = sys.stdin.readline().strip().lower()
  52. if line in ('p', 'pause'):
  53. return 'pause'
  54. if line in ('q', 'quit'):
  55. return 'quit'
  56. return None
  57. # ===== 交互菜单 =====
  58. def _read_multiline() -> str:
  59. """读取多行输入,以连续两次回车(空行)结束。"""
  60. print("\n请输入干预消息(连续输入两次回车结束):")
  61. lines: list[str] = []
  62. blank_count = 0
  63. while True:
  64. line = input()
  65. if line == "":
  66. blank_count += 1
  67. if blank_count >= 2:
  68. break
  69. lines.append("")
  70. else:
  71. blank_count = 0
  72. lines.append(line)
  73. while lines and lines[-1] == "":
  74. lines.pop()
  75. return "\n".join(lines)
  76. async def show_interactive_menu(
  77. runner: AgentRunner,
  78. trace_id: str,
  79. current_sequence: int,
  80. store: FileSystemTraceStore,
  81. ):
  82. """显示交互式菜单,让用户选择操作。"""
  83. print("\n" + "=" * 60)
  84. print(" 执行已暂停")
  85. print("=" * 60)
  86. print("请选择操作:")
  87. print(" 1. 插入干预消息并继续")
  88. print(" 2. 触发经验总结(reflect)")
  89. print(" 3. 查看当前 GoalTree")
  90. print(" 4. 手动压缩上下文(compact)")
  91. print(" 5. 继续执行")
  92. print(" 6. 停止执行")
  93. print("=" * 60)
  94. while True:
  95. choice = input("请输入选项 (1-6): ").strip()
  96. if choice == "1":
  97. text = _read_multiline()
  98. if not text:
  99. print("未输入任何内容,取消操作")
  100. continue
  101. print("\n将插入干预消息并继续执行...")
  102. live_trace = await store.get_trace(trace_id)
  103. actual_sequence = live_trace.last_sequence if live_trace and live_trace.last_sequence else current_sequence
  104. return {
  105. "action": "continue",
  106. "messages": [{"role": "user", "content": text}],
  107. "after_sequence": actual_sequence,
  108. }
  109. elif choice == "2":
  110. print("\n触发经验总结...")
  111. focus = input("请输入反思重点(可选,直接回车跳过): ").strip()
  112. trace = await store.get_trace(trace_id)
  113. saved_head = trace.head_sequence
  114. prompt = build_reflect_prompt()
  115. if focus:
  116. prompt += f"\n\n请特别关注:{focus}"
  117. print("正在生成反思...")
  118. reflect_cfg = RunConfig(trace_id=trace_id, max_iterations=1, tools=[])
  119. reflection_text = ""
  120. try:
  121. result = await runner.run_result(
  122. messages=[{"role": "user", "content": prompt}],
  123. config=reflect_cfg,
  124. )
  125. reflection_text = result.get("summary", "")
  126. finally:
  127. await store.update_trace(trace_id, head_sequence=saved_head)
  128. if reflection_text:
  129. from datetime import datetime
  130. experiences_path = runner.experiences_path or "./.cache/experiences_overall_derivation.md"
  131. os.makedirs(os.path.dirname(experiences_path), exist_ok=True)
  132. header = f"\n\n---\n\n## {trace_id} ({datetime.now().strftime('%Y-%m-%d %H:%M')})\n\n"
  133. with open(experiences_path, "a", encoding="utf-8") as f:
  134. f.write(header + reflection_text + "\n")
  135. print(f"\n反思已保存到: {experiences_path}")
  136. print("\n--- 反思内容 ---")
  137. print(reflection_text)
  138. print("--- 结束 ---\n")
  139. else:
  140. print("未生成反思内容")
  141. continue
  142. elif choice == "3":
  143. goal_tree = await store.get_goal_tree(trace_id)
  144. if goal_tree and goal_tree.goals:
  145. print("\n当前 GoalTree:")
  146. print(goal_tree.to_prompt())
  147. else:
  148. print("\n当前没有 Goal")
  149. continue
  150. elif choice == "4":
  151. print("\n正在执行上下文压缩(compact)...")
  152. try:
  153. goal_tree = await store.get_goal_tree(trace_id)
  154. trace = await store.get_trace(trace_id)
  155. if not trace:
  156. print("未找到 Trace,无法压缩")
  157. continue
  158. main_path = await store.get_main_path_messages(trace_id, trace.head_sequence)
  159. history = [msg.to_llm_dict() for msg in main_path]
  160. head_seq = main_path[-1].sequence if main_path else 0
  161. next_seq = head_seq + 1
  162. compact_config = RunConfig(trace_id=trace_id)
  163. new_history, new_head, new_seq = await runner._compress_history(
  164. trace_id=trace_id,
  165. history=history,
  166. goal_tree=goal_tree,
  167. config=compact_config,
  168. sequence=next_seq,
  169. head_seq=head_seq,
  170. )
  171. print(f"\n✅ 压缩完成: {len(history)} 条消息 → {len(new_history)} 条")
  172. except Exception as e:
  173. print(f"\n❌ 压缩失败: {e}")
  174. continue
  175. elif choice == "5":
  176. print("\n继续执行...")
  177. return {"action": "continue"}
  178. elif choice == "6":
  179. print("\n停止执行...")
  180. return {"action": "stop"}
  181. else:
  182. print("无效选项,请重新输入")
  183. def _replace_prompt_placeholders(
  184. messages: list,
  185. account_name: str,
  186. post_id: str,
  187. log_id: str,
  188. post_point_count: int,
  189. ) -> None:
  190. """在 messages 的 content 中用 replace 替换 {account_name}, {帖子ID}, {log_id}, {post_point_count}。"""
  191. post_point_count_str = str(post_point_count)
  192. for m in messages:
  193. content = m.get("content")
  194. if isinstance(content, str):
  195. m["content"] = (
  196. content.replace("{account_name}", account_name)
  197. .replace("{帖子ID}", post_id)
  198. .replace("{log_id}", log_id)
  199. .replace("{post_point_count}", post_point_count_str)
  200. )
  201. elif isinstance(content, list):
  202. for part in content:
  203. if isinstance(part, dict) and part.get("type") == "text":
  204. part["text"] = (
  205. (part.get("text") or "")
  206. .replace("{account_name}", account_name)
  207. .replace("{帖子ID}", post_id)
  208. .replace("{log_id}", log_id)
  209. .replace("{post_point_count}", post_point_count_str)
  210. )
  211. async def main(account_name, post_id):
  212. parser = argparse.ArgumentParser(description="选题点整体推导 Agent(支持交互与恢复)")
  213. parser.add_argument(
  214. "--trace", type=str, default=None,
  215. help="已有的 Trace ID,用于恢复继续执行(不指定则新建)",
  216. )
  217. args = parser.parse_args()
  218. base_dir = Path(__file__).parent
  219. prompt_path = base_dir / "derivation_main.md"
  220. output_dir = base_dir / "output"
  221. output_dir.mkdir(exist_ok=True)
  222. # 加载项目级 presets(evaluate_derivation、derivation_search 等)
  223. presets_path = base_dir / "presets.json"
  224. if presets_path.exists():
  225. import json
  226. with open(presets_path, "r", encoding="utf-8") as f:
  227. project_presets = json.load(f)
  228. for name, cfg in project_presets.items():
  229. register_preset(name, AgentPreset(**cfg))
  230. print(f" - 已加载项目 presets: {list(project_presets.keys())}")
  231. # 注册选题点推导专用工具(主 agent 与评估子 agent 会调用)
  232. import importlib.util
  233. tools_dir = base_dir / "tools"
  234. for mod_name, file_name in [
  235. ("find_tree_node", "find_tree_node.py"),
  236. ("find_pattern", "find_pattern.py"),
  237. ("point_match", "point_match.py"),
  238. ]:
  239. path = tools_dir / file_name
  240. if path.is_file():
  241. spec = importlib.util.spec_from_file_location(f"overall_derivation.{mod_name}", path)
  242. mod = importlib.util.module_from_spec(spec)
  243. spec.loader.exec_module(mod)
  244. print(f" - 已注册推导工具: {mod_name}")
  245. skills_dir = str(base_dir / "skills")
  246. print("=" * 60)
  247. print("选题点整体推导 Agent(交互增强)")
  248. print("=" * 60)
  249. print()
  250. print("💡 交互提示:")
  251. print(" - 执行过程中输入 'p' 或 'pause' 暂停并进入交互模式")
  252. print(" - 执行过程中输入 'q' 或 'quit' 停止执行")
  253. print("=" * 60)
  254. print()
  255. # 在读取 prompt 前生成 log_id(格式 yyyyMMddHHmmss),保证每次运行使用同一 log_id(用于推导日志输出路径)
  256. log_id = datetime.now().strftime("%Y%m%d%H%M%S")
  257. print(f" - 本次运行 log_id: {log_id}")
  258. print(f" - account_name: {account_name}")
  259. print(f" - post_id: {post_id}")
  260. # 读取选题点列表,得到 post_point_count(用于 prompt 占位符)
  261. input_dir = base_dir / "input" / account_name / "post_topic"
  262. post_topic_path = input_dir / f"{post_id}.json"
  263. post_point_count = 0
  264. if post_topic_path.exists():
  265. import json
  266. with open(post_topic_path, "r", encoding="utf-8") as f:
  267. post_topics = json.load(f)
  268. post_point_count = len(post_topics) if isinstance(post_topics, list) else 0
  269. print(f" - 选题点数量 post_point_count: {post_point_count} (来自 {post_topic_path.relative_to(base_dir)})")
  270. else:
  271. print(f" - 未找到选题点文件: {post_topic_path},post_point_count 使用 0")
  272. print("1. 加载 prompt 配置...")
  273. prompt = SimplePrompt(prompt_path)
  274. print("2. 构建任务消息...")
  275. messages = prompt.build_messages()
  276. _replace_prompt_placeholders(messages, account_name, post_id, log_id, post_point_count)
  277. print("3. 创建 Agent Runner...")
  278. print(f" - Skills 目录: {skills_dir}")
  279. model_key = prompt.config.get("model", "google/gemini-3-flash-preview")
  280. # model_id = f"google/{model_key}" if not model_key.startswith("google/") else model_key
  281. model_id = model_key
  282. print(f" - 模型: {model_id}")
  283. store = FileSystemTraceStore(base_path=".trace")
  284. runner = AgentRunner(
  285. trace_store=store,
  286. llm_call=create_openrouter_llm_call(model=model_id),
  287. skills_dir=skills_dir,
  288. # experiences_path="./.cache/experiences_overall_derivation.md",
  289. debug=True,
  290. )
  291. resume_trace_id = args.trace
  292. if resume_trace_id:
  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("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. if resume_trace_id:
  310. initial_messages = None
  311. config = RunConfig(
  312. model=model_id,
  313. temperature=float(prompt.config.get("temperature", 0.3)),
  314. max_iterations=200,
  315. trace_id=resume_trace_id,
  316. )
  317. else:
  318. initial_messages = messages
  319. config = RunConfig(
  320. model=model_id,
  321. temperature=float(prompt.config.get("temperature", 0.3)),
  322. max_iterations=200,
  323. name="选题点整体推导任务",
  324. )
  325. while not should_exit:
  326. if current_trace_id:
  327. config.trace_id = current_trace_id
  328. final_response = ""
  329. if current_trace_id and initial_messages is None:
  330. check_trace = await store.get_trace(current_trace_id)
  331. if check_trace and check_trace.status in ("completed", "failed"):
  332. if check_trace.status == "completed":
  333. print("\n[Trace] ✅ 已完成")
  334. print(f" - Total messages: {check_trace.total_messages}")
  335. print(f" - Total cost: ${check_trace.total_cost:.4f}")
  336. else:
  337. print(f"\n[Trace] ❌ 已失败: {check_trace.error_message}")
  338. current_sequence = check_trace.head_sequence
  339. menu_result = await show_interactive_menu(
  340. runner, current_trace_id, current_sequence, store
  341. )
  342. if menu_result["action"] == "stop":
  343. break
  344. elif menu_result["action"] == "continue":
  345. new_messages = menu_result.get("messages", [])
  346. if new_messages:
  347. initial_messages = new_messages
  348. config.after_sequence = menu_result.get("after_sequence")
  349. else:
  350. initial_messages = []
  351. config.after_sequence = None
  352. continue
  353. break
  354. initial_messages = []
  355. print(f"{'▶️ 开始执行...' if not current_trace_id else '▶️ 继续执行...'}")
  356. paused = False
  357. try:
  358. async for item in runner.run(messages=initial_messages, config=config):
  359. cmd = check_stdin()
  360. if cmd == 'pause':
  361. print("\n⏸️ 正在暂停执行...")
  362. if current_trace_id:
  363. await runner.stop(current_trace_id)
  364. await asyncio.sleep(0.5)
  365. menu_result = await show_interactive_menu(
  366. runner, current_trace_id, current_sequence, store
  367. )
  368. if menu_result["action"] == "stop":
  369. should_exit = True
  370. paused = True
  371. break
  372. elif menu_result["action"] == "continue":
  373. new_messages = menu_result.get("messages", [])
  374. if new_messages:
  375. initial_messages = new_messages
  376. after_seq = menu_result.get("after_sequence")
  377. if after_seq is not None:
  378. config.after_sequence = after_seq
  379. paused = True
  380. break
  381. else:
  382. initial_messages = []
  383. config.after_sequence = None
  384. paused = True
  385. break
  386. elif cmd == 'quit':
  387. print("\n🛑 用户请求停止...")
  388. if current_trace_id:
  389. await runner.stop(current_trace_id)
  390. should_exit = True
  391. break
  392. if isinstance(item, Trace):
  393. current_trace_id = item.trace_id
  394. if item.status == "running":
  395. print(f"[Trace] 开始: {item.trace_id[:100]}...")
  396. elif item.status == "completed":
  397. print("\n[Trace] ✅ 完成")
  398. print(f" - Total messages: {item.total_messages}")
  399. print(f" - Total tokens: {item.total_tokens}")
  400. print(f" - Total cost: ${item.total_cost:.4f}")
  401. elif item.status == "failed":
  402. print(f"\n[Trace] ❌ 失败: {item.error_message}")
  403. elif item.status == "stopped":
  404. print("\n[Trace] ⏸️ 已停止")
  405. elif isinstance(item, Message):
  406. current_sequence = item.sequence
  407. if item.role == "assistant":
  408. content = item.content
  409. if isinstance(content, dict):
  410. text = content.get("text", "")
  411. tool_calls = content.get("tool_calls")
  412. if text and not tool_calls:
  413. final_response = text
  414. print("\n[Response] Agent 回复:")
  415. print(text)
  416. elif text:
  417. preview = text[:500] + "..." if len(text) > 500 else text
  418. print(f"[Assistant] {preview}")
  419. if tool_calls:
  420. for tc in tool_calls:
  421. tool_name = tc.get("function", {}).get("name", "unknown")
  422. tool_args = tc.get("function", {}).get("arguments", "")
  423. print(f"[Tool Call] 🛠️ {tool_name}")
  424. print(f" params: {tool_args}")
  425. elif item.role == "tool":
  426. content = item.content
  427. if isinstance(content, dict):
  428. tool_name = content.get("tool_name", "unknown")
  429. tool_result = content.get("result", content)
  430. print(f"[Tool Result] ✅ {tool_name}")
  431. print(f" result: {tool_result}")
  432. if item.description:
  433. desc = item.description[:500] if len(item.description) > 500 else item.description
  434. print(f" {desc}...")
  435. except Exception as e:
  436. print(f"\n执行出错: {e}")
  437. import traceback
  438. traceback.print_exc()
  439. if paused:
  440. if should_exit:
  441. break
  442. continue
  443. if should_exit:
  444. break
  445. if current_trace_id:
  446. menu_result = await show_interactive_menu(
  447. runner, current_trace_id, current_sequence, store
  448. )
  449. if menu_result["action"] == "stop":
  450. break
  451. elif menu_result["action"] == "continue":
  452. new_messages = menu_result.get("messages", [])
  453. if new_messages:
  454. initial_messages = new_messages
  455. config.after_sequence = menu_result.get("after_sequence")
  456. else:
  457. initial_messages = []
  458. config.after_sequence = None
  459. continue
  460. break
  461. except KeyboardInterrupt:
  462. print("\n\n用户中断 (Ctrl+C)")
  463. if current_trace_id:
  464. await runner.stop(current_trace_id)
  465. if final_response:
  466. print()
  467. print("=" * 60)
  468. print("Agent 响应:")
  469. print("=" * 60)
  470. print(final_response)
  471. print("=" * 60)
  472. print()
  473. output_file = output_dir / account_name / "推导日志" / current_trace_id / log_id / "result.txt"
  474. with open(output_file, 'w', encoding='utf-8') as f:
  475. f.write(final_response)
  476. print(f"✓ 结果已保存到: {output_file}")
  477. print()
  478. if current_trace_id:
  479. print("=" * 60)
  480. print("可视化 Step Tree:")
  481. print("=" * 60)
  482. print("1. 启动 API Server:")
  483. print(" python3 api_server.py")
  484. print()
  485. print("2. 浏览器访问:")
  486. print(" http://localhost:8000/api/traces")
  487. print()
  488. print(f"3. Trace ID: {current_trace_id}")
  489. print(f"4. Log ID(推导日志目录): {log_id}")
  490. print("=" * 60)
  491. if __name__ == "__main__":
  492. # anthropic/claude-sonnet-4.6
  493. # google/gemini-3-flash-preview
  494. asyncio.run(main(account_name="家有大志", post_id="68fb6a5c000000000302e5de"))