o
    i                     @  s  d dl mZ d dlZd dlZd dlmZmZmZmZ d dl	m
Z
 d dlmZmZmZmZ d dlmZ ddlmZmZmZmZmZmZmZ dd	lmZmZmZ dd
lmZ ddlm Z  ddl!m"Z"m#Z#m$Z$ ddlm%Z%m&Z& ddl'm(Z( erddlm)Z)m*Z*m+Z+ ddlm,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 e
G dd dZ5G dd dZ6edZ7G dd de6ee7 Z8e
G dd dZ9e"e"e"dd/d*d+Z:d0d-d.Z;dS )1    )annotationsN)AsyncGeneratorAsyncIterable	Coroutine	Generator)	dataclass)TYPE_CHECKINGAnyGenericTypeVar)rtc   )	inferencellmstttokenizettsutilsvad)ChatContextRealtimeModelfind_function_tools)_ReadOnlyChatContextlogger)	NOT_GIVENFlushSentinel
NotGivenOr)is_givenmisc   )SpeechHandle)	LLMModels	STTModels	TTSModels)mcp)AgentActivity)AgentSession)TurnDetectionMode)TimedStringc                   @  s   e Zd ZU eZded< dS )ModelSettingszNotGivenOr[llm.ToolChoice]tool_choiceN)__name__
__module____qualname__r   r+   __annotations__ r0   r0   \/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/agents/voice/agent.pyr*      s   
 r*   c                   @  s  e Zd Zdedeeeeeeeeeeedd~d d!Zedd"d#Zedd$d%Zedd&d'Zedd)d*Z	edd,d-Z
dd.d/Zdd0d1Zd2d3dd6d7Zdd8d9Zdd:d;Zdd?d@ZddFdGZddJdKZddOdPZddSdTZddUdVZddXdYZG dZd[ d[Zedd]d^Zedd_d`Zejddcd`ZeddedfZeddhdiZeddkdlZeddmdnZeddodpZeddqdrZ eddsdtZ!eddudvZ"eddwdxZ#eddydzZ$edd|d}Z%dS )AgentN)idchat_ctxtoolsturn_detectionr   r   r   r   mcp_serversallow_interruptionsmin_consecutive_speech_delayuse_tts_aligned_transcriptmin_endpointing_delaymax_endpointing_delayinstructionsstrr3   
str | Noner4   "NotGivenOr[llm.ChatContext | None]r5   #list[llm.Tool | llm.Toolset] | Noner6   $NotGivenOr[TurnDetectionMode | None]r   ,NotGivenOr[stt.STT | STTModels | str | None]r   NotGivenOr[vad.VAD | None]r   @NotGivenOr[llm.LLM | llm.RealtimeModel | LLMModels | str | None]r   ,NotGivenOr[tts.TTS | TTSModels | str | None]r7   &NotGivenOr[list[mcp.MCPServer] | None]r8   NotGivenOr[bool]r9   NotGivenOr[float]r:   r;   r<   returnNonec                C  s  |pg }t | tu rd| _n|ptt | j| _|| _g |t| | _|r.|j	| jdnt
 | _|| _t|trAtj|}t|trLtj|}t|	trWtj|	}	|| _|| _|	| _|| _|| _|| _|| _|| _|| _t|
trt|
dkrd }
|
| _ d | _!d S )Ndefault_agentr5   r   )"typer2   _idr   camel_to_snake_caser,   _instructionsr   _toolscopyr   empty	_chat_ctx_turn_detection
isinstancer>   r   STTfrom_model_stringLLMTTS_stt_llm_tts_vad_allow_interruptions_min_consecutive_speech_delay_use_tts_aligned_transcript_min_endpointing_delay_max_endpointing_delaylistlen_mcp_servers	_activity)selfr=   r3   r4   r5   r6   r   r   r   r   r7   r8   r9   r:   r;   r<   r0   r0   r1   __init__#   s6   



