+
    ~jb                    <  a  0 t $ ^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
HtHt ^ RIHtHt ^ RIHtHt ^ RIHt ^ RIHt ^ RIHtHtHtHt ^ RIt^ RIt^ RIHt ^ R	I H!t! ^ R
I"H#t#H$t$H%t%H&t&H't'H(t( ^ RI)H*t* ^RI+H,t, ^RI-H.t.H/t/H0t0 ^RI1H2t2 ^RI3H4t4H5t5 ^RI6H7t7 ^RI8H9t9H:t:H;t;H<t< ^RI,H=t=H>t>H?t?H@t@HAtAHBtB ^RICHDtDHEtEHFtFHGtGHHtH RtIRtJRStK^tLRtMRtNRtO^(tP ! R R]#4      tQ]! RRR7       ! R R4      4       tR]! RRR7       ! R R 4      4       tS ! R! R"]#4      tT]S! ^ R#7      tU ! R$ R%4      tV ! R& R'4      tW ! R( R)4      tX ! R* R+4      tY ! R, R-4      tZ]*P                  ]V,          ]W,          ]X,          ]Y,          ]Z,          t\R.]]R/&    ! R0 R1]*P                  ]RT,          ,          4      t_ ! R2 R3]4      t` ! R4 R5]#4      ta ! R6 R7]`4      tb ! R8 R9]c]4      td ! R: R;]#4      te ! R< R=]#4      tf ! R> R?]#4      tg ! R@ RA]#4      th ! RB RC]#4      ti ! RD RE]#4      tj ! RF RG]#4      tk ! RH RI]#4      tl]g]e,          ]h,          ]i,          ]j,          ]k,          ]l,          tmR.]]RJ&   ]'! ]]m]%! RKRL7      3,          4      tnRM]]RN&    ! RO RP]`4      to]K3RQ RR lltpR# )U    )annotationsN)ABCabstractmethod)AsyncIterableAsyncIterator)	dataclassfield)Enum)perf_counter_ns)	AnnotatedAnyLiteral	TypeAlias)trace)	BaseModel
ConfigDictFieldSerializerFunctionWrapHandlerTypeAdaptermodel_serializer)rtc)utils)APIConnectionErrorAPIErrorAPIStatusErrorlogger)InterruptionMetricsMetadata)trace_types)DEFAULT_API_CONNECT_OPTIONS	NOT_GIVENAPIConnectOptions
NotGivenOr)AudioArrayBufferBoundedDictaiohttp_contextis_given	shortuuid)DEFAULT_INFERENCE_URLSTAGING_INFERENCE_URLcreate_access_tokenget_default_inference_urlget_inference_headersi>  g      ?g?g      ?gffffff?c                      ] tR t^@t$ ]! RR7      tRtR]R&   ]! ]	P                  R7      t
R]R&   R	]R
&   ]! RRR7      tR]R&   R]R&   RtR# )InterruptionDetectionErrorTarbitrary_types_allowedinterruption_detection_errorz'Literal['interruption_detection_error']typedefault_factoryfloat	timestampstrlabel.exclude	Exceptionerrorboolrecoverable N)__name__
__module____qualname____firstlineno__r   model_configr5   __annotations__r   timer9   r?   __static_attributes__rB       z/Users/mitch_tango/dev/rabbit-r1-livekit/agent/.venv/lib/python3.14/site-packages/livekit/agents/inference/interruption.pyr1   r1   @   sF    d;L4RD
1RTYY7Iu7JS$/E9/rK   r1   T)slotskw_onlyc                      ] tR t^It$ R]R&    R]R&    R]R&    R]R&    R]R&    R]R&    R]R	&    R
]R&   R
]R&   R
]R&   R]R&   RtR# )InterruptionOptionsintsample_rater8   	threshold
min_framesmax_audio_durationaudio_prefix_durationdetection_intervalinference_timeoutr:   base_urlapi_key
api_secretr@   	use_proxyrB   NrC   rD   rE   rF   rH   rJ   rB   rK   rL   rP   rP   I   sW    BGOZv  [BJMLOO@rK   rP   c                      ] tR t^`t$ Rt]! ]P                  R7      tR]	R&    Rt
R]	R&   RtR]	R	&   RtR]	R
&   RtR]	R&   RtR]	R&   RtR]	R&   RR R lltRR R lltRR R lltRR R lltRtR# )InterruptionCacheEntryz5Typed cache entry for interruption inference results.r6   rQ   
created_atNnpt.NDArray[np.int16] | Nonespeech_inputfloat | Nonetotal_durationprediction_durationdetection_delaynpt.NDArray[np.float32] | Noneprobabilitieszbool | Noneis_interruptionc                    V ^8  d   QhRRRR/#    defaultr8   returnrB   )formats   "rL   __annotate__#InterruptionCacheEntry.__annotate__m   s     S S% S% SrK   c                :    V P                   e   V P                   # T# )zFRTT (Round Trip Time) time taken to perform the inference, in seconds.rd   selfrm   s   &&rL   get_total_duration)InterruptionCacheEntry.get_total_durationm   s    &*&9&9&Et""R7RrK   c                    V ^8  d   QhRRRR/# rk   rB   )ro   s   "rL   rp   rq   q   s     ] ]u ]u ]rK   c                :    V P                   e   V P                   # T# )zDTime taken to perform the inference from the model side, in seconds.)re   rt   s   &&rL   get_prediction_duration.InterruptionCacheEntry.get_prediction_durationq   s    +/+C+C+Ot''\U\\rK   c                    V ^8  d   QhRRRR/# rk   rB   )ro   s   "rL   rp   rq   u   s     U U5 U5 UrK   c                :    V P                   e   V P                   # T# )zLTotal time from the onset of the speech to the final prediction, in seconds.)rf   rt   s   &&rL   get_detection_delay*InterruptionCacheEntry.get_detection_delayu   s    '+';';'Gt##TWTrK   c                    V ^8  d   QhRRRR/# rk   rB   )ro   s   "rL   rp   rq   y   s     
 
u 
u 
rK   c                L    V P                   e   \        V P                   4      # T# )zAThe conservative estimated probability of the interruption event.)rh   _estimate_probabilityrt   s   &&rL   get_probability&InterruptionCacheEntry.get_probabilityy   s+     :>9K9K9W!$"4"45	
]d	
rK   rB   )        )rC   rD   rE   rF   __doc__r	   rI   r   r`   rH   rb   rd   re   rf   rh   ri   rv   rz   r~   r   rJ   rB   rK   rL   r_   r_   `   sx    ?D,@,@AJAy15L.5#'NL'(,,$(O\(48M18#'O['S]U
 
rK   r_   c                  v   ] tR t^t$ Rt]! RR7      tRtR]R&   ]	! ]
P                  R7      tR]R	&    ]	! ]
P                  R7      tR]R
&    RtR]R&    RtR]R&    RtR]R&    RtR]R&    RtR]R&    RtR]R&    RtR]R&    RtR]R&    ^ tR]R&    ]! RR7      R R l4       t]R RR!R/R" R# ll4       tR$tR# )%OverlappingSpeechEventzDRepresents an overlapping speech event detected during agent speech.Tr2   overlapping_speechzLiteral['overlapping_speech']r5   r6   r8   r`   detected_atFr@   ri   r   rd   re   rf   Nrc   overlap_started_atra   rb   rg   rh   probabilityrQ   num_requestswrap)modec                    V ^8  d   QhRRRR/# )rl   handlerr   rn   r   rB   )ro   s   "rL   rp   #OverlappingSpeechEvent.__annotate__   s     	 	'D 	 	rK   c                	    V P                  R R7      pVP                  VP                  3pRRuVn        Vn         V! V4      pVw  Vn        Vn        V#   Tw  Tn        Tn        i ; i)T)deepN)
model_copyrb   rh   )ru   r   copydata
serializeds   &&   rL   serialize_model&OverlappingSpeechEvent.serialize_model   st     D)  $"4"4404d-4-	9 J481Dt1 591Dt1s   A A%
started_atended_atc          
     ,    V ^8  d   QhRRRRRRRRRR	/# )
