o
    i                     @   s  d Z ddlZddl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 ddlmZ G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZ G d(d) d)eZ!G d*d+ d+e!Z"G d,d- d-e!Z#G d.d/ d/e!Z$G d0d1 d1e!Z%G d2d3 d3e!Z&G d4d5 d5e!Z'G d6d7 d7e!Z(G d8d9 d9e!Z)G d:d; d;e!Z*G d<d= d=e!Z+G d>d? d?eZ,G d@dA dAe,Z-G dBdC dCe,Z.G dDdE dEe,Z/G dFdG dGe,Z0G dHdI dIe,Z1G dJdK dKe,Z2G dLdM dMe,Z3G dNdO dOe,Z4G dPdQ dQe,Z5G dRdS dSe,Z6G dTdU dUe,Z7G dVdW dWe,Z8G dXdY dYe,Z9G dZd[ d[e,Z:G d\d] d]e,Z;G d^d_ d_e,Z<G d`da dae,Z=G dbdc dce,Z>G ddde dee,Z?G dfdg dge,Z@G dhdi die,ZAG djdk dke,ZBG dldm dme,ZCG dndo doe,ZDG dpdq dqe,ZEG drds dse,ZFG dtdu due,ZGG dvdw dwe,ZHG dxdy dye,ZIG dzd{ d{e,ZJG d|d} d}e,ZKG d~d deZLG dd deZMG dd deZNG dd deZOi deJde-de.de/deHdeIdeFdeGde0de1de2de3de4de5de6de7de8e9e:e;e<e=e>e?e@eAeBeCeDeEeKdZPdd ZQdS )zGEvent models and data structures for OpenAI Realtime API communication.    N)AnyDictListLiteralOptionalUnion)	BaseModel
ConfigDictField)ToolsSchemac                   @   s   e Zd ZU dZeed< dS )AudioFormatz*Base class for audio format configuration.typeN)__name__
__module____qualname____doc__str__annotations__ r   r   [/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/services/openai/realtime/events.pyr      s   
 r   c                   @   s2   e Zd ZU dZdZed ed< dZed ed< dS )PCMAudioFormatzPCM audio format configuration.

    Parameters:
        type: Audio format type, always "audio/pcm".
        rate: Sample rate, always 24000 for PCM.
    z	audio/pcmr   i]  rateN)r   r   r   r   r   r   r   r   r   r   r   r   r         
 r   c                   @   "   e Zd ZU dZdZed ed< dS )PCMUAudioFormatu{   PCMU (G.711 μ-law) audio format configuration.

    Parameters:
        type: Audio format type, always "audio/pcmu".
    z
audio/pcmur   Nr   r   r   r   r   r   r   r   r   r   r   r   (      
 r   c                   @   r   )PCMAAudioFormatzzPCMA (G.711 A-law) audio format configuration.

    Parameters:
        type: Audio format type, always "audio/pcma".
    z
audio/pcmar   Nr   r   r   r   r   r   2   r   r   c                       sh   e Zd ZU dZdZeed< ee ed< ee ed< 			d	dee dee dee f fddZ  Z	S )
InputAudioTranscriptionz/Configuration for audio transcription settings.gpt-4o-transcribemodellanguagepromptNc                    s   t  j|||d dS )a	  Initialize InputAudioTranscription.

        Args:
            model: Transcription model to use (e.g., "gpt-4o-transcribe", "whisper-1").
            language: Optional language code for transcription.
            prompt: Optional transcription hint text.
        )r    r!   r"   N)super__init__)selfr    r!   r"   	__class__r   r   r$   C   s   z InputAudioTranscription.__init__)r   NN)
r   r   r   r   r    r   r   r   r$   __classcell__r   r   r&   r   r   <   s   
 r   c                   @   sV   e Zd ZU dZdZeed  ed< dZee	 ed< dZ
ee ed< dZee ed	< d
S )TurnDetectiona  Server-side voice activity detection configuration.

    Parameters:
        type: Detection type, must be "server_vad".
        threshold: Voice activity detection threshold (0.0-1.0). Defaults to 0.5.
        prefix_padding_ms: Padding before speech starts in milliseconds. Defaults to 300.
        silence_duration_ms: Silence duration to detect speech end in milliseconds. Defaults to 500.
    
