Update recursive_context.py
Browse files- recursive_context.py +23 -2
recursive_context.py
CHANGED
|
@@ -721,8 +721,8 @@ class RecursiveContextManager:
|
|
| 721 |
testament_results.sort(key=lambda r: (not r.get('is_testament', False)))
|
| 722 |
return testament_results[:n]
|
| 723 |
|
| 724 |
-
|
| 725 |
-
"""WHY: Provides the
|
| 726 |
try:
|
| 727 |
return {
|
| 728 |
"total_files": self.collection.count(),
|
|
@@ -735,6 +735,27 @@ class RecursiveContextManager:
|
|
| 735 |
except Exception as e:
|
| 736 |
return {"index_error": str(e)}
|
| 737 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 738 |
def save_conversation_turn(self, u, a, t_id):
|
| 739 |
"""WHY: Prevents amnesia by pushing the FULL history to the cloud, not just the last turn."""
|
| 740 |
combined = f"USER: {u}\n\nASSISTANT: {a}"
|
|
|
|
| 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 |
+
"""WHY: Provides the metrics for the sidebar to prevent 'blind' coding."""
|
| 726 |
try:
|
| 727 |
return {
|
| 728 |
"total_files": self.collection.count(),
|
|
|
|
| 735 |
except Exception as e:
|
| 736 |
return {"index_error": str(e)}
|
| 737 |
|
| 738 |
+
def save_conversation_turn(self, u, a, t_id):
|
| 739 |
+
"""WHY: Pulls the FULL history before pushing to 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 to ChromaDB
|
| 744 |
+
self.conversations.add(documents=[combined], metadatas=[{"turn": t_id}], ids=[u_id])
|
| 745 |
+
|
| 746 |
+
# 2. Retrieve the complete historical record to avoid overwriting with a single turn
|
| 747 |
+
all_convs = self.conversations.get()
|
| 748 |
+
full_data = []
|
| 749 |
+
for i in range(len(all_convs['ids'])):
|
| 750 |
+
full_data.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 entire manifest back to your PRO storage dataset
|
| 757 |
+
self.persistence.save_conversations(full_data)
|
| 758 |
+
|
| 759 |
def save_conversation_turn(self, u, a, t_id):
|
| 760 |
"""WHY: Prevents amnesia by pushing the FULL history to the cloud, not just the last turn."""
|
| 761 |
combined = f"USER: {u}\n\nASSISTANT: {a}"
|