-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_manager.py
More file actions
57 lines (47 loc) · 1.8 KB
/
memory_manager.py
File metadata and controls
57 lines (47 loc) · 1.8 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
import json
import os
from datetime import datetime
MEMORY_FILE = "focus_memory.json"
def load_memory():
if not os.path.exists(MEMORY_FILE):
return []
with open(MEMORY_FILE, "r", encoding="utf-8") as f:
try:
return json.load(f)
except json.JSONDecodeError:
return []
def save_memory(entry):
"""Appends structured memory entry to persistent file."""
data = load_memory()
data.append(entry)
with open(MEMORY_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
def record_session(goal, duration, reflection, actual_focus=None, fatigue_score=None, breaks_taken=0):
"""Stores structured feedback for each session."""
entry = {
"goal": goal,
"duration": duration,
"reflection": reflection,
"actual_focus_minutes": actual_focus,
"breaks_taken": breaks_taken,
"fatigue_score": fatigue_score,
"timestamp": datetime.now().isoformat()
}
save_memory(entry)
def get_recent_sessions(n=3):
"""Returns recent sessions as text for reflection context."""
data = load_memory()[-n:]
if not data:
return "No previous sessions found."
summary = []
for d in data:
fatigue = f" (fatigue {d['fatigue_score']}/5)" if d.get("fatigue_score") else ""
summary.append(f"- {d['goal']} ({d['duration']}) → focus {d.get('actual_focus_minutes','?')} min{fatigue}")
return "\n".join(summary)
def compute_average_focus_time():
"""Computes avg actual focus minutes from recorded memory."""
data = [d for d in load_memory() if d.get("actual_focus_minutes")]
if not data:
return None
total = sum(d["actual_focus_minutes"] for d in data)
return round(total / len(data), 1)