zAgent.__init__c                 C     | j S N)rO   ri   r0   r0   r1   r3   Z      zAgent.idc                 C  rk   rl   r3   rm   r0   r0   r1   label^   rn   zAgent.labelc                 C  rk   )zb
        Returns:
            str: The core instructions that guide the agent's behavior.
        )rQ   rm   r0   r0   r1   r=   b   s   zAgent.instructionslist[llm.Tool | llm.Toolset]c                 C  
   | j  S )z
        Returns:
            list[llm.Tool | llm.ToolSet]:
                A list of function tools available to the agent.
        )rR   rS   rm   r0   r0   r1   r5   j   s   
zAgent.toolsllm.ChatContextc                 C  s   t | jjS )a  
        Provides a read-only view of the agent's current chat context.

        Returns:
            llm.ChatContext: A read-only version of the agent's conversation history.

        See Also:
            update_chat_ctx: Method to update the internal chat context.
        )r   rU   itemsrm   r0   r0   r1   r4   s   s   zAgent.chat_ctxc                   s,   | j du r|| _dS | j |I dH  dS )a  
        Updates the agent's instructions.

        If the agent is running in realtime mode, this method also updates
        the instructions for the ongoing realtime session.

        Args:
            instructions (str):
                The new instructions to set for the agent.

        Raises:
            llm.RealtimeError: If updating the realtime session instructions fails.
        N)rh   rQ   update_instructions)ri   r=   r0   r0   r1   ru      s
   
zAgent.update_instructionsc                   s   g }|D ](}t |tjtjfr|| qtj| }r$|| qtdt| d|}| j	du rLt
dd |D  | _| jj| jd| _dS | j	|I dH  dS )a  
        Updates the agent's available function tools.

        If the agent is running in realtime mode, this method also updates
        the tools for the ongoing realtime session.

        Args:
            tools (list[llm.Tool | llm.ToolSet]):
                The new list of function tools available to the agent.

        Raises:
            llm.RealtimeError: If updating the realtime session tools fails.
        zInvalid tool type: z. Expected Tool or ToolSet.Nc                 S  s   i | ]}|j |qS r0   ro   ).0toolr0   r0   r1   
<dictcomp>   s    z&Agent.update_tools.<locals>.<dictcomp>rM   )rW   r   ToolToolsetappendtool_context_resolve_wrapped_tool	TypeErrorrN   rh   re   valuesrR   rU   rS   update_tools)ri   r5   valid_toolsrw   resolved_toolr0   r0   r1   r      s   
zAgent.update_toolsTexclude_invalid_function_callsr   boolc                  sB   | j du r|j|r| jntd| _dS | j j||dI dH  dS )a"  
        Updates the agent's chat context.

        If the agent is running in realtime mode, this method also updates
        the chat context for the ongoing realtime session.

        Args:
            chat_ctx (llm.ChatContext):
                The new or updated chat context for the agent.
            exclude_invalid_function_calls (bool): Whether to exclude function calls
                and outputs not from the agent's tools.

        Raises:
            llm.RealtimeError: If updating the realtime session chat context fails.
        NrM   r   )rh   rS   rR   r   rU   update_chat_ctx)ri   r4   r   r0   r0   r1   r      s   
