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", [])