AgentOS-Core-V2 / context_graph.py
Tpayne101's picture
Create context_graph.py
98323d5 verified
raw
history blame
1.05 kB
import json
import os
class ContextGraph:
"""
Stores relationships between prompts and responses.
Think of it as a 'map' of what the agent has learned.
"""
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({"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["links"]:
graph["links"][agent_id] = {}
graph["links"][agent_id][key] = value
with open(self.file_path, "w") as f:
json.dump(graph, f, indent=2)
print(f"[ContextGraph] Linked ({key}{value}) for {agent_id}")
def get_context(self, agent_id):
with open(self.file_path, "r") as f:
graph = json.load(f)
return graph["links"].get(agent_id, {})