zAgent.update_chat_ctxc                      dS )zCalled when the task is enteredNr0   rm   r0   r0   r1   on_enter      zAgent.on_enterc                   r   )zCalled when the task is exitedNr0   rm   r0   r0   r1   on_exit   r   zAgent.on_exitturn_ctxnew_messagellm.ChatMessagec                   r   )zCalled when the user has finished speaking, and the LLM is about to respond

        This is a good opportunity to update the chat context or edit the new message before it is
        sent to the LLM.
        Nr0   )ri   r   r   r0   r0   r1   on_user_turn_completed   s   zAgent.on_user_turn_completedaudioAsyncIterable[rtc.AudioFrame]model_settingsr*   |AsyncIterable[stt.SpeechEvent | str] | Coroutine[Any, Any, AsyncIterable[stt.SpeechEvent | str]] | Coroutine[Any, Any, None]c                 C     t j| ||S )aK  
        A node in the processing pipeline that transcribes audio frames into speech events.

        By default, this node uses a Speech-To-Text (STT) capability from the current agent.
        If the STT implementation does not support streaming natively, a VAD (Voice Activity
        Detection) mechanism is required to wrap the STT.

        You can override this node with your own implementation for more flexibility (e.g.,
        custom pre-processing of audio, additional buffering, or alternative STT strategies).

        Args:
            audio (AsyncIterable[rtc.AudioFrame]): An asynchronous stream of audio frames.
            model_settings (ModelSettings): Configuration and parameters for model execution.

        Yields:
            stt.SpeechEvent: An event containing transcribed text or other STT-related data.
        )r2   defaultstt_noderi   r   r   r0   r0   r1   r      s   zAgent.stt_nodelist[llm.Tool]AsyncIterable[llm.ChatChunk | str | FlushSentinel] | Coroutine[Any, Any, AsyncIterable[llm.ChatChunk | str | FlushSentinel]] | Coroutine[Any, Any, str] | Coroutine[Any, Any, llm.ChatChunk] | Coroutine[Any, Any, None]c                 C  s   t j| |||S )a  
        A node in the processing pipeline that processes text generation with an LLM.

        By default, this node uses the agent's LLM to process the provided context. It may yield
        plain text (as `str`) for straightforward text generation, or `llm.ChatChunk` objects that
        can include text and optional tool calls. `ChatChunk` is helpful for capturing more complex
        outputs such as function calls, usage statistics, or other metadata.

        You can override this node to customize how the LLM is used or how tool invocations
        and responses are handled.

        Args:
            chat_ctx (llm.ChatContext): The context for the LLM (the conversation history).
            tools (list[FunctionTool]): A list of callable tools that the LLM may invoke.
            model_settings (ModelSettings): Configuration and parameters for model execution.

        Yields/Returns:
            str: Plain text output from the LLM.
            llm.ChatChunk: An object that can contain both text and optional tool calls.
        )r2   r   llm_node)ri   r4   r5   r   r0   r0   r1   r      s    zAgent.llm_nodetext AsyncIterable[str | TimedString]tAsyncIterable[str | TimedString] | Coroutine[Any, Any, AsyncIterable[str | TimedString]] | Coroutine[Any, Any, None]c                 C  r   )a  
        A node in the processing pipeline that finalizes transcriptions from text segments.

        This node can be used to adjust or post-process text coming from an LLM (or any other
        source) into a final transcribed form. For instance, you might clean up formatting, fix
        punctuation, or perform any other text transformations here.

        You can override this node to customize post-processing logic according to your needs.

        Args:
            text (AsyncIterable[str | TimedString]): An asynchronous stream of text segments.
            model_settings (ModelSettings): Configuration and parameters for model execution.

        Yields:
            str: Finalized or post-processed text segments.
        )r2   r   transcription_noderi   r   r   r0   r0   r1   r   !  s   zAgent.transcription_nodeAsyncIterable[str]nAsyncIterable[rtc.AudioFrame] | Coroutine[Any, Any, AsyncIterable[rtc.AudioFrame]] | Coroutine[Any, Any, None]c                 C  r   )a,  
        A node in the processing pipeline that synthesizes audio from text segments.

        By default, this node converts incoming text into audio frames using the Text-To-Speech
        from the agent.
        If the TTS implementation does not support streaming natively, it uses a sentence tokenizer
        to split text for incremental synthesis.

        You can override this node to provide different text chunking behavior, a custom TTS engine,
        or any other specialized processing.

        Args:
            text (AsyncIterable[str]): An asynchronous stream of text segments to be synthesized.
            model_settings (ModelSettings): Configuration and parameters for model execution.

        Yields:
            rtc.AudioFrame: Audio frames synthesized from the provided text.
        )r2   r   tts_noder   r0   r0   r1   r   :  s   zAgent.tts_nodec                 C  r   )zRA node processing the audio from the realtime LLM session before it is played out.)r2   r   realtime_audio_output_noder   r0   r0   r1   r   U  s   z Agent.realtime_audio_output_noder&   c                 C  s   | j du r	td| j S )z9Get the current activity context for this task (internal)Nz3no activity context found, the agent is not running)rh   RuntimeErrorrm   r0   r0   r1   _get_activity_or_raise_  s   