rl   entryr_   ri   r@   r   rc   r   rn   r   rB   )ro   s   "rL   rp   r      s>     
 
 &
 	

 !
 
 
 
rK   c                   T ! RT;'       g    \         P                   ! 4       VVVP                  VP                  VP                  4       VP	                  4       VP                  4       VP                  4       R7
      # )aC  Initialize the event from a cache entry.

Args:
    entry: The cache entry to initialize the event from.
    is_interruption: Whether the interruption is detected.
    started_at: The timestamp when the overlap speech started.
    ended_at: The timestamp when the overlap speech ended.

Returns:
    The initialized event.
r   )
r5   r   ri   r   rb   rh   rd   rf   re   r   )rI   rb   rh   rv   r~   rz   r   )clsr   ri   r   r   s   &$$$$rL   from_cache_entry'OverlappingSpeechEvent.from_cache_entry   sm    ( % //DIIK+)++-- 335!557 % = = ?--/
 	
rK   rB   )rC   rD   rE   rF   r   r   rG   r5   rH   r   rI   r`   r   ri   rd   re   rf   r   rb   rh   r   r   r   r   classmethodr   rJ   rB   rK   rL   r   r      s    Nd;L*>D
'>dii8J8<tyy9K9?!OT!+NEP!$$N OU V'++g15L.5:48M18?KKL#16"	 #	 

 $(
 "&
 
rK   r   r`   c                      ] tR t^tRtR# )_AgentSpeechStartedSentinelrB   NrC   rD   rE   rF   rJ   rB   rK   rL   r   r          rK   r   c                      ] tR t^tRtR# )_AgentSpeechEndedSentinelrB   Nr   rB   rK   rL   r   r      r   rK   r   c                  &    ] tR t^tRR R lltRtR# )_OverlapSpeechStartedSentinelNc               (    V ^8  d   QhRRRRRRRR/# )rl   speech_durationr8   r   user_speaking_spanztrace.Span | Nonern   NonerB   )ro   s   "rL   rp   *_OverlapSpeechStartedSentinel.__annotate__   s2     & && & .	&
 
&rK   c                	*    Wn         W0n        W n        R # N)_speech_duration_user_speaking_span_started_at)ru   r   r   r   s   &&&&rL   __init__&_OverlapSpeechStartedSentinel.__init__   s     !0#5 %rK   )r   r   r   r   rC   rD   rE   rF   r   rJ   rB   rK   rL   r   r      s    & &rK   r   c                  "    ] tR t^tR R ltRtR# )_OverlapSpeechEndedSentinelc                    V ^8  d   QhRRRR/# )rl   r   r8   rn   r   rB   )ro   s   "rL   rp   (_OverlapSpeechEndedSentinel.__annotate__   s     " " "4 "rK   c                	    Wn         R # r   	_ended_at)ru   r   s   &&rL   r   $_OverlapSpeechEndedSentinel.__init__   s    !rK   r   Nr   rB   rK   rL   r   r      s    " "rK   r   c                      ] tR t^tRtR# )_FlushSentinelrB   Nr   rB   rK   rL   r   r      r   rK   r   r   InterruptionDataFrameTypec                     a  ] tR tRtR]R]R]R]R]R]	RR	R
R	RR	RR	/
R V 3R lllt
]R R l4       t]R R l4       t]R R l4       t]R R l4       tR R ltR R ltR]/R R lltR]R]/R R lltR tV ;t# )!AdaptiveInterruptionDetectori  rS   min_interruption_durationrU   rV   rW   rX   rY   NrZ   r[   http_sessionc               D    V ^8  d   QhRRRRRRRRRRRRRR	R
R	RR	RRRR/# )rl   rS   r8   r   rU   rV   rW   rX   rY   z
str | NonerZ   r[   r   zaiohttp.ClientSession | Nonern   r   rB   )ro   s   "rL   rp   )AdaptiveInterruptionDetector.__annotate__  s     a
 a
 a
 $)	a

 "a
  %a
 "a
 !a
 a
 a
 a
 3a
 
