Ë
    b‡„i!#  ã                  óž   — d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
 ddlZddlmZmZmZ ddlmZ ddlmZ erdd	lmZ e G d
„ d«      «       Zy)z?Sandbox class for interacting with a specific sandbox instance.é    )Úannotations)Ú	dataclassÚfield)ÚTYPE_CHECKINGÚAnyÚOptionalÚUnionN)ÚDataplaneNotConfiguredErrorÚResourceNotFoundErrorÚSandboxConnectionError)Úhandle_sandbox_http_error)ÚExecutionResult)ÚSandboxClientc                  ó:  — e Zd ZU dZded<   ded<   dZded<   dZded<   dZded	<   dZded
<    e	dd¬«      Z
ded<    e	dd¬«      Zded<   e	 d	 	 	 	 	 	 	 dd„«       Zdd„Z	 	 	 	 	 	 	 	 d d„Zd!d„Zdddddœ	 	 	 	 	 	 	 	 	 	 	 d"d„Zddœ	 	 	 	 	 	 	 d#d„Zddœd$d„Zy)%ÚSandboxao  Represents an active sandbox for running commands and file operations.

    This class is typically obtained from SandboxClient.sandbox() and supports
    the context manager protocol for automatic cleanup.

    Attributes:
        name: Display name (can be updated).
        template_name: Name of the template used to create this sandbox.
        dataplane_url: URL for data plane operations (file I/O, command execution).
        id: Unique identifier (UUID). Remains constant even if name changes.
            May be None for resources created before ID support was added.
        created_at: Timestamp when the sandbox was created.
        updated_at: Timestamp when the sandbox was last updated.

    Example:
        with client.sandbox(template_name="python-sandbox") as sandbox:
            result = sandbox.run("python --version")
            print(result.stdout)
    ÚstrÚnameÚtemplate_nameNúOptional[str]Údataplane_urlÚidÚ
created_atÚ
updated_atF)ÚreprÚdefaultr   Ú_clientTÚboolÚ_auto_deletec           
     óÚ   —  | |j                  dd«      |j                  dd«      |j                  d«      |j                  d«      |j                  d«      |j                  d«      ||¬«      S )	a4  Create a Sandbox from API response dict.

        Args:
            data: API response dictionary containing sandbox data.
            client: Parent SandboxClient for operations.
            auto_delete: Whether to delete the sandbox on context exit.

        Returns:
            Sandbox instance.
        r   Ú r   r   r   r   r   )r   r   r   r   r   r   r   r   )Úget)ÚclsÚdataÚclientÚauto_deletes       úd/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/langsmith/sandbox/_sandbox.pyÚ	from_dictzSandbox.from_dict8   sb   € ñ" Ø—‘˜& "Ó%ØŸ(™( ?°BÓ7ØŸ(™( ?Ó3Øx‰x˜‹~Ø—x‘x Ó-Ø—x‘x Ó-ØØ$ô	
ð 		
ó    c                ó   — | S )zEnter context manager.© ©Úselfs    r&   Ú	__enter__zSandbox.__enter__T   s   € àˆr(   c                óˆ   — | j                   r'	 | j                  j                  | j                  «       yy# t        $ r Y yw xY w)z6Exit context manager, optionally deleting the sandbox.N)r   r   Údelete_sandboxr   Ú	Exception)r,   Úexc_typeÚexc_valÚexc_tbs       r&   Ú__exit__zSandbox.__exit__X   sA   € ð ×ÒðØ—‘×+Ñ+¨D¯I©IÕ6ð øô ò áðús   Ž%5 µ	AÁ Ac                ód   — | j                   st        d| j                  › d«      ‚| j                   S )z¾Validate and return the dataplane URL.

        Returns:
            The dataplane URL.

        Raises:
            DataplaneNotConfiguredError: If dataplane_url is not configured.
        z	Sandbox 'zW' does not have a dataplane_url configured. Runtime operations require a dataplane URL.)r   r
   r   r+   s    r&   Ú_require_dataplane_urlzSandbox._require_dataplane_urlf   s=   € ð ×!Ò!Ü-Ø˜DŸI™I˜;ð '>ð >óð ð ×!Ñ!Ð!r(   é<   z	/bin/bash)ÚtimeoutÚenvÚcwdÚshellc               ó  — | j                  «       }|› d}|||dœ}|||d<   |||d<   	 | j                  j                  j                  |||dz   ¬«      }	|	j	                  «        |	j                  «       }
t        |
j                  dd	«      |
j                  d
d	«      |
j                  dd«      ¬«      S # t        j                  $ r!}t        d| j                  › d|› «      |‚d}~wt        j                  $ r}t        |«       ‚ d}~ww xY w)a!  Execute a command in the sandbox.

        Args:
            command: Shell command to execute.
            timeout: Command timeout in seconds.
            env: Environment variables to set for the command.
            cwd: Working directory for command execution. If None, uses sandbox default.
            shell: Shell to use for command execution. Defaults to "/bin/bash".

        Returns:
            ExecutionResult with stdout, stderr, and exit_code.

        Raises:
            DataplaneNotConfiguredError: If dataplane_url is not configured.
            SandboxOperationError: If command execution fails.
            SandboxConnectionError: If connection to sandbox fails.
            SandboxNotReadyError: If sandbox is not ready.
            SandboxClientError: For other errors.
        z/execute)Úcommandr8   r;   Nr9   r:   é
   )Újsonr8   Ústdoutr    ÚstderrÚ	exit_codeéÿÿÿÿ)r@   rA   rB   úFailed to connect to sandbox 'ú': )r6   r   Ú_httpÚpostÚraise_for_statusr?   r   r!   ÚhttpxÚConnectErrorr   r   ÚHTTPStatusErrorr   )r,   r=   r8   r9   r:   r;   r   ÚurlÚpayloadÚresponser#   Úes               r&   ÚrunzSandbox.runv   s  € ð8 ×3Ñ3Ó5ˆØ˜xÐ(ˆàØØñ#
ˆð
 ˆ?Ø ˆGE‰NØˆ?Ø ˆGE‰Nð	Ø—|‘|×)Ñ)×.Ñ.¨s¸È'ÐTVÉ,Ð.ÓWˆHØ×%Ñ%Ô'Ø—=‘=“?ˆDÜ"Ø—x‘x ¨"Ó-Ø—x‘x ¨"Ó-ØŸ(™( ;°Ó3ôð øô
 ×!Ñ!ò 	Ü(Ø0°·±°¸3¸q¸cÐBóàðûô ×$Ñ$ò 	Ü% aÔ(àûð	ús$   «BB4 Â4D
ÃC#Ã#D
Ã9DÄD
)r8   c               ó®  — | j                  «       }|› d}t        |t        «      r|j                  d«      }dd|fi}	 | j                  j
                  j                  |d|i||¬«      }|j                  «        y# t        j                  $ r!}t        d| j                  › d|› «      |‚d}~wt        j                  $ r}t        |«       Y d}~yd}~ww xY w)	a  Write content to a file in the sandbox.

        Args:
            path: Target file path in the sandbox.
            content: File content (str or bytes).
            timeout: Request timeout in seconds.

        Raises:
            DataplaneNotConfiguredError: If dataplane_url is not configured.
            SandboxOperationError: If file write fails.
            SandboxConnectionError: If connection to sandbox fails.
            SandboxNotReadyError: If sandbox is not ready.
            SandboxClientError: For other errors.
        z/uploadzutf-8ÚfileÚpath)ÚparamsÚfilesr8   rD   rE   N)r6   Ú
isinstancer   Úencoder   rF   rG   rH   rI   rJ   r   r   rK   r   )	r,   rS   Úcontentr8   r   rL   rU   rN   rO   s	            r&   ÚwritezSandbox.write°   sÜ   € ð* ×3Ñ3Ó5ˆØ˜wÐ'ˆô gœsÔ#Ø—n‘n WÓ-ˆGà˜& 'Ð*Ð+ˆð
	)Ø—|‘|×)Ñ)×.Ñ.Ø˜V T˜N°%Àð /ó ˆHð ×%Ñ%Õ'øÜ×!Ñ!ò 	Ü(Ø0°·±°¸3¸q¸cÐBóàðûô ×$Ñ$ò 	)Ü% a×(Ñ(ûð	)ús#   ¾;A: Á:CÂB)Â)CÂ?CÃCc               óÜ  — | j                  «       }|› d}	 | j                  j                  j                  |d|i|¬«      }|j	                  «        |j
                  S # t        j                  $ r!}t        d| j                  › d|› «      |‚d}~wt        j                  $ rI}|j                  j                  dk(  rt        d|› d	| j                  › d
d¬«      |‚t        |«       ‚ d}~ww xY w)aº  Read a file from the sandbox.

        Args:
            path: File path to read. Supports both absolute paths (e.g., /tmp/file.txt)
                  and relative paths (resolved from /home/user/).
            timeout: Request timeout in seconds.

        Returns:
            File contents as bytes.

        Raises:
            DataplaneNotConfiguredError: If dataplane_url is not configured.
            ResourceNotFoundError: If the file doesn't exist.
            SandboxOperationError: If file read fails.
            SandboxConnectionError: If connection to sandbox fails.
            SandboxNotReadyError: If sandbox is not ready.
            SandboxClientError: For other errors.
        z	/downloadrS   )rT   r8   rD   rE   Ni”  zFile 'z' not found in sandbox 'ú'rR   )Úresource_type)r6   r   rF   r!   rH   rX   rI   rJ   r   r   rK   rN   Ústatus_coder   r   )r,   rS   r8   r   rL   rN   rO   s          r&   ÚreadzSandbox.readÚ   sý   € ð& ×3Ñ3Ó5ˆØ˜yÐ)ˆð	Ø—|‘|×)Ñ)×-Ñ-Ø˜V T˜N°Gð .ó ˆHð ×%Ñ%Ô'Ø×#Ñ#Ð#øÜ×!Ñ!ò 	Ü(Ø0°·±°¸3¸q¸cÐBóàðûô ×$Ñ$ò 	Øz‰z×%Ñ%¨Ò,Ü+Ø˜T˜FÐ":¸4¿9¹9¸+ÀQÐGØ"(ôð ðô & aÔ(àûð	ús%   —AA ÁC+Á0BÂC+Â"AC&Ã&C+)T)r#   zdict[str, Any]r$   r   r%   r   Úreturnr   )r_   r   )r1   zOptional[type]r2   zOptional[BaseException]r3   zOptional[Any]r_   ÚNone)r_   r   )r=   r   r8   Úintr9   zOptional[dict[str, str]]r:   r   r;   r   r_   r   )rS   r   rX   zUnion[str, bytes]r8   ra   r_   r`   )rS   r   r8   ra   r_   Úbytes)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   r   r   r   r   r   Úclassmethodr'   r-   r4   r6   rP   rY   r^   r*   r(   r&   r   r      s[  … ñð* ƒIØÓØ#'€M=Ó'Ø€BˆÓØ $€JÓ$Ø $€JÓ$ñ #¨°tÔ<€Gˆ]Ó<Ù E°4Ô8€L$Ó8àð
 !ð	
àð
ð ð
ð ð	
ð
 
ò
ó ð
ó6ðà ðð )ðð ð	ð
 
óó"ð( Ø(,Ø!Ø ñ8àð8ð ð	8ð
 &ð8ð ð8ð ð8ð 
ó8ð~ ñ()àð()ð #ð()ð
 ð()ð 
ó()ðT 13ö (r(   r   )rf   Ú
__future__r   Údataclassesr   r   Útypingr   r   r   r	   rI   Úlangsmith.sandbox._exceptionsr
   r   r   Úlangsmith.sandbox._helpersr   Úlangsmith.sandbox._modelsr   Úlangsmith.sandbox._clientr   r   r*   r(   r&   ú<module>rp      sL   ðÙ Eå "ç (ß 6Ó 6ã ÷ñ õ
 AÝ 5áÝ7ð ÷kð kó ñkr(   