|
|
@@ -1,126 +1,480 @@
|
|
|
-import asyncio
|
|
|
-import logging
|
|
|
+"""
|
|
|
+选题点整体推导 Agent(增强版)
|
|
|
+
|
|
|
+参考 examples/how/run.py,提供:
|
|
|
+1. 命令行交互:输入 'p' 暂停、'q' 退出
|
|
|
+2. 暂停后可插入干预消息、触发经验总结、查看 GoalTree、手动压缩上下文
|
|
|
+3. 支持 --trace <ID> 恢复已有 Trace 继续执行
|
|
|
+4. 使用 SimplePrompt 加载 production.prompt,支持评估子 agent(agent_type=evaluate_derivation)
|
|
|
+"""
|
|
|
+
|
|
|
+import argparse
|
|
|
+import os
|
|
|
import sys
|
|
|
+import select
|
|
|
+import asyncio
|
|
|
from pathlib import Path
|
|
|
-from agent import AgentRunner, RunConfig
|
|
|
-from agent.llm import create_openrouter_llm_call
|
|
|
+
|
|
|
+# 与 examples/how/run.py 一致:禁止 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.trace.compaction import build_reflect_prompt
|
|
|
|
|
|
-# 配置日志,只显示错误信息(避免干扰最终输出)
|
|
|
-logging.basicConfig(
|
|
|
- level=logging.WARNING,
|
|
|
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
- stream=sys.stderr
|
|
|
-)
|
|
|
|
|
|
-DEFAULT_MODEL="google/gemini-3-flash-preview"
|
|
|
+# ===== 非阻塞 stdin 检测 =====
|
|
|
+if sys.platform == 'win32':
|
|
|
+ import msvcrt
|
|
|
|
|
|
-store = FileSystemTraceStore(base_path=".trace")
|
|
|
-runner = AgentRunner(
|
|
|
- trace_store=store,
|
|
|
- llm_call=create_openrouter_llm_call(model=DEFAULT_MODEL),
|
|
|
-)
|
|
|
|
|
|
-def print_step(step_num, description, details=""):
|
|
|
- """打印步骤信息"""
|
|
|
- print(f"\n[步骤 {step_num}] {description}", file=sys.stderr)
|
|
|
- if details:
|
|
|
- print(f" {details}", file=sys.stderr)
|
|
|
+def check_stdin() -> str | None:
|
|
|
+ """
|
|
|
+ 跨平台非阻塞检查 stdin 输入。
|
|
|
+ Windows: msvcrt.kbhit();macOS/Linux: select.select()
|
|
|
+ """
|
|
|
+ if sys.platform == 'win32':
|
|
|
+ if msvcrt.kbhit():
|
|
|
+ ch = msvcrt.getwch().lower()
|
|
|
+ if ch == 'p':
|
|
|
+ return 'pause'
|
|
|
+ if ch == 'q':
|
|
|
+ return 'quit'
|
|
|
+ return None
|
|
|
+ else:
|
|
|
+ ready, _, _ = select.select([sys.stdin], [], [], 0)
|
|
|
+ if ready:
|
|
|
+ line = sys.stdin.readline().strip().lower()
|
|
|
+ if line in ('p', 'pause'):
|
|
|
+ return 'pause'
|
|
|
+ if line in ('q', 'quit'):
|
|
|
+ return 'quit'
|
|
|
+ return None
|
|
|
+
|
|
|
+
|
|
|
+# ===== 交互菜单 =====
|
|
|
+
|
|
|
+def _read_multiline() -> str:
|
|
|
+ """读取多行输入,以连续两次回车(空行)结束。"""
|
|
|
+ print("\n请输入干预消息(连续输入两次回车结束):")
|
|
|
+ lines: list[str] = []
|
|
|
+ blank_count = 0
|
|
|
+ while True:
|
|
|
+ line = input()
|
|
|
+ if line == "":
|
|
|
+ blank_count += 1
|
|
|
+ if blank_count >= 2:
|
|
|
+ break
|
|
|
+ lines.append("")
|
|
|
+ else:
|
|
|
+ blank_count = 0
|
|
|
+ lines.append(line)
|
|
|
+ while lines and lines[-1] == "":
|
|
|
+ lines.pop()
|
|
|
+ return "\n".join(lines)
|
|
|
+
|
|
|
+
|
|
|
+async def show_interactive_menu(
|
|
|
+ runner: AgentRunner,
|
|
|
+ trace_id: str,
|
|
|
+ current_sequence: int,
|
|
|
+ store: FileSystemTraceStore,
|
|
|
+):
|
|
|
+ """显示交互式菜单,让用户选择操作。"""
|
|
|
+ print("\n" + "=" * 60)
|
|
|
+ print(" 执行已暂停")
|
|
|
+ print("=" * 60)
|
|
|
+ print("请选择操作:")
|
|
|
+ print(" 1. 插入干预消息并继续")
|
|
|
+ print(" 2. 触发经验总结(reflect)")
|
|
|
+ print(" 3. 查看当前 GoalTree")
|
|
|
+ print(" 4. 手动压缩上下文(compact)")
|
|
|
+ print(" 5. 继续执行")
|
|
|
+ print(" 6. 停止执行")
|
|
|
+ print("=" * 60)
|
|
|
+
|
|
|
+ while True:
|
|
|
+ choice = input("请输入选项 (1-6): ").strip()
|
|
|
+
|
|
|
+ if choice == "1":
|
|
|
+ text = _read_multiline()
|
|
|
+ if not text:
|
|
|
+ print("未输入任何内容,取消操作")
|
|
|
+ continue
|
|
|
+ print("\n将插入干预消息并继续执行...")
|
|
|
+ live_trace = await store.get_trace(trace_id)
|
|
|
+ actual_sequence = live_trace.last_sequence if live_trace and live_trace.last_sequence else current_sequence
|
|
|
+ return {
|
|
|
+ "action": "continue",
|
|
|
+ "messages": [{"role": "user", "content": text}],
|
|
|
+ "after_sequence": actual_sequence,
|
|
|
+ }
|
|
|
+
|
|
|
+ elif choice == "2":
|
|
|
+ print("\n触发经验总结...")
|
|
|
+ focus = input("请输入反思重点(可选,直接回车跳过): ").strip()
|
|
|
+ trace = await store.get_trace(trace_id)
|
|
|
+ saved_head = trace.head_sequence
|
|
|
+ prompt = build_reflect_prompt()
|
|
|
+ if focus:
|
|
|
+ prompt += f"\n\n请特别关注:{focus}"
|
|
|
+ print("正在生成反思...")
|
|
|
+ reflect_cfg = RunConfig(trace_id=trace_id, max_iterations=1, tools=[])
|
|
|
+ reflection_text = ""
|
|
|
+ try:
|
|
|
+ result = await runner.run_result(
|
|
|
+ messages=[{"role": "user", "content": prompt}],
|
|
|
+ config=reflect_cfg,
|
|
|
+ )
|
|
|
+ reflection_text = result.get("summary", "")
|
|
|
+ finally:
|
|
|
+ await store.update_trace(trace_id, head_sequence=saved_head)
|
|
|
+ if reflection_text:
|
|
|
+ from datetime import datetime
|
|
|
+ experiences_path = runner.experiences_path or "./.cache/experiences_overall_derivation.md"
|
|
|
+ os.makedirs(os.path.dirname(experiences_path), exist_ok=True)
|
|
|
+ header = f"\n\n---\n\n## {trace_id} ({datetime.now().strftime('%Y-%m-%d %H:%M')})\n\n"
|
|
|
+ with open(experiences_path, "a", encoding="utf-8") as f:
|
|
|
+ f.write(header + reflection_text + "\n")
|
|
|
+ print(f"\n反思已保存到: {experiences_path}")
|
|
|
+ print("\n--- 反思内容 ---")
|
|
|
+ print(reflection_text)
|
|
|
+ print("--- 结束 ---\n")
|
|
|
+ else:
|
|
|
+ print("未生成反思内容")
|
|
|
+ continue
|
|
|
+
|
|
|
+ elif choice == "3":
|
|
|
+ goal_tree = await store.get_goal_tree(trace_id)
|
|
|
+ if goal_tree and goal_tree.goals:
|
|
|
+ print("\n当前 GoalTree:")
|
|
|
+ print(goal_tree.to_prompt())
|
|
|
+ else:
|
|
|
+ print("\n当前没有 Goal")
|
|
|
+ continue
|
|
|
+
|
|
|
+ elif choice == "4":
|
|
|
+ print("\n正在执行上下文压缩(compact)...")
|
|
|
+ try:
|
|
|
+ goal_tree = await store.get_goal_tree(trace_id)
|
|
|
+ trace = await store.get_trace(trace_id)
|
|
|
+ if not trace:
|
|
|
+ print("未找到 Trace,无法压缩")
|
|
|
+ continue
|
|
|
+ main_path = await store.get_main_path_messages(trace_id, trace.head_sequence)
|
|
|
+ history = [msg.to_llm_dict() for msg in main_path]
|
|
|
+ head_seq = main_path[-1].sequence if main_path else 0
|
|
|
+ next_seq = head_seq + 1
|
|
|
+ compact_config = RunConfig(trace_id=trace_id)
|
|
|
+ new_history, new_head, new_seq = await runner._compress_history(
|
|
|
+ trace_id=trace_id,
|
|
|
+ history=history,
|
|
|
+ goal_tree=goal_tree,
|
|
|
+ config=compact_config,
|
|
|
+ sequence=next_seq,
|
|
|
+ head_seq=head_seq,
|
|
|
+ )
|
|
|
+ print(f"\n✅ 压缩完成: {len(history)} 条消息 → {len(new_history)} 条")
|
|
|
+ except Exception as e:
|
|
|
+ print(f"\n❌ 压缩失败: {e}")
|
|
|
+ continue
|
|
|
+
|
|
|
+ elif choice == "5":
|
|
|
+ print("\n继续执行...")
|
|
|
+ return {"action": "continue"}
|
|
|
+
|
|
|
+ elif choice == "6":
|
|
|
+ print("\n停止执行...")
|
|
|
+ return {"action": "stop"}
|
|
|
+
|
|
|
+ else:
|
|
|
+ print("无效选项,请重新输入")
|
|
|
+
|
|
|
|
|
|
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 / "production.prompt"
|
|
|
+ output_dir = base_dir / "output"
|
|
|
+ output_dir.mkdir(exist_ok=True)
|
|
|
+
|
|
|
+ # 加载项目级 presets(evaluate_derivation 等)
|
|
|
+ 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())}")
|
|
|
+
|
|
|
+ skills_dir = str(base_dir / "skills")
|
|
|
+
|
|
|
+ print("=" * 60)
|
|
|
+ print("选题点整体推导 Agent(交互增强)")
|
|
|
+ print("=" * 60)
|
|
|
+ print()
|
|
|
+ print("💡 交互提示:")
|
|
|
+ print(" - 执行过程中输入 'p' 或 'pause' 暂停并进入交互模式")
|
|
|
+ print(" - 执行过程中输入 'q' 或 'quit' 停止执行")
|
|
|
+ print("=" * 60)
|
|
|
+ print()
|
|
|
+
|
|
|
+ print("1. 加载 prompt 配置...")
|
|
|
+ prompt = SimplePrompt(prompt_path)
|
|
|
+
|
|
|
+ print("2. 构建任务消息...")
|
|
|
+ messages = prompt.build_messages()
|
|
|
+
|
|
|
+ print("3. 创建 Agent Runner...")
|
|
|
+ print(f" - Skills 目录: {skills_dir}")
|
|
|
+ model_key = prompt.config.get("model", "gemini-3-flash-preview")
|
|
|
+ model_id = f"google/{model_key}" if not model_key.startswith("google/") else model_key
|
|
|
+ print(f" - 模型: {model_id}")
|
|
|
+
|
|
|
+ store = FileSystemTraceStore(base_path=".trace")
|
|
|
+ runner = AgentRunner(
|
|
|
+ trace_store=store,
|
|
|
+ llm_call=create_openrouter_llm_call(model=model_id),
|
|
|
+ skills_dir=skills_dir,
|
|
|
+ experiences_path="./.cache/experiences_overall_derivation.md",
|
|
|
+ debug=True,
|
|
|
+ )
|
|
|
+
|
|
|
+ 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"4. 恢复已有 Trace: {resume_trace_id[:8]}...")
|
|
|
+ print(f" - 状态: {existing_trace.status}")
|
|
|
+ print(f" - 消息数: {existing_trace.total_messages}")
|
|
|
+ print(f" - 任务: {existing_trace.task}")
|
|
|
+ else:
|
|
|
+ print("4. 启动新 Agent 模式...")
|
|
|
+
|
|
|
+ print()
|
|
|
+
|
|
|
+ final_response = ""
|
|
|
+ current_trace_id = resume_trace_id
|
|
|
+ current_sequence = 0
|
|
|
+ should_exit = False
|
|
|
+
|
|
|
try:
|
|
|
- print_step(1, "开始执行任务")
|
|
|
-
|
|
|
- # 读取prompt文件
|
|
|
- print_step(2, "读取prompt文件")
|
|
|
- prompt_file = Path(__file__).parent / "prompt_overall_derivation.md"
|
|
|
- print(f" 文件路径: {prompt_file}", file=sys.stderr)
|
|
|
-
|
|
|
- if not prompt_file.exists():
|
|
|
- print(f"错误: prompt文件不存在: {prompt_file}", file=sys.stderr)
|
|
|
- return
|
|
|
-
|
|
|
- with open(prompt_file, 'r', encoding='utf-8') as f:
|
|
|
- prompt_content = f.read()
|
|
|
-
|
|
|
- content_length = len(prompt_content)
|
|
|
- content_lines = len(prompt_content.splitlines())
|
|
|
- print(f" 文件大小: {content_length} 字符, {content_lines} 行", file=sys.stderr)
|
|
|
-
|
|
|
- print_step(3, "准备发送给模型")
|
|
|
- print(f" 模型: {DEFAULT_MODEL}", file=sys.stderr)
|
|
|
- print(f" Prompt内容预览: {prompt_content[:100]}...", file=sys.stderr)
|
|
|
-
|
|
|
- print_step(4, "等待模型响应")
|
|
|
- print(" 正在接收模型输出...", file=sys.stderr)
|
|
|
-
|
|
|
- last_result = None
|
|
|
- message_count = 0
|
|
|
- tool_call_count = 0
|
|
|
-
|
|
|
- async for item in runner.run(
|
|
|
- messages=[{"role": "user", "content": prompt_content}],
|
|
|
- config=RunConfig(model=DEFAULT_MODEL),
|
|
|
- ):
|
|
|
- # 显示处理进度
|
|
|
- if hasattr(item, '__class__'):
|
|
|
- class_name = item.__class__.__name__
|
|
|
-
|
|
|
- if class_name == 'Message':
|
|
|
- message_count += 1
|
|
|
- role = getattr(item, 'role', 'unknown')
|
|
|
- print(f" [收到消息 #{message_count}] 角色: {role}", file=sys.stderr)
|
|
|
-
|
|
|
- if role == 'assistant':
|
|
|
- content = item.content
|
|
|
- text = ""
|
|
|
-
|
|
|
- if isinstance(content, dict):
|
|
|
- text = content.get('text', '')
|
|
|
- tool_calls = content.get('tool_calls', [])
|
|
|
- if tool_calls:
|
|
|
- tool_call_count += len(tool_calls)
|
|
|
- print(f" [工具调用] 数量: {len(tool_calls)}", file=sys.stderr)
|
|
|
- elif isinstance(content, str):
|
|
|
- text = content
|
|
|
-
|
|
|
- if text:
|
|
|
- text_preview = text[:200] + "..." if len(text) > 200 else text
|
|
|
- print(f" [文本内容] 长度: {len(text)} 字符", file=sys.stderr)
|
|
|
- print(f" [内容预览] {text_preview}", file=sys.stderr)
|
|
|
- last_result = text
|
|
|
-
|
|
|
- elif class_name == 'ToolCall':
|
|
|
- tool_call_count += 1
|
|
|
- tool_name = getattr(item, 'name', 'unknown')
|
|
|
- print(f" [工具调用 #{tool_call_count}] {tool_name}", file=sys.stderr)
|
|
|
-
|
|
|
- elif class_name == 'ToolResult':
|
|
|
- tool_name = getattr(item, 'tool_name', 'unknown')
|
|
|
- print(f" [工具结果] {tool_name}", file=sys.stderr)
|
|
|
-
|
|
|
- print_step(5, "处理完成")
|
|
|
- print(f" 总计收到消息: {message_count} 条", file=sys.stderr)
|
|
|
- print(f" 总计工具调用: {tool_call_count} 次", file=sys.stderr)
|
|
|
-
|
|
|
- print_step(6, "输出最终结果")
|
|
|
- if last_result:
|
|
|
- print("\n" + "="*80, file=sys.stderr)
|
|
|
- print("最终结果:", file=sys.stderr)
|
|
|
- print("="*80, file=sys.stderr)
|
|
|
- print(last_result)
|
|
|
+ if resume_trace_id:
|
|
|
+ initial_messages = None
|
|
|
+ config = RunConfig(
|
|
|
+ model=model_id,
|
|
|
+ temperature=float(prompt.config.get("temperature", 0.3)),
|
|
|
+ max_iterations=1000,
|
|
|
+ trace_id=resume_trace_id,
|
|
|
+ )
|
|
|
else:
|
|
|
- print("未获取到最终结果", file=sys.stderr)
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- print(f"\n[错误] {e}", file=sys.stderr)
|
|
|
- import traceback
|
|
|
- traceback.print_exc()
|
|
|
- raise
|
|
|
-
|
|
|
-if __name__ == '__main__':
|
|
|
- asyncio.run(main())
|
|
|
+ initial_messages = messages
|
|
|
+ config = RunConfig(
|
|
|
+ model=model_id,
|
|
|
+ temperature=float(prompt.config.get("temperature", 0.3)),
|
|
|
+ max_iterations=1000,
|
|
|
+ name="选题点整体推导任务",
|
|
|
+ )
|
|
|
+
|
|
|
+ while not should_exit:
|
|
|
+ if current_trace_id:
|
|
|
+ config.trace_id = current_trace_id
|
|
|
+
|
|
|
+ final_response = ""
|
|
|
+
|
|
|
+ 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 show_interactive_menu(
|
|
|
+ runner, current_trace_id, current_sequence, store
|
|
|
+ )
|
|
|
+ if menu_result["action"] == "stop":
|
|
|
+ break
|
|
|
+ elif menu_result["action"] == "continue":
|
|
|
+ new_messages = menu_result.get("messages", [])
|
|
|
+ if new_messages:
|
|
|
+ initial_messages = new_messages
|
|
|
+ config.after_sequence = menu_result.get("after_sequence")
|
|
|
+ else:
|
|
|
+ initial_messages = []
|
|
|
+ config.after_sequence = None
|
|
|
+ continue
|
|
|
+ break
|
|
|
+ initial_messages = []
|
|
|
+
|
|
|
+ print(f"{'▶️ 开始执行...' if not current_trace_id else '▶️ 继续执行...'}")
|
|
|
+
|
|
|
+ paused = False
|
|
|
+ try:
|
|
|
+ async for item in runner.run(messages=initial_messages, config=config):
|
|
|
+ cmd = 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 show_interactive_menu(
|
|
|
+ runner, current_trace_id, current_sequence, store
|
|
|
+ )
|
|
|
+ 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:
|
|
|
+ config.after_sequence = after_seq
|
|
|
+ paused = True
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ initial_messages = []
|
|
|
+ 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
|
|
|
+
|
|
|
+ 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 tokens: {item.total_tokens}")
|
|
|
+ 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] ⏸️ 已停止")
|
|
|
+
|
|
|
+ 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}")
|
|
|
+ if tool_calls:
|
|
|
+ for tc in tool_calls:
|
|
|
+ tool_name = tc.get("function", {}).get("name", "unknown")
|
|
|
+ print(f"[Tool Call] 🛠️ {tool_name}")
|
|
|
+ elif item.role == "tool":
|
|
|
+ content = item.content
|
|
|
+ if isinstance(content, dict):
|
|
|
+ tool_name = content.get("tool_name", "unknown")
|
|
|
+ print(f"[Tool Result] ✅ {tool_name}")
|
|
|
+ if item.description:
|
|
|
+ desc = item.description[:80] if len(item.description) > 80 else item.description
|
|
|
+ print(f" {desc}...")
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ print(f"\n执行出错: {e}")
|
|
|
+ import traceback
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
+ if paused:
|
|
|
+ if should_exit:
|
|
|
+ break
|
|
|
+ continue
|
|
|
+
|
|
|
+ if should_exit:
|
|
|
+ break
|
|
|
+
|
|
|
+ if current_trace_id:
|
|
|
+ menu_result = await show_interactive_menu(
|
|
|
+ runner, current_trace_id, current_sequence, store
|
|
|
+ )
|
|
|
+ if menu_result["action"] == "stop":
|
|
|
+ break
|
|
|
+ elif menu_result["action"] == "continue":
|
|
|
+ new_messages = menu_result.get("messages", [])
|
|
|
+ if new_messages:
|
|
|
+ initial_messages = new_messages
|
|
|
+ config.after_sequence = menu_result.get("after_sequence")
|
|
|
+ else:
|
|
|
+ initial_messages = []
|
|
|
+ config.after_sequence = None
|
|
|
+ continue
|
|
|
+ break
|
|
|
+
|
|
|
+ except KeyboardInterrupt:
|
|
|
+ print("\n\n用户中断 (Ctrl+C)")
|
|
|
+ if current_trace_id:
|
|
|
+ await runner.stop(current_trace_id)
|
|
|
+
|
|
|
+ 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())
|