File size: 960 Bytes
0b63a4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)