
    /j~H                   ,   S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJrJ	r	J
r
  S SKJr  S SKJrJr  S SKJrJr  S SKJr  S SKJr  S S	KJr  S
SKJrJrJrJrJrJ r   S
SK!J"r"  S
SK#J$r$J%r%J&r&  S
SK'J(r(  S
SK)J*r*J+r+J,r,J-r-J.r.J/r/  S
SK0J1r1J2r2J3r3Jr4  S
SK5J6r6  S
SK7J8r8J9r9J:r:  S
SK;J<r<  SSK=J>r>  SSK?J@r@JArAJBrBJCrC  SSKDJErEJFrFJGrGJHrHJIrI  SSKJJKrK  SSKLJMrMJNrNJOrOJPrPJQrQJRrRJSrS  SSKTJUrUJVrVJWrWJXrXJYrYJZrZJ[r[J\r\J]r]J^r^J_r_J`r`  SSKaJbrbJcrcJdrd  SSKeJfrfJgrg  \(       a  S
SKJhrh  SSKiJjrj  \R                  S   " S 5      rl\R                  S!   " S"5      rm\ " S# S$5      5       rn\R                  S$   " S%5      ro\ " S& S'5      5       rp\ " S( S)5      5       rq " S* S\F5      rrg)+    )annotationsN)AsyncIterable	CoroutineSequence)	dataclass)TYPE_CHECKINGAny)contexttrace)rtc)MessageGeneration)Metadata   )	inferencellmsttttsutilsvad)Instructions)StopResponseToolFlagget_fnc_tool_nameslogger)
EOUMetrics
LLMMetricsRealtimeModelMetrics
STTMetrics
TTSMetrics
VADMetrics)otel_metricstrace_typestracerr   )split_words)	NOT_GIVENFlushSentinel
NotGivenOr)is_given   )_set_participant_attributes)AgentModelSettings_get_activity_task_info_set_activity_task_info)AudioRecognitionRecognitionHooks_EndOfTurnInfo_PreemptiveGenerationInfo_STTPipeline)create_endpointing)AgentFalseInterruptionEvent
ErrorEventFunctionToolsExecutedEventMetricsCollectedEventSessionUsageUpdatedEventSpeechCreatedEventUserInputTranscribedEvent)ToolExecutionOutput_AudioOutput_TextOutput_TTSGenerationDataapply_instructions_modalityperform_audio_forwardingperform_llm_inferenceperform_text_forwardingperform_tool_executionsperform_tts_inferenceremove_instructionsupdate_instructions)DEFAULT_INPUT_DETAILSInputDetailsSpeechHandle)EndpointingOptionsTurnDetectionMode)mcp)AgentSessionAgentActivityagents_activityrK   agents_speech_handlec                  *    \ rS rSr% S\S'   S\S'   Srg)_OnEnterDataX   rO   sessionr,   agent N__name__
__module____qualname____firstlineno____annotations____static_attributes__rX       T/app/agent/.venv/lib/python3.13/site-packages/livekit/agents/voice/agent_activity.pyrT   rT   X   s    Lr`   rT   agents_activity_on_enterc                  <    \ rS rSr% SrS\S'   SrS\S'   S	S jrSrg)
_ReusableResourcesa   Nz_STTPipeline | Nonestt_pipelinellm.RealtimeSession | None
rt_sessionc                  #    / nU R                   b0  UR                  U R                   R                  5       5        S U l         U R                  b0  UR                  U R                  R                  5       5        S U l        U(       aT  [        R
                  " USS06I S h  vN nU H/  n[        U[        5      (       d  M  [        R                  " SUS9  M1     g g  N;7f)Nreturn_exceptionsTz$error cleaning up reusable resources)exc_info)
rf   appendacloserh   asynciogather
isinstance	Exceptionr   error)selftasksoutputsoutputs       ra   cleanup_ReusableResources.cleanupf   s     (LL**1134 $D??&LL//12"DO#NNEJTJJG!fi00LL!GRXY " Js   BCC C C)rh   rf   returnNone)	rZ   r[   r\   r]   rf   r^   rh   rw   r_   rX   r`   ra   rd   rd   a   s    (,L%,-1J*1Zr`   rd   c                  \    \ rS rSr% S\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'   S\S'   Srg)_PreemptiveGenerationv   rK   speech_handlellm.ChatMessageuser_messager3   infollm.ChatContextchat_ctxlist[llm.Tool | llm.Toolset]toolszllm.ToolChoice | Nonetool_choicefloat
created_atrX   NrY   rX   r`   ra   r}   r}   v   s,    !!
##''&&r`   r}   c            	      d   \ rS rSrSjS jr    SkS jr\SlS j5       r\SmS j5       r\SnS j5       r	\SlS j5       r
\SoS j5       r\SlS	 j5       r\SpS
 j5       r\SqS j5       r\SqS j5       r\SrS j5       r\SsS j5       r\  StS j5       r\SqS j5       r\SlS j5       rSuS jrSvS jrSS.     SwS jjr\\\\\S.           SxS jjrSSS.       SyS jjrSS.SzS jjrS{S jrSS.SzS jjr\R>                  " S 5      SS!.   S|S" jj5       r SS#.   S}S$ jjr!S~S% jr"SS.SzS& jjr#S~S' jr$SS!.     SS( jjr%S~S) jr&S~S* jr'SS+ jr(SS, jr)\\SS-.         SS. jjr*\\\\\\S\+S/.                 SS0 jjr,S~S1 jr-S~S2 jr.S~S3 jr/SSS5 jjr0S4S6.SS7 jjr1S~S8 jr2S4S9.       SS: jjr3SSS; jjr4\5Rl                  " \7S<9S~S= j5       r8S~S> jr9    SS? jr:SS@ jr;    SSA jr<SSB jr=SSC jr>SSD jr?SSE jr@SSF jrASSG.   SSH jjrBSSI jrCSSJ jrDSSK jrESSL jrFSSM jrGSSN.SSO jjrHSSP jrISSQ jrJ\5Rl                  " \7S<9      SSR j5       rKSSS jrLSST jrM\5Rl                  " \7S<9            SSU j5       rN            SSV jrO\5Rl                  " \7S<9SSSSSW.                 SSX jj5       rPSSSSSW.                 SSY jjrQ\5Rl                  " \7S<9SSSS4SSZ.               SS[ jj5       rR\5Rl                  " \7S<9SS\.         SS] jj5       rSSS\.         SS^ jjrTSS_ jrU SSS`.     SSa jjjrVS~Sb jrWS~Sc jrX\SSd j5       rYSSe jrZ\SSf j5       r[\SSg j5       r\\SSh j5       r]Sir^g)rP      c                    XsU l         U l        S U l        S U l        S U l        [
        R                  " 5       U l        S U l        SU l	        SU l
        SU l        SU l        S U l        / U l        [
        R                  " 5       U l        U R                   R#                  5         S U l        S U l        S U l        SU l        [
        R                  " 5       U l        S U l        S U l        / U l        S U l        SU l        [
        R                  " 5       U l        U R8                  R#                  5         / U l        / U l        S U l        S U l         [C        U RD                  [D        RF                  5      (       aA  U RD                  RH                  RJ                  (       a  U RL                  (       d  [O        S5      e[Q        U R                   RJ                  5      (       a  U R                   RJ                  OU R                  RJ                  nU RS                  U5      U l*        U RW                  5       U l,        U RX                  S LU l-        SU l.        U RT                  S;  U l/        U R^                  U l0        [#        5       U l1        g )NFTr   zthe RealtimeModel uses a server-side turn detection, allow_interruptions cannot be False, disable turn_detection in the RealtimeModel and use VAD on the AgentSession insteadmanualrealtime_llm)2_agent_session_rt_session_realtime_spans_audio_recognitionrn   Lock_lock_tool_choice_started_closed_scheduling_paused_new_turns_blocked_current_speech	_speech_qEvent_user_silence_eventset_paused_speech_false_interruption_timer_cancel_speech_pause_task_stt_eos_received
_q_updated_scheduling_atask_user_turn_completed_atask_speech_tasks_preemptive_generation_preemptive_generation_count_authorization_allowed_drain_blocked_tasks
_mcp_tools_on_enter_task_on_exit_taskrp   r   RealtimeModelcapabilitiesturn_detectionallow_interruptions
ValueErrorr)   _validate_turn_detection_turn_detection_resolve_interruption_detection_interruption_detector_interruption_detection_enabled_interruption_detected'_interruption_by_audio_activity_enabled/_default_interruption_by_audio_activity_enabled_background_speeches)rs   rW   sessr   s       ra   __init__AgentActivity.__init__   s&   %*"T]7;JN;?\\^
37"&"'48@B29--/   $$& 48EI&DH&', "--/<@EI'68DH#12)&-mmo###'')=?!023726 txx!2!233%%44,,L  2233 KK&&-- 	
  $<<^L 002 	# 6:5P5PX\5\,,1# >B=Q=Q Z
 >
4
 88 	<
 8;u!r`   c                .   Ub  [        U[        5      (       d  U$ [        U[        5      (       a  UOS nU R                  nU R                  nU R                  nUS:X  a  U(       d  [
        R                  " S5        S nUS:X  a  U(       d  [
        R                  " S5        S n[        U[        R                  5      (       a  US:X  a3  UR                  R                  (       d  [
        R                  " S5        S nUS:X  a  [
        R                  " S5        S nODU(       a=  US:w  a7  UR                  R                  (       a  [
        R                  " SU S	35        S nUR                  R                  (       d  U(       a  Uc  SnOUS:X  a  [
        R                  " S
5        S nU(       dk  U(       ad  UR                  R                  (       dI  [        U[        R                  5      (       a*  U R                  (       a  Uc  [
        R                  " S5        U$ )Nr   ztturn_detection is set to 'vad', but no VAD model is provided. Pass a VAD instance, e.g. Agent(vad=silero.VAD.load())r   zrturn_detection is set to 'stt', but no STT model is provided. Pass an STT instance, e.g. Agent(stt=deepgram.STT())r   zturn_detection is set to 'realtime_llm', but the LLM is not a RealtimeModel or the server-side turn detection is not supported/enabled, ignoring the turn_detection settingzcturn_detection is set to 'stt', but the LLM is a RealtimeModel, ignoring the turn_detection settingzturn_detection is set to 'zm', but the LLM is a RealtimeModel and server-side turn detection enabled, ignoring the turn_detection settingzKturn_detection is set to 'realtime_llm', but the LLM is not a RealtimeModelz{VAD is not set. Enabling VAD is recommended when using LLM and non-streaming STT for more responsive interruption handling.)rp   strr   r   r   r   warningr   r   r   	streamingLLMr   )rs   r   mode	vad_model	stt_model	llm_models         ra   r   &AgentActivity._validate_turn_detection   s    %j.M.M!!!+NC!@!@~dHH	HH	HH	5=NNI D5=NNG Di!2!233~%i.D.D.S.S:
 u}: $.0Y5K5K5Z5Z0 7: :
  ))88Y4<^#NN] D **449cgg..((NN=
 r`   c                    U R                   $ N)r   rs   s    ra   scheduling_pausedAgentActivity.scheduling_paused   s    &&&r`   c                    U R                   $ r   )r   r   s    ra   rV   AgentActivity.session$  s    }}r`   c                    U R                   $ r   )r   r   s    ra   rW   AgentActivity.agent(  s    {{r`   c                    U R                   $ r   )r   r   s    ra   interruption_enabled"AgentActivity.interruption_enabled,  s    333r`   c                    [        U R                  R                  5      (       a  U R                  R                  $ U R                  R                  $ r   )r)   r   mcp_serversr   r   s    ra   r   AgentActivity.mcp_servers0  sA     //00 KK##	
 **	
r`   c                    [        U R                  R                  5      (       a  U R                  R                  $ U R                  R                  R
                  S   $ )Nenabled)r)   r   r   r   optionsinterruptionr   s    ra   r   !AgentActivity.allow_interruptions8  sL     7788 KK++	
 &&33I>	
r`   c           	        U R                   R                  R                  S0 5      nU R                  R                  R
                  S   n[        UR                  SUS   5      UR                  SUS   5      UR                  SUS   5      S9$ )Nendpointingr   	min_delay	max_delayr   r   r   )r   _turn_handlinggetrV   _optsturn_handlingrL   )rs   agent_endpointingsession_endpointings      ra   endpointing_optsAgentActivity.endpointing_opts@  s     !KK66::="M"ll00>>}M!"&&v/B6/JK'++K9L[9YZ'++K9L[9YZ
 	
r`   c                     U R                   S   $ )Nr   r   r   s    ra   min_endpointing_delay#AgentActivity.min_endpointing_delayK       $$[11r`   c                     U R                   S   $ )Nr   r   r   s    ra   max_endpointing_delay#AgentActivity.max_endpointing_delayP  r   r`   c                    U R                   $ r   )r   r   s    ra   realtime_llm_session"AgentActivity.realtime_llm_sessionU  s    r`   c                    U R                   $ r   )r   r   s    ra   current_speechAgentActivity.current_speechY  s    ###r`   c                v    U R                   R                  U R                  R                  -   U R                  -   $ r   )r   r   r   r   r   s    ra   r   AgentActivity.tools]  s,     }}""T[[%6%66HHr`   c                    [        U R                  R                  5      (       a  U R                  R                  $ U R                  R                  R                  $ r   )r)   r   min_consecutive_speech_delayr   r   r   s    ra   r   *AgentActivity.min_consecutive_speech_delayc  sG     @@AA KK44	
 &&CC	
