import json, os class MemoryManager: def __init__(self, agent_id): self.agent_id = agent_id self.memory_file = f"memory_{agent_id}.json" if not os.path.exists(self.memory_file): with open(self.memory_file, "w") as f: json.dump({"history": []}, f) def save(self, event): with open(self.memory_file, "r") as f: data = json.load(f) data["history"].append(event) with open(self.memory_file, "w") as f: json.dump(data, f) def recall(self, n=5): with open(self.memory_file, "r") as f: data = json.load(f) return data["history"][-n:] class ContextGraph: def __init__(self): self.graph_file = "context_graph.json" if not os.path.exists(self.graph_file): with open(self.graph_file, "w") as f: json.dump({"nodes": [], "links": []}, f) def add_link(self, source, target, info): with open(self.graph_file, "r") as f: data = json.load(f) data["links"].append({"source": source, "target": target, "info": info}) with open(self.graph_file, "w") as f: json.dump(data, f)