
    /j')                        S SK J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  SSKJrJr  SS	KJr  S
r\	 " S S5      5       r\" SS9r " S S5      rg)    )annotationsN)Callable	GeneratorSequence)	dataclass)AnyLiteral)context   )llmutils)loggerg      @c                       \ rS rSr% S\S'   Srg)InputDetails   zLiteral['text', 'audio']modality N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r       S/app/agent/.venv/lib/python3.13/site-packages/livekit/agents/voice/speech_handle.pyr   r      s    &&r   r   audio)r   c                     \ rS rSrSr Sr Sr         S)S jr\S\	4     S*S jj5       r
\S+S j5       r\S,S	 j5       r\S-S
 j5       r\S,S j5       r\S.S j5       r\S/S j5       r\S/S j5       r\S/S j5       r\R(                  S0S j5       r\S1S j5       rS/S jrSS.S2S jjrS3S jrS4S jrS5S jrS5S jrS6S jrS7S jrS8S jrS8S jrS9S jr S3S jr!S3S  jr"S3S! jr#S:S;S" jjr$S3S# jr%S3S$ jr&S3S% jr'S3S& jr(S'r)g()<SpeechHandle   r      
   c                 ^  UT l         UT l        UT l        [        R                  S    " 5       T l        [        R                  S    " 5       T l        [        R                  S    " 5       T l        [        R                  " 5       T l	        / T l
        / T l        / T l        ST l        S T l        S T l        [!        5       T l        [!        5       T l        SU 4S jjnT R                  R'                  U5        S T l        g )N   c                   > [        TR                  5       H  n U" T5        M     g ! [         a#  n[        R                  " SU 3US9   S nAM9  S nAff = f)Nzerror in done_callback: exc_info)list_done_callbacks	Exceptionr   warning)_cbeselfs      r   _on_done'SpeechHandle.__init__.<locals>._on_done:   sR    4//0PtH 1 ! PNN%=bT#BQOOPs   (
AAA)r+   zasyncio.Future[None]returnNone)_id_allow_interruptions_input_detailsasyncioFuture_interrupt_fut	_done_fut_scheduled_futEvent_authorize_event_generations_tasks_chat_items
_num_steps_agent_turn_context_interrupt_timeout_handleset_item_added_callbacksr(   add_done_callback_maybe_run_final_output)r.   	speech_idallow_interruptionsinput_detailsr/   s   `    r   __init__SpeechHandle.__init__!   s     $7!+%nnT24 -/%nnT24 '8: +-/1@D EI&JM%"DGE	P 	((2,0$r   Tc                @    [        [        R                  " S5      U US9$ )Nspeech_)rG   rH   rI   )r   r   	shortuuid)rH   rI   s     r   createSpeechHandle.createD   s#    
 ooi0 3'
 	
r   c                    U R                   $ N)r@   r.   s    r   	num_stepsSpeechHandle.num_stepsO   s    r   c                    U R                   $ rR   )r3   rS   s    r   idSpeechHandle.idS   s    xxr   c                    U R                   $ rR   )r5   rS   s    r   rI   SpeechHandle.input_detailsW   s    """r   c                8    U R                    SU R                   3$ )Nr+   )r3   r@   rS   s    r   _generation_idSpeechHandle._generation_id[   s    ((1T__-..r   c                `    U R                   S::  a  g U R                   SU R                   S-
   3$ )Nr#   r+   )r@   r3   rS   s    r   _parent_generation_id"SpeechHandle._parent_generation_id_   s0    ??a((1T__q0122r   c                6    U R                   R                  5       $ rR   )r:   donerS   s    r   	scheduledSpeechHandle.schedulede       ""''))r   c                6    U R                   R                  5       $ rR   )r8   rb   rS   s    r   interruptedSpeechHandle.interruptedi   re   r   c                    U R                   $ rR   )r4   rS   s    r   rH    SpeechHandle.allow_interruptionsm   s    (((r   c                V    U R                   (       a  U(       d  [        S5      eXl        g)a  Allow or disallow interruptions on this SpeechHandle.

When set to False, the SpeechHandle will no longer accept any incoming
interruption requests until re-enabled. If the handle is already
interrupted, clearing interruptions is not allowed.

Args:
    value (bool): True to allow interruptions, False to disallow.

Raises:
    RuntimeError: If attempting to disable interruptions when already interrupted.
zPCannot set allow_interruptions to False, the SpeechHandle is already interruptedN)rg   RuntimeErrorr4   )r.   values     r   rH   rj   q   s'     Eb  %*!r   c                    U R                   $ rR   )r?   rS   s    r   
chat_itemsSpeechHandle.chat_items   s    r   c                6    U R                   R                  5       $ rR   )r9   rb   rS   s    r   rb   SpeechHandle.done   s    ~~""$$r   F)forcec               l    U(       d  U R                   (       d  [        S5      eU R                  5         U $ )zInterrupt the current speech generation.

Raises:
    RuntimeError: If this speech handle does not allow interruptions.

Returns:
    SpeechHandle: The same speech handle that was interrupted.
z3This generation handle does not allow interruptions)r4   rl   _cancel)r.   rs   s     r   	interruptSpeechHandle.interrupt   s)     T66TUUr   c                  #    SSK Jn  [        R                  " 5       =n(       a~  U" U5      nU(       ao  UR                  (       a^  UR
                  U :X  aN  UR                  R                  R                  SS5      (       d#  [        SUR                  R                   S35      e[        R                  " U R                  5      I Sh  vN   g N7f)aS  Waits for the entire assistant turn to complete playback.

This method waits until the assistant has fully finished speaking,
including any finalization steps beyond initial response generation.
This is appropriate to call when you want to ensure the speech output
has entirely played out, including any tool calls and response follow-ups.r#   )_get_activity_task_info__livekit_agents_tool_pendingFzMcannot call `SpeechHandle.wait_for_playout()` from inside the function tool `u<  ` that owns this SpeechHandle. This creates a circular wait: the speech handle is waiting for the function tool to complete, while the function tool is simultaneously waiting for the speech handle.
To wait for the assistant’s spoken response prior to running this tool, use `RunContext.wait_for_playout()` instead.N)agentry   r6   current_taskfunction_callspeech_handleextragetrl   nameshieldr9   )r.   ry   taskinfos       r   wait_for_playoutSpeechHandle.wait_for_playout   s      	3''))4)*40D&&&&$.**00445TV[\\"cdhdvdvd{d{c| }M M  nnT^^,,,s   CCCCc                >   ^  SU 4S jjnU" 5       R                  5       $ )Nc                 D   >#    T R                  5       I S h  vN   T $  N7frR   )r   rS   s   r   _await_impl+SpeechHandle.__await__.<locals>._await_impl   s"     '')))K *s     r1   r   )	__await__)r.   r   s   ` r   r   SpeechHandle.__await__   s    	 }&&((r   c                    U R                  5       (       a%  [        R                  " 5       R                  X5        g U R                  R                  U5        g rR   )rb   r6   get_running_loop	call_soonr(   addr.   callbacks     r   rE   SpeechHandle.add_done_callback   s;    99;;$$&00@  *r   c                :    U R                   R                  U5        g rR   )r(   discardr   s     r   remove_done_callback!SpeechHandle.remove_done_callback   s    $$X.r   c                  #    [         R                  " U Vs/ s H  n[         R                  " U5      PM     snSS06nX0R                  1n[         R                  " U[         R
                  S9I S h  vN u  pVX6;   aH  [        R                  " [         R                  5         UR                  5         UI S h  vN   S S S 5        g g s  snf  NY N! , (       d  f       g = f7f)Nreturn_exceptionsT)return_when)
r6   gatherr   r8   waitFIRST_COMPLETED
contextlibsuppressCancelledErrorcancel)r.   awfut
gather_futfsr+   pendings          r   wait_if_not_interrupted$SpeechHandle.wait_if_not_interrupted   s     ^^R%HRcgnnS&9R%Ha\`a
(24G4G'H"<<8O8OPP
 $$W%;%;<!!#    =< ! &IP ! =<sE   C  C:C 1C2/C !C7C8C<C C
CC c                  ^  T R                  5       (       a  T $ T R                  R                  5       (       dQ  T R                  R                  S 5        SU 4S jjn[        R                  " 5       R                  [        U5      T l        T $ )Nc                    > [         R                  " STR                  [        S.S9  TR                   H  n U R                  5         M     TR                  5         g )NzNspeech not done in time after interruption, cancelling the speech arbitrarily.)rG   timeout)r   )r   errorr3   INTERRUPTION_TIMEOUTr>   r   
_mark_done)r   r.   s    r   _on_timeout)SpeechHandle._cancel.<locals>._on_timeout   sB    d(,=QR !KKDKKM (!r   r1   r2   )rb   r8   
set_resultr6   get_event_loop
call_laterr   rB   )r.   r   s   ` r   ru   SpeechHandle._cancel   sk    99;;K""''))**40" .5-C-C-E-P-P$k.D* r   c                :    U R                   R                  U5        g rR   )rD   r   r   s     r   _add_item_added_callback%SpeechHandle._add_item_added_callback   s    ""&&x0r   c                :    U R                   R                  U5        g rR   )rD   r   r   s     r   _remove_item_added_callback(SpeechHandle._remove_item_added_callback   s    ""**84r   c                    U HC  n[        U R                  5       H  n U" U5        M     U R
                  R                  U5        ME     g ! [         a#  n[        R                  " SU 3US9   S nAMX  S nAff = f)Nzerror in item_added_callback: r%   )r'   rD   r)   r   r*   r?   append)r.   itemsitemr,   r-   s        r   _item_addedSpeechHandle._item_added   sr    D4556VtH 7 ##D)  ! VNN%CB4#HSTUUVs   A
A8A33A8c                    [         R                  S    " 5       nU R                  R                  U5        U R                  R                  5         g rR   )r6   r7   r=   r   r<   rC   )r.   r   s     r   _authorize_generation"SpeechHandle._authorize_generation   s9    nnT"$  %!!#r   c                8    U R                   R                  5         g rR   )r<   clearrS   s    r   _clear_authorization!SpeechHandle._clear_authorization   s    ##%r   c                T   #    U R                   R                  5       I S h  vN   g  N7frR   )r<   r   rS   s    r   _wait_for_authorization$SpeechHandle._wait_for_authorization  s     ##((***s   (&(c                   #    U R                   (       d  [        S5      e[        R                  " U R                   U   5      I S h  vN   g  N7f)Nz@cannot use wait_for_generation: no active generation is running.)r=   rl   r6   r   )r.   step_idxs     r   _wait_for_generation!SpeechHandle._wait_for_generation  s7       abbnnT..x8999s   AAAAc                `   #    [         R                  " U R                  5      I S h  vN   g  N7frR   )r6   r   r:   rS   s    r   _wait_for_scheduled SpeechHandle._wait_for_scheduled
  s     nnT00111s   $.,.c                    U R                   (       d  [        S5      e[        R                  " [        R
                  5         U R                   S   R                  S 5        S S S 5        g ! , (       d  f       g = f)NzAcannot use mark_generation_done: no active generation is running.)r=   rl   r   r   r6   InvalidStateErrorr   rS   s    r   _mark_generation_done"SpeechHandle._mark_generation_done  sP      bcc  !:!:;b!,,T2 <;;s   A))
A7c                V   [         R                  " [        R                  5         U R                  R                  S 5        U R                  (       a  U R                  5         S S S 5        U R                  b"  U R                  R                  5         S U l        g g ! , (       d  f       N>= frR   )
r   r   r6   r   r9   r   r=   r   rB   r   rS   s    r   r   SpeechHandle._mark_done  sx      !:!:;NN%%d+  **,	 < ))5**113-1D* 6 <;s   =B
B(c                    [         R                  " [        R                  5         U R                  R                  S 5        S S S 5        g ! , (       d  f       g = frR   )r   r   r6   r   r:   r   rS   s    r   _mark_scheduledSpeechHandle._mark_scheduled  s6      !:!:;**40 <;;s   A


A)rA   r4   r<   r?   r(   r9   r=   r3   r5   r8   rB   rD   rF   r@   r:   r>   N)rG   strrH   boolrI   r   r1   r2   )rH   r   rI   r   r1   r   )r1   int)r1   r   )r1   r   )r1   z
str | None)r1   r   )rm   r   r1   r2   )r1   zlist[llm.ChatItem])rs   r   r1   r   r   )r1   z#Generator[None, None, SpeechHandle])r   zCallable[[SpeechHandle], None]r1   r2   )r   z!list[asyncio.futures.Future[Any]]r1   r2   r   )r   zCallable[[llm.ChatItem], Any]r1   r2   )r   zSequence[llm.ChatItem]r1   r2   )r   )r   r   r1   r2   )*r   r   r   r   SPEECH_PRIORITY_LOWSPEECH_PRIORITY_NORMALSPEECH_PRIORITY_HIGHrJ   staticmethodDEFAULT_INPUT_DETAILSrO   propertyrT   rW   rI   r\   r_   rc   rg   rH   setterro   rb   rv   r   r   rE   r   r   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s   [MN!1!16:!1KW!1	!1F $(&;
!
#
 

 
     # # / / 3 3
 * * * * ) ) *  *(    % */ -8)+/!,15*$
&+:23	21r   r   )
__future__r   r6   r   collections.abcr   r   r   dataclassesr   typingr   r	   opentelemetryr
   otel_context r   r   logr   r   r   r   r   r   r   r   <module>r      s[    "   9 9 !  1    ' ' ' %g6 H1 H1r   