-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_preload.py
More file actions
307 lines (255 loc) · 11.1 KB
/
Copy pathmemory_preload.py
File metadata and controls
307 lines (255 loc) · 11.1 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
"""
記憶預載入鉤子 (Memory Pre-Query Hook)
========================================
在 session 啟動時自動查詢最近 48 小時的關鍵事件,
產出 structured JSON 供 Agent 參考。
來源:
Layer 1: Qdrant 向量記憶 (openclaw_mem) → 語義搜索
Layer 2: MemoryHub API (port 3872) → 結構化事件(如運行中)
Layer 3: 檔案系統 daily log → grep 關鍵詞
輸出:memory/preload/recent-events.json
用法:
python memory_preload.py # 預設 48h
python memory_preload.py --hours 72 # 自定義時間範圍
python memory_preload.py --query "模型 配置" # 指定查詢主題
"""
import os, sys, json, time, subprocess
from datetime import datetime, timezone, timedelta
from pathlib import Path
HKT = timezone(timedelta(hours=8))
NOW = datetime.now(HKT)
# ── Config ──────────────────────────────────────────
SHARED_WORKSPACE = Path("/Users/Claw/.openclaw/workspace")
DAILY_DIR = SHARED_WORKSPACE / "memory" / "daily"
PRELOAD_DIR = SHARED_WORKSPACE / "coder-deepseek" / "memory" / "preload"
PRELOAD_FILE = Path("/Users/Claw/.openclaw/workspace/coder-deepseek/memory/preload/recent-events.json")
QDRANT_URL = "http://localhost:6333"
MEMORYHUB_URL = "http://localhost:3872/api"
# 重要性關鍵詞(用於過濾和排序)
HIGH_PRIORITY_KEYWORDS = [
"配置變更", "provider", "模型", "model", "gateway",
"決定", "以後都要", "永久規則", "R1", "R2", "R3",
"老闆指示", "老闆要求", "鐵律", "禁止",
"部署", "上線", "安裝",
"致命", "修復", "bug", "故障",
"完成", "✅",
]
# 查詢模板(自動執行,覆蓋不同主題)
DEFAULT_QUERIES = [
"最近發生的重要事件 決策 配置變更",
"老闆指示 新規則 永久規則",
"新增的模型 provider 服務",
"部署 安裝 上線",
"錯誤 故障 修復 bug",
]
def check_qdrant():
"""檢查 Qdrant 是否在線"""
import urllib.request
try:
req = urllib.request.Request(f"{QDRANT_URL}/", method="GET")
with urllib.request.urlopen(req, timeout=3) as resp:
return resp.status == 200
except Exception:
return False
def check_memoryhub():
"""檢查 MemoryHub 是否在線"""
import urllib.request
try:
req = urllib.request.Request(f"{MEMORYHUB_URL}/state", method="GET")
with urllib.request.urlopen(req, timeout=3) as resp:
if resp.status == 200:
data = json.loads(resp.read())
return "started_at" in data
return False
except Exception:
return False
def search_qdrant(query: str, limit: int = 10) -> list:
"""直接查詢 Qdrant(使用 curl,避免 Python 依賴問題)"""
import urllib.request, urllib.parse
# 先獲取 collection info
try:
req = urllib.request.Request(
f"{QDRANT_URL}/collections/openclaw_mem",
method="GET"
)
with urllib.request.urlopen(req, timeout=5) as resp:
info = json.loads(resp.read())
except Exception as e:
return [{"error": f"Qdrant collection query failed: {e}"}]
vector_size = info.get("result", {}).get("config", {}).get("params", {}).get("vectors", {}).get("size", 1024)
# 使用簡單的 search API(需要 embedding,這裡使用 keyword search fallback)
# 先用 scroll 獲取最近點,再手動過濾
try:
body = json.dumps({
"filter": {
"must": []
},
"limit": min(limit * 3, 50),
"with_payload": True,
"with_vector": False
}).encode()
req = urllib.request.Request(
f"{QDRANT_URL}/collections/openclaw_mem/points/scroll",
data=body,
method="POST",
headers={"Content-Type": "application/json"}
)
with urllib.request.urlopen(req, timeout=10) as resp:
result = json.loads(resp.read())
points = result.get("result", {}).get("points", [])
results = []
for p in points:
payload = p.get("payload", {})
content = payload.get("content", "") or payload.get("text", "")
# 關鍵詞匹配打分
score = sum(1 for kw in query.lower().split() if kw in content.lower())
if score > 0:
results.append({
"score": score,
"content": content[:500],
"source": payload.get("source", "unknown"),
"tags": payload.get("tags", []),
"id": p.get("id", "")
})
results.sort(key=lambda x: x["score"], reverse=True)
return results[:limit]
except Exception as e:
return [{"error": f"Qdrant search failed: {e}"}]
def search_memoryhub(query: str, limit: int = 10) -> list:
"""查詢 MemoryHub API"""
import urllib.request, urllib.parse
try:
params = urllib.parse.urlencode({"q": query, "limit": limit})
url = f"{MEMORYHUB_URL}/search?{params}"
req = urllib.request.Request(url, method="GET")
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read())
results = data.get("results", [])
# MemoryHub returns {content, platform, channel, importance, ...}
return [{
"content": r.get("content", "")[:300],
"platform": r.get("platform", "?"),
"importance": r.get("importance", 5),
"memory_type": r.get("memory_type", "?"),
} for r in results[:limit]]
except Exception as e:
return [{"error": f"MemoryHub query failed: {e}"}]
def grep_daily_logs(keywords: list, hours: int = 48) -> list:
"""從 daily log 文件中 grep 關鍵詞"""
results = []
cutoff = NOW - timedelta(hours=hours)
# 檢查最近幾天的 daily log
for day_offset in range(4): # 最多回溯 4 天
check_date = NOW - timedelta(days=day_offset)
date_str = check_date.strftime("%Y-%m-%d")
daily_file = DAILY_DIR / f"{date_str}.md"
if not daily_file.exists():
continue
try:
content = daily_file.read_text(encoding="utf-8")
lines = content.split("\n")
# 對每個關鍵詞搜索
for kw in keywords[:5]: # 限制關鍵詞數量
for i, line in enumerate(lines):
if kw.lower() in line.lower() and len(line.strip()) > 10:
# 獲取上下文(前後 2 行)
ctx_start = max(0, i - 2)
ctx_end = min(len(lines), i + 3)
context = "\n".join(lines[ctx_start:ctx_end])
results.append({
"file": str(daily_file.name),
"keyword": kw,
"line": line.strip()[:300],
"context": context[:500],
"date": date_str,
})
except Exception as e:
results.append({"error": f"Failed to read {date_str}: {e}"})
# 去重
seen = set()
unique = []
for r in results:
key = r.get("line", "")[:100]
if key not in seen:
seen.add(key)
unique.append(r)
max_results = 20
return unique[:max_results]
def generate_preload(hours: int = 48, custom_query: str = None) -> dict:
"""主函數:生成預載入數據"""
qdrant_ok = check_qdrant()
mh_ok = check_memoryhub()
queries = [custom_query] if custom_query else DEFAULT_QUERIES
queries = [q for q in queries if q] # 過濾 None
result = {
"generated_at": NOW.isoformat(),
"generated_at_hkt": NOW.strftime("%Y-%m-%d %H:%M:%S HKT"),
"time_window_hours": hours,
"sources_available": {
"qdrant": qdrant_ok,
"memoryhub": mh_ok,
"daily_logs": DAILY_DIR.exists() if DAILY_DIR else False,
},
"queries": [],
}
for query in queries:
query_result = {
"query": query,
"qdrant": [],
"memoryhub": [],
"daily_logs": [],
}
# Layer 1: Qdrant
if qdrant_ok:
try:
query_result["qdrant"] = search_qdrant(query, limit=8)
except Exception as e:
query_result["qdrant"] = [{"error": str(e)}]
# Layer 2: MemoryHub
if mh_ok:
try:
query_result["memoryhub"] = search_memoryhub(query, limit=8)
except Exception as e:
query_result["memoryhub"] = [{"error": str(e)}]
# Layer 3: Daily logs
try:
kw_list = query.split()[:5]
query_result["daily_logs"] = grep_daily_logs(kw_list, hours)
except Exception as e:
query_result["daily_logs"] = [{"error": str(e)}]
result["queries"].append(query_result)
# 生成摘要
total_hits = sum(
len(q.get("qdrant", [])) + len(q.get("memoryhub", [])) + len(q.get("daily_logs", []))
for q in result["queries"]
)
result["summary"] = {
"total_hits": total_hits,
"qdrant_hits": sum(len(q.get("qdrant", [])) for q in result["queries"]),
"memoryhub_hits": sum(len(q.get("memoryhub", [])) for q in result["queries"]),
"daily_log_hits": sum(len(q.get("daily_logs", [])) for q in result["queries"]),
}
return result
def main():
import argparse
parser = argparse.ArgumentParser(description="Memory Pre-Query Hook")
parser.add_argument("--hours", type=int, default=48, help="Time window in hours")
parser.add_argument("--query", type=str, default=None, help="Custom query topic")
parser.add_argument("--output", type=str, default=None, help="Output file path")
parser.add_argument("--quiet", action="store_true", help="Suppress stdout output")
args = parser.parse_args()
PRELOAD_DIR.mkdir(parents=True, exist_ok=True)
data = generate_preload(hours=args.hours, custom_query=args.query)
output_path = args.output or str(PRELOAD_FILE)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
if not args.quiet:
summary = data["summary"]
sources = data["sources_available"]
print(f"🧠 Memory Preload complete ({args.hours}h window)")
print(f" Qdrant: {'✅' if sources['qdrant'] else '❌'} | MemoryHub: {'✅' if sources['memoryhub'] else '❌'} | Daily Logs: {'✅' if sources['daily_logs'] else '❌'}")
print(f" Hits: {summary['total_hits']} total (Q:{summary['qdrant_hits']} M:{summary['memoryhub_hits']} D:{summary['daily_log_hits']})")
print(f" Output: {output_path}")
if __name__ == "__main__":
main()