
    Ig>                    \   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZ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mZmZ erd dlmZ  ej8                  d	      Zej<                   G d
 d             Z	 d	 	 	 	 	 	 	 ddZ 	 	 	 	 	 	 	 	 	 	 ddZ!	 	 	 	 ddZ"ddZ#	 	 	 	 	 	 ddZ$y)    )annotationsN)EmptyQueue)TYPE_CHECKINGListUnioncast)schemas)_AUTO_SCALE_DOWN_NEMPTY_TRIGGER_AUTO_SCALE_UP_NTHREADS_LIMIT_AUTO_SCALE_UP_QSIZE_TRIGGER)SerializedFeedbackOperationSerializedRunOperation#combine_serialized_queue_operations)Clientzlangsmith.clientc                  N    e Zd ZU dZded<   ded<   dZ	 	 	 	 	 	 ddZddZdd	Zy
)TracingQueueItemzAn item in the tracing queue.

    Attributes:
        priority (str): The priority of the item.
        action (str): The action associated with the item.
        item (Any): The item itself.
    strpriority:Union[SerializedRunOperation, SerializedFeedbackOperation]itemr   r   c                     || _         || _        y Nr   )selfr   r   s      c/var/www/html/answerous/venv/lib/python3.12/site-packages/langsmith/_internal/_background_thread.py__init__zTracingQueueItem.__init__1   s    
 !	    c                    | j                   | j                  j                  f|j                   |j                  j                  fk  S r   )r   r   	__class__r   others     r   __lt__zTracingQueueItem.__lt__9   s;    tyy223NNJJ  7
 
 	
r   c                    t        |t              xrE | j                  | j                  j                  f|j                  |j                  j                  fk(  S r   )
isinstancer   r   r   r    r!   s     r   __eq__zTracingQueueItem.__eq__?   sM    %!12 4MMII8
 nnejj22384 	4r   N)r   r   r   r   returnNone)r"   r   r'   bool)r"   objectr'   r)   )	__name__
__module____qualname____doc____annotations__	__slots__r   r#   r&    r   r   r   r   "   sE     M
DD$I I 
	
4r   r   c                   g }	 | j                  |d      x}r|j                  |       | j                  |d      x}r:|j                  |       |rt        |      |k\  r	 |S | j                  |d      x}r:|S # t        $ r Y |S w xY w)Ng      ?)blocktimeoutg?)getappendlenr   )tracing_queuelimitr3   
next_batchr   s        r   _tracing_thread_drain_queuer;   F   s     *,J !$$5$$??4?d##''eT'BBdBd#ZE1  $''eT'BBdB   s   AA; #A; ;	BBc                   	 t        |D cg c]  }|j                   c}      }|r| j                  |       nmt        d |D              r5t        j                  d       |D cg c]  }t        |t              r| }}| j                  t        t        t           |             |D ]  }|j                           y c c}w c c}w # t        $ r t        j                  dd       Y Dw xY w# |D ]  }|j                           w xY w)Nc              3  <   K   | ]  }t        |t                y wr   )r%   r   ).0ops     r   	<genexpr>z/_tracing_thread_handle_batch.<locals>.<genexpr>f   s     M2:b"=>Ms   z;Feedback operations are not supported in non-multipart modezError in tracing queueT)exc_info)r   r   _multipart_ingest_opsanyloggerwarnr%   r   _batch_ingest_run_opsr	   r   r   	Exceptionerror	task_done)clientr8   batchuse_multipartr   opsr?   _s           r   _tracing_thread_handle_batchrO   [   s    &12O4992OP((-MMMQ "%Jr;V,WB  ((d3I.JC)PQ  	&A##%	&' 3P
  -= 		  	&A##%	&sF   	C B>AC $C:C>(C >
C  C+(C. *C++C. .Dc                    t        j                  dd dt        t        t              }| s|S 	 | j
                  s|S | j
                  S # t        $ r |cY S w xY w)NFd   )use_multipart_endpointsize_limit_bytes
size_limitscale_up_nthreads_limitscale_up_qsize_triggerscale_down_nempty_trigger)
ls_schemasBatchIngestConfigr   r   r   batch_ingest_configBaseException)infodefault_configs     r   _ensure_ingest_configr^   y   sf      11$ =;"AN ''!!''' s   A A AAc                    |        y j                   }|J t        j                        }|d   }|d   }|d   }|j                  dd      }g ddfd} |       rD ]$  }|j	                         rj                  |       & t              |k  rd|j                         |kD  rQt        j                  t        t        j                        |f      }	j                  |	       |	j                          t        ||	      x}
rt!        ||
|        |       rt        ||d
      x}
r t!        ||
|       t        ||d
      x}
ry y )NrT   rU   rV   rR   F   c                      rt         d      r j                  ryt        j                         j	                         syt        t
        d      r$t        j                         t              z   kD  S y)N_manual_cleanupFgetrefcountT)hasattrrb   	threadingmain_threadis_alivesysrc   r7   )rJ   num_known_refssub_threadss   r   keep_thread_activez7tracing_control_thread_func.<locals>.keep_thread_active   sc    F-.63I3I$$&//13& ??6*^c+>N-NNN r   )targetargsr9   r9   r3   )r'   r)   )r8   r^   r\   r5   rg   remover7   qsizere   Thread_tracing_sub_thread_funcweakrefrefr6   startr;   rO   )
client_refr8   rZ   rT   rU   rV   rL   rk   thread
new_threadr:   rJ   ri   rj   s              @@@r   tracing_control_thread_funcrz      s   \F~((M$$$/<),7J#67P#Q"56N"O'++,DeLM*,KN( 
! 	+F??$""6*	+ 66##%(>>"))/kk&)=9J z*4]*UU:U(z= 
& 4Zu *  	%V]JV 4Zu * r   c                T    |        }|y 	 |j                   sy 	 |j                  }|J t        |j                         }|j                  dd      }d}t        j                         j                         rX||d   k  rPt        ||      x}rd}t        ||||       n|dz  }t        j                         j                         r	||d   k  rPt        ||d	      x}r t        ||||       t        ||d	      x}ry y # t        $ r }t        j                  d|       Y d }~y d }~ww xY w)
Nz#Error in tracing control thread: %srT   rQ   r   rW   rn      Fro   )r\   r[   rD   debugr8   r^   r5   re   rf   rg   r;   rO   )	rw   rL   rJ   er8   rZ   rT   seen_successive_empty_queuesr:   s	            r   rs   rs      s]    \F~{{ 
 ((M$$$/<$((s;J#$ 
 	((*(:;< 5]*UU:U+,((z= )A-( 	((*(:;< 4Zu *  	%V]JV 4Zu * 5  :A>s   C> >	D'D""D')rQ   T)r8   r   r9   intr3   r)   r'   List[TracingQueueItem])
rJ   r   r8   r   rK   r   rL   r)   r'   r(   )r\   zls_schemas.LangSmithInfor'   zls_schemas.BatchIngestConfig)rw   weakref.ref[Client]r'   r(   )rw   r   rL   r)   r'   r(   )%
__future__r   	functoolsloggingrh   re   rt   queuer   r   typingr   r   r   r	   	langsmithr
   rX   langsmith._internal._constantsr   r   r   langsmith._internal._operationsr   r   r   langsmith.clientr   	getLoggerrD   total_orderingr   r;   rO   r^   rz   rs   r1   r   r   <module>r      s   "   
     , 
  '			-	.  4  4  4H ;?!$37*&&& "& 	&
 
&<
"!*:Wz'W#'W'W 
'Wr   