Ë
    ñÏji&  ã                  óh  — 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	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZmZ  ej(                  e«      Zdd
„Zdd„Zdd„Zdd„Zee
e   e
e   f   Z	 dd„Zdd„Z	 	 	 	 	 	 	 	 dd„Z d d„Z!ejD                  d!d„«       Z#dd„Z$ddddœ	 	 	 	 	 	 	 d"d„Z%ddœd#d„Z&y)$a   Resolving whether a run is addressed by project or by address.

!!! warning "Beta"
    Addressing runs to an address is enabled per workspace. A workspace
    without it rejects the runs, so tracing is lost rather than falling back
    to a project. This API may change without notice.

A run goes to exactly one destination: a project (`project_name`) or an
address (`langsmith.AgentAddress(id, environment)`), which
names an agent and one of its environments. The server resolves an address to
the agent environment's project, so traces follow the agent rather than a
project name. Project addressing keeps working alongside it.

A run with a parent, in-process or from distributed-tracing headers, joins its
parent's destination. A root run takes the first of these levels that names a
project or an address, whichever it names; naming both at one level raises:

1. `tracing_context`
2. `langsmith_extra`
3. the `@traceable` / `trace` arguments
4. `configure`
5. the env vars (`LANGSMITH_PROJECT`, `LANGSMITH_AGENT_*`)

In code the address is an `AgentAddress`: in context variables, run trees and
replicas. It becomes its LRN string only on the wire, in the `address` payload
field, which is set only here, in `apply_to_payload` and
`FeedbackCreate.model_dump`, and in the `langsmith-address` baggage entry.
é    )ÚannotationsN)ÚMapping)ÚAnyÚOptional©Úaddress)Úutils©Ú
_warn_once)ÚAgentAddressÚEnvAddressErrorc                ó4   — | €dS t        j                  | «      S )z¢Return `address` if it is an `AgentAddress`, or `None` if it is `None`.

    Raises:
        utils.LangSmithUserError: If `address` is not an `AgentAddress`.
    N)Ú_addressÚensure_agentr   s    ú\/var/www/html/gaurav-v2/venv/lib/python3.12/site-packages/langsmith/_internal/_addressing.pyÚcheck_addressr   .   s   € ð �?ˆ4ÐF¬×(=Ñ(=¸gÓ(FÐFó    c                ó4   — | €dS t        j                  | «      S )zôReturn `address` if it is an `Address` of any kind, or `None` if it is `None`.

    For the calls that resolve an address to its project, which take any kind.

    Raises:
        utils.LangSmithUserError: If `address` is not an `Address`.
    N)r   Úensure_addressr   s    r   Úcheck_lookup_addressr   7   s   € ð �?ˆ4ÐH¬×(?Ñ(?ÀÓ(HÐHr   c                óô   — | €yg }| D ]m  }t        |t        «      s|j                  dt        |«      i«       Œ0t	        |«      }|j                  d«      �t        |d   «      |d<   |j                  |«       Œo |S )zJPut a bare `AgentAddress` replica in an `address` key, and check the rest.Nr   )Ú
isinstancer   Úappendr   ÚdictÚget)ÚreplicasÚ
normalizedÚreplicaÚnormalized_replicas       r   Únormalize_replicasr    B   s„   € àÐØØ€JÛˆÜ˜'¤7Ô+Ø×Ñ˜y¬-¸Ó*@ÐAÔBØÜ! '›]ÐØ�;‰;�yÓ!Ð-Ü,9¸'À)Ñ:LÓ,MÐ˜yÑ)Ø×ÑÐ,Õ-ð ð Ðr   c                 ó   — t        d«       y)zŒWarn the first time a run is actually addressed to an address.

    `_warn_once` caches on the message, so this fires once per process.
    zÝAddressing runs with `langsmith.address` is in beta and is enabled per workspace. A workspace without it rejects the run, so the trace is lost rather than falling back to a project. The behavior may change without notice.Nr
   © r   r   Úwarn_is_betar#   R   s   € ô
 ð	õr   c            	     ó8  — t        | d¬«      D ],  \  }\  }}|r|�t        ||d|› �«      ‚|r|dfc S |€Œ(d|fc S  t        j                  d¬«      xs d}t	        j
                  «       }|r!|�t        t        t        ||d«      «      «      ‚|�d|fS |xs ddfS )	a*  Settle a run's single destination, as `(project, address)`.

    `tiers` are the levels named in code, highest precedence first; the env
    vars are the last level, consulted here. The first level that names
    anything decides, whichever mode it names -- an address in `tracing_context`
    beats a project on the decorator, and the other way round.

    Raises:
        utils.LangSmithUserError: If one level named in code names both.
        EnvAddressError: If the env vars are the deciding level and name both,
            or half an address.
    é   ©ÚstartNúat precedence level F©Úreturn_default_valuezin the environmentÚdefault)Ú	enumerateÚ_both_at_one_levelr	   Úget_tracer_projectr   Úfrom_envr   Ústr)ÚtiersÚlevelÚprojectr   Úenv_projectÚenv_addresss         r   Úresolver6   c   sË   € ô &/¨u¸A×%>Ñ!ˆÑ!�˜Ù�wÐ*Ü$ W¨gÐ9MÈeÈWÐ7UÓVÐVÙØ˜D�=Ò ØÑØ˜�=Ò ð &?ô ×*Ñ*ÀÔFÒNÈ$€KÜ×'Ñ'Ó)€KÙ�{Ð.ÜÜÔ" ;°Ð=QÓRÓSó
ð 	
ð ÐØ�[Ð Ð ØÒ#˜) TÐ)Ð)r   c                 óx   — t        | d¬«      D ]*  \  }\  }}|r|�t        ||d|› �«      ‚|s|€Œ"|xs d|fc S  y)z–Return the highest level that names a destination, without the env vars.

    Raises:
        utils.LangSmithUserError: If that level names both.
    r%   r&   Nr(   )NN)r,   r-   )r1   r2   r3   r   s       r   Úfirst_namedr8   ‚   sZ   € ô &/¨u¸A×%>Ñ!ˆÑ!�˜Ù�wÐ*Ü$ W¨gÐ9MÈeÈWÐ7UÓVÐVÙ�gÑ)Ø’?˜d GÐ+Ò+ð	 &?ð
 r   c           	     ó@   — t        j                  d| ›d|›d|› d�«      S )NzA project (z) and an address (z) are both set zj, so neither outranks the other. Set only one there, or set the one you want at a higher-precedence level.©r	   ÚLangSmithUserError)r3   r   Úwheres      r   r-   r-   �   s6   € ô ×#Ñ#Ø
�g�[Ð 2°7°+¸_Øˆ'ð 9ð	9óð r   c                ó,   — t        t        | «      «       y)zDLog, once per distinct cause, that calls run untraced for a bad env.N)Ú_log_untraced_oncer0   )Úerrors    r   Úlog_untracedr@   š   s   € ä”s˜5“zÕ"r   c                ó0   — t         j                  d| «       y )Nz&LangSmith is not tracing this call: %s)Ú_LOGGERÚwarning)Úmessages    r   r>   r>   Ÿ   s   € ä‡O�OÐ<¸gÕFr   c                 ó¾  — t        j                  «       j                  «       D � �cg c]
  \  } }|sŒ	| ‘Œ }} }|sy	 t        j                  «       }|€yt        j                  d¬«      }|€yt        j                  d|›d|›d	�t        j                  d¬«       yc c}} w # t
        $ r3}t        j                  |› d�t        j                  d¬«       Y d}~yd}~ww xY w)
aa  Warn when the environment's address cannot be used.

    Half an address, an invalid one, or one beside a configured project, leaves
    calls that name nothing in code untraced -- and a name like
    `LANGSMITH_AGENT_ENVIRONMENT` is generic enough to be set by accident.

    Emitted at client construction rather than per run, so it is seen once.
    Nz7 Calls that name no destination in code are not traced.é   )Ú
stacklevelFr)   zEThe address from LANGSMITH_AGENT_ID and LANGSMITH_AGENT_ENVIRONMENT (z) and a configured project (zo) are both set in the environment, so calls that name no destination in code are not traced. Unset one of them.)r   Ú
env_valuesÚitemsr   r/   r   ÚwarningsÚwarnr	   ÚLangSmithWarningr.   )ÚnameÚvalueÚpresentr   Úer3   s         r   Úwarn_on_envrQ   ¤   sç   € ô (0×':Ñ':Ó'<×'BÑ'BÔ'DÔNÑ'D™˜˜eÊŠtÐ'D€GÑNÙØðÜ×'Ñ'Ó)ˆð €ØÜ×&Ñ&¸EÔB€GØ€ØÜ‡M�MðØˆ;ð Ø�;ð Dð	Dô 	×ÑØöùó# Oøô
 ò Ü�‰ØˆcÐHÐIÜ×"Ñ"Øõ	
ô
 	ûðús!   ¦
B±B¼B  Â 	CÂ))CÃC)r3   Ú
session_idr   c                óR   — | �| n|}|�|�t        j                  d|›d|›d�«      ‚yy)a¢  Reject a call that names both a project and an address.

    Pass only values the caller supplied in this call: an inherited or
    ambient address beside an explicit project is not a conflict -- the project
    wins, which is what lets an evaluation set its own project while
    `LANGSMITH_AGENT_ID` is set process-wide.

    Raises:
        utils.LangSmithUserError: If a project and an address are both named.
    NzA run is addressed by project (z) or by address (z), not both.r:   )r3   rR   r   Únamed_projects       r   Úreject_conflictingrU   È   sO   € ð   'Ð2‘G¸
€MØÐ  WÐ%8Ü×&Ñ&Ø-¨mÐ->ð ?Øˆ{˜,ð(ó
ð 	
ð &9Ð r   F)Úupdatec               óø  — | j                  dd«      }t        |t        «      r	 t        j                  |«      }nt        |«      }| j                  d«      duxs | j                  d«      du}|€|s|st        j                  «       }|€yt        «        |j                  «       | d<   |s%| j                  dd«       | j                  dd«       yy# t
        j                  $ r t        j                  d«      d‚w xY w)aÖ  Put a run payload's address in its wire field, as its LRN string.

    The one place a run's `address` is settled. A project already on the
    payload addresses the run, so the environment is not consulted. With no
    project, an ambient address fills in and the null project keys are dropped.

    On an update the environment is not consulted: a patch inherits its
    address from the post that established it, and one naming nothing is
    resolved by run id.
    r   Nz1The run's `address` is not a valid agent address.rR   Úsession_name)Úpopr   r0   r   Ú	_from_lrnr	   r;   r   r   r/   r#   Ú_lrn)ÚpayloadrV   r   rT   s       r   Úapply_to_payloadr]   à   só   € ð �k‰k˜) TÓ*€GÜ�'œ3Ôð	Ü"×,Ñ,¨WÓ5‰Gô   Ó(ˆà�‰�LÓ!¨Ð-ÒX°·±¸^Ó1LÐTXÐ1Xð ð €¡©-Ü×'Ñ'Ó)ˆØ€ØÜ„NØ Ÿ™›€GˆIÑÙØ�‰�N DÔ)Ø�‰�L $Õ'ð øô ×'Ñ'ò 	Ü×*Ñ*ØCóàðð	ús   ¤C Ã*C9)r   r   ÚreturnzOptional[AgentAddress])r   r   r^   úOptional[_address.Address])r   úOptional[Any]r^   zOptional[list])r^   ÚNone)r1   ÚTierr^   z,tuple[Optional[str], Optional[AgentAddress]])r1   rb   r^   rb   )r3   r0   r   r   r<   r0   r^   zutils.LangSmithUserError)r?   r   r^   ra   )rD   r0   r^   ra   )r3   r`   rR   r`   r   r_   r^   ra   )r\   r   rV   Úboolr^   ra   )'Ú__doc__Ú
__future__r   Ú	functoolsÚloggingrJ   Úcollections.abcr   Útypingr   r   Ú	langsmithr   r   r	   Ú#langsmith._internal._beta_decoratorr   Úlangsmith.addressr   r   Ú	getLoggerÚ__name__rB   r   r   r    r#   Útupler0   rb   r6   r8   r-   r@   Úcacher>   rQ   rU   r]   r"   r   r   Ú<module>rq      s  ðñõ: #ã Û Û Ý #ß  å )Ý Ý :ß ;à
ˆ'×
Ñ
˜HÓ
%€óGóIóó 
ð ˆX�c‰]˜H \Ñ2Ð2Ñ3€Ø Só*ó>ðØðØ'ðØ03ðàóó#ð
 ‡�òGó ðGó!ðL "Ø $Ø*.ñ	
àð
ð ð
ð (ð	
ð
 
ó
ð0 7<ö "(r   