
    %Gg                     X   d dl Z d dlZd dlmZmZmZ 	 d dlmZ dZ ej                  e      Zed   Zerddeded	efd
Zdej                   d	efdZdej                   d	efdZdej&                  d	eeef   fdZdej*                  d	eeef   fdZdej.                  d	eeef   fdZdej2                  d	eeef   fdZdej6                  d	eeef   fdZdej:                  d	eeef   fdZdej>                  d	eeef   fdZ dej                   d	eeef   fdZ!yy# e	$ r dZY w xY w)    N)AnyDictLiteral)tracingTF)toolchainllm	retriever	embeddingpromptparserdatadefault_keyreturnc                     t        | t              r| }|S t        | t              r/	 t        j                  |       }t        |t              r|}n|| i}|S | Gt        | d      r;t        | j                        r&t        | t              s	 | j                  dd      }|S || i}|S # t        j
                  $ r || i}Y |S w xY w# t        $ r3}t        j                  dt        |        d|        || i}Y d}~|S d}~ww xY w)a8  Parse inputs or outputs into a dictionary format.

        Args:
            data: The data to parse (can be inputs or outputs)
            default_key: The default key to use if data is not a dict
                ("input" or "output")

        Returns:
            Dict: The parsed data as a dictionary
        N
model_dumpTjsonexclude_nonemodez&Failed to use model_dump to serialize z
 to JSON: )
isinstancedictstrr   loadsJSONDecodeErrorhasattrcallabler   type	Exceptionloggerdebug)r   r   data_parsed_jsones        |/home/kushmeetdev/apache_webroot/langgraph_flaskproject/venv/lib/python3.12/site-packages/langsmith/wrappers/_agent_utils.pyparse_ior&      s    dD!E4 3 c","jj.k40'E($/E&  l+)tT*,TG  !$'E% '' ,$d+" %,  ,<T$ZL
STRUV %d+ ,s)   ,B# C #B>=B>	C=
(C88C=spanc                 P    t        | j                  dd       }|dv ry|dv ry|dv ryy)Nr   )agenthandoffcustomr   )function	guardrailr   )
generationresponser	   )getattr	span_datar'   	span_types     r%   get_run_typer4   :   s;    DNNFD9	663344    c                     t        | j                  d      r,| j                  j                  r| j                  j                  S t        | j                  dd       }|dk(  ry|dk(  ry|dk(  ryy	)
Nnamer   r.   
Generationr/   Responser*   HandoffSpan)r   r1   r7   r0   r2   s     r%   get_run_namer<   E   sb    4>>6*t~~/B/B>>&&&DNNFD9	$*$)#r5   r1   c                 \    t        | j                  d      t        | j                  d      dS )Ninputoutput)inputsoutputs)r&   r>   r?   r1   s    r%   _extract_function_span_datarC   R   s,     y8	 0 0(;
 	
r5   c                 V   t        | j                  d      t        | j                  d      | j                  | j                  dd}| j
                  rW| j
                  j                  d      | j
                  j                  d      | j
                  j                  d      d|d	   d
<   |S )Nr>   r?   )modelmodel_config)r@   rA   invocation_paramstotal_tokensprompt_tokenscompletion_tokens)rH   input_tokensoutput_tokensrA   usage_metadata)r&   r>   r?   rE   rF   usageget)r1   r   s     r%   _extract_generation_span_datarP   Z   s     y8	 0 0(;" ) 6 6"
 ?? ) 3 3N C ) 3 3O D!*!4!45H!I1DO,-
 r5   c           	      |   i }| j                   &| j                   | j                  j                  d|d<   | j                  n| j                  j                  dd      }d|j	                  dg       i|d<   |j	                  dd       x}rnd	|v r/|j	                  d	      |d
<   |d
   j	                  dd      |d
   d<   d|v r/|j	                  d      |d<   |d   j	                  dd      |d   d<   ||d   d<   |j                         D ci c]  \  }}|dv r|| c}}|d<   |j                         D ci c]   \  }}|h dj                  |d         vr||" }}}|j                  |d   j                  d      |d   j                  d      |d   j                  d      ddd       ||d<   |S c c}}w c c}}w )N)r>   instructionsr@   Tr   r   r?   rA   rN   output_tokens_detailsoutput_token_detailsreasoning_tokensr   	reasoninginput_tokens_detailsinput_token_detailscached_tokens
cache_readrM   )
max_output_tokensrE   parallel_tool_callsrV   temperaturetexttool_choicetoolstop_p
truncationrG   >   rN   r?   rR   rE   r[   r]   chatopenai)ls_model_namels_max_tokensls_temperaturels_model_typels_providermetadata)	r>   r/   rR   r   popitemsunionupdaterO   )r1   r   r/   rN   kvrj   s          r%   _extract_response_span_datarq   m   s"     "??&" ) 2 2 ? ?DN ) ))44$V4TH'h)CDDO Wd33u3*e349II>U4VE01AF.Bc,a0 01+> *U23899=S3TE/0AF-Bc/1- /0> 5:Y 01 %NN,)Aq 1)D$%& %NN,Aq7==dCV>WX 1H  OO%)*=%>%B%B7%K%)*=%>%B%BCV%W&*+>&?&C&CM&R%+#+  (DI)"s   F26%F8c                 R    | j                   | j                  dd| j                  idS )N)r`   handoffsoutput_type)rG   rj   )r`   rs   rt   rB   s    r%   _extract_agent_span_dataru      s5     #%.."
 y44
 	
r5   c                 8    d| j                   | j                  diS )Nr@   
from_agentto_agentrw   rB   s    r%   _extract_handoff_span_datarz      s(     '22%..
 	
r5   c                 "    dd| j                   iiS )Nrj   	triggered)r|   rB   s    r%   _extract_guardrail_span_datar}      s     [)*=*=>??r5   c                     d| j                   iS )Nrj   )r   rB   s    r%   _extract_custom_span_datar      s    INN++r5   c                    i }t        | j                  t        j                        r&|j	                  t        | j                               |S t        | j                  t        j                        r&|j	                  t        | j                               |S t        | j                  t        j                        r&|j	                  t        | j                               |S t        | j                  t        j                        r&|j	                  t        | j                               |S t        | j                  t        j                        r&|j	                  t        | j                               |S t        | j                  t        j                        r&|j	                  t        | j                               |S t        | j                  t        j                         r&|j	                  t#        | j                               |S i S )N)r   r1   r   FunctionSpanDatarn   rC   GenerationSpanDatarP   ResponseSpanDatarq   AgentSpanDataru   HandoffSpanDatarz   GuardrailSpanDatar}   CustomSpanDatar   )r'   r   s     r%   extract_span_datar      sq   !dnng&>&>?KK3DNNCD   (B(BCKK5dnnEF  (@(@AKK3DNNCD  (=(=>KK0@A  (?(?@KK24>>BC  (A(ABKK4T^^DE  (>(>?KK1$..AB  Ir5   )r?   )"r   loggingtypingr   r   r   agentsr   HAVE_AGENTSImportError	getLogger__name__r    RunTypeTr   r&   r;   r4   r<   r   rC   r   rP   r   rq   r   ru   r   rz   r   r}   r   r   r    r5   r%   <module>r      s     % %K 
		8	$WX&s & &D &P	7<< 	H 	7<< C 
++
	c3h
--	c3h&>++>	c3h>@	
G,A,A 	
d3PS8n 	

**
	c3h
@,,@	c3h@
,W-C-C ,SRUX , c3h u   Ks   D D)(D)