-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_store.py
More file actions
575 lines (523 loc) · 18.9 KB
/
memory_store.py
File metadata and controls
575 lines (523 loc) · 18.9 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#!/usr/bin/env python3
import fcntl
import re
import sqlite3
import subprocess
import tempfile
import time
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional
from zoneinfo import ZoneInfo
TELEGRAM_MEMORY_SECTION = "## Telegram 协作记录"
class ConversationMemoryStore:
def __init__(self, db_path: str, keep_messages: int = 24) -> None:
self.db_path = Path(db_path)
self.keep_messages = keep_messages
self.db_path.parent.mkdir(parents=True, exist_ok=True)
self._init_db()
def _connect(self) -> sqlite3.Connection:
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
return conn
def _init_db(self) -> None:
with self._connect() as conn:
conn.executescript(
"""
CREATE TABLE IF NOT EXISTS conversation_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
bot_role TEXT NOT NULL,
chat_id TEXT NOT NULL,
user_id TEXT NOT NULL DEFAULT '',
role TEXT NOT NULL,
summary TEXT NOT NULL DEFAULT '',
content TEXT NOT NULL,
created_at REAL NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_conversation_lookup
ON conversation_messages(bot_role, chat_id, id);
CREATE TABLE IF NOT EXISTS conversation_profiles (
bot_role TEXT NOT NULL,
chat_id TEXT NOT NULL,
profile TEXT NOT NULL,
updated_at REAL NOT NULL,
PRIMARY KEY (bot_role, chat_id)
);
"""
)
self._ensure_column(
conn,
"conversation_messages",
"summary",
"TEXT NOT NULL DEFAULT ''",
)
conn.execute(
"""
UPDATE conversation_messages
SET summary = ?
WHERE summary = ''
""",
("",),
)
def append_message(
self,
bot_role: str,
chat_id: str,
user_id: str,
role: str,
content: str,
) -> None:
clean = content.strip()
if not clean:
return
summary = build_memory_summary(clean)
with self._connect() as conn:
conn.execute(
"""
INSERT INTO conversation_messages (
bot_role, chat_id, user_id, role, summary, content, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(bot_role, str(chat_id), str(user_id), role, summary, clean, time.time()),
)
if self.keep_messages > 0:
conn.execute(
"""
DELETE FROM conversation_messages
WHERE id IN (
SELECT id FROM conversation_messages
WHERE bot_role = ? AND chat_id = ?
ORDER BY id DESC
LIMIT -1 OFFSET ?
)
""",
(bot_role, str(chat_id), self.keep_messages),
)
def get_history(self, bot_role: str, chat_id: str, limit: int) -> List[Dict[str, str]]:
with self._connect() as conn:
rows = conn.execute(
"""
SELECT role, content
FROM conversation_messages
WHERE bot_role = ? AND chat_id = ?
ORDER BY id DESC
LIMIT ?
""",
(bot_role, str(chat_id), limit),
).fetchall()
return [
{"role": str(row["role"]), "content": str(row["content"])}
for row in reversed(rows)
]
def get_history_summaries(
self,
bot_role: str,
chat_id: str,
limit: int = 20,
) -> List[Dict[str, str]]:
with self._connect() as conn:
rows = conn.execute(
"""
SELECT id, role, summary, created_at
FROM conversation_messages
WHERE bot_role = ? AND chat_id = ?
ORDER BY id DESC
LIMIT ?
""",
(bot_role, str(chat_id), limit),
).fetchall()
return [
{
"id": str(row["id"]),
"role": str(row["role"]),
"summary": str(row["summary"]),
"created_at": str(row["created_at"]),
}
for row in reversed(rows)
]
def get_chat_summaries(
self,
chat_id: str,
limit: int = 20,
*,
bot_roles: Optional[List[str]] = None,
exclude_bot_role: str = "",
role: str = "",
) -> List[Dict[str, str]]:
clauses = ["chat_id = ?"]
params: List[object] = [str(chat_id)]
if bot_roles:
placeholders = ",".join("?" for _ in bot_roles)
clauses.append(f"bot_role IN ({placeholders})")
params.extend(bot_roles)
if exclude_bot_role:
clauses.append("bot_role != ?")
params.append(exclude_bot_role)
if role:
clauses.append("role = ?")
params.append(role)
params.append(limit)
where_sql = " AND ".join(clauses)
with self._connect() as conn:
rows = conn.execute(
f"""
SELECT id, bot_role, role, summary, content, created_at
FROM conversation_messages
WHERE {where_sql}
ORDER BY id DESC
LIMIT ?
""",
params,
).fetchall()
return [
{
"id": str(row["id"]),
"bot_role": str(row["bot_role"]),
"role": str(row["role"]),
"summary": str(row["summary"]),
"content": str(row["content"]),
"created_at": str(row["created_at"]),
}
for row in reversed(rows)
]
def search_history_summaries(
self,
bot_role: str,
chat_id: str,
query: str,
limit: int = 20,
) -> List[Dict[str, str]]:
clean = " ".join(query.split()).strip()
if not clean:
return []
like = f"%{clean}%"
with self._connect() as conn:
rows = conn.execute(
"""
SELECT id, role, summary, content, created_at
FROM conversation_messages
WHERE bot_role = ? AND chat_id = ?
AND (summary LIKE ? OR content LIKE ?)
ORDER BY id DESC
LIMIT ?
""",
(bot_role, str(chat_id), like, like, limit),
).fetchall()
return [
{
"id": str(row["id"]),
"role": str(row["role"]),
"summary": str(row["summary"]),
"content": str(row["content"]),
"created_at": str(row["created_at"]),
}
for row in rows
]
def search_chat_summaries(
self,
chat_id: str,
query: str,
limit: int = 20,
*,
bot_roles: Optional[List[str]] = None,
exclude_bot_role: str = "",
role: str = "",
) -> List[Dict[str, str]]:
clean = " ".join(query.split()).strip()
if not clean:
return []
clauses = ["chat_id = ?", "(summary LIKE ? OR content LIKE ?)"]
params: List[object] = [str(chat_id), f"%{clean}%", f"%{clean}%"]
if bot_roles:
placeholders = ",".join("?" for _ in bot_roles)
clauses.append(f"bot_role IN ({placeholders})")
params.extend(bot_roles)
if exclude_bot_role:
clauses.append("bot_role != ?")
params.append(exclude_bot_role)
if role:
clauses.append("role = ?")
params.append(role)
params.append(limit)
where_sql = " AND ".join(clauses)
with self._connect() as conn:
rows = conn.execute(
f"""
SELECT id, bot_role, role, summary, content, created_at
FROM conversation_messages
WHERE {where_sql}
ORDER BY id DESC
LIMIT ?
""",
params,
).fetchall()
return [
{
"id": str(row["id"]),
"bot_role": str(row["bot_role"]),
"role": str(row["role"]),
"summary": str(row["summary"]),
"content": str(row["content"]),
"created_at": str(row["created_at"]),
}
for row in rows
]
def clear_history(self, bot_role: str, chat_id: str) -> None:
with self._connect() as conn:
conn.execute(
"DELETE FROM conversation_messages WHERE bot_role = ? AND chat_id = ?",
(bot_role, str(chat_id)),
)
def set_chat_profile(self, bot_role: str, chat_id: str, profile: str) -> None:
clean = profile.strip()
if not clean:
return
with self._connect() as conn:
conn.execute(
"""
INSERT INTO conversation_profiles (bot_role, chat_id, profile, updated_at)
VALUES (?, ?, ?, ?)
ON CONFLICT(bot_role, chat_id)
DO UPDATE SET profile = excluded.profile, updated_at = excluded.updated_at
""",
(bot_role, str(chat_id), clean, time.time()),
)
def get_chat_profile(self, bot_role: str, chat_id: str) -> str:
with self._connect() as conn:
row = conn.execute(
"""
SELECT profile
FROM conversation_profiles
WHERE bot_role = ? AND chat_id = ?
""",
(bot_role, str(chat_id)),
).fetchone()
if not row:
return ""
return str(row["profile"])
def clear_chat_profile(self, bot_role: str, chat_id: str) -> None:
with self._connect() as conn:
conn.execute(
"DELETE FROM conversation_profiles WHERE bot_role = ? AND chat_id = ?",
(bot_role, str(chat_id)),
)
def _ensure_column(
self,
conn: sqlite3.Connection,
table: str,
column: str,
definition: str,
) -> None:
rows = conn.execute(f"PRAGMA table_info({table})").fetchall()
names = {str(row["name"]) for row in rows}
if column in names:
return
conn.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}")
if table == "conversation_messages" and column == "summary":
rows = conn.execute("SELECT id, content FROM conversation_messages").fetchall()
conn.executemany(
"UPDATE conversation_messages SET summary = ? WHERE id = ?",
[
(build_memory_summary(str(row["content"])), int(row["id"]))
for row in rows
],
)
class SharedMemoryJournal:
def __init__(self, memory_dir: str, timezone_name: str = "Asia/Shanghai") -> None:
self.memory_dir = Path(memory_dir)
self.timezone_name = timezone_name
def append_event(
self,
bot_role: str,
scope: str,
task_summary: str,
result_summary: Optional[str] = None,
status: str = "completed",
task_id: Optional[int] = None,
category: str = "",
allowed_agents: Optional[List[str]] = None,
) -> None:
now = datetime.now(ZoneInfo(self.timezone_name))
daily_path = self.memory_dir / f"{now:%Y-%m-%d}.md"
daily_path.parent.mkdir(parents=True, exist_ok=True)
task_part = _clip(task_summary, 120)
result_part = _clip(result_summary or "", 180)
extras: List[str] = []
if task_id is not None:
extras.append(f"task=#{task_id}")
if category:
extras.append(f"category={category}")
if allowed_agents:
extras.append(f"eligible={','.join(allowed_agents)}")
extras_text = f" ({'; '.join(extras)})" if extras else ""
if status == "queued":
line = f"- {now:%H:%M} [{bot_role}/{scope}] 已创建任务{extras_text} | 任务:{task_part}"
elif status == "failed":
line = f"- {now:%H:%M} [{bot_role}/{scope}] 任务失败{extras_text} | 任务:{task_part} | 错误:{result_part}"
else:
line = f"- {now:%H:%M} [{bot_role}/{scope}] 已完成{extras_text} | 任务:{task_part}"
if result_part:
line += f" | 结果:{result_part}"
self._append_line(daily_path, now, line)
def _append_line(self, daily_path: Path, now: datetime, line: str) -> None:
if not daily_path.exists():
daily_path.write_text(
f"# 今日记忆 - {now.year}年{now.month:02d}月{now.day:02d}日\n\n"
f"{TELEGRAM_MEMORY_SECTION}\n"
f"{line}\n",
encoding="utf-8",
)
return
with daily_path.open("r+", encoding="utf-8") as handle:
fcntl.flock(handle.fileno(), fcntl.LOCK_EX)
text = handle.read()
if not text.endswith("\n"):
text += "\n"
if TELEGRAM_MEMORY_SECTION not in text:
if text.strip():
text += "\n"
text += f"{TELEGRAM_MEMORY_SECTION}\n"
text += f"{line}\n"
handle.seek(0)
handle.write(text)
handle.truncate()
fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
def _clip(text: str, limit: int) -> str:
clean = text.replace("\n", " ").strip()
if len(clean) <= limit:
return clean
return f"{clean[:limit]}..."
def build_memory_summary(text: str, limit: int = 96) -> str:
clean = " ".join(text.split()).strip()
if not clean:
return ""
summary_match = re.search(r"(?:^|\s)summary=(.+)$", clean, flags=re.IGNORECASE)
if summary_match:
candidate = summary_match.group(1).strip()
return _clip(candidate, limit)
lines = [line.strip() for line in text.splitlines() if line.strip()]
if not lines:
return _clip(clean, limit)
first = lines[0]
if first.startswith("[任务 #") and len(lines) > 1:
return _clip(f"{first} | {lines[1]}", limit)
if first.startswith("[") and len(lines) > 1 and any(
marker in first for marker in ("群聊结果", "OpenClaw", "Codex", "Gemini", "Claude")
):
return _clip(f"{first} | {lines[1]}", limit)
if first.startswith("[") and "]" in first:
first = first.split("]", 1)[1].strip() or lines[0]
return _clip(first, limit)
def build_instant_memory_snapshot(
store: ConversationMemoryStore,
*,
bot_role: str,
chat_id: str,
own_limit: int = 6,
shared_limit: int = 6,
) -> str:
own_entries = store.get_history_summaries(bot_role, chat_id, own_limit)
shared_entries = store.get_chat_summaries(
chat_id,
limit=max(shared_limit * 3, shared_limit),
exclude_bot_role=bot_role,
role="assistant",
)
own_lines = [
f"- {entry['role']}: {entry['summary']}"
for entry in own_entries
if entry.get("summary")
]
shared_lines: List[str] = []
seen_shared: set[str] = set()
for entry in reversed(shared_entries):
summary = str(entry.get("summary", "")).strip()
if not summary or summary in seen_shared:
continue
seen_shared.add(summary)
shared_lines.append(f"- {entry['bot_role']}: {summary}")
if len(shared_lines) >= shared_limit:
break
shared_lines.reverse()
sections: List[str] = []
if own_lines:
sections.append("即时记忆(当前会话最近摘要):\n" + "\n".join(own_lines[-own_limit:]))
if shared_lines:
sections.append("共享即时记忆(同聊天其他 bot 最近结果):\n" + "\n".join(shared_lines[-shared_limit:]))
return "\n\n".join(sections).strip()
def render_recent_memory_digest(
store: ConversationMemoryStore,
*,
bot_role: str,
chat_id: str,
own_limit: int = 6,
shared_limit: int = 6,
) -> str:
snapshot = build_instant_memory_snapshot(
store,
bot_role=bot_role,
chat_id=chat_id,
own_limit=own_limit,
shared_limit=shared_limit,
)
return snapshot or "当前还没有可用的最近记忆摘要。"
def render_memory_search_digest(
store: ConversationMemoryStore,
*,
bot_role: str,
chat_id: str,
query: str,
limit: int = 8,
) -> str:
hits = store.search_chat_summaries(
chat_id,
query,
limit=limit,
)
if not hits:
return f"没有找到和“{query}”相关的记忆摘要。"
lines = [f"记忆搜索:{query}"]
for item in hits[:limit]:
lines.append(
f"- {item['bot_role']} / {item['role']}: {item['summary']}"
)
return "\n".join(lines)
class LongTermMemoryWriter:
def __init__(
self,
script_path: str,
enabled: bool = False,
timezone_name: str = "Asia/Shanghai",
) -> None:
self.script_path = Path(script_path)
self.enabled = enabled
self.timezone_name = timezone_name
def append_note(self, note: str) -> None:
clean = note.strip()
if not clean:
raise ValueError("长期记忆内容不能为空")
if not self.enabled:
raise PermissionError("当前 bot 未开启长期记忆写入权限")
if not self.script_path.exists():
raise FileNotFoundError(f"长期记忆脚本不存在: {self.script_path}")
now = datetime.now(ZoneInfo(self.timezone_name))
lines = [line.strip() for line in clean.splitlines() if line.strip()]
body = "\n".join(f"- {line}" for line in lines)
content = f"### Telegram 长期记忆补充 - {now:%Y-%m-%d %H:%M}\n{body}\n"
with tempfile.NamedTemporaryFile("w", encoding="utf-8", delete=False) as tmp:
tmp.write(content)
temp_path = Path(tmp.name)
try:
result = subprocess.run(
[str(self.script_path), "append", str(temp_path)],
capture_output=True,
text=True,
check=False,
)
finally:
temp_path.unlink(missing_ok=True)
if result.returncode != 0:
error_text = (result.stderr or "").strip() or (result.stdout or "").strip() or "unknown error"
raise RuntimeError(f"长期记忆写入失败: {error_text}")