ciyidogan commited on
Commit
b0a5e07
·
verified ·
1 Parent(s): dac537f

Update websocket_handler.py

Browse files
Files changed (1) hide show
  1. websocket_handler.py +38 -8
websocket_handler.py CHANGED
@@ -303,13 +303,10 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
303
  "stt_initialized": stt_initialized
304
  })
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
 
312
- # Session'dan chat history'yi al
313
  chat_history = session.chat_history
314
 
315
  if chat_history and len(chat_history) > 0:
@@ -339,8 +336,42 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
339
  if tts_provider:
340
  try:
341
  log_info(f"🎤 Generating welcome TTS...", session_id=session_id)
342
- await send_tts_welcome_message(websocket, session_id, tts_provider, welcome_text)
343
- log_info(f"✅ Welcome TTS sent", session_id=session_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  except Exception as e:
345
  log_error(f"❌ Failed to send welcome TTS", error=str(e), traceback=traceback.format_exc(), session_id=session_id)
346
  else:
@@ -397,8 +428,7 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
397
  finally:
398
  log_info(f"🧹 Cleaning up WebSocket connection", session_id=session_id)
399
  await realtime_session.cleanup()
400
-
401
-
402
  # ========================= MESSAGE HANDLERS =========================
403
  async def handle_audio_chunk(websocket: WebSocket, session: RealtimeSession, message: Dict[str, Any]):
404
  """Handle incoming audio chunk with barge-in support"""
 
303
  "stt_initialized": stt_initialized
304
  })
305
 
 
 
 
306
  # Send welcome message from session history
307
  log_info(f"📋 Checking for welcome message in session history...", session_id=session_id)
308
 
309
+ # chat_history değişkenini session'dan al
310
  chat_history = session.chat_history
311
 
312
  if chat_history and len(chat_history) > 0:
 
336
  if tts_provider:
337
  try:
338
  log_info(f"🎤 Generating welcome TTS...", session_id=session_id)
339
+
340
+ # TTS preprocessor kullan
341
+ from tts_preprocessor import TTSPreprocessor
342
+ preprocessor = TTSPreprocessor(language=session.locale)
343
+ processed_text = preprocessor.preprocess(
344
+ welcome_text,
345
+ tts_provider.get_preprocessing_flags()
346
+ )
347
+
348
+ # TTS oluştur
349
+ audio_data = await tts_provider.synthesize(processed_text)
350
+
351
+ if audio_data:
352
+ # Audio'yu base64'e çevir ve chunk'lara böl
353
+ audio_base64 = base64.b64encode(audio_data).decode('utf-8')
354
+ chunk_size = 16384
355
+ total_length = len(audio_base64)
356
+ total_chunks = (total_length + chunk_size - 1) // chunk_size
357
+
358
+ log_info(f"📤 Sending welcome TTS in {total_chunks} chunks", session_id=session_id)
359
+
360
+ for i in range(0, total_length, chunk_size):
361
+ chunk = audio_base64[i:i + chunk_size]
362
+ chunk_index = i // chunk_size
363
+ is_last = chunk_index == total_chunks - 1
364
+
365
+ await websocket.send_json({
366
+ "type": "tts_audio",
367
+ "data": chunk,
368
+ "chunk_index": chunk_index,
369
+ "total_chunks": total_chunks,
370
+ "is_last": is_last,
371
+ "mime_type": "audio/mpeg"
372
+ })
373
+
374
+ log_info(f"✅ Welcome TTS sent", session_id=session_id)
375
  except Exception as e:
376
  log_error(f"❌ Failed to send welcome TTS", error=str(e), traceback=traceback.format_exc(), session_id=session_id)
377
  else:
 
428
  finally:
429
  log_info(f"🧹 Cleaning up WebSocket connection", session_id=session_id)
430
  await realtime_session.cleanup()
431
+
 
432
  # ========================= MESSAGE HANDLERS =========================
433
  async def handle_audio_chunk(websocket: WebSocket, session: RealtimeSession, message: Dict[str, Any]):
434
  """Handle incoming audio chunk with barge-in support"""