
    $ht                       S SK Jr  S SKJr  S SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKrS SKrS SKJr  S SKJrJr  S SKJrJrJrJr  S SKJr  S SKJr  S SKJr  S S	KJrJ r J!r!J"r"  S S
K#J$r$J%r%J&r&  \(       a  S SK'J(r(  S SK)J*r*  \RV                  " S5      r,\RZ                  " \" 5       S-  S9r.\R^                   " S S5      5       r0 S       SS jjr1 S        S!S jjr2  S"             S#S jjr3  S"           S$S jjr4 S%           S&S jjr5S'S jr6S(S jr7    S)S jr8S*S jr9S+S jr: S,     S-S jjr;      S.S jr<g)/    )annotationsN)	cpu_count)EmptyQueue)TYPE_CHECKINGOptionalUnioncast)schemas)utils)CompressedTraces)_AUTO_SCALE_DOWN_NEMPTY_TRIGGER_AUTO_SCALE_UP_NTHREADS_LIMIT_AUTO_SCALE_UP_QSIZE_TRIGGER	_BOUNDARY)SerializedFeedbackOperationSerializedRunOperation#combine_serialized_queue_operations)Context)Clientzlangsmith.client   max_workersc                  n    \ rS rSr% SrS\S'   S\S'   S\S'   S	r S       SS jjrSS jrSS jr	Sr
g
)TracingQueueItem.   zAn item in the tracing queue.

Attributes:
    priority (str): The priority of the item.
    item (Any): The item itself.
    otel_context (Optional[Context]): The OTEL context of the item.
strpriority:Union[SerializedRunOperation, SerializedFeedbackOperation]itemOptional[Context]otel_contextr   r    r"   Nc                (    Xl         X l        X0l        g Nr#   )selfr   r    r"   s       ^/var/www/html/shao/venv/lib/python3.13/site-packages/langsmith/_internal/_background_thread.py__init__TracingQueueItem.__init__>   s     !	(    c                    U R                   U R                  R                  4UR                   UR                  R                  4:  $ r%   )r   r    	__class__r&   others     r'   __lt__TracingQueueItem.__lt__H   s;    tyy223NNJJ  7
 
 	
r*   c                    [        U[        5      =(       aE    U R                  U R                  R                  4UR                  UR                  R                  4:H  $ r%   )
isinstancer   r   r    r,   r-   s     r'   __eq__TracingQueueItem.__eq__N   sM    %!12 4MMII8
 nnejj22384 	4r*   )r    r"   r   r%   )r   r   r    r   r"   r!   returnNone)r.   r   r5   bool)r.   objectr5   r7   )__name__
