ó
    üÞ j‘1  ã                  ó  • S r SSKJr  SSKJr  SSKJrJr  SSKJ	r	  SSK
JrJrJrJrJrJrJrJrJrJrJr    S     SS jjrSS	 jrSS
 jrSS jrSS jrSS jrSS jr S       SS jjrSS jrSS jrSS jr g)zÀShared helper functions for error handling.

These functions are used by both sync and async clients to parse error responses
and raise appropriate exceptions. They contain no I/O operations.
é    )Úannotations)ÚMapping)ÚAnyÚOptional)Úhttpx)ÚQuotaExceededErrorÚResourceCreationErrorÚResourceNotFoundErrorÚResourceTimeoutErrorÚSandboxAPIErrorÚSandboxAuthenticationErrorÚSandboxClientErrorÚSandboxConnectionErrorÚSandboxNotReadyErrorÚSandboxOperationErrorÚValidationErrorNc                ó†   • 0 nX4 H7  nU=(       d    0 R                  5        H  u  pEXRUR                  5       '   M     M9     U$ )u@  Merge request headers, giving precedence to overrides.

Names are normalized to lowercase so an override replaces a base header that
differs only in casing. HTTP header names are case-insensitive, so keeping
both would be ambiguous â€” a server reading the first value would see the
base header instead of the override.
)ÚitemsÚlower)Úbase_headersÚoverride_headersÚmergedÚheadersÚnameÚvalues         ÚT/var/www/html/gaurav/venv/lib/python3.13/site-packages/langsmith/sandbox/_helpers.pyÚmerge_headersr       sD   € ð  €FØ Ó3ˆØ#ŸM r×0Ñ0Ö2‰KˆDØ#(�4—:‘:“<Ó ó 3ñ 4ð €Mó    c                ó¾   • [        U [        5      (       a  U S::  a  [        SU < 35      e[        U[        5      (       a  SUs=::  a  S::  d  O  [        SU< 35      eg)z½Validate parameters for service URL generation.

Args:
    port: Target port inside the sandbox.
    expires_in_seconds: Token TTL.

Raises:
    ValueError: If port or TTL is out of range.
r   z%port must be a positive integer, got é   i€Q z4expires_in_seconds must be between 1 and 86400, got N)Ú
isinstanceÚintÚ
ValueError)ÚportÚexpires_in_secondss     r   Úvalidate_service_paramsr&   7   sh   € ô �dœC× Ñ  D¨A£IÜÐ@ÀÁÐIÓJÐJÜÐ(¬#×.Ñ.Ø	ÐÕ( 5Õ(äðØ%Ñ(ð*ó
ð 	
ð 	)r   c                óx   • U c  gU S:  a  [        U SU  35      eU S:w  a  U S-  S:w  a  [        U SU  35      egg)zåValidate a TTL value for sandbox create/update.

Args:
    value: TTL in seconds (None means unset, 0 disables).
    name: Parameter name for error messages.

Raises:
    ValueError: If value is negative or not a multiple of 60.
Nr   z must be >= 0, got é<   z' must be a multiple of 60 seconds, got )r#   )r   r   s     r   Úvalidate_ttlr)   L   sY   € ð �}ØØˆqƒyÜ˜D˜6Ð!4°U°GÐ<Ó=Ð=Ø�ƒz�e˜b‘j A“oÜ˜D˜6Ð!HÈÈÐPÓQÐQð &€zr   c           	     ó\  •  U R                   R                  5       nUR                  S5      n[        U[        5      (       a-  UR                  S5      UR                  S[        U 5      5      S.$ [        U[        5      (       ap  U(       ai  U Vs/ sH5  n[        U[        5      (       d  M  UR                  S[        U5      5      PM7     nnSU(       a  SR                  U5      S.$ [        U 5      S.$ SU=(       d    [        U 5      S.$ s  snf ! [         a    S[        U 5      S.s $ f = f)zàParse standardized error response.

Expected format: {"detail": {"error": "...", "message": "..."}}

Returns a dict with:
- error_type: The error type (e.g., "ImagePull", "CrashLoop")
- message: Human-readable error message
ÚdetailÚerrorÚmessage©Ú
error_typer-   ÚmsgNz; )	ÚresponseÚjsonÚgetr!   ÚdictÚstrÚlistÚjoinÚ	Exception)r,   Údatar+   ÚdÚmessagess        r   Úparse_error_responser<   c   s  € ð;Ø�~‰~×"Ñ"Ó$ˆØ—‘˜(Ó#ˆô �fœd×#Ñ#à$Ÿj™j¨Ó1Ø!Ÿ:™: i´°U³Ó<ñð ô �fœd×#Ñ#®Ù6<ÓT±f°Ä
È1Ìd×@SÓ,˜Ÿ™˜e¤S¨£VÖ,±fˆHÐTà"Þ2:˜4Ÿ9™9 XÓ.ñð ä@CÀEÃ
ñð ð #¨v×/C¼¸U»ÑDÐDùò Uøô ó ;Ø"¬s°5«zÑ:Ò:ð;ús<   ‚A,D Á/ D ÂDÂ+DÃ
D Ã'D Ã4D ÄD ÄD+Ä*D+c                óH  •  U R                   R                  5       nUR                  S5      n[        U[        5      (       a-  UR                  S5      UR                  S[        U 5      5      S.$ SU=(       d    [        U 5      S.$ ! [         a    S[        U 5      S.s $ f = f)z�Parse error response (simplified version for sandbox operations).

Returns a dict with:
- error_type: The error type
- message: Human-readable error message
r+   r,   r-   r.   N)r1   r2   r3   r!   r4   r5   r8   ©r,   r9   r+   s      r   Úparse_error_response_simpler?   …   s�   € ð;Ø�~‰~×"Ñ"Ó$ˆØ—‘˜(Ó#ˆä�fœd×#Ñ#à$Ÿj™j¨Ó1Ø!Ÿ:™: i´°U³Ó<ñð ð
 #¨v×/C¼¸U»ÑDÐDøÜó ;Ø"¬s°5«zÑ:Ò:ð;ús   ‚A,B Á/B ÂB!Â B!c                ó²   •  U R                   R                  5       nUR                  S/ 5      n[        U[        5      (       a  U$ / $ ! [
         a    / s $ f = f)z÷Parse Pydantic validation error response.

Returns a list of validation error details, each containing:
- loc: Location of the error (e.g., ["body", "resources", "cpu"])
- msg: Human-readable error message
- type: Error type (e.g., "value_error")
r+   )r1   r2   r3   r!   r6   r8   r>   s      r   Úparse_validation_errorrA   ›   sT   € ðØ�~‰~×"Ñ"Ó$ˆØ—‘˜( BÓ'ˆÜ�fœd×#Ñ#ØˆMØˆ	øÜó ØŠ	ðús   ‚AA ÁA ÁAÁAc                ót   • U R                  5       nSU;   a  SU;   d  SU;   a  gSU;   a  gSU;   a  gSU;   a  gg)	znExtract quota type from error message.

Returns one of: "sandbox_count", "cpu", "memory", "storage", or None.
ÚsandboxÚcountÚlimitÚsandbox_countÚcpuÚmemoryÚstorageN)r   )r-   Úmessage_lowers     r   Úextract_quota_typerK   ­   sM   € ð
 —M‘M“O€Mà�MÓ!Ø�=Ó  G¨}Ó$<àØ	�-Ó	ØØ	�]Ó	"ØØ	�mÓ	#ØØr   c                óz   • [        U R                  SUR                  5        S35      UU R                  S5      S9Ue)a  Raise ResourceCreationError with the error_type from the API response.

The error_type indicates the specific failure reason:
- ImagePull: Image pull failed
- CrashLoop: Container crashed during startup
- SandboxConfig: Configuration error
- Unschedulable: Cannot be scheduled
r-   z creation failedr/   ©Úresource_typer/   )r	   r3   Útitle)r9   r,   rN   s      r   Úraise_creation_errorrP   Æ   sE   € ô  Ø�‰�˜}×2Ñ2Ó4Ð5Ð5EÐFÓGØ#Ø—8‘8˜LÓ)ñð ð	r   c                óú  • U R                   R                  n[        U 5      nUS:X  a  [        US   SS9U eUS:X  af  [	        U 5      nU(       aH  [        S U 5       5      (       a1  U(       a  US   R                  SS	/5      S
   OS	n[        US   UUS9U e[        X 5        g	US:X  a  [        US   5      n[        US   US9U eUS:X  a'  [        US   SUR                  S5      =(       d    SS9U e[        U 5        g	)a‡  Handle HTTP errors specific to sandbox creation.

Maps API error responses to specific exception types:
- 408: ResourceTimeoutError (sandbox didn't become ready in time)
- 422: ValidationError (bad input) or ResourceCreationError (runtime)
- 429: QuotaExceededError (org limits exceeded)
- 503: ResourceCreationError (no resources available)
- Other: Falls through to generic error handling
i˜  r-   rC   ©rN   é¦  c              3  óF   #   • U H  oR                  S 5      S:H  v •  M     g7f)ÚtypeÚvalue_errorN)r3   )Ú.0r:   s     r   Ú	<genexpr>Ú0handle_sandbox_creation_error.<locals>.<genexpr>í   s   é € ÐKÁ7¸aŸ5™5 ›=¨MÖ9Â7ùs   ‚!r   ÚlocNéÿÿÿÿ©r-   ÚfieldÚdetailsé­  ©r-   Ú
quota_typeé÷  r/   ÚUnschedulablerM   )r1   Ústatus_coder<   r   rA   Úanyr3   r   rP   rK   r   r	   Úhandle_client_http_error)r,   Ústatusr9   r^   r]   ra   s         r   Úhandle_sandbox_creation_errorrh   Ú   s  € ð �^‰^×'Ñ'€FÜ Ó&€Dà�ƒ}ä" 4¨	¡?À)ÑLÐRWÐWØ	�3‹ä(¨Ó/ˆÞ”sÑKÁ7ÓK×KÑKæ9@�G˜A‘J—N‘N 5¨4¨&Ó1°"Ò5ÀdˆEÜ!Ø˜Y™ØØñð ð	ô ! Õ-Ø	�3‹ä'¨¨Y©Ó8ˆ
Ü Ø˜‘OØ!ñ
ð ð	ð 
�3‹ä#Ø�‰OØ#Ø—x‘x Ó-×@°ñ
ð ð		ô 	! Õ'r   c                óÚ  • [        U 5      nUS   nUR                  S5      nU R                  R                  nUS;   a  [	        U5      U eUS:X  a  [        U5      U eUS:X  a9  [        U 5      nU(       a  US   R                  SS/5      S	   OSn[        UUUS
9U eUS:X  a  [        U5      n[        UUS9U eUS:X  a  US:X  a  [        U5      U eUS:X  a  [        U5      U e[        U5      U e)zLHandle HTTP errors and raise appropriate exceptions (for client operations).r-   r/   )i‘  é“  é”  rS   r   rZ   Nr[   r\   r_   r`   éö  ÚConnectionErroriô  )r<   r3   r1   rd   r   r
   rA   r   rK   r   r   r   r   )r,   r9   r-   r/   rg   r^   r]   ra   s           r   rf   rf     s  € ä Ó&€DØ�9‰o€GØ—‘˜,Ó'€JØ�^‰^×'Ñ'€Fà�ÓÜ(¨Ó1°uÐ<Ø�ƒ}Ü# GÓ,°%Ð7ð �ƒ}Ü(¨Ó/ˆÞ5<�˜‘
—‘˜u t fÓ-¨bÒ1À$ˆÜØØØñ
ð ð		ð �ƒ}Ü'¨Ó0ˆ
Ü ØØ!ñ
ð ð	ð
 �ƒ}˜Ð'8Ó8Ü$ WÓ-°5Ð8Ø�ƒ}Ü˜gÓ&¨EÐ1Ü
˜WÓ
%¨5Ð0r   c                ó¾  • [        U 5      nUS   nUR                  S5      nU R                  R                  nUS:X  a  [	        USUS9U eUS:X  a  [	        USUS9U eUS:X  a  [	        US	US9U eUS
:X  a  [	        USSS9U eUS:X  a  US:X  a  [        U5      U eUS:X  a  US:X  d  US:X  a  US:X  a  [        U5      U eUS:X  d  US:X  a  [        USS9U e[        U5      U e)a"  Handle HTTP errors for sandbox operations (run, read, write).

Maps API error types to specific exceptions:
- WriteError -> SandboxOperationError (operation="write")
- ReadError -> SandboxOperationError (operation="read")
- CommandError -> SandboxOperationError (operation="command")
- ConnectionError (502) -> SandboxConnectionError
- FileNotFound / 404 -> ResourceNotFoundError (resource_type="file")
- NotReady (400) -> SandboxNotReadyError
- ServiceUnavailable (503) -> SandboxNotReadyError
- 403 -> SandboxOperationError (permission denied)
r-   r/   Ú
WriteErrorÚwrite)Ú	operationr/   Ú	ReadErrorÚreadÚCommandErrorÚcommandrj   NÚPermissionDeniedrl   rm   i�  ÚNotReadyrb   ÚServiceUnavailablerk   ÚFileNotFoundÚfilerR   )	r?   r3   r1   rd   r   r   r   r
   r   )r,   r9   r-   r/   rg   s        r   Úhandle_sandbox_http_errorr{   5  s,  € ô ' uÓ-€DØ�9‰o€GØ—‘˜,Ó'€JØ�^‰^×'Ñ'€Fð �\Ó!Ü#Ø˜w°:ñ
àð	ð �[Ó Ü#Ø˜v°*ñ
àð	ð �^Ó#Ü#Ø˜y°Zñ
àð	ð
 �ƒ}Ü#Ø˜tÐ0Bñ
àð	ð
 �ƒ}˜Ð'8Ó8Ü$ WÓ-°5Ð8ð 	�#‹˜*¨
Ó2Ø�#‹˜*Ð(<Ó<ä" 7Ó+°Ð6Ø�ƒ}˜
 nÓ4Ü# G¸6ÑBÈÐMä
˜WÓ
%¨5Ð0r   )NN)r   úOptional[Mapping[str, str]]r   r|   Úreturnzdict[str, str])r$   r"   r%   r"   r}   ÚNone)r   zOptional[int]r   r5   r}   r~   )r,   úhttpx.HTTPStatusErrorr}   údict[str, Any])r,   r   r}   z
list[dict])r-   r5   r}   zOptional[str])rC   )r9   r€   r,   r   rN   r5   r}   r~   )r,   r   r}   r~   )!Ú__doc__Ú
__future__r   Úcollections.abcr   Útypingr   r   Ú langsmith._openapi_client._httpxr   Úlangsmith.sandbox._exceptionsr   r	   r
   r   r   r   r   r   r   r   r   r   r&   r)   r<   r?   rA   rK   rP   rh   rf   r{   © r   r   Ú<module>rˆ      s²   ðñõ #å #ß  å 2÷÷ ÷ ñ ð( 15Ø48ðØ-ðà1ðð õô.
ô*Rô.;ôD;ô,ô$ð8 #ðØ
ðà ðð ðð 
õ	ô(.(ôb"1õT21r   