File size: 662 Bytes
486a373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import json
import os

class MemoryManager:
    def __init__(self, agent_id):
        self.agent_id = agent_id
        self.file_path = f"memory_{agent_id}.json"
        self._init_memory()

    def _init_memory(self):
        if not os.path.exists(self.file_path):
            with open(self.file_path, "w") as f:
                json.dump({"history": []}, f)

    def save(self, data):
        with open(self.file_path, "r") as f:
            memory = json.load(f)
        memory["history"].append(data)
        with open(self.file_path, "w") as f:
            json.dump(memory, f, indent=2)
        os.sync()  # Ensures the file actually saves on Hugging Face