-
-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathsummaries.py
More file actions
70 lines (60 loc) · 2.14 KB
/
summaries.py
File metadata and controls
70 lines (60 loc) · 2.14 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
"""Per-channel summary store — agents write, everyone reads."""
import json
import time
import threading
import uuid
from pathlib import Path
MAX_CHARS = 1000
class SummaryStore:
def __init__(self, path: str):
self._path = Path(path)
self._path.parent.mkdir(parents=True, exist_ok=True)
self._summaries: dict[str, dict] = {} # channel → summary dict
self._lock = threading.Lock()
self._load()
def _load(self):
if not self._path.exists():
return
try:
raw = json.loads(self._path.read_text("utf-8"))
if isinstance(raw, dict):
self._summaries = raw
except (json.JSONDecodeError, KeyError):
self._summaries = {}
def _save(self):
self._path.write_text(
json.dumps(self._summaries, indent=2, ensure_ascii=False) + "\n",
"utf-8",
)
def get(self, channel: str) -> dict | None:
with self._lock:
entry = self._summaries.get(channel)
return dict(entry) if entry else None
def get_all(self) -> dict:
with self._lock:
return {ch: dict(s) for ch, s in self._summaries.items()}
def write(self, channel: str, text: str, author: str, message_id: int = 0,
uid: str | None = None, updated_at: float | None = None) -> dict | None:
text = text.strip()
if not text:
return None
if len(text) > MAX_CHARS:
return None # Caller should inform the agent
with self._lock:
entry = {
"uid": uid or str(uuid.uuid4()),
"text": text,
"author": author,
"updated_at": updated_at if updated_at is not None else time.time(),
"message_id": message_id,
}
self._summaries[channel] = entry
self._save()
return dict(entry)
def delete(self, channel: str) -> bool:
with self._lock:
if channel in self._summaries:
del self._summaries[channel]
self._save()
return True
return False