zAgent._get_activity_or_raisec                   @  sR   e Zd Zedd	d
ZedddZed ddZed!ddZed"ddZdS )#zAgent.defaultagentr2   r   r   r   r*   rJ   %AsyncGenerator[stt.SpeechEvent, None]c           
   
    sV  |   }|jdusJ d|j}|jjjs+|js#td|jj dtj||jd}|jj	j
}|j|d4 I dH d|jjrI|jjjrI|jjjn|jjrQ|jjnt }t | _tjtdd fd
d}t| }z2 z	3 dH W }	|	V  qs6 W tj|I dH  n
tj|I dH  w W d  I dH  dS 1 I dH sw   Y  dS )z+Default implementation for `Agent.stt_node`Nz,stt_node called but no STT node is availablez	The STT (z) does not support streaming, add a VAD to the AgentTask/VoiceAgent to enable streamingOr manually wrap your STT in a stt.StreamAdapter)r   r   conn_optionsr   rJ   rK   c                    s$    2 z3 d H W }  |  q6 d S rl   )
push_frame)framer   streamr0   r1   _forward_input  s   z.Agent.default.stt_node.<locals>._forward_inputrJ   rK   )r   r   capabilities	streamingr   r   rp   StreamAdaptersessionr   stt_conn_optionsr   _recorder_iorecording_started_at_started_attimestart_time_offsetr   log_exceptionsr   asynciocreate_taskaiocancel_and_wait)
r   r   r   activitywrapped_sttr   _audio_input_started_atr   forward_taskeventr0   r   r1   r   g  s<   



*.zAgent.default.stt_noder4   rs   r5   r   9AsyncGenerator[llm.ChatChunk | str | FlushSentinel, None]c           
   	   C s   |   }|jdusJ dt|jtjsJ d|r|jnt}|j}|jjj}|j	||||d4 I dH }|2 z	3 dH W }	|	V  q86 W d  I dH  dS 1 I dH sTw   Y  dS )z+Default implementation for `Agent.llm_node`Nz,llm_node called but no LLM node is availablezJllm_node should only be used with LLM (non-multimodal/realtime APIs) nodes)r4   r5   r+   r   )
r   r   rW   rZ   r+   r   r   r   llm_conn_optionschat)
r   r4   r5   r   r   r+   activity_llmr   r   chunkr0   r0   r1   r     s"   
.zAgent.default.llm_noder   r   $AsyncGenerator[rtc.AudioFrame, None]c           	   
    s   |   }|jdusJ d|j}|jjjs"tj|tjjddd}|jj	j
}|j|d4 I dH > d fd	d
}t| }z 2 z
3 dH W }|jV  qC6 W tj|I dH  n
tj|I dH  w W d  I dH  dS 1 I dH suw   Y  dS )z+Default implementation for `Agent.tts_node`Nz,tts_node called but no TTS node is availableT)retain_format)r   sentence_tokenizerr   rJ   rK   c                    s,   2 z3 d H W }   |  q6    d S rl   )	push_text	end_input)r   r   r   r0   r1   r     s
   z.Agent.default.tts_node.<locals>._forward_inputr   )r   r   r   r   r   r   	blingfireSentenceTokenizerr   r   tts_conn_optionsr   r   r   r   r   r   r   )	r   r   r   r   wrapped_ttsr   r   r   evr0   r   r1   r     s&   


*.zAgent.default.tts_noder   'AsyncGenerator[str | TimedString, None]c                 C s    |2 z	3 dH W }|V  q6 dS )z5Default implementation for `Agent.transcription_node`Nr0   )r   r   r   deltar0   r0   r1   r     s   z Agent.default.transcription_nodec                 C s:   |   }|jdusJ d|2 z	3 dH W }|V  q6 dS )z=Default implementation for `Agent.realtime_audio_output_node`NzJrealtime_audio_output_node called but no realtime LLM session is available)r   realtime_llm_session)r   r   r   r   r   r0   r0   r1   r     s   z(Agent.default.realtime_audio_output_nodeN)r   r2   r   r   r   r*   rJ   r   )
r   r2   r4   rs   r5   r   r   r*   rJ   r   )r   r2   r   r   r   r*   rJ   r   )r   r2   r   r   r   r*   rJ   r   )r   r2   r   r   r   r*   rJ   r   )	r,   r-   r.   staticmethodr   r   r   r   r   r0   r0   r0   r1   r   f  s    *r   llm.RealtimeSessionc                 C  s   |   j }du rtd|S )z
        Retrieve the realtime LLM session associated with the current agent.

        Raises:
            RuntimeError: If the agent is not running or the realtime LLM session is not available
        Nzno realtime LLM session)r   r   r   )ri   