a
rK   c       
          < \         SV `  4        VR8  d   \        R4      hV'       d   TM\        P                  ! R\        4       4      pV'       d   TMRpV	'       d   T	MRpV\        \        39   pV'       d   V'       d   TM+\        P                  ! R\        P                  ! RR4      4      pV'       g   \        R4      hV	'       d   T	M+\        P                  ! R\        P                  ! R	R4      4      pV'       g   \        R
4      hRpMRp\        \        V\        P                  ! V\        ,          4      VVVVVVVVR7      V n        \        V 4      P                   R\        V 4      P                    2V n        \        V n        Wn        \(        P*                  \,        \.        ,          ,          ! 4       V n        \2        P4                  ! RRV P                  P6                  RV P                  P8                  RV P                  P:                  RV P                  P<                  RV P                  P>                  RV P                  P@                  RV P                  PB                  RV P                  PD                  /R7       R# )aJ  
Initialize a AdaptiveInterruptionDetector instance.

Args:
    threshold (float, optional): The threshold for the interruption detection, defaults to 0.5.
    min_interruption_duration (float, optional): The minimum duration, in seconds, of the interruption event, defaults to 50ms.
    max_audio_duration (float, optional): The maximum audio duration, including the audio prefix, in seconds, for the interruption detection, defaults to 3s.
    audio_prefix_duration (float, optional): The audio prefix duration, in seconds, for the interruption detection, defaults to 0.5s.
    detection_interval (float, optional): The interval between detections, in seconds, for the interruption detection, defaults to 0.1s.
    inference_timeout (float, optional): The timeout for the interruption detection, defaults to 1 second.
    base_url (str, optional): The base URL for the interruption detection, defaults to the shared LIVEKIT_REMOTE_EOT_URL environment variable.
    api_key (str, optional): The API key for the interruption detection, defaults to the LIVEKIT_INFERENCE_API_KEY environment variable.
    api_secret (str, optional): The API secret for the interruption detection, defaults to the LIVEKIT_INFERENCE_API_SECRET environment variable.
    http_session (aiohttp.ClientSession, optional): The HTTP session to use for the interruption detection.
g      @z<max_audio_duration must be less than or equal to 3.0 secondsLIVEKIT_REMOTE_EOT_URL LIVEKIT_INFERENCE_API_KEYLIVEKIT_API_KEYzUapi_key is required, either as argument or set LIVEKIT_API_KEY environmental variableLIVEKIT_INFERENCE_API_SECRETLIVEKIT_API_SECRETz[api_secret is required, either as argument or set LIVEKIT_API_SECRET environmental variableTF)rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   .z*adaptive interruption detector initializedrY   rW   rV   rU   rT   rS   rX   r\   extraN)#superr   
ValueErrorosgetenvr.   r+   r,   rP   SAMPLE_RATEmathceil_FRAMES_PER_SECOND_optsr5   rD   rC   _label_sample_rate_sessionweakrefWeakSetInterruptionHttpStreamInterruptionWebSocketStream_streamsr   inforY   rW   rV   rU   rT   rS   rX   r\   )ru   rS   r   rU   rV   rW   rX   rY   rZ   r[   r   lk_base_url
lk_api_keylk_api_secretis_inference_urlr\   	__class__s   &$$$$$$$$$$     rL   r   %AdaptiveInterruptionDetector.__init__  s   : 	#[\\  35N5PQ 	
 &-'"
+5Z2&+@BW*XX  YY:BIIFWY[<\] 
  k   YY=ryyI]_a?bc 
 ! q  II(#yy!:=O!OP1"71/ $

 d../qd1D1D0EF'$(>A\(\]_8DJJ//$djj&C&C')I)I$djj&C&Cdjj33TZZ11#TZZ%A%ATZZ11		
rK   c                   V ^8  d   QhRR/# rl   rn   r:   rB   )ro   s   "rL   rp   r   q  s     ' 's 'rK   c                	    R # )zadaptive interruptionrB   ru   s   &rL   model"AdaptiveInterruptionDetector.modelp  s    &rK   c                   V ^8  d   QhRR/# r   rB   )ro   s   "rL   rp   r   u  s      # rK   c                	    R # )livekitrB   r   s   &rL   provider%AdaptiveInterruptionDetector.providert  s    rK   c                   V ^8  d   QhRR/# r   rB   )ro   s   "rL   rp   r   y  s      s rK   c                	    V P                   # r   )r   r   s   &rL   r;   "AdaptiveInterruptionDetector.labelx  s    {{rK   c                   V ^8  d   QhRR/# )rl   rn   rQ   rB   )ro   s   "rL   rp   r   }  s     ! !S !rK   c                	    V P                   # r   )r   r   s   &rL   rR   (AdaptiveInterruptionDetector.sample_rate|  s       rK   c               $    V ^8  d   QhRRRRRR/# rl   	api_errorr>   rA   r@   rn   r   rB   )ro   s   "rL   rp   r     s!     
 
Y 
T 
d 
rK   c           	     	V    V P                  R \        V P                  VVR7      4       R# )r?   )r;   r?   rA   N)emitr1   r   ru   r   rA   s   &&&rL   _emit_error(AdaptiveInterruptionDetector._emit_error  s&    		&kk'	
rK   c                   V ^8  d   QhRR/# )rl   rn   zaiohttp.ClientSessionrB   )ro   s   "rL   rp   r     s      !6 rK   c                	r    V P                   '       g   \        P                  ! 4       V n         V P                   # r   )r   r(   r   r   s   &rL   _ensure_session,AdaptiveInterruptionDetector._ensure_session  s%    }}}(557DM}}rK   conn_optionsc                    V ^8  d   QhRRRR/# )rl   r
  r#   rn   z4InterruptionHttpStream | InterruptionWebSocketStreamrB   )ro   s   "rL   rp   r     s      0	=rK   c               	     V P                   P                  '       d   \        WR 7      pM\        WR 7      p V P                  P                  V4       V#   \         d   pT P                  TRR7       h Rp?ii ; i)r   r
  FrA   N)r   r\   r   r   r>   r  r   add)ru   r
  streames   &$  rL   r  #AdaptiveInterruptionDetector.stream  sl    	zz###44[/dV 	&!	  	QE2	s!   A A A A9 A44A9c               $    V ^8  d   QhRRRRRR/# rl   rS   zNotGivenOr[float]r   rn   r   rB   )ro   s   "rL   rp   r     s*       % $5	
 
rK   c               	   \        V4      '       d   WP                  n        \        V4      '       d1   \        P                  ! V\
        ,          4      V P                  n        V P                   F  pVP                  WR 7       K  	  R# ))rS   r   N)	r)   r   rS   r   r   r   rT   r   update_options)ru   rS   r   r  s   &$$ rL   r  +AdaptiveInterruptionDetector.update_options  se     I#,JJ -..$(II.GJ\.\$]DJJ!mmF!!# "  $rK   )r   r   r   r   r   )rC   rD   rE   rF   	THRESHOLDMIN_INTERRUPTION_DURATIONMAX_AUDIO_DURATIONAUDIO_PREFIX_DURATIONDETECTION_INTERVALREMOTE_INFERENCE_TIMEOUTr   propertyr   r   r;   rR   r  r  r!   r  r"   r  rJ   __classcell__r   s   @rL   r   r     s   a
 %a
 ,E	a

 %7a
 (=a
 %7a
 $<a
  $a
 #a
 "&a
 6:a
 a
F ' '     ! !

3N (1 8A	 rK   r   c                     ] tR tRtR R lt]R R l4       tR R ltR R	 ltR
 R lt	R R lt
R R ltR R ltR R ltR R ltR R ltR R lt]R R l4       tR R ltR R lt]P,                  ! ]R 7      R! R" l4       tR#tR$# )%InterruptionStreamBasei  c               $    V ^8  d   QhRRRRRR/# rl   r   r   r
  r#   rn   r   rB   )ro   s   "rL   rp   #InterruptionStreamBase.__annotate__  s)     #` #`4#`DU#`	#`rK   c               	^  a  VS n         VP                  S n        VP                  4       S n        \        P
                  \        ,          ! 4       S n        \        P
                  \        ,          ! 4       S n	        \        \        S P                  P                  S P                  P                  ,          4      \        P                  S P                  P                  R 7      S n        \"        \        \$        3,          ! ^
