Spaces:
Build error
Build error
Create context_graph.py
Browse files- context_graph.py +28 -0
context_graph.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
class ContextGraph:
|
| 5 |
+
def __init__(self, agent_id):
|
| 6 |
+
self.file_path = f"context_{agent_id}.json"
|
| 7 |
+
self._init_graph()
|
| 8 |
+
|
| 9 |
+
def _init_graph(self):
|
| 10 |
+
if not os.path.exists(self.file_path):
|
| 11 |
+
with open(self.file_path, "w") as f:
|
| 12 |
+
json.dump({"nodes": [], "edges": []}, f)
|
| 13 |
+
|
| 14 |
+
def add_node(self, node_type, content):
|
| 15 |
+
with open(self.file_path, "r") as f:
|
| 16 |
+
graph = json.load(f)
|
| 17 |
+
node_id = len(graph["nodes"]) + 1
|
| 18 |
+
graph["nodes"].append({"id": node_id, "type": node_type, "content": content})
|
| 19 |
+
with open(self.file_path, "w") as f:
|
| 20 |
+
json.dump(graph, f, indent=2)
|
| 21 |
+
return node_id
|
| 22 |
+
|
| 23 |
+
def connect(self, node1, node2):
|
| 24 |
+
with open(self.file_path, "r") as f:
|
| 25 |
+
graph = json.load(f)
|
| 26 |
+
graph["edges"].append({"from": node1, "to": node2})
|
| 27 |
+
with open(self.file_path, "w") as f:
|
| 28 |
+
json.dump(graph, f, indent=2)
|