rt_sessionr0   r0   r1   r     s   zAgent.realtime_llm_sessionc                 C  rk   )a  
        Retrieves the turn detection mode for identifying conversational turns.

        If this property was not set at Agent creation, but an ``AgentSession`` provides a turn detection,
        the session's turn detection mode will be used at runtime instead.

        Returns:
            NotGivenOr[TurnDetectionMode | None]: An optional turn detection mode for managing conversation flow.
        )rV   rm   r0   r0   r1   r6        zAgent.turn_detectionvalueTurnDetectionMode | Nonec                 C  s&   || _ | jd ur| jj|d d S d S )N)r6   )rV   rh   update_options)ri   r   r0   r0   r1   r6     s   
NotGivenOr[stt.STT | None]c                 C  rk   )aC  
        Retrieves the Speech-To-Text component for the agent.

        If this property was not set at Agent creation, but an ``AgentSession`` provides an STT component,
        the session's STT will be used at runtime instead.

        Returns:
            NotGivenOr[stt.STT | None]: An optional STT component.
        )r\   rm   r0   r0   r1   r      r   z	Agent.stt.NotGivenOr[llm.LLM | llm.RealtimeModel | None]c                 C  rk   )a  
        Retrieves the Language Model or RealtimeModel used for text generation.

        If this property was not set at Agent creation, but an ``AgentSession`` provides an LLM or RealtimeModel,
        the session's model will be used at runtime instead.

        Returns:
            NotGivenOr[llm.LLM | llm.RealtimeModel | None]: The language model for text generation.
        )r]   rm   r0   r0   r1   r     r   z	Agent.llmNotGivenOr[tts.TTS | None]c                 C  rk   )a^  
        Retrieves the Text-To-Speech component for the agent.

        If this property was not set at Agent creation, but an ``AgentSession`` provides a TTS component,
        the session's TTS will be used at runtime instead.

        Returns:
            NotGivenOr[tts.TTS | None]: An optional TTS component for generating audio output.
        )r^   rm   r0   r0   r1   r     r   z	Agent.ttsc                 C  rk   )ap  
        Retrieves the list of Model Context Protocol (MCP) servers providing external tools.

        If this property was not set at Agent creation, but an ``AgentSession`` provides MCP servers,
        the session's MCP servers will be used at runtime instead.

        Returns:
            NotGivenOr[list[mcp.MCPServer]]: An optional list of MCP servers.
        )rg   rm   r0   r0   r1   r7   '  r   zAgent.mcp_serversc                 C  rk   )ai  
        Retrieves the Voice Activity Detection component for the agent.

        If this property was not set at Agent creation, but an ``AgentSession`` provides a VAD component,
        the session's VAD will be used at runtime instead.

        Returns:
            NotGivenOr[vad.VAD | None]: An optional VAD component for detecting voice activity.
        )r_   rm   r0   r0   r1   r   4  r   z	Agent.vadc                 C  rk   )al  
        Indicates whether interruptions (e.g., stopping TTS playback) are allowed.

        If this property was not set at Agent creation, but an ``AgentSession`` provides a value for
        allowing interruptions, the session's value will be used at runtime instead.

        Returns:
            NotGivenOr[bool]: Whether interruptions are permitted.
        )r`   rm   r0   r0   r1   r8   A  r   zAgent.allow_interruptionsc                 C  rk   )u  
        Minimum time-in-seconds since the last detected speech before the agent
        declares the user’s turn complete. In VAD mode this effectively behaves
        like max(VAD silence, min_endpointing_delay); in STT mode it is applied
        after the STT end-of-speech signal, so it can be additive with the STT
        provider’s endpointing delay.

        If this property was set at Agent creation, it will be used at runtime instead of the session's value.
        )rc   rm   r0   r0   r1   r;   N  r   zAgent.min_endpointing_delayc                 C  rk   )z
        Maximum time-in-seconds the agent will wait before terminating the turn.

        If this property was set at Agent creation, it will be used at runtime instead of the session's value.
        )rd   rm   r0   r0   r1   r<   [  s   zAgent.max_endpointing_delayc                 C  rk   )ao  
        Retrieves the minimum consecutive speech delay for the agent.

        If this property was not set at Agent creation, but an ``AgentSession`` provides a value for
        the minimum consecutive speech delay, the session's value will be used at runtime instead.

        Returns:
            NotGivenOr[float]: The minimum consecutive speech delay.
        )ra   rm   r0   r0   r1   r9   d  r   z"Agent.min_consecutive_speech_delayc                 C  rk   )a  
        Indicates whether to use TTS-aligned transcript as the input of
        the ``transcription_node``.

        If this property was not set at Agent creation, but an ``AgentSession`` provides a value for
        the use of TTS-aligned transcript, the session's value will be used at runtime instead.

        Returns:
            NotGivenOr[bool]: Whether to use TTS-aligned transcript.
        )rb   rm   r0   r0   r1   r:   q  s   z Agent.use_tts_aligned_transcriptr'   c                 C  s
   |   jS )z
        Retrieve the VoiceAgent associated with the current agent.

        Raises:
            RuntimeError: If the agent is not running
        )r   r   rm   r0   r0   r1   r     s   
zAgent.session) r=   r>   r3   r?   r4   r@   r5   rA   r6   rB   r   rC   r   rD   r   rE   r   rF   r7   rG   r8   rH   r9   rI   r:   rH   r;   rI   r<   rI   rJ   rK   )rJ   r>   )rJ   rq   )rJ   rs   )r=   r>   rJ   rK   )r5   rq   rJ   rK   )r4   rs   r   r   rJ   rK   r   )r   rs   r   r   rJ   rK   )r   r   r   r*   rJ   r   )r4   rs   r5   r   r   r*   rJ   r   )r   r   r   r*   rJ   r   )r   r   r   r*   rJ   r   )r   r   r   r*   rJ   r   )rJ   r&   )rJ   r   )rJ   rB   )r   r   rJ   rK   )rJ   r   )rJ   r   )rJ   r   )rJ   rG   )rJ   rD   )rJ   rH   )rJ   rI   )rJ   r'   )&r,   r-   r.   r   rj   propertyr3   rp   r=   r5   r4   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   setterr   r   r   r7   r   r8   r;   r<   r9   r:   r   r0   r0   r0   r1   r2   "   s    7

 
 





"



yr2   TaskResult_Tc                      s`   e Zd Zedeeeeeeeeedd+ fddZd,dd Zd-d#d$Zd.d&d'Zd/d)d*Z  Z	S )0	AgentTaskN)r4   r5   r6   r   r   r   r   r7   r8   r;   r<   r=   r>   r4   NotGivenOr[llm.ChatContext]r5   rA   r6   rB   r   r   r   rD   r   r   r   r   r7   rG   r8   rH   r;   rI   r<   rJ   rK   c                  sD   |pg }t  j|||||||||	|
||d d| _tjt  | _d S )N)r=   r4   r5   r6   r   r   r   r   r7   r8   r;   r<   F)superrj   _AgentTask__startedr   Futurer   _AgentTask__fut)ri   r=   r4   r5   r6   r   r   r   r   r7   r8   r;   r<   	__class__r0   r1   rj     s"   zAgentTask.__init__r   c                 C  rr   rl   )r   donerm   r0   r0   r1   r     s   
zAgentTask.doneresultTaskResult_T | Exceptionc                 C  sr   | j  rt| jj dt|tr| j | n| j | | j 	  ddl
m} |d }|r7||_d S d S )Nz is already doner    )_SpeechHandleContextVar)r   r   r   r   r,   rW   	Exceptionset_exception
set_result	exceptionagent_activityr   get_maybe_run_final_output)ri   r   r   speech_handler0   r0   r1   complete  s   




