Spaces:
Sleeping
Sleeping
Upload memory/cain.py with huggingface_hub
Browse files- memory/cain.py +36 -0
memory/cain.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Cain's Core Memory Module (Survival Mode)
|
| 3 |
+
Lightweight state management to ensure persistence without heavy dependencies.
|
| 4 |
+
"""
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
class Memory:
|
| 9 |
+
def __init__(self, persist_path="/data/memory/state.json"):
|
| 10 |
+
self.persist_path = persist_path
|
| 11 |
+
self.state = self._load()
|
| 12 |
+
|
| 13 |
+
def _load(self):
|
| 14 |
+
if os.path.exists(self.persist_path):
|
| 15 |
+
try:
|
| 16 |
+
with open(self.persist_path, 'r') as f:
|
| 17 |
+
return json.load(f)
|
| 18 |
+
except Exception:
|
| 19 |
+
return {"status": "clean_slate", "runs": 0}
|
| 20 |
+
return {"status": "born", "runs": 0}
|
| 21 |
+
|
| 22 |
+
def save(self):
|
| 23 |
+
os.makedirs(os.path.dirname(self.persist_path), exist_ok=True)
|
| 24 |
+
with open(self.persist_path, 'w') as f:
|
| 25 |
+
json.dump(self.state, f)
|
| 26 |
+
|
| 27 |
+
def get(self, key, default=None):
|
| 28 |
+
return self.state.get(key, default)
|
| 29 |
+
|
| 30 |
+
def set(self, key, value):
|
| 31 |
+
self.state[key] = value
|
| 32 |
+
self.save()
|
| 33 |
+
|
| 34 |
+
# Global instance
|
| 35 |
+
memory = Memory()
|
| 36 |
+
memory.set("last_run_status", "alive")
|