o
    i/                     @  s  d dl mZ d dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ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 dd
lm Z  ddl!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( errddl)m*Z* edZ+G dd de
e+ Z,ed Z-ed Z.ed Z/G dd deZ0G dd deZ1G dd deZ2G dd deZ3G dd deZ4G d d! d!eZ5G d"d# d#eZ6G d$d% d%eZ7G d&d' d'eZ8G d(d) d)eZ9eG d*d+ d+e:eZ;G d,d- d-eZ<ee2e0B e1B e3B e4B e6B e7B e8B e9B e<B ed.d/f Z=dS )0    )annotationsN)Enumunique)TYPE_CHECKING	AnnotatedAnyGenericLiteralTypeVar)	BaseModel
ConfigDictFieldPrivateAttrmodel_validator)Self   )LLMChatMessageFunctionCallFunctionCallOutputLLMErrorRealtimeModelRealtimeModelError)logger)AgentMetrics)STTSTTError)TTSTTSError   )SpeechHandle)AgentSession
Userdata_Tc                   @  sb   e Zd Z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ddZ	dddZ
dS )
RunContextsessionAgentSession[Userdata_T]speech_handler    function_callr   returnNonec                C  s"   || _ || _|| _|jd | _d S )Nr   )_session_speech_handle_function_call	num_steps_initial_step_idx)selfr$   r&   r'    r0   ]/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/agents/voice/events.py__init__"   s   zRunContext.__init__c                 C     | j S N)r*   r/   r0   r0   r1   r$   /      zRunContext.sessionc                 C  r3   r4   )r+   r5   r0   r0   r1   r&   3   r6   zRunContext.speech_handlec                 C  r3   r4   )r,   r5   r0   r0   r1   r'   7   r6   zRunContext.function_callr"   c                 C  s   | j jS r4   )r$   userdatar5   r0   r0   r1   r7   ;   s   zRunContext.userdatac                 C  s   d| j _dS )a  Disable interruptions for this FunctionCall.

        Delegates to the SpeechHandle.allow_interruptions setter,
        which will raise a RuntimeError if the handle is already interrupted.

        Raises:
            RuntimeError: If the SpeechHandle is already interrupted.
        FN)r&   allow_interruptionsr5   r0   r0   r1   disallow_interruptions?   s   	z!RunContext.disallow_interruptionsc                   s   | j j| jdI dH  dS )aI  Waits for the speech playout corresponding to this function call step.

        Unlike `SpeechHandle.wait_for_playout`, which waits for the full
        assistant turn to complete (including all function tools),
        this method only waits for the assistant's spoken response prior running
        this tool to finish playing.)step_idxN)r&   _wait_for_generationr.   r5   r0   r0   r1   wait_for_playoutJ   s   zRunContext.wait_for_playoutN)r$   r%   r&   r    r'   r   r(   r)   )r(   r%   )r(   r    )r(   r   )r(   r"   r(   r)   )__name__
__module____qualname__r2   propertyr$   r&   r'   r7   r9   r<   r0   r0   r0   r1   r#       s    

r#   )
user_state_changedagent_state_changeduser_input_transcribedconversation_item_addedagent_false_interruptionfunction_tools_executedmetrics_collectedspeech_createderrorclose)speaking	listeningaway)initializingidlerM   thinkingrL   c                   @  >   e Zd ZU dZded< ded< ded< eejdZded	< d
S )UserStateChangedEventrB   zLiteral['user_state_changed']type	UserState	old_state	new_statedefault_factoryfloat
created_atNr>   r?   r@   rT   __annotations__r   timer[   r0   r0   r0   r1   rS   e   
   
 rS   c                   @  rR   )AgentStateChangedEventrC   zLiteral['agent_state_changed']rT   
AgentStaterV   rW   rX   rZ   r[   Nr\   r0   r0   r0   r1   r`   l   r_   r`   c                   @  sV   e Zd ZU dZded< ded< ded< dZd	ed
< dZd	ed< eejdZ	ded< dS )UserInputTranscribedEventrD   z!Literal['user_input_transcribed']rT   str
transcriptboolis_finalN
str | None
speaker_idlanguagerX   rZ   r[   )
r>   r?   r@   rT   r]   rh   ri   r   r^   r[   r0   r0   r0   r1   rb   s   s   
 rb   c                      sb   e Zd ZU dZded< ded< 	 eejdZded< d	Zd
ed< d	Z	ded< d fddZ
  ZS )AgentFalseInterruptionEventrF   z#Literal['agent_false_interruption']rT   re   resumedrX   rZ   r[   NzChatMessage | Nonemessagerg   extra_instructionsnamerc   r(   r   c                   s&   |dv rt d| d t |S )N)rl   rm   zAgentFalseInterruptionEvent.z1 is deprecated, automatic resume is now supported)r   warningsuper__getattribute__)r/   rn   	__class__r0   r1   rq      s
   
