File size: 879 Bytes
540c668
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json, os

class ContextMemoryGraph:
    def __init__(self, file_path="context_graph.json"):
        self.file_path = file_path
        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({"context_links": {}}, f)

    def link_context(self, agent_id, key, value):
        with open(self.file_path, "r") as f:
            graph = json.load(f)
        if agent_id not in graph["context_links"]:
            graph["context_links"][agent_id] = {}
        graph["context_links"][agent_id][key] = value
        with open(self.file_path, "w") as f:
            json.dump(graph, f, indent=2)

    def get_context(self, agent_id):
        with open(self.file_path, "r") as f:
            graph = json.load(f)
        return graph["context_links"].get(agent_id, {})