File size: 1,199 Bytes
f513ee4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
import json, os

class MemoryManager:
    def __init__(self, agent_id):
        self.agent_id = agent_id
        self.memory_file = f"memory_{agent_id}.json"
        if not os.path.exists(self.memory_file):
            with open(self.memory_file, "w") as f:
                json.dump({"history": []}, f)

    def save(self, event):
        with open(self.memory_file, "r") as f:
            data = json.load(f)
        data["history"].append(event)
        with open(self.memory_file, "w") as f:
            json.dump(data, f)

    def recall(self, n=5):
        with open(self.memory_file, "r") as f:
            data = json.load(f)
        return data["history"][-n:]

class ContextGraph:
    def __init__(self):
        self.graph_file = "context_graph.json"
        if not os.path.exists(self.graph_file):
            with open(self.graph_file, "w") as f:
                json.dump({"nodes": [], "links": []}, f)

    def add_link(self, source, target, info):
        with open(self.graph_file, "r") as f:
            data = json.load(f)
        data["links"].append({"source": source, "target": target, "info": info})
        with open(self.graph_file, "w") as f:
            json.dump(data, f)