Executor-Tyrant-Framework commited on
Commit
357e8ae
·
verified ·
1 Parent(s): c426220

Update recursive_context.py

Browse files
Files changed (1) hide show
  1. recursive_context.py +34 -27
recursive_context.py CHANGED
@@ -721,30 +721,37 @@ class RecursiveContextManager:
721
  testament_results.sort(key=lambda r: (not r.get('is_testament', False)))
722
  return testament_results[:n]
723
 
724
- def save_conversation_turn(self, user_msg: str, assistant_msg: str, turn_id):
725
- """Save a conversation turn to ChromaDB and cloud backup.
726
-
727
- CHANGELOG [2025-01-28 - Josh]
728
- Persistent memory across sessions. Every user/assistant exchange
729
- gets embedded and stored in ChromaDB for semantic retrieval later.
730
-
731
- CHANGELOG [2026-01-31 - Gemini]
732
- Added cloud backup via HFDatasetPersistence so conversations survive
733
- Space restarts.
734
-
735
- Args:
736
- user_msg: The user's message text
737
- assistant_msg: The assistant's response text
738
- turn_id: Conversation turn number for ordering
739
- """
740
- combined = f"USER: {user_msg}\n\nASSISTANT: {assistant_msg}"
741
- unique_id = f"turn_{int(time.time())}_{turn_id}"
742
- self.conversations.add(
743
- documents=[combined],
744
- metadatas=[{"turn": turn_id, "timestamp": int(time.time())}],
745
- ids=[unique_id]
746
- )
747
- # Cloud backup
748
- self.persistence.save_conversations([
749
- {"document": combined, "metadata": {"turn": turn_id}, "id": unique_id}
750
- ])
 
 
 
 
 
 
 
 
721
  testament_results.sort(key=lambda r: (not r.get('is_testament', False)))
722
  return testament_results[:n]
723
 
724
+ def get_stats(self) -> dict:
725
+ """Fetch current system statistics for the sidebar."""
726
+ try:
727
+ return {
728
+ "total_files": self.collection.count(),
729
+ "indexed_chunks": self.collection.count(),
730
+ "conversations": self.conversations.count(),
731
+ "chroma_path": CHROMA_DB_PATH,
732
+ "persistence_configured": self.persistence.is_configured,
733
+ "indexing_in_progress": False
734
+ }
735
+ except Exception as e:
736
+ return {"index_error": str(e)}
737
+
738
+ def save_conversation_turn(self, u, a, t_id):
739
+ """Save turn locally and push the FULL history to the cloud to prevent memory loss."""
740
+ combined = f"USER: {u}\n\nASSISTANT: {a}"
741
+ u_id = f"turn_{int(time.time())}"
742
+
743
+ # 1. Save locally
744
+ self.conversations.add(documents=[combined], metadatas=[{"turn": t_id}], ids=[u_id])
745
+
746
+ # 2. To prevent amnesia, we must retrieve ALL historical turns from the local database
747
+ all_convs = self.conversations.get()
748
+ data_to_save = []
749
+ for i in range(len(all_convs['ids'])):
750
+ data_to_save.append({
751
+ "document": all_convs['documents'][i],
752
+ "metadata": all_convs['metadatas'][i],
753
+ "id": all_convs['ids'][i]
754
+ })
755
+
756
+ # 3. Push the COMPLETE history to your PRO storage (replaces the previous file)
757
+ self.persistence.save_conversations(data_to_save)