import json import os def load_memory(agent_id): path = f"agents/{agent_id}/memory.json" if not os.path.exists(path): return [] with open(path, "r", encoding="utf-8") as f: return json.load(f) def save_memory(agent_id, memory): path = f"agents/{agent_id}/memory.json" with open(path, "w", encoding="utf-8") as f: json.dump( memory, f, indent=2 ) def add_message(agent_id, role, content): memory = load_memory(agent_id) memory.append({ "role": role, "content": content }) memory = memory[-10:] save_memory(agent_id, memory)