forked from XuJiachengZust/codeAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_agent_memories.py
More file actions
125 lines (100 loc) · 4.26 KB
/
Copy pathview_agent_memories.py
File metadata and controls
125 lines (100 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""
查看智能体的中间产物
包括保存在 /memories/ 路径下的文件和 LangGraph store 中的状态
"""
import json
import sys
from typing import Dict, Any, Optional
from services.ai.agents.deep_agent_service import deep_agent_service
from langgraph.store.memory import InMemoryStore
from utils.logger import analysis_logger
def view_memories_from_store(session_id: Optional[str] = None):
"""从 LangGraph store 中查看中间产物"""
print("=== 查看智能体中间产物 ===\n")
try:
service = deep_agent_service.get_service()
if not service:
print("❌ Deep Agent 服务未初始化")
return
if not service.store:
print("❌ 未配置存储,无法查看中间产物")
return
print(f"存储类型: {type(service.store).__name__}")
# 如果是 InMemoryStore,尝试查看内容
if isinstance(service.store, InMemoryStore):
print("\n⚠️ 当前使用 InMemoryStore(内存存储)")
print(" 中间产物只保存在内存中,服务重启后会丢失")
print(" 无法直接查看内存中的内容")
print("\n建议:")
print(" 1. 查看日志文件获取智能体的操作记录")
print(" 2. 使用流式 API 查看实时操作")
print(" 3. 检查是否有文件系统工具创建的文件")
return
# 如果有其他类型的 store,尝试列出内容
print("\n尝试列出存储内容...")
# 这里需要根据实际的 store 类型来实现
except Exception as e:
print(f"❌ 查看中间产物失败: {e}")
import traceback
traceback.print_exc()
def view_logs():
"""查看日志文件中的智能体操作记录"""
print("\n=== 查看日志文件 ===\n")
log_files = [
"logs/deepagent.log",
"logs/analysis.log"
]
for log_file in log_files:
try:
import os
if os.path.exists(log_file):
print(f"\n📄 {log_file}:")
print("-" * 60)
# 读取最后 50 行
with open(log_file, 'r', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
for line in lines[-50:]:
if 'TOOL' in line or 'write_file' in line or 'memories' in line.lower():
print(line.strip())
else:
print(f"⚠️ {log_file} 不存在")
except Exception as e:
print(f"❌ 读取 {log_file} 失败: {e}")
def view_recent_sessions():
"""查看最近的会话信息"""
print("\n=== 查看最近的会话 ===\n")
try:
# 这里可以扩展为从数据库或文件系统读取会话信息
print("提示:会话信息保存在 LangGraph store 中")
print("当前使用 InMemoryStore,无法持久化查看")
print("\n建议使用流式 API 查看实时操作:")
print(" POST /api/deep-agent/chat/stream")
print(" 设置 stream_mode=['tasks', 'checkpoints', 'debug']")
except Exception as e:
print(f"❌ 查看会话失败: {e}")
def main():
"""主函数"""
import argparse
parser = argparse.ArgumentParser(description="查看智能体的中间产物")
parser.add_argument("--session-id", type=str, help="会话ID")
parser.add_argument("--logs", action="store_true", help="查看日志文件")
parser.add_argument("--sessions", action="store_true", help="查看会话信息")
args = parser.parse_args()
if args.logs:
view_logs()
elif args.sessions:
view_recent_sessions()
else:
# 默认查看所有
view_memories_from_store(args.session_id)
view_logs()
view_recent_sessions()
print("\n" + "=" * 60)
print("💡 提示:")
print(" 1. 使用 --logs 只查看日志")
print(" 2. 使用 --sessions 查看会话信息")
print(" 3. 使用流式 API 查看实时操作")
print(" 4. 中间产物保存在 /memories/ 路径下,通过文件系统工具访问")
if __name__ == "__main__":
main()