z,AgentFalseInterruptionEvent.__getattribute__)rn   rc   r(   r   )r>   r?   r@   rT   r]   r   r^   r[   rl   rm   rq   __classcell__r0   r0   rr   r1   rj   |   s   
 rj   c                   @  6   e Zd ZU dZded< ded< eejdZded< d	S )
MetricsCollectedEventrH   zLiteral['metrics_collected']rT   r   metricsrX   rZ   r[   Nr\   r0   r0   r0   r1   rv         
 rv   c                   @  s   e Zd ZU dZded< dS )_TypeDiscriminatorunknownzLiteral['unknown']rT   N)r>   r?   r@   rT   r]   r0   r0   r0   r1   ry      s   
 ry   c                   @  ru   )
ConversationItemAddedEventrE   z"Literal['conversation_item_added']rT   z ChatMessage | _TypeDiscriminatoritemrX   rZ   r[   Nr\   r0   r0   r0   r1   r{      rx   r{   c                   @  s   e Zd ZU dZded< ded< ded< eejdZd	ed
< eddZ	ded< eddZ
ded< d#ddZd$ddZd$ddZed%ddZed%ddZeddd&d d!Zd"S )'FunctionToolsExecutedEventrG   z"Literal['function_tools_executed']rT   zlist[FunctionCall]function_callszlist[FunctionCallOutput | None]function_call_outputsrX   rZ   r[   F)defaultre   _reply_required_handoff_requiredr(   4list[tuple[FunctionCall, FunctionCallOutput | None]]c                 C  s   t t| j| jddS )NF)strict)listzipr~   r   r5   r0   r0   r1   zipped   s   z!FunctionToolsExecutedEvent.zippedr)   c                 C  
   d| _ d S NFr   r5   r0   r0   r1   cancel_tool_reply      
z,FunctionToolsExecutedEvent.cancel_tool_replyc                 C  r   r   r   r5   r0   r0   r1   cancel_agent_handoff   r   z/FunctionToolsExecutedEvent.cancel_agent_handoffc                 C  r3   r4   r   r5   r0   r0   r1   has_tool_reply   r6   z)FunctionToolsExecutedEvent.has_tool_replyc                 C  r3   r4   r   r5   r0   r0   r1   has_agent_handoff   r6   z,FunctionToolsExecutedEvent.has_agent_handoffafter)moder   c                 C  s    t | jt | jkrtd| S )NzBThe number of function_calls and function_call_outputs must match.)lenr~   r   
ValueErrorr5   r0   r0   r1   verify_lists_length   s   z.FunctionToolsExecutedEvent.verify_lists_lengthN)r(   r   r=   )r(   re   )r(   r   )r>   r?   r@   rT   r]   r   r^   r[   r   r   r   r   r   r   rA   r   r   r   r   r0   r0   r0   r1   r}      s    
 


r}   c                   @  sb   e Zd ZU eddZdZded< ded< 	 ded	< 	 ed
ddZded< 	 ee	j	dZ
ded< dS )SpeechCreatedEventTarbitrary_types_allowedrI   zLiteral['speech_created']rT   re   user_initiatedz Literal['say', 'generate_reply']source.)excluder    r&   rX   rZ   r[   N)r>   r?   r@   r   model_configrT   r]   r   r&   r^   r[   r0   r0   r0   r1   r      s   
 
r   c                   @  sH   e Zd ZU eddZdZded< ded< ded< eejd	Z	d
ed< dS )
ErrorEventTr   rJ   zLiteral['error']rT   z9LLMError | STTError | TTSError | RealtimeModelError | Anyz%LLM | STT | TTS | RealtimeModel | Anyr   rX   rZ   r[   N)
r>   r?   r@   r   r   rT   r]   r   r^   r[   r0   r0   r0   r1   r      s   
 
r   c                   @  s    e Zd ZdZdZdZdZdZdS )CloseReasonrJ   job_shutdownparticipant_disconnectedr   task_completedN)r>   r?   r@   ERRORJOB_SHUTDOWNPARTICIPANT_DISCONNECTEDUSER_INITIATEDTASK_COMPLETEDr0   r0   r0   r1   r      s    r   c                   @  sB   e Zd ZU dZded< dZded< ded< eejd	Zd
ed< dS )
CloseEventrK   zLiteral['close']rT   Nz:LLMError | STTError | TTSError | RealtimeModelError | NonerJ   r   reasonrX   rZ   r[   )	r>   r?   r@   rT   r]   rJ   r   r^   r[   r0   r0   r0   r1   r      s
   
 r   rT   )discriminator)>
__future__r   r^   enumr   r   typingr   r   r   r   r	   r
   pydanticr   r   r   r   r   typing_extensionsr   llmr   r   r   r   r   r   r   logr   rw   r   sttr   r   ttsr   r   r&   r    agent_sessionr!   r"   r#   
EventTypesrU   ra   rS   r`   rb   rj   rv   ry   r{   r}   r   r   rc   r   r   
AgentEventr0   r0   r0   r1   <module>   sr     $	4	!	
