§
    ~Štjl1  ã                  óÐ   — d Z ddlmZ ddlmZ ddlmZmZ ddlZddl	m
Z
mZmZmZmZmZmZmZmZmZmZ 	 	 d(d)d„Zd*d„Zd+d„Zd,d„Zd,d„Zd-d„Zd.d „Z	 d/d0d$„Zd1d%„Zd1d&„Zd1d'„ZdS )2zÀ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ÚOptionalN)ÚQuotaExceededErrorÚResourceCreationErrorÚResourceNotFoundErrorÚResourceTimeoutErrorÚSandboxAPIErrorÚSandboxAuthenticationErrorÚSandboxClientErrorÚSandboxConnectionErrorÚSandboxNotReadyErrorÚSandboxOperationErrorÚValidationErrorÚbase_headersúOptional[Mapping[str, str]]Úoverride_headersÚreturnúdict[str, str]c                ó~   — i }| |fD ]5}|pi                       ¦   «         D ]\  }}|||                     ¦   «         <   ŒŒ6|S )uT  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)r   r   ÚmergedÚheadersÚnameÚvalues         úX/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langsmith/sandbox/_helpers.pyÚmerge_headersr   !   s`   € ð  €FØ Ð"2Ð3ð )ð )ˆØ#˜M r×0Ò0Ñ2Ô2ð 	)ð 	)‰KˆD�%Ø#(ˆF�4—:’:‘<”<Ñ Ð ð	)à€Mó    ÚportÚintÚexpires_in_secondsÚNonec                óÌ   — t          | t          ¦  «        r| dk    rt          d| ›�¦  «        ‚t          |t          ¦  «        rd|cxk    rdk    sn t          d|›�¦  «        ‚dS )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)Ú
isinstancer"   Ú
ValueError)r!   r#   s     r   Úvalidate_service_paramsr)   8   s•   € õ �d�CÑ Ô ð K D¨A¢I IÝÐIÀÐIÐIÑJÔJÐJÝÐ(­#Ñ.Ô.ð 
Ø	ÐÐ(Ð(Ò(Ð( 5Ò(Ð(Ð(Ð(åð*Ø%ð*ð *ñ
ô 
ð 	
ð 	)Ð(r    r   úOptional[int]r   Ústrc                óŒ   — | €dS | dk     rt          |› d| › �¦  «        ‚| dk    r| dz  dk    rt          |› d| › �¦  «        ‚dS dS )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.   M   so   € ð €}ØˆØˆq‚y€yÝ˜DÐ<Ð<°UÐ<Ð<Ñ=Ô=Ð=Ø�‚z€z�e˜b‘j A’o�oÝ˜DÐPÐPÈÐPÐPÑQÔQÐQð €z�o�or    Úerrorúhttpx.HTTPStatusErrorúdict[str, Any]c                ó   — 	 | j                              ¦   «         }|                     d¦  «        }t          |t          ¦  «        r9|                     d¦  «        |                     dt          | ¦  «        ¦  «        dœS t          |t          ¦  «        r7|r5d„ |D ¦   «         }d|rd                     |¦  «        nt          | ¦  «        dœS d|pt          | ¦  «        dœS # t          $ r dt          | ¦  «        dœcY S w xY w)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
    Údetailr/   Úmessage©Ú
error_typer4   c                ó~   — g | ]:}t          |t          ¦  «        ¯|                     d t          |¦  «        ¦  «        ‘Œ;S )Úmsg)r'   ÚdictÚgetr+   ©Ú.0Úds     r   ú
<listcomp>z(parse_error_response.<locals>.<listcomp>z   s;   € ÐTÐTÐT°Å
È1ÍdÑ@SÔ@SÐT˜Ÿš˜e¥S¨¡V¤VÑ,Ô,ÐTÐTÐTr    Nz; )	ÚresponseÚjsonr:   r'   r9   r+   ÚlistÚjoinÚ	Exception)r/   Údatar3   Úmessagess       r   Úparse_error_responserF   d   s#  € ð;ØŒ~×"Ò"Ñ$Ô$ˆØ—’˜(Ñ#Ô#ˆõ �f�dÑ#Ô#ð 	à$Ÿjšj¨Ñ1Ô1Ø!Ÿ:š: iµ°U±´Ñ<Ô<ðð ð õ �f�dÑ#Ô#ð 	¨ð 	ØTÐT°fÐTÑTÔTˆHà"Ø2:ÐJ˜4Ÿ9š9 XÑ.Ô.Ð.ÅÀEÁ
Ä
ðð ð ð #¨vÐ/C½¸U¹¼ÐDÐDÐDøÝð ;ð ;ð ;Ø"­s°5©z¬zÐ:Ð:Ð:Ð:Ð:ð;øøøs   ‚A;C Á>AC Ã
C ÃC=Ã<C=c                óh  — 	 | j                              ¦   «         }|                     d¦  «        }t          |t          ¦  «        r9|                     d¦  «        |                     dt          | ¦  «        ¦  «        dœS d|pt          | ¦  «        dœS # t          $ r dt          | ¦  «        dœcY S w xY w)z­Parse error response (simplified version for sandbox operations).

    Returns a dict with:
    - error_type: The error type
    - message: Human-readable error message
    r3   r/   r4   r5   N)r?   r@   r:   r'   r9   r+   rC   ©r/   rD   r3   s      r   Úparse_error_response_simplerI   †   sÂ   € ð;ØŒ~×"Ò"Ñ$Ô$ˆØ—’˜(Ñ#Ô#ˆå�f�dÑ#Ô#ð 	à$Ÿjšj¨Ñ1Ô1Ø!Ÿ:š: iµ°U±´Ñ<Ô<ðð ð ð
 #¨vÐ/C½¸U¹¼ÐDÐDÐDøÝð ;ð ;ð ;Ø"­s°5©z¬zÐ:Ð:Ð:Ð:Ð:ð;øøøs   ‚A;B Á>B ÂB1Â0B1ú
