o
    iL                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlm  m	  m
Z ddlmZmZmZmZmZmZmZmZ ddlmZ ejG dd dZG d	d
 d
eZdS )z)Protobuf frame serialization for Pipecat.    N)Optional)logger)FrameInputAudioRawFrameInputTransportMessageFrameOutputAudioRawFrameOutputTransportMessageFrame!OutputTransportMessageUrgentFrame	TextFrameTranscriptionFrame)FrameSerializerc                   @   s   e Zd ZU dZeed< dS )MessageFramezData class for converting transport messages into Protobuf format.

    Parameters:
        data: JSON-encoded message data for transport.
    dataN)__name__
__module____qualname____doc__str__annotations__ r   r   P/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/serializers/protobuf.pyr      s   
 r   c                       s   e Zd ZdZedededediZdd e	 D Z
edededediZdd e	 D Zdd
eej f fddZdedeeB d	B fddZdeeB ded	B fddZ  ZS )ProtobufFrameSerializera#  Serializer for converting Pipecat frames to/from Protocol Buffer format.

    Provides efficient binary serialization for frame transport over network
    connections. Supports text, audio, transcription, and message frames with
    automatic conversion between transport message types.
    textaudiotranscriptionmessagec                 C      i | ]\}}||qS r   r   .0kvr   r   r   
<dictcomp>6       z"ProtobufFrameSerializer.<dictcomp>c                 C   r   r   r   r   r   r   r   r!   >   r"   Nparamsc                    s   t  | dS )zoInitialize the Protobuf frame serializer.

        Args:
            params: Configuration parameters.
        N)super__init__)selfr#   	__class__r   r   r%   @   s   z ProtobufFrameSerializer.__init__framereturnc                    s   t |ttfr| |rdS tt|jd}t	 }t
|| jvr0tdt
| d dS | jt
| }t||}t|D ]}t||j}|rXt||jrXt||j| qA| S )zSerialize a frame to Protocol Buffer binary format.

        Args:
            frame: The frame to serialize.

        Returns:
            Serialized frame as bytes, or None if frame type is not serializable.
        N)r   zFrame type z is not serializable)
isinstancer   r	   should_ignore_framer   jsondumpsr   frame_protosr   typeSERIALIZABLE_TYPESr   warninggetattrdataclassesfieldsnamehasattrsetattrSerializeToString)r&   r)   proto_frameproto_optional_name
proto_attrfieldvaluer   r   r   	serializeH   s&   



z!ProtobufFrameSerializer.serializer   c              
      sl  t j|}|d}|| jvrtd dS | j| }t||}i }|jj	| j
jD ]}t||j||j< q,t|dd}t|dd}	t|dd}
d|v rQ|d= d|v rX|d= d|v r_|d= |tkrzt|d }t|d}td	|  W n" ty } ztd
|  W Y d}~dS d}~ww |di |}|rt|d| |	rt|d|	 |
rt|d|
 |S )zDeserialize Protocol Buffer binary data to a frame.

        Args:
            data: Binary protobuf data to deserialize.

        Returns:
            Deserialized frame instance, or None if deserialization fails.
        r)   z#Unable to deserialize a valid frameNidr6   ptsr   )r   z+ProtobufFrameSerializer: Transport message z!Error parsing MessageFrame data: r   )r/   r   
FromString
WhichOneofDESERIALIZABLE_FIELDSr   errorr3   
DESCRIPTORfields_by_namemessage_typer5   r6   r   r-   loadsr   debug	Exceptionr8   )r&   r   protowhich
class_nameargs	args_dictr=   r@   r6   rA   msginstanceer   r   r   deserializeh   sL   	





z#ProtobufFrameSerializer.deserialize)N)r   r   r   r   r
   r   r   r   r1   itemsSERIALIZABLE_FIELDSr   DESERIALIZABLE_TYPESrD   r   r   InputParamsr%   r   r   bytesr?   rT   __classcell__r   r   r'   r   r   (   s"    " r   )r   r4   r-   typingr   logurur   #pipecat.frames.protobufs.frames_pb2frames	protobufs
frames_pb2r/   pipecat.frames.framesr   r   r   r   r   r	   r
   r   #pipecat.serializers.base_serializerr   	dataclassr   r   r   r   r   r   <module>   s   (

