Spaces:
Running
Running
Update websocket_handler.py
Browse files- websocket_handler.py +74 -1
websocket_handler.py
CHANGED
@@ -367,7 +367,80 @@ class RealtimeSession:
|
|
367 |
delattr(self, 'speech_started')
|
368 |
|
369 |
log_info(f"✅ Reset for new utterance complete", session_id=self.session.session_id)
|
370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
async def cleanup(self):
|
372 |
"""Clean up resources"""
|
373 |
try:
|
|
|
367 |
delattr(self, 'speech_started')
|
368 |
|
369 |
log_info(f"✅ Reset for new utterance complete", session_id=self.session.session_id)
|
370 |
+
|
371 |
+
async def handle_control_message(websocket: WebSocket, session: RealtimeSession, message: Dict[str, Any]):
|
372 |
+
"""Handle control messages"""
|
373 |
+
action = message.get("action")
|
374 |
+
config = message.get("config", {})
|
375 |
+
|
376 |
+
log_debug(f"🎮 Control message", action=action, session_id=session.session.session_id)
|
377 |
+
|
378 |
+
if action == "start_session":
|
379 |
+
# Session configuration
|
380 |
+
await websocket.send_json({
|
381 |
+
"type": "session_config",
|
382 |
+
"session_id": session.session.session_id,
|
383 |
+
"config": {
|
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 |
+
elif action == "end_session" or action == "stop_session":
|
391 |
+
# Clean up and close
|
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 |
+
elif action == "reset":
|
400 |
+
# Reset conversation state
|
401 |
+
await session.reset_for_new_utterance()
|
402 |
+
await session.stop_stt_streaming()
|
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 |
+
elif action == "audio_ended":
|
411 |
+
# Audio playback ended on client
|
412 |
+
if session.state == ConversationState.PLAYING_AUDIO:
|
413 |
+
log_info(f"🎵 Client reported audio ended", session_id=session.session.session_id)
|
414 |
+
await session.change_state(ConversationState.LISTENING)
|
415 |
+
await websocket.send_json({
|
416 |
+
"type": "state_change",
|
417 |
+
"from": "playing_audio",
|
418 |
+
"to": "listening"
|
419 |
+
})
|
420 |
+
# STT'yi yeniden başlat
|
421 |
+
success = await session.restart_stt_if_needed()
|
422 |
+
|
423 |
+
# STT hazır olduğunda sinyal gönder
|
424 |
+
if success and session.is_streaming:
|
425 |
+
log_info(f"✅ Sending STT ready signal", session_id=session.session.session_id)
|
426 |
+
await websocket.send_json({
|
427 |
+
"type": "stt_ready",
|
428 |
+
"message": "STT is ready to receive audio"
|
429 |
+
})
|
430 |
+
else:
|
431 |
+
log_error(f"❌ STT not ready after restart", session_id=session.session.session_id)
|
432 |
+
await websocket.send_json({
|
433 |
+
"type": "error",
|
434 |
+
"error_type": "stt_init_failed",
|
435 |
+
"message": "Failed to initialize STT after audio playback"
|
436 |
+
})
|
437 |
+
|
438 |
+
elif action == "restart_stt":
|
439 |
+
# Manual STT restart request
|
440 |
+
log_info(f"🔄 Manual STT restart requested", session_id=session.session.session_id)
|
441 |
+
await session.stop_stt_streaming()
|
442 |
+
await session.restart_stt_if_needed()
|
443 |
+
|
444 |
async def cleanup(self):
|
445 |
"""Clean up resources"""
|
446 |
try:
|