ciyidogan commited on
Commit
e0dea2b
Β·
verified Β·
1 Parent(s): d6a3c24

Update websocket_handler.py

Browse files
Files changed (1) hide show
  1. websocket_handler.py +44 -0
websocket_handler.py CHANGED
@@ -305,7 +305,51 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
305
 
306
  # Don't send welcome TTS here - it's already sent by the frontend
307
  log_info(f"πŸ’¬ Ready for conversation", session_id=session_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
 
 
 
309
  try:
310
  while True:
311
  try:
 
305
 
306
  # Don't send welcome TTS here - it's already sent by the frontend
307
  log_info(f"πŸ’¬ Ready for conversation", session_id=session_id)
308
+
309
+ # Send welcome message from session history
310
+ log_info(f"πŸ“‹ Checking for welcome message in session history...", session_id=session_id)
311
+ if session.messages and len(session.messages) > 0:
312
+ log_info(f"πŸ“‹ Found {len(session.messages)} messages in history", session_id=session_id)
313
+
314
+ # Get the last assistant message (welcome message)
315
+ for i, msg in enumerate(reversed(session.messages)):
316
+ log_debug(f"πŸ“‹ Message {i}: role={msg['role']}, content_preview={msg['content'][:50]}...", session_id=session_id)
317
+
318
+ if msg['role'] == 'assistant':
319
+ welcome_text = msg['content']
320
+ log_info(f"πŸ“’ Found welcome message: {welcome_text[:50]}...", session_id=session_id)
321
+
322
+ # Send text first
323
+ try:
324
+ await websocket.send_json({
325
+ "type": "assistant_response",
326
+ "text": welcome_text,
327
+ "is_welcome": True
328
+ })
329
+ log_info(f"βœ… Welcome text sent via WebSocket", session_id=session_id)
330
+ except Exception as e:
331
+ log_error(f"❌ Failed to send welcome text", error=str(e), session_id=session_id)
332
+
333
+ # Generate and send TTS if available
334
+ tts_provider = TTSFactory.create_provider()
335
+ if tts_provider:
336
+ try:
337
+ log_info(f"🎀 Generating welcome TTS...", session_id=session_id)
338
+ await send_tts_welcome_message(websocket, session_id, tts_provider, welcome_text)
339
+ log_info(f"βœ… Welcome TTS sent", session_id=session_id)
340
+ except Exception as e:
341
+ log_error(f"❌ Failed to send welcome TTS", error=str(e), traceback=traceback.format_exc(), session_id=session_id)
342
+ else:
343
+ log_warning(f"⚠️ No TTS provider available", session_id=session_id)
344
+
345
+ break
346
+ else:
347
+ log_warning(f"⚠️ No assistant message found in history", session_id=session_id)
348
+ else:
349
+ log_warning(f"⚠️ No messages in session history", session_id=session_id)
350
 
351
+ log_info(f"πŸ’¬ Ready for conversation", session_id=session_id)
352
+
353
  try:
354
  while True:
355
  try: