Ë
    ÂXÕi�8  ã                  óò   — 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mZ 	 	 d	 	 	 	 	 dd„Zdd„Zdd	„Zdd
„Zdd„Zdd„Z	 d	 	 	 	 	 	 	 dd„Zdd„Zdd„Zdd„Zdd„Z dd„Z!y)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ÚOptionalN)ÚQuotaExceededErrorÚResourceAlreadyExistsErrorÚResourceCreationErrorÚResourceNotFoundErrorÚResourceTimeoutErrorÚSandboxAPIErrorÚSandboxAuthenticationErrorÚSandboxClientErrorÚSandboxConnectionErrorÚSandboxNotReadyErrorÚSandboxOperationErrorÚValidationErrorc                óJ   — t        | xs i «      }|r|j                  |«       |S )z6Merge request headers, giving precedence to overrides.)ÚdictÚupdate)Úbase_headersÚoverride_headersÚmergeds      úf/root/rosshost/parking-map/parking-api/venv/lib/python3.12/site-packages/langsmith/sandbox/_helpers.pyÚmerge_headersr   "   s(   € ô
 " ,Ò"4°"Ó5€FÙØ�‰Ð&Ô'Ø€Mó    c                óp   — | €y| dk  rt        |› d| › �«      ‚| dk7  r| dz  dk7  rt        |› d| › �«      ‚yy)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 )Ú
ValueError)ÚvalueÚnames     r   Úvalidate_ttlr!   2   sY   € ð €}ØØˆq‚yÜ˜D˜6Ð!4°U°GÐ<Ó=Ð=Ø�‚z�e˜b‘j A’oÜ˜D˜6Ð!HÈÈÐPÓQÐQð &€zr   c           	     ó  — 	 | j                   j                  «       }|j                  d«      }t        |t        «      r-|j                  d«      |j                  dt        | «      «      dœS t        |t        «      r^|r\|D �cg c].  }t        |t        «      sŒ|j                  dt        |«      «      ‘Œ0 }}d|rdj                  |«      dœS t        | «      dœS d|xs t        | «      dœS c c}w # 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
    ÚdetailÚerrorÚmessage©Ú
error_typer%   ÚmsgNz; )	ÚresponseÚjsonÚgetÚ
isinstancer   ÚstrÚlistÚjoinÚ	Exception)r$   Údatar#   ÚdÚmessagess        r   Úparse_error_responser4   I   sü   € ð;Ø�~‰~×"Ñ"Ó$ˆØ—‘˜(Ó#ˆô �fœdÔ#à$Ÿj™j¨Ó1Ø!Ÿ:™: i´°U³Ó<ñð ô �fœdÔ#©Ø6<ÖT°Ä
È1ÌdÕ@S˜Ÿ™˜e¤S¨£VÕ,ÐTˆHÐTà"Ù2:˜4Ÿ9™9 XÓ.ñð ä@CÀEÃ
ñð ð #¨vÒ/C¼¸U»ÑDÐDùò Uøô ò ;Ø"¬s°5«zÑ:Ò:ð;ús<   ‚A'C/ Á*C/ Â C*ÂC*Â3C/ ÃC/ ÃC/ Ã*C/ Ã/D	ÄD	c                ó2  — 	 | j                   j                  «       }|j                  d«      }t        |t        «      r-|j                  d«      |j                  dt        | «      «      dœS d|xs 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
    r#   r$   r%   r&   N)r)   r*   r+   r,   r   r-   r0   ©r$   r1   r#   s      r   Úparse_error_response_simpler7   k   s�   € ð;Ø�~‰~×"Ñ"Ó$ˆØ—‘˜(Ó#ˆä�fœdÔ#à$Ÿj™j¨Ó1Ø!Ÿ:™: i´°U³Ó<ñð ð
 #¨vÒ/C¼¸U»ÑDÐDøÜò ;Ø"¬s°5«zÑ:Ò:ð;ús   ‚A'A< Á*A< Á<BÂBc                ó¦   — 	 | j                   j                  «       }|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")
    r#   )r)   r*   r+   r,   r.   r0   r6   s      r   Úparse_validation_errorr9   �   sQ   € ðØ�~‰~×"Ñ"Ó$ˆØ—‘˜( BÓ'ˆÜ�fœdÔ#ØˆMØˆ	øÜò ØŠ	ðús   ‚=A Á A ÁAÁAc                óv   — | j                  «       }d|v r	d|v sd|v ryd|v ryd|v ryd|v r	d|v sd|v ryd	|v ry	y
)zŠExtract quota type from error message.

    Returns one of: "sandbox_count", "cpu", "memory", "volume_count",
    "storage", or None.
    ÚsandboxÚcountÚlimitÚsandbox_countÚcpuÚmemoryÚvolumeÚvolume_countÚstorageN)Úlower)r%   Úmessage_lowers     r   Úextract_quota_typerF   “   sg   € ð —M‘M“O€Mà�MÑ!Ø�=Ñ  G¨}Ñ$<àØ	�-Ñ	ØØ	�]Ñ	"Øà	�]Ñ	"Ø�=Ñ  G¨}Ñ$<àØ	�mÑ	#ØØr   c                ó€   — t        | j                  d|j                  «       › d�«      || j                  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
    r%   z creation failedr'   ©Úresource_typer'   )r	   r+   Útitle)r1   r$   rI   s      r   Úraise_creation_errorrK   ²   sE   € ô  Ø�‰�˜}×2Ñ2Ó4Ð5Ð5EÐFÓGØ#Ø—8‘8˜LÓ)ôð ð	r   c                óä  — | j                   j                  }t        | «      }|dk(  rt        |d   d¬«      | ‚|dk(  r[t	        | «      }|rAt        d„ |D «       «      r/|r|d   j                  dd	g«      d
   nd	}t        |d   ||¬«      | ‚t        || «       y	|dk(  rt        |d   «      }t        |d   |¬«      | ‚|dk(  r%t        |d   d|j                  d«      xs d¬«      | ‚t        | «       y	)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%   r;   ©rI   é¦  c              3  óD   K  — | ]  }|j                  d «      dk(  –— Œ y­w)ÚtypeÚvalue_errorN)r+   )Ú.0r2   s     r   ú	<genexpr>z0handle_sandbox_creation_error.<locals>.<genexpr>Ù   s   è ø€ ÒK¸a˜1Ÿ5™5 ›=¨MÕ9ÑKùs   ‚ r   ÚlocNéÿÿÿÿ©r%   ÚfieldÚdetailsé­  ©r%   Ú
quota_typeé÷  r'   ÚUnschedulablerH   )r)   Ústatus_coder4   r   r9   Úanyr+   r   rK   rF   r   r	   Úhandle_client_http_error)r$   Ústatusr1   rX   rW   r[   s         r   Úhandle_sandbox_creation_errorrb   Æ   s  € ð �^‰^×'Ñ'€FÜ Ó&€Dà�‚}ä" 4¨	¡?À)ÔLÐRWÐWØ	�3Šä(¨Ó/ˆÙ”sÑKÀ7ÔKÔKá9@�G˜A‘J—N‘N 5¨4¨&Ó1°"Ò5ÀdˆEÜ!Ø˜Y™ØØôð ð	ô !  uÕ-Ø	�3Šä'¨¨Y©Ó8ˆ
Ü Ø˜‘OØ!ô
ð ð	ð 
�3Šä#Ø�‰OØ#Ø—x‘x Ó-Ò@°ô
ð ð		ô 	! Õ'r   c                ó¶   — | j                   j                  }t        | «      }|dk(  rt        |d   dd¬«      | ‚|dk(  rt	        |d   d¬«      | ‚t        | «       y)	a   Handle HTTP errors specific to volume creation.

    Maps API error responses to specific exception types:
    - 503: ResourceCreationError (provisioning failed)
    - 504: ResourceTimeoutError (volume didn't become ready in time)
    - Other: Falls through to generic error handling
    r\   r%   rA   ÚVolumeProvisioningrH   éø  rM   N)r)   r^   r4   r	   r   r`   )r$   ra   r1   s      r   Úhandle_volume_creation_errorrf   ÷   sj   € ð �^‰^×'Ñ'€FÜ Ó&€Dà�‚}ä#Ø�‰OØ"Ø+ô
ð ð		ð
 
�3Šä" 4¨	¡?À(ÔKÐQVÐVô 	! Õ'r   c                ó˜  — | j                   j                  }t        | «      }|j                  d«      }|dk(  r8|dk(  rt	        |d   d¬«      | ‚|dk(  rt        |d   |¬«      | ‚t        | «       y|d	k(  rt        |d   d
¬«      | ‚|dk(  rt        |d   «      }t        |d   |¬«      | ‚|dk(  rt        |d   d
¬«      | ‚t        | «       y)a“  Handle HTTP errors specific to pool creation/update.

    Maps API error responses to specific exception types:
    - 400: ResourceNotFoundError or ValidationError (template has volumes)
    - 409: ResourceAlreadyExistsError
    - 429: QuotaExceededError (org limits exceeded)
    - 504: ResourceTimeoutError (timeout waiting for ready replicas)
    - Other: Falls through to generic error handling
    r'   é�  ÚTemplateNotFoundr%   ÚtemplaterM   r   )r'   i™  ÚpoolrY   rZ   re   N)r)   r^   r4   r+   r
   r   r`   r   rF   r   r   )r$   ra   r1   r'   r[   s        r   Úhandle_pool_errorrl     sõ   € ð �^‰^×'Ñ'€FÜ Ó&€DØ—‘˜,Ó'€Jà�‚}àÐ+Ò+Ü'Ø�Y‘¨zôàðð Ð,Ò,ä! $ y¡/¸jÔIÈuÐTô % UÕ+Ø	�3Šä(Ø�‰O¨6ô
àð	ð 
�3Šä'¨¨Y©Ó8ˆ
Ü Ø˜‘OØ!ô
ð ð	ð 
�3Šä" 4¨	¡?À&ÔIÈuÐTô 	! Õ'r   c                óÌ  — t        | «      }|d   }|j                  d«      }| j                  j                  }|dv rt	        |«      | ‚|dk(  rt        |«      | ‚|dk(  r7t        | «      }|r|d   j                  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).r%   r'   )i‘  é“  é”  rN   r   rT   NrU   rV   rY   rZ   éö  ÚConnectionErroriô  )r4   r+   r)   r^   r   r
   r9   r   rF   r   r   r   r   )r$   r1   r%   r'   ra   rX   rW   r[   s           r   r`   r`   ?  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                ó°  — t        | «      }|d   }|j                  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(  rt        |«      | ‚|dk(  s|dk(  rt        |d¬«      | ‚t        |«      | ‚)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
    - 403 -> SandboxOperationError (permission denied)
    r%   r'   Ú
WriteErrorÚwrite)Ú	operationr'   Ú	ReadErrorÚreadÚCommandErrorÚcommandrn   NÚPermissionDeniedrp   rq   rh   ÚNotReadyro   ÚFileNotFoundÚfilerM   )	r7   r+   r)   r^   r   r   r   r
   r   )r$   r1   r%   r'   ra   s        r   Úhandle_sandbox_http_errorr~   i  s  € ô ' uÓ-€DØ�9‰o€GØ—‘˜,Ó'€JØ�^‰^×'Ñ'€Fð �\Ò!Ü#Ø˜w°:ô
àð	ð �[Ò Ü#Ø˜v°*ô
àð	ð �^Ò#Ü#Ø˜y°Zô
àð	ð
 �‚}Ü#Ø˜tÐ0Bô
àð	ð
 �‚}˜Ð'8Ò8Ü$ WÓ-°5Ð8ð �‚}˜ zÒ1Ü" 7Ó+°Ð6Ø�‚}˜
 nÒ4Ü# G¸6ÔBÈÐMä
˜WÓ
%¨5Ð0r   )NN)r   úOptional[Mapping[str, str]]r   r   Úreturnzdict[str, str])r   zOptional[int]r    r-   r€   ÚNone)r$   úhttpx.HTTPStatusErrorr€   údict[str, Any])r$   r‚   r€   z
list[dict])r%   r-   r€   zOptional[str])r;   )r1   rƒ   r$   r‚   rI   r-   r€   r�   )r$   r‚   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!   r4   r7   r9   rF   rK   rb   rf   rl   r`   r~   © r   r   ú<module>r‹      s¹   ðñõ #å #ß  ã ÷÷ ÷ ó ð* 15Ø48ðØ-ðà1ðð óó Ró.;óD;ó,ó$ðD #ðØ
ðà ðð ðð 
ó	ó(.(ób(ó4+(ó\"1ôT/1r   