R7      S n        \        P(                  P+                  S P                  ^4      S n        S P,                  w  S n        p\0        P2                  ! S P5                  V4      RR7      S n        \0        P2                  ! S P9                  4       4      S n        S P:                  P=                  V 3R l4       ^ S n        VS n         S P                  P                  S n!        RS n"        RS n#        RS n$        RS n%        ^ S n&        ^ S n'        \        PP                  ! ^ R7      S n)        \        S P                  PT                  S P                  P                  ,          4      S n+        \        S P                  PX                  S P                  P                  ,          4      S n-        R# ))buffer_sizedtyperR   )maxsizez$InterruptionStreamBase._metrics_task)namec                8   < SP                   P                  4       # r   )	_event_chclose)_ru   s   &rL   <lambda>1InterruptionStreamBase.__init__.<locals>.<lambda>  s    t~~/C/C/ErK   NF)initial)._modelr   r  r   r'   Chanr   	_input_chr   r,  r%   rQ   rU   rR   npint16_audio_bufferr&   r_   _cache	itertoolstee
_tee_aiter_event_aiterasynciocreate_task_metrics_monitor_task_metrics_task
_main_task_taskadd_done_callback_num_retries_conn_optionsr   _overlap_started_at_user_speech_span_agent_speech_started_overlap_started_overlap_count_accumulated_samplesAsyncAtomicCounter_num_requestsrW   _batch_sizerV   _prefix_size)ru   r   r
  monitor_aiters   f$$ rL   r   InterruptionStreamBase.__init__  s    [[
--/";<>"89;-DJJ99DJJ<R<RRS((

..

 "#'="=>rJ--++DNNA>+/??(=$00&&}5<b
 (():;


$$%EF) JJ2215 48+0"&+#$)*! 33A> #DJJ$A$ADJJDZDZ$Z [!$TZZ%E%E

H^H^%^!_rK   c                   V ^8  d   QhRR/# rl   rn   r   rB   )ro   s   "rL   rp   r%    s    %%D%rK   c                	   "   R # 5ir   rB   r   s   &rL   _runInterruptionStreamBase._run  s     "%s   c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   r%    s     ! !$ !rK   c           	     	@  "   V P                   P                  pV P                  V8:  d    V P                  4       G R j  xL
 # R #  L  \         Ed'   pT^ 8X  g   TP
                  '       g   T P                  TRR7       h T P                  T8X  d.   T P                  TRR7       \        RT P                   R24      ThT P                  TRR7       T P                   P                  T P                  4      p\        P                  ! RT R2TRT P                  P                  R	T P                  /R
7       \        P                  ! T4      G R j  xL 
  T ;P                  ^,          un         R p?EK[  R p?i\         d   pT P                  TRR7       h R p?ii ; i5i)NFr  z$failed to detect interruption after z	 attemptsTz+failed to detect interruption, retrying in sr   attempt)exc_infor   )rE  	max_retryrD  rU  r   	retryabler  r   _interval_for_retryr   warningr2  r   r=  sleepr>   )ru   max_retriesr  retry_intervals   &   rL   rA  !InterruptionStreamBase._main_task  sn    ((22;.!YY[(( /( '!#1;;;$$QE$:&&+5$$QE$:,>t?P?P>QQZ[ $$QD$9%)%7%7%K%KDL]L]%^NNNEnEUUVW!"#T[[%7%7%t'8'8 "--777!!Q&!!    6sc   (FA AA FA FE4,C"E4EE4-F4FFFFFc               $    V ^8  d   QhRRRRRR/# r   rB   )ro   s   "rL   rp   r%    s!     8 8Y 8T 8d 8rK   c                	<    V P                   P                  W4       R # r   )r2  r  r  s   &&&rL   r  "InterruptionStreamBase._emit_error  s    	7rK   c                    V ^8  d   QhRRRR/# )rl   framer   rn   r   rB   )ro   s   "rL   rp   r%    s     * * 9 *d *rK   c                |    V P                  4        V P                  4        V P                  P                  V4       R# )z$Push some audio frame to be analyzedN)_check_input_not_ended_check_not_closedr4  send_nowait)ru   rh  s   &&rL   
push_frame!InterruptionStreamBase.push_frame  s,    ##% ""5)rK   c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   r%    s     5 5t 5rK   c                    V P                  4        V P                  4        V P                  P                  \	        4       4       R# )z#Mark the end of the current segmentN)rj  rk  r4  rl  r   r   s   &rL   flushInterruptionStreamBase.flush  s/    ##% "">#34rK   c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   r%  
  s      4 rK   c                Z    V P                  4        V P                  P                  4        R# )z3Mark the end of input, no more audio will be pushedN)rq  r4  r-  r   s   &rL   	end_input InterruptionStreamBase.end_input
  s    

rK   c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   r%    s     + +d +rK   c                  "   V P                   P                  4        \        P                  ! V P                  4      G Rj  xL
  V P
                  P                  4         V P                  G Rj  xL
  V P                  P                  4       G Rj  xL
  R#  LW L* L
  T P                  P                  4       G Rj  xL 
  i ; i5i)zClose the stream immediatelyN)	r4  r-  r'   cancel_and_waitrB  r,  r@  r;  acloser   s   &rL   rz  InterruptionStreamBase.aclose  s     !!$**---	+$$$$//((*** 	. %*$//((***sY   >C BC B /B0B 4CBCB CC<B?=CCc                   V ^8  d   QhRR/# )rl   rn   r   rB   )ro   s   "rL   rp   r%    s     	 	!7 	rK   c                	  "    V P                   P                  4       G R j  xL
 pV#  L  \         dM    T P                  P	                  4       '       g%   T P                  P                  4       ;p'       d   Th\        R hi ; i5ir   )r<  	__anext__StopAsyncIterationrB  	cancelled	exception)ru   valexcs   &  rL   r~   InterruptionStreamBase.__anext__  sr     	/))3355C 
 6! 	/::''))djj6J6J6L/Ls/L	$$.		/s1   B* (* B* *B!B7
BBc                   V ^8  d   QhRR/# )rl   rn   z%AsyncIterator[OverlappingSpeechEvent]rB   )ro   s   "rL   rp   r%  $  s      @ rK   c                	    V # r   rB   r   s   &rL   	__aiter__ InterruptionStreamBase.__aiter__$  s    rK   c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   r%  '  s     M M4 MrK   c                	    V P                   P                  '       d1   \        V 4      p\        VP                   R VP
                   R24      hR# )r   z
 is closedN)r,  closedr5   RuntimeErrorrD   rC   ru   r   s   & rL   rk  (InterruptionStreamBase._check_not_closed'  sA    >>   t*C#..!13<<.
KLL !rK   c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   r%  ,  s     O O OrK   c                	    V P                   P                  '       d1   \        V 4      p\        VP                   R VP
                   R24      hR# )r   z input endedN)r4  r  r5   r  rD   rC   r  s   & rL   rj  -InterruptionStreamBase._check_input_not_ended,  sA    >>   t*C#..!13<<.MNN !rK   c               $    V ^8  d   QhRRRRRR/# )rl   user_speech_spanz
trace.Spanr   r_   rn   r   rB   )ro   s   "rL   rp   r%  2  s$     
 
$
-C
	
rK   c                	   V P                  \        P                  \        VP                  4      P                  4       4       V P                  \        P                  VP                  4       4       V P                  \        P                  VP                  4       4       V P                  \        P                  VP                  4       4       V P                  \        P                  VP                  4       4       R # r   )set_attributer    ATTR_IS_INTERRUPTIONr:   ri   lowerATTR_INTERRUPTION_PROBABILITYr    ATTR_INTERRUPTION_TOTAL_DURATIONrv   %ATTR_INTERRUPTION_PREDICTION_DURATIONrz   !ATTR_INTERRUPTION_DETECTION_DELAYr~   )r  r   s   &&rL   _update_user_speech_span/InterruptionStreamBase._update_user_speech_span1  s     	&&,,c%2G2G.H.N.N.P	
 	&&55u7L7L7N	
 	&&88%:R:R:T	
 	&&==u?\?\?^	
 	&&995;T;T;V	
rK   c                    V ^8  d   QhRRRR/# )rl   	output_chaio.Chan[npt.NDArray[np.int16]]rn   r   rB   )ro   s   "rL   rp   r%  E  s      R R-L RQU RrK   c           	     `  a "   R V 3R llpS P                     Rj  xL
  pT;\        Rc ;e   w    K   ;;\        Rc ;e   w   M ;\        Rc ;e   w   M  M(  V! 4       G Rj  xL
  \	        V\        4      S n        Kh  ;\        Rc ;Ee(   w   S P
                  '       Ed    VP                  S n        VP                  S n
        RS n        ^ S n        S ;P                  ^,          un        S P                  ^8X  dw   \        ^ \        S P                   4      \#        VP$                  S P&                  ,          4      S P(                  ,           ,
          4      pS P                   P+                  V4       \,        P.                  ! RRS P                  /R7       S P0                  P3                  4        EK   ;\4        Rc ;Ee   w    S P                  '       d   S P                  e   \,        P.                  ! R	4       RS n
        S P0                  P7                  R
 4      w  rVVf   \,        P.                  ! R4       \8        P;                  T;'       g    \<        RS P                  VP>                  R7      pS P@                  PC                  4       G Rj  xL
 Vn"        S PG                  V4       RS n        ^ S n        RS n        EK   \H        PJ                  Rc ;e   w   S P
                  '       g   EK  S P                   PM                  V4      pS ;P                  V,          un        S P                  S PN                  8  dI   S P                  '       d4   VPQ                  S P                   PS                  4       4       ^ S n        EKn  EKq  EKt   EKx   ELt EL/ ELED|TPU                  4        R# 5i)zMPreprocess the audio data and forward it to the output channel for inference.c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   :InterruptionStreamBase._forward_data.<locals>.__annotate__H  s     		* 		*D 		*rK   c                   <"   R S n         R S n        ^ S n        ^ S n        S P                  P                  ^ 4      G Rj  xL
  S P                  P                  4        S P                  P                  4        RS n
        R#  LA5i)FN)rH  rI  rJ  rK  rM  setr7  resetr8  clearrG  r   s   rL   _reset_state:InterruptionStreamBase._forward_data.<locals>._reset_stateH  so     ).D&$)D!"#D()D%$$((+++$$&KK%)D"	 ,s   ;BB ABNrB   Tz7overlap speech started, starting interruption inferenceoverlap_countr   z5overlap speech ended, stopping interruption inferencec                L    V P                   R J;'       d    V P                   ^ 8  # r   rs   )r   s   &rL   r/  6InterruptionStreamBase._forward_data.<locals>.<lambda>}  s'     % 4 4D @ ] ]UEYEY\]E] ]rK   z"no request made for overlap speechFr   ri   r   r   )+r4  r   r   r   
isinstancerH  r   r   rF  r   rG  rI  rK  rJ  maxlenr7  rQ   r   r   rO  shiftr   r   r8  r  r   pop_ifr   r   _EMPTY_CACHE_ENTRYr   rM  get_and_resetr   sendr   
AudioFramerm  rN  rl  readr-  )	ru   r  r  input_frame
shift_sizer.  
last_entryevsamples_writtens	   f&       rL   _forward_data$InterruptionStreamBase._forward_dataE  s    		* 		* "& B	6 B	6+%^% &P20225P5N5P5PP&.((1;#%@2D. 42448R8R8R/:/F/FD,-8-L-LD*,0D)01D-''1,' **a/%( 2 23 #K$@$@4CTCT$T U"&"3"3!4&
 **00<LLQ+T-@-@ KK%%'5 56 3022,,,1I1I1U%\]15.(,(:(:)
 &-"LL)MN3DD","B"B0B,1'+'?'?%0%:%:	 E  150B0B0P0P0R*R		",1D)01D-/3D,- 30 ^^%)C)C)C&*&8&8&C&CK&PO--@-00D4D4DDI^I^I^!--d.@.@.E.E.GH451 J_D &{B	6
 )b +Sm "0H 	sm   N.NNNAN.%N&<N.#D4N.A:N.A N.NAN.3A N.=N.NN.N.N.c                    V ^8  d   QhRRRR/# )rl   eventr   rn   r   rB   )ro   s   "rL   rp   r%    s     , ,0 ,T ,rK   c                	    V P                   P                  V4       V P                  P                  VP                  V4       R # r   )r,  rl  r2  r  r5   )ru   r  s   &&rL   r  InterruptionStreamBase.send  s,    ""5)U+rK   r   c                    V ^8  d   QhRRRR/# )rl   event_aiterz%AsyncIterable[OverlappingSpeechEvent]rn   r   rB   )ro   s   "rL   rp   r%    s     ; ;@;	;rK   c                	  "   V  R j  xL
  p\        \        P                  ! 4       VP                  VP                  VP                  VP
                  '       d   ^M^ VP
                  '       g   ^M^ VP                  \        V P                  P                  V P                  P                  R7      R7      pV P                  P                  RV4       K   LDR # 5i)N)
model_namemodel_provider)r9   rd   re   rf   num_interruptionsnum_backchannelsr   metadatametrics_collected)r   rI   rd   re   rf   ri   r   r   r2  r   r   r  )ru   r  r  metricss   &&  rL   r?  ,InterruptionStreamBase._metrics_monitor_task  s      $ 	; 	;")))+!00$&$:$: " 2 2')'9'9'9!q*,*<*<*<!__!#{{00AUAUG KK0':	;s-   CCCCA"C0A&CCC)rK  rH  r7  rN  r8  rE  r<  r,  r4  r@  r2  rM  rD  r   rJ  rI  rF  rO  r   r   rB  r;  rG  N)rC   rD   rE   rF   r   r   rU  rA  r  rm  rq  ru  rz  r~  r  rk  rj  staticmethodr  r  r  r   log_exceptionsr   r?  rJ   rB   rK   rL   r"  r"    s    #`J % %!F8*5
+	M
O
 
 
&Rh, (; );rK   r"  c                  f    ] tR tRt$ ]! RR7      tR]R&   R]R&   R]R	&   ]! R
RR7      tR]R&   Rt	R# )InterruptionResponsei  Tr2   rQ   r`   r@   
is_bargeinr8   re   .r<   znpt.NDArray[np.float32]rh   rB   N)
rC   rD   rE   rF   r   rG   rH   r   rh   rJ   rB   rK   rL   r  r    s/    d;LO-23-EM*ErK   r  c                  ^   a  ] tR tRtR V 3R lltR]R]/R R lltR R	 ltR
 R ltRt	V ;t
