Spaces:
Sleeping
Sleeping
File size: 960 Bytes
ff2a3d0 f3fb685 ff2a3d0 f3fb685 ff2a3d0 f3fb685 ff2a3d0 f3fb685 ff2a3d0 f3fb685 ff2a3d0 f3fb685 ff2a3d0 f3fb685 ff2a3d0 f3fb685 ff2a3d0 |
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 |
import json, os
class MemoryManager:
def __init__(self, agent_id, file_path=None):
self.agent_id = agent_id
self.file_path = file_path or f"memory_{agent_id}.json"
self._init_memory()
def _init_memory(self):
# If no memory file exists, create it
if not os.path.exists(self.file_path):
with open(self.file_path, "w") as f:
json.dump({"history": []}, f)
def save(self, data):
# Append a new memory entry
with open(self.file_path, "r") as f:
memory = json.load(f)
memory["history"].append(data)
with open(self.file_path, "w") as f:
json.dump(memory, f, indent=2)
os.sync() # ✅ ensures Hugging Face writes to disk immediately
def recall(self):
# Load all past memory for this agent
with open(self.file_path, "r") as f:
memory = json.load(f)
return memory.get("history", []) |