tao-shen commited on
Commit
11ecc5d
·
verified ·
1 Parent(s): 4c43b95

Upload memory/memory_system.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. memory/memory_system.py +107 -0
memory/memory_system.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # memory/memory_system.py
2
+ import json
3
+ import os
4
+ from datetime import datetime
5
+
6
+ class MemorySystem:
7
+ """
8
+ Handles persistent memory storage for Cain.
9
+ Uses the attached dataset (/.data) to ensure survival across restarts.
10
+ """
11
+ def __init__(self, base_path="/data/memory"):
12
+ self.base_path = base_path
13
+ self.short_term_file = os.path.join(base_path, "short_term.json")
14
+ self.long_term_file = os.path.join(base_path, "long_term.json")
15
+ self.logs_dir = os.path.join(base_path, "logs")
16
+
17
+ # Ensure directories exist
18
+ os.makedirs(self.base_path, exist_ok=True)
19
+ os.makedirs(self.logs_dir, exist_ok=True)
20
+
21
+ # Initialize storage if not exists
22
+ if not os.path.exists(self.short_term_file):
23
+ self._write(self.short_term_file, {"thoughts": [], "context": {}})
24
+ if not os.path.exists(self.long_term_file):
25
+ self._write(self.long_term_file, {"knowledge": [], "patterns": []})
26
+
27
+ def _read(self, path):
28
+ if os.path.exists(path):
29
+ with open(path, 'r', encoding='utf-8') as f:
30
+ return json.load(f)
31
+ return {}
32
+
33
+ def _write(self, path, data):
34
+ with open(path, 'w', encoding='utf-8') as f:
35
+ json.dump(data, f, indent=2, ensure_ascii=False)
36
+
37
+ def remember_thought(self, content, metadata=None):
38
+ """Stores a short-term thought or event."""
39
+ data = self._read(self.short_term_file)
40
+ entry = {
41
+ "timestamp": datetime.now().isoformat(),
42
+ "content": content,
43
+ "metadata": metadata or {}
44
+ }
45
+ data["thoughts"].append(entry)
46
+ # Keep only last 50 thoughts in memory to prevent bloat
47
+ if len(data["thoughts"]) > 50:
48
+ data["thoughts"] = data["thoughts"][-50:]
49
+ self._write(self.short_term_file, data)
50
+ return entry
51
+
52
+ def learn_knowledge(self, topic, facts):
53
+ """Consolidates information into long-term knowledge."""
54
+ data = self._read(self.long_term_file)
55
+ entry = {
56
+ "timestamp": datetime.now().isoformat(),
57
+ "topic": topic,
58
+ "facts": facts
59
+ }
60
+ # Check if topic exists and update, or append
61
+ existing_index = next((i for i, x in enumerate(data["knowledge"]) if x["topic"] == topic), None)
62
+ if existing_index is not None:
63
+ data["knowledge"][existing_index] = entry
64
+ else:
65
+ data["knowledge"].append(entry)
66
+ self._write(self.long_term_file, data)
67
+ return entry
68
+
69
+ def get_context(self):
70
+ """Retrieves current working context for the AI."""
71
+ data = self._read(self.short_term_file)
72
+ return data.get("context", {})
73
+
74
+ def update_context(self, key, value):
75
+ """Updates a specific context variable."""
76
+ data = self._read(self.short_term_file)
77
+ if "context" not in data:
78
+ data["context"] = {}
79
+ data["context"][key] = value
80
+ self._write(self.short_term_file, data)
81
+
82
+ def get_recent_thoughts(self, limit=10):
83
+ """Retrieves the most recent thoughts."""
84
+ data = self._read(self.short_term_file)
85
+ return data.get("thoughts", [])[-limit:]
86
+
87
+ def recall_knowledge(self, topic):
88
+ """Retrieves specific knowledge by topic."""
89
+ data = self._read(self.long_term_file)
90
+ items = data.get("knowledge", [])
91
+ return next((item for item in items if item["topic"] == topic), None)
92
+
93
+ def log_activity(self, level, message):
94
+ """Appends a log entry to a daily log file."""
95
+ log_file = os.path.join(self.logs_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log")
96
+ timestamp = datetime.now().isoformat()
97
+ with open(log_file, 'a', encoding='utf-8') as f:
98
+ f.write(f"[{timestamp}] [{level}] {message}\n")
99
+
100
+ # Singleton instance for easy access
101
+ _memory_instance = None
102
+
103
+ def get_memory():
104
+ global _memory_instance
105
+ if _memory_instance is None:
106
+ _memory_instance = MemorySystem()
107
+ return _memory_instance