list[dict]c                ó¸   — 	 | j                              ¦   «         }|                     dg ¦  «        }t          |t          ¦  «        r|S g S # t
          $ r g cY S w xY w)a  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")
    r3   )r?   r@   r:   r'   rA   rC   rH   s      r   Úparse_validation_errorrL   œ   sm   € ðØŒ~×"Ò"Ñ$Ô$ˆØ—’˜( BÑ'Ô'ˆÝ�f�dÑ#Ô#ð 	ØˆMØˆ	øÝð ð ð Øˆ	ˆ	ˆ	ðøøøs   ‚AA
 ÁA
 Á
AÁAr4   úOptional[str]c                ón   — |                       ¦   «         }d|v r
d|v sd|v rdS d|v rdS d|v rdS d|v rdS dS )	zvExtract quota type from error message.

    Returns one of: "sandbox_count", "cpu", "memory", "storage", or None.
    ÚsandboxÚcountÚlimitÚsandbox_countÚcpuÚmemoryÚstorageN)r   )r4   Úmessage_lowers     r   Úextract_quota_typerW   ®   sk   € ð
 —M’M‘O”O€Mà�MÐ!Ð!Ø�=Ð Ð  G¨}Ð$<Ð$<àˆØ	�-Ð	Ð	ØˆuØ	�]Ð	"Ð	"ØˆxØ	�mÐ	#Ð	#ØˆyØˆ4r    rO   rD   Úresource_typec                ó    — t          |                      d|                     ¦   «         › d�¦  «        ||                      d¦  «        ¬¦  «        |‚)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
    r4   z creation failedr6   ©rX   r6   )r   r:   Útitle)rD   r/   rX   s      r   Úraise_creation_errorr\   Ç   sX   € õ  Ø�Š�˜}×2Ò2Ñ4Ô4ÐFÐFÐFÑGÔGØ#Ø—8’8˜LÑ)Ô)ðñ ô ð ð	r    c                óh  — | j         j        }t          | ¦  «        }|dk    rt          |d         d¬¦  «        | ‚|dk    r|t	          | ¦  «        }|rYt          d„ |D ¦   «         ¦  «        r@|r#|d                              dd	g¦  «        d
         nd	}t          |d         ||¬¦  «        | ‚t          || ¦  «         d	S |dk    r-t          |d         ¦  «        }t          |d         |¬¦  «        | ‚|dk    r.t          |d         d|                     d¦  «        pd¬¦  «        | ‚t          | ¦  «         d	S )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˜  r4   rO   ©rX   é¦  c              3  óH   K  — | ]}|                      d ¦  «        dk    V — ŒdS )ÚtypeÚvalue_errorN)r:   r;   s     r   ú	<genexpr>z0handle_sandbox_creation_error.<locals>.<genexpr>î   s1   è è € ÐKÐK¸a˜1Ÿ5š5 ™=œ=¨MÒ9ÐKÐKÐKÐKÐKÐKr    r   ÚlocNéÿÿÿÿ©r4   ÚfieldÚdetailsé­  ©r4   Ú
quota_typeé÷  r6   ÚUnschedulablerZ   )r?   Ústatus_coderF   r
   rL   Úanyr:   r   r\   rW   r   r   Úhandle_client_http_error)r/   ÚstatusrD   rh   rg   rk   s         r   Úhandle_sandbox_creation_errorrr   Û   s~  € ð Œ^Ô'€FÝ Ñ&Ô&€Dà�‚}€}å" 4¨	¤?À)ÐLÑLÔLÐRWÐWØ	�3Šˆå(¨Ñ/Ô/ˆØð 
	.•sÐKÐKÀ7ÐKÑKÔKÑKÔKð 
	.à9@ÐJ�G˜A”J—N’N 5¨4¨&Ñ1Ô1°"Ô5Ð5ÀdˆEÝ!Ø˜YœØØðñ ô ð ð	õ !  uÑ-Ô-Ð-Ð-Ð-Ø	�3Šˆå'¨¨Y¬Ñ8Ô8ˆ