server_vadr   g      ?	thresholdi,  prefix_padding_msi  silence_duration_msN)r   r   r   r   r   r   r   r   r+   floatr,   intr-   r   r   r   r   r)   S   s   
 	r)   c                   @   sZ   e Zd ZU dZdZeed  ed< dZeed  ed< dZ	ee
 ed< dZee
 ed< dS )	SemanticTurnDetectiona  Semantic-based turn detection configuration.

    Parameters:
        type: Detection type, must be "semantic_vad".
        eagerness: Turn detection eagerness level. Can be "low", "medium", "high", or "auto".
        create_response: Whether to automatically create responses on turn detection.
        interrupt_response: Whether to interrupt ongoing responses on turn detection.
    semantic_vadr   N)lowmediumhighauto	eagernesscreate_responseinterrupt_response)r   r   r   r   r   r   r   r   r6   r7   boolr8   r   r   r   r   r0   c   s   
 	r0   c                   @   s"   e Zd ZU dZeed  ed< dS )InputAudioNoiseReductionzInput audio noise reduction configuration.

    Parameters:
        type: Noise reduction type for different microphone scenarios.
    )
near_field	far_fieldr   N)r   r   r   r   r   r   r   r   r   r   r   r:   s   r   r:   c                   @   sf   e Zd ZU dZdZeeeee	f  e
d< dZee e
d< dZee e
d< dZeeeeef  e
d< dS )
AudioInputa:  Audio input configuration.

    Parameters:
        format: The format of the input audio.
        transcription: Configuration for input audio transcription.
        noise_reduction: Configuration for input audio noise reduction.
        turn_detection: Configuration for turn detection, or False to disable.
    Nformattranscriptionnoise_reductionturn_detection)r   r   r   r   r>   r   r   r   r   r   r   r?   r   r@   r:   rA   r)   r0   r9   r   r   r   r   r=   }   s   
 	r=   c                   @   sL   e Zd ZU dZdZeeeee	f  e
d< dZee e
d< dZee e
d< dS )AudioOutputzAudio output configuration.

    Parameters:
        format: The format of the output audio.
        voice: The voice the model uses to respond.
        speed: The speed of the model's spoken response.
    Nr>   voicespeed)r   r   r   r   r>   r   r   r   r   r   r   rC   r   rD   r.   r   r   r   r   rB      s
   
 rB   c                   @   s2   e Zd ZU dZdZee ed< dZee	 ed< dS )AudioConfigurationzAudio configuration for input and output.

    Parameters:
        input: Configuration for input audio.
        output: Configuration for output audio.
    Ninputoutput)
r   r   r   r   rF   r   r=   r   rG   rB   r   r   r   r   rE      r   rE   c                   @   s4  e Zd ZU dZeddZdZeed  e	d< dZ
eed  e	d< dZee e	d	< dZee e	d
< dZeeed   e	d< dZee e	d< dZee e	d< dZeeee B  e	d< dZeed  e	d< dZeeeed f  e	d< dZeeed ef  e	d< dZee e	d< dZee e	d< dZeee  e	d< dS )SessionPropertiesa4  Configuration properties for an OpenAI Realtime session.

    Parameters:
        type: The type of session, always "realtime".
        object: Object type identifier, always "realtime.session".
        id: Unique identifier for the session.
        model: The Realtime model used for this session.
            Note: The model is set at connection time via model arg in __init__
            and cannot be changed during the session.
        output_modalities: The set of modalities the model can respond with.
        instructions: System instructions for the assistant.
        audio: Configuration for input and output audio.
        tools: Available function tools for the assistant.
        tool_choice: Tool usage strategy ("auto", "none", or "required").
        max_output_tokens: Maximum tokens in response or "inf" for unlimited.
        tracing: Configuration options for tracing.
        prompt: Reference to a prompt template and its variables.
        expires_at: Session expiration timestamp.
        include: Additional fields to include in server outputs.
    Tarbitrary_types_allowedrealtimer   Nzrealtime.sessionobjectidr    textaudiooutput_modalitiesinstructionsrP   toolsr5   nonerequiredtool_choiceinfmax_output_tokensr5   tracingr"   
expires_atinclude)r   r   r   r   r	   model_configr   r   r   r   rL   rM   r   r    rQ   r   rR   rP   rE   rS   r   r   rW   rY   r   r/   rZ   r"   r[   r\   r   r   r   r   rH      s"   
 
rH   c                   @   sr   e Zd ZU dZed ed< dZee ed< dZ	ee ed< dZ
ee ed< dZee ed< dZeed	  ed
< dS )ItemContenta  Content within a conversation item.

    Parameters:
        type: Content type (text, audio, input_text, input_audio, input_image, output_text, or output_audio).
        text: Text content for text-based items.
        audio: Base64-encoded audio data for audio items.
        transcript: Transcribed text for audio items.
        image_url: Base64-encoded image data as a data URI for input_image items.
        detail: Detail level for image processing ("auto", "low", or "high").
    )rO   rP   
input_textinput_audioinput_imageoutput_textoutput_audior   NrO   rP   
transcript	image_url)r5   r2   r4   detail)r   r   r   r   r   r   rO   r   r   rP   rd   re   rf   r   r   r   r   r^      s   
 r^   c                   @   s   e Zd ZU dZedd dZeed< dZe	e
d  ed< e
d	 ed
< dZe	e
d  ed< dZe	e
d  ed< dZe	ee  ed< dZe	e ed< dZe	e ed< dZe	e ed< dZe	e ed< dS )ConversationItema  A conversation item in the realtime session.

    Parameters:
        id: Unique identifier for the item, auto-generated if not provided.
        object: Object type identifier for the realtime API.
        type: Item type (message, function_call, or function_call_output).
        status: Current status of the item.
        role: Speaker role for message items (user, assistant, or system).
        content: Content list for message items.
        call_id: Function call identifier for function_call items.
        name: Function name for function_call items.
        arguments: Function arguments as JSON string for function_call items.
        output: Function output as JSON string for function_call_output items.
    c                   C   s   t t jS N)r   uuiduuid4hexr   r   r   r   <lambda>   s    zConversationItem.<lambda>default_factoryrM   Nzrealtime.itemrL   )messagefunction_callfunction_call_outputr   )	completedin_progress
incompletestatus)user	assistantsystemrolecontentcall_idname	argumentsrG   )r   r   r   r   r
   rM   r   r   rL   r   r   ru   ry   rz   r   r^   r{   r|   r}   rG   r   r   r   r   rg      s   
 rg   c                   @   s&   e Zd ZU dZeed< ed ed< dS )RealtimeConversationzA realtime conversation session.

    Parameters:
        id: Unique identifier for the conversation.
        object: Object type identifier, always "realtime.conversation".
    rM   zrealtime.conversationrL   N)r   r   r   r   r   r   r   r   r   r   r   r~     s   
 r~   c                   @   s   e Zd ZU dZdgZeeed   ed< dZ	ee
 ed< dZee ed< dZeee  ed< dZeed  ed	< dZee ed
< dZeeeed f  ed< dS )ResponsePropertiesa  Properties for configuring assistant responses.

    Parameters:
        output_modalities: Output modalities for the response. Must be either ["text"] or ["audio"]. Defaults to ["audio"].
        instructions: Specific instructions for this response.
        audio: Audio configuration for this response.
        tools: Available tools for this response.
        tool_choice: Tool usage strategy for this response.
        temperature: Sampling temperature for this response.
        max_output_tokens: Maximum tokens for this response.
    rP   rN   rQ   NrR   rS   rT   rW   temperaturerX   rY   )r   r   r   r   rQ   r   r   r   r   rR   r   rP   rE   rS   r   rW   r   r.   rY   r   r/   r   r   r   r   r     s   
  r   c                   @   sR   e Zd ZU dZeed< dZee ed< eed< dZee ed< dZ	ee ed< dS )	RealtimeErrora>  Error information from the realtime API.

    Parameters:
        type: Error type identifier.
        code: Specific error code.
        message: Human-readable error message.
        param: Parameter name that caused the error, if applicable.
        event_id: Event ID associated with the error, if applicable.
    r    codero   Nparamevent_id)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   1  s   
 
r   c                   @   s(   e Zd ZU dZedd dZeed< dS )ClientEventzBase class for client events sent to the realtime API.

    Parameters:
        event_id: Unique identifier for the event, auto-generated if not provided.
    c                   C   s   t t S rh   )r   ri   rj   r   r   r   r   rl   O  s    zClientEvent.<lambda>rm   r   N)r   r   r   r   r
   r   r   r   r   r   r   r   r   H  s   
 r   c                       sH   e Zd ZU dZdZed ed< eed< dee	e
f f fddZ  ZS )SessionUpdateEventzEvent to update session properties.

    Parameters:
        type: Event type, always "session.update".
        session: Updated session properties.
    zsession.updater   sessionreturnc                    s   t  j|i |}d|d v rE|d d rEd|d d v rE|d d d rEd|d d d v rE|d d d d du rEd|d d d d< |S )a`  Serialize the event to a dictionary.

        Handles special serialization for turn_detection where False becomes null.

        Args:
            *args: Positional arguments passed to parent model_dump.
            **kwargs: Keyword arguments passed to parent model_dump.

        Returns:
            Dictionary representation of the event.
        rP   r   rF   rA   FN)r#   
model_dump)r%   argskwargsdumpr&   r   r   r   ]  s    zSessionUpdateEvent.model_dump)r   r   r   r   r   r   r   rH   r   r   r   r   r(   r   r   r&   r   r   R  s
   
 "r   c                   @   *   e Zd ZU dZdZed ed< eed< dS )InputAudioBufferAppendEventzEvent to append audio data to the input buffer.

    Parameters:
        type: Event type, always "input_audio_buffer.append".
        audio: Base64-encoded audio data to append.
    zinput_audio_buffer.appendr   rP   Nr   r   r   r   r   r   r   r   r   r   r   r   r   u     
 r   c                   @   r   )InputAudioBufferCommitEventzEvent to commit the current input audio buffer.

    Parameters:
        type: Event type, always "input_audio_buffer.commit".
    zinput_audio_buffer.commitr   Nr   r   r   r   r   r     r   r   c                   @   r   )InputAudioBufferClearEventzyEvent to clear the input audio buffer.

    Parameters:
        type: Event type, always "input_audio_buffer.clear".
    zinput_audio_buffer.clearr   Nr   r   r   r   r   r     r   r   c                   @   s:   e Zd ZU dZdZed ed< dZee	 ed< e
ed< dS )ConversationItemCreateEventzEvent to create a new conversation item.

    Parameters:
        type: Event type, always "conversation.item.create".
        previous_item_id: ID of the item to insert after, if any.
        item: The conversation item to create.
    zconversation.item.creater   Nprevious_item_iditem)r   r   r   r   r   r   r   r   r   r   rg   r   r   r   r   r     s
   
 r   c                   @   s:   e Zd ZU dZdZed ed< eed< eed< eed< dS )ConversationItemTruncateEventaI  Event to truncate a conversation item's audio content.

    Parameters:
        type: Event type, always "conversation.item.truncate".
        item_id: ID of the item to truncate.
        content_index: Index of the content to truncate within the item.
        audio_end_ms: End time in milliseconds for the truncated audio.
    zconversation.item.truncater   item_idcontent_indexaudio_end_msN)	r   r   r   r   r   r   r   r   r/   r   r   r   r   r     s   
 	r   c                   @   r   )ConversationItemDeleteEventzEvent to delete a conversation item.

    Parameters:
        type: Event type, always "conversation.item.delete".
        item_id: ID of the item to delete.
    zconversation.item.deleter   r   Nr   r   r   r   r   r     r   r   c                   @   r   )ConversationItemRetrieveEventzEvent to retrieve a conversation item by ID.

    Parameters:
        type: Event type, always "conversation.item.retrieve".
        item_id: ID of the item to retrieve.
    zconversation.item.retriever   r   Nr   r   r   r   r   r     r   r   c                   @   s2   e Zd ZU dZdZed ed< dZee	 ed< dS )ResponseCreateEventzEvent to create a new assistant response.

    Parameters:
        type: Event type, always "response.create".
        response: Optional response configuration properties.
    zresponse.creater   Nresponse)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     r   r   c                   @   r   )ResponseCancelEventzyEvent to cancel the current assistant response.

    Parameters:
        type: Event type, always "response.cancel".
    zresponse.cancelr   Nr   r   r   r   r   r     r   r   c                   @   s,   e Zd ZU dZeddZeed< eed< dS )ServerEventzBase class for server events received from the realtime API.

    Parameters:
        event_id: Unique identifier for the event.
        type: Type of the server event.
    TrI   r   r   N)r   r   r   r   r	   r]   r   r   r   r   r   r   r     s
   
 
r   c                   @   &   e Zd ZU dZed ed< eed< dS )SessionCreatedEventzEvent indicating a session has been created.

    Parameters:
        type: Event type, always "session.created".
        session: The created session properties.
    session.createdr   r   Nr   r   r   r   r   r   rH   r   r   r   r   r        
 r   c                   @   r   )SessionUpdatedEventzEvent indicating a session has been updated.

    Parameters:
        type: Event type, always "session.updated".
        session: The updated session properties.
    session.updatedr   r   Nr   r   r   r   r   r      r   r   c                   @   r   )ConversationCreatedzEvent indicating a conversation has been created.

    Parameters:
        type: Event type, always "conversation.created".
        conversation: The created conversation.
    conversation.createdr   conversationN)r   r   r   r   r   r   r~   r   r   r   r   r     r   r   c                   @   6   e Zd ZU dZed ed< dZee ed< e	ed< dS )ConversationItemAddedzEvent indicating a conversation item has been added.

    Parameters:
        type: Event type, always "conversation.item.added".
        previous_item_id: ID of the previous item, if any.
        item: The added conversation item.
    conversation.item.addedr   Nr   r   
r   r   r   r   r   r   r   r   r   rg   r   r   r   r   r     
   
 r   c                   @   r   )ConversationItemDonezEvent indicating a conversation item is done processing.

    Parameters:
        type: Event type, always "conversation.item.done".
        previous_item_id: ID of the previous item, if any.
        item: The completed conversation item.
    conversation.item.doner   Nr   r   r   r   r   r   r   r   &  r   r   c                   @   6   e Zd ZU dZed ed< eed< eed< eed< dS ),ConversationItemInputAudioTranscriptionDeltaaO  Event containing incremental input audio transcription.

    Parameters:
        type: Event type, always "conversation.item.input_audio_transcription.delta".
        item_id: ID of the conversation item being transcribed.
        content_index: Index of the content within the item.
        delta: Incremental transcription text.
    1conversation.item.input_audio_transcription.deltar   r   r   deltaNr   r   r   r   r   r   r   r/   r   r   r   r   r   4     
 	r   c                   @   r   )0ConversationItemInputAudioTranscriptionCompletedaX  Event indicating input audio transcription is complete.

    Parameters:
        type: Event type, always "conversation.item.input_audio_transcription.completed".
        item_id: ID of the conversation item that was transcribed.
        content_index: Index of the content within the item.
        transcript: Complete transcription text.
    5conversation.item.input_audio_transcription.completedr   r   r   rd   Nr   r   r   r   r   r   D  r   r   c                   @   6   e Zd ZU dZed ed< eed< eed< eed< dS )-ConversationItemInputAudioTranscriptionFaileda`  Event indicating input audio transcription failed.

    Parameters:
        type: Event type, always "conversation.item.input_audio_transcription.failed".
        item_id: ID of the conversation item that failed transcription.
        content_index: Index of the content within the item.
        error: Error details for the transcription failure.
    2conversation.item.input_audio_transcription.failedr   r   r   errorN)	r   r   r   r   r   r   r   r/   r   r   r   r   r   r   T  r   r   c                   @   s6   e Zd ZU dZed ed< eed< eed< eed< dS )ConversationItemTruncatedaK  Event indicating a conversation item has been truncated.

    Parameters:
        type: Event type, always "conversation.item.truncated".
        item_id: ID of the truncated conversation item.
        content_index: Index of the content within the item.
        audio_end_ms: End time in milliseconds for the truncated audio.
    conversation.item.truncatedr   r   r   r   Nr   r   r   r   r   r   d  r   r   c                   @   r   )ConversationItemDeletedzEvent indicating a conversation item has been deleted.

    Parameters:
        type: Event type, always "conversation.item.deleted".
        item_id: ID of the deleted conversation item.
    conversation.item.deletedr   r   N)r   r   r   r   r   r   r   r   r   r   r   r   t  r   r   c                   @   r   )ConversationItemRetrievedzEvent containing a retrieved conversation item.

    Parameters:
        type: Event type, always "conversation.item.retrieved".
        item: The retrieved conversation item.
    conversation.item.retrievedr   r   N)r   r   r   r   r   r   rg   r   r   r   r   r     r   r   c                   @   &   e Zd ZU dZed ed< ded< dS )ResponseCreatedzEvent indicating an assistant response has been created.

    Parameters:
        type: Event type, always "response.created".
        response: The created response object.
    response.createdr   Responser   Nr   r   r   r   r   r   r   r   r   r   r     r   r   c                   @   r   )ResponseDonezEvent indicating an assistant response is complete.

    Parameters:
        type: Event type, always "response.done".
        response: The completed response object.
    response.doner   r   r   Nr   r   r   r   r   r     r   r   c                   @   r   )ResponseOutputItemAddeda  Event indicating an output item has been added to a response.

    Parameters:
        type: Event type, always "response.output_item.added".
        response_id: ID of the response.
        output_index: Index of the output item.
        item: The added conversation item.
    response.output_item.addedr   response_idoutput_indexr   N	r   r   r   r   r   r   r   r/   rg   r   r   r   r   r     r   r   c                   @   r   )ResponseOutputItemDonea  Event indicating an output item is complete.

    Parameters:
        type: Event type, always "response.output_item.done".
        response_id: ID of the response.
        output_index: Index of the output item.
        item: The completed conversation item.
    response.output_item.doner   r   r   r   Nr   r   r   r   r   r     r   r   c                   @   F   e Zd ZU dZed ed< eed< eed< eed< eed< eed< d	S )
ResponseContentPartAddedar  Event indicating a content part has been added to a response.

    Parameters:
        type: Event type, always "response.content_part.added".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        content_index: Index of the content part.
        part: The added content part.
    response.content_part.addedr   r   r   r   r   partN	r   r   r   r   r   r   r   r/   r^   r   r   r   r   r        
 r   c                   @   r   )
ResponseContentPartDonead  Event indicating a content part is complete.

    Parameters:
        type: Event type, always "response.content_part.done".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        content_index: Index of the content part.
        part: The completed content part.
    response.content_part.doner   r   r   r   r   r   Nr   r   r   r   r   r     r   r   c                   @   F   e Zd ZU dZed ed< eed< eed< eed< eed< eed< d	S )
ResponseTextDeltaai  Event containing incremental text from a response.

    Parameters:
        type: Event type, always "response.output_text.delta".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        content_index: Index of the content part.
        delta: Incremental text content.
    response.output_text.deltar   r   r   r   r   r   Nr   r   r   r   r   r     r   r   c                   @   r   )
ResponseTextDonea\  Event indicating text content is complete.

    Parameters:
        type: Event type, always "response.output_text.done".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        content_index: Index of the content part.
        text: Complete text content.
    response.output_text.doner   r   r   r   r   rO   Nr   r   r   r   r   r      r   r   c                   @   r   )
ResponseAudioTranscriptDeltaa  Event containing incremental audio transcript from a response.

    Parameters:
        type: Event type, always "response.output_audio_transcript.delta".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        content_index: Index of the content part.
        delta: Incremental transcript text.
    &response.output_audio_transcript.deltar   r   r   r   r   r   Nr   r   r   r   r   r     r   r   c                   @   r   )
ResponseAudioTranscriptDoneau  Event indicating audio transcript is complete.

    Parameters:
        type: Event type, always "response.output_audio_transcript.done".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        content_index: Index of the content part.
        transcript: Complete transcript text.
    %response.output_audio_transcript.doner   r   r   r   r   rd   Nr   r   r   r   r   r   (  r   r   c                   @   r   )
ResponseAudioDeltaa}  Event containing incremental audio data from a response.

    Parameters:
        type: Event type, always "response.output_audio.delta".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        content_index: Index of the content part.
        delta: Base64-encoded incremental audio data.
    response.output_audio.deltar   r   r   r   r   r   Nr   r   r   r   r   r   <  r   r   c                   @   s>   e Zd ZU dZed ed< eed< eed< eed< eed< dS )	ResponseAudioDonea9  Event indicating audio content is complete.

    Parameters:
        type: Event type, always "response.output_audio.done".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        content_index: Index of the content part.
    response.output_audio.doner   r   r   r   r   Nr   r   r   r   r   r   P  s   
 
