|
@@ -0,0 +1,358 @@
|
|
|
|
|
+"""
|
|
|
|
|
+示例(简化版 - 使用框架交互功能)
|
|
|
|
|
+
|
|
|
|
|
+使用 Agent 模式 + Skills + 框架交互控制器
|
|
|
|
|
+
|
|
|
|
|
+新功能:
|
|
|
|
|
+1. 使用框架提供的 InteractiveController
|
|
|
|
|
+2. 使用配置文件管理运行参数
|
|
|
|
|
+3. 支持命令行随时打断(输入 'p' 暂停,'q' 退出)
|
|
|
|
|
+4. 暂停后可插入干预消息
|
|
|
|
|
+5. 支持触发经验总结
|
|
|
|
|
+6. 查看当前 GoalTree
|
|
|
|
|
+7. 支持通过 --trace <ID> 恢复已有 Trace 继续执行
|
|
|
|
|
+"""
|
|
|
|
|
+
|
|
|
|
|
+import argparse
|
|
|
|
|
+import os
|
|
|
|
|
+import sys
|
|
|
|
|
+import asyncio
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+
|
|
|
|
|
+# Clash Verge TUN 模式兼容:禁止 httpx/urllib 自动检测系统 HTTP 代理
|
|
|
|
|
+os.environ.setdefault("no_proxy", "*")
|
|
|
|
|
+
|
|
|
|
|
+# 添加项目根目录到 Python 路径
|
|
|
|
|
+sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
|
|
|
|
+
|
|
|
|
|
+from dotenv import load_dotenv
|
|
|
|
|
+load_dotenv()
|
|
|
|
|
+
|
|
|
|
|
+from agent.llm.prompts import SimplePrompt
|
|
|
|
|
+from agent.core.runner import AgentRunner, RunConfig
|
|
|
|
|
+from agent.core.presets import AgentPreset, register_preset
|
|
|
|
|
+from agent.trace import (
|
|
|
|
|
+ FileSystemTraceStore,
|
|
|
|
|
+ Trace,
|
|
|
|
|
+ Message,
|
|
|
|
|
+)
|
|
|
|
|
+from agent.llm import create_openrouter_llm_call
|
|
|
|
|
+from agent.cli import InteractiveController
|
|
|
|
|
+from agent.utils import setup_logging
|
|
|
|
|
+from agent.tools.builtin.browser.baseClass import init_browser_session, kill_browser_session
|
|
|
|
|
+
|
|
|
|
|
+# 导入项目配置
|
|
|
|
|
+from config import RUN_CONFIG, SKILLS_DIR, TRACE_STORE_PATH, DEBUG, LOG_LEVEL, LOG_FILE, BROWSER_TYPE, HEADLESS
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+async def main():
|
|
|
|
|
+ # 解析命令行参数
|
|
|
|
|
+ parser = argparse.ArgumentParser(description="任务 (Agent 模式 + 交互增强)")
|
|
|
|
|
+ parser.add_argument(
|
|
|
|
|
+ "--trace", type=str, default=None,
|
|
|
|
|
+ help="已有的 Trace ID,用于恢复继续执行(不指定则新建)",
|
|
|
|
|
+ )
|
|
|
|
|
+ args = parser.parse_args()
|
|
|
|
|
+
|
|
|
|
|
+ # 路径配置
|
|
|
|
|
+ base_dir = Path(__file__).parent
|
|
|
|
|
+ project_root = base_dir.parent.parent
|
|
|
|
|
+ prompt_path = base_dir / "research.prompt"
|
|
|
|
|
+ output_dir = base_dir / "output_1"
|
|
|
|
|
+ output_dir.mkdir(exist_ok=True)
|
|
|
|
|
+
|
|
|
|
|
+ # 1. 配置日志
|
|
|
|
|
+ setup_logging(level=LOG_LEVEL, file=LOG_FILE)
|
|
|
|
|
+
|
|
|
|
|
+ # 2. 加载项目级 presets
|
|
|
|
|
+ print("2. 加载 presets...")
|
|
|
|
|
+ presets_path = base_dir / "presets.json"
|
|
|
|
|
+ if presets_path.exists():
|
|
|
|
|
+ import json
|
|
|
|
|
+ with open(presets_path, "r", encoding="utf-8") as f:
|
|
|
|
|
+ project_presets = json.load(f)
|
|
|
|
|
+ for name, cfg in project_presets.items():
|
|
|
|
|
+ register_preset(name, AgentPreset(**cfg))
|
|
|
|
|
+ print(f" - 已加载项目 presets: {list(project_presets.keys())}")
|
|
|
|
|
+
|
|
|
|
|
+ # 3. 加载 prompt
|
|
|
|
|
+ print("3. 加载 prompt...")
|
|
|
|
|
+ prompt = SimplePrompt(prompt_path)
|
|
|
|
|
+
|
|
|
|
|
+ # 4. 构建任务消息
|
|
|
|
|
+ print("4. 构建任务消息...")
|
|
|
|
|
+ messages = prompt.build_messages()
|
|
|
|
|
+
|
|
|
|
|
+ # 5. 初始化浏览器
|
|
|
|
|
+ import platform
|
|
|
|
|
+ actual_browser_type = BROWSER_TYPE
|
|
|
|
|
+ if platform.system() == "Windows" and BROWSER_TYPE == "local":
|
|
|
|
|
+ actual_browser_type = "cloud"
|
|
|
|
|
+ print("⚠️ Windows 平台检测到本地浏览器配置,自动切换为云浏览器模式")
|
|
|
|
|
+
|
|
|
|
|
+ browser_mode_name = "云浏览器" if actual_browser_type == "cloud" else "本地浏览器"
|
|
|
|
|
+ print(f"5. 正在初始化{browser_mode_name}...")
|
|
|
|
|
+ await init_browser_session(
|
|
|
|
|
+ browser_type=actual_browser_type,
|
|
|
|
|
+ headless=HEADLESS,
|
|
|
|
|
+ url="about:blank"
|
|
|
|
|
+ )
|
|
|
|
|
+ print(f" ✅ {browser_mode_name}初始化完成\n")
|
|
|
|
|
+
|
|
|
|
|
+ # 6. 创建 Agent Runner
|
|
|
|
|
+ print("6. 创建 Agent Runner...")
|
|
|
|
|
+ print(f" - Skills 目录: {SKILLS_DIR}")
|
|
|
|
|
+ print(f" - 模型: {RUN_CONFIG.model}")
|
|
|
|
|
+
|
|
|
|
|
+ store = FileSystemTraceStore(base_path=TRACE_STORE_PATH)
|
|
|
|
|
+ runner = AgentRunner(
|
|
|
|
|
+ trace_store=store,
|
|
|
|
|
+ llm_call=create_openrouter_llm_call(model=f"anthropic/{RUN_CONFIG.model}"),
|
|
|
|
|
+ skills_dir=SKILLS_DIR,
|
|
|
|
|
+ debug=DEBUG
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # 7. 创建交互控制器
|
|
|
|
|
+ interactive = InteractiveController(
|
|
|
|
|
+ runner=runner,
|
|
|
|
|
+ store=store,
|
|
|
|
|
+ enable_stdin_check=True
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # 8. 任务信息
|
|
|
|
|
+ task_name = RUN_CONFIG.name or base_dir.name
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+ print(f"{task_name}")
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+ print("💡 交互提示:")
|
|
|
|
|
+ print(" - 执行过程中输入 'p' 或 'pause' 暂停并进入交互模式")
|
|
|
|
|
+ print(" - 执行过程中输入 'q' 或 'quit' 停止执行")
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+ print()
|
|
|
|
|
+
|
|
|
|
|
+ # 9. 判断是新建还是恢复
|
|
|
|
|
+ resume_trace_id = args.trace
|
|
|
|
|
+ if resume_trace_id:
|
|
|
|
|
+ existing_trace = await store.get_trace(resume_trace_id)
|
|
|
|
|
+ if not existing_trace:
|
|
|
|
|
+ print(f"\n错误: Trace 不存在: {resume_trace_id}")
|
|
|
|
|
+ sys.exit(1)
|
|
|
|
|
+ print(f"恢复已有 Trace: {resume_trace_id[:8]}...")
|
|
|
|
|
+ print(f" - 状态: {existing_trace.status}")
|
|
|
|
|
+ print(f" - 消息数: {existing_trace.total_messages}")
|
|
|
|
|
+ else:
|
|
|
|
|
+ print(f"启动新 Agent...")
|
|
|
|
|
+
|
|
|
|
|
+ print()
|
|
|
|
|
+
|
|
|
|
|
+ final_response = ""
|
|
|
|
|
+ current_trace_id = resume_trace_id
|
|
|
|
|
+ current_sequence = 0
|
|
|
|
|
+ should_exit = False
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ # 配置
|
|
|
|
|
+ run_config = RUN_CONFIG
|
|
|
|
|
+ if resume_trace_id:
|
|
|
|
|
+ initial_messages = None
|
|
|
|
|
+ run_config.trace_id = resume_trace_id
|
|
|
|
|
+ else:
|
|
|
|
|
+ initial_messages = messages
|
|
|
|
|
+ run_config.name = f"{task_name}:调研任务"
|
|
|
|
|
+
|
|
|
|
|
+ while not should_exit:
|
|
|
|
|
+ if current_trace_id:
|
|
|
|
|
+ run_config.trace_id = current_trace_id
|
|
|
|
|
+
|
|
|
|
|
+ final_response = ""
|
|
|
|
|
+
|
|
|
|
|
+ # 如果 trace 已完成/失败且没有新消息,进入交互菜单
|
|
|
|
|
+ if current_trace_id and initial_messages is None:
|
|
|
|
|
+ check_trace = await store.get_trace(current_trace_id)
|
|
|
|
|
+ if check_trace and check_trace.status in ("completed", "failed"):
|
|
|
|
|
+ if check_trace.status == "completed":
|
|
|
|
|
+ print(f"\n[Trace] ✅ 已完成")
|
|
|
|
|
+ print(f" - Total messages: {check_trace.total_messages}")
|
|
|
|
|
+ print(f" - Total cost: ${check_trace.total_cost:.4f}")
|
|
|
|
|
+ else:
|
|
|
|
|
+ print(f"\n[Trace] ❌ 已失败: {check_trace.error_message}")
|
|
|
|
|
+ current_sequence = check_trace.head_sequence
|
|
|
|
|
+
|
|
|
|
|
+ menu_result = await interactive.show_menu(current_trace_id, current_sequence)
|
|
|
|
|
+
|
|
|
|
|
+ if menu_result["action"] == "stop":
|
|
|
|
|
+ break
|
|
|
|
|
+ elif menu_result["action"] == "continue":
|
|
|
|
|
+ new_messages = menu_result.get("messages", [])
|
|
|
|
|
+ if new_messages:
|
|
|
|
|
+ initial_messages = new_messages
|
|
|
|
|
+ run_config.after_sequence = menu_result.get("after_sequence")
|
|
|
|
|
+ else:
|
|
|
|
|
+ initial_messages = []
|
|
|
|
|
+ run_config.after_sequence = None
|
|
|
|
|
+ continue
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ initial_messages = []
|
|
|
|
|
+
|
|
|
|
|
+ print(f"{'▶️ 开始执行...' if not current_trace_id else '▶️ 继续执行...'}")
|
|
|
|
|
+
|
|
|
|
|
+ # 执行 Agent
|
|
|
|
|
+ paused = False
|
|
|
|
|
+ try:
|
|
|
|
|
+ async for item in runner.run(messages=initial_messages, config=run_config):
|
|
|
|
|
+ # 检查用户中断
|
|
|
|
|
+ cmd = interactive.check_stdin()
|
|
|
|
|
+ if cmd == 'pause':
|
|
|
|
|
+ print("\n⏸️ 正在暂停执行...")
|
|
|
|
|
+ if current_trace_id:
|
|
|
|
|
+ await runner.stop(current_trace_id)
|
|
|
|
|
+ await asyncio.sleep(0.5)
|
|
|
|
|
+
|
|
|
|
|
+ menu_result = await interactive.show_menu(current_trace_id, current_sequence)
|
|
|
|
|
+
|
|
|
|
|
+ if menu_result["action"] == "stop":
|
|
|
|
|
+ should_exit = True
|
|
|
|
|
+ paused = True
|
|
|
|
|
+ break
|
|
|
|
|
+ elif menu_result["action"] == "continue":
|
|
|
|
|
+ new_messages = menu_result.get("messages", [])
|
|
|
|
|
+ if new_messages:
|
|
|
|
|
+ initial_messages = new_messages
|
|
|
|
|
+ after_seq = menu_result.get("after_sequence")
|
|
|
|
|
+ if after_seq is not None:
|
|
|
|
|
+ run_config.after_sequence = after_seq
|
|
|
|
|
+ paused = True
|
|
|
|
|
+ break
|
|
|
|
|
+ else:
|
|
|
|
|
+ initial_messages = []
|
|
|
|
|
+ run_config.after_sequence = None
|
|
|
|
|
+ paused = True
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ elif cmd == 'quit':
|
|
|
|
|
+ print("\n🛑 用户请求停止...")
|
|
|
|
|
+ if current_trace_id:
|
|
|
|
|
+ await runner.stop(current_trace_id)
|
|
|
|
|
+ should_exit = True
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ # 处理 Trace 对象
|
|
|
|
|
+ if isinstance(item, Trace):
|
|
|
|
|
+ current_trace_id = item.trace_id
|
|
|
|
|
+ if item.status == "running":
|
|
|
|
|
+ print(f"[Trace] 开始: {item.trace_id[:8]}...")
|
|
|
|
|
+ elif item.status == "completed":
|
|
|
|
|
+ print(f"\n[Trace] ✅ 完成")
|
|
|
|
|
+ print(f" - Total messages: {item.total_messages}")
|
|
|
|
|
+ print(f" - Total cost: ${item.total_cost:.4f}")
|
|
|
|
|
+ elif item.status == "failed":
|
|
|
|
|
+ print(f"\n[Trace] ❌ 失败: {item.error_message}")
|
|
|
|
|
+ elif item.status == "stopped":
|
|
|
|
|
+ print(f"\n[Trace] ⏸️ 已停止")
|
|
|
|
|
+
|
|
|
|
|
+ # 处理 Message 对象
|
|
|
|
|
+ elif isinstance(item, Message):
|
|
|
|
|
+ current_sequence = item.sequence
|
|
|
|
|
+
|
|
|
|
|
+ if item.role == "assistant":
|
|
|
|
|
+ content = item.content
|
|
|
|
|
+ if isinstance(content, dict):
|
|
|
|
|
+ text = content.get("text", "")
|
|
|
|
|
+ tool_calls = content.get("tool_calls")
|
|
|
|
|
+
|
|
|
|
|
+ if text and not tool_calls:
|
|
|
|
|
+ final_response = text
|
|
|
|
|
+ print(f"\n[Response] Agent 回复:")
|
|
|
|
|
+ print(text)
|
|
|
|
|
+ elif text:
|
|
|
|
|
+ preview = text[:150] + "..." if len(text) > 150 else text
|
|
|
|
|
+ print(f"[Assistant] {preview}")
|
|
|
|
|
+
|
|
|
|
|
+ elif item.role == "tool":
|
|
|
|
|
+ content = item.content
|
|
|
|
|
+ tool_name = "unknown"
|
|
|
|
|
+ if isinstance(content, dict):
|
|
|
|
|
+ tool_name = content.get("tool_name", "unknown")
|
|
|
|
|
+
|
|
|
|
|
+ if item.description and item.description != tool_name:
|
|
|
|
|
+ desc = item.description[:80] if len(item.description) > 80 else item.description
|
|
|
|
|
+ print(f"[Tool Result] ✅ {tool_name}: {desc}...")
|
|
|
|
|
+ else:
|
|
|
|
|
+ print(f"[Tool Result] ✅ {tool_name}")
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(f"\n执行出错: {e}")
|
|
|
|
|
+ import traceback
|
|
|
|
|
+ traceback.print_exc()
|
|
|
|
|
+
|
|
|
|
|
+ if paused:
|
|
|
|
|
+ if should_exit:
|
|
|
|
|
+ break
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ if should_exit:
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ # Runner 退出后显示交互菜单
|
|
|
|
|
+ if current_trace_id:
|
|
|
|
|
+ menu_result = await interactive.show_menu(current_trace_id, current_sequence)
|
|
|
|
|
+
|
|
|
|
|
+ if menu_result["action"] == "stop":
|
|
|
|
|
+ break
|
|
|
|
|
+ elif menu_result["action"] == "continue":
|
|
|
|
|
+ new_messages = menu_result.get("messages", [])
|
|
|
|
|
+ if new_messages:
|
|
|
|
|
+ initial_messages = new_messages
|
|
|
|
|
+ run_config.after_sequence = menu_result.get("after_sequence")
|
|
|
|
|
+ else:
|
|
|
|
|
+ initial_messages = []
|
|
|
|
|
+ run_config.after_sequence = None
|
|
|
|
|
+ continue
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ except KeyboardInterrupt:
|
|
|
|
|
+ print("\n\n用户中断 (Ctrl+C)")
|
|
|
|
|
+ if current_trace_id:
|
|
|
|
|
+ await runner.stop(current_trace_id)
|
|
|
|
|
+ finally:
|
|
|
|
|
+ # 清理浏览器会话
|
|
|
|
|
+ try:
|
|
|
|
|
+ await kill_browser_session()
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ # 7. 输出结果
|
|
|
|
|
+ if final_response:
|
|
|
|
|
+ print()
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+ print("Agent 响应:")
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+ print(final_response)
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+ print()
|
|
|
|
|
+
|
|
|
|
|
+ output_file = output_dir / "result.txt"
|
|
|
|
|
+ with open(output_file, 'w', encoding='utf-8') as f:
|
|
|
|
|
+ f.write(final_response)
|
|
|
|
|
+
|
|
|
|
|
+ print(f"✓ 结果已保存到: {output_file}")
|
|
|
|
|
+ print()
|
|
|
|
|
+
|
|
|
|
|
+ # 可视化提示
|
|
|
|
|
+ if current_trace_id:
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+ print("可视化 Step Tree:")
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+ print("1. 启动 API Server:")
|
|
|
|
|
+ print(" python3 api_server.py")
|
|
|
|
|
+ print()
|
|
|
|
|
+ print("2. 浏览器访问:")
|
|
|
|
|
+ print(" http://localhost:8000/api/traces")
|
|
|
|
|
+ print()
|
|
|
|
|
+ print(f"3. Trace ID: {current_trace_id}")
|
|
|
|
|
+ print("=" * 60)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ asyncio.run(main())
|