Update recursive_context.py
Browse files- recursive_context.py +34 -5
recursive_context.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Recursive Context Manager for Clawdbot
|
| 3 |
-
[Corrected version to fix /.cache PermissionError]
|
| 4 |
"""
|
| 5 |
|
| 6 |
from pathlib import Path
|
|
@@ -117,7 +117,7 @@ class RecursiveContextManager:
|
|
| 117 |
)
|
| 118 |
|
| 119 |
collection_name = self._get_collection_name()
|
| 120 |
-
#
|
| 121 |
self.collection = self.chroma_client.get_or_create_collection(
|
| 122 |
name=collection_name,
|
| 123 |
embedding_function=self.embedding_function,
|
|
@@ -125,7 +125,6 @@ class RecursiveContextManager:
|
|
| 125 |
)
|
| 126 |
|
| 127 |
conversations_name = f"conversations_{collection_name.split('_')[1]}"
|
| 128 |
-
# FIX: Pass embedding_function here as well
|
| 129 |
self.conversations = self.chroma_client.get_or_create_collection(
|
| 130 |
name=conversations_name,
|
| 131 |
embedding_function=self.embedding_function,
|
|
@@ -136,7 +135,7 @@ class RecursiveContextManager:
|
|
| 136 |
self._restore_from_cloud()
|
| 137 |
|
| 138 |
self._saves_since_backup = 0
|
| 139 |
-
self.BACKUP_EVERY_N_SAVES = 1 #
|
| 140 |
self._is_first_save = True
|
| 141 |
|
| 142 |
def _restore_from_cloud(self):
|
|
@@ -210,6 +209,36 @@ class RecursiveContextManager:
|
|
| 210 |
except Exception as e: return [str(e)]
|
| 211 |
|
| 212 |
def save_conversation_turn(self, user_message: str, assistant_message: str, turn_id: int):
|
|
|
|
| 213 |
combined = f"USER: {user_message}\n\nASSISTANT: {assistant_message}"
|
| 214 |
u_id = f"turn_{int(time.time())}_{turn_id}"
|
| 215 |
-
self.conversations.add(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Recursive Context Manager for Clawdbot
|
| 3 |
+
[Corrected version to fix SyntaxError and /.cache PermissionError]
|
| 4 |
"""
|
| 5 |
|
| 6 |
from pathlib import Path
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
collection_name = self._get_collection_name()
|
| 120 |
+
# Ensure the collection uses the custom embedding function
|
| 121 |
self.collection = self.chroma_client.get_or_create_collection(
|
| 122 |
name=collection_name,
|
| 123 |
embedding_function=self.embedding_function,
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
conversations_name = f"conversations_{collection_name.split('_')[1]}"
|
|
|
|
| 128 |
self.conversations = self.chroma_client.get_or_create_collection(
|
| 129 |
name=conversations_name,
|
| 130 |
embedding_function=self.embedding_function,
|
|
|
|
| 135 |
self._restore_from_cloud()
|
| 136 |
|
| 137 |
self._saves_since_backup = 0
|
| 138 |
+
self.BACKUP_EVERY_N_SAVES = 1 # Sync frequently for reliability
|
| 139 |
self._is_first_save = True
|
| 140 |
|
| 141 |
def _restore_from_cloud(self):
|
|
|
|
| 209 |
except Exception as e: return [str(e)]
|
| 210 |
|
| 211 |
def save_conversation_turn(self, user_message: str, assistant_message: str, turn_id: int):
|
| 212 |
+
# FIX: Ensure all brackets and quotes are closed correctly
|
| 213 |
combined = f"USER: {user_message}\n\nASSISTANT: {assistant_message}"
|
| 214 |
u_id = f"turn_{int(time.time())}_{turn_id}"
|
| 215 |
+
self.conversations.add(
|
| 216 |
+
documents=[combined],
|
| 217 |
+
metadatas=[{"user": user_message[:500], "assistant": assistant_message[:500], "turn": turn_id}],
|
| 218 |
+
ids=[u_id]
|
| 219 |
+
)
|
| 220 |
+
if self._is_first_save:
|
| 221 |
+
self._backup_to_cloud(force=True)
|
| 222 |
+
self._is_first_save = False
|
| 223 |
+
else:
|
| 224 |
+
self._saves_since_backup += 1
|
| 225 |
+
if self._saves_since_backup >= self.BACKUP_EVERY_N_SAVES:
|
| 226 |
+
self._backup_to_cloud()
|
| 227 |
+
self._saves_since_backup = 0
|
| 228 |
+
|
| 229 |
+
def search_conversations(self, query: str, n_results: int = 5) -> List[Dict]:
|
| 230 |
+
if self.conversations.count() == 0: return []
|
| 231 |
+
res = self.conversations.query(query_texts=[query], n_results=min(n_results, self.conversations.count()))
|
| 232 |
+
return [{"turn": m.get("turn"), "full_text": d} for d, m in zip(res['documents'][0], res['metadatas'][0])]
|
| 233 |
+
|
| 234 |
+
def get_conversation_count(self) -> int:
|
| 235 |
+
return self.conversations.count()
|
| 236 |
+
|
| 237 |
+
def get_stats(self) -> Dict:
|
| 238 |
+
return {"total_files": self.collection.count(), "conversations": self.conversations.count(), "storage_path": CHROMA_DB_PATH, "cloud_backup_configured": self.persistence.is_configured, "cloud_backup_repo": self.persistence.repo_id}
|
| 239 |
+
|
| 240 |
+
def force_backup(self):
|
| 241 |
+
self._backup_to_cloud(force=True)
|
| 242 |
+
|
| 243 |
+
def shutdown(self):
|
| 244 |
+
self.force_backup()
|