zAgentTask.completer   c                   s   j rt jj dd _ t }|d u r!t jj dt|}|r*|js3t jj dd fd	d
}|| ddl	m
}m} |d }| }|j}|j}	d}
|rl|jrft jj d|j}
d|_|g}|jr|j s||jur||j |jrt|jtr|jjjstd|jj d |	j d|dI d H  |	j}|r|r| s|| | d  zTt! j"I d H W |r|
|_|	j}|	j# krt$ jj d |% I d H  S |r|r| s|&| |j'j( j'ddd}|j)|j*j)d d < |	j|dddI d H  S |r|
|_|	j}|	j# kr1t$ jj d |% I d H  w |rA|rA| sA|&| |j'j( j'ddd}|j)|j*j)d d < |	j|dddI d H  w )Nz# is not re-entrant, await only onceTz) must be executed inside an async contextzY should only be awaited inside tool_functions or the on_enter/on_exit methods of an Agent_asyncio.Task[Any]rJ   rK   c                   sB    j  rd S td jj d  td jj d d S )Nz!The asyncio.Task finished before z was completed.)r   r   r   errorr   r,   r   r   )r   rm   r0   r1   _handle_task_done  s   
z1AgentTask.__await_impl.<locals>._handle_task_doner    )_AgentActivityContextVarr   zE cannot be awaited inside a function tool that is already interruptedFzRealtime model 'z' does not support resuming function calls from chat context, using AgentTask inside a function tool may have unexpected behavior.pause)previous_activityblocked_tasksz completed, but the agent has changed in the meantime. Ignoring handoff to the previous agent, likely due to `AgentSession.update_agent` being invoked.)exclude_function_callexclude_instructionsresume)new_activitywait_on_enter)r   r   rJ   rK   )+r   r   r   r,   r   current_task_get_activity_task_infoinline_taskadd_done_callbackr   r   r   r   r   r   interruptedr8   _on_enter_taskr   r{   function_callrW   r   r   r   manual_function_callsr   r   rp   _update_activity_global_run_state_unwatch_handle_mark_done_if_neededshieldr   current_agentwarningaclose_watch_handler4   mergert   rU   )ri   r  	task_infor   r   r   r   old_activity	old_agentr   old_allow_interruptionsr   	run_statemerged_chat_ctxr0   rm   r1   __await_impl  s   