# )r   i  c               $    V ^8  d   QhRRRRRR/# r$  rB   )ro   s   "rL   rp   #InterruptionHttpStream.__annotate__  s)     A A4ADUA	ArK   c               	(   < \         SV `  WR 7       R# )r  N)r   r   ru   r   r
  r   s   &$$rL   r   InterruptionHttpStream.__init__  s     	u@rK   rS   r   c               $    V ^8  d   QhRRRRRR/# r  rB   )ro   s   "rL   rp   r    s/     	^ 	^ %	^ $5		^
 
	^rK   c               	    \        V4      '       d   WP                  n        \        V4      '       d3   \        P                  ! V\
        ,          4      V P                  n        R # R # r   )r)   r   rS   r   r   r   rT   ru   rS   r   s   &$$rL   r  %InterruptionHttpStream.update_options  sF     I#,JJ -..$(II.GJ\.\$]DJJ! /rK   c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   r    s     *. *.D *.rK   c                	  a "   R  V 3R llp\         P                  \        P                  \        P
                  ,          ,          ! 4       p\        P                  ! S P                  V4      4      \        P                  ! V! V4      4      .p \        P                  ! V!  G Rj  xL
  \         P                  ! V!  G Rj  xL
  R#  L" L  \         P                  ! T!  G Rj  xL 
  i ; i5i)c                    V ^8  d   QhRRRR/# )rl   input_chr  rn   r   rB   )ro   s   "rL   rp   1InterruptionHttpStream._run.<locals>.__annotate__  s     	2 	2'F 	24 	2rK   c                4  <"   V   R j  xL
  pSP                   ;p e   SP                  '       g   K/  SP                  V4      G R j  xL
 pVP                  p\	        VVVP
                  \        P                  ! 4       V,
          R,          \        P                  ! 4       V,
          VP                  VP                  R7      ;SP                  V&   pVP                  '       g   K  SP                  '       g   K  \        P                  ! R4       SP                  '       d$   SP                  SP                  V4       R Sn        \         P#                  VRV\        P                  ! 4       R7      pSP%                  V4       RSn        EK   EL ELHEDR # 5i)N    eA)r`   rb   re   rd   rf   rh   ri   zuser interruption detectedTr  F)rF  rI  predictr`   r_   re   rI   r   rh   r  r8  ri   r   debugrG  r  r   r   r  )r  r   r   respr`   r   r  ru   s   &      rL   
