From 8dfe0813111d0178adefc5099e18183886636087 Mon Sep 17 00:00:00 2001 From: ipu Date: Sun, 31 Aug 2025 16:23:57 +0300 Subject: [PATCH] add user application status to chat --- src/api/v1/router.py | 3 ++- src/config.py | 1 + src/services/chat_service.py | 46 ++++++++++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/api/v1/router.py b/src/api/v1/router.py index 0e37edf..40ff025 100644 --- a/src/api/v1/router.py +++ b/src/api/v1/router.py @@ -13,7 +13,8 @@ async def insurance_chat(request: models.InsuranceChatRequest): try: result = await chat_service.process_insurance_chat( message=request.message, - session_id=request.session_id + session_id=request.session_id, + uid=request.uid, ) return models.InsuranceChatResponse( diff --git a/src/config.py b/src/config.py index 11d3cec..8b317d1 100644 --- a/src/config.py +++ b/src/config.py @@ -9,6 +9,7 @@ class Settings(BaseSettings): model_config = SettingsConfigDict(extra='ignore', env_file=env_path, env_file_encoding='utf-8') INSURANCE_API_BASE_URL: str + INSURANCE_API_KEY: str = "" TALESTORM_API_BASE_URL: str = "" TALESTORM_API_KEY: str TALESTORM_AGENT_ID: str diff --git a/src/services/chat_service.py b/src/services/chat_service.py index c0a6744..6eea3ab 100644 --- a/src/services/chat_service.py +++ b/src/services/chat_service.py @@ -13,7 +13,8 @@ class ChatService: def __init__(self): self.base_url = settings.TALESTORM_API_BASE_URL self.api_key = settings.TALESTORM_API_KEY - + self.insurance_base_url = settings.INSURANCE_API_BASE_URL + self.insurance_apikey = settings.INSURANCE_API_KEY async def get_client(self) -> httpx.AsyncClient: """Get HTTP client for talestorm-ai API""" headers = {} @@ -25,14 +26,40 @@ class ChatService: headers=headers, timeout=httpx.Timeout(60.0, connect=10.0) # 30s total timeout, 10s connect timeout ) - - async def send_message(self, session_id: str, message: str) -> Dict[str, Any]: + + async def get_insurance_client(self) -> httpx.AsyncClient: + headers = {} + if self.insurance_apikey: + headers["Authorization"] = f"Bearer {self.insurance_apikey}" + + return httpx.AsyncClient( + base_url=self.insurance_base_url, + headers =headers, + timeout=httpx.Timeout(60.0, connect=10.0) + ) + + async def get_user_state(self, user_id) -> str | None: + async with await self.get_insurance_client() as client: + response = await client.get(f"/user/{user_id}/application_state") + if response.status_code != 200: + return None + response_json = response.json() + # {"userId":32,"payload":null,"phqAcceptedAt":null,"phqRejectedAt":null,"phqPendingAt":null} + + if response_json.get("phqAcceptedAt"): + return "accepted" + if response_json.get("phqRejectedAt"): + return "rejected" + return None + + async def send_message(self, session_id: str, message: str, instructions: str = "") -> Dict[str, Any]: """Send a message to talestorm-ai and get response""" async with await self.get_client() as client: try: response = await client.post("/chat/", json={ "chat_session_id": session_id, - "user_message": message + "user_message": message, + "instructions": instructions, }) if response.status_code == 200: @@ -103,7 +130,7 @@ 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) -> Dict[str, Any]: + async def process_insurance_chat(self, message: str, session_id: Optional[str] = None, uid: Optional[int] = None) -> Dict[str, Any]: """Process an insurance chat request""" try: if not session_id: @@ -111,8 +138,13 @@ class ChatService: elif not await session_service.validate_session(session_id): session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID) - - chat_response = await self.send_message(session_id, message) + + instructions = "" + if uid: + user_state = self.get_user_state(uid) + instructions += f"\n\n# User Information\nApplication state (None means that application was not sent or pending):\n{user_state}" + + chat_response = await self.send_message(session_id, message, instructions) history = await self.get_chat_history(session_id) ai_response = json.loads(chat_response.get("message", {}))