File size: 1,151 Bytes
e272f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from app.databases.mongo_handler import MongoDBHandler
from app.main import SessionManager
import logging

class StorageService:
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.session_manager = SessionManager()
        self.mongo_handler = MongoDBHandler()

    async def add_conversation(self, session_id: str, query: str, response: str, sources: list = None):
        """Save conversation to both session manager and MongoDB"""
        try:
            # Save to session manager (memory)
            self.session_manager.add_conversation(session_id, query, response)
            
            # Save to MongoDB (persistent)
            mongo_id = self.mongo_handler.save_conversation(
                session_id=session_id,
                query=query,
                response=response,
                metadata={"sources": sources} if sources else {}
            )
            
            self.logger.debug(f"Conversation saved to MongoDB with ID: {mongo_id}")
            return True
        except Exception as e:
            self.logger.error(f"Failed to save conversation: {e}")
            return False