
    Ji]                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZmZ d dlmZmZmZmZ d dlmZ  ede	      Z G d
 dej2                        Z G d deee         Zy)    N)abstractmethod)copy_context)partial)AnyCallableDictGenericListOptionalTupleTypeVar)	BaseModel
ConfigDictFieldPrivateAttr)BaseSpanT)boundc                        e Zd ZdZ	 	 	 	 	 ddddee   deedef      dee   deedf   d	ee	eef      d
ee
   ddf fdZ xZS )Threadzm
    A wrapper for threading.Thread that copies the current context and uses the copy to run the target.
    N)daemongrouptarget.nameargskwargsr   returnc                    |#t        |g|i t        |t              r|ni f}nd}t        |   |t               j                  |||       y )N )r   r   r   r   r   )r   
isinstancedictsuper__init__r   run)selfr   r   r   r   r   r   	__class__s          /var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/llama_index_instrumentation/span_handlers/base.pyr#   zThread.__init__   s`     VVJvt4L&RTVD D>%% 	 	
    )NNNr   N)__name__
__module____qualname____doc__r   r   r   strr   r   boolr#   __classcell__r&   s   @r'   r   r      s      $/3" "+/
 "&
}
 #s(+,
 sm	

 CHo
 c3h(
 
 

 
r(   r   c                   ~    e Zd ZU  ed      Z eed      Zee	e
f   ed<    eed      Zee
   ed<    eed      Zee
   ed<    ei d	
      Zeeee	   f   ed<    e       Zeej,                     ed<   i g g i fdee	e
f   dee
   dee
   deee	f   f fdZede	fd       Zedej,                  fd       Z	 	 	 d!de	dej:                  dee   dee	   deee	ef      deddfdZ	 	 d"de	dej:                  dee   dee   deddfdZ	 	 d"de	dej:                  dee   dee    deddfdZ!e"	 	 	 d!de	dej:                  dee   dee	   deee	ef      dedee
   fd       Z#e"	 	 d"de	dej:                  dee   dee   dedee
   fd       Z$e"	 	 d"de	dej:                  dee   dee    dedee
   fd        Z% xZ&S )#BaseSpanHandlerT)arbitrary_types_allowedzDictionary of open spans.)default_factorydescription
open_spanszList of completed spans.completed_spansdropped_spansz&Id of current spans in a given thread.)defaultr5   current_span_ids_lockc                 :    t         |   ||||       d | _        y )N)r6   r7   r8   r:   )r"   r#   r;   )r%   r6   r7   r8   r:   r&   s        r'   r#   zBaseSpanHandler.__init__>   s,     	!+'-	 	 	
 
r(   r   c                      y)zClass name.r2   r   )clss    r'   
class_namezBaseSpanHandler.class_nameM   s     !r(   c                 d    | j                   t        j                         | _         | j                   S )N)r;   	threadingLock)r%   s    r'   lockzBaseSpanHandler.lockR   s$    ::")DJzzr(   Nid_
bound_argsinstance	parent_idtagsr   c                     || j                   v ry| j                  |||||      }|r%| j                  5  || j                   |<   ddd       yy# 1 sw Y   yxY w)zLogic for entering a span.)rD   rE   rF   parent_span_idrH   N)r6   new_spanrC   )r%   rD   rE   rF   rG   rH   r   spans           r'   
span_enterzBaseSpanHandler.span_enterX   sn     $//!==%!( ! D YY 0+/DOOC(0 0 0 0s   AAresultc                     | j                  ||||      }|r#| j                  5  | j                  |= ddd       yy# 1 sw Y   yxY w)zLogic for exiting a span.)rD   rE   rF   rN   N)prepare_to_exit_spanrC   r6   )r%   rD   rE   rF   rN   r   rL   s          r'   	span_exitzBaseSpanHandler.span_exitp   sY     ((
Xf ) 
  )OOC() ) ) )	   <Aerrc                     | j                  ||||      }|r#| j                  5  | j                  |= ddd       yy# 1 sw Y   yxY w)z*Logic for dropping a span i.e. early exit.)rD   rE   rF   rS   N)prepare_to_drop_spanrC   r6   )r%   rD   rE   rF   rS   r   rL   s          r'   	span_dropzBaseSpanHandler.span_drop   sY     ((
X3 ) 
  )OOC() ) ) )rR   rJ   c                      y)z
        Create a span.

        Subclasses of BaseSpanHandler should create the respective span type T
        and return it. Only NullSpanHandler should return a None here.
        Nr   )r%   rD   rE   rF   rJ   rH   r   s          r'   rK   zBaseSpanHandler.new_span         	r(   c                      y)z
        Logic for preparing to exit a span.

        Subclasses of BaseSpanHandler should return back the specific span T
        that is to be exited. If None is returned, then the span won't actually
        be exited.
        Nr   )r%   rD   rE   rF   rN   r   s         r'   rP   z$BaseSpanHandler.prepare_to_exit_span   rX   r(   c                      y)z
        Logic for preparing to drop a span.

        Subclasses of BaseSpanHandler should return back the specific span T
        that is to be dropped. If None is returned, then the span won't actually
        be dropped.
        Nr   )r%   rD   rE   rF   rS   r   s         r'   rU   z$BaseSpanHandler.prepare_to_drop_span   rX   r(   )NNN)NN)'r)   r*   r+   r   model_configr   r!   r6   r   r-   r   __annotations__listr7   r
   r8   r:   r   r   r   r;   rA   rB   r#   classmethodr?   propertyrC   inspectBoundArgumentsrM   rQ   BaseExceptionrV   r   rK   rP   rU   r/   r0   s   @r'   r2   r2   .   s   d;L$*E JS!V   %*D OT!W  #*DM47  27 H2d3-.  '2mE8INN#3 $&#%!#+-aL a Aw	
 sCx. !3 ! ! inn   #'#')-00 **0 3-	0
 C=0 tCH~&0 0 
08 #' $)) **) 3-	)
 ) ) 
)( #''+)) **) 3-	)
 m$) ) 
)  
 #'(,)- ** 3-	
 ! tCH~&  
! " 
 #' $ ** 3-	
   
! " 
 #''+ ** 3-	
 m$  
! r(   r2   )r`   rA   abcr   contextvarsr   	functoolsr   typingr   r   r   r	   r
   r   r   r   pydanticr   r   r   r   %llama_index_instrumentation.span.baser   r   r   r2   r   r(   r'   <module>ri      sX       $  O O O > > :Cx 
Y 
>Vi Vr(   