o
    0 i=                  	   @   s$  d dl Z d dlmZ d dlmZ d dlmZ er.d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlZd dlmZ dZG d	d
 d
ZejdgejddgejddgejddgiZi Ze D ]\ZZeD ]Zeee< qbq\d#ddZd#ddZdd Z dd Z!dd Z"dd Z#efdd Z$efd!d"Z%dS )$    N)deque)TYPE_CHECKING)	getsizeof)AnyCallableDictListOptionalTuple)Span)loggeri N  c                   @   s   e Zd ZdZdZdZdZdS )GEN_AI_ALLOWED_MESSAGE_ROLESsystemuser	assistanttoolN)__name__
__module____qualname__SYSTEMUSER	ASSISTANTTOOL r   r   _/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/ai/utils.pyr      s
    r   r   r   Zhumanr   Zair   Z	tool_callTc              
      s   t | dr6z	t|   dW S  ty5 } ztd| t| ttt	t
fr'| nt
| W  Y d }~S d }~ww t| trV rKt| dkrKt| d  dS t fdd| D S t| trf fdd	|  D S t| ttt	t
frq| S t
| S )
N
model_dumpunpackz+Could not convert pydantic data to JSON: %s   r   c                 3   s    | ]	}t | d V  qdS )r   N_normalize_data).0xr   r   r   	<genexpr>2   s    z"_normalize_data.<locals>.<genexpr>c                    s   i | ]\}}|t | d qS )r   r   )r!   kvr   r   r   
<dictcomp>5   s    z#_normalize_data.<locals>.<dictcomp>)hasattrr    r   	Exceptionr   warning
isinstanceintfloatboolstrlistlendictitems)datar   er   r   r   r    %   s   
*

r    c                 C   sD   t ||d}t|ttttfr| || d S | |t| d S )Nr   )	r    r*   r+   r,   r-   r.   set_datajsondumps)spankeyvaluer   
normalizedr   r   r   set_data_normalized:   s   r<   c                 C   s   t | | S )z
    Normalize a message role to one of the 4 allowed gen_ai role values.
    Maps "ai" -> "assistant" and keeps other standard roles unchanged.
    )GEN_AI_MESSAGE_ROLE_MAPPINGget)roler   r   r   normalize_message_roleC   s   r@   c                 C   sR   g }| D ]"}t |ts|| q| }d|v r!t|d |d< || q|S )z
    Normalize roles in a list of messages to use standard gen_ai role values.
    Creates a deep copy to avoid modifying the original messages.
    r?   )r*   r1   appendcopyr@   )messagesZnormalized_messagesmessageZnormalized_messager   r   r   normalize_message_rolesL   s   

rE   c                  C   s*   t  } | d uo| jd u}|rt jS t jS )N)
sentry_sdkZget_current_spanZcontaining_transactionZ
start_spanZstart_transaction)Zcurrent_spanZtransaction_existsr   r   r   get_start_span_function_   s   rG   c                 C   sZ   d}t t| d ddD ]}ttj| | ddd}||7 }||kr*|d   S qdS )z
    Find the index of the first message that would exceed the max bytes limit.
    Compute the individual message sizes, and return the index of the first message from the back
    of the list that would exceed the max bytes limit.
    r   r   ,:
separatorsutf-8)ranger0   r6   r7   encode)rC   	max_bytesZrunning_sumidxsizer   r   r   _find_truncation_indexh   s   rT   c                 C   sF   t j| dd}t|d}||kr| dfS t| |}| |d  |fS )NrI   rL   rN   r   )r6   r7   r0   rP   rT   )rC   rQ   Zserialized_jsonZcurrent_sizeZtruncation_indexr   r   r   truncate_messages_by_sizey   s   
rU   c                 C   s2   | sd S t | |\}}|dkrt| |j|j< |S )Nr   )rU   r0   Z_gen_ai_original_message_countZspan_id)rC   r8   scoperQ   Ztruncated_messagesremoved_countr   r   r   truncate_and_annotate_messages   s   rX   )T)&r6   collectionsr   typingr   sysr   r   r   r   r   r	   r
   Zsentry_sdk.tracingr   rF   Zsentry_sdk.utilsr   ZMAX_GEN_AI_MESSAGE_BYTESr   r   r   r   r   Z#GEN_AI_MESSAGE_ROLE_REVERSE_MAPPINGr=   r2   Ztarget_roleZsource_rolesZsource_roler    r<   r@   rE   rG   rT   rU   rX   r   r   r   r   <module>   s<     





			