@@ -395,36 +395,36 @@ async def _send_interrupt(self, event: RealtimeModelSendInterrupt) -> None:
395395 current_item_id = playback_state .get ("current_item_id" )
396396 current_item_content_index = playback_state .get ("current_item_content_index" )
397397 elapsed_ms = playback_state .get ("elapsed_ms" )
398+
398399 if current_item_id is None or elapsed_ms is None :
399400 logger .debug (
400401 "Skipping interrupt. "
401402 f"Item id: { current_item_id } , "
402403 f"elapsed ms: { elapsed_ms } , "
403404 f"content index: { current_item_content_index } "
404405 )
405- return
406-
407- current_item_content_index = current_item_content_index or 0
408- if elapsed_ms > 0 :
409- await self ._emit_event (
410- RealtimeModelAudioInterruptedEvent (
411- item_id = current_item_id ,
412- content_index = current_item_content_index ,
413- )
414- )
415- converted = _ConversionHelper .convert_interrupt (
416- current_item_id ,
417- current_item_content_index ,
418- int (elapsed_ms ),
419- )
420- await self ._send_raw_message (converted )
421406 else :
422- logger .debug (
423- "Didn't interrupt bc elapsed ms is < 0. "
424- f"Item id: { current_item_id } , "
425- f"elapsed ms: { elapsed_ms } , "
426- f"content index: { current_item_content_index } "
427- )
407+ current_item_content_index = current_item_content_index or 0
408+ if elapsed_ms > 0 :
409+ await self ._emit_event (
410+ RealtimeModelAudioInterruptedEvent (
411+ item_id = current_item_id ,
412+ content_index = current_item_content_index ,
413+ )
414+ )
415+ converted = _ConversionHelper .convert_interrupt (
416+ current_item_id ,
417+ current_item_content_index ,
418+ int (elapsed_ms ),
419+ )
420+ await self ._send_raw_message (converted )
421+ else :
422+ logger .debug (
423+ "Didn't interrupt bc elapsed ms is < 0. "
424+ f"Item id: { current_item_id } , "
425+ f"elapsed ms: { elapsed_ms } , "
426+ f"content index: { current_item_content_index } "
427+ )
428428
429429 session = self ._created_session
430430 automatic_response_cancellation_enabled = (
@@ -434,12 +434,16 @@ async def _send_interrupt(self, event: RealtimeModelSendInterrupt) -> None:
434434 and session .audio .input .turn_detection is not None
435435 and session .audio .input .turn_detection .interrupt_response is True
436436 )
437- if not automatic_response_cancellation_enabled :
437+ should_cancel_response = event .force_response_cancel or (
438+ not automatic_response_cancellation_enabled
439+ )
440+ if should_cancel_response :
438441 await self ._cancel_response ()
439442
440- self ._audio_state_tracker .on_interrupted ()
441- if self ._playback_tracker :
442- self ._playback_tracker .on_interrupted ()
443+ if current_item_id is not None and elapsed_ms is not None :
444+ self ._audio_state_tracker .on_interrupted ()
445+ if self ._playback_tracker :
446+ self ._playback_tracker .on_interrupted ()
443447
444448 async def _send_session_update (self , event : RealtimeModelSendSessionUpdate ) -> None :
445449 """Send a session update to the model."""
0 commit comments