_send_task/InterruptionHttpStream._run.<locals>._send_task  sR    & 2 2d*.*B*BB&!%!6!6!6 48<<3E-E!__
2H)!%(,(@(@$($8$8$:Z$G3#N$(IIK2D$D"&"4"4$(OO3 J'% (((T-B-B-BLL!=>---55d6L6LeT15./@@#(,#5!%	 A B IIbM,1D)=2 .F 'sP   FFFF$FF	F
BF(F<'F$A*FFFFN)r'   r3  nptNDArrayr5  r6  r=  r>  r  gatherry  )ru   r  data_chtaskss   f   rL   rU  InterruptionHttpStream._run  s     	2 	2B ((3;;rxx013 2 27 ;<
7 34
	...%(((%%u--- )-#%%u---sN   B
C.C %C&C *C.C
C.C 
C.C+$C'%C++C.c                    V ^8  d   QhRRRR/# )rl   waveformz
np.ndarrayrn   r  rB   )ro   s   "rL   rp   r    s     8P 8Pj 8P5I 8PrK   c                	2  "   \        4       p V P                  P                  V P                  P                   R V P                  P
                   R\        V P                  P                  4       R\        V4       2RRRR\        V P                  P                  V P                  P                  4       2/VP                  4       \        P                  ! V P                  P                  R7      R7      ;_uu_4       GR	j  xL
 p VP                  4        VP!                  4       G R	j  xL
 p\"        P%                  VR
\&        P                   ! 4       V,
          R,          R\(        P*                  ! VP-                  R. 4      \(        P.                  R7      /,          4      p\0        P2                  ! RRVRVP4                  R
VP6                  /R7       TuuR	R	R	4      GR	j  xL
  #  L L L	  \8         dy   pTP;                  4       G R	j  xL 
 p\=        T\        P>                  4      '       d   TP@                  MTP@                  p\C        RT 2TTTR8X  d	   RR7      ThR	R7      ThR	p?ii ; i  + GR	j  xL 
 '       g   i     R	# ; i  \D        PF                   d   p\C        RT 2RRR7      ThR	p?i\        PH                   d   p\K        RT 24      ThR	p?i\L         d   pThR	p?i\8         d   p\M        RT 24      ThR	p?ii ; i5i)z/bargein?threshold=z&min_frames=z&created_at=zContent-Typezapplication/octet-streamAuthorizationBearer )total)urlheadersr   timeoutNre   r  rh   r(  interruption inference doner`   ri   r   z&error during interruption prediction:   F)bodystatus_coder]  z interruption inference timeout:   r  r]  z)interruption inference connection error: )'r   r   postr   rY   rS   rQ   rT   r-   rZ   r[   tobytesaiohttpClientTimeoutrX   raise_for_statusjsonr  model_validaterI   r5  arraygetfloat32r   r   r  re   r>   textr  ClientResponseErrorstatusr   r=  TimeoutErrorClientErrorr   r   )	ru   r  r`   r  r   resultr  msgr  s	   &&       rL   r  InterruptionHttpStream.predict  s    $&
6	P}}))zz**++>tzz?S?S>TT`adeieoeoezeza{`|  }I  JM  NX  JY  IZ  ["$>#w/B4::CUCUW[WaWaWlWl/m.n%o %%'--DJJ4P4PQ *     ))+15+<D1@@1D4H4H4JZ4W[^3^+RXX $" =RZZ.F LL5(*-v/@/@163M3M "=   ,=> ! 
 $		++C$.q'2M2M$N$NTXT_T_   )@D $/+6#+=%	
  DH	
 
?   T ## 	 21#6 	
 "" 	]$'PQRPS%TU[\\ 	G 	PCA3GHaO	Ps   LC4J G+J I7#G1+G-,B,G1J %G/&J *L+J -G1/J 1I4<I/H
AI//I44I77J	=J >
J		J	J LJ L)J;;LLK""L/L0K22L?L LLL)rI  rG  )rC   rD   rE   rF   r   r"   r  rU  r  rJ   r  r   s   @rL   r   r     s>    A A
	^ (1	^ 8A		^*.X8P 8PrK   r   c                  2    ] tR tRtRtRtRtRtRtRt	Rt
R	tR
# )InterruptionWSMessageTypei;  zsession.createzsession.closezsession.createdzsession.closedbargein_detectedinference_doner?   rB   N)rC   rD   rE   rF   SESSION_CREATESESSION_CLOSESESSION_CREATEDSESSION_CLOSEDINTERRUPTION_DETECTEDINFERENCE_DONEERRORrJ   rB   rK   rL   r  r  ;  s&    %N#M'O%N.%NErK   r  c                  :    ] tR tRt$ ]P
                  tR]R&   RtR# )#InterruptionWSSessionCreatedMessageiE  z2Literal[InterruptionWSMessageType.SESSION_CREATED]r5   rB   N)	rC   rD   rE   rF   r  r  r5   rH   rJ   rB   rK   rL   r  r  E  s    !11 	
< rK   r  c                  J    ] tR tRt$ R]R&   R]R&   R]R&   R]R&   R]R	&   R
tR# )#InterruptionWSSessionCreateSettingsiK  rQ   rR   num_channelsr8   rS   rT   zLiteral['s16le']encodingrB   Nr]   rB   rK   rL   r  r  K  s    OrK   r  c                  D    ] tR tRt$ ]P
                  tR]R&   R]R&   RtR# )"InterruptionWSSessionCreateMessageiS  z1Literal[InterruptionWSMessageType.SESSION_CREATE]r5   r  settingsrB   N)	rC   rD   rE   rF   r  r  r5   rH   rJ   rB   rK   rL   r#  r#  S  s     !00 	
;  21rK   r#  c                  :    ] tR tRt$ ]P
                  tR]R&   RtR# )!InterruptionWSSessionCloseMessageiZ  z0Literal[InterruptionWSMessageType.SESSION_CLOSE]r5   rB   N)	rC   rD   rE   rF   r  r  r5   rH   rJ   rB   rK   rL   r&  r&  Z  s    =V=d=dD
:drK   r&  c                  :    ] tR tRt$ ]P
                  tR]R&   RtR# )"InterruptionWSSessionClosedMessagei^  z1Literal[InterruptionWSMessageType.SESSION_CLOSED]r5   rB   N)	rC   rD   rE   rF   r  r  r5   rH   rJ   rB   rK   rL   r(  r(  ^  s    !00 	
; rK   r(  c                  |    ] tR tRt$ ]P
                  tR]R&   R]R&   ]! RR7      t	R]R	&   ]! ]
R
7      tR]R&   RtR# )InterruptionWSDetectedMessageid  z8Literal[InterruptionWSMessageType.INTERRUPTION_DETECTED]r5   rQ   r`   r   rm   r8   re   r6   list[float]rh   rB   N)rC   rD   rE   rF   r  r  r5   rH   r   re   listrh   rJ   rB   rK   rL   r*  r*  d  s>    !77 	
B  O!&s!33!&t!<M;<rK   r*  c                  |    ] tR tRt$ ]P
                  tR]R&   R]R&   ]! RR7      t	R]R	&   ]! ]
R
7      tR]R&   RtR# )"InterruptionWSInferenceDoneMessageim  z1Literal[InterruptionWSMessageType.INFERENCE_DONE]r5   rQ   r`   r   r+  r8   re   r6   r,  rh   rB   N)rC   rD   rE   rF   r  r  r5   rH   r   re   r-  rh   rJ   rB   rK   rL   r/  r/  m  s>    !00 	
;  O!&s!33!&t!<M;<rK   r/  c                  X    ] tR tRt$ ]P
                  tR]R&   R]R&   R]R&   R]R&   R	tR
