
    f3fi"                        U d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z
 dZded<   dd	Zddd
Z	ddZdaddZddZy)z"UUID helpers backed by uuid-utils.    )annotationsN)Final)uuid7i ʚ;r   _NANOS_PER_SECONDc                0    t        | t              \  }}||fS )zDSplit a nanosecond timestamp into seconds and remaining nanoseconds.)divmodr   nanosecondssecondsnanoss      W/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langsmith/_internal/_uuid.py_to_timestamp_and_nanosr      s    K):;NGUE>    c                P    | 
t               S t        |       \  }}t        ||      S )zGenerate a UUID from a Unix timestamp in nanoseconds and random bits.

    UUIDv7 objects feature monotonicity within a millisecond.

    Args:
        nanoseconds: Optional ns timestamp. If not provided, uses current time.
    )	timestampr   )_uuid_utils_uuid7r   r	   s      r   r   r      s.    ,  "",[9NGUwe<<r   c                     | 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)uuid_objs    r   
is_uuid_v7r   1   s     q  r   Fc                f    t        |       s&t        sdat        j                  dt        d       yyy)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)r   _UUID_V7_WARNING_EMITTEDwarningswarnUserWarning)r   id_types     r   warn_if_not_uuid_v7r    @   s7     h(@#' 8 
	
 )Ar   c                   ddl }|  d| j                         }t        j                  |      j	                         }t        d      }t        |       r| j                  dd |dd n-|j                         dz  }|dz  }|j                  dd      |dd d	|d   d
z  z  |d<   |d   |d<   d|d   dz  z  |d<   |dd |dd t        j                  t        |            S )a  Generate a deterministic UUID7 derived from an original UUID and a key.

    This function creates a new UUID that:
    - Preserves the timestamp from the original UUID if it's UUID v7
    - Uses current time if the original is not UUID v7
    - Uses deterministic "random" bits derived from hashing the original + key
    - Is valid UUID v7 format

    This is used for creating replica IDs that maintain time-ordering properties
    while being deterministic across distributed systems.

    Args:
        original_id: The source UUID (ideally UUID v7 to preserve timestamp).
        key: A string key used for deterministic derivation (e.g., project name).

    Returns:
        A new UUID v7 with preserved timestamp (if original is v7) and
        deterministic random bits.

    Example:
        >>> original = uuid7()
        >>> replica_id = uuid7_deterministic(original, "replica-project")
        >>> # Same inputs always produce same output
        >>> assert uuid7_deterministic(original, "replica-project") == replica_id
    r   N:      i@B l    bigp         r         ?      r   
   	   )bytes)timeencodehashlibsha256digest	bytearrayr   r/   time_nsto_bytesuuidUUID)original_idkeyr0   
hash_inputhbtimestamp_ms
unix_ts_mss           r   uuid7_deterministicrA   W   s    4   =#'..0Jz"))+A 	"A +""1Q'!A ||~2!$44
$$Q.!A 1Q4$;AaD Q4AaD 1Q4$;AaD "gAaG9958$$r   )r
   intreturnztuple[int, int])N)r
   z
int | NonerC   	uuid.UUID)r   rD   rC   bool)r   rD   r   strrC   None)r:   rD   r;   rF   rC   rD   )__doc__
__future__r   r2   r8   r   typingr   uuid_utils.compatr   r   r   __annotations__r   r   r   r    rA    r   r   <module>rN      sG    ( "     8( 5 (=8	! ! 
.E%r   