o
    +i                     @  s   U d Z 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ee	Z
dZdaded< daded< dddZddddZdddZdadddZdS ) zUUID v7 implementation backported from Python 3.14+.

This module provides UUID v7 generation for Python versions < 3.14.
The implementation is taken directly from CPython's uuid.py:
https://github.com/python/cpython/blob/main/Lib/uuid.py
    )annotationsNl             
int | None_last_timestamp_v7int_last_counter_v7returntuple[int, int]c                  C  s0   t jtddd} | d? d@ }| d@ }||fS )z4Generate random counter and tail values for UUID v7.
   big	byteorder    l       )r   
from_bytesosurandom)randcountertail r   V/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/langsmith/_internal/_uuid.py_uuid7_get_counter_and_tail   s   r   nanoseconds	uuid.UUIDc           
      C  s   | du rt  } | d }tdu s|tkrt \}}n$|tk r"td }td }|dkr4|d7 }t \}}n
tjtddd}|d@ }|d	? }|d
@ }|d@ }|dM }|d> }||d> O }||d> O }||O }|t	O }t
j|d}	|a|a|	S )zGenerate a UUID from a Unix timestamp in milliseconds and random bits.

    UUIDv7 objects feature monotonicity within a millisecond.

    Args:
        nanoseconds: Optional ns timestamp. If not provided, uses current time.
    Ni@B    l      r
   r   l       i  i?r   P   @   r   )r   )timetime_nsr   r   r   r   r   r   r   _RFC_4122_VERSION_7_FLAGSuuidUUID)
r   timestamp_msr   r   
unix_ts_mscounter_msbs
counter_hi
counter_lo
int_uuid_7resr   r   r   uuid7$   s4   r+   uuid_objboolc                 C  s
   | j dkS )zCheck if a UUID is version 7.

    Args:
        uuid_obj: The UUID to check.

    Returns:
        True if the UUID is version 7, False otherwise.
       )version)r,   r   r   r   
is_uuid_v7h   s   
	r0   Fid_typestrNonec                 C  s,   t | stsdatjdtdd dS dS dS )zWarn if a UUID is not version 7.

    Args:
        uuid_obj: The UUID to check.
        id_type: The type of ID (e.g., "run_id", "trace_id") for the warning message.
    TzLangSmith now uses UUID v7 for run and trace identifiers. This warning appears when passing custom IDs. Please use: from langsmith import uuid7
            id = uuid7()
Future versions will require UUID v7.   )
stacklevelN)r0   _UUID_V7_WARNING_EMITTEDwarningswarnUserWarning)r,   r1   r   r   r   warn_if_not_uuid_v7w   s   
r:   )r   r   )N)r   r   r   r   )r,   r   r   r-   )r,   r   r1   r2   r   r3   )__doc__
__future__r   loggingr   r   r"   r7   	getLogger__name__loggerr!   r   __annotations__r   r   r+   r0   r6   r:   r   r   r   r   <module>   s     



D