# )InterruptionWSErrorMessageiv  z(Literal[InterruptionWSMessageType.ERROR]r5   r:   messagerQ   code
session_idrB   N)	rC   rD   rE   rF   r  r  r5   rH   rJ   rB   rK   rL   r1  r1  v  s"    5N5T5TD
2TL
IOrK   r1  AnyInterruptionWSMessager5   )discriminatorz%TypeAdapter[AnyInterruptionWSMessage]InterruptionWSMessagec                  ^   a  ] tR tRtR V 3R lltR]R]/R R lltR R	 ltR
 R ltRt	V ;t
# )r   i  c               $    V ^8  d   QhRRRRRR/# r$  rB   )ro   s   "rL   rp   (InterruptionWebSocketStream.__annotate__  s$     0 040DU0	0rK   c               	   < \         SV `  WR 7       \        \        R4      4      V n        \
        P                  ! 4       V n        R# )r  interruption_request_N)r   r   r:   r*   _request_idr=  Event_reconnect_eventr  s   &$$rL   r   $InterruptionWebSocketStream.__init__  s6     	u@y)@AB 'rK   rS   r   c               $    V ^8  d   QhRRRRRR/# r  rB   )ro   s   "rL   rp   r:    s*     
$ 
$ %
$ $5	
$
 

$rK   c               	    \        V4      '       d   WP                  n        \        V4      '       d1   \        P                  ! V\
        ,          4      V P                  n        V P                  P                  4        R # r   )	r)   r   rS   r   r   r   rT   r?  r  r  s   &$$rL   r  *InterruptionWebSocketStream.update_options  sS     I#,JJ -..$(II.GJ\.\$]DJJ!!!#rK   c                   V ^8  d   QhRR/# rS  rB   )ro   s   "rL   rp   r:    s     v% v%D v%rK   c                	  a a"   R oR VV 3R llpR VV 3R llpRp \         P                  \        P                  \        P
                  ,          ,          ! 4       p R oS P                  4       G Rj  xL
 p\        P                  ! S P                  V4      4      \        P                  ! V! W44      4      \        P                  ! V! V4      4      .p\        P                  ! V!  p\        P                  ! S P                  P                  4       4      p \        P                  ! Wg3\        P                  R7      G Rj  xL
 w  rV F  p
W8w  g   K  V
P                  4        K  	  Wx9  d    RoVe-   VP                  '       g   VP!                  4       G Rj  xL
  Rp\         P"                  ! . VOVN5!  G Rj  xL
  VP%                  4         VP'                  4        RoVe/   VP                  '       g   VP!                  4       G Rj  xL
  R# R# R# S P                  P+                  4        RoVe-   VP                  '       g   VP!                  4       G Rj  xL
  Rp\         P"                  ! . VOVN5!  G Rj  xL
  VP%                  4         VP'                  4        RoTe1   TP                  '       g   TP!                  4       G Rj  xL
  EK  EK  EK   ELn EL ELL EL,  \        P(                   d     EL!i ; i L L L  \        P(                   d     L~i ; i  RoTe.   TP                  '       g   TP!                  4       G Rj  xL 
  Rp\         P"                  ! . TOTN5!  G Rj  xL 
  TP%                  4         TP'                  4        i   \        P(                   d     i i ; i; i L  RoTe.   TP                  '       g   TP!                  4       G Rj  xL 
  i i i ; i5i)Fc               $    V ^8  d   QhRRRRRR/# )rl   wsaiohttp.ClientWebSocketResponser  r  rn   r   rB   )ro   s   "rL   rp   6InterruptionWebSocketStream._run.<locals>.__annotate__  s$     !	5 !	5/!	5;Z!	5!	5rK   c                $  <"   \        SP                  P                  R ,          4      pV  Rj  xL
  p\        4       pSP                  P                  4        FV  w  rVVP                  e   K  WFP                  ,
          V8  d+   \        RWFP                  ,
          R ,          R R2RRR7      h M	  SP                  P                  4       G Rj  xL
  \        4       p\        P                  ! RV4      pV P                  WP                  4       ,           4      G Rj  xL
  \        VVR	7      SP                  V&   EK   EL Lr L&EDR
o
\!        \"        P$                  R7      p	T P'                  T	P)                  4       4      G Rj  xL 
  R# 5i)r  Nz'interruption inference timed out after z.1fzs (ws)r  Fr  z<Q)r`   rb   T)r5   )rQ   r   rX   r   r8  itemsrd   r`   r   rM  	incrementstructpack
send_bytesr   r_   r&  r  r  send_strmodel_dump_json)rG  r  
timeout_ns
audio_datanow_keyr   r`   headerr  
closing_wsru   s   &&        rL   	send_task3InterruptionWebSocketStream._run.<locals>.send_task  s_     TZZ99C?@J$,  j%'#';;#4#4#6KD++7 ---
:,E #&6&6 6#=cB&J(+&+	   $7 ((22444,.
T:6mmF-?-?-A$ABBB*@)!++J'% 5 C# %-. J3.<<C ++c113444sX   )FEEEBFEAFE !FEFFA FF	Fc                    V ^8  d   QhRRRR/# )rl   rG  rH  rn   r   rB   )ro   s   "rL   rp   rI    s     c	 c	 ? c	D c	rK   c                	  <"    V P                  4       G Rj  xL
 pVP                  \        P                  P                  \        P                  P
                  \        P                  P                  39   dr   S'       g   S	P                  P                  '       d   R# \        RVP                   2V P                  ;'       g    RRVP                  : RVP                  : 2R7      hVP                  \        P                  P                  8w  d%   \        P                  ! RVP                  4       EK3  \         P"                  ! VP                  4      p\$        P'                  V4      pT;;\(        Rc ;e   w   M ;\*        Rc ;e   w   M  M  EK  ;\,        Rc ;Ee   w    VP.                  pS	P0                  ;p Ee   S	P2                  '       Ed   S	P4                  P7                  VV3R l\9        4       V,
          R	,          \:        P<                  ! VP>                  \:        P@                  R
7      R VPB                  \D        PD                  ! 4       V,
          R7      pS	PF                  '       d$   S	PI                  S	PF                  V4       RS	n#        \        PJ                  ! RRVPM                  4       RVPO                  4       RVPQ                  4       RVPS                  4       /R7       \T        PW                  VR V\D        PD                  ! 4       R7      pS	PX                  P[                  4       G Rj  xL
 Vn.        S	P_                  V4       RS	n        EK[  EK^  EKa   ;\`        Rc ;Ee   w    VP.                  pS	P0                  ;p e   S	P2                  '       d   S	P4                  P7                  VV3R l\9        4       V,
          R	,          VPB                  \:        P<                  ! VP>                  \:        P@                  R
7      R\D        PD                  ! 4       V,
          R7      p\        Pb                  ! RRVPM                  4       RVPO                  4       RVPS                  4       /R7       EKz  EK}  EK   \d        Rc ;e2   w   \        RVPf                   2VPh                  VPf                  R7      h  \        P                  ! RV4       EK   EL EL5i)TNz>LiveKit Adaptive Interruption connection closed unexpectedly: zws_msg.data=z ws_msg.extra=)r2  r  r  z8unexpected LiveKit Adaptive Interruption message type %srB   c                    \        V R 7      # r   r_   cs   &rL   r/  EInterruptionWebSocketStream._run.<locals>.recv_task.<locals>.<lambda>      5KWX5YrK   r  r  )rd   rh   ri   re   rf   zinterruption detectedrd   re   rf   r   r   r  Fc                    \        V R 7      # r]  r^  r_  s   &rL   r/  ra    rb  rK   )rd   re   rh   ri   rf   r  z.LiveKit Adaptive Interruption returned error: )r  r  zBreceived unexpected message from LiveKit Adaptive Interruption: %s)5receiver5   r  	WSMsgTypeCLOSEDCLOSECLOSINGr   r  r   r   
close_coder   TEXTr   r_  r  loadsr7  validate_pythonr  r(  r*  r`   rF  rI  r8  set_or_updater   r5  r  rh   r  re   rI   rG  r  r  rv   rz   r~   r   r   r   rM  r  r   r  r/  r   r1  r3  r2  )
rG  ws_msgr   r  r`   r   r   r  rW  ru   s
   &       rL   	recv_task3InterruptionWebSocketStream._run.<locals>.recv_task  s     !zz|+;;%%,,%%++%%--# 
 "T]]%9%9%9("`agalal`m n$&MM$7$7R ,~_v||o>  ;;'"3"3"8"88NNRTZT_T_ zz&++.0E0U0UVZ0[d=;==@d@b@d@dd8688%(^^
262J2JJ.!%&*.*?*?*?$(KK$=$= *)3 Y0?0AJ0NRU/U.0hhs7H7HPRPZPZ.[04474K4K04		>P0P %> %E  $555 $ = =d>T>TV[ \9= 6"LL 7$4e6N6N6P$95;X;X;Z$5u7P7P7R$153H3H3J	'" "8!H!H&+04+=)-	 "I "B 594F4F4T4T4V.VBO IIbM49D1= +@& 9F >;==%(^^
262J2JJ.!%&*.*?*?*?$(KK$=$= *)3 Y0?0AJ0NRU/U474K4K.0hhs7H7HPRPZPZ.[0504		>P0P %> %E #LL =$4e6N6N6P$95;X;X;Z$153H3H3J'"  +@& >, 45,LSXXJW!$(+  6 ` y ,x /WsD   S#SA'S#S#'S#DS#E$S#:S ;A)S#%D9S# S#NT)return_when)r'   r3  r  r  r5  r6  _connect_wsr=  r>  r  r  r?  waitFIRST_COMPLETEDr  r  r-  gracefully_cancelcancelr  CancelledErrorr  )ru   rX  rp  rG  r  r  tasks_groupwait_reconnect_taskdoner.  taskrW  s   f          @rL   rU   InterruptionWebSocketStream._run  sR    
!	5 !	5Fc	 c	J 6:hhs{{288457G'%"
++--''(:(:7(CD''	"(>?''	"6
 &nne4&-&9&9$:O:O:T:T:V&W#$+LL$:$+$;$;% GD
 !%6 KKM !% +6 "&J~biii hhj((!//LL8KLLL&&(#--/ "
>")))((*$$ +4> ))//1!%J~biii hhj((!//LL8KLLL&&(#--/ "
>")))((*$$ +4>I .  )L #11 
 % )L #11  "&J~biii hhj((!//LL8KLLL&&(#--/"11 
 % "
>")))((*$$ +4>s  AO:O 3K!4B"O *L* K$L* L* 0O O K'"O >K*?O K- $O:<O:L	
O:L* 4O O L "O LO L (O: O:O O:!O $L* 'O *O -LO LO 	O:O O L'$O &L''O *N=N=M#N=:M=;N=N"!N="N:7N=9N::N==O  O:O7O7.O1/O77O:c                   V ^8  d   QhRR/# )rl   rn   rH  rB   )ro   s   "rL   rp   r:  Y  s     4 4#B 4rK   c                  "   \        V P                  P                  ^V P                  P                  V P                  P                  P
                  RR7      pV P                  P                  pVP                  R4      '       d   VP                  RR^4      p/ \        4       CRR\        V P                  P                  V P                  P                  4       2/Cp \        P                  ! V P                  P!                  V R2VR7      V P"                  P$                  4      G R	j  xL
 p \7        \8        P:                  TR7      pTP=                  TP?                  4       4      G R	j  xL
  T#  LI  \&        P(                  \        P*                  \&        P,                  3 d   p\/        T\&        P,                  4      '       d+   TP0                  R
8X  d   \3        RTP0                  RR7      Th\/        T\        P*                  4      '       d   \5        RRR7      Th\5        R4      ThR	p?ii ; i L  \@         d+   pTPC                  4       G R	j  xL 
  \5        R4      ThR	p?ii ; i5i)z7Connect to the LiveKit Adaptive Interruption WebSocket.s16le)rR   r   rS   rT   r!  httprG  r  r  z/bargein)r  Nr  z,LiveKit Adaptive Interruption quota exceededFr  z;failed to connect to LiveKit Adaptive Interruption: timeout)r]  z2failed to connect to LiveKit Adaptive Interruption)r5   r$  zFfailed to send session.create message to LiveKit Adaptive Interruption)zhttp://zhttps://)"r  r   rR   rS   r2  rT   rY   
