-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmemory.py
More file actions
33 lines (26 loc) · 1023 Bytes
/
Copy pathmemory.py
File metadata and controls
33 lines (26 loc) · 1023 Bytes
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
import json
import os
from datetime import datetime
class SessionMemory:
def __init__(self, session_dir="sessions"):
self.session_dir = session_dir
os.makedirs(self.session_dir, exist_ok=True)
def save(self, topic: str, data: dict):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_topic = topic.replace(" ", "_").lower()
filename = f"{safe_topic}_{timestamp}.json"
path = os.path.join(self.session_dir, filename)
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
return path
# ✅ NEW: list saved sessions
def list_sessions(self):
return sorted(
os.listdir(self.session_dir),
reverse=True
)
# ✅ NEW: load a session
def load(self, filename: str):
path = os.path.join(self.session_dir, filename)
with open(path, "r", encoding="utf-8") as f:
return json.load(f)