r`   c                    [        U R                  R                  5      (       a  U R                  R                  OU R                  R                  R                  nUSL $ )NT)r)   r   use_tts_aligned_transcriptr   r   )rs   use_aligned_transcripts     ra   r   (AgentActivity.use_tts_aligned_transcriptk  sM     >>?? KK22&&AA 	 &--r`   c                l  #    XR                   l        [        R                  " UU R                   R                  S9nU R                   R
                  R                  U5        U R                  b$  U R                  R                  U5      I S h  vN   g [        U R                   R
                  USS9  g  N%7f)N)instructionsagent_idTr  add_if_missing)	r   _instructionsr   AgentConfigUpdateid	_chat_ctxinsertr   rH   )rs   r  config_updates      ra   rH   !AgentActivity.update_instructionsu  s     $0! --%[[^^
 	$$]3'""66|DDD%%LQU Es   B
B4B2&B4c                  #    [        [        U R                  R                  5      5      n[        [        U5      5      n[	        X2-
  5      =(       d    S n[	        X#-
  5      =(       d    S n[	        U Vs0 s H  ofR
                  U_M     snR                  5       5      nXR                  l        [        R                  " UUU R                  R
                  S9n[        R                  " U5      R                  5       Ul        U R                  R                  R                  U5        U R                  bO  U R                  R                  [        R                  " U R                  5      R                  5       5      I S h  vN   [!        U R                  [        R"                  5      (       a;  U R%                  U R                  R                  R'                  US95      I S h  vN   g g s  snf  Nn N7f)N)tools_addedtools_removedr  r   )r   r   r   _toolslistr
  valuesr   r	  ToolContextflattenr  r  r   update_toolsr   rp   r   update_chat_ctxcopy)rs   r   old_tool_namesnew_tool_namesr  r  toolr  s           ra   r  AgentActivity.update_tools  so    /0B0BCD/67>:;Ct^<=E6ggtm6==?@" --#'[[^^
  #u5==?$$]3'""//

0K0S0S0UVVVdhh((&&t{{'<'<'A'A'A'NOOO )! 7 W Ps2   A2G14G(
C4G1>G-?A"G1!G/"G1/G1T)exclude_invalid_function_callsc               0  #    UR                  U(       a  U R                  O[        S9nXR                  l        U R
                  b/  [        U5        U R
                  R                  U5      I S h  vN   g [        XR                  R                  SS9  g  N$7f)Nr  Tr  )
r  r   r&   r   r  r   rG   r  rH   r  )rs   r   r  s      ra   r  AgentActivity.update_chat_ctx  sv      ==5StzzYb=c (')""228<<<{{'?'?PT =s   A-B/B0%B)r   r   r   r   r   c               h   [        U5      (       d  [        U5      (       al  [        R                  " S5        [        U R                  S   [        U5      (       a  UOU R                  S   [        U5      (       a  UOU R                  S   S9n[
        R                   " U5      (       a  Xl        U R                  b#  U R                  R                  U R                  S9  [
        R                   " U5      (       ao  U R                  U5      nU R                  S:X  d  US:X  a.  U R                  b!  U R                  R                  5         S U l        X0l
        U R                  S;  U l        U R                  (       a:  U R                  R                  [        U5      (       a  [        U5      O[         US	9  g g )
NzWmin_endpointing_delay and max_endpointing_delay are deprecated, use endpointing insteadr   r   r   r   r   r   r   )r   r   )r)   r   r   rL   r   r   r   r   update_optionsr   r   r   cancelr   r   r5   r&   )rs   r   r   r   r   r   s         ra   r$  AgentActivity.update_options  s    )**h7L.M.MNNi  2**62122 0**;7122 0**;7  >>+&& +'++8I8I+J>>.))!::>JN $$0Nh4N00<..55715.#1 CGCWCW ` DD@
 ""##22,-- //?@-	 3  #r`   Nr   namec               N  ^ ^^^^	 [         R                  T 5      nSnTb  [        R                  T5      n[        R                  " 5       mSUU4S jjn[
        R                  " U" 5       US9m	T R                  R                  T	5        T	R                  U U	4S j5        [        T	TS9  Tb4  TR                  R                  T	5        S	U4S jjnT	R                  U5        T	R                  U 4S j5        [         R                  U5        U(       a  [        R                  U5        T	$ )
z
This method must only be used for tasks that "could" create a new SpeechHandle.
When draining, every task created with this method will be awaited.
Nc                    >#    [         R                  " T5      n  TI S h  vN [         R                  " U 5        $  N! [         R                  " U 5        f = f7fr   )otel_contextattachdetach)tokencorocurrent_contexts    ra   _context_aware_coro>AgentActivity._create_speech_task.<locals>._context_aware_coro  sB      ''8E+!z##E* "##E*s%   A= ;= A= AAr(  c                :   > TR                   R                  T5      $ r   )r   remove)_rs   tasks    ra   <lambda>3AgentActivity._create_speech_task.<locals>.<lambda>  s    ););)B)B4)Hr`   )r   c                j   > [        S TR                   5       5      (       a  TR                  5         g g )Nc              3  @   #    U  H  oR                  5       v   M     g 7fr   done).0r7  s     ra   	<genexpr>RAgentActivity._create_speech_task.<locals>._mark_done_if_needed.<locals>.<genexpr>  s     D/Ctyy{{/C   )all_tasks
_mark_done)r6  r   s    ra   _mark_done_if_needed?AgentActivity._create_speech_task.<locals>._mark_done_if_needed  s,    D}/C/CDDD!,,. Er`   c                $   > TR                  5       $ r   )_wake_up_scheduling_taskr6  rs   s    ra   r8  r9    s    )F)F)Hr`   )rz   r	   )r6  zasyncio.Taskrz   r{   )_AgentActivityContextVarr   _SpeechHandleContextVarr+  get_currentrn   create_taskr   rl   add_done_callbackr/   rC  reset)
rs   r/  r   r(  tktk1r1  rE  r0  r7  s
   ```     @@ra   _create_speech_task!AgentActivity._create_speech_task  s     &))$/$)--m<C '224	+ 	+ ""#6#8tD!!$'HIMB$  ''-/ ""#78HI &&r*#))#.r`   reuse_resourcesc               &  ^ #    T R                    IS h  vN   T R                  (       a   S S S 5      IS h  vN   g [        R                  " S[        R
                  T R                  R                  0S9n T T R                  l	        [        R                  " USS9   [        T R                  [        R                  5      (       a  T R                  R                  5         [        T R                   [         R"                  5      (       a  T R                   R                  5         [        T R$                  [$        R&                  5      (       a  T R$                  R                  5         S S S 5        T R)                  US9I S h  vN   ST l        [        R*                  " S[        R,                  " U5      [        R
                  T R                  R                  0S9[.        R0                  " [2        S	9SU 4S
 jj5       5       nT R5                  U" 5       SS9=T l        n[9        USS9  UR;                  5         S S S 5      IS h  vN   g  GN3 GN! , (       d  f       N= f N! UR;                  5         f = f N3! , IS h  vN  (       d  f       g = f7f)Nstart_agent_activity
attributesF)end_on_exitrT  Ton_enter)r
   rY  r   c                   >#    [        TR                  TR                  S9n  [        R	                  U 5      nTR                  R                  5       I S h  vN   [        R                  U5        g  N! [        R                  W5        f = f7f)N)rV   rW   )rT   r   r   _OnEnterContextVarr   r[  rO  )datarP  rs   s     ra   _traceable_on_enter0AgentActivity.start.<locals>._traceable_on_enter5  si      (T[[QD5/33D9"kk22444*004 5*004s.   B2A2 A0A2 B0A2 2B		BAgentTask_on_enterr3  inline_taskry   )r   r   r$   
start_spanr#   ATTR_AGENT_LABELrW   labelr   	_activityr   use_spanrp   r   r   prewarmr   STTr   TTS_start_sessionstart_as_current_spanset_span_in_contextr   log_exceptionsr   rR  r   r/   end)rs   rU  rd  r_  r7  s   `    ra   startAgentActivity.start  s     :::}} ::  **&'88$**:J:JKJ$!(,%^^JEB!$((CGG44((*!$((CGG44((*!$((CGG44((* C ))/)JJJ $--!55jA + < <dkk>O>OP
 %%V45 55 .2-E-E')0D .F . #d ($? Y :: CB K,  Y :::s   JIJI7JI
J9I76&I C
I&I  IB%I &I76JI5J
J
I	I  I22I75J7J=J >J
Jc                D  #    [        5       n U R                  (       a  U R                  b  [        U R                  5      R
                  [        UR                  5      R
                  L a@  U R                  UR                  L a'  U R                  R                  5       I Sh  vN Ul        U R                  Gb  [        U R                  [        R                  5      (       Ga  U R                  UR                  L Gav  U R                  R                  R                  =(       dW    U R                  R                  R                  SSSS9R!                  UR                  R                  R                  SSSS95      nU=(       aT    U R                  R                  R"                  =(       d-    U R                  R$                  UR                  R$                  :H  nU=(       ah    U R                  R                  R&                  =(       dA    [        R(                  " U R*                  5      [        R(                  " UR*                  5      :H  nU(       Ga"  U R                  R-                  SU R.                  5        U R                  R-                  SU R0                  5        U R                  R-                  SU R2                  5        U R                  R-                  SU R4                  5        U R                  R-                  SU R6                  5        U R                  R-                  S	U R8                  5        U R                  R-                  S
U R:                  5        U R                  Ul        SU l        U$  GN! [>         a    URA                  5       I Sh  vN    e f = f7f)z8Detach reusable resources for handoff to *new_activity*.NT)exclude_instructionsexclude_handoffexclude_config_updategeneration_createdinput_speech_startedinput_speech_stopped#input_audio_transcription_completedmetrics_collectedremote_item_addedrr   )!rd   r   r   typerW   stt_node
detach_sttrf   r   rp   r   r   r   mutable_chat_contextr   r  is_equivalentmutable_instructionsr  mutable_toolsr  r   off_on_generation_created_on_input_speech_started_on_input_speech_stopped'_on_input_audio_transcription_completed_on_metrics_collected_on_remote_item_added	_on_errorrh   rq   rw   )rs   new_activity	resourcesreusables       ra   _detach_reusable_resources(AgentActivity._detach_reusable_resourcesJ  s    &(	9	 ''HH($--l6H6H1I1R1RRHH 0 00/3/F/F/Q/Q/S)S	&   ,txx):):;;HH 0 00  8800EE 
$$--22-14_c 3 #m$**338815,026 9   $ HH))>> Rzz..,2D2D2Q2QQ 
 $ HH))77 Ztzz2coolFXFX6YY 
 $$(()=t?Z?Z[$$(()?A^A^_$$(()?A^A^_$$((=DD $$(()<d>X>XY$$(()<d>X>XY$$(($..A+/+;+;I('+D$ g *T\  	##%%%	s<   N BM8 !M5"KM8 3N 5M8 8NNNN c                 ^#    U R                   R                  5       (       d   S5       e[        U R                  [        R                  5      (       aL  U R                  R                  SU R                  5        U R                  R                  SU R                  5        [        U R                  [        R                  5      (       aL  U R                  R                  SU R                  5        U R                  R                  SU R                  5        [        U R                  [        R                  5      (       aL  U R                  R                  SU R                  5        U R                  R                  SU R                  5        [        U R                  [        R                  5      (       a&  U R                  R                  SU R                  5        [        U R                  [        R                   5      (       ar  U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  R                  SU R"                  5        U R$                  (       aW  SSKJn  [*        R,                  " S5        U R$                   Vs/ s H  nU" [.        R0                  " S5      US	9PM      snU l        U R4                   Vs/ s H&  n[        U[        R6                  5      (       d  M$  UPM(     nnU(       aH  [.        R8                  " [*        S
9SS j5       m[:        R<                  " U4S jU 5       SS06I S h  vN   [        U R                  [        R>                  5      (       GaS  US L=(       a    UR@                  S LnU(       ay  U(       a  UR@                  c   e[*        RB                  " S5        UR@                  U l"        S Ul         U RD                  RG                  5         U RD                  RI                  5         OU R                  RK                  5       U l"        U RD                  R                  SU RL                  5        U RD                  R                  SU RN                  5        U RD                  R                  SU RP                  5        U RD                  R                  SU RR                  5        U RD                  R                  SU R                  5        U RD                  R                  SU RT                  5        U RD                  R                  SU R                  5        [W        U RX                  RZ                  5        U R                  R\                  nS=n=pU(       a$  UR^                  nUR`                  n	URb                  n
U RD                  Re                  U(       a  U RX                  Rf                  O[h        U	(       a  U RX                  Rj                  O[h        U
(       a.  [        Rl                  " U R4                  5      Ro                  5       O[h        S9I S h  vN   [.        Rp                  [r        [t        Rv                  4   " SS9U l<        URz                  (       dL  U R                  (       d;  U R|                  R~                  R                  (       a  [*        R                  " S5        O][        U R                  [        R                  5      (       a4   [        U RX                  RZ                  U RX                  Rf                  SS9  [        R                  " U RX                  Rf                  [        U R4                  5      =(       d    S U RX                  R                  S9n[        Rl                  " U R4                  5      Ro                  5       UlH        U RX                  RZ                  R                  U5        U R                  5       I S h  vN   [        U R|                  U U R                  (       a  U RX                  R                  OS U R                  U R                  [        U R                  5      U R                  U R                  (       a  U R                  R                  OS U R                  (       a  U R                  R                  OS S9	U lR        U(       aN  UR                  bA  [*        RB                  " S5        U R                  R                  UR                  S9  S UlS        g U R                  R                  5         g s  snf s  snf  GN GN! [         a    [*        R                  " S5         GN3f = f GNr7f) Nz/_start_session should only be used when locked.r{  rr   overlapping_speechr   )
MCPToolsetz}passing MCP servers to AgentSession or Agent is deprecated and will be removed in a future version. Use `MCPToolset` instead.mcp_toolset_)r
  
mcp_serverr   c                @   #    U R                  5       I S h  vN   g  N7fr   )setup)toolsets    ra   _setup_toolset4AgentActivity._start_session.<locals>._setup_toolset  s     mmo%%s   c              3  4   >#    U  H  nT" U5      v   M     g 7fr   rX   )r>  r  r  s     ra   r?  /AgentActivity._start_session.<locals>.<genexpr>  s     Bg.))s   rj   Tz/reusing realtime session from previous activityrw  rx  ry  rz  r|  )r  r   r   d   )maxsizezaudio output is enabled but RealtimeModel has no audio modality and no TTS is set. Either enable audio modality in the RealtimeModel or set a TTS model.r  z!failed to update the instructions)r  r  r  )hooksr   r   interruption_detectionr   r   r   stt_providerz+reusing STT pipeline from previous activity)rf   )r  zllm.Toolsetrz   r{   )Ur   lockedrp   r   r   onr  r  r   rj  r   rk  r   VADr   r   AdaptiveInterruptionDetector_on_overlap_speech_endedr   llm.mcpr  r   r   r   	shortuuidr   r   Toolsetro  rn   ro   r   rh   debugr   	interruptclear_audiorV   r  r  r  r  r  rG   r   r  r   r  r  r  _update_sessionr  r&   r   r  r  BoundedDictr   r   Spanr   audio_outputr   rv   audiorr   rH   r   	exceptionr	  r   r
  r  r  _resume_scheduling_taskr0   r~  r5   r   r   modelproviderr   rf   rq  )rs   rU  r  serverr  toolsets	rt_reusedr   reset_instructionsreset_chat_ctxreset_toolsinitial_configr  s               @ra   rl  AgentActivity._start_session  s    zz  ""U$UU"dhh((HHKK+T-G-GHHHKK0dhh((HHKK+T-G-GHHHKK0dhh((HHKK+T-G-GHHHKK0dhh((HHKK+T-G-GHd1193Y3YZZ''**+>@Z@Z[''**7DNNC''**+?A^A^_,NNU #...F eoon=&Q.DO
 &*ZZQZT:dCKK3PDZQ!!0& 1& ..BB"&  
 dhh 1 122't3^8R8RZ^8^I&?+E+E+QQQNO#2#=#= -1*   **,  ,,.#'88#3#3#5  4d6Q6QR 68U8UV 68U8UV5<<  3T5O5OP 3T5O5OP8 5 568800L@DDD &2%F%F"!-!B!B*88""229KT[[55QZ1?--Y?Jcoodjj199;PY 3    $)#4#4S%**_#Ec#RD ,,TXX$--BVBVB\B\* #''**F#KK))!%!9!9#' ..11*4::6>$[[^^

 !$

 ; C C E$$^4**,,,"2MM(,$$d#'#>#>*4+@+@A//(,dhhnnd.2hh**D
#
 ;;GLLFG##))7S7S)T+/O(##))+O
 RR*  F  !DEF 	-sw   J+e;.%ee;)#eeAe;"e#K.e;eB;e;3e Ce;e8Ee;e; e51e;4e55e;drain_agent_activity)r  c                 ^ #    [         R                  " 5       nUR                  [        R                  T R
                  R                  5        [        R                  " S[        R                  T R
                  R                  0S9[        R                  " [        S9S
U 4S jj5       5       nT R                   IS h  vN   T R                  c&  T R                  U" 5       SS9=T l        n[        USS9  T R!                  5          T R                  I S h  vN   T R%                  5       I S h  vN   Ub+   T R'                  U5      I S h  vN sS S S 5      IS h  vN   $  S S S 5      IS h  vN   g  N N^! ["         a     Nhf = f NX N= N/! [(         a    [        R*                  " S	5         NNf = f NE! , IS h  vN  (       d  f       g = f7f)Non_exitrX  r   c                 V   >#    T R                   R                  5       I S h  vN   g  N7fr   )r   r  r   s   ra   _traceable_on_exit/AgentActivity.drain.<locals>._traceable_on_exit  s     
 ++%%'''s   )')AgentTask_on_exitr3  Trb  z#failed to detach reusable resourcesry   )r   get_current_spanset_attributer#   re  r   rf  r$   rm  r   ro  r   r   r   rR  r/   _cancel_preemptive_generationrq   _pause_scheduling_taskr  BaseExceptionr  )rs   r  current_spanr  r7  s   `    ra   drainAgentActivity.drain  s     --/"";#?#?ARARS		%	%;#?#?ARAR"S

 
		V	,	( 
-

	( :::!!),0,D,D&(/B -E - "T ($?..0(((( --/// 'L!%!@!@!NN' ::. / :: )  0
 O' ( % L$$%JKL) :::s   B9G<E1=G AF3E5E3E5F3,F-F35F	F
FGF	GF3 G+F1,G3E55
F?F3FF3F	G F.+F3-F..F31G3G
9F<:G
Gblocked_tasksc               :  #    U R                   R                  5       (       d   S5       eU R                  (       a  g SU l        U=(       d    / U l        U R	                  5         U R
                  b)  [        R                  " U R
                  5      I S h  vN   g g  N7f)N4_finalize_main_task should only be used when locked.T)r   r  r   r   rH  r   rn   shield)rs   r  s     ra   r  $AgentActivity._pause_scheduling_task;  s      zz  ""Z$ZZ""""&$1$7R!%%'!!- ..!7!7888	 . 9s   BBBBc                   #    U R                   R                  5       (       d   S5       eU R                  (       d  g SU l        SU l        [        R
                  " U R                  5       SS9U l        g 7f)Nr  F_scheduling_taskr3  )r   r  r   r   rn   rM  r  r   r   s    ra   r  %AgentActivity._resume_scheduling_taskM  sb     zz  ""Z$ZZ"&&"'"'!(!4!4!!#*<"
s   A0A2c                 #    U R                    IS h  vN   [        R                  " S[        R                  U R
                  R                  0S9n U R                  US9I S h  vN   UR                  5         S S S 5      IS h  vN   g  Nv N'! UR                  5         f = f N! , IS h  vN  (       d  f       g = f7f)Nresume_agent_activityrX  rT  )	r   r$   rd  r#   re  rW   rf  rl  rp  )rs   rU  spans      ra   resumeAgentActivity.resumeY  s      :::$$''88$**:J:JKD))/)JJJ
 :: K
 :::sn   B?B
B?:B%B$B%B)B%9B?B#B?BB  B%#B?%B<+B.,B<8B?c                8    U R                   R                  5         g r   )r   r   r   s    ra   rH  &AgentActivity._wake_up_scheduling_taskf  s    r`   c               R  #    U R                    IS h  vN   [        R                  " S[        R                  U R
                  R                  0S9nS n U R                  US9I S h  vN   Ub  U R                  U5      I S h  vN nU R                  5       I S h  vN    UR                  5         UsS S S 5      IS h  vN   $  N N^ ND N.! [         a    Ub  UR                  5       I S h  vN    e f = f! UR                  5         f = f NM! , IS h  vN  (       d  f       g = f7f)Npause_agent_activityrX  r  )r   r$   rd  r#   re  r   rf  r  r  _close_sessionr  rw   rp  )rs   r  r  r  r  s        ra   pauseAgentActivity.pausei  s      :::$$&'88$++:K:KLD
 48I111NNN  +&*&E&El&S SI))+++ 
- :: O !T+  (#++---
 
) :::s   D'CD'<DC&C'CCCC	C D1D'=D>D'CC	C C3+C.,C33C66DDD'D$DD$ D'c                	  #    U R                   R                  5       (       d   S5       e[        U R                  [        R                  5      (       aL  U R                  R                  SU R                  5        U R                  R                  SU R                  5        [        U R                  [        R                  5      (       Ga  U R                  Gb
  U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  R                  SU R                  5        [        U R                  [        R                   5      (       aL  U R                  R                  SU R                  5        U R                  R                  SU R                  5        [        U R"                  ["        R$                  5      (       aL  U R"                  R                  SU R                  5        U R"                  R                  SU R                  5        [        U R&                  [&        R(                  5      (       a&  U R&                  R                  SU R                  5        [        U R*                  [,        R.                  5      (       ar  U R*                  R                  SU R                  5        U R*                  R                  SU R                  5        U R*                  R                  S	U R0                  5        U R                  b"  U R                  R3                  5       I S h  vN   U R4                  b  U R4                  R7                  5         U R8                  b"  U R8                  R3                  5       I S h  vN   U R:                  U R<                  R>                   Vs/ s H&  n[        U[        R@                  5      (       d  M$  UPM(     sn-   nU(       a%  [B        RD                  " S
 U 5       SS06I S h  vN   U RG                  U RH                  SS9I S h  vN   S U l$        g  GN Ns  snf  N8 N7f)Nz/_close_session should only be used when locked.r{  rr   rw  rx  ry  rz  r|  r  c              3  @   #    U  H  oR                  5       v   M     g 7fr   )rm   )r>  r  s     ra   r?  /AgentActivity._close_session.<locals>.<genexpr>  s     ;(w..""(rA  rj   TF)old_taskr  )%r   r  rp   r   r   r  r  r  r   r   r  r  r  r  r  r   rj  r   rk  r   r  r   r   r  r  rm   r   clearr   r   r   r   r  rn   ro   _cancel_speech_pauser   )rs   r  r  s      ra   r  AgentActivity._close_session  s    zz  ""U$UU"dhh((HHLL,d.H.HIHHLL$..1dhh 1 122t7G7G7S  !5t7R7RS  !79V9VW  !79V9VW  5<<   !4d6P6PQ  !4d6P6PQ  $..9dhh((HHLL,d.H.HIHHLL$..1dhh((HHLL,d.H.HIHHLL$..1dhh((HHLL,d.H.HId1193Y3YZZ''++,?A[A[\''++GT^^D''++,@$B_B_`'""))++++  &&("".))00222 ??![[..&
.T*T3;;2OD.&
 
 ..;(;OS   ''33 ( 
 	
 	
 *.&- , 3&
	
s[   OS2S$AS24S'5'S2#S)S)	,S25S.6!S2S0S2'S2)S20S2c                B  #    U R                    IS h  vN   U R                  (       a   S S S 5      IS h  vN   g SU l        U R                  5         S U l        U R	                  5       I S h  vN   [
        R                  " U R                  SS96 I S h  vN   U R                  b1  [        R                  R                  U R                  5      I S h  vN   S U R                  l        S S S 5      IS h  vN   g  N N N Nj N. N! , IS h  vN  (       d  f       g = f7f)NTFforce)r   r   r  r   r  rn   ro   _interrupt_background_speechesr   r   aiocancel_and_waitr   rg  r   s    ra   rm   AgentActivity.aclose  s      :::|| ::  DL..0 "&D%%'''..$"E"EE"E"RSSS%%1ii//0F0FGGG$(DKK!! :: (S H :::s   DC9DDDC;D1D,C=-'DC?=DDD(D3D4D;D=D?DDDDDDDc                B   U R                   (       d  g U R                  =(       aE    U R                  R                  (       + =(       a#    U R                  R                  R
                  S   =(       dX    U R                  R                  S:H  =(       a8    U R                  R                  S:  =(       a    U R                  R                  S LnU(       d(  U R                  b  U R                  R                  U5        U R                  b  U R                  R                  XS9  g g )N discard_audio_if_uninterruptiblespeakingr   )skip_stt)r   r   r   r   r   r   agent_state_aec_warmup_remaining_aec_warmup_timerr   
push_audior   )rs   frameshould_discards      ra   r  AgentActivity.push_audio  s    }}    W((<<<W%%223UV

 MM%%3 <33a7<//t; 	 +  ++E2 "".##..u.N /r`   c                z    U R                   (       d  g U R                  b  U R                  R                  U5        g g r   )r   r   
push_video)rs   r  s     ra   r  AgentActivity.push_video  s1    }}'''. (r`   )r  r   add_to_chat_ctxc          
     v   [        U5      (       d  U R                  (       d  [        U R                  [        R                  5      (       a%  U R                  R
                  R                  (       dU  U R                  R                  R                  (       a0  U R                  R                  R                  (       a  [        S5      e[        U R                  [        R                  5      (       aF  U R                  R
                  R                  (       a!  USL a  [        R                  " S5        [        n[         R"                  " [        U5      (       a  UOU R$                  S9nU R                  R'                  S[)        USSS95        U R*                  b  [        U5      (       d  U R                  (       d  [        U R                  [        R                  5      (       ak  U R                  R
                  R                  (       aF  U(       d  [        R                  " S	5        U R-                  U R/                  UU[1        5       S
9USS9  ONU R-                  U R3                  UUU=(       d    S U[1        5       S9USS9nUR5                  U R6                  5        U R9                  U[         R:                  5        U$ )Nztrying to generate speech from text without a TTS model or a RealtimeSession that supports say(); add a TTS model to AgentSession to enable say()Fzthe RealtimeModel uses a server-side turn detection, allow_interruptions cannot be False when using VoiceAgent.say(), disable turn_detection in the RealtimeModel and use VAD on the AgentTask/VoiceAgent instead)r   speech_createdTsayr   user_initiatedsourcez{add_to_chat_ctx=False is not supported when say() uses a RealtimeModel; the message will still be added to the chat context)r   textmodel_settingszAgentActivity.realtime_sayr'  r   r  r  r  r  zAgentActivity.tts_say)r)   r   rp   r   r   r   supports_sayr   rv   r  audio_enabledRuntimeErrorr   r   r   r&   rK   creater   emitr;   r   rR  _realtime_reply_taskr-   	_tts_taskrN  _on_pipeline_reply_done_schedule_speechSPEECH_PRIORITY_NORMAL)rs   r  r  r   r  handler7  s          ra   r  AgentActivity.say  s     HH#*;*;<<AVAVAcAc$$**$$22B  txx!2!233%%44#u,NNn #,$$+,, !4))

 	VDQVW	
 (UOOHH488S%6%677%%22"J $$))"(#0? * 
 %1 %  ++"(-4$3#0?   %, , 
D ""4#?#?@fl&I&IJr`   )r   r   r  r   r   r   schedule_speechinput_detailsc               	   [        U R                  [        R                  5      (       aF  U R                  R                  R                  (       a!  USL a  [
        R                  " S5        [        nU R                  c  [        S5      e[        R                  " 5       n	[        U5      (       d$  U	b!  [        U	5      =n
(       a  U
R                  b  SnU R                  R                  5       n[        n[        U5      (       Ga  [        R                   " U5      nUR"                   Vs0 s H  oR$                  U_M     nnUR'                  5        Vs0 s H  oR$                  U_M     nn[(        [        R*                  [        R,                  -     " 5       n[/        U5       Hr  nUR1                  U5      =(       d    UR1                  U5      nUc2  [3        SU S[)        UR4                  R7                  5       5       35      eUR9                  U5        Mt     [:        R1                  S 5      =n(       a  UR<                  U R>                  :X  a  UR@                  U RB                  :X  a  / n[        U5      (       a  UOUnU Hr  n[        U[        RD                  [        RF                  -  5      (       a.  URH                  RJ                  [L        RN                  -  (       a  Ma  UR9                  U5        Mt     UUS S & [P        RR                  " [        U5      (       a  UOU RT                  US9nU RB                  RW                  S[Y        US	S
S95        [        U R                  [        R                  5      (       aX  U R[                  U R]                  UU(       a  UR^                  OS U=(       d    S [        U5      (       a  UOS [a        US9S9USS9  O[        U R                  [        Rb                  5      (       a  U R[                  U Re                  UU=(       d    U R>                  Rf                  [        U5      (       a  UOU[        U5      (       a  UOS U=(       d    S [a        [h        R                  " U5      (       d  U Rj                  c  UOU Rj                  S9S9USS9n	U	Rm                  U Rn                  5        U(       a   U Rq                  U[P        Rr                  5        U$ s  snf s  snf )NFzthe RealtimeModel uses a server-side turn detection, allow_interruptions cannot be False when using VoiceAgent.generate_reply(), disable turn_detection in the RealtimeModel and use VAD on the AgentTask/VoiceAgent insteadz-trying to generate reply without an LLM modelnoneztool 'z:' not found in agent's registered tools. Available tools: r   r  r   Tgenerate_replyr  r#  )r   
user_inputr  r   r  AgentActivity.realtime_replyr'  )r   r   r   new_messager  r  AgentActivity.pipeline_reply):rp   r   r   r   r   r   r   r&   r
  rn   current_taskr)   r.   function_callr   r  r  r  r
  r  r  Toolr  r   r   r   function_toolskeysrl   r]  rW   r   rV   r   RawFunctionToolFunctionToolr   flagsr   IGNORE_ON_ENTERrK   r  r   r  r;   rR  r  text_contentr-   r   _pipeline_reply_taskr  r   r   rN  r  r  r  )rs   r   r   r  r   r   r   r  r  r7  	task_info	all_toolsresolved_toolstool_ctxttoolset_dict	tool_dictr(  r  on_enter_datafiltered_tools	to_filterr  s                          ra   _generate_replyAgentActivity._generate_replyK  s    txx!2!233%%44#u,NNn #,88NOO##%$$)93D99y9**6"(KJJOO%	 DME??y1H-5->->?->DD!G->L?*2*:*:*<=*<Qq*<I=!#((S[["89;NE
#''-Dt1D<$  ',,01H1H1M1M1O,P+QS  %%d+ # /22488=8""dkk1m6K6Kt}}6\?A.6~.F.FNI	%D"4)<)<s?O?O)OPP IIOOh.F.FF  #))$/ &  .	!$$+,, !4))'	
 	VDQab	

 dhh 1 122$$))"(<H|88d!-!5,4^,D,D.$#0[#I *  %3 %  #''**++))"(%>)>)>,4^,D,D.)080F0FD!-!5#0 >>+66$:K:K:S %0!..$ *  %3 , D  ""4#?#?@!!&,*M*MNW @=s   &S%S*c                v    U R                   b,  U R                   R                  R                  5         S U l         g g r   )r   r   _cancelr   s    ra   r  +AgentActivity._cancel_preemptive_generation  s3    &&2''55==?*.D' 3r`   c                8    U R                   R                  5         g r   )r   r  r   s    ra   _pause_authorization"AgentActivity._pause_authorization  s    ##))+r`   c                8    U R                   R                  5         g r   )r   r   r   s    ra   _resume_authorization#AgentActivity._resume_authorization  s    ##'')r`   Fc                    / nU R                    H;  nU(       d  UR                  (       d  M  UR                  UR                  US95        M=     U$ )Nr  )r   r   rl   r  )rs   r  interrupted_speechesspeechs       ra   r  ,AgentActivity._interrupt_background_speeches  sJ    35//F222$++F,<,<5,<,IJ 0 $#r`   r  c               .  ^^ U R                  5         [        R                  S   " 5       mU R                  US9mU R                  b4  U R                  R                  US9  TR                  U R                  5        U R                   H&  u    p#UR                  US9  TR                  U5        M(     U R                  b  U R                  R                  5         T(       d  TR                  S5        T$ SUU4S jjnT H  nUR                  U5        M     T$ )zInterrupt the current speech generation and any queued speeches.

Returns:
    An asyncio.Future that completes when the interruption is fully processed
    and chat context has been updated
Nr  c                   > TR                  5       (       d*  [        S T 5       5      (       a  TR                  S 5        g g g )Nc              3  @   #    U  H  oR                  5       v   M     g 7fr   r<  )r>  r@  s     ra   r?  CAgentActivity.interrupt.<locals>.on_playout_done.<locals>.<genexpr>  s     ,^I]v[[]]I]rA  )r=  rB  
set_result)r6  futurer?  s    ra   on_playout_done0AgentActivity.interrupt.<locals>.on_playout_done  s5    {{}},^I],^)^)^%%d+ *_}r`   )r6  rK   rz   r{   )r  rn   Futurer  r   r  rl   r   r   rF  rN  )rs   r  r6  r@  rH  rG  r?  s        @@ra   r  AgentActivity.interrupt  s    	**,%'#BBBO+  ***7 ''(<(<= NNLAq5) ''/ + '&&(#d# , , /((9 / r`   c                    U R                   (       a  U R                   R                  5         U R                  b  U R                  R                  5         g g r   )r   clear_user_turnr   r  r   s    ra   rM  AgentActivity.clear_user_turn  s@    ""##335'((* (r`   )
skip_replyc               2   U R                   b=  U R                   R                  5         U(       d  U R                  R                  5         SnU R                  c   eU R                  R                  U R                  R                  R                  (       + UUUS9$ )NT)audio_detachedtranscript_timeoutstt_flush_durationrO  )r   commit_audior   r  r   commit_user_turninputr	  )rs   rR  rS  rO  s       ra   rU  AgentActivity.commit_user_turn  s     '))+,,. J&&222&&77#}}22@@@11!	 8 
 	
r`   c                   U R                   (       a!  U(       d  UR                  SS9  [        S5      eU R                  (       aE  U R                  R	                  5       (       a&  [
        R                  " S5        UR                  SS9  g   [        R                  " U R                  U* [        R                  " 5       U45         UR                  5         U R                  5         g ! [         a     Of = fMm  )NTr  zccannot schedule new speech, the speech scheduling is draining/pausing, the speech will be cancelledzoattempting to schedule a new SpeechHandle, but the scheduling_task is not running, the speech will be cancelled)r   r  r
  r   r=  r   r   heapqheappushr   timeperf_counter_ns	TypeError_mark_scheduledrH  )rs   r@  priorityr  s       ra   r  AgentActivity._schedule_speech  s    
 ""54(u  !!d&<&<&A&A&C&CNN B 4(t~~	4;O;O;QSY/Z[ 	 %%'    s   8C% %
C21C2r   c                  #    Sn U R                   R                  5       I S h  vN   U R                   R                  5         U R                  (       Ga  [        R
                  " U R                  5      u    p#UR                  5       (       a	  S U l        MS  X0l        U R                  S:  ag  U R                  [        R                  " 5       U-
  -
  nUS:  a  [        R                  " U5      I S h  vN   UR                  5       (       a	  S U l        M  UR                  5         UR                  5       I S h  vN   S U l        [        R                  " 5       nU R                  (       a  GM  / nU R                   H[  n[        U5      nU(       d  [         R"                  " S5        M-  UR$                  (       d  M@  UR'                  UR$                  5        M]     / nU R(                   HI  nX`R                  ;   a  M  [        U5      nU(       a  UR$                  U;   a  M8  UR'                  U5        MK     U R*                  (       a  [-        U5      S:X  a  g GMP   GN3 GNj GN'7f)N        r   z-blocked task without activity info; skipping.)r   waitr  r   rY  heappopr=  r   r   r[  rn   sleep_authorize_generation_wait_for_generationr   r.   r   rr   r   rl   r   r   len)	rs   last_playout_tsr6  r@  delayblocked_handlesr7  r   to_waits	            ra   r  AgentActivity._scheduling_task1  s    //&&(((OO!!#...$}}T^^<1;;==+/D('-$44s: ==A^_Eqy%mmE222{{}}/3, ,,.11333'+$"&))+' ...0 35O11.t4LL!PQ))&&t'9'9: 2 +-G**444.t4D../At$ + &&3w<1+<i ( 3 4s<   !IICI/I0AI5I61I*C-IIIc                  #    SnSnU(       d  U(       Ga'  U R                   c  U R                  (       d  SnO\SnU R                   =n(       a)  UR                  (       a  UR                  5       I S h  vN   [        R
                  " S5      I S h  vN   U R                  R                  5       (       a  SnO$SnU R                  R                  5       I S h  vN   U R                  (       a>  U R                  R                  =n(       a!  UR                  5       (       d  SnUI S h  vN   U(       a  GM  U(       a  GM&  g g  N N Nm N 7f)NTFr   )r   r   _generationsrg  rn   re  r   is_setrc  r   _end_of_turn_taskr=  )rs   agent_activeuser_activer@  eou_tasks        ra   _wait_for_inactive AgentActivity._wait_for_inactivej  s     k##+DNN$#"222F28K8K 55777mmA&&&''..00#"..33555 ''!%!8!8!J!JJXJ "+ lkk 8& 6 sV   A.E0D>1EE AEEAE#E$E2E<E EEEc                   [         R                  S 5      =n(       a;  [        U[        5      (       d  [        U[        5      (       a  UR
                  Ul        [        U[        5      (       aP  U R                  bC  U R                  R                  UR                  S 5      =n(       a  [        R                  " X15        U R                  R                  R                  U5        [         R"                  " U5        U R                  R%                  S['        US95        U R                  R%                  S[)        U R                  R*                  S95        g )Nr{  metricssession_usage_updated)usage)rK  r   rp   r   r    r
  	speech_idr   r   pop
request_idtrace_utilsrecord_realtime_metricsr   _usage_collectorcollectr"   collect_usager  r9   r:   r{  )rs   evr   realtime_spans       ra   r  #AgentActivity._on_metrics_collected  s     588>>M>r:&&*R*D*D(++BLr/00$$0"&"6"6":":2==$"OOO//B&&..r2""2&.0Eb0QR#$4==+>+>?	
r`   c                x   U R                   R                  nUR                  UR                  R                  5      b  g UR
                  (       a  UR
                  S   R                  OS nUR                  b  UR                  U:X  a4  UR
                  R                  UR                  R                  5       5        g g )N)	r   r  	get_by_iditemr
  itemsprevious_item_idrl   
model_copy)rs   r  local_chat_ctxlast_item_ids       ra   r  #AgentActivity._on_remote_item_added  s    ..##BGGJJ/; 7E6J6J~++B/22PT&"*=*=*M  ''(:(:(<= +Nr`   c                   [        U[        R                  5      (       a1  [        XR                  S9nU R                  R                  SU5        GOy[        U[        R                  5      (       a1  [        XR                  S9nU R                  R                  SU5        GO)[        U[        R                  5      (       a0  [        XR                  S9nU R                  R                  SU5        O[        U[        R                  5      (       a0  [        XR                  S9nU R                  R                  SU5        O[        U[        R                  5      (       al  [        XR                  S9nU R                  R                  SU5        UR                  (       d,  U R                  R                  U5        U R!                  5         g U R                  R                  U5        g )N)rr   r  rr   )rp   r   LLMErrorr7   r   r  RealtimeModelErrorr   STTErrorr   TTSErrorr   InterruptionDetectionErrorr   recoverabler  _fallback_to_vad_interruption)rs   rr   error_events      ra   r  AgentActivity._on_error  sE    eS\\**$5BKMMw4s5566$5BKMMw4s||,,$5BKMMw4s||,,$5BKMMw4yCCDD$59T9TUKMMw4$$''.224&r`   c                |    UR                   (       a  SU l        OSU l        U R                  R                  SU5        g )NTFr  )is_interruptionr   r   r  rs   r  s     ra   r  &AgentActivity._on_overlap_speech_ended  s0    *.D'*/D'/4r`   c                f   U R                   cm  U R                  R                  S5        U R                  (       aA  U R                  R	                  [
        R
                  " 5       U R                  R                  S9   U R                  5         g ! [         a    [        R                  " S5         g f = f)Nr  )
started_atuser_speaking_spanzdRealtimeAPI input_speech_started, but current speech is not interruptable, this should never happen!)r   r   _update_user_stater   on_start_of_speechr[  _user_speaking_spanr  r
  r   r  rs   r6  s     ra   r  &AgentActivity._on_input_speech_started  s    88MM,,Z8&&''::#yy{'+}}'H'H ; 	NN 	v	s   <B  B0/B0c                b   U R                   cm  U R                  (       aA  U R                  R                  [        R                  " 5       U R                  R
                  S9  U R                  R                  S5        UR                  (       a$  U R                  R                  [        SSS95        g g )N)ended_atr  	listening F
transcriptis_final)
r   r   on_end_of_speechr[  r   r  r  user_transcription_enabled_user_input_transcribedr<   r  s     ra   r  &AgentActivity._on_input_speech_stopped  s    88&&''88!YY['+}}'H'H 9 
 MM,,[9((MM11)R%H )r`   c                l   U R                   R                  [        UR                  UR                  S95        UR                  (       al  [
        R                  " SUR                  /UR                  S9nU R                  R                  R                  U5        U R                   R                  U5        g g )Nr  user)rolecontentr
  )r   r  r<   r  r  r   ChatMessageitem_idr   r  _upsert_item_conversation_item_added)rs   r  msgs      ra   r  5AgentActivity._on_input_audio_transcription_completed  s|    --%U	
 ;; //v2::VCKK!!..s3MM2237 r`   c           	        UR                   (       a  g U R                  (       d  U R                  (       a  [        R                  " S5        g [
        R                  " U R                  [        SS9S9nU R                  R                  S[        USSS95        U R                  U R                  UU[        5       S	9US
S9  U R                  U[
        R                   5        g )NzFskipping new realtime generation, the speech scheduling is not runningr  modalityr  r   Fr  r  r   generation_evr  z!AgentActivity.realtime_generationr'  )r  r   r   r   r   rK   r  r   rJ   r   r  r;   rR  _realtime_generation_taskr-   r  r  )rs   r  r  s      ra   r  $AgentActivity._on_generation_created  s    ""d&=&=NNcd$$ $ 8 8&8
 	VERbc	

 	  **$ , + 
 !4 	! 	
 	fl&I&IJr`   ignore_user_transcript_untilc               p   U R                   (       d  gU R                  R                  S:  a  U R                  R                  b  gU R                  R                  nUR
                  S   =(       a    UR
                  S   SLn[        U R                  [        R                  5      (       a&  U R                  R                  R                  (       a  gU R                  b[  UR
                  S   S:  aH  U R                  b;  U R                  R                  n[        [        USS95      UR
                  S   :  a  gU R                   b  U R                   R#                  5         U R$                  GbI  U R$                  R&                  (       Gd,  U R$                  R(                  (       Ga  U R$                  U l        U R,                  (       a!  U R,                  R/                  5         SU l        U R                  (       ak  U R                  R0                  R2                  (       dF  U R                  R4                  S:X  a,  U R                  R7                  [8        R8                  " 5       S	9  U(       Ga  U R                  R:                  R<                  (       a  U R                  R:                  R<                  R>                  (       a  U R                  R:                  R<                  RA                  5         U R                  RC                  S
5        U R                  (       a5  U R                  RE                  U=(       d    [8        R8                  " 5       S9  U RF                  (       a  U RI                  5         ggU R                   b  U R                   RK                  5         U R$                  RK                  5         gggg)a)  
Interrupt the current speech or generation, and optionally ignore the user transcript until the given timestamp.

Args:
    ignore_user_transcript_until: The timestamp until which the user transcript should be ignored.
        If None, the user transcript will be ignored until the current time.
Nr   resume_false_interruptionfalse_interruption_timeout	min_wordsTsplit_characterr  r  r  r  )&r   r   r  r  r   r   rp   r   r   r   r   r   r   current_transcriptrh  r%   r   start_user_activityr   interruptedr   r   r   r%  _endpointingoverlappingr  r  r[  rv   r  	can_pauser  _update_agent_stateon_end_of_agent_speechr   '_restore_interruption_by_audio_activityr  )rs   r  opt	use_pauser  s        ra   _interrupt_by_audio_activity*AgentActivity._interrupt_by_audio_activity  s    ;;==..2t}}7V7V7bmm##89 K  !=>dJ 	
 dhh 1 122txx7L7L7[7[ HH   -1''3**==D ;tT:;c>N>N{>[['002   ,((444$$888"&"6"6D --..55715. ''//<<HHMM--;''::#yy{ ;  T]]1177DMM<P<P<V<V<`<`$$**00211+>**++BB5Q5`UYU^U^U` C  ,,@@B - ##/$$..0$$..0A 9 5 -r`   c                   [         R                   " 5       nU(       a  X!R                  -
  UR                  -
  nU R                  R	                  SUS9  U R
                  (       aB  U R
                  R                  UU(       a  UR                  OSU R                  R                  S9  U R                  R                  5         SU l
        SU l        U R                  (       a"  U R                  R                  5         S U l        g g )Nr  last_speaking_timerb  )r  speech_durationr  F)r[  r  inference_durationr   r  r   r  r  r   r  r   r   r   r%  )rs   r  speech_start_times      ra   r   AgentActivity.on_start_of_speechm  s     IIK 14F4F FI^I^ ^((HY(Z""##66,68 2 2c#'==#D#D 7 
 	  &&(!&&+#))**113-1D* *r`   c                B   [         R                   " 5       nU(       a  X!R                  -
  UR                  -
  nOSU l        U R                  (       aP  U R                  R                  UU R                  R                  U R                  (       a  U R                  O[        S9  U R                  R                  SUS9  U R                  R                  5         U R                  (       a:  U R                  R                  R                   S   =n b  U R#                  U5        g g g )NT)r  r  r   r  r  r  )r[  silence_durationr  r   r   r  r   r  r   r   r&   r  r   r   r   r   r   _start_false_interruption_timer)rs   r  speech_end_timetimeouts       ra   r  AgentActivity.on_end_of_speech  s    ))+-0C0CCbF[F[[O%)D"""##44(#'==#D#D77 "88 5  	((. 	) 	
 	  $$&  MM11>>?[\\ 009	  r`   c                
   U R                   S;   a  g UR                  U R                  R                  R                  S   :  nU(       aA  U R                   S:w  d!  U R
                  (       a  UR                  S:X  a  U R                  5         UR                  (       aO  UR                  U R                  R                  R                  S   S-  ::  a  U R                  R                  5         g U R                  R                  5         g )Nr   min_durationr   r   r   r   )r   r  r   r   r   r   raw_accumulated_silencer  r  r   r   r  r   )rs   r  active_speechs      ra   on_vad_inference_done#AgentActivity.on_vad_inference_done  s    #==**dmm.C.C.P.PQ_.``  E)))))Q. --/ KK**dmm.C.C.O.OP[.\_`.``$$**,$$((*r`   c                   U R                  5         U R                  UR                  =(       d    UR                  S9  U R                  (       a7  U R                  R                  UR                  =(       d    UR                  S9  g g )Nr  )r  r  overlap_started_atdetected_atr   r  r  s     ra   on_interruptionAgentActivity.on_interruption  sn    446)))+)>)>)P".. 	* 	
 ""##::-/-B-B-Tbnn ;  #r`   c          	        [        U R                  [        R                  5      (       a&  U R                  R                  R                  (       a  g U R
                  R                  [        UR                  S   R                  UR                  S   R                  SUR                  S   R                  S95        UR                  S   R                  (       as  U R                  S;  ab  U R                  5         USL aL  U R                  (       a:  U R
                  R                  R                   S   =n b  U R#                  U5        g g g g g g )Nr   Flanguager  r  
speaker_idr   r  )rp   r   r   r   user_transcriptionr   r  r<   alternativesr  r  r  r   r  r   r   r   r  rs   r  r  r  s       ra   on_interim_transcript#AgentActivity.on_interim_transcript  s   dhh 1 122txx7L7L7_7_--%+44??1-22??1-88		
 ??1""t';'; D
 (
 --/ E!'' $ 5 5 B BC_ ``W 44W=	 ( "(
"r`   )r  c          	        [        U R                  [        R                  5      (       a&  U R                  R                  R                  (       a  g U R
                  R                  [        UR                  S   R                  UR                  S   R                  SUR                  S   R                  S95        U R                  (       an  U R                  S;  a^  U R                  5         USL aI  U R                  (       a8  U R
                  R                   R"                  S   =n b  U R%                  U5        [&        R(                  " U R+                  U R,                  S95      U l        g )Nr   Tr  r   Fr  )r  )rp   r   r   r   r  r   r  r<   r  r  r  r  r   r   r  r   r   r   r  rn   rM  r  r   r  s       ra   on_final_transcript!AgentActivity.on_final_transcript  s    dhh 1 122txx7L7L7_7_--%+44??1-22??1-88		
 ""t';'; D
 (
 --/ E!'' $ 5 5 B BC_ ``W 44W=)0)<)<%%t/M/M%N*
&r`   c           
        U R                   R                  R                  nUS   (       as  U R                  (       db  U R                  (       dQ  U R
                  b  U R
                  R                  (       a)  [        U R                  [        R                  5      (       d  g U R                  5         UR                  b*  [        R                  " 5       UR                  -
  US   :  a  g U R                  US   :  a  g U =R                  S-  sl        [        R                  " SUR                  /UR                   S9nU R"                  R$                  R'                  5       nU R)                  UUS[+        SS	9S
9n[-        UUUUR'                  5       U R.                  R'                  5       U R0                  [        R                  " 5       S9U l        g )Nr   max_speech_durationmax_retriesr*   r  r  r  transcript_confidenceFr  r  )r   r   r  r  )r   r   r   r   r   r   r   )r   r   preemptive_generationr   r   r   r  rp   r   r   r  started_speaking_atr[  r   r  new_transcriptr  r   r   r  r3  rJ   r}   r   r   r   )rs   r   preemptive_optsr   r   r   s         ra   on_preemptive_generation&AgentActivity.on_preemptive_generation  sm   --//EE	*&&&&$$09M9M9Y9Ydhh00**, $$0		d666I^9__,,0NN))Q.)(()"&"<"<
 ;;'',,.,,%!&8 - 
 '<'%]]_**//#))yy{'
#r`   c                   U R                   (       d  U R                  (       a  U R                  5         [        R                  " SSUR
                  0S9  U R                  R                  (       au  [        R                  " SUR
                  /UR                  S9nU R                  R                  R                  R                  U5        U R                  R                  U5        gU R                   b  U R"                  S:w  a  U R$                  b  U R$                  R&                  (       a  U R$                  R(                  (       dz  U R                  R*                  R,                  S   S	:  aS  [/        [1        UR
                  SS
95      U R                  R*                  R,                  S   :  a  U R                  5         gU R2                  nU R5                  U R7                  X15      SS9U l        g)Nz0skipping user input, speech scheduling is pausedr  extrar  r   Tr   r  r   r  Fz'AgentActivity._user_turn_completed_taskr3  )r   r   r  r   r   r  r   _closingr   r  r  r   r  r  rl   r  r   r   r   r   r  r   r   rh  r%   r   rR  _user_turn_completed_task)rs   r   r   r  s       ra   on_end_of_turnAgentActivity.on_end_of_turn7  s    ""d&=&=..0NNB#T%8%89
 }}%%"!001*.*D*D 
 %%++22<@66|D  HH $$0$$0$$88((44%%22;?!CK 3 3TJKmm##00=> ..022*.*B*B**8:: +C +
' r`   c           	       #    Ub
  UI S h  vN   SU l         [        R                  " U R                  SS96 I S h  vN   [        R
                  " SUR                  /UR                  S9n[        U R                  [        R                  5      (       a  U R                  R                  R                  (       a  g U R                  b  UR                  (       a[  UR                  S:w  aJ  U R                  R                  R                   R#                  U5        U R$                  R'                  U5        g U R                  R)                  5         UR                  (       a[  UR                  S:w  aJ  U R                  R                  R                   R#                  U5        U R$                  R'                  U5        g U R*                  =nb  UR,                  (       d"  [.        R0                  " SSUR                  0S	9  g U R3                  U R4                  5      I S h  vN   UR7                  5       I S h  vN   U R                  b  U R                  R7                  5         U R8                  (       d  U R:                  (       a  [.        R0                  " S
SUR                  0S	9  U R$                  R<                  (       aJ  U R                  R                  R                   R#                  U5        U R$                  R'                  U5        g U R                  R>                  RA                  5       n[B        RD                  " 5       n U R                  RG                  XSS9I S h  vN   [B        RD                  " 5       U-
  n[        U R                  [        R                  5      (       a  S nOU R                  c  g U R8                  (       d  U R:                  (       a  [.        R0                  " SSUR                  0S	9  U(       ae  U R$                  R<                  (       aJ  U R                  R                  R                   R#                  U5        U R$                  R'                  U5        g 0 nU RN                  (       a0  U RN                  RP                  U RN                  RR                  S.US'   URT                  b  URT                  US'   URV                  b  URV                  US'   URX                  b  URX                  US'   URZ                  b  URZ                  US'   XxS'   Ub  Xl.        S n	U R^                  =n
(       Ga!  U
R`                  R                  URb                  :X  a  U
R>                  Re                  U5      (       a  U
Rf                  U Rf                  :X  a  U
Rh                  U Rj                  :X  ar  U
Rl                  n	XRn                  l.        U Rq                  U	[r        Rt                  S9  [.        Rv                  " SS[B        RB                  " 5       U
Rx                  -
  0S	9  O0[.        R0                  " S5        U
Rl                  R{                  5         S U l/        U	c  U R}                  UU[        SS9S9n	U R                  [        R                  " 5       :w  a  U	R7                  5       I S h  vN   S n[        U R                  [        5      (       a  [        SU R                  S9nO?U R                  b2  [        U R                  RP                  U R                  RR                  S9n[        [B        RB                  " 5       URZ                  =(       d    SURX                  =(       d    SUU	R                  US9nU R$                  R                  S[        US 95        g  GN GNb GN GNr GNJ! [H         a     g [J         a    [.        RL                  " S5         g f = f GN/7f)!Nr   Fr  r  r   r  zMskipping reply to user input, current speech generation cannot be interruptedr  r	  z<skipping on_user_turn_completed, speech scheduling is paused)r  z,error occurred during on_user_turn_completedz9skipping reply to user input, speech scheduling is paused
model_namemodel_providerstt_metadatar  stopped_speaking_attranscription_delayend_of_turn_delayon_user_turn_completed_delay)r_  zusing preemptive generationpreemptive_lead_timezcpreemptive generation enabled but chat context or tools have changed after `on_user_turn_completed`r  r  )r   r   r  unknownrb  )	timestampend_of_utterance_delayr  r  r|  metadatar{  rx  )Ir   rn   ro   r  r   r  r  r  rp   r   r   r   r   rO  r   r  r  rl   r   r  rT  r   r   r   r   r  r   r  r   r   r  r   r  r[  perf_counteron_user_turn_completedr   rq   r  r   r  r  r  r  r  r  ry  r   r   r'  r  r   r   r   r   r   r  rK   r  r  r   r6  r3  rJ   r   r  r   r   r   r   r
  r  r9   )rs   r  r   r   r   temp_mutable_chat_ctx
start_timer  metrics_reportr   
preemptiver  eou_metricss                ra   r  'AgentActivity._user_turn_completed_taskd  sN     
 NN,-) nndAAANOOO(()"&"<"<
 dhh 1 122xx$$33+??**b0--33::<H>>|L  --/??""b(%%++22<@66|D"222N?!55c')<)<= ++D,J,JKKK **,,,+  **,""d&=&=NNN#T%8%89 }}%%%%++22<@66|D
 !% 4 4 9 9 ;&&(
	++44% 5    (,'8'8':Z'G$dhh 1 122LXX""d&=&=NNK#T%8%89  6 6%%++22<@66|D,.88"hhnn"&(("3"3.N>* ##/484L4LN01##/484L4LN01##/484L4LN01!!-262H2HN./9U56##1 -1444:4 ..,2K2KK''556KLL$$

2**d.?.?? * 8 8 3A''/%%ml>a>a%b11499;AVAV3VW
 y ((002*.D'  !00).*G< 1 M **g.B.B.DD
  ))+++$(d**C009TEYEYZH!!-//55dFZFZFcFcH !iik#'#9#9#@S $ 8 8 ?C)E#&&
 	.0Ek0Z[g  	PD L,*  	 	KL	n ,s   	_^._^G_^_0^"1D_?^( ^%^(  M
_*_+C/___"_%^( (
_2_4____c                .    U R                   R                  $ r   )r   r   r   s    ra   retrieve_chat_ctxAgentActivity.retrieve_chat_ctx#  s    {{###r`   c                ~   U R                   (       d  U R                  (       a  U R                  R                  5       (       a{  U R                  R	                  S5        U R
                  (       a,  U R
                  R                  [        R                  " 5       S9  U R                  (       a  U R                  5         g g g g )Nr  r  )
r   r   r=  r   r  r   r  r[  r   r  r  s     ra   r  %AgentActivity._on_pipeline_reply_done(  s    ~~t';';t?S?S?X?X?Z?ZMM--k:&&''>>15 ?  ((<<> ) @[~r`   c           	       #    [         R                  " SU R                  R                  S9 nUR	                  [
        R                  UR                  5        UR                  =n(       a   UR	                  [
        R                  U5        [        R                  " 5       Ul        U R                  UUUUUS9I S h  vN   S S S 5        g  N! , (       d  f       g = f7f)N
agent_turnr
   r  )r$   rm  r   _root_span_contextr  r#   ATTR_AGENT_TURN_ID_generation_id_parent_generation_idATTR_AGENT_PARENT_TURN_IDr+  rL  _agent_turn_context_tts_task_impl)rs   r   r  r  r  r  r  	parent_ids           ra   r  AgentActivity._tts_task2  s      ))$--"B"B
&&{'E'E}GcGcd)???y?**;+P+PR[\0<0H0H0JM-%%+ /- &   
 

 
s/   *CBC
:C;C
?	CC


CCc           	     V  ^ ^^^"#    [         R                  " TR                  S9nUR                  [        R
                  TR                  5        T R                  R                  R                  (       a   T R                  R                  R                  OS nT R                  R                  R                  (       a   T R                  R                  R                  OS n[        R                  " TR                  5       5      [        R                  " T R                   R#                  5       5      /n	TR$                  (       a=  U	R'                  [        R                  " T R(                  R#                  5       5      5        TR+                  U	5      I S h  vN   TR-                  5         TR.                  (       aF  UR                  [        R0                  S5        [2        R4                  R6                  " U	6 I S h  vN   g S n
S nS n[9        T[:        5      (       a/  [2        R4                  R<                  R?                  TS5      nUu  pO+[9        T[@        5      (       a  SU4S jjnU" 5       n
U" 5       nS nS nS nS m"S nSU UU"4S jjnS nS nUGbK  UGc   [C        T RD                  RF                  UUT R                  RH                  RJ                  T RL                  (       a  T RL                  RN                  OS T RL                  (       a  T RL                  RP                  OS S9u  nnT RR                  (       af  T RL                  =n(       aS  URT                  RV                  (       d  URT                  RX                  (       d  URZ                  I S h  vN =n(       a  Un
[]        UUR^                  S9u  nnO[]        XS9u  nnUR`                  Rc                  U5        T RD                  Re                  X5      n[        Rf                  " U5      (       a
  UI S h  vN OUnS nUb+  [i        UUS9u  nnUc  URj                  Rc                  U5        XU4 Vs/ s H
  nUc  M  UPM     nnTR+                  U5      I S h  vN   XU4 HO  nUc  M  URm                  5       (       d  M  URo                  5       (       a  M6  URq                  5       =n(       d  MO  Ue   Ub<  TR+                  [        R                  " URs                  5       5      /5      I S h  vN   [t        Rt                  " 5       nUR                  [        R0                  TR.                  5        TR.                  (       aP  [2        R4                  R6                  " U6 I S h  vN   Ub(  URw                  5         URs                  5       I S h  vN   Ub  URy                  5       I S h  vN   U(       a  URz                  OS	nTR.                  (       ax  Ubu  URs                  5       I S h  vN nUbX  UR`                  Rm                  5       (       a9  UR`                  Ro                  5       (       d  UR|                  b  UR|                  nOS	nUR                  [        R~                  U5        U(       a  U(       a  0 n U(       a  UR                  b  UR                  U S
'   U(       a  T"(       a
  T"U S'   UU S'   T RD                  R                  R                  SUTR.                  U S9n!TR                  U!/5        T R                  R                  U!5        T R                  R                  S:X  ay  T R                  R                  S5        T R                  (       a,  T R                  R                  [t        Rt                  " 5       S9  T R                  (       a  T R                  5         Ub;  UR`                  Rm                  5       (       d  UR`                  R                  5         g g g  GNu GN GN GNs  snf  GN GN GN GNr GNZ GN7f)Nr,  Tr   c                   >#    T 7v   g 7fr   rX   )r  s   ra   
_read_text0AgentActivity._tts_task_impl.<locals>._read_textu  s     
s   
c                P  >  U R                  5       =(       d    [        R                  " 5       mTR                  R	                  STTR
                  S9  TR                  (       a  TR                  R                  TS9  TR                  (       a  STl	        gg! [         a     gf = f
Callback to update the agent state when the first frame is captured:
1. _AudioOutput.first_frame_fut (float)
2. _TextOutput.first_text_fut (None)
Nr  r   r+  r  F
resultr[  r  r   r  r2  r   on_start_of_agent_speechr   r   futrs   r   r  s    ra   _on_first_frame5AgentActivity._tts_task_impl.<locals>._on_first_frame      &)jjl&Adiik# MM--.*>> . 
 &&''@@L_@`((?D< ) !    ,B 
B%$B%noderV  r  text_transformsr  r  r  
tts_outputtext_outputr  r  tts_node_ttfbr  r  	assistant)r  r  r  ry  r  r  r  )rz   AsyncIterable[str]rB  z,asyncio.Future[float] | asyncio.Future[None]rz   r{   )Lr   r  r2  r  r#   ATTR_SPEECH_IDr
  r   rv   transcription_enabledtranscriptionr	  r  rn   ensure_future_wait_for_authorizationr   rc  r   rl   r   wait_if_not_interrupted_clear_authorizationr  ATTR_SPEECH_INTERRUPTEDr   r  r  rp   r   	itertoolsteer   rF   r   tts_noder   tts_text_transformsr   r  r  r   r   aligned_transcriptr   timed_texts_futrB   audio_chfirst_frame_futrN  transcription_nodeiscoroutinerD   first_text_futr=  	cancelledr  wait_for_playoutr[  clear_bufferrm   r  synchronized_transcriptATTR_RESPONSE_TEXTttfbr  add_message_item_addedr  r  r  r   r  r   r  r%  )#rs   r   r  r  r  r  r  	tr_outputr  authorization_taskstext_sourceaudio_sourcer[  r8  tts_taskforward_audio_taskforward_text_taskr  rC  	audio_outtts_gen_datar   timed_textstr_nodetr_node_resulttext_outr-  	all_tasksr7  excforwarded_textplayback_evassistant_metricsr  r  s#   ```                               @ra   r3  AgentActivity._tts_task_implK  s     --m6W6WX"";#=#=}?O?OP }}##99 MM  .. 	
 6:]]5I5I5W5Wt}}++11]a !!-"G"G"IJ!!$"="="B"B"DE:
 ,,&&w'<'<T=U=U=Z=Z=\']^334GHHH**,$$&&{'J'JDQ))++-@AAA152637dM**))%%))$2C(+%Kc"" %,K%<L-17;6:,0,0	E 	E, *.	26#})>--&#1$(MM$9$9$M$M,0HH$((..$26((TXX..*&, 33 $))<<CDTDTD^D^.:.J.J(JJJ"-K0H!-,:O:O1-"I
 1I!-1-"I %%77H ++00M*1*=*=g*F*FwG'+%*A%%+'x #''99/J !6GH0
H!AAH 	 0
 33I>>> 3DEDDIIKK8H8H..**3*I F
 #77&&|'D'D'FGH   #iik"";#F#FHaHab$$))++Y777'))+"33555?**, +3$$)A , = = ??K %--2244!11;;==66B%0%H%HN!#"";#A#A>Ro35 1 1 =5A5F5F!/2"':;N!"78;N!"78++''33 &)55)	 4 C %%se,MM2237==$$
2MM--k:&&''>>15 ?  ((<<> )B)B)G)G)I)I%%,,. *J Q 	I
 B~ )K" '0
 	? 8 6 
 @s   F`)`A%`)7`	8F`)`B`)`<`)``"`)8`9`)	`) `)7`)>`)`A4`)`*`).` /`)	`#
A `)
`&G<`)	`)`)`)`)`)`) `)#`)&`))r  r  _previous_user_metrics_previous_tools_messagesc                 #    [         R                  " SU R                  R                  S9 n	U	R	                  [
        R                  UR                  5        UR                  =n
(       a   U	R	                  [
        R                  U
5        [        R                  " 5       Ul        U R                  UUUUUUUUS9I S h  vN   S S S 5        g  N! , (       d  f       g = f7f)Nr+  r,  )r   r   r   r  r  r  r  r  )r$   rm  r   r-  r  r#   r.  r/  r0  r1  r+  rL  r2  _pipeline_reply_task_impl)rs   r   r   r   r  r  r  r  r  r  r4  s              ra   r(  "AgentActivity._pipeline_reply_task	  s      ))$--"B"B
&&{'E'E}GcGcd)???y?**;+P+PR[\0<0H0H0JM-00+!-')'=)A 1 	 	 	
 
	
 
s/   *CBC=C>C	CC
CCc               "!  ^ ^^^9^:^;^<^=#    SSK Jn	  [        R                  " TR                  S9n
U
R                  [        R                  TR                  5        Ub   U
R                  [        R                  U5        U(       a3  U
R                  [        R                  UR                  =(       d    S5        T R                  R                  =n(       a>  UR                  R                  5       (       a  [!        XR                  R"                  5        T R                  R$                  R&                  (       a   T R                  R$                  R(                  OS nT R                  R$                  R*                  (       a   T R                  R$                  R,                  OS nUR/                  5       n[0        R2                  " U5      nUb  UR5                  U5        Ub  UR7                  SU/S9  [9        UTR:                  R<                  S9  / n[?        T R@                  RB                  UUTT R0                  (       a  T R0                  RD                  OS T R0                  (       a  T R0                  RF                  OS S9u  nm9URI                  U5        [J        RL                  RN                  RQ                  T9RR                  S	5      nUu  m<nS nS m;S5U9UU U<4S
 jjnT R                  RT                  RV                  nUb7  US   (       a-  US   (       a#  U" 5       I S h  vN u  nm;URI                  U5        [X        RZ                  " TR]                  5       5      nTR_                  U/5      I S h  vN   Um=Ub]  TR`                  (       aL  T R@                  Rb                  R5                  U5        T R                  Re                  U5        URf                  m=TRh                  (       ac  U
R                  [        Rj                  S5        [J        RL                  Rl                  " / UQUP76 I S h  vN   URo                  5       I S h  vN   g Ub&  Uc#  U" 5       I S h  vN u  nm;URI                  U5        SnT;by  T Rp                  (       ah  T Rr                  =n(       aU  URt                  Rv                  (       d  URt                  Rx                  (       d  T;Rz                  I S h  vN =n(       a  UnSnT R                  R}                  S5        [X        RZ                  " TR                  5       5      [X        RZ                  " T R                  R                  5       5      /nTR                  (       a=  URI                  [X        RZ                  " T R                  R                  5       5      5        TR_                  U5      I S h  vN   TR                  5         TRh                  (       ac  U
R                  [        Rj                  S5        [J        RL                  Rl                  " / UQUQ76 I S h  vN   URo                  5       I S h  vN   g [        R                  " 5       n    S6S jnT R@                  R                  U" U5      T5      n[X        R                  " U5      (       a
  UI S h  vN OUnS nS n Ub  [        UUS9u  n nURI                  U 5        S m:S n!S7U9U UU:U;U=4S jjn"S n#UbI  T;c   e[        UT;R                  S9u  n$n#URI                  U$5        U#R                  R                  U"5        OUb  UR                  R                  U"5        S8U4S jjn%S9U4S jjn&[        T R                  TUTR                  T9R                  U%U&S9u  n'n(TR_                  / UQ5      I S h  vN   Ub<  TR_                  [X        RZ                  " UR                  5       5      /5      I S h  vN   [        R                  " 5       n!0 n)T R0                  (       a0  T R0                  RD                  T R0                  RF                  S.U)S'   T Rr                  (       a0  T Rr                  RD                  T Rr                  RF                  S.U)S'   T9R                  b  T9R                  U)S'   T;(       a  T;R                  b  T;R                  U)S'   U!(       aK  T:(       aD  T:U)S'   U!U)S'   T=(       a3  ST=;   a-  T:T=S   -
  n*U*U)S'   U
R                  [        R                  U*5        U
R                  [        Rj                  TRh                  5        U(       a@  T R@                  Rb                  R5                  U5        T R                  R                  U5        U(       a  UR                  OSn+TRh                  (       a  [J        RL                  Rl                  " U6 I S h  vN   Ub  UR                  5         UR                  5       I S h  vN n,U#bX  U#R                  R                  5       (       a9  U#R                  R                  5       (       d  U,R                  b  U,R                  n+O8Sn+O5U(       a.  U(       a'  UR                  (       d  [        R                  " S5        U+(       a  0 n-T9R                  (       a  T9R                  U-S '   UR6                  " S:S!U+T9R                  TRh                  UU)S".U-D6n.T R@                  Rb                  R5                  U.5        T R                  Re                  U.5        TR                  U./5        U
R                  [        R                  U+5        TRh                  (       d5  [        U(R$                  5      S#:  a  T R                  R}                  S5        OT R                  R                  S$:X  ay  T R                  R}                  S%5        T R                  (       a,  T R                  R                  [        R                  " 5       S&9  T R                  (       a  T R                  5         U#b9  U#R                  R                  5       (       d  U#R                  R                  5         URo                  5       I S h  vN   TR                  5         TRh                  (       a(  [J        RL                  Rm                  U'5      I S h  vN   g T R                  R                  T5         U'I S h  vN   T R                  R                  T5        [        U(R$                  5      S#:  Ga  TR                  T R                  RT                  R                  S-   :  n/U/(       a!  [        R                  " S'S(TR                  0S)9  T=R                  S-  slm        / n0/ n1S n2Sn3[        / / S*9n4U(R$                   H  n5U5R                  bN  U0RI                  U5R                  5        U1RI                  U5R                  5        U5R                  (       a  SU4lr        U4R                  RI                  U5R                  5        U4R                  RI                  U5R                  5        U2b%  U5R                  b  [        R                  " S+5        Sn3U5R                  n2M     U2(       a  U3(       d  SU4lw        T R                  R                  S,U45        T R                  n6U4R                  (       a+  U2(       a$  U3(       d  T R                  R                  U25        Sn6U0U1-   n7U4R                  (       a  UR                  R                  U75        [        UT R@                  R                  SS-9  T R                  T GR                  TUUU	" U/(       d  U6(       d  TR                  S.:X  a  S.OS/S09U+(       d  T=OS U7S19TS2S39n8U8R                  T GR                  5        T GR                  TG[        GR                  SS49  g [        U15      S#:  aA  T R@                  Rb                  R5                  U75        T R                  R                  U75        g g g  GN" GN GN GN GN GNm G
N G
N) G
N G	N GN GNy GN GNn GN GN GN! T R                  R                  T5        f = f7f);Nr*   )r-   r,  r  systemr  r  r  )rH  r   r,  r  r  r  r   c            	     ^  >#    TR                   I S h  vN   [        TR                  R                  TTTR                  R
                  R                  TR                  (       a  TR                  R                  OS TR                  (       a  TR                  R                  OS S9u  pX4$  N7f)NrG  )
started_futrF   r   r\  r   r   r]  r   r  r  )rq  ru  llm_gen_datar  rs   tts_text_inputs     ra   _start_tts_inferenceEAgentActivity._pipeline_reply_task_impl.<locals>._start_tts_inferencem	  s     ****%:[[))$- $ 5 5 I I(,dhhnnd.2hh**D&"H )) +s   B-B+BB-r   preemptive_ttsTFthinkingc               d   #    U   S h  vN n[        U[        5      (       a  M   U7v   M'   N"
 g 7fr   )rp   r'   )
llm_outputchunks     ra   r8  ;AgentActivity._pipeline_reply_task_impl.<locals>._read_text	  s-       * ee]33zs   0.,.0.0rL  c                $  >  U R                  5       =(       d    [        R                  " 5       m0 nTR                  b  TR                  US'   T(       a  TR                  b  TR                  US'   T(       a  ST;   a  TTS   -
  US'   UTR
                  l        TR
                  R                  STTR                  S9  TR                  (       a  TR                  R                  TS9  TR                  (       a  S	Tl        gg! [         a     gf = f)
r<  Nllm_node_ttftrN  r  e2e_latencyr  r=  r  F)r?  r[  r  ttftrj  r   _early_assistant_metricsr  r2  r   r@  r   r   )rB  early_metricsr  rs   r   r  ru  user_metricss     ra   rC  @AgentActivity._pipeline_reply_task_impl.<locals>._on_first_frame	  s   &)jjl&Adiik# 02M  ,1=1B1Bo. 1 1 =1=1B1Bo. 5 E',7L*MM m, 6CDMM2MM--.*>> .  &&''@@L_@`((?D< )1 ! s   ,D 
DDrJ  c                ^   > [         R                   " 5       U l        TR                  U /5        g r   )r[  r   rl  )fnc_callr   s    ra   _tool_execution_started_cbKAgentActivity._pipeline_reply_task_impl.<locals>._tool_execution_started_cb	  s#     #'))+H%%xj1r`   c                b   > U R                   (       a  TR                  U R                   /5        g g r   fnc_call_outrl  outr   s    ra   _tool_execution_completed_cbMAgentActivity._pipeline_reply_task_impl.<locals>._tool_execution_completed_cb
  (    ))3+;+;*<=  r`   rV   r   r,  r   function_streamtool_execution_started_cbtool_execution_completed_cbr  llm_metadatatts_metadatar  rN  r  r  r  U`use_tts_aligned_transcript` is enabled but no agent transcript was returned from ttsr
  rO  )r  r  r
  r  r   ry  r   r  r  r  zamaximum number of function calls steps reached, generating final response with tool_choice='none'r|  r	  function_callsfunction_call_outputsz?expected to receive only one AgentTask from the tool executionsfunction_tools_executedr  r  autor#  )r   r   r   r  r  r  r  r'  r  )rz   z-tuple[asyncio.Task[bool], _TTSGenerationData])r  z"AsyncIterable[str | FlushSentinel]rz   rP  rQ  r  zllm.FunctionCallrz   r{   r  r=   rz   r{   rX   )rW   r-   r   r  r2  r  r#   rR  r
  ATTR_INSTRUCTIONSATTR_USER_INPUTr'  r   _room_ioroomisconnectedr+   local_participantrv   r	  r  rS  rT  r  r   r  r  rk  rA   r  r  rC   r   llm_noder  r  rl   r   r  rZ  r[  text_chr   r  rn   rU  _wait_for_scheduledrW  	scheduledr  r  ry  r  rY  r  rm   r   r   r   r^  r   r_  r  rV  r   rc  r   r   rX  r[  rb  rc  rD   rB   r`  ra  rN  rd  rE   r   function_chrf  r  rj  ATTR_E2E_LATENCY_tool_items_addedr  rg  r=  re  rh  r   r   generated_extrarl  ri  rh  r  r   r  r   r  r%  _mark_generation_doner   adddiscard	num_stepsmax_tool_steps
_num_stepsr8   r  r  reply_required_reply_requiredr  r  
agent_taskrr   _handoff_requiredr  r   update_agentr  extendrH   r  rR  r(  r  r  rK   r  )>rs   r   r   r   r  r  r  r  r  r-   r  room_ior  rM  r,  rt   llm_tasktext_teetr_inputrq  r  r  wait_for_scheduledread_transcript_from_ttsr   rv  rn  reply_started_atr8  rw  rx  ry  text_forward_taskr  rC  rt  forward_taskr  r  exe_tasktool_outputr~  r  r|  r}  extra_kwargsr  max_steps_reached	new_callsnew_fnc_outputsnew_agent_taskignore_task_switchfnc_executed_evsanitized_outdrainingtool_messagestool_response_taskr  r  ru  r  r  s>   ``  `                                                    @@@@@ra   r  'AgentActivity._pipeline_reply_task_impl/	  s     	)--m6W6WX"";#=#=}?O?OP#&&{'D'DlS&&{'B'BKD\D\Db`bc}}---G-7<<3K3K3M3M'll6T6TU59]]5I5I5W5Wt}}++11]a }}##99 MM  .. 	
 ==???5)"OOK(#  h G 	$H}7R7R7[7[\
 *,!6%%)$(HH$((..$*.((TXX&&"
, 	X99&&**<+?+?C#+ .226
	* 
	* --//EE$	* 01+?+A%A"HlLL"$22=3T3T3VW335G4HIII 2H"}'>'>KK!!((5MM22;?&..L$$&&{'J'JDQ))++GUG4FGGG//### #(8+?+A%A"HlLL"#( $//!!44C<L<L<V<V&2&B&B BBB"H'+$))*5 !!-"G"G"IJ!!$"="="B"B"DE:
 ,,&&w'<'<T=U=U=Z=Z=\']^334GHHH**,$$&&{'J'JDQ))++IUI5HIII//###99;	:		 ++00H1E~V*1*=*=g*F*FwG'+15%*A'+'x LL*+,0,0"	E "	EH *.	#+++&>)l6K6K'#L) LL&%%77H!##55oF	2	>
 !8MM'&22(44&@(D!
+ 33HeH=== #77&&|'D'D'FGH   #iik/188"hhnn"&(("3"31n- 88"hhnn"&(("3"31n-
 (1=1B1Bo.L--91=1B1Bo.#67J347J34 5 E1LAV4WW3>!-0**;+G+GU"";#F#FHaHab $KK!!(()ABMM++,DE*2$$))++U333 '))+$0$A$A$CC)!116688%55??AA #::F)4)L)L%'N%(8==NNg !#L++(4(D(DW%&&  &??)55+) C KK!!((-MM2237%%se,&&{'E'E~V((S1C1C-Dq-HMM--j9]]&&*4MM--k:&&''>>15 ?  ((<<> )B)B)G)G)I)I%%,,.oo++-$$))++H555 	!!%%m4	=NN%%--m< {!!"Q& - 7 74==;P;P;_;_bc;c c H&(8(89 $$)$02I<>O+/N!&8!O "-!3!3 --9$$]%;%;<#**=+E+EF$33:>7  ..55m6L6LM55<<]=W=WX!--2J2J2VLL!bc)-& "/!9!9! "4$ &8481MM8/J--H00^L^**>:%7M..%%m4
 $!%!:!:#( &*%=%=--&3!)#'4  1H@Z@Z^d@d )/!'( DR|W[1>! . $ #07) &> &", #44T5Q5QR%%!<#F#Fd &  _%)%%,,];//> *m 'S &B 	J H#
 &B !C 	I
 J# 'f 	>T 4 Db 	 
 6 %%--m<s  LABA@<AAB,A@?-B<AB)AA*ABAAABAABABAACAB=AA>A*AB(AA)AB AAA%AB&AA'C>AB%AA&>AB$AA%G%AB
AA *AB5AA#6JAB=AA&>AABAA) AB'AA/ ,AA,-AA/ 1MAB@?ABAABAABAABAABAABAABAABAABAABAABA ABA#ABA&ABA)ABA,AA/ A/ABBAB)r   r  r  
tool_replyr  c                 #    U R                   c   S5       e[        R                  " UR                  5       5      [        R                  " U R                  R                  5       5      /nUR                  (       a=  UR                  [        R                  " U R                  R                  5       5      5        UR                  U5      I S h  vN   UR                  (       a%  [        R                  R                  " U6 I S h  vN   Ub>   U R                   R                  U5      I S h  vN n	U R)                  UU	US9I S h  vN   g Ub  U R                   R*                  R-                  5       nUR/                  SUS9nU R                   R1                  U5      I S h  vN   U R2                  R4                  R7                  U5        U R8                  R;                  U5        [<        n[<        n U R                   R>                  R@                  RB                  =n(       d  [E        URF                  5      (       aI  URF                  U RH                  :w  a/  U RH                  nU R                   RK                  URF                  S9  Ubi  U R                   RL                  RO                  5       nU R                   RQ                  [        RR                  " U5      RO                  5       5      I S h  vN    U R                   RU                  U=(       d    [<        U(       a  URF                  O[<        U(       a'  Ub$  [        RR                  " U5      RO                  5       O[<        S9I S h  vN n	U R)                  UU	UUS9I S h  vN   [E        U5      (       a   U R                   RK                  US9  [E        U5      (       a%   U R                   RQ                  U5      I S h  vN   g g  GN GN GN! [        R                    a*  n
["        R$                  " S['        U
5      5         S n
A
g S n
A
ff = f GN GN GNI N! [        R                    a  n
["        R$                  " SU(       a  S	OS
['        U
5      5        U R8                  RW                  S5         S n
A
[E        U5      (       aA   U R                   RK                  US9  O&! [X         a    ["        RZ                  " S5         Of = f[E        U5      (       aL   U R                   RQ                  U5      I S h  vN    g ! [X         a    ["        RZ                  " S5         g f = fg S n
A
ff = f GN! [X         a    ["        RZ                  " S5         GNf = f GN! [X         a    ["        RZ                  " S5         g f = f! [E        U5      (       aA   U R                   RK                  US9  O&! [X         a    ["        RZ                  " S5         Of = f[E        U5      (       aL   U R                   RQ                  U5      I S h  vN    f ! [X         a    ["        RZ                  " S5         f f = ff = f7f)Nrt_session is not availablezfailed to say text: %sr  r  r  r#  )r  r   r   z failed to generate a reply%s: %sz after tool executionr  r  zfailed to reset tool_choicezfailed to reset toolsr   r  r  r  ).r   rn   rU  rV  r   rc  r   rl   r   rW  r  r   r  r  r  r   RealtimeErrorr   rr   r   r  r   r  rk  r  r   r  r  r   r  r&   realtime_modelr   per_response_tool_choicer)   r   r   r$  r   r  r  r  r  r  rq   r  )rs   r   r  r   r  r  r  r  rn  r  er   r  ori_tool_choice	ori_toolsr  s                   ra   r  "AgentActivity._realtime_reply_task
  sh     +J-JJ+ !!-"G"G"IJ!!$"="="B"B"DE:
 ,,&&w'<'<T=U=U=Z=Z=\']^334GHHH$$))++-@AAA&*&6&6&:&:4&@ @
 00++- 1   
 !''00557H&&FJ&GC""228<<<KK!!..s3MM2237=F09	9	> ##22??XXY( Y
 ^7788&22d6G6GG&*&7&7O$$33@Z@Z3[$ $ 0 0 6 6 > > @I**778N8V8V8XYYY&*&6&6&E&E!-!:6N22T] 48I .668& 'F 
' 
!* 00++-)	 1    ((D$$333P 	"">**77	BBB #a 	IA !A$$ 5s1v> =* Z
! $$ 6/9+rF
 11+> ((D$$333P  D$$%BCD 	"">**77	BBB  >$$%<=> #1 ! D$$%BCD
 C  >$$%<=> ((D$$333P  D$$%BCD 	"">**77	BBB  >$$%<=> #s  C	YO"5YO%Y
O+ (O()O+ -YP,AYP/AY.C;V )P2*V /A4P7 #P5$P7 (V =U
>V YU ,Y=U7 U4U7  Y%Y(O+ +P)? P$Y$P))Y/Y2V 5P7 7UAUV Y%R? >Y? S"Y!S""Y6T TT Y T>;Y=T>>YUV  U1-Y0U11Y4U7 7 VYVYY/W	Y	 W,)Y+W,,Y X%X!X%$Y% YYYYY)r  c                 #    [         R                  " SU R                  R                  S9 nUR	                  [
        R                  UR                  5        UR                  =n(       a   UR	                  [
        R                  U5        [        R                  " 5       Ul        U R                  UUUUS9I S h  vN   S S S 5        g  N! , (       d  f       g = f7f)Nr+  r,  r  )r$   rm  r   r-  r  r#   r.  r/  r0  r1  r+  rL  r2  _realtime_generation_task_impl)rs   r   r  r  r  r  r4  s          ra   r  'AgentActivity._realtime_generation_taskN  s      ))$--"B"B
&&{'E'E}GcGcd)???y?**;+P+PR[\0<0H0H0JM-55++-)	 6   
 

 
s/   *CBC	9C:C	>	CC		
CCc               h  ^ ^^^^&^'^(^)^*^+^,^-^.^/#    [         R                  " TR                  S9nUR                  [        R
                  TR                  5        T R                  R                  nU(       a>  UR                  R                  5       (       a  [        XVR                  R                  5        T R                  c   S5       e[        T R                  [        R                   5      (       d   S5       eUR#                  [        R$                  S[        R&                  T R                  R(                  [        R*                  T R                  R,                  05        T R.                  b*  TR0                  (       a  UT R.                  TR0                  '   T R                  R2                  R4                  (       a   T R                  R2                  R6                  OS m'T R                  R2                  R8                  (       a   T R                  R2                  R:                  OS m/[        R<                  " T R>                  5      n[@        RB                  " TRE                  5       5      [@        RB                  " T RF                  RI                  5       5      /nTRJ                  (       a=  URM                  [@        RB                  " T RN                  RI                  5       5      5        TRQ                  U5      I S h  vN   TRS                  5         TRT                  (       aF  [V        RX                  RZ                  " U6 I S h  vN   UR                  [        R\                  S5        g S m+S m,S0U UU+4S jjm&/ m-/ m.Sm*[V        R^                  " [`        S9    S1U&U'UUU*U U-U.U/4	S	 jj5       n	/ n
T-RM                  [@        Rb                  " U	" U
5      S
S95        [V        RX                  Rd                  Rg                  TRh                  S5      nUu  nm(T.RM                  U5        / m)S2U(U)4S jjnT-RM                  [@        Rb                  " U" 5       SS95        S3U U4S jjnS4U4S jjn[k        T R                  TUTRl                  UUUS9u  nnTRQ                  / T-Q5      I S h  vN   UR                  [        R\                  TRT                  5        UR                  [        Rn                  [p        Rr                  " T) Vs/ s H  nURu                  SS1S9PM     sn5      5        T'b  TRQ                  [@        RB                  " T'Rw                  5       5      /5      I S h  vN   T R                  Ry                  S5        T Rz                  (       a,  T Rz                  R}                  [~        R~                  " 5       S9  T R                  (       a  T R                  5         UR                  [        R\                  TRT                  5        [~        R~                  " 5       m,        S5U+U,4S jjn[        U
5      S:  a  U
S   OSu  nnnU(       a  UR                  OSnTRT                  (       Ga/  [V        RX                  RZ                  " T-6 I S h  vN   U(       Ga  T'b  T'R                  5         T'Rw                  5       I S h  vN nUR                  nUbX  UR                  R                  5       (       a9  UR                  R                  5       (       d  UR                  b  UR                  nOSnSnT R                  R                  R                  (       aF  UR                  I S h  vN nT R                  R                  UR                  U[        US-  5      US9  O5T*(       a.  U(       a'  UR                  (       d  [`        R                  " S5        U(       a  U(       a  U" UR                  UTRT                  S9nT R                  R                  R                  U5        TR                  U/5        T R                  R                  U5        UR                  [        R                  U5        Ub9  UR                  R                  5       (       d  UR                  R                  5         T. H  nUR                  5       I S h  vN   M     TR                  5         TRT                  (       a(  [V        RX                  R[                  U5      I S h  vN   g UR                  R                  U 4S j5        T R                  R                  T5         UI S h  vN   T R                  R                  T5        [        UR2                  5      S:  Ga  T=R                  S -  sl_        / nSn[        / / S!9nS n Sn!UR2                   GH  n"UR                  RM                  U"R                  5        UR                  RM                  U"R                  5        U"R                  b  URM                  U"R                  5        U"R                  (       a	  SnSUlf        T R                  R                  R                  U"R                  5        T R                  R                  U"R                  /5        U b%  U"R                  b  [`        R                  " S"5        Sn!U"R                  n GM     U (       a  U!(       d  SUlj        T R                  R                  S#U5        T R                  n#UR                  (       a+  U (       a$  U!(       d  T R                  R                  U 5        Sn#[        U5      S:  Ga  T R                  (       d  T R                  (       a  T R                  (       aC  T R                  R                  5       (       d$  T R                  TLa  T R                  I S h  vN   O[@        R                  " S5      I S h  vN   T R                  (       a  M  T R                  (       a  M  T R                  R                  R                  5       n$U$R                  R                  U5         T R                  R                  U$5      I S h  vN   UR                  (       a  T R                  R                  R                  (       dz  T R                  R                  5         T R                  T R                  T[        U#(       d  TRl                  S':X  a  S'OS(S)9SS*9TS+S,9  T R                  T[        R                  SS-9  g T R                  R                  R                  (       aJ  UR                  (       d8  U(       a0  [`        R                  " S.T R                  GR                    S/35        g g g g g  G
N G
Ng G	N
s  snf  GNU GN) GN GNX GN GN GNr! T R                  R                  T5        f = f GN GN GNo! [        R                   a+  n%[`        R                  " S$S%[        U%5      0S&9   S n%A%GNS n%A%ff = f7f)6Nr,  r  zllm is not a realtime modelchatTc                P  >  U R                  5       =(       d    [        R                  " 5       mTR                  R	                  STTR
                  S9  TR                  (       a  TR                  R                  TS9  TR                  (       a  STl	        gg! [         a     gf = fr;  r>  rA  s    ra   rC  EAgentActivity._realtime_generation_task_impl.<locals>._on_first_frame  rE  rF  Fr   c           
       >	#    [        TR                  [        R                  5      (       d   e/ n TR                    S h  vN n[	        U5      S:  a  [
        R                  " S5          GOUR                  I S h  vN nS nSU;  a  TR                  (       a  TR                  R                  R                  (       a  [
        R                  " S5        [        R                  R                  R                  UR                  S5      nUu  pFTR!                  U5        OUR                  R#                  5       nS nTGb  S nUGb'  [%        TR&                  R(                  UTTR*                  R,                  R.                  TR                  (       a  TR                  R0                  OS TR                  (       a  TR                  R2                  OS S9u  pTR4                  (       ah  TR                  =n(       aU  UR                  R6                  (       d  UR                  R8                  (       d  U
R:                  I S h  vN =n(       a  UnSmTR!                  U	5        U
R<                  nOSU;   aN  TR&                  R?                  UR@                  T5      n[B        RD                  " U5      (       a
  UI S h  vN OUnORTR                  R                  R                  (       a  [
        RF                  " S5        O[
        R                  " S	5        Ub8  [I        TUS
9u  pUR!                  U5        URJ                  RM                  T5        TR&                  RO                  UT5      n[B        RD                  " U5      (       a
  UI S h  vN OUnS nUb  [Q        TUS9u  nnUR!                  U5        U(       d"  U(       a  URR                  RM                  T5        U R!                  UUU45        GM  [B        RT                  " U6 I S h  vN   [        R                  RV                  " U6 I S h  vN   g  GN GN GN GN N
 NQ N: N! [        R                  RV                  " U6 I S h  vN    f = f7f)Nr   zEexpected to receive only one message generation from the realtime APIr  zJtext response received from realtime API, falling back to use a TTS model.r   rG  TzText message received from Realtime API with audio modality. This usually happens when text chat context is synced to the API. Try to add a TTS model as fallback or use text modality with TTS instead.zGaudio output is enabled but neither tts nor realtime audio is availablerJ  rL  ),rp   r   r   message_streamrh  r   r   
modalitiesr   r   r  r   r  rZ  r[  text_streamrl   	__aiter__rF   r   r\  r   r   r]  r  r  r   r^  r   r_  r`  realtime_audio_output_nodeaudio_streamrn   rc  rr   rB   ra  rN  rb  rD   rd  ro   r  )ru   forward_tasksr  msg_modalitiesr  r[  tr_text_inputrt  realtime_audio_resultrq  ru  r   rv  realtime_audior  rw  rx  ry  rC  r  r  r  r  rs   rt   teesrM  s                     ra   _read_messagesDAgentActivity._realtime_generation_task_impl.<locals>._read_messages  s    
 dhh(9(9::::57M_@!.!=!= Z?#=)A-c +.>>%9N@DNn48800=="NN l $ii1155cooqI8;5C((+(A(A(C !%I#/VZ-)55J%)[[%9%9&4/=040E0E0Y0Y8<dhhnnd>Bhh):):D62H !% ? ?,0HH_S_$'$4$4$G$G+.+;+;+E+E:F:V:V4V%V[%V0;;? 8!LL24@4I4I1$6-1[[-S-S # 0 0..N
 $+#6#6~#F#F '5 4 4%3 2
 "XX22??"LL!l #NN i 1<6N-9+@73L *00>%55GGX #kk<<]N[G6=6I6I'6R6R7]]X_N37H%11H(3#12.h &,,\:$ //AA/RNNC9#=>nnm444ii//???}Z? &:D 5W !52 &3] ">x 5?eii//???s   .Q5Q
 QP6Q8Q
 >P9?GQ
  P<A9Q
 :P?;CQ
 QA?Q
 QQ
  Q50Q1Q56Q9Q
 <Q
 ?Q
 Q
 Q
 Q
 Q5
!Q2+Q.,Q22Q5z/AgentActivity.realtime_generation.read_messagesr3  r   c                 P   >#    T  S h  vN n TR                  U 5        M   N
 g 7fr   )rl   )fncfnc_stream_for_tracingr  s    ra   _read_fnc_streamFAgentActivity._realtime_generation_task_impl.<locals>._read_fnc_stream0  s&     3 +c%%c*+3s   &$"$&$&z1AgentActivity.realtime_generation.read_fnc_streamc                   > TR                  U /5        TR                  R                  R                  U 5        TR                  R                  U /5        g r   )rl  r   r  r  r   r  )r  rs   r   s    ra   r  PAgentActivity._realtime_generation_task_impl.<locals>._tool_execution_started_cb;  s?    %%xj1KK!!..x8MM++XJ7r`   c                b   > U R                   (       a  TR                  U R                   /5        g g r   r  r  s    ra   r  RAgentActivity._realtime_generation_task_impl.<locals>._tool_execution_completed_cb@  r  r`   r  r}  r   )excluder  r  c                   > 0 nT(       a  T(       a
  TUS'   TUS'   [         R                  " SU/U US9nTb  TUl        X4l        U$ )Nr  r  rO  )r  r  r
  r  )r   r  r   ry  )
message_idr|  r  r~  r  r  r  s        ra   _create_assistant_messageOAgentActivity._realtime_generation_task_impl.<locals>._create_assistant_messageg  s_     46"':;N!"78;N!"78// '('	C #.!4+KJr`   r   )NNNr  i  )r  r  audio_end_msaudio_transcriptr  )r  r|  r  c                :   > TR                   R                  S5      $ )Nr  )r   r  rI  s    ra   r8  >AgentActivity._realtime_generation_task_impl.<locals>.<lambda>  s    dmm77
Cr`   r*   r  z;expected to receive only one Agent from the tool executionsr  zJfailed to update chat context before generating the function calls resultsrr   r	  r  r  r#  )r   r  r  r  r'  r  z*Tool reply cannot be prevented when using z#, it generates reply automatically.rQ  )ru   zGlist[tuple[MessageGeneration, _TextOutput | None, _AudioOutput | None]]rz   r{   ry   r  r  )r  r   r|  r   r  boolrz   r   )r   r  r2  r  r#   rR  r
  r   r  r  r  r+   r  r   rp   r   r   set_attributesATTR_GEN_AI_OPERATION_NAMEATTR_GEN_AI_PROVIDER_NAMEr  ATTR_GEN_AI_REQUEST_MODELr  r   response_idrv   r	  r  rS  rT  r  r   rn   rU  rV  r   rc  r   rl   r   rW  rX  r  r   r  r  rY  ro  r   rM  rZ  r[  r  rE   r   ATTR_RESPONSE_FUNCTION_CALLSjsondumps
model_dumprf  r  r   r  r[  r   r  rh  r  rg  playback_positionra  r=  re  rh  r   message_truncationr  truncater  intr   r   r  r  rl  r  ri  r%  rm   r  first_tool_started_futrN  r   r  r  r  r8   r  r  r  r  r  r  r  r  rr   r  r  r   r  r   r   re  r   r  r  r  r  r  r   auto_tool_reply_generationr  rR  r  r-   r  rK   r  _label)0rs   r   r  r  r  r  r  r,  rn  r  message_outputsfnc_tee
fnc_streamr  r  r  r  r  r	  r  msg_genry  rt  r|  r}  r$  r  r  r[  r  generate_tool_replyr  r  r  r  r  r   r  rC  r  r
  r  r  r  r  rt   r  rM  s0   ````                                  @@@@@@@@@@ra   r  ,AgentActivity._realtime_generation_task_implf  s     --m6W6WX"";#=#=}?O?OP--((w||//11'll6T6TU+J-JJ+$((C$5$566U8UU6##6655txx7H7H55txx~~	
 +0I0I>JD  !:!:;59]]5I5I5W5Wt}}++11]a }}##99 MM  .. 	
 ??4::. !!-"G"G"IJ!!$"="="B"B"DE:
 ,,&&w'<'<T=U=U=Z=Z=\']^334GHHH**,$$))++-@AAA&&{'J'JDQ,0,0	E 	E, *,35#(  
		V	,f	@\f	@f	@ f	@ 
-f	@T  	 	/F	
 ))%%))-*G*GK-4*
*G13	+ 	+ 	 "H	
	8 	8
	> !8MM'&22&&@(D!
+ 33HeH==="";#F#FHaHab""44JJR`aR`3/EFR`ab	

 #77&&|'D'D'FGH   MM--k:&&''>>15 ?  ((<<>&&33]5N5N #iik		-0	?C		 	* #&o"6":OA@R 	%9 +3$$$))++U333<3))+$0$A$A$CC$/$A$A!)!116688%55??AA #::F)4)L)L%'N()% 88((;;+2+=+=%=N$$--#*#5#5#1%():T)A%B)7	 .  &(8==NNg ~+"---)55C
 KK!!..s3%%se,MM2237&&{'E'E~V )B)B)G)G)I)I%%,,.C**, ++-$$))++H555 	**<<C	
 	!!%%m4	=NN%%--m< {!!"Q&$$)$<>O(-8!O ,0N!&!,!3!3..55m6L6LM55<<]=W=WX --9#**=+E+EF$33.2+:>7 KK))66}7Q7QRMM33]5O5O4PQ!--2J2J2VLLU *.&!.!9!9+ "4. &8481MM8/J--H00^L^**>:?#a' **dnn,, $ 4 4 9 9 ; ; 00E"2222%mmA... ***dnnn  ++4499;%%o6**::8DDD  //--HH  **,((--&3'4  (>+E+E+O )/!'( $( . 
 #07 )  %%!<#F#Fd &  %%@@'77'@@QQtu ( 8 Ay 'm	 	I Bt 	>
 bT 4
 D  &>8  6 %%--m<r 3.
 E(( NNd&A/ sH  K*x2:v$;Ax2 v'Ex2 v*!Ax2=v-Ax2v2D.x2v52x2?v8 B#x2#v;$Ex2 v>Ax2w?x2w ww I?x2w'x25w*6x2x2 ?x2 w0 >w-?w0 D"x2'x2*x2-x25x28x2;x2>x2x2w w$$x2*x2-w0 0x/ x*$x2*x//x2c                   ^ ^ T R                   b  T R                   R                  5         SU U4S jjnT R                  R                  R	                  TU5      T l         g )Nc                 B  > TR                   b*  TR                  (       a!  TR                  TR                   La  S Tl         g Sn TR                  R                  R                  S   (       Ga  TR                  R
                  R                  =n(       a  UR                  (       a  TR                   R                  5       (       d  TR                  R                  STR                   R                  S9  TR                  (       a,  TR                  R                  [        R                  " 5       S9  TR                  (       a  STl        UR!                  5         Sn ["        R$                  " SST0S	9  TR                  R'                  S
[)        U S95        S Tl         S Tl        g )NFr  r  )r+  r  Tz resumed false interrupted speechr  r	  agent_false_interruption)resumed)r   r   r   r   r   rv   r  r  r=  r  r2  r   r@  r[  r   r   r  r   r  r  r6   r   )r4  r  rs   r  s     ra   _on_false_interruptionMAgentActivity._start_false_interruption_timer.<locals>._on_false_interruption.  sN   ""*$$)=)=TEXEX)X '+#G%%223NOO%)]]%9%9%?%??\? **++002211!%!4!4!H!H 2  **++DDPTPYPYP[D\,,CHD@##%?	SZG[\MM*,GPW,X #'D-1D*r`   ry   )r   r%  r   _loop
call_later)rs   r  r5  s   `` ra   r  -AgentActivity._start_false_interruption_timer*  sQ    ))5**113 	2  	2D *.)<)<)G)G+*
&r`   )r  c               0  #    Ub   UI S h  vN   U R                  b!  U R                  R	                  5         S U l        U R
                  (       d  g U(       a  U R
                  R                  (       dr  U R
                  R                  (       aW  U R
                  R                  5         U R
                  R                  (       a"  U R
                  R                  5       I S h  vN   S U l        U R                  R                  R                  S   (       aU  U R                  R                  R                  (       a/  U R                  R                  R                  R!                  5         g g g  GN^! [          a    [        R                  " S5         GNf = f N7f)Nz3previous _cancel_speech_pause task failed, ignoringr  )rq   r   r  r   r%  r   r  r   r  ro  rg  r   r   r   rv   r  r  )rs   r  r  s      ra   r  "AgentActivity._cancel_speech_pauseT  s4     T ))5**113-1D*"" ''33##77))+ ""//))>>@@@" MM!!../JK$$**MM  &&--/ + L9  T RS	T. AsE   FE- E*E- CF F!B	F*E- - FFFFc                &    U R                   U l        g r   )r   r   r   s    ra   r  5AgentActivity._restore_interruption_by_audio_activityz  s    @@ 	4r`   c                   U R                   (       d  gSU l         U R                  5         [        U R                  [        R
                  5      (       ar  U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  (       a  U R                  R                  S5        [        R                  " S5        g)a0  Degrade gracefully from adaptive interruption to VAD-based interruption.

Called when the adaptive interruption detector encounters an unrecoverable error.
Re-enables audio-activity interruption so VAD events can trigger interruptions,
and flushes any held transcripts that were waiting on the detector.
NFr{  rr   r  zaadaptive interruption disabled due to unrecoverable error, falling back to VAD-based interruption)r   r  rp   r   r   r  r  r  r  r  r   update_interruption_detectionr   r   r   s    ra   r  +AgentActivity._fallback_to_vad_interruption  s     33/4,446d1193Y3YZZ''++,?A[A[\''++GT^^D''++,@$B_B_`""##AA$G5	
r`   c                    [        U R                  R                  5      (       a  U R                  R                  $ U R                  R                  $ r   )r)   r   r   r   r   s    ra   r   AgentActivity.vad  1    "*4;;??";";t{{RARARRr`   c                F   U R                   b  U R                   R                  R                  (       ak  U R                   R                  R                  (       aF  U R                  b9  U R
                  S;  a)  [        U R                  [        R                  5      (       a  [        U R                  R                  5      (       a  U R                  R                  S:X  d>  [        U R                  R                  5      (       a0  U R                  R                  S:X  a  [        R                  " S5        g U R                  (       d  g [        U R                  R                  5      (       a  U R                  R                  S:X  a  g [        U R                  R                  5      (       a  U R                  R                  S:X  a  g [        U R                  R                  5      (       do  [        U R                  R                  5      (       dK  [         R"                  " 5       (       d1  [         R$                  " 5       (       d  [        R&                  " S5        g  [(        R*                  " 5       nU$ ! [,         a*  n[        R                  " SS[/        U5      0S9   S nAg S nAff = f)	Nr   adaptivezointerruption_detection is provided, but it's not compatible with the current configuration and will be disabledr   z?adaptive interruption is disabled by default in production modez-failed to create AdaptiveInterruptionDetectorrr   r	  )r   r   r^  r   r   r   rp   r   r   r)   r   r  r   r   r   r   r   	is_hostedis_dev_moder   r   r  r   r   )rs   detectorr  s      ra   r   -AgentActivity._resolve_interruption_detection  s   HH %%88%%//$$$,FFtxx):):;; ;;<<KK66*D==>>MM88JF F '' T[[778822e;T]]99::44= ;;<<T]]AABBOO%%%%''KKYZ	 ==?H
 	  	NNJSZ\_`a\bRcd	s   I, ,
J 6 JJ c                    [        U R                  R                  5      (       a  U R                  R                  $ U R                  R                  $ r   )r)   r   r   r   r   s    ra   r   AgentActivity.stt  rC  r`   c                    [        U R                  R                  5      (       a  U R                  R                  $ U R                  R                  $ r   )r)   r   r   r   r   s    ra   r   AgentActivity.llm  rC  r`   c                    [        U R                  R                  5      (       a  U R                  R                  $ U R                  R                  $ r   )r)   r   r   r   r   s    ra   r   AgentActivity.tts  rC  r`   )$r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rW   r,   r   rO   rz   r{   )r   TurnDetectionMode | Nonerz   rP  )rz   r  )rz   rO   )rz   r,   )rz   zlist[mcp.MCPServer] | None)rz   rL   )rz   r   )rz   rg   )rz   SpeechHandle | None)rz   r   )r  r   rz   r{   )r   r   rz   r{   )r   r   r  r  rz   r{   )r   z!NotGivenOr[llm.ToolChoice | None]r   zNotGivenOr[EndpointingOptions]r   z$NotGivenOr[TurnDetectionMode | None]r   NotGivenOr[float]r   rR  rz   r{   )r/  zCoroutine[Any, Any, Any]r   rQ  r(  
str | Nonerz   zasyncio.Task[Any])rU  _ReusableResources | Nonerz   r{   )r  rP   rz   rd   )r  AgentActivity | Nonerz   rT  )r  zlist[asyncio.Task] | Nonerz   r{   ry   )r  zlist[asyncio.Task]r  rU  rz   rT  )r  zrtc.AudioFramerz   r{   )r  zrtc.VideoFramerz   r{   )
r  str | AsyncIterable[str]r  z)NotGivenOr[AsyncIterable[rtc.AudioFrame]]r   NotGivenOr[bool]r  r  rz   rK   )r   z"NotGivenOr[llm.ChatMessage | None]r   z"NotGivenOr[llm.ChatContext | None]r  zNotGivenOr[str | Instructions]r   zNotGivenOr[llm.ToolChoice]r   zNotGivenOr[list[str]]r   rW  r  r  r  rJ   rz   rK   )F)r  r  rz   zlist[SpeechHandle])r  r  rz   zasyncio.Future[None])rR  r   rS  r   rO  r  rz   zasyncio.Future[str])r@  rK   r_  r'  r  r  rz   r{   )r  zHSTTMetrics | TTSMetrics | VADMetrics | LLMMetrics | RealtimeModelMetricsrz   r{   )r  zllm.RemoteItemAddedEventrz   r{   )rr   zjllm.LLMError | stt.STTError | tts.TTSError | llm.RealtimeModelError | inference.InterruptionDetectionErrorrz   r{   )r  z inference.OverlappingSpeechEventrz   r{   )r6  zllm.InputSpeechStartedEventrz   r{   )r  zllm.InputSpeechStoppedEventrz   r{   )r  zllm.InputTranscriptionCompletedrz   r{   )r  llm.GenerationCreatedEventrz   r{   )r  zfloat | Nonerz   r{   )r  zvad.VADEvent | Nonerz   r{   )r  zvad.VADEventrz   r{   )r  zstt.SpeechEventr  zbool | Nonerz   r{   )r   r3   rz   r{   )r   r2   rz   r  )r  asyncio.Task[None] | Noner   r2   rz   r{   )rz   r   )r6  zasyncio.Task[None]rz   r{   )r   rK   r  rV  r  z$AsyncIterable[rtc.AudioFrame] | Noner  r  r  r-   rz   r{   )r   rK   r   r   r   r   r  r-   r  zllm.ChatMessage | Noner  zstr | Instructions | Noner  zllm.MetricsReport | Noner  z:Sequence[llm.FunctionCall | llm.FunctionCallOutput] | Nonerz   r{   )r   rK   r  r-   r   z#list[llm.Tool | llm.Toolset] | Noner  rS  r  rS  r  r  r  zstr | AsyncIterable[str] | Nonerz   r{   )
r   rK   r  rX  r  r-   r  rS  rz   r{   )r  r   rz   r{   r   )r  rY  r  r  rz   r{   )rz   zvad.VAD | None)rz   z-inference.AdaptiveInterruptionDetector | None)rz   zstt.STT | None)rz   z"llm.LLM | llm.RealtimeModel | None)rz   ztts.TTS | None)_rZ   r[   r\   r]   r   r   propertyr   rV   rW   r   r   r   r   r   r   r   r   r   r   r   rH   r  r  r&   r$  rR  rq  r  rl  r$   rm  r  r  r  r  rH  r  r  rm   r  r  r  rI   r3  r  r9  r<  r  r  rM  rU  r  r   ro  r   r  ru  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r&  r  r  r3  r(  r  r  r  r  r  r  r  r  r   r   r   r   r   r_   rX   r`   ra   rP   rP      sT   P=dI6I	!IV ' '     4 4 
 
 
 
 
 
 2 2 2 2     $ $ I	%I I
 
 
 . ."P8 TX'LP	" :C;D?H3<3<3 73 9	3
 =3  13  13 
3r .23&3 +	3
 3 
3j KO /!b?B TX E,N !!"896:&3&	"& :&R =A9 99	9$

 LP  .2	# *# +	#
 
##J:.x)*O./ <E09 $L&L 9	L
 .L L 
Lb <E7@7@2;'009 $&;r 9r 5	r
 5r 0r %r .r r $r 
rh/
,*$ */ #J+ [`
%*
@E
SW
	
((> (6 )6p8
T
 

,>'/' 
'B5$
8K> ?CL1/;L1	L1`2(:<+4
>: SW "
H/
b+Z ({\1{\9G{\	{\ ){\|$
? (# ' 4	
  & 
 )0B/#B/ 'B/ 4	B/
 B/ &B/ 
B/H ( /326;?_c $ "	
 , & , 0 !9 #] 
 )L /326;?_cs? $s? "	s?
 ,s? &s? ,s? 0s? !9s? #]s? 
s?j ( 6:!%#' 04g> $g> &	g>
 3g> g> !g> g> .g> 
g> )g>R ( $( $ 2	
 & ! 
 ): $(B $B 2	B
 &B !B 
BH(
V 59$0OS$01$0HL$0	$0L


6 S S2h S S S S S Sr`   )s
__future__r   rn   contextvarsrY  r!  r[  collections.abcr   r   r   dataclassesr   typingr   r	   opentelemetryr
   r+  r   livekitr   livekit.agents.llm.realtimer   livekit.agents.metrics.baser   r  r   r   r   r   r   r   llm.chat_contextr   llm.tool_contextr   r   r   logr   ry  r   r   r   r   r    r!   	telemetryr"   r#   r$   r  tokenize.basicr%   typesr&   r'   r(   
utils.miscr)   _utilsr+   rW   r,   r-   r.   r/   audio_recognitionr0   r1   r2   r3   r4   r   r5   eventsr6   r7   r8   r9   r:   r;   r<   
generationr=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   r   rI   rJ   rK   turnrL   rM   rN   agent_sessionrO   
ContextVarrJ  rK  rT   r]  rd   r}   rP   rX   r`   ra   <module>rr     sb   "      > > ! % 8  9 0 3 3 + 
   P O ( 8 8 ! /   ,      M L 7+ '11/BCTU %00@AWX    
 !++N;<VW  Z Z Z(   Z5S$ Z5Sr`   