
    i              	       @   d dl Z d dlmZ erd dlmZmZ d dlmZ d dlZd dlm	Z	  G 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j#                         D ]  \  ZZeD ]  Zeee<   	  ddZddZd Zd Zd Zy)    N)TYPE_CHECKING)AnyCallable)Span)loggerc                       e Zd ZdZdZdZdZy)GEN_AI_ALLOWED_MESSAGE_ROLESsystemuser	assistanttoolN)__name__
__module____qualname__SYSTEMUSER	ASSISTANTTOOL     s/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/sentry_sdk/ai/utils.pyr	   r	      s    FDIDr   r	   r
   r   humanr   air   	tool_callc           	      r   t        | d      r	 t        | j                               S t        | t              r4rt        |       dk(  rt        | d         S t        fd| D              S t        | t              r/| j                         D ci c]  \  }}|t        |       c}}S t        | t        t        t        t        f      r| S t        |       S # t        $ rM}t	        j
                  d|       t        | t        t        t        t        f      r| n
t        |       cY d }~S d }~ww xY wc c}}w )N
model_dumpunpackz+Could not convert pydantic data to JSON: %s   r   c              3   8   K   | ]  }t        |         yw)r   N)_normalize_data).0xr   s     r   	<genexpr>z"_normalize_data.<locals>.<genexpr>.   s     D!OAf55Ds   )hasattrr!   r   	Exceptionr   warning
isinstanceintfloatboolstrlistlendictitems)datar   ekvs    `   r   r!   r!   !   s     t\"	T"4??#4VDD
 $c$i1n"476::DtDDD$CG::<P!Q?1V44PPdS%s$;<4K#d)K  	TNNH!L%dS%s,CD4#d)S	T Qs$   C D3	D0#AD+%D0+D0c                     t        ||      }t        |t        t        t        t
        f      r| j                  ||       y | j                  |t        j                  |             y )Nr   )	r!   r(   r)   r*   r+   r,   set_datajsondumps)spankeyvaluer   
normalizeds        r   set_data_normalizedr=   6   sF     v6J*sE456c:&c4::j12r   c                 .    t         j                  | |       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)roles    r   normalize_message_rolerB   ?   s     '**466r   c                     g }| D ]Z  }t        |t              s|j                  |       %|j                         }d|v rt	        |d         |d<   |j                  |       \ |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.
    rA   )r(   r/   appendcopyrB   )messagesnormalized_messagesmessagenormalized_messages       r   normalize_message_rolesrJ   H   ss      7'4(&&w/$\\^W)?)Pv&""#567 r   c                      t        j                         } | d uxr | j                  d u}|rt         j                  S t         j                  S )N)
sentry_sdkget_current_spancontaining_transaction
start_spanstart_transaction)current_spantransaction_existss     r   get_start_span_functionrS   [   sI    ..0LD T\%H%HPT%T  %7:  XJ<X<XXr   )T)r7   typingr   r   r   sentry_sdk.tracingr   rL   sentry_sdk.utilsr   r	   r   r   r   r   #GEN_AI_MESSAGE_ROLE_REVERSE_MAPPINGr?   r0   target_rolesource_rolessource_roler!   r=   rB   rJ   rS   r   r   r   <module>r[      s      $'  #  !''( %%'8 **[$,? %%'<	' # ! !D!J!J!L ?K# ?3>#K0??
L*37&Yr   