r   c                   @   F   e Zd ZU dZed ed< eed< eed< eed< eed< eed< d	S )
"ResponseFunctionCallArgumentsDeltaa~  Event containing incremental function call arguments.

    Parameters:
        type: Event type, always "response.function_call_arguments.delta".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        call_id: ID of the function call.
        delta: Incremental function arguments as JSON.
    &response.function_call_arguments.deltar   r   r   r   r{   r   Nr   r   r   r   r   r   b  r   r   c                   @   r   )
!ResponseFunctionCallArgumentsDonea  Event indicating function call arguments are complete.

    Parameters:
        type: Event type, always "response.function_call_arguments.done".
        response_id: ID of the response.
        item_id: ID of the conversation item.
        output_index: Index of the output item.
        call_id: ID of the function call.
        arguments: Complete function arguments as JSON string.
    %response.function_call_arguments.doner   r   r   r   r{   r}   Nr   r   r   r   r   r   v  r   r   c                   @   .   e Zd ZU dZed ed< eed< eed< dS )InputAudioBufferSpeechStarteda  Event indicating speech has started in the input audio buffer.

    Parameters:
        type: Event type, always "input_audio_buffer.speech_started".
        audio_start_ms: Start time of speech in milliseconds.
        item_id: ID of the associated conversation item.
    !input_audio_buffer.speech_startedr   audio_start_msr   Nr   r   r   r   r   r   r/   r   r   r   r   r   r     
   
 r   c                   @   r   )InputAudioBufferSpeechStoppeda  Event indicating speech has stopped in the input audio buffer.

    Parameters:
        type: Event type, always "input_audio_buffer.speech_stopped".
        audio_end_ms: End time of speech in milliseconds.
        item_id: ID of the associated conversation item.
    !input_audio_buffer.speech_stoppedr   r   r   Nr   r   r   r   r   r     r   r   c                   @   s6   e Zd ZU dZed ed< dZee ed< eed< dS )InputAudioBufferCommitteda  Event indicating the input audio buffer has been committed.

    Parameters:
        type: Event type, always "input_audio_buffer.committed".
        previous_item_id: ID of the previous item, if any.
        item_id: ID of the committed conversation item.
    input_audio_buffer.committedr   Nr   r   )	r   r   r   r   r   r   r   r   r   r   r   r   r   r     r   r   c                   @   s   e Zd ZU dZed ed< dS )InputAudioBufferClearedzEvent indicating the input audio buffer has been cleared.

    Parameters:
        type: Event type, always "input_audio_buffer.cleared".
    input_audio_buffer.clearedr   Nr   r   r   r   r   r     s   
 r   c                   @   s&   e Zd ZU dZed ed< eed< dS )