Ý Ø˜”OØ!ð
ñ 
ô 
ð ð	ð 
�3Šˆå#Ø�ŒOØ#Ø—x’x Ñ-Ô-Ð@°ð
ñ 
ô 
ð ð		õ 	! Ñ'Ô'Ð'Ð'Ð'r    c                ó6  — t          | ¦  «        }|d         }|                     d¦  «        }| j        j        }|dv rt	          |¦  «        | ‚|dk    rt          |¦  «        | ‚|dk    rIt          | ¦  «        }|r#|d                              ddg¦  «        d	         nd}t          |||¬
¦  «        | ‚|dk    r!t          |¦  «        }t          ||¬¦  «        | ‚|dk    r|dk    rt          |¦  «        | ‚|dk    rt          |¦  «        | ‚t          |¦  «        | ‚)zLHandle HTTP errors and raise appropriate exceptions (for client operations).r4   r6   )i‘  é“  é”  r_   r   rd   Nre   rf   ri   rj   éö  ÚConnectionErroriô  )rF   r:   r?   rn   r   r	   rL   r   rW   r   r   r   r   )r/   rD   r4   r6   rq   rh   rg   rk   s           r   rp   rp     sU  € å Ñ&Ô&€DØ�9Œo€GØ—’˜,Ñ'Ô'€JØŒ^Ô'€Fà�ÐÐÝ(¨Ñ1Ô1°uÐ<Ø�‚}€}Ý# GÑ,Ô,°%Ð7ð �‚}€}Ý(¨Ñ/Ô/ˆØ5<ÐF�˜”
—’˜u t fÑ-Ô-¨bÔ1Ð1À$ˆÝØØØð
ñ 
ô 
ð ð		ð �‚}€}Ý'¨Ñ0Ô0ˆ
Ý ØØ!ð
ñ 
ô 
ð ð	ð
 �‚}€}˜Ð'8Ò8Ð8Ý$ WÑ-Ô-°5Ð8Ø�‚}€}Ý˜gÑ&Ô&¨EÐ1Ý
˜WÑ
%Ô
%¨5Ð0r    c                ó  — t          | ¦  «        }|d         }|                     d¦  «        }| j        j        }|dk    rt	          |d|¬¦  «        | ‚|dk    rt	          |d|¬¦  «        | ‚|dk    rt	          |d	|¬¦  «        | ‚|d
k    rt	          |dd¬¦  «        | ‚|dk    r|dk    rt          |¦  «        | ‚|dk    r|dk    s|dk    r|dk    rt          |¦  «        | ‚|dk    s|dk    rt          |d¬¦  «        | ‚t          |¦  «        | ‚)aJ  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)
    r4   r6   Ú
WriteErrorÚwrite)Ú	operationr6   Ú	ReadErrorÚreadÚCommandErrorÚcommandrt   NÚPermissionDeniedrv   rw   i�  ÚNotReadyrl   ÚServiceUnavailableru   ÚFileNotFoundÚfiler^   )	rI   r:   r?   rn   r   r   r   r	   r   )r/   rD   r4   r6   rq   s        r   Úhandle_sandbox_http_errorr…   6  sƒ  € õ ' uÑ-Ô-€DØ�9Œo€GØ—’˜,Ñ'Ô'€JØŒ^Ô'€Fð �\Ò!Ð!Ý#Ø˜w°:ð
ñ 
ô 
àð	ð �[Ò Ð Ý#Ø˜v°*ð
ñ 
ô 
àð	ð �^Ò#Ð#Ý#Ø˜y°Zð
ñ 
ô 
àð	ð
 �‚}€}Ý#Ø˜tÐ0Bð
ñ 
ô 
àð	ð
 �‚}€}˜Ð'8Ò8Ð8Ý$ WÑ-Ô-°5Ð8ð 	�#Šˆ˜*¨
Ò2Ð2Ø�#Šˆ˜*Ð(<Ò<Ð<å" 7Ñ+Ô+°Ð6Ø�‚}€}˜
 nÒ4Ð4Ý# G¸6ÐBÑBÔBÈÐMå
˜WÑ
%Ô
%¨5Ð0r    )NN)r   r   r   r   r   r   )r!   r"   r#   r"   r   r$   )r   r*   r   r+   r   r$   )r/   r0   r   r1   )r/   r0   r   rJ   )r4   r+   r   rM   )rO   )rD   r1   r/   r0   rX   r+   r   r$   )r/   r0   r   r$   ) Ú__doc__Ú
__future__r   Úcollections.abcr   Útypingr   r   ÚhttpxÚlangsmith.sandbox._exceptionsr   r   r	   r
   r   r   r   r   r   r   r   r   r)   r.   rF   rI   rL   rW   r\   rr   rp   r…   © r    r   ú<module>r�      sÜ  ððð ð #Ð "Ð "Ð "Ð "Ð "à #Ð #Ð #Ð #Ð #Ð #Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à €€€ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð( 15Ø48ðð ð ð ð ð.
ð 
ð 
ð 
ð*Rð Rð Rð Rð.;ð ;ð ;ð ;ðD;ð ;ð ;ð ;ð,ð ð ð ð$ð ð ð ð8 #ðð ð ð ð ð(.(ð .(ð .(ð .(ðb"1ð "1ð "1ð "1ðT21ð 21ð 21ð 21ð 21ð 21r    