add user_session

This commit is contained in:
ipu 2025-09-19 12:09:26 +03:00
parent 9b4a6a3ad5
commit b2eba19fb1
3 changed files with 83 additions and 7 deletions

View file

@ -7,6 +7,7 @@ from src.models import ApplicantParam, ChatHook, PlansParam, InsuranceChatContex
from .session_service import session_service
from ..api.v1.models import Source, HistoryItem
from ..config import settings
from ..database import Session, UserSession
class ChatService:
@ -134,15 +135,37 @@ class ChatService:
# For now, return empty list - this would be populated when RAG is implemented
return []
async def process_insurance_chat(self, message: str, session_id: Optional[str] = None, uid: Optional[int] = None, current_page: Optional[str] = None, application: Optional[dict] = None) -> Dict[str, Any]:
async def get_user_session(self, uid: str) -> str | None:
with Session() as session:
session = session.query(UserSession).filter(UserSession.user_id == uid).first()
if not session:
return None
return session.session_id
async def create_user_session(self, uid: str, session_id: str):
with Session() as session:
user_session = UserSession(
user_id=uid,
session_id=session_id,
)
session.add(user_session)
session.commit()
async def process_insurance_chat(self, message: str, session_id: Optional[str] = None, uid: Optional[str] = None, current_page: Optional[str] = None, application: Optional[dict] = None) -> Dict[str, Any]:
"""Process an insurance chat request"""
try:
if not session_id:
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
elif not await session_service.validate_session(session_id):
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
if not session_id or not await session_service.validate_session(session_id):
if uid:
session_id = await self.get_user_session(uid)
if not session_id or not await session_service.validate_session(session_id):
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
await self.create_user_session(uid, session_id)
else:
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
instructions = ""
if uid: