Spaces:
Build error
Build error
| import json | |
| import os | |
| class ContextGraph: | |
| def __init__(self, agent_id): | |
| self.file_path = f"context_{agent_id}.json" | |
| self._init_graph() | |
| def _init_graph(self): | |
| if not os.path.exists(self.file_path): | |
| with open(self.file_path, "w") as f: | |
| json.dump({"nodes": [], "edges": []}, f) | |
| def add_node(self, node_type, content): | |
| with open(self.file_path, "r") as f: | |
| graph = json.load(f) | |
| node_id = len(graph["nodes"]) + 1 | |
| graph["nodes"].append({"id": node_id, "type": node_type, "content": content}) | |
| with open(self.file_path, "w") as f: | |
| json.dump(graph, f, indent=2) | |
| return node_id | |
| def connect(self, node1, node2): | |
| with open(self.file_path, "r") as f: | |
| graph = json.load(f) | |
| graph["edges"].append({"from": node1, "to": node2}) | |
| with open(self.file_path, "w") as f: | |
| json.dump(graph, f, indent=2) |