Spaces:
Building
Building
Update websocket_handler.py
Browse files- websocket_handler.py +76 -74
websocket_handler.py
CHANGED
@@ -368,91 +368,93 @@ class RealtimeSession:
|
|
368 |
|
369 |
log_info(f"✅ Reset for new utterance complete", session_id=self.session.session_id)
|
370 |
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
375 |
|
376 |
-
|
|
|
|
|
377 |
|
378 |
-
if
|
379 |
-
#
|
|
|
380 |
await websocket.send_json({
|
381 |
-
"type": "
|
382 |
-
"
|
383 |
-
"
|
384 |
-
"silence_threshold_ms": session.silence_threshold_ms,
|
385 |
-
"audio_chunk_size": session.audio_chunk_size,
|
386 |
-
"supports_barge_in": False # Barge-in devre dışı
|
387 |
-
}
|
388 |
})
|
389 |
|
390 |
-
|
391 |
-
|
392 |
-
await session.cleanup()
|
393 |
-
await websocket.close()
|
394 |
-
|
395 |
-
elif action == "interrupt":
|
396 |
-
# Barge-in devre dışı - ignore
|
397 |
-
log_warning(f"⚠️ Interrupt request ignored (barge-in disabled)", session_id=session.session.session_id)
|
398 |
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
await session.change_state(ConversationState.IDLE)
|
404 |
-
await websocket.send_json({
|
405 |
-
"type": "state_change",
|
406 |
-
"from": session.state.value,
|
407 |
-
"to": "idle"
|
408 |
-
})
|
409 |
|
410 |
-
|
411 |
-
|
412 |
-
log_info(f"🎵 Client reported audio ended, current state: {session.state.value}", session_id=session.session.session_id)
|
413 |
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
await websocket.send_json({
|
418 |
-
"type": "
|
419 |
-
"
|
420 |
-
"to": "listening"
|
421 |
})
|
422 |
-
|
423 |
-
# STT'yi başlat
|
424 |
-
log_info(f"🎤 Starting STT after audio playback ended", session_id=session.session.session_id)
|
425 |
-
|
426 |
-
# Önce mevcut STT varsa temizle
|
427 |
-
if session.stt_manager:
|
428 |
-
await session.stop_stt_streaming()
|
429 |
-
await asyncio.sleep(0.1) # Kısa bekleme
|
430 |
-
|
431 |
-
# Yeni STT başlat
|
432 |
-
success = await session.initialize_stt()
|
433 |
-
|
434 |
-
# STT hazır olduğunda sinyal gönder
|
435 |
-
if success and session.is_streaming:
|
436 |
-
log_info(f"✅ Sending STT ready signal", session_id=session.session.session_id)
|
437 |
-
await websocket.send_json({
|
438 |
-
"type": "stt_ready",
|
439 |
-
"message": "STT is ready to receive audio"
|
440 |
-
})
|
441 |
-
else:
|
442 |
-
log_error(f"❌ STT initialization failed", session_id=session.session.session_id)
|
443 |
-
await websocket.send_json({
|
444 |
-
"type": "error",
|
445 |
-
"error_type": "stt_init_failed",
|
446 |
-
"message": "Failed to initialize STT after audio playback"
|
447 |
-
})
|
448 |
else:
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
456 |
|
457 |
# ========================= MAIN HANDLER =========================
|
458 |
async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
|
|
368 |
|
369 |
log_info(f"✅ Reset for new utterance complete", session_id=self.session.session_id)
|
370 |
|
371 |
+
|
372 |
+
# ========================= Backend-Frontend Messaging =========================
|
373 |
+
async def handle_control_message(websocket: WebSocket, session: RealtimeSession, message: Dict[str, Any]):
|
374 |
+
"""Handle control messages"""
|
375 |
+
action = message.get("action")
|
376 |
+
config = message.get("config", {})
|
377 |
+
|
378 |
+
log_debug(f"🎮 Control message", action=action, session_id=session.session.session_id)
|
379 |
+
|
380 |
+
if action == "start_session":
|
381 |
+
# Session configuration
|
382 |
+
await websocket.send_json({
|
383 |
+
"type": "session_config",
|
384 |
+
"session_id": session.session.session_id,
|
385 |
+
"config": {
|
386 |
+
"silence_threshold_ms": session.silence_threshold_ms,
|
387 |
+
"audio_chunk_size": session.audio_chunk_size,
|
388 |
+
"supports_barge_in": False # Barge-in devre dışı
|
389 |
+
}
|
390 |
+
})
|
391 |
+
|
392 |
+
elif action == "end_session" or action == "stop_session":
|
393 |
+
# Clean up and close
|
394 |
+
await session.cleanup()
|
395 |
+
await websocket.close()
|
396 |
+
|
397 |
+
elif action == "interrupt":
|
398 |
+
# Barge-in devre dışı - ignore
|
399 |
+
log_warning(f"⚠️ Interrupt request ignored (barge-in disabled)", session_id=session.session.session_id)
|
400 |
+
|
401 |
+
elif action == "reset":
|
402 |
+
# Reset conversation state
|
403 |
+
await session.reset_for_new_utterance()
|
404 |
+
await session.stop_stt_streaming()
|
405 |
+
await session.change_state(ConversationState.IDLE)
|
406 |
+
await websocket.send_json({
|
407 |
+
"type": "state_change",
|
408 |
+
"from": session.state.value,
|
409 |
+
"to": "idle"
|
410 |
+
})
|
411 |
|
412 |
+
elif action == "audio_ended":
|
413 |
+
# Audio playback ended on client
|
414 |
+
log_info(f"🎵 Client reported audio ended, current state: {session.state.value}", session_id=session.session.session_id)
|
415 |
|
416 |
+
if session.state == ConversationState.PLAYING_AUDIO:
|
417 |
+
# State'i listening'e çevir
|
418 |
+
await session.change_state(ConversationState.LISTENING)
|
419 |
await websocket.send_json({
|
420 |
+
"type": "state_change",
|
421 |
+
"from": "playing_audio",
|
422 |
+
"to": "listening"
|
|
|
|
|
|
|
|
|
423 |
})
|
424 |
|
425 |
+
# STT'yi başlat
|
426 |
+
log_info(f"🎤 Starting STT after audio playback ended", session_id=session.session.session_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
427 |
|
428 |
+
# Önce mevcut STT varsa temizle
|
429 |
+
if session.stt_manager:
|
430 |
+
await session.stop_stt_streaming()
|
431 |
+
await asyncio.sleep(0.1) # Kısa bekleme
|
|
|
|
|
|
|
|
|
|
|
|
|
432 |
|
433 |
+
# Yeni STT başlat
|
434 |
+
success = await session.initialize_stt()
|
|
|
435 |
|
436 |
+
# STT hazır olduğunda sinyal gönder
|
437 |
+
if success and session.is_streaming:
|
438 |
+
log_info(f"✅ Sending STT ready signal", session_id=session.session.session_id)
|
439 |
await websocket.send_json({
|
440 |
+
"type": "stt_ready",
|
441 |
+
"message": "STT is ready to receive audio"
|
|
|
442 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
443 |
else:
|
444 |
+
log_error(f"❌ STT initialization failed", session_id=session.session.session_id)
|
445 |
+
await websocket.send_json({
|
446 |
+
"type": "error",
|
447 |
+
"error_type": "stt_init_failed",
|
448 |
+
"message": "Failed to initialize STT after audio playback"
|
449 |
+
})
|
450 |
+
else:
|
451 |
+
log_warning(f"⚠️ audio_ended received but state is not playing_audio: {session.state.value}", session_id=session.session.session_id)
|
452 |
+
|
453 |
+
elif action == "restart_stt":
|
454 |
+
# Manual STT restart request
|
455 |
+
log_info(f"🔄 Manual STT restart requested", session_id=session.session.session_id)
|
456 |
+
await session.stop_stt_streaming()
|
457 |
+
await session.restart_stt_if_needed()
|
458 |
|
459 |
# ========================= MAIN HANDLER =========================
|
460 |
async def websocket_endpoint(websocket: WebSocket, session_id: str):
|