Spaces:
Sleeping
Sleeping
Update memory_core.py
Browse files- memory_core.py +19 -17
memory_core.py
CHANGED
|
@@ -1,26 +1,28 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
|
| 4 |
-
class
|
| 5 |
-
def __init__(self, file_path=
|
| 6 |
-
self.
|
| 7 |
-
self.
|
|
|
|
| 8 |
|
| 9 |
-
def
|
|
|
|
| 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
|
|
|
|
| 15 |
with open(self.file_path, "r") as f:
|
| 16 |
-
|
| 17 |
-
|
| 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(
|
|
|
|
| 22 |
|
| 23 |
-
def
|
|
|
|
| 24 |
with open(self.file_path, "r") as f:
|
| 25 |
-
|
| 26 |
-
return
|
|
|
|
| 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", [])
|