
    /j
                      % S SK J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
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Jr  S S	KJr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(J)r)  SSK*J+r+  SSK,J-r-  SSK%J.r.J/r/J0r0  SSK1J2r2  SSK3J4r4  SSK5J6r6J7r7  SSK8J9r9J:r:  SSKJ;r;J<r<J=r=J>r>  SSK?J@r@  SSKAJBrB  SSK"JCrCJDrD  SSKEJFrF  SSKGJHrHJIrI  SSKJJKrKJLrL  SSKMJNrN  SSKOJPrPJQrQJRrRJSrSJTrTJUrUJVrVJWrWJXrXJYrY  SSKZJ[r[  SS K\J]r]  SS!K^J_r_J`r`  SS"KaJbrb  SS#KcJdrdJere  SS$KfJgrgJhrhJiriJjrjJkrkJlrlJmrmJnrnJoro  \(       a  SS%K$JprpJqrqJrrr  SS&K%Jsrs  SS'KtJuru   " S( S)\S*S+9rvS,S,S,S,S-.rwS)\xS.'   S*S*S*S*S-.ryS)\xS/'   SAS0 jrz\ " S1 S25      5       r{\ " S3 S45      5       r|\" S55      r}\" S65      r~\ " S7 S8\5      5       r " S9 S:5      rS;S</rS=\xS>'    " S? S@\!GR                  \V   \\}   5      rg)B    )annotationsN)AsyncIterableCallableSequence)AbstractContextManagernullcontext)Token)	dataclass)TracebackType)TYPE_CHECKINGGenericLiteralProtocolTypeVaroverloadruntime_checkable)contexttrace)	TypedDict)rtc   )cli	inferencellmsttttsutilsvad)APIError)get_job_context)AgentHandoffChatContextMetricsReport)Instructionslogger)AgentSessionUsageModelUsageCollector)trace_typestracer)DEFAULT_API_CONNECT_OPTIONS	NOT_GIVENAPIConnectOptions
NotGivenOr)deprecate_params)is_given   )ioroom_io)_set_participant_attributes)Agent	AgentTask)AgentActivity_ReusableResources)AMD)

AgentEvent
AgentStateAgentStateChangedEvent
CloseEventCloseReasonConversationItemAddedEvent
EventTypesUserInputTranscribedEvent	UserStateUserStateChangedEvent)IVRActivity)
RecorderIO)RoomSessionTransportSessionHost)	RunResult)InputDetailsSpeechHandle)	EndpointingOptionsInterruptionOptionsPreemptiveGenerationOptionsTurnDetectionModeTurnHandlingOptions_migrate_turn_handling_resolve_endpointing_resolve_interruption_resolve_preemptive_generation)	LLMModels	STTModels	TTSModels)mcp)TextTransformsc                  H    \ rS rSr% SrS\S'    S\S'    S\S'    S\S'   Srg	)
RecordingOptionsR   u  Granular control over which recording features are active.

All keys default to ``True`` when not specified, so ``{"logs": False}``
means "record everything except logs."

Can be passed directly to :pymethod:`AgentSession.start(record=...)`:

* ``record=True``  → all on (backward compatible)
* ``record=False`` → all off (backward compatible)
* ``record={"audio": True, "traces": False}`` → granular
boolaudiotraceslogs
transcript N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__ra       S/app/agent/.venv/lib/python3.13/site-packages/livekit/agents/voice/agent_session.pyrZ   rZ   R   s*    
 K5LA
