
    j\                        U d dl 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  ej                  d      Zh dZee   ed<   d	eeef   d
eeef   fdZdej$                  d
efdZdeej(                     ded
ej(                  fdZy)    N)AnyDictSetType)jsonable_encoder   )messageszE^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?$>   end_time
start_timelast_updated_atDATETIME_FIELD_NAMESobjreturnc                    t         D ]m  }| j                  |      }t        |t              s%t        j                  |      s;	 t        j                  j                  |j                  dd            | |<   o | S # t        $ r Y }w xY w)zObject hook for json.loads that converts ISO format strings to datetime objects.

    Only converts values for keys listed in DATETIME_FIELD_NAMES to avoid
    false conversions of ISO-like strings in arbitrary data fields.
    Zz+00:00)
r   get
isinstancestr_ISO_DATETIME_PATTERNmatchdatetimefromisoformatreplace
ValueError)r   keyvalues      /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/message_processing/replay/message_serialization.pydatetime_object_hookr      sx     $eS!&;&A&A%&H#,,::5==h;WXC	 $ J  s   2A99	BBmessagec                 v    | j                         }t        j                  |      }t        j                  |      S )aw  
    Serializes a message object into a JSON string.

    This function converts a given message object, which must be a subclass
    of `messages.BaseMessage`, into a dictionary format suitable for database
    storage. The dictionary is first encoded via `jsonable_encoder.encode`
    (which converts datetime objects to ISO 8601 strings including timezone
    info via `datetime_utils.serialize_datetime`) and then serialized to a
    JSON string using the standard `json.dumps`.

    Args:
        message: The message object to be serialized.

    Returns:
        str: A JSON string representation of the message object.
    )as_db_message_dictr   encodejsondumps)r   dataencoded_data_dicts      r   serialize_messager'   &   s3    " %%'D(//5::'((    message_classjson_strc                 f    t        j                  |t              }t        j                  ||       S )a  
    Deserializes a JSON string into an instance of a specified message class.

    This function takes a JSON string and transforms it into an instance of the
    provided message class. The conversion process includes mapping JSON structures
    into their equivalent class representations, with custom handling of date and
    time fields.

    Args:
        message_class: The class type into which the JSON string
            will be deserialized. This must be a subclass of `messages.T`.
        json_str: The JSON string to be deserialized.

    Returns:
        An instance of the provided message class populated with data
        from the JSON string.
    )object_hook)r%   r)   )r#   loadsr   r	   from_db_message_dict)r)   r*   r%   s      r   deserialize_messager/   <   s(    $ ::h,@AD((d-PPr(   )r   r#   retypingr   r   r   r   opikr    r	   compiler   r   r   __annotations__r   BaseMessager'   Tr/    r(   r   <module>r9      s      	 ' ' !  #

L  "O c#h Nd38n c3h  )x33 ) ),QtHJJ'7 Q3 Q8:: Qr(   