import json import os class MemoryManager: def __init__(self, agent_id): self.agent_id = agent_id self.file_path = f"memory_{agent_id}.json" self._init_memory() def _init_memory(self): if not os.path.exists(self.file_path): with open(self.file_path, "w") as f: json.dump({"history": []}, f) def save(self, data): 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 the file actually saves on Hugging Face