o
    ]+ ih:                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ eeZG dd dZG dd dZ d0ddZ!d1ddZ"d2ddZ#d3d d!Z$d4d#d$Z%d5d)d*Z&d6d.d/Z'dS )7    )annotationsN)Iterable)BufferedReader)LiteralOptionalUnioncast)schemas)_orjson)CompressedTraces)MultipartPartMultipartPartsAndContext)
dumps_jsonc                   @  s   e Zd ZU ded< ded< ded< ded< ded	< ded
< ded< ded< ded< ded< ded< dZ							d"d#ddZd$ddZd%ddZd&d d!ZdS )'SerializedRunOperationLiteral['post', 'patch']	operation	uuid.UUIDidtrace_idbytes_noneOptional[bytes]inputsoutputseventsextraerror
serialized Optional[ls_schemas.Attachments]attachmentsr   r   r   r   r   r   r   r   r   r   r   NreturnNonec                 C  sF   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
d S Nr    )selfr   r   r   r   r   r   r   r   r   r   r    r%   k/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/langsmith/_internal/_operations.py__init__3   s   
zSerializedRunOperation.__init__intc                 C  s   d}| j r|t| j 7 }| jr|t| j7 }| jr |t| j7 }| jr*|t| j7 }| jr4|t| j7 }| jr>|t| j7 }| jrH|t| j7 }| jr`| j	 D ]\}}t
|tr_|t|7 }qP|S )3Calculate actual serialized size of this operation.r   )r   lenr   r   r   r   r   r   r   values
isinstancer   )r$   sizecontent_typedata_or_pathr%   r%   r&   calculate_serialized_sizeM   s*   
z0SerializedRunOperation.calculate_serialized_sizedictc                 C  s^   t | j}| jdurt | j|d< | jdur t | j|d< | jdur-t | j|d< |S )zFDeserialize the main run info (_none and extra, error and serialized).Nr   r   r   )r
   loadsr   r   r   r   )r$   Zrun_infor%   r%   r&   deserialize_run_infod   s   


z+SerializedRunOperation.deserialize_run_infootherobjectboolc                 C  sj   t |to4| j| j| j| j| j| j| j| j	| j
| j| jf|j|j|j|j|j|j|j|j	|j
|j|jfkS r#   )r,   r   r   r   r   r   r   r   r   r   r   r   r   r$   r4   r%   r%   r&   __eq__r   s4   
zSerializedRunOperation.__eq__)NNNNNNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r"   r!   r(   )r!   r1   r4   r5   r!   r6   )	__name__
__module____qualname____annotations__	__slots__r'   r0   r3   r8   r%   r%   r%   r&   r      s0   
 

r   c                   @  sH   e Zd ZU ded< ded< ded< dZdd	d
ZdddZdddZdS )SerializedFeedbackOperationr   r   r   r   feedbackr   r   rA   r!   r"   c                 C  s   || _ || _|| _d S r#   rB   )r$   r   r   rA   r%   r%   r&   r'      s   
z$SerializedFeedbackOperation.__init__r(   c                 C  s
   t | jS )r)   )r*   rA   )r$   r%   r%   r&   r0      s   
z5SerializedFeedbackOperation.calculate_serialized_sizer4   r5   r6   c                 C  s*   t |to| j| j| jf|j|j|jfkS r#   )r,   r@   r   r   rA   r7   r%   r%   r&   r8      s   
z"SerializedFeedbackOperation.__eq__N)r   r   r   r   rA   r   r!   r"   r9   r:   )r;   r<   r=   r>   r?   r'   r0   r8   r%   r%   r%   r&   r@      s   
 

r@   rA   &Union[ls_schemas.FeedbackCreate, dict]r!   c                 C  s   t | drtt| dr|  }ntt| }d|vr!t |d< nt|d tr1t	|d |d< d|vr<t |d< nt|d trLt	|d |d< t
|d |d t|dS )Nr1   r   r   rB   )hasattrcallablegetattrr1   r   uuiduuid4r,   strUUIDr@   _dumps_json)rA   Zfeedback_creater%   r%   r&   serialize_feedback_dict   s    

rL   r   r   payloadr1   c           	      C  s   | dd }| dd }| dd }| dd }| dd }| dd }| dd }t| |d |d	 t||d ur=t|nd |d urFt|nd |d urOt|nd |d urXt|nd |d urat|nd |d urjt|nd |d urs|d
S d d
S )Nr   r   r   r   r   r   r   r   r   r    )popr   rK   )	r   rM   r   r   r   r   r   r   r   r%   r%   r&   serialize_run_dict   s,   
rO   ops@list[Union[SerializedRunOperation, SerializedFeedbackOperation]]c                 C  sX  dd | D }g }| D ]}t |tr|jdkrq||j}|d u r(|| q|jd urQ|j|jkrQt|j}dd t|j	 D }|
| t||_|jd urZ|j|_|jd urc|j|_|jd url|j|_|jd uru|j|_|jd ur~|j|_|jd ur|j|_|jd ur|jd u ri |_|j
|j q|| qtt| |S )Nc                 S  s(   i | ]}t |tr|jd kr|j|qS )post)r,   r   r   r   ).0opr%   r%   r&   
<dictcomp>   s    
z7combine_serialized_queue_operations.<locals>.<dictcomp>rR   c                 S  s   i | ]\}}|d ur||qS r#   r%   rS   kvr%   r%   r&   rU      s    )r,   r   r   getr   appendr   r
   r2   itemsupdatedumpsr   r   r   r   r   r   r   list	itertoolschainr+   )rP   Zcreate_ops_by_idZpassthrough_opsrT   Z	create_opZcreate_op_dictZop_dictr%   r%   r&   #combine_serialized_queue_operations   sN   











ra   rT   r   c              	   C  s@   t d| j d | jddtt| jiffgd| j d| j S )Nz	feedback.application/jsonContent-Lengthtrace=,id=)r   r   rA   rI   r*   r   )rT   r%   r%   r&   <serialized_feedback_operation_to_multipart_parts_and_context  s   
rf   :tuple[MultipartPartsAndContext, dict[str, BufferedReader]]c                 C  s  g }i }| | j d| j d | jddtt| jiff d| jfd| jfd| jfd| j	fd| j
fd	| jffD ]&\}}|d u rAq8|}| | j d| j d| d |ddtt|iff q8| jr| j D ]x\}\}}d|v rtd
| d| j d qgt|tr| d| j d| d ||dtt|iff qgztj|}	t|d}
W n ty   td| j| Y qgw |
|t|tt  < | d| j d| d |
| d|	 i ff qgt|d| j d| j |fS )N.rb   rc   r   r   r   r   r   r   z Skipping logging of attachment 'z
' for run zx: Invalid attachment name.  Attachment names must not contain periods ('.'). Please rename the attachment and try again.zattachment.rbz(Attachment file not found for run %s: %sz	; length=rd   re   )rZ   r   r   r   rI   r*   r   r   r   r   r   r   r   r[   loggerwarningr,   r   ospathgetsizeopenFileNotFoundErrorrG   rH   r   r   )rT   Z	acc_partsZopened_files_dictkeyvalueZvalbnr.   r/   	file_sizefiler%   r%   r&   7serialized_run_operation_to_multipart_parts_and_context  s   
rv   parts_and_contextboundaryrI   4Iterable[tuple[bytes, Union[bytes, BufferedReader]]]c                 c  s    | j D ]>\}\}}}}d| dd| dg}|r#|d| d |d| dgdd | D d d	| |fV  qd S )
Nz--
z&Content-Disposition: form-data; name=""z; filename="z
Content-Type: c                 S  s    g | ]\}}| d | dqS )z: rz   r%   rV   r%   r%   r&   
<listcomp>  s     z6encode_multipart_parts_and_context.<locals>.<listcomp> )partsrZ   extendr[   joinencode)rw   rx   Z	part_namefilenamedatar.   headersheader_partsr%   r%   r&   "encode_multipart_parts_and_contextx  s"   


r   compressed_tracesr   r6   c           
      C  s  |j j}g }d}t| |D ]'\}}t|ttfs't|tr!| nt|	 }|
||f |t|7 }qt|dd}|dur\|dkr\|j}	|	dkr\|	| |kr\td|| j|	| dS |D ]\}}|| | jt|7  _|| |d q^|j
| j dS )zCompress multipart parts into the shared compressed buffer.

    Returns True if the parts were enqueued into the compressed buffer, or False
    if they were rejected because the configured in-memory size limit would be
    exceeded.
    r   Zmax_uncompressed_size_bytesNzCompressed traces queue size limit (%s bytes) exceeded. Dropping trace data with context: %s. Current queue size: %s bytes, attempted addition: %s bytes.Fs   
T)Zcompressor_writerwriter   r,   r   	bytearrayr   readrI   r   rZ   r*   rF   uncompressed_sizerj   rk   context_context)
rw   r   rx   r   r~   Zop_uncompressed_sizer   r   Z	max_bytesZcurrent_sizer%   r%   r&   $compress_multipart_parts_and_context  s<   	
r   )rA   rC   r!   r@   )r   r   rM   r1   r!   r   )rP   rQ   r!   rQ   )rT   r@   r!   r   )rT   r   r!   rg   )rw   r   rx   rI   r!   ry   )rw   r   r   r   rx   rI   r!   r6   )(
__future__r   r_   loggingrl   rG   collections.abcr   ior   typingr   r   r   r   Z	langsmithr	   Z
ls_schemasZlangsmith._internalr
   Z&langsmith._internal._compressed_tracesr   Zlangsmith._internal._multipartr   r   Zlangsmith._internal._serder   rK   	getLoggerr;   rj   r   r@   rL   rO   ra   rf   rv   r   r   r%   r%   r%   r&   <module>   s.    
z



6

Y