Tpayne101 commited on
Commit
0cf9212
·
verified ·
1 Parent(s): 8bcbb2b

Update context_graph.py

Browse files
Files changed (1) hide show
  1. context_graph.py +10 -12
context_graph.py CHANGED
@@ -1,28 +1,26 @@
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)
 
1
  import json
2
  import os
3
 
4
+ class ContextMemoryGraph:
5
+ def __init__(self, file_path="context_graph.json"):
6
+ self.file_path = file_path
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({"context_links": {}}, f)
13
 
14
+ def link_context(self, agent_id, key, value):
15
  with open(self.file_path, "r") as f:
16
  graph = json.load(f)
17
+ if agent_id not in graph["context_links"]:
18
+ graph["context_links"][agent_id] = {}
19
+ graph["context_links"][agent_id][key] = value
20
  with open(self.file_path, "w") as f:
21
  json.dump(graph, f, indent=2)
 
22
 
23
+ def get_context(self, agent_id):
24
  with open(self.file_path, "r") as f:
25
  graph = json.load(f)
26
+ return graph["context_links"].get(agent_id, {})