test_new_method.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. """
  3. 直接测试新的get_conversation_history方法
  4. """
  5. import asyncio
  6. import sys
  7. import json
  8. from pathlib import Path
  9. # 添加项目路径
  10. project_root = Path(__file__).parent
  11. sys.path.insert(0, str(project_root))
  12. from react_agent.agent import CustomReactAgent
  13. async def test_new_method():
  14. """测试新的get_conversation_history方法"""
  15. print("=" * 60)
  16. print("直接测试新的get_conversation_history方法")
  17. print("=" * 60)
  18. try:
  19. # 初始化Agent
  20. print("🚀 初始化Agent...")
  21. agent = await CustomReactAgent.create()
  22. thread_id = "wang10:20250717211620915"
  23. print(f"\n📖 测试Thread: {thread_id}")
  24. # 测试不包含工具消息
  25. print(f"\n🔍 测试不包含工具消息...")
  26. result = await agent.get_conversation_history(thread_id, include_tools=False)
  27. print(f"📊 结果类型: {type(result)}")
  28. print(f"📝 结果键: {list(result.keys()) if isinstance(result, dict) else 'Not a dict'}")
  29. if isinstance(result, dict):
  30. messages = result.get("messages", [])
  31. print(f"💬 消息数量: {len(messages)}")
  32. print(f"🕐 线程创建时间: {result.get('thread_created_at')}")
  33. print(f"📋 总checkpoint数: {result.get('total_checkpoints')}")
  34. if messages:
  35. print(f"\n📋 前3条消息:")
  36. for i, msg in enumerate(messages[:3]):
  37. print(f" 消息 {i+1}:")
  38. print(f" id: {msg.get('id')}")
  39. print(f" type: {msg.get('type')}")
  40. print(f" timestamp: {msg.get('timestamp')}")
  41. print(f" content: {str(msg.get('content', ''))[:50]}...")
  42. # 保存结果
  43. with open('direct_method_test_no_tools.json', 'w', encoding='utf-8') as f:
  44. json.dump(result, f, ensure_ascii=False, indent=2)
  45. print(f"\n💾 结果已保存到 direct_method_test_no_tools.json")
  46. # 测试包含工具消息
  47. print(f"\n🔍 测试包含工具消息...")
  48. result_with_tools = await agent.get_conversation_history(thread_id, include_tools=True)
  49. if isinstance(result_with_tools, dict):
  50. messages_with_tools = result_with_tools.get("messages", [])
  51. print(f"💬 包含工具的消息数量: {len(messages_with_tools)}")
  52. # 统计消息类型
  53. type_counts = {}
  54. for msg in messages_with_tools:
  55. msg_type = msg.get('type', 'unknown')
  56. type_counts[msg_type] = type_counts.get(msg_type, 0) + 1
  57. print(f"📊 消息类型统计:")
  58. for msg_type, count in type_counts.items():
  59. print(f" {msg_type}: {count}")
  60. # 保存结果
  61. with open('direct_method_test_with_tools.json', 'w', encoding='utf-8') as f:
  62. json.dump(result_with_tools, f, ensure_ascii=False, indent=2)
  63. print(f"\n💾 结果已保存到 direct_method_test_with_tools.json")
  64. await agent.close()
  65. except Exception as e:
  66. print(f"❌ 测试失败: {e}")
  67. import traceback
  68. traceback.print_exc()
  69. if __name__ == "__main__":
  70. asyncio.run(test_new_method())