ErrorEventzEvent indicating an error occurred.

    Parameters:
        type: Event type, always "error".
        error: Error details.
    r   r   N)r   r   r   r   r   r   r   r   r   r   r   r     r   r   c                   @   s2   e Zd ZU dZed ed< eeee	f  ed< dS )RateLimitsUpdatedzEvent indicating rate limits have been updated.

    Parameters:
        type: Event type, always "rate_limits.updated".
        rate_limits: List of rate limit information.
    rate_limits.updatedr   rate_limitsN)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
 r   c                   @   s2   e Zd ZU dZdZee ed< dZee ed< dS )CachedTokensDetailszDetails about cached tokens.

    Parameters:
        text_tokens: Number of cached text tokens.
        audio_tokens: Number of cached audio tokens.
    r   text_tokensaudio_tokensN)	r   r   r   r   r   r   r/   r   r   r   r   r   r   r     r   r   c                   @   sl   e Zd ZU dZeddZdZee e	d< dZ
ee e	d< dZee e	d< dZee e	d	< dZee e	d
< dS )TokenDetailsa  Detailed token usage information.

    Parameters:
        cached_tokens: Number of cached tokens used. Defaults to 0.
        text_tokens: Number of text tokens used. Defaults to 0.
        audio_tokens: Number of audio tokens used. Defaults to 0.
        cached_tokens_details: Detailed breakdown of cached tokens.
        image_tokens: Number of image tokens used (for input only).
    allow)extrar   cached_tokensr   r   Ncached_tokens_detailsimage_tokens)r   r   r   r   r	   r]   r  r   r/   r   r   r   r  r   r  r   r   r   r   r     s   
 

r   c                   @   s:   e Zd ZU dZeed< eed< eed< eed< eed< dS )Usageae  Token usage statistics for a response.

    Parameters:
        total_tokens: Total number of tokens used.
        input_tokens: Number of input tokens used.
        output_tokens: Number of output tokens used.
        input_token_details: Detailed breakdown of input token usage.
        output_token_details: Detailed breakdown of output token usage.
    total_tokensinput_tokensoutput_tokensinput_token_detailsoutput_token_detailsN)r   r   r   r   r/   r   r   r   r   r   r   r    s   
 
r  c                   @   s   e Zd ZU dZeed< ed ed< ed ed< eed< ee	 ed< d	Z
eeed
   ed< d	Zeeeed f  ed< d	Zee ed< d	Zee ed< d	Zee ed< d	Zee ed< d	Zee ed< d	S )r   a.  A complete assistant response.

    Parameters:
        id: Unique identifier for the response.
        object: Object type, always "realtime.response".
        status: Current status of the response.
        status_details: Additional status information.
        output: List of conversation items in the response.
        conversation_id: Which conversation the response is added to.
        output_modalities: The set of modalities the model used to respond.
        max_output_tokens: Maximum number of output tokens used.
        audio: Audio configuration for the response.
        usage: Token usage statistics for the response.
        voice: The voice the model used to respond.
        temperature: Sampling temperature used for the response.
        output_audio_format: The format of output audio.
    rM   zrealtime.responserL   )rr   rs   rt   	cancelledfailedru   status_detailsrG   NrN   rQ   rX   rY   rP   usagerC   r   output_audio_format)r   r   r   r   r   r   r   r   r   rg   rQ   r   rY   r   r/   rP   rE   r  r  rC   r   r.   r  r   r   r   r   r     s   
 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   c              
   C   sd   zt | }|d }|tvrtd| t| |W S  ty1 } z	t| d|  d}~ww )a  Parse a server event from JSON string.

    Args:
        str: JSON string containing the server event.

    Returns:
        Parsed server event object of the appropriate type.

    Raises:
        Exception: If the event type is unimplemented or parsing fails.
    r   z!Unimplemented server event type: z 

N)jsonloads_server_event_types	Exceptionmodel_validate)r   event
event_typeer   r   r   parse_server_eventL  s   
r  )Rr   r  ri   typingr   r   r   r   r   r   pydanticr   r	   r
   %pipecat.adapters.schemas.tools_schemar   r   r   r   r   r   r)   r0   r:   r=   rB   rE   rH   r^   rg   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  r  r   r   r   r   <module>   s    


1
#


!	
#