From 37920ef259a43eee7a0ac6f03297dec82ac32e1e Mon Sep 17 00:00:00 2001 From: ipu Date: Wed, 1 Oct 2025 14:32:20 +0300 Subject: [PATCH] add name to context --- src/api/v1/router.py | 6 +++++- src/models.py | 5 +++++ src/services/chat_service.py | 18 +++++------------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/api/v1/router.py b/src/api/v1/router.py index 1009f5b..e1b2bef 100644 --- a/src/api/v1/router.py +++ b/src/api/v1/router.py @@ -54,7 +54,11 @@ async def init_chat(request: models.InitializeChatRequest): elif request.context and request.context.applicationDTO: application = json.loads(base64.b64decode(request.context.applicationDTO).decode()) - result = await chat_service.initialize_chat(str(request.userId), application) + name = None + if request.context and request.context.name: + name = request.context.name + + result = await chat_service.initialize_chat(str(request.userId), application, name.first_name) return models.InitializeChatResponse( session_id=result["session_id"], answer=result["answer"], diff --git a/src/models.py b/src/models.py index 684250c..d3e8089 100644 --- a/src/models.py +++ b/src/models.py @@ -85,10 +85,15 @@ class EstimationResponse(BaseModel): details: EstimationDetails results: List[EstimationResult] +class UserNameContext(BaseModel): + first_name: str + last_name: str + class InsuranceChatContext(BaseModel): page: str application: dict | None = None applicationDTO: str | None = None + name: UserNameContext | None = None class InsuranceChatRequest(BaseModel): userId: str | int | None = None diff --git a/src/services/chat_service.py b/src/services/chat_service.py index 559411a..57f2bff 100644 --- a/src/services/chat_service.py +++ b/src/services/chat_service.py @@ -1,14 +1,14 @@ import json +from datetime import datetime from typing import Dict, Any, List, Optional import httpx -from src.models import ApplicantParam, ChatHook, PlansParam, InsuranceChatContext +from src.models import ApplicantParam, ChatHook, PlansParam from .session_service import session_service from ..api.v1.models import Source, HistoryItem from ..config import settings from ..database import Session, UserSession -from datetime import datetime # vars: name INIT_MESSAGES = [ @@ -173,7 +173,7 @@ class ChatService: session.add(user_session) session.commit() - async def initialize_chat(self, uid: str, application): + async def initialize_chat(self, uid: str, application, name): 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) @@ -183,7 +183,8 @@ class ChatService: pass try: - name = application["applicants"][0]["firstName"] + if not name: + name = application["applicants"][0]["firstName"] except: return { "session_id": session_id, @@ -210,15 +211,6 @@ class ChatService: } - - - - - - - - - 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: