Tpayne101 commited on
Commit
ff2a3d0
·
verified ·
1 Parent(s): 1b4ac82

Update memory_core.py

Browse files
Files changed (1) hide show
  1. memory_core.py +19 -17
memory_core.py CHANGED
@@ -1,26 +1,28 @@
1
- import json
2
- import os
3
 
4
- class MemoryCore:
5
- def __init__(self, file_path="memory_store.json"):
6
- self.file_path = file_path
7
- self._init_store()
 
8
 
9
- def _init_store(self):
 
10
  if not os.path.exists(self.file_path):
11
  with open(self.file_path, "w") as f:
12
- json.dump({}, f)
13
 
14
- def learn(self, agent_id, key, value):
 
15
  with open(self.file_path, "r") as f:
16
- data = json.load(f)
17
- if agent_id not in data:
18
- data[agent_id] = []
19
- data[agent_id].append({"key": key, "value": value})
20
  with open(self.file_path, "w") as f:
21
- json.dump(data, f, indent=2)
 
22
 
23
- def recall_context(self, agent_id):
 
24
  with open(self.file_path, "r") as f:
25
- data = json.load(f)
26
- return data.get(agent_id, [])
 
1
+ import json, os
 
2
 
3
+ class MemoryManager:
4
+ def __init__(self, agent_id, file_path=None):
5
+ self.agent_id = agent_id
6
+ self.file_path = file_path or f"memory_{agent_id}.json"
7
+ self._init_memory()
8
 
9
+ def _init_memory(self):
10
+ # If no memory file exists, create it
11
  if not os.path.exists(self.file_path):
12
  with open(self.file_path, "w") as f:
13
+ json.dump({"history": []}, f)
14
 
15
+ def save(self, data):
16
+ # Append a new memory entry
17
  with open(self.file_path, "r") as f:
18
+ memory = json.load(f)
19
+ memory["history"].append(data)
 
 
20
  with open(self.file_path, "w") as f:
21
+ json.dump(memory, f, indent=2)
22
+ os.sync() # ✅ ensures Hugging Face writes to disk immediately
23
 
24
+ def recall(self):
25
+ # Load all past memory for this agent
26
  with open(self.file_path, "r") as f:
27
+ memory = json.load(f)
28
+ return memory.get("history", [])