J:Rri   rZ   F)totalT)r]   r^   r_   r`   _RECORDING_ALL_ON_RECORDING_ALL_OFFc                    [        U [        5      (       a  U (       a  [        O[        n[	        S0 UD6$ [	        S0 0 [        EU ED6$ )Nra   )
isinstancer\   rl   rm   rZ   )recorddefaultss     rj   _resolve_recording_optionsrr   w   sB    &$(.$4F+(++>=!2=f=>>ri   c                  N    \ rS rSr% \rS\S'   \rS\S'   \rS\S'   Sr	S\S'   S	r
g
)SessionConnectOptions~   r-   stt_conn_optionsllm_conn_optionstts_conn_options   intmax_unrecoverable_errorsra   N)rb   rc   rd   re   r+   rv   rg   rw   rx   r{   rh   ra   ri   rj   rt   rt   ~   s4    *E'E*E'E*E'E$%c%Mri   rt   c                      \ rS rSr% S\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'   S\S'   S\S'   S\S'   \SS j5       r\SS j5       r\SS j5       rSr	g)AgentSessionOptions   rO   turn_handlingrz   max_tool_stepsfloat | Noneuser_away_timeoutfloatmin_consecutive_speech_delayzbool | Noneuse_tts_aligned_transcriptzSequence[TextTransforms] | Nonetts_text_transformsr\   ivr_detectionaec_warmup_duration session_close_transcript_timeoutc                     U R                   S   $ )Nendpointingr   selfs    rj   r   AgentSessionOptions.endpointing   s    !!-00ri   c                     U R                   S   $ )Ninterruptionr   r   s    rj   r    AgentSessionOptions.interruption   s    !!.11ri   c                     U R                   S   $ )Npreemptive_generationr   r   s    rj   r   )AgentSessionOptions.preemptive_generation   s    !!"9::ri   ra   N)returnrK   )r   rL   )r   rM   )
rb   rc   rd   re   rg   propertyr   r   r   rh   ra   ri   rj   r}   r}      sr    &&##"'' ++88%%&++1 1 2 2 ; ;ri   r}   
Userdata_TRun_Tc                      \ rS rSrSS jrSrg)_VideoSampler   c                    g Nra   )r   framesessions      rj   __call___VideoSampler.__call__   s    cri   ra   Nr   zrtc.VideoFramer   AgentSessionr   r\   )rb   rc   rd   re   r   rh   ra   ri   rj   r   r      s    Qri   r   c                  2    \ rS rSrSSS.S	S jjrS
S jrSrg)VoiceActivityVideoSampler         ?333333?speaking_fps
silent_fpsc               *    Xl         X l        S U l        g r   )r   r   _last_sampled_time)r   r   r   s      rj   __init__"VoiceActivityVideoSampler.__init__   s    ($04ri   c                    [         R                   " 5       nUR                  S:H  nU(       a  U R                  OU R                  nUS:X  a  gSU-  nU R                  c  X0l        gX0R                  -
  U:  a  X0l        gg)Nspeakingr   Fr   T)time
user_stater   r   r   )r   r   r   nowis_speaking
target_fpsmin_frame_intervals          rj   r   "VoiceActivityVideoSampler.__call__   sx    iik((J6*5T&&4??
? :-""*&)#))).@@&)#ri   )r   r   r   N)r   r   r   r   r   )rb   rc   rd   re   r   r   rh   ra   ri   rj   r   r      s    03 5
ri   r   filter_markdownfilter_emojizlist[TextTransforms]DEFAULT_TTS_TEXT_TRANSFORMSc                     ^  \ rS rSr\" SSSSSSSSSSSS.SS9\\\\\\\S\\S\\SS	S
S\S\\\\\\\\\\\S.                                                             SaU 4S jjj5       r\SbS j5       rScSdU 4S jjjr	SeU 4S jjr
\SfS j5       r\R                  SgS j5       r\ShS j5       r\SiS j5       r\SjS j5       r\SkS j5       r\SlS j5       r\SmS j5       r\SnS j5       r\SoS j5       r\SpS j5       r\SqS j5       r\SrS j5       r\SsS j5       r\StS  j5       rS!SS".       SuS# jjr\\\S$\\S%.               SvS& jj5       r\S	\\S$\\S'.               SwS( jj5       rS	\\\\\S'.               SxS) jjrSyS* jr\SzS+ j5       rS	SS,.       S{S- jjrS$S..S|S/ jjr \!RD                  " \#S09S	SS,.       S}S1 jj5       r$SyS2 jr%\\\\S3.         S~S4 jjr&ScSS5 jjr'\\S$S6.         SS7 jjr(\\\\\\S!S8.               SS9 jjr)S	S:.SS; jjr*SyS< jr+SSS	S=.       SS> jjr,SS? jr-SyS@ jr.SASBSS$SC.           SSD jjr/\!RD                  " \#S09      SSE j5       r0    SSF jr1\!RD                  " \#S09SySG j5       r2\!RD                  " \#S09SySH j5       r3SySI jr4SySJ jr5SySK jr6SSSL.       SSM jjr7SSN.     SSO jjr8SSP jr9SSQ jr:SSR jr;SSS jr<\SST j5       r=\SSU j5       r>\SSV j5       r?\SSW j5       r@\SSX j5       rASySY jrBSySZ jrCSyS[ jrDSyS\ jrESyS] jrFSS^ jrG        SS_ jrHS`rIU =rJ$ )r      z2Use turn_handling=TurnHandlingOptions(...) instead)min_endpointing_delaymax_endpointing_delayfalse_interruption_timeoutresume_false_interruptionallow_interruptions discard_audio_if_uninterruptiblemin_interruption_durationr   min_interruption_wordsturn_detection agent_false_interruption_timeoutzv2.0)target_versionry                 @Fg      .@g       @N)r   r   r   r   r   toolsmcp_serversr   r   r   r   userdatavideo_samplerr   r   r   r   conn_optionsloopr   r   r   r   r   r   r   r   r   r   r   c                 > [         T#U ]  5         U=(       d    [        R                  " 5       U l        [        U5      (       a  UO	[        SSS9U l        [        U5      (       d7  [        [        U5      (       a  UOS[        U5      (       a  UOSUUUUUUUUUS9OUn[        UR                  S5      5      n[        UR                  S5      5      n [        UR                  S	5      5      n!UR                  S
S5      n"[        R                  " 5       U l        [!        [#        UU U"U!S9UUU[        U
5      (       a  U
O[$        U[        U	5      (       a  U	OSUUS9	U l        U=(       d
    [)        5       U l        SU l        [/        U[0        5      (       a  [2        R4                  R7                  U5      n[/        U[0        5      (       a  [2        R8                  R7                  U5      n[/        U[0        5      (       a  [2        R:                  R7                  U5      nU=(       d    SU l        U=(       d    SU l        U=(       d    SU l         U=(       d    SU l!        U"U l"        U R                  S[F        5      U l$        U=(       d    SU l%        [        U5      (       a  UO/ U l&        SU l'        SU l(        U=(       d    SU l)        SU l*        [V        RX                  " U RZ                  U R\                  U R^                  S9U l0        [V        Rb                  " U Rd                  U Rf                  U Rh                  5      U l5        SU l6        SU l7        SU l8        [        Rr                  " 5       U l:        [        Rr                  " 5       U l;        SU l<        SU l=        SU l>        SU l?        SU l@        SU lA        SU lB        SU lC        SU lD        [        U5      (       a  UOSU lE        SU lF        SU lG        SU lH        SU lI        SU lJ        SU lK        SU lL        SU lM        SU lN        SU lO        / U lP        [        R                  5       U lS        SU lT        [        5       U lV        SU lW        SU lX        g)aX  `AgentSession` is the LiveKit Agents runtime that glues together
media streams, speech/LLM components, and tool orchestration into a
single real-time voice agent.

It links audio, video, and text I/O with STT, VAD, TTS, and the LLM;
handles turn detection, endpointing, interruptions, and multi-step
tool calls; and exposes everything through event callbacks so you can
focus on writing function tools and simple hand-offs rather than
low-level streaming logic.

Args:
    stt (stt.STT | str, optional): Speech-to-text backend.
    vad (vad.VAD, optional): Voice-activity detector
    llm (llm.LLM | llm.RealtimeModel | str, optional): LLM or RealtimeModel
    tts (tts.TTS | str, optional): Text-to-speech engine.
    tools (list[llm.FunctionTool | llm.RawFunctionTool], optional): List of
        tools shared by every agent in the agent session.
    mcp_servers (list[mcp.MCPServer], optional): List of MCP servers
        providing external tools for the agent to use.
    userdata (Userdata_T, optional): Arbitrary per-session user data.
    turn_handling (TurnHandlingOptions, optional): Configuration for turn handling.
    max_endpointing_delay (float): Maximum time-in-seconds the agent
        will wait before terminating the turn. Default ``3.0`` s.
    max_tool_steps (int): Maximum consecutive tool calls per LLM turn.
        Default ``3``.
    video_sampler (_VideoSampler, optional): Uses
        :class:`VoiceActivityVideoSampler` when *NOT_GIVEN*; that sampler
        captures video at ~1 fps while the user is speaking and ~0.3 fps
        when silent by default.
    min_consecutive_speech_delay (float, optional): The minimum delay between
        consecutive speech. Default ``0.0`` s.
    use_tts_aligned_transcript (bool, optional): Whether to use TTS-aligned
        transcript as the input of the ``transcription_node``. Only applies
        if ``TTS.capabilities.aligned_transcript`` is ``True`` or ``streaming``
        is ``False``. When NOT_GIVEN, it's disabled.
    tts_text_transforms (Sequence[TextTransforms], optional): The transforms to apply
        to the tts input text, available built-in transforms: ``"filter_markdown"``, ``"filter_emoji"``.
        Set to ``None`` to disable. When NOT_GIVEN, all filters will be applied.
    ivr_detection (bool): Whether to detect if the agent is interacting with an IVR system.
        Default ``False``.
    conn_options (SessionConnectOptions, optional): Connection options for
        stt, llm, and tts.
    loop (asyncio.AbstractEventLoop, optional): Event loop to bind the
        session to. Falls back to :pyfunc:`asyncio.get_event_loop()`.
    user_away_timeout (float, optional): If set, set the user state as
        "away" after this amount of time after user and agent are silent.
        Defaults to ``15.0`` s, set to ``None`` to disable.
    aec_warmup_duration (float, optional): The duration in seconds that the agent
        will ignore user's audio interruptions after the agent starts speaking.
        This is useful to prevent the agent from being interrupted by echo before AEC is ready.
        Set to ``None`` to disable. Default ``3.0`` s.
    session_close_transcript_timeout (float, optional): Seconds to wait for the
        final STT transcript when closing the session (after audio is detached).
        Default ``2.0`` s (independent of ``commit_user_turn``'s ``transcript_timeout``).
    preemptive_generation (NotGivenOr[bool | PreemptiveGenerationOptions]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    min_endpointing_delay (NotGivenOr[float]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    max_endpointing_delay (NotGivenOr[float]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    false_interruption_timeout (NotGivenOr[float | None]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    turn_detection (NotGivenOr[TurnDetectionMode]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    discard_audio_if_uninterruptible (NotGivenOr[bool]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    min_interruption_duration (NotGivenOr[float]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    min_interruption_words (NotGivenOr[int]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    allow_interruptions (NotGivenOr[bool]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    resume_false_interruption (NotGivenOr[bool]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
    agent_false_interruption_timeout (NotGivenOr[float | None]): Deprecated, use turn_handling=TurnHandlingOptions(...) instead.
r   r   r   g      ?r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   N)r   r   r   r   )	r   r   r   r   r   r   r   r   r   Fmoder   r   )audio_enabled_cb	listeninginitializing)Ysuperr   asyncioget_event_loop_loopr0   r   _video_samplerrP   rQ   getrR   rS   r"   empty	_chat_ctxr}   rO   r   _optsrt   _conn_options_startedro   strr   STTfrom_model_stringLLMTTS_stt_vad_llm_tts_turn_detectionr,   _interruption_detection_mcp_servers_tools_llm_error_counts_tts_error_counts_aec_warmup_remaining_aec_warmup_timerr2   
AgentInput_on_video_input_changed_on_audio_input_changed_on_audio_enabled_changed_inputAgentOutput_on_video_output_changed_on_audio_output_changed_on_text_output_changed_output_forward_audio_atask_forward_video_atask_update_activity_ataskLock_activity_lock_lock_room_io_recorder_io_session_host_agent	_activity_next_activity_user_state_agent_state_user_away_timer	_userdata_closing_task_closing_job_context_cb_registered_global_run_state_early_assistant_metrics_user_speaking_span_agent_speaking_span_session_span_root_span_context_session_ctx_token_recorded_eventsrm   copy_recording_options_started_atr(   _usage_collector_ivr_activity_amd)$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   preemptive_genraw_turn_detection	__class__s$                                      rj   r   AgentSession.__init__   s   r 	5W335
 && *L 	2 M**% # .66K-L-L)RU .66K-L-L)RU+E-1Q*C'=$7*C1Q&;!& ) 	. +=+<+<]+KL,]->->~-NO78I8IJa8bc*../?F %**,(-')1&4	 */)E /00 $0'233 (B 3-M)

, *D-B-Dc3--11#6Cc3--11#6Cc3--11#6CK4	K4	K4	K4	1'3'7'7	'J$'/4'eB "#!" &9%?C"=A mm((((!;;

 ~~))))((
 @D!?C!AE#%lln\\^
 04/315$(/348&1(6<@8@8J8JHPT8<#05'37>B% 7; 7;!04?CFJ244F4K4K4M)- 3 5 26 $	ri   c                    U R                   $ )zOThe Answering Machine Detection (AMD) instance, or ``None`` if AMD is disabled.)r  r   s    rj   amdAgentSession.amd  s     yyri   c                `   > US:X  a  Ub  [         R                  " S5        [        TU ]  X5      $ )Nmetrics_collectedz{metrics_collected is deprecated. Use session_usage_updated for usage tracking and ChatMessage.metrics for per-turn latency.)r&   warningr   on)r   eventcallbackr   s      rj   r(  AgentSession.on  s4    ''H,@NN@
 wz%**ri   c                Z   > U R                   R                  U5        [        TU ]  X5        g r   )r  appendr   emit)r   r)  argr   s      rj   r.  AgentSession.emit  s#    $$S)U ri   c                J    U R                   c  [        S5      eU R                   $ )Nz AgentSession userdata is not set)r  
ValueErrorr   s    rj   r   AgentSession.userdata  s"    >>!?@@~~ri   c                    Xl         g r   )r  )r   values     rj   r   r3    s    ri   c                    U R                   $ r   )r   r   s    rj   r   AgentSession.turn_detection  s    ###ri   c                    U R                   $ r   )r   r   s    rj   r   AgentSession.mcp_servers         ri   c                    U R                   $ r   )r   r   s    rj   inputAgentSession.input      {{ri   c                    U R                   $ r   )r   r   s    rj   outputAgentSession.output  s    ||ri   c                    U R                   $ r   )r   r   s    rj   optionsAgentSession.options  s    zzri   c                    U R                   $ r   )r   r   s    rj   r   AgentSession.conn_options  s    !!!ri   c                    U R                   $ r   )r   r   s    rj   historyAgentSession.history  s    ~~ri   c                L    U R                   b  U R                   R                  $ S $ r   )r  current_speechr   s    rj   rK  AgentSession.current_speech  s!    040Jt~~,,TPTTri   c                    U R                   $ r   )r	  r   s    rj   r   AgentSession.user_state  s    ri   c                    U R                   $ r   )r
  r   s    rj   agent_stateAgentSession.agent_state  r:  ri   c                J    U R                   c  [        S5      eU R                   $ )NzVoiceAgent isn't running)r  RuntimeErrorr   s    rj   current_agentAgentSession.current_agent  s"    ;;9::{{ri   c                    U R                   $ r   )r   r   s    rj   r   AgentSession.tools  r>  ri   c                D    [        U R                  R                  5       S9$ )zMReturns usage summaries for this session, one per model/provider combination.)model_usage)r'   r  flattenr   s    rj   usageAgentSession.usage"  s     !T-B-B-J-J-LMMri   text)input_modalityoutput_typec                   U R                   b*  U R                   R                  5       (       d  [        S5      e[        XS9nX@l         U R	                  XS9  U$ )Nnested runs are not supported)
user_inputr_  )rb  r^  )r  donerS  rH   generate_reply)r   rb  r^  r_  	run_states        rj   runAgentSession.run'  sW     !!-d6L6L6Q6Q6S6S>??M	!*zQri   T)roomroom_optionsrp   room_input_optionsroom_output_optionsc                  #    g 7fr   ra   r   agentcapture_runrh  ri  rp   rj  rk  s           rj   startAgentSession.start6  s
         )ro  rh  ri  rp   rj  rk  c                  #    g 7fr   ra   rm  s           rj   rp  rq  D  s
      rr  c               >  ^ ^^#    T R                    ISh  vN   T R                  (       a   SSS5      ISh  vN   g[        R                  " 5       T l        [	        U5      n[        SS9n	[	        U5      (       d  U	(       a  U	R                  R                  OSn[        U5      T l	        Sn
U	(       a  U	R                  b  U	R                  T L a  T U	l
        OLSn
[        T R                  R                  5       5      (       a"  U(       a  [        S5      e[        S5      T l	        U	R                  T R                  5        [        R                   " S5      =T l        nT R$                  b'  [&        R(                  " T R$                  5        ST l        [*        R,                  " U5      n[&        R.                  " U5      T l        / T l        [3        5       T l        ST l        ST l        ST l        ST l        [&        R>                  " 5       T l         [*        RB                  " 5       nURE                  [F        RH                  URJ                  5        UT l&        T RO                  S5        / n[P        RR                  RU                  5       nURV                  (       aq  URX                  (       d`  T RZ                  R\                  c  T R^                  R\                  b  [`        Rb                  " S5        URe                  T Rf                  T S	9  GO[	        U5      (       Ga  T R6                  (       Gd  [h        Rj                  Rm                  UUUS
9n[n        Rn                  " U5      nT RZ                  R\                  b.  URp                  (       a  [`        Rb                  " S5        SUl8        T R^                  R\                  b.  URr                  (       a  [`        Rb                  " S5        SUl9        T R^                  Rt                  b.  URv                  (       a  [`        Rb                  " S5        SUl;        [h        Rx                  " UT US9T l        T R6                  R{                  5       I Sh  vN   U
(       a6  [}        U5      n[        U5      T l        T R:                  R                  T 5        UR                  5       nU(       aW  T R6                  R                  UR                  5        T R:                  b%  T R:                  R                  UR                  5        U	(       Ga  T RZ                  R\                  (       Ga\  T R^                  R\                  (       Ga@  T R                  S   (       d$  URV                  (       Ga  UR                  (       Ga  [        T S9T l        T R8                  R                  T RZ                  R\                  5      T RZ                  l.        T R8                  R                  T R^                  R\                  5      T R^                  l.        URV                  (       a  UR                  (       d  URV                  (       dK  [        R                  " T R8                  R{                  U	R                  S-  S95      nUR                  U5        T R                  R                  (       a2  UR                  [        R                  " T R                  5       SS95        URE                  [F        R                  U	R                  R                  5        URE                  [F        R                  U	R                  R                  5        URE                  [F        R                  U	R                  R                  5        T R6                  (       a2  UR                  [        R                  " U	R                  5       SS95        T R                  (       d  U	R                  U 4S j5        ST lW        SnU(       aG  T R                  b*  T R                  R                  5       (       d  [        S5      e[        SS9nUT lY        UR                  [        R                  " T R                  T RL                  SS95      5         [        R                  " U6 I Sh  vN   [        R                  R                  " U6 I Sh  vN   T R:                  b"  T R:                  R{                  5       I Sh  vN   T RZ                  R\                  b(  [        R                  " T R                  5       SS9T lb        T RZ                  R                  b(  [        R                  " T R                  5       SS9T le        ST l        T RO                  S5        T R6                  (       aH  T R6                  R                  (       a-  S+U 4S jjnT R6                  R                  R                  U5            S,U4S jjm    S-U4S jjmT" T RZ                  R\                  5      SSS 2   nT" T RZ                  R                  5      SSS 2   nT" T R^                  R\                  5      nT" T R^                  R                  5      nT" T R^                  Rt                  5      n[`        R                  " S!S"R                  U Vs/ s H  nS#URJ                   S#3PM     sn5      =(       d    S$S"R                  U Vs/ s H  nS#URJ                   S#3PM     sn5      =(       d    S$5        T R                  R                  S%   (       au  T R^                  R\                  (       aZ  T R^                  R\                  R                  (       d5  [`        Rb                  " S&S'T R^                  R\                  RJ                  0S(9  [`        R                  " S)S"R                  U Vs/ s H  nS#URJ                   S#3PM     sn5      =(       d    S$5        U(       d  U(       a  [`        R                  " S*S"R                  U Vs/ s H  nS#URJ                   S#3PM     sn5      =(       d    S$S"R                  U Vs/ s H  nS#URJ                   S#3PM     sn5      =(       d    S$5        U(       a
  UI Sh  vN   UsSSS5      ISh  vN   $  GN GNt GNJ GN  GN! [        R                  R                  " U6 I Sh  vN    f = f GNs  snf s  snf s  snf s  snf s  snf  Nm N]! , ISh  vN  (       d  f       g= f7f).a  Start the voice agent.

Create a default RoomIO if the input or output audio is not already set.
If the console flag is provided, start a ChatCLI.

Args:
    capture_run: Whether to return a RunResult and capture the run result during session start.
    room: The room to use for input and output
    room_input_options: Options for the room input
    room_output_options: Options for the room output
    record: Whether to record the audio, transcripts, traces, or logs
NF)requiredTzOnly one `AgentSession` can be the primary at a time. If you want to ignore primary designation, use session.start(record=False).agent_sessionr   zeagent started with the console subcommand, but input.audio/output.audio is already set, overriding...)r   r   )rj  rk  zHRoomIO audio input is enabled but input.audio is already set, ignoring..zJRoomIO audio output is enabled but output.audio is already set, ignoring..zZRoomIO transcription output is enabled but output.transcription is already set, ignoring..)rh  rv  rC  r]   )rv  z	audio.ogg)output_path_ivr_activity_startname_job_ctx_connectc                 >   > T R                  [        R                  S9$ N)reason)_aclose_implr>   JOB_SHUTDOWNr   s   rj   <lambda>$AgentSession.start.<locals>.<lambda>  s     1 19Q9Q 1 Rri   ra  )r_  )wait_on_enter_forward_audio_task_forward_video_taskr   c                j   > TR                   S:X  a"  TR                  S:X  a  TR                  5         g g g )Nr   )r	  r
  _set_user_away_timer_r   s    rj   on_room_io_subscribed1AgentSession.start.<locals>.on_room_io_subscribed#  s4    '';64;L;LP[;[113 <\6ri   c                :   > U c  / $ U /T" U R                   5      -   $ r   )source)inp_collect_sources    rj   r  +AgentSession.start.<locals>._collect_source*  s$     ![rQseocjj6Q.QQri   c                :   > U c  / $ U /T" U R                   5      -   $ r   )next_in_chain)out_collect_chains    rj   r  *AgentSession.start.<locals>._collect_chain/  s&     ![rWsenSEVEV6W.WWri   z*using audio io: %s -> `AgentSession` -> %sz -> `z(none)r   z`resume_false_interruption is enabled but audio output does not support pause, it will be ignoredaudio_outputextraz)using transcript io: `AgentSession` -> %sz(using video io: %s > `AgentSession` > %s)r  asyncio.Future[None]r   None)r  z$io.AudioInput | io.VideoInput | Noner   z#list[io.AudioInput | io.VideoInput])r  z6io.TextOutput | io.VideoOutput | io.AudioOutput | Noner   z5list[io.VideoOutput | io.AudioOutput | io.TextOutput])mr  r   r   r  r0   r    jobenable_recordingrr   r  _primary_agent_sessionanyvaluesrS  init_recordingr*   
start_spanr  r  otel_contextdetachr   set_span_in_contextattachr  r(   r  r  r  r  r  get_currentr  get_current_spanset_attributer)   ATTR_AGENT_LABELlabelr  _update_agent_stater   AgentsConsoleget_instanceenabledio_acquiredr<  r]   r@  r&   r'  
acquire_ior   r3   RoomOptions_ensure_optionsr  audio_inputr  transcriptiontext_outputRoomIOrp  rF   rG   register_sessionget_text_input_optionsregister_text_inputtext_input_cbrp   rE   record_inputrecord_outputr   create_tasksession_directoryr-  rC  r   _start_ivr_detectionATTR_ROOM_NAMErh  rz  ATTR_JOB_IDidATTR_AGENT_NAME
agent_nameconnectr  add_shutdown_callbackr  rc  rH   _update_activitygatherr   aiocancel_and_waitr  r   videor  r   subscribed_futadd_done_callbackdebugjoinr   r   	can_pause)r   rn  ro  rh  ri  rp   rj  rk  record_is_givenjob_ctx
is_primarycurrent_spanctxtasksc	transporttext_input_optstaskre  r  r  video_inputr  video_outputtranscript_outputr  r  r  s   `                         @@rj   rp  rq  R  s	    0 :::}} ::  $yy{D 'v.O%u5GF##9@55e&@&HD#J119W=[=[_c=c59G2!&J42299;<<*".!C#  7QQV6WD3&&t'>'>?060A0A/0RRD &&2##D$;$;<*.'++L9C&2&9&9#&>D#$&D!$7$9D! DM $D!%D!DM&2&>&>&@D# 113L&&{'C'CU[[QDK$$^4.0E!!..0Ayy::##/4;;3D3D3PNN8
 $**d;$&22BB '9(;  C  
  $yy6::##/#//f 05L,;;$$0#00h 16L-;;,,8#//x 05L, 'DVb cmm))+++ 4T :I)4Y)?D&&&77="."E"E"G"MM55o6S6ST))5**>>?\?\]::###(9(9(9..w7AIII!(((,6T,J)+/+<+<+I+I$**JZJZ+[

(,0,=,=,K,KDKKL]L],^)II!((199#*#6#6 $ 1 1 7 7070I0IK0W !8 !"$D
 "LL.<<--LL++D,E,E,GNcd **;+E+Ew||GXGXY**;+B+BGKKNNS**;+F+FH^H^_==LL!4!4W__5FM_!`a 6611R 7;D3*.I))5d>T>T>Y>Y>[>[&'FGG%$7	)2& LL##D$9$9$++UZ$9$[\8nne,,,ii//777!!-((..000 zz+,3,?,?,,.5J-) zz+,3,?,?,,.5J-) !DM$$[1}}!=!=4 ,,>>?TUR9R4R
XKXFX
 *$***:*:;DbDAK)$***:*:;DbDAK)$++*;*;<L)$++*;*;<L .t{{/H/H ILL<E#q1-EFR(F#q1-FGS8 

''(CDKK%%))33v)4;;+<+<+B+BC
 LL;9JK9J#q1-9JKLXPX
 l>KK[ I[c1SYYKq!1[ IJVhKK\ J\c1SYYKq!1\ JKWx _ ::F ,~ -7eii//777 1X FF L !J J  [ :::s  tr)tttr,tP't%r/&Pt<r8r2r8 t7r58.t&s#'Gt*s&t"s+<Cts0Ats59ts:1 ts?tt#t$t,t/t2r85t8!s s
s  t&ttt	t
ttc                   #    U R                   c  [        S5      eU R                   R                  5       I S h  vN   g  N7fNAgentSession isn't running)r  rS  drainr   s    rj   r  AgentSession.drain[  s1     >>!;<<nn""$$$s   6A >A c                R    U R                   (       d  [        S5      eU R                   $ )NzDCannot access room_io: the AgentSession was not started with a room.)r  rS  r   s    rj   r3   AgentSession.room_ioa  s%    }}V  }}ri   )r  errorc               z    U R                   (       a  g [        R                  " U R                  X2US95      U l         g N)r  r  r~  )r  r   r  r  )r   r~  r  r  s       rj   _close_soonAgentSession._close_soonj  s6     $00EvF
ri   )r  c               B    U R                  S U[        R                  S9  g r  )r  r>   USER_INITIATED)r   r  s     rj   shutdownAgentSession.shutdownw  s    t59S9STri   r%   c          	     
  #    U R                   (       a   [        R                  " U R                   5        U R                   IS h  vN   U R                  (       d   S S S 5      IS h  vN   g SU l        U R                  5         U R                  5         U R                  b)  U R                  R                  5       I S h  vN   S U l        U R                  nU(       a  [        UR                  =n[        5      (       as  UR                  5         UR                  5       I S h  vN   UR                   =n(       a  UR                  nOO*U(       a#  [        UR                  =n[        5      (       a  Ms  Ub  U(       d   UR#                  SS9I S h  vN   UR'                  5       I S h  vN   UR(                  (       a  UR(                  I S h  vN   S U R*                  l        S U R*                  l        S U R0                  l        S U R0                  l        U[4        R6                  :w  a3  UR8                  =nb$  UR;                  SU R<                  R>                  S9  UR                  5       I S h  vN   S U l
        U R@                  (       a!  U R@                  RC                  5         S U l         U RD                  (       a!  U RD                  RC                  5         S U l"        U RF                  b1  [H        RJ                  RM                  U RF                  5      I S h  vN   U RN                  (       a"  U RN                  R                  5       I S h  vN   U RP                  b"  U RP                  R                  5       I S h  vN   U RR                   Vs/ s H&  n[        U[T        RV                  5      (       d  M$  UPM(     n	nU	(       a%  [X        RZ                  " S U	 5       SS06I S h  vN   U R\                  (       a!  U R\                  RC                  5         S U l.        SU l        U R_                  S[a        X1S95        U R                  5         S	U l1        S
U l2        SU l3        SU l4        S U l         U Rj                  (       a)  U Rj                  R                  5       I S h  vN   S U l5        U Rl                  (       a)  U Rl                  R                  5       I S h  vN   S U l6        S S S 5      IS h  vN   [n        Rp                  " SURr                  US.S9  g  GN GN GN[ GN GN! [$         a     GNf = f GN GNk GN GN% GN GNs  snf  GNh N Nz Ne! , IS h  vN  (       d  f       Nz= f7f)NTforce)audio_detachedtranscript_timeoutc              3  @   #    U  H  oR                  5       v   M     g 7fr   aclose).0toolsets     rj   	<genexpr>,AgentSession._aclose_impl.<locals>.<genexpr>  s     ?h7nn&&hs   return_exceptionsFcloser  r~  r   r   r   zsession closed)r~  r  r  ):r  r  r  r  r   r  _cancel_user_away_timer_on_aec_warmup_expiredr  r  r  ro   rn  r6   cancel_wait_for_inactive
_old_agent	interruptrS  r  rK  r<  r]   r  r@  r  r>   ERROR_audio_recognitioncommit_user_turnr   r   r  endr  r   r   r  r  r  r  r   r   Toolsetr   r  r  r.  r=   r	  r
  r   r   r  r  r&   r  r5  )
r   r~  r  r  activity
agent_task	old_agentaudio_recognitiontooltoolsetss
             rj   r  AgentSession._aclose_implz  s1     "" 7 78:::== :: !DM((*'')yy$ii&&((( 	~~Hz*F*	RR!!# 33555 * 5 5595(22H z*F*	RR #&00t0<<< nn&&& **"1111 $(

 #'

 $(!,0) k///.6.I.II*V &66'++/::+V+V 7 
 oo'''!DN(())--/,0)''((,,.+/(((4ii//0I0IJJJ  ''..000!!-((//111)-V
48UHVnn?h?&*  
 !!""&&(%)"!DMIIgzEF((**D .D%&D"%&D"&*D#!!((//111%)" }}mm**,,, $M :P 	%u-UVQ  ) 6 ='  ' 2$ ( K 1 2V* 2
 -K :::sz  AU,TU,UU,&T'U,,AU=T>A"U T!AU0
U;T!TT!U%T2&$U
T5B"U-T8.B(UT;2U	T>
.U8U9U#U2U8)U!U	"B7UU9UUUU,*U+(U,U,UUT!!
T/+U.T//U5U8U;U>UUUUUU,U)UU)%U,c                Z   #    U R                  [        R                  S9I S h  vN   g  N7fr}  )r  r>   r  r   s    rj   r  AgentSession.aclose  s"     {'A'ABBBs   !+)+)endpointing_optsr   r   r   c               P   [        U5      (       d  [        U5      (       a  [        R                  " S5        [        U R                  R
                  S   [        U5      (       a  UOU R                  R
                  S   [        U5      (       a  UOU R                  R
                  S   S9n[        U5      (       a  UR                  S5      =nb  XPR                  R
                  S'   UR                  S5      =nb  X`R                  R
                  S'   UR                  S5      =nb  XpR                  R
                  S'   [        U5      (       a  X l        U R                  bE  U R                  R                  [        U5      (       a  U R                  R
                  O[        US9  gg)a  
Update the options for the agent session.

Args:
    endpointing_opts (NotGivenOr[EndpointingOptions], optional): Endpointing options.
    turn_detection (NotGivenOr[TurnDetectionMode | None], optional): Strategy for deciding
        when the user has finished speaking. ``None`` reverts to automatic selection.
    min_endpointing_delay: Deprecated, use ``endpointing_opts`` instead.
    max_endpointing_delay: Deprecated, use ``endpointing_opts`` instead.
z\min_endpointing_delay and max_endpointing_delay are deprecated, use endpointing_opts insteadr   	min_delay	max_delay)r   r  r  N)r  r   )r0   r&   r'  rK   r   r   r   r   r  update_optionsr,   )r   r  r   r   r   r   r  r  s           rj   r  AgentSession.update_options  so   & )**h7L.M.MNN/  2ZZ++F3   566 *//<   566 *//<  $%%(,,V44A15

&&v.-11+>>	K6?

&&{3-11+>>	K6?

&&{3N###1 >>%NN)).67G.H.HDJJ**i-	 *  &ri   c                  #    U R                   b  [        R                  " S5        g[        U 5      U l         U R                  R                  U R                   R                  5        U R                   R                  5       I Sh  vN   Ub;  [        R                  " SSU0S9  U R                   R                  [        USS95        gg NC7f)a  Start IVR detection on this session.

This method injects the DTMF tool and enables loop/silence detection,
allowing the agent to navigate IVR phone trees. Safe to call after AMD resolves.

Args:
    transcript (str | None, optional): The transcript to start IVR detection with.
Nz'IVR detection already started, skippingz%IVR detection started with transcriptr`   r  T)r`   is_final)r  r&   r'  rD   r   extendr   rp  r  _on_user_input_transcribedrA   )r   r`   s     rj   r  !AgentSession._start_ivr_detection1  s      )NNDE(.4--334  &&(((!LL@WaHbc99)Z$O " 	)s   BC	CAC	r]   r   add_to_chat_ctxc                  U R                   c  [        S5      eU R                  nU R                   R                  (       a  U R                  OU R                   nUc  [        S5      e[        5       n[        R                  " 5       [        R                  L a,  U R                  b  [        R                  " U R                  SS9nU   UR                  UUUUS9nU(       a  UR                  U5        S S S 5        U$ ! , (       d  f       W$ = f)Nr  z)AgentSession is closing, cannot use say()Fend_on_exitr  )r  rS  r  scheduling_pausedr  r   r   r  INVALID_SPANr  use_spansay_watch_handle)	r   r]  r]   r   r  re  r
  r%  handles	            rj   r&  AgentSession.sayG  s     >>!;<<**	*...*J*J4&&PTP^P^JKK ?Jm!!#u'9'99d>P>P>\~~d&8&8eLH\\$7 /	 " F ''/   X s   +C99
D)rb  instructionstool_choicer   r   chat_ctxr^  c                  U R                   c  [        S5      e[        U[        5      (       a  [        R
                  " SU/S9OUnU R                  n	U R                   R                  (       a  U R                  OU R                   n
U
c  [        S5      e[        5       n[        R                  " 5       [        R                  L a,  U R                  b  [        R                  " U R                  SS9nU   U
R                  U(       a  UOSUUUUU[!        US9S	9nU	(       a  U	R#                  U5        SSS5        U$ ! , (       d  f       W$ = f)
aC  Generate a reply for the agent to speak to the user.

Args:
    user_input (NotGivenOr[str | llm.ChatMessage], optional): The user's input that may influence the reply,
        such as answering a question.
    instructions (NotGivenOr[str], optional): Additional instructions for generating the reply.
    tool_choice (NotGivenOr[llm.ToolChoice], optional): Specifies the external tool to use when
        generating the reply. If generate_reply is invoked within a function_tool, defaults to "none".
    tools (NotGivenOr[list[str]], optional): List of tool IDs to make available for this response.
        When set, only the specified tools can be used. Tool IDs must match registered tools on the
        agent. For function tools, the ID is the function name (accessible via ``my_tool.id``).
        For toolsets, the ID is the one provided at construction (accessible via ``my_toolset.id``).
    allow_interruptions (NotGivenOr[bool], optional): Indicates whether the user can interrupt this speech.
    chat_ctx (NotGivenOr[ChatContext], optional): The chat context to use for generating the reply.
        Defaults to the chat context of the current agent if not provided.
    input_modality (Literal["text", "audio"], optional): The input mode to use for generating the reply.

Returns:
    SpeechHandle: A handle to the generated reply.
Nr  user)rolecontentz4AgentSession is closing, cannot use generate_reply()Fr!  )modality)user_messager*  r+  r   r   r,  input_details)r  rS  ro   r   r   ChatMessager  r#  r  r   r   r  r$  r  r%  _generate_replyrI   r'  )r   rb  r*  r+  r   r   r,  r^  r2  re  r
  r%  r(  s                rj   rd  AgentSession.generate_replyi  s(   > >>!;<< *c** OO*> 	 **	*...*J*J4&&PTP^P^UVV ?Jm!!#u'9'99d>P>P>\~~d&8&8eLH---9\t)'$7!*NC . F ''/   X s   1>D99
Er  c               d    U R                   c  [        S5      eU R                   R                  US9$ )zInterrupt the current speech generation.

Returns:
    An asyncio.Future that completes when the interruption is fully processed
    and chat context has been updated.
r  r  )r  rS  r  )r   r  s     rj   r  AgentSession.interrupt  s2     >>!;<<~~''e'44ri   c                h    U R                   c  [        S5      eU R                   R                  5         g r  )r  rS  clear_user_turnr   s    rj   r:  AgentSession.clear_user_turn  s(    >>!;<<&&(ri   r  stt_flush_duration
skip_replyc               h    U R                   c  [        S5      eU R                   R                  UUUS9$ )a=  Commit the user turn and generate a reply.

Returns a future that resolves with the user's audio transcript once STT
is complete and end-of-turn detection has been triggered.

Args:
    transcript_timeout (float, optional): The timeout for the final transcript
        to be received after committing the user turn.
        Default ``2.0`` s. Increase this value if the STT is slow to respond.
    stt_flush_duration (float, optional): The duration of the silence to be appended to the STT
        to flush the buffer and generate the final transcript.
        Default ``2.0`` s.
    skip_reply (bool, optional): Whether to skip the reply generation after committing the user turn.

Returns:
    asyncio.Future[str]: A future that resolves with the audio transcript.

Raises:
    RuntimeError: If the AgentSession isn't running.
r  r<  )r  rS  r  )r   r  r=  r>  s       rj   r  AgentSession.commit_user_turn  s?    6 >>!;<<~~..11! / 
 	
ri   c                :   Xl         U R                  (       a  U R                  b  SU R                  l        [        R
                  " U R                  U R                  U R                   5      SS9=U l        nU R                  nU(       a  UR                  U5        g g g )NT_update_activity_taskry  )
r  r   r  _new_turns_blockedr   r  rB  r   r  r'  )r   rn  r  re  s       rj   update_agentAgentSession.update_agent  s    == ~~)481181D1D**4+F+FT,2 D'$ ..I ''-  ri   c                p   #    U R                   b#  U R                   R                  5       I S h  vN   g g  N7fr   )r  r  r   s    rj   wait_for_inactiveAgentSession.wait_for_inactive  s-     >>%..33555 &5s   +646r  rp  )previous_activitynew_activityblocked_tasksr  c                 #    U R                    IS h  vN   Xl        US:X  a\  U R                  (       a  U R                  R                  OS nUR                  b  XLd  US:w  a  [	        S5      e[        X5      U l        O/US:X  a)  UR                  c  [	        S5      eUR                  U l        U R                  b   [        R                  " U R                  5        S n U R                  nU R                  =n	bq  US:X  a:  U	R                  U R                  S9I S h  vN nU	R                  5       I S h  vN   O1US:X  a+  U	R                  U=(       d    / U R                  S9I S h  vN nU R                  (       a}  US:X  aw  [        R                  " S	U S
U R                  R                  R                    35        Ub  UR#                  5       I S h  vN   S nS U l        S U l         S S S 5      IS h  vN   g U R                  U l        S U l        U R$                  n
['        U(       a  UR                  R                   OS U R                  R                  R                   S9nU
(       a8  U
R)                  UU(       a  UR                  OS U R                  R                  S9  U R*                  R-                  U5        U R/                  S[1        US95        US:X  a"  U R                  R3                  US9I S h  vN   O'US:X  a!  U R                  R5                  US9I S h  vN   S S S 5      IS h  vN   U(       aL  U R                  R8                  c   e[:        R<                  " U R                  R8                  5      I S h  vN   g g  GNu GNo GNZ GN+ GN GN N N|! [6         a    Ub  UR#                  5       I S h  vN    e f = f N! , IS h  vN  (       d  f       N= f Nc7f)Nrp  r  z2cannot start agent: an activity is already runningresumez:cannot resume agent: no existing active activity to resume)rJ  pause)rK  rJ  zsession is closing, skipping z activity of )old_agent_idnew_agent_id)itemr  	new_agentconversation_item_addedrQ  )reuse_resources)r  r  r  rn  rS  r7   r  r  r  r  r  r  rN  r  r&   r'  r  cleanupr  r!   _agent_handoffr   insertr.  r?   rp  rM  BaseException_on_enter_taskr   shield)r   rn  rI  rJ  rK  r  previous_agentrU  previous_activity_vr
  re  handoff_items               rj   r  AgentSession._update_activity  sa     &&&Kw&9=!5!5T??./3D3O&'[\\&3E&@#)??*&'cdd&+oo#&&2 ##D$;$;<9=O/&*nn# $.H;(G308DL_L_0`*`&oo///*g508*7*=2DL_L_ 1? 1 + ==\W%<NN7~]SWSfSfSlSlSoSoRpq '2-55777*.*.D'%)DN] '&&` "&!4!4&*# 22	+AT!4!:!:!=!=Z^!%!5!5!8!8  ,,)?R"5";";X\"&.."6"6 - 
 %%l3		35OUa5bc7*.....OOO!X-..///PPPI '&V >>00<<<..!>!>??? W ': +a/+ 8U 'D PP  ".)11333K '&&&Z @s  O'NO'CO=NNN4N51N&N'A+NNN(O'3N4O'9C>N7N8N<O="NN N$O'/O	0AO'O%O'NNNNO'NN O>O?OO	O'O"OO"O'c                `   #    Ub
  UI S h  vN   U R                  U5      I S h  vN   g  N N7fr   )r  )r   old_taskrn  s      rj   rB  "AgentSession._update_activity_taskQ  s0      NN##E*** *s   	.*.,..c                  ^  T R                   (       d  UR                  (       a  g UR                  S:X  a;  T =R                  S-  sl        T R                  T R                  R
                  ::  a  g O\UR                  S:X  a;  T =R                  S-  sl        T R                  T R                  R
                  ::  a  g OUR                  S:X  a  g [        UR                  [        5      (       a$  [        R                  " SUR                   35        O[        R                  " SUR                  S9  S
U 4S jjn[        R                  " T R                  U[        R                  S	95      T l         T R                   R!                  U5        g )N	llm_errorr1   	tts_errorinterruption_detection_errorz4AgentSession is closing due to unrecoverable error: z2AgentSession is closing due to unrecoverable error)exc_infoc                   > S Tl         g r   )r  r  s    rj   on_close_done-AgentSession._on_error.<locals>.on_close_donez  s    !%Dri   r  )r  zasyncio.Task[None]r   r  )r  recoverabletyper   r   r{   r   ro   r  r   r&   r   r  r  r>   r  r  )r   r  ri  s   `  rj   	_on_errorAgentSession._on_errorZ  s6    !2!2::$""a'"%%):):)S)SS TZZ;&""a'"%%):):)S)SS TZZ99 ekk8,,LLOPUP[P[}]^LLD
	& %00E+2C2CD
 	,,];ri   c                   #    U R                   R                  nUc  g U  S h  vN nU R                  c  M  U R                  R                  U5        M5   N0
 g 7fr   )r<  r]   r  
push_audio)r   r  r   s      rj   r   AgentSession._forward_audio_task  sJ     jj&&& 	1%~~)))%0	1;s*   AAAAAAAAc                   #    U R                   R                  nUc  g U  S h  vN nU R                  c  M  U R                  b  U R                  X 5      (       d  M=  U R                  R	                  U5        MZ   NU
 g 7fr   )r<  r  r  r   
push_video)r   r  r   s      rj   r   AgentSession._forward_video_task  si     jj&&& 	1%~~)&&24;N;Nu;[;[))%0	1;s+   A=A;A9A;A=AA=9A;;A=c                \   U R                  5         U R                  R                  c  g U R                  =n(       a1  UR                  (       a   UR                  R                  5       (       d  g U R                  R                  U R                  R                  U R                  S5      U l	        g )Naway)
r  r   r   r  r  rc  r   
call_later_update_user_stater  )r   r3   s     rj   r  !AgentSession._set_user_away_timer  s    $$&::''/ %W%&&**//11  $

 5 5JJ(($*A*A6!
ri   c                b    U R                   b"  U R                   R                  5         S U l         g g r   )r  r  r   s    rj   r  $AgentSession._cancel_user_away_timer  s-      ,!!((*$(D! -ri   c                    U R                   S:  a'  U R                  (       d  [        R                  " S5        SU l         U R                  b"  U R                  R                  5         S U l        g g )Nr   z-aec warmup expired, re-enabling interruptionsr   )r   r  r&   r  r   r  r   s    rj   r   #AgentSession._on_aec_warmup_expired  sV    %%)$--LLHI%("!!-""))+%)D" .ri   )r  
start_timec                  U R                   U:X  a  g U(       a  [        US-  5      OS nUS:X  a{  SU l        SU l        U R                  c_  [
        R                  " SX$S9U l        U R                  (       a4  [        U R                  U R                  R                  R                  5        O.U R                  b!  U R                  R                  5         S U l        US:X  a  U R                  S:  a  U R                  c  U R                  R                  (       am  U R                  R                   bV  U R"                  R%                  U R                  U R&                  5      U l        [(        R*                  " SU R                  5        US:X  a!  U R,                  S:X  a  U R/                  5         OU R1                  5         U R                   nXl         U R3                  S[5        XQS	95        g )
N ʚ;r   r   agent_speaking)r   r~  z4aec warmup active, disabling interruptions for %.2fsr   agent_state_changed)	old_state	new_state)r
  rz   r   r   r  r*   r  r  r4   rh  local_participantr  r   r   r   audio_enabledr]   r   rw  r   r&   r  r	  r  r  r.  r<   )r   stater  r~  start_time_nsr  s         rj   r   AgentSession._update_agent_state  s    %;EJ674J%&D"%&D"((0,2,=,=$l-) ==/114==3E3E3W3W &&2%%))+(,D% Z**Q.&&.**"".%)ZZ%:%:**D,G,G&D" LLF**
 KD$4$4$C%%'((*%%	!		!"YH	
ri   )last_speaking_timec          	        U R                   U:X  a  g U(       a  [        US-  5      OS nUS:X  a~  U R                  cq  [        R                  " SUS9U l        U R
                  (       aE  U R
                  R                  (       a*  [        U R                  U R
                  R                  5        O-U R                  b   U R                  R                  US9  S U l        US:X  a!  U R                  S:X  a  U R                  5         OU R                  5         U R                   nXl         U R                  S[        UUU=(       d    [        R                  " 5       S95        g )	Nr  r   user_speaking)r~  )end_timer   user_state_changed)r  r  
created_at)r	  rz   r  r*   r  r  linked_participantr4   r  r
  r  r  r.  rC   r   )r   r  r  last_speaking_time_nsr  s        rj   rx  AgentSession._update_user_state  s)    u$ 8JC"]23t 	 J4#;#;#C'-'8'8,A(D$ }}!A!A+,,dmm.N.N
 %%1 $$((2G(H'+D$KD$5$5$D%%'((*$$	 		 !#-<	
ri   c                    U(       dL  U R                   S:X  a;  U R                  b  U R                  R                  S5        gU R                  S5        ggg)z:End user speaking state when audio is disabled by default.r   Nr   )r	  r  on_end_of_speechrx  )r   r  s     rj   r   &AgentSession._on_audio_enabled_changed  sC    4++z9~~)//5''4	 :wri   c                    U R                   S:X  a"  UR                  (       a  U R                  S5        U R                  SU5        g )Nrv  r   user_input_transcribed)r   r  rx  r.  )r   evs     rj   _user_input_transcribed$AgentSession._user_input_transcribed&  s1    ??f$##K0		*B/ri   c                l    U R                   R                  U5        U R                  S[        US95        g )NrS  rT  )r   rX  r.  r?   )r   messages     rj   _conversation_item_added%AgentSession._conversation_item_added-  s)    g&		+-GW-UVri   c                :    U R                   R                  U5        g r   )r   rX  )r   itemss     rj   _tool_items_addedAgentSession._tool_items_added1  s    e$ri   c                    U R                   $ r   )r   r   s    rj   r   AgentSession.stt5      yyri   c                    U R                   $ r   )r   r   s    rj   r   AgentSession.llm9  r  ri   c                    U R                   $ r   )r   r   s    rj   r   AgentSession.tts=  r  ri   c                    U R                   $ r   )r   r   s    rj   r   AgentSession.vadA  r  ri   c                    U R                   $ r   )r   r   s    rj   interruption_detection#AgentSession.interruption_detectionE  s    +++ri   c                    U R                   (       d  g U R                  b  U R                  R                  5         [        R                  " U R                  5       SS9U l        g )Nr  ry  )r   r   r  r   r  r  r   s    rj   r   $AgentSession._on_video_input_changedK  L    }}$$0%%,,.$+$7$7$$&-B%
!ri   c                    U R                   (       d  g U R                  b  U R                  R                  5         [        R                  " U R                  5       SS9U l        g )Nr  ry  )r   r   r  r   r  r  r   s    rj   r   $AgentSession._on_audio_input_changedV  r  ri   c                    g r   ra   r   s    rj   r   %AgentSession._on_video_output_changeda      ri   c                   U R                   (       aq  U R                  R                  S   (       aR  U R                  R                  =n(       a4  UR
                  (       d"  [        R                  " SSUR                  0S9  g g g g g )Nr   zZresume_false_interruption is enabled, but the audio output does not support pause, ignoredr  r  )	r   r   r   r@  r]   r  r&   r'  r  )r   r  s     rj   r   %AgentSession._on_audio_output_changedd  sl    MM

''(CD!%!2!222 **NNl%|'9'9: + 3 E ri   c                    g r   ra   r   s    rj   r   $AgentSession._on_text_output_changedp  r  ri   c                   #    U $ 7fr   ra   r   s    rj   
__aenter__AgentSession.__aenter__u  s
     s   c                @   #    U R                  5       I S h  vN   g  N7fr   r  )r   exc_typeexcexc_tbs       rj   	__aexit__AgentSession.__aexit__x  s      kkms   )3r  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  r   r   r   r   r   r   r  r  r  r	  r  r   r   )>r   z%NotGivenOr[stt.STT | STTModels | str]r   zNotGivenOr[vad.VAD]r   z9NotGivenOr[llm.LLM | llm.RealtimeModel | LLMModels | str]r   z%NotGivenOr[tts.TTS | TTSModels | str]r   zNotGivenOr[TurnHandlingOptions]r   z(NotGivenOr[list[llm.Tool | llm.Toolset]]r   zNotGivenOr[list[mcp.MCPServer]]r   rz   r   NotGivenOr[bool]r   z+NotGivenOr[Sequence[TextTransforms] | None]r   r   r   zNotGivenOr[Userdata_T]r   z NotGivenOr[_VideoSampler | None]r   r   r   r\   r   r   r   r   r   z!NotGivenOr[SessionConnectOptions]r   z asyncio.AbstractEventLoop | Noner   r  r   NotGivenOr[float]r   r  r   NotGivenOr[float | None]r   zNotGivenOr[TurnDetectionMode]r   r  r   r  r   zNotGivenOr[int]r   r  r   r  r   r  r   r  )r   z
AMD | Noner   )r)  r@   r*  zCallable | Noner   r   )r)  r@   r/  r:   r   r  )r   r   )r5  r   r   r  )r   zTurnDetectionMode | None)r   zlist[mcp.MCPServer] | None)r   zio.AgentInput)r   zio.AgentOutput)r   r}   )r   rt   )r   zllm.ChatContext)r   zSpeechHandle | None)r   rB   )r   r;   )r   r5   )r   zlist[llm.Tool | llm.Toolset])r   r'   )rb  r   r^  Literal['text', 'audio']r_  ztype[Run_T] | Noner   zRunResult[Run_T])rn  r5   ro  zLiteral[True]rh  NotGivenOr[rtc.Room]ri  NotGivenOr[room_io.RoomOptions]rp   bool | RecordingOptionsrj  $NotGivenOr[room_io.RoomInputOptions]rk  %NotGivenOr[room_io.RoomOutputOptions]r   rH   )rn  r5   ro  zLiteral[False]rh  r  ri  r  rp   r  rj  r  rk  r  r   r  )rn  r5   ro  r\   rh  r  ri  r  rp   z#NotGivenOr[bool | RecordingOptions]rj  r  rk  r  r   zRunResult | None)r   r  )r   zroom_io.RoomIO)r~  r>   r  r\   r  zJllm.LLMError | stt.STTError | tts.TTSError | llm.RealtimeModelError | Noner   r  )r  r\   r   r  )r~  r>   r  r\   r  zqllm.LLMError | stt.STTError | tts.TTSError | llm.RealtimeModelError | inference.InterruptionDetectionError | Noner   r  )
r  zNotGivenOr[EndpointingOptions]r   z$NotGivenOr[TurnDetectionMode | None]r   r  r   r  r   r  )r`   z
str | Noner   r  )
r]  zstr | AsyncIterable[str]r]   z)NotGivenOr[AsyncIterable[rtc.AudioFrame]]r   r  r  r\   r   rJ   )rb  z!NotGivenOr[str | llm.ChatMessage]r*  zNotGivenOr[str | Instructions]r+  zNotGivenOr[llm.ToolChoice]r   zNotGivenOr[list[str]]r   r  r,  zNotGivenOr[ChatContext]r^  r  r   rJ   )r  r\   r   r  )r  r   r=  r   r>  r\   r   zasyncio.Future[str])rn  r5   r   r  )rn  r5   rI  zLiteral['close', 'pause']rJ  zLiteral['start', 'resume']rK  zlist[asyncio.Task] | Noner  r\   r   r  )ra  zasyncio.Task[None] | Nonern  r5   r   r  )r  zjllm.LLMError | stt.STTError | tts.TTSError | llm.RealtimeModelError | inference.InterruptionDetectionErrorr   r  )r  r;   r  zotel_context.Context | Noner~  r   r   r  )r  rB   r  r   r   r  )r  r\   r   r  )r  rA   r   r  )r  zllm.ChatMessager   r  )r  z3Sequence[llm.FunctionCall | llm.FunctionCallOutput]r   r  )r   zstt.STT | None)r   z"llm.LLM | llm.RealtimeModel | None)r   ztts.TTS | None)r   zvad.VAD | None)r   z&NotGivenOr[Literal['adaptive', 'vad']])r   r   )r  ztype[BaseException] | Noner  zBaseException | Noner  zTracebackType | Noner   r  )Krb   rc   rd   re   r/   r,   r   r   r#  r(  r.  r   setterr   r   r<  r@  rC  r   rH  rK  r   rP  rT  r   r[  rf  r   rp  r  r3   r  r  r   log_exceptionsr&   r  r  r  r  r&  rd  r  r:  r  rD  rG  r  rB  rm  r  r  r  r  r   r  rx  r   r  r  r  r   r   r   r   r  r   r   r   r   r   r  r  rh   __classcell__)r   s   @rj   r   r      s&	   %Y%Y*^)]#W0d)]%Y&ZR0d	
 & 6?#,IR5>9B:C7@7@KT.1+4:C,/#*.25:C152;3<3<?H8A=F7@2;096?ENKw% 3w% !	w%
 Gw% 3w% 7w% 8w% 5w% w% %5w% Iw% ',w%" )#w%$ 8%w%& *'w%( )w%* (+w%, +0-w%0 81w%2 /3w%6  07w%8  19w%:  1;w%< %==w%> 6?w%@ +;Aw%B $5Cw%D !0Ew%F .Gw%H $4Iw%J +CKw%L 
Mw%! w%r  + +!   __  $ $ ! !       " "   U U     ! !     N N 4:*.  1	
 ( 
  &/8A*.CLEN #	
 # 6 ( A C 
  
 ',%.8A*.CLEN $	
 # 6 ( A C 
 " "%.8A6?CLENGG 	G
 #G 6G 4G AG CG 
GR%   \`
 
 	

 Z
 

 )- U (
  xW xW 	xW
xW 
xW )xWtC <E?H3<3<7 97 =	7  17  17 
7r4 <E09 $ &  9	 
 .    
 J 9B7@2;'009,539@ 6@ 5	@
 0@ %@ .@ *@ 1@ 
@D */ 
5) %($' "
 ""
 "	"

 "
 
"
H.&6 8?3:37"V@V@ 5	V@
 1V@ 1V@ V@ 
V@p (+1+:?+	+ )+&</&< 
&<P (1 )1 (
1 )
1
")
* 59#'9
9
 2	9

 !9
 
9
x GK)
)
7C)
	)
V50W%         , ,
	
	


, " %	
 
 ri   r   )rp   r  r   rZ   )
__future__r   r   r  r   collections.abcr   r   r   
contextlibr   r   contextvarsr	   dataclassesr
   typesr   typingr   r   r   r   r   r   r   opentelemetryr   r  r   typing_extensionsr   livekitr    r   r   r   r   r   r   r   _exceptionsr   r  r    r!   r"   r#   llm.chat_contextr$   logr&   metricsr'   r(   	telemetryr)   r*   r+   r,   r-   r.   utils.deprecationr/   
utils.miscr0   r2   r3   _utilsr4   rn  r5   r6   agent_activityr7   r8   r#  r9   eventsr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   ivrrD   recorder_iorE   remote_sessionrF   rG   
run_resultrH   speech_handlerI   rJ   turnrK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   transcription.text_transformsrX   rZ   rl   rg   rm   rr   rt   r}   r   r   r   r   r   EventEmitterr   ra   ri   rj   <module>r     s   "    = = :  !    9 '  8 8 8 " ! : : +  < +  1 !  / # =     # = ! 5
 
 
 ;;=Sy S0 	' #  	( $ ? N N N ; ; ;0 \"

 RH R R
 2 6G4W 1 Wv3##J/1D vri   