-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
68 lines (60 loc) · 2.32 KB
/
memory.py
File metadata and controls
68 lines (60 loc) · 2.32 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
import json, os
from datetime import datetime
from config import MEMORY_FILE, MAX_MEMORY
DEFAULT_MEMORY = {"messages": [], "summaries": [], "thoughts": [], "decisions": []}
def load_memory():
if not os.path.exists(MEMORY_FILE):
os.makedirs(os.path.dirname(MEMORY_FILE), exist_ok=True)
save_memory(DEFAULT_MEMORY)
return DEFAULT_MEMORY.copy()
try:
with open(MEMORY_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data, dict):
return DEFAULT_MEMORY.copy()
data.setdefault("messages", [])
data.setdefault("summaries", [])
data.setdefault("thoughts", [])
data.setdefault("decisions", [])
return data
except:
return DEFAULT_MEMORY.copy()
def save_memory(mem):
os.makedirs(os.path.dirname(MEMORY_FILE), exist_ok=True)
with open(MEMORY_FILE, "w", encoding="utf-8") as f:
json.dump(mem, f, ensure_ascii=False, indent=2)
def add(mem, role, content):
entry = {"role": role, "content": content, "time": datetime.now().isoformat()}
mem.setdefault("messages", []).append(entry)
if len(mem["messages"]) > MAX_MEMORY:
mem["messages"] = mem["messages"][-MAX_MEMORY:]
def add_message(mem, role, content):
return add(mem, role, content)
def add_decision(mem, decision, reason=None):
if "decisions" not in mem or not isinstance(mem["decisions"], list):
mem["decisions"] = []
entry = {
"decision": decision,
"reason": reason,
"time": datetime.now().isoformat()
}
mem["decisions"].append(entry)
if len(mem["decisions"]) > MAX_MEMORY:
mem["decisions"] = mem["decisions"][-MAX_MEMORY:]
def get_memory_stats(mem):
return {
"total_messages": len(mem.get("messages", [])),
"total_thoughts": len(mem.get("thoughts", [])),
"total_decisions": len(mem.get("decisions", [])),
}
def add_thought(mem, thought, thought_type="internal"):
if "thoughts" not in mem or not isinstance(mem["thoughts"], list):
mem["thoughts"] = []
entry = {
"type": thought_type,
"content": thought,
"time": datetime.now().isoformat()
}
mem["thoughts"].append(entry)
if len(mem["thoughts"]) > MAX_MEMORY:
mem["thoughts"] = mem["thoughts"][-MAX_MEMORY:]