
    h(              	          d dl Z d dlZd dlmZmZ d dlmZmZmZ d dlm	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rd d
ededefdZdej2                  defdZdej2                  defdZdej8                  deeef   fdZdeeef   defdZdej>                  deeef   fdZ dejB                  deeef   fdZ"dejF                  deeef   fdZ$dejJ                  deeef   fdZ&dejN                  deeef   fdZ(dejR                  deeef   fdZ*dej2                  deeef   fdZ+	 d!dee   dee   dee   defdZ,yy# e$ r dZY 1w xY w)"    N)datetimetimezone)AnyLiteralOptional)uuid4)InputTokenDetailsOutputTokenDetailsUsageMetadata)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        ]/var/www/html/eduruby.in/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_typer:   >   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$   r4   
Generationr5   Responser0   HandoffSpan)r"   r7   r=   r6   r8   s     r+   get_run_namerB   I   sb    4>>6*t~~/B/B>>&&&DNNFD9	$*$)#r;   r7   c                 \    t        | j                  d      t        | j                  d      dS )Ninputoutput)inputsoutputs)r,   rD   rE   r7   s    r+   _extract_function_span_datarI   V   s,     y8	 0 0(;
 	
r;   usagec                    | j                  d      xs | j                  d      xs d}| j                  d      xs | j                  d      xs d}| j                  d      xs ||z   }i }| j                  d      x}r|j                  d      x}r||d	<   i }| j                  d
      x}r|j                  d      x}	r|	|d<   t        |||t        di |t        di |      S )aX  Extract standardized usage metadata.

        Supports both older OpenAI chat completions format
        (prompt_tokens/completion_tokens) and newer format (input_tokens/output_tokens).

        Token details follow new OpenAI format only:
        - input_tokens_details: cached_tokens
        - output_tokens_details: reasoning_tokens
        input_tokensprompt_tokensr   output_tokenscompletion_tokenstotal_tokensinput_tokens_detailscached_tokens
cache_readoutput_tokens_detailsreasoning_tokens	reasoning)rL   rN   rP   input_token_detailsoutput_token_details )getr   r	   r
   )
rJ   rL   rN   rP   rW   input_detailsrR   rX   output_detailsrU   s
             r+   _extract_usage_metadatar]   ^   s    yy0SEIIo4NSRSIIo&M%))4G*HMA 	 yy0R\M5Q %'!II&<==== - 1 1/ BB}B4A#L1 &("YY'>??>?#1#5#56H#III4D$[1%'% 1 H4G H!3!K6J!K
 	
r;   c                     t        | j                  d      t        | j                  d      | j                  | j                  dd}| j
                  rt        | j
                        |d   d<   |S )NrD   rE   )modelmodel_config)rF   rG   invocation_paramsrG   usage_metadata)r,   rD   rE   r_   r`   rJ   r]   )r7   r   s     r+   _extract_generation_span_datarc      sd     y8	 0 0(;" ) 6 6"
 ??0G	0XDO,-r;   c           	         i }| j                   J| j                   | j                  ,| j                  j                  r| j                  j                  nd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 )rD   instructionsrF   Tr   r   rE   rG   rJ   rT   rX   rU   r   rV   rQ   rW   rR   rS   rb   )
max_output_tokensr_   parallel_tool_callsrV   temperaturetexttool_choicetoolstop_p
truncationra   >   rJ   rE   rf   r_   rg   ri   chatopenai)ls_model_namels_max_tokensls_temperaturels_model_typels_providermetadata)	rD   r5   rf   r   popitemsunionupdaterZ   )r7   r   r5   rJ   kvrv   s          r+   _extract_response_span_datar}      sF     "??&" !))5!**77 &&33 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   /G%Gc                 R    | j                   | j                  dd| j                  idS )N)rl   handoffsoutput_type)ra   rv   )rl   r   r   rH   s    r+   _extract_agent_span_datar      s5     #%.."
 y44
 	
r;   c                 8    d| j                   | j                  diS )NrF   
from_agentto_agentr   rH   s    r+   _extract_handoff_span_datar      s(     '22%..
 	
r;   c                 "    dd| j                   iiS )Nrv   	triggered)r   rH   s    r+   _extract_guardrail_span_datar      s     [)*=*=>??r;   c                     d| j                   iS )Nrv   )r   rH   s    r+   _extract_custom_span_datar      s    INN++r;   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   r7   r   FunctionSpanDatarz   rI   GenerationSpanDatarc   ResponseSpanDatar}   AgentSpanDatar   HandoffSpanDatar   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  Ir;   
start_timerun_idparent_dotted_orderc                     | xs# t        j                  t        j                        }|xs t	        t                     }|j                  d      |z   }||dz   |z   S |S )z3Create a dotted order from a start time and run id.z%Y%m%dT%H%M%S%fZ.)r   nowr   utcr   r   strftime)r   r   r   stid_current_dotted_orders         r+   ensure_dotted_orderr   	  s]     58<<5$EG!{{+=>D*&,/CCC##r;   )rE   r   )-r   loggingr   r   typingr   r   r   uuidr   langsmith.schemasr	   r
   r   agentsr   HAVE_AGENTSImportError	getLogger__name__r&   RunTypeTr   r   r,   rA   r:   rB   r   rI   r]   r   rc   r   r}   r   r   r   r   r   r   r   r   r   r   rY   r;   r+   <module>r      s!     ' ) )  R RK 
		8	$WX&s & &D &P	7<< 	H 	7<< C 
++
	c3h
"
tCH~ "
- "
H--	c3hC++C	c3hCJ	
G,A,A 	
d3PS8n 	

**
	c3h
@,,@	c3h@
,W-C-C ,SRUX , c3h 2 .2$X&$$ &c]$ 
	$k   Ks   E E&%E&