__module____qualname____firstlineno____doc____annotations__	__slots__r(   r/   r3   __static_attributes__ r*   r'   r   r   .   sZ     M
DD##4I +/	)) I) (	)
 
)
4r*   r   c                2   / n U R                  USS9=n(       a  UR                  U5        U R                  USS9=n(       aC  UR                  U5        U(       a  [        U5      U:  a   U$ U R                  USS9=n(       a  MC  U$ ! [         a     U$ f = f)Ng      ?)blocktimeout皙?)getappendlenr   )tracing_queuelimitrC   
next_batchr    s        r'   _tracing_thread_drain_queuerL   U   s     *,J !$$5$$??4?d##''eT'BBdBd#ZE1  $''eT'BBdB   s   A&B -B 
BBc                    U R                   c  gU R                   R                     U R                   R                  R                  5         U R                   R                  R                  5       nU R                   R                  nUb  US::  a  [        SU 35      eUb  US:  a  [        SU 35      eUb  X2:  a'  Ub  U R                   R                  U:  a
   S S S 5        gU R                   R                  R                  S[         S3R                  5       5        U R                   R                  R                  5         U R                   R                  nU R                   R                  Ul        XC4nU R                   R                  5         S S S 5        WR!                  S5        UW4$ ! , (       d  f       N#= f! ["         a    [$        R'                  SSS	9   gf = f)
N)NNr   z!size_limit must be positive; got z*size_limit_bytes must be nonnegative; got z--z--
uLangSmith tracing error: Failed to submit trace data.
This does not affect your application's runtime.
Error details:Texc_info)compressed_traceslockcompressor_writerflushbuffertelluncompressed_size
ValueErrortrace_countwriter   encodeclose_contextcontextresetseek	Exceptionloggererror)client
size_limitsize_limit_bytescurrent_sizepre_compressed_sizefilled_buffercompressed_traces_infos          r'   '_tracing_thread_drain_compressed_bufferrk   j   s   -##+%%**$$66<<>!33::??AL"(":":"L"L%*/ #DZL!QRR+0@10D @AQ@RS  !(L,K"f&>&>&J&JZ&W!! +*& $$66<<YKv&--/ $$66<<>"44;;M$*$<$<$E$EM!&9%H"$$**,; +> 	1566A +*B  	 	 	 	
 	s;   F= F= B3F,F= "B-F,F= ,
F:6F= =GGc                z    Uc$  [        U Vs/ sH  ofR                  PM     sn5      nU(       a  U R                  U5        O{[        S U 5       5      (       a=  [        R                  S5        U Vs/ sH  n[        U[        5      (       a  M  UPM     nnU R                  [        [        [           U5      5         U(       a  Ub  U H  n UR                  5         M     gggs  snf s  snf ! [         a    [        R                  SSS9   NQf = f! [         a3  n	S[!        U	5      ;   a  [        R#                  SU	 35         Sn	A	M  e Sn	A	ff = f! U(       a_  Ub[  U HT  n UR                  5         M  ! [         a3  n	S[!        U	5      ;   a  [        R#                  SU	 35         Sn	A	MM  e Sn	A	ff = f   f f f = f)	a  Handle a batch of tracing queue items by sending them to LangSmith.

Args:
    client: The LangSmith client to use for sending data.
    tracing_queue: The queue containing tracing items (used for task_done calls).
    batch: List of tracing queue items to process.
    use_multipart: Whether to use multipart endpoint for sending data.
    mark_task_done: Whether to mark queue tasks as done after processing.
        Set to False when called from parallel execution to avoid double counting.
    ops: Pre-combined serialized operations to use instead of combining from batch.
        If None, operations will be combined from the batch items.
Nc              3  @   #    U H  n[        U[        5      v   M     g 7fr%   )r2   r   ).0ops     r'   	<genexpr>/_tracing_thread_handle_batch.<locals>.<genexpr>   s     M2:b"=>>s   z;Feedback operations are not supported in non-multipart moderN   TrO   !task_done() called too many times3Ignoring harmless task_done error during shutdown: )r   r    _multipart_ingest_opsanyrb   warnr2   r   _batch_ingest_run_opsr
   listr   ra   rc   	task_donerX   r   debug)
rd   rI   batchuse_multipartmark_task_doneopsr    ro   _es
             r'   _tracing_thread_handle_batchr      s   ,$;5U6SUTyyU6STC((-MMMMQ "%!$2Jr;V,WB   ((d3I.JC)PQ m7
!++-  8>- 7T
  
 	 	 	

 " :c!fD QRSQTU  	 m7
!++-! :c!fD QRSQTU    8>s   C. C$C. E 0C. 2C)C)(C. <E D$
C. .DE DE 
E'E
	E

EF:#E53F:5
F2	?'F-	&F:,F-	-F2	2F:c                    Uc$  [        U Vs/ sH  oUR                  PM     sn5      nU Vs/ sH  n[        U[        5      (       d  M  UPM     nnU Vs0 sHE  n[        UR                  [        5      (       d  M$  UR                  R                  UR
                  _MG     nnU(       a>  U R                  b  U R                  R                  Xx5        O[        R                  S5        U(       a  Ub  U H  n	 UR                  5         M     gggs  snf s  snf s  snf ! [         a    [        R                  SSS9   NVf = f! [         a3  n
S[        U
5      ;   a  [        R                  SU
 35         Sn
A
M  e Sn
A
ff = f! U(       a_  Ub[  U HT  n	 UR                  5         M  ! [         a3  n
S[        U
5      ;   a  [        R                  SU
 35         Sn
A
MM  e Sn
A
ff = f   f f f = f)a@  Handle a batch of tracing queue items by exporting them to OTEL.

Args:
    client: The LangSmith client containing the OTEL exporter.
    tracing_queue: The queue containing tracing items (used for task_done calls).
    batch: List of tracing queue items to process.
    mark_task_done: Whether to mark queue tasks as done after processing.
        Set to False when called from parallel execution to avoid double counting.
    ops: Pre-combined serialized operations to use instead of combining from batch.
        If None, operations will be combined from the batch items.
NzLangSmith tracing error: Failed to submit OTEL trace data.
This does not affect your application's runtime.
Error details: client.otel_exporter is NonezpOTEL tracing error: Failed to submit trace data.
This does not affect your application's runtime.
Error details:TrO   rr   rs   )r   r    r2   r   idr"   otel_exporterexport_batchrb   rc   ra   ry   rX   r   rz   )rd   rI   r{   r}   r~   r    ro   run_opsotel_context_mapr   r   s              r'   !_otel_tracing_thread_handle_batchr      s   ((;5U6SUTyyU6STC #N"z"6L'M2N 
$))%;< ,DIILL$+++ 	 

 ##/$$11'LB m7
!++-  8>5 7TN
  
 	 	 	

 " :c!fD QRSQTU  	 m7
!++-! :c!fD QRSQTU    8>s   D DD D	DD "D;%D 1D F D 7ED D>;F =D>>F 
E>'E98E99E>G)F$"G)$
G!	.'G	G)G	G!	!G)c           
     2   [        U Vs/ sH  oUR                  PM     sn5      n[        R                  " U5      n[        R                  " U5      n [        R
                  " SS9 n	U	R                  [        U UUUSU5      n
U	R                  [        U UUSU5      nU
R                  5         UR                  5         SSS5        U(       a  Ub  U H  n UR                  5         M     gggs  snf ! , (       d  f       N:= f! [         aK  nS[        U5      ;   a6  [        R                  S5        [        XX#SU5        [        XUSU5         SnANe SnAff = f! [         a3  nS[        U5      ;   a  [        R                  SU 35         SnAM  e SnAff = f)	a  Handle a batch of tracing queue items by sending to both both LangSmith and OTEL.

Args:
    client: The LangSmith client to use for sending data.
    tracing_queue: The queue containing tracing items (used for task_done calls).
    batch: List of tracing queue items to process.
    use_multipart: Whether to use multipart endpoint for LangSmith.
    mark_task_done: Whether to mark queue tasks as done after processing.
        Set to False primarily for testing when items weren't actually queued.
   r   FNz6cannot schedule new futures after interpreter shutdownz@Interpreter shutting down, falling back to sequential processingrr   rs   )r   r    copydeepcopycfThreadPoolExecutorsubmitr   r   resultRuntimeErrorr   rb   rz   ry   rX   )rd   rI   r{   r|   r}   r    r~   langsmith_opsotel_opsexecutorfuture_langsmithfuture_otelr   r   s                 r'   #_hybrid_tracing_thread_handle_batchr     s   $ .U.KUTyyU.K
LC MM#&M}}S!H'""q1X',  #//1K ##% - 2P -3A
'')  4~a /L 21.  Cs1vM LLR )uUM .ueX  *  6#a&@ LLMaSQ  sa   C+D &AC0<D E0
C>:D >D 
EA EEE
F#'FFFc                   [        U S5      (       a  U R                  c  g [        R                  " [        R                  " S5      5      (       d  gSSKJn  UR                  5       n[        US5      (       a[  [        UR                  S5      (       a@  UR                  R                  R                  S	S5      n[        R                  S
U 35        U$ g! [         a#  n[        R                  SU S35         SnAgSnAff = f)zCheck if client is using LangSmith's internal OTLP provider.

Returns True if using LangSmith's internal provider, False if user
provided their own.
r   NFOTEL_ENABLEDr   )traceresource
attributeszlangsmith.internal_providerz;TracerProvider resource check: langsmith.internal_provider=z)Could not determine TracerProvider type: z, assuming user-provided)hasattrr   ls_utils	is_truishget_env_varopentelemetryr   get_tracer_providerr   r   rF   rb   rz   ra   )rd   r   tracer_provideris_internalr   s        r'    _is_using_internal_otlp_providerr   j  s     6?++v/C/C/K !!("6"6~"FGG 	(335?J//G$$l5
 5
 *22==AA-uK LL//:m=  7s:RS	
 	s   /C BC 
DC<<Dc                     [         R                  " S5      n U b   [        U 5      $ g ! [         a    [        R                  SU  S35         g f = f)NBATCH_INGEST_SIZE_LIMITz+Invalid value for BATCH_INGEST_SIZE_LIMIT: z, continuing with default)r   r   intrX   rb   warning)size_limit_strs    r'   get_size_limit_from_envr     sg    ))!N !	~&&   	NN=n=M N* * 	s   
' #AAc           	         [         R                  " SS S[        [        [        S9nU (       d  U$  U R
                  (       d  U$ [        5       nUb  X R
                  S'   U R
                  $ ! [         a    Us $ f = f)NFd   )use_multipart_endpointrf   re   scale_up_nthreads_limitscale_up_qsize_triggerscale_down_nempty_triggerre   )
ls_schemasBatchIngestConfigr   r   r   batch_ingest_configr   BaseException)infodefault_configenv_size_limits      r'   _ensure_ingest_configr     s      11$ =;"AN ''!!02%5C$$\2''' s   A* &A* *A98A9c                     [         R                  " [         R                  " S5      5      n [         R                  " [         R                  " S5      5      nU (       d  gU(       + nUnX#4$ )aR  Get the current tracing mode configuration.

Returns:
    tuple[bool, bool]:
        - hybrid_otel_and_langsmith: True if both OTEL and LangSmith tracing
          are enabled, which is default behavior if OTEL_ENABLED is set to
          true and OTEL_ONLY is not set to true
        - is_otel_only: True if only OTEL tracing is enabled
r   	OTEL_ONLY)FF)r   r   r   )otel_enabled	otel_onlyhybrid_otel_and_langsmithis_otel_onlys       r'   get_tracing_moder     sX     %%h&:&:>&JKL""8#7#7#DEI $-L$22r*   c                  ^^^ U " 5       mTc  g TR                   nUc   e[        TR                  5      nUS   nUS   nUS   nUR                  SS5      n/ mSm[        R
                  " [        R                  " S5      5      =(       d    TR                  S LnU(       d  U(       a  TR                  R                  =(       d    0 R                  SS5      (       d  [        R                  S	5        O[        R                  " 5       Tl        [        5       Tl        [         R"                  " 5       Tl        [         R&                  " [(        [        R*                  " T5      4S
9R-                  5         TS-  mSUUU4S jjnU" 5       (       Ga  T H+  n	U	R/                  5       (       a  M  TR1                  U	5        M-     [3        T5      U:  ad  UR5                  5       U:  aP  [         R&                  " [6        [        R*                  " T5      U4S
9n
TR9                  U
5        U
R-                  5         [;        5       u  p[=        XS9=n(       a6  U(       a  [?        TXU5        O!U(       a  [A        TX5        O[C        TXU5        U" 5       (       a  GM  [;        5       u  p[=        XSS9=n(       a  U(       a#  [        RE                  S5        [?        TXU5        OKU(       a"  [        RE                  S5        [A        TX5        O"[        RE                  S5        [C        TXU5        [=        XSS9=n(       a  M  [        RE                  S5        g )Nre   r   r   r   Fr   DISABLE_RUN_COMPRESSIONzstd_compression_enabledz~Run compression is not enabled. Please update to the latest version of LangSmith. Falling back to regular multipart ingestion.)targetargs   c                   > T(       a"  [        TS5      (       a'  TR                  (       a  [        R                  S5        g[        R
                  " 5       R                  5       (       d  [        R                  S5        g[        [        S5      (       aC  [        R                  " T5      T[        T5      -   :  n U (       d  [        R                  S5        U $ g)N_manual_cleanupz3Client is being cleaned up, stopping tracing threadFz,Main thread is dead, stopping tracing threadgetrefcountzeClient refs count indicates we're the only remaining reference to the client, stopping tracing threadT)
r   r   rb   rz   	threadingmain_threadis_alivesysr   rH   )should_keep_threadrd   num_known_refssub_threadss    r'   keep_thread_active7tracing_control_thread_func.<locals>.keep_thread_active  s    F-..63I3ILLNO$$&//11LLGH3&& "%!8>CM < " &= &% r*   rJ   rJ   rC   zHybrid mode cleanupOTEL-only cleanupLangSmith-only cleanupz'Tracing control thread is shutting downr5   r7   )#rI   r   r   rF   r   r   r   r   instance_flagsrb   r   weakrefWeakSet_futuresr   rQ   r   Event_data_available_eventThread-tracing_control_thread_func_compress_parallelrefstartr   removerH   qsize_tracing_sub_thread_funcrG   r   rL   r   r   r   rz   )
client_refrI   r   re   r   r   r|   disable_compressionr   thread
new_threadr   r   rK   rd   r   r   s                 @@@r'   tracing_control_thread_funcr     s   \F~((M$$$/<),7J#67P#Q"56N"O'++,DeLM*,KN 	8//0IJK 	,t+  =**0b55&
 
 NNU
 &oo/FO'7'9F$+4??+<F(Dkk&)+ egaN < 

!F??$$""6* " 66##%(>>"))/kk&)=9J z*2B2D/!4]UU:U(3M} 1&-T -M}7 

@ /?.@+3u *  %LL.//= LL,--fmP LL12(=! 4u * & LL:;r*   c                  ^^ U " 5       mTc  g [         R                  S5        TR                  b  TR                  b  TR                  c  [         R                  S5        g [        TR                  5      nUS   nUR                  SS5      nSmSUU4S jjn[        R                  " 5       n TR                  R                  S	S
9nU" 5       (       d  GOU(       a~  TR                  R                  5         [        TX45      u  pUb=   [        R                  TR                   UU	5      n
TR                  R#                  U
5        [        R                  " 5       nOy[        R                  " 5       U-
  U:  a]  [        TSSS9u  nn	Ub7   [&        R                  " [        R                  TR                   UU	5      /5        [        R                  " 5       nGM(   [        TSSS9u  nn	Ub8   [&        R                  " [        R                  TR                   UU	5      /5        O [         R                  S5        g ! [$         a    TR!                  UU	5         GNf = f! [$         a    TR!                  UU	5         Nf = f! [$         a    TR!                  UU	5         Nyf = f! [(         a    [         R                  SSS9   Nf = f)Nz4Tracing control thread func compress parallel calledzLangSmith tracing error: Required compression attributes not initialized.
This may affect trace submission but does not impact your application's runtime.re   rf     @   c                   > T(       a"  [        TS5      (       a'  TR                  (       a  [        R                  S5        g[        R
                  " 5       R                  5       (       d  [        R                  S5        g[        [        S5      (       a7  [        R                  " T5      T:  n U (       d  [        R                  S5        U $ g)Nr   z7Client is being cleaned up, stopping compression threadFz0Main thread is dead, stopping compression threadr   ziClient refs count indicates we're the only remaining reference to the client, stopping compression threadT)	r   r   rb   rz   r   r   r   r   r   )r   rd   r   s    r'   r   Itracing_control_thread_func_compress_parallel.<locals>.keep_thread_activef  s    F-..63I3ILLRS$$&//11LLKL3&& "%!8>!I%A &% r*   TrE   )rD   r   )re   rf   zuLangSmith tracing error: Failed during final cleanup.
This does not affect your application's runtime.
Error details:rO   z1Compressed traces control thread is shutting downr   )rb   rz   rQ   r   r   rc   r   r   rF   time	monotonicwaitclearrk   HTTP_REQUEST_THREAD_POOLr   _send_compressed_multipart_reqaddr   r   ra   )r   flush_intervalr   re   rf   r   last_flush_time	triggereddata_streamrj   futurefinal_data_streamrd   r   s               @@r'   r   r   L  s    \F~
LLGH  (''/??"1	

 	/<),7J*../A:N N 4 nn&O
0055d5C	!## ((..0 8j34/K &5<<==#.F
 OO''/ #nn.O  ?2~E <q1* * 8 ? ?$*$I$I$/$:!" #'.."2i n
 4q1
	
" (077"AA-2 )0 LLDEC $ 99#.4 ( =='24   55%*  
 	 	 	

sT   <H6 6I J (6I; 6III87I8;JJ JJ J>=J>c                   U " 5       nUc  g  UR                   (       d  g  UR                  nUc   e[        UR                   5      nUR                  SS5      nSn[        R                  " 5       R                  5       (       a  UUS   ::  a  [        XFS9=n(       aC  Sn[        5       u  pU	(       a  [        X$X5        O&U
(       a  [        X$U5        O[        X$X5        OUS-  n[        R                  " 5       R                  5       (       a  UUS   ::  a  M  [        5       u  p[        XFSS	9=n(       aq  U	(       a  [        X$X5        OJU
(       a"  [        R                  S
5        [        X$U5        O![        R                  S5        [        X$X5        [        XFSS	9=n(       a  Mq  [        R                  S5        g ! [         a   n[        R                  SU5         S nAg S nAff = f)Nz#Error in tracing control thread: %sre   r   r   r   r   r   Fr   r   r   z+Tracing control sub-thread is shutting down)r   r   rb   rz   rI   r   rF   r   r   r   rL   r   r   r   r   )r   r|   rd   r   rI   r   re   seen_successive_empty_queuesrK   r   r   s              r'   r   r     s    \F~{{ 
 ((M$$$/<$((s;J#$ 
 	((**(:;< 5]UU:U+,(6F6H3%(3: 1&T -: )A-(/ 	((**(:;<0 /?.@+3u *  %/z LL,--fZP LL12(z 4u * $ LL>?s  :A>s   F: :
G$GG$)r   T)rI   r   rJ   r   rC   r7   r5   list[TracingQueueItem])r   r   )rd   r   re   r   rf   z
int | Noner5   z6tuple[Optional[io.BytesIO], Optional[tuple[int, int]]])TN)rd   r   rI   r   r{   r   r|   r7   r}   r7   r~   JOptional[list[Union[SerializedRunOperation, SerializedFeedbackOperation]]]r5   r6   )rd   r   rI   r   r{   r   r}   r7   r~   r   r5   r6   )T)rd   r   rI   r   r{   r   r|   r7   r}   r7   r5   r6   )rd   r   r5   r7   )r5   zOptional[int])r   zls_schemas.LangSmithInfor5   zls_schemas.BatchIngestConfig)r5   ztuple[bool, bool])r   weakref.ref[Client]r5   r6   )g      ?)r   r   r   floatr5   r6   )r   r   r|   r7   r5   r6   )=
__future__r   concurrent.futuresfuturesr   r   	functoolsiologgingr   r   r   r   multiprocessingr   queuer   r   typingr   r   r	   r
   	langsmithr   r   r   r   &langsmith._internal._compressed_tracesr   langsmith._internal._constantsr   r   r   r   langsmith._internal._operationsr   r   r   opentelemetry.context.contextr   langsmith.clientr   	getLoggerrb   r   r   total_orderingr   rL   rk   r   r   r   r   r   r   r   r   r   r   rA   r*   r'   <module>r     s_   "    	  
    %   , ' C   5'			-	.00Y[1_M  #4 #4 #4N ;?!$37, KU00 #0=G0;0p   	::: ": 	:
 :
: 
:B   	<<< "< 	<

< 
<H  NNN "N 	N
 N 
Nb$N
"!03.z<| >ALF#LF5:LF	LF^C@#C@C@ 
C@r*   