startswithreplacer/   r-   rZ   r[   r=  wait_forr   
ws_connectrE  r  r  ClientConnectorErrorr  r
  r  r  r   r   r#  r  r  rP  rQ  r>   r-  )ru   r$  rY   r  rG  r  r  s   &      rL   rs  'InterruptionWebSocketStream._connect_wsY  s)    6

..jj**{{((33
 ::&&677''a8H
#%
w':4::;M;MtzzOdOd'e&fg
	b''((H:X)>(P""** B,
	4.==!C ++c113444 	E
 ((  ''
 	b
 !W8899ahh#o$B !# 	
 Aw3344(Q#  %%YZ`aa!	b. 5 	((*$X	sz   CI1AE7 +E5,E7 1=H9 .H7/H9 3I15E7 74H4+BH//H44I17H9 9I.I)II))I..I1)rI  r?  r=  rG  )rC   rD   rE   rF   r   r"   r  rU  rs  rJ   r  r   s   @rL   r   r     s8    0 0
$ (1
$ 8A	
$v%p4 4rK   r   c               $    V ^8  d   QhRRRRRR/# )rl   rh   rg   window_sizer8   rn   rB   )ro   s   "rL   rp   rp     s$     < <1<@E<
<rK   c                    V f   R# \         P                  ! VR,          4      p\        V 4      V8  d   R# \        \        P
                  ! W) 4      V) ,          4      # )z
Estimate the probability of the interruption event based on the probabilities of the frames.
The estimated probability is the maximum of the minimum of every window_size consecutive frames.
r   g?)r   r   r  r8   r5  	partition)rh   r  n_ths   && rL   r   r     sO     99[5()D
=D  mU3TE:;;rK   g?)r   r?   r  )q__conditional_annotations__
__future__r   r=  r  r   r   rM  rI   r   abcr   r   collections.abcr   r   dataclassesr   r	   enumr
   r   typingr   r   r   r   r  numpyr5  numpy.typingr  opentelemetryr   pydanticr   r   r   r   r   r   r   r   r   r   _exceptionsr   r   r   logr   metrics.baser   r   	telemetryr    typesr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   _utilsr+   r,   r-   r.   r/   r   r  r  r  r  r  r  r   r1   rP   r_   r   r  r   r   r   r   r   r  r   rH   EventEmitterr   r"  r  r   r:   r  r  r  r#  r&  r(  r*  r/  r1  r5  r7  r   r   )r  s   @rL   <module>r     s   " "    	    # 8 (    5 5        F F  8 # Y Y   	%        t$A A %A, t$
 
 %
>S
Y S
n ,q9 	 		 		& 	&" "
	 	 NN!"  $$ "	"
  9 h!	
hV;S ;JF9 FuP3 uP~T ) ) 2 2e	 e =I == =  ')*'( )) $	$
 )) !! )  @K&F(CCD@ < @"8 @N Ib< <rK   