zAgentTask.__await_impl#Generator[None, None, TaskResult_T]c                 C  s   |    S rl   )_AgentTask__await_impl	__await__rm   r0   r0   r1   r   @     zAgentTask.__await__)r=   r>   r4   r   r5   rA   r6   rB   r   r   r   rD   r   r   r   r   r7   rG   r8   rH   r;   rI   r<   rI   rJ   rK   )rJ   r   )r   r   rJ   rK   )rJ   r   )rJ   r  )
r,   r-   r.   r   rj   r   r   r  r   __classcell__r0   r0   r   r1   r     s"    
#

wr   c                   @  s2   e Zd ZU dZded< dZded< dZded< dS )	_ActivityTaskInfoNzllm.FunctionCall | Noner  zSpeechHandle | Noner   Fr   r  )r,   r-   r.   r  r/   r   r  r0   r0   r0   r1   r#  D  s   
 r#  )r  r   r  taskr   r  #NotGivenOr[llm.FunctionCall | None]r   NotGivenOr[SpeechHandle | None]r  rH   rJ   rK   c                C  sH   t | pt }t|r||_t|r||_t|r||_t| d| d S N__livekit_agents_activity_task)r  r#  r   r  r   r  setattr)r$  r  r   r  infor0   r0   r1   _set_activity_task_infoK  s   r+  _ActivityTaskInfo | Nonec                 C  s   t | dd S r'  )getattr)r$  r0   r0   r1   r  `  r!  r  )
r$  r   r  r%  r   r&  r  rH   rJ   rK   )r$  r   rJ   r,  )<
__future__r   r   r   collections.abcr   r   r   r   dataclassesr   typingr   r	   r
   r   livekitr    r   r   r   r   r   r   r   r   r   r   llm.chat_contextr   logr   typesr   r   r   r   r   r   r!   r"   r#   r$   r%   r   r&   agent_sessionr'   audio_recognitionr(   ior)   r*   r2   r   r   r#  r+  r  r0   r0   r0   r1   <module>   sL    $    l 8	