
    a
i8|                        d Z ddlmZ ddlmZmZ ddlZddlmZ	 ddl
mZmZmZmZmZmZmZ ddlmZmZmZmZmZ ddlmZmZmZmZmZ dd	lmZ dd
l m!Z! ddZ"ddZ# G d d          Z$dS )zEMain SandboxClient class for interacting with the sandbox server API.    )annotations)AnyOptionalN)utils)ResourceCreationErrorResourceInUseErrorResourceNameConflictErrorResourceNotFoundErrorResourceTimeoutErrorSandboxAPIErrorValidationError)handle_client_http_errorhandle_pool_errorhandle_sandbox_creation_errorhandle_volume_creation_errorparse_error_response)PoolResourceStatusSandboxTemplateVolumeVolumeMountSpec)Sandbox)RetryTransportreturnstrc                 ^    t          j        dd          } |                     d           dS )zGet the default sandbox API endpoint from environment.

    Derives the endpoint from LANGSMITH_ENDPOINT (or LANGCHAIN_ENDPOINT).
    ENDPOINTzhttps://api.smith.langchain.com)default/z/v2/sandboxes)ls_utilsget_env_varrstrip)bases    C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langsmith/sandbox/_client.py_get_default_api_endpointr%   %   s5    
 
4UVVVDkk#----    Optional[str]c                 *    t          j        d          S )z)Get the default API key from environment.API_KEY)r    r!    r&   r$   _get_default_api_keyr+   .   s    	***r&   c                  @   e Zd ZdZddddddXdZdYdZdYdZdZdZd[dZddd\d Z	d]d!Z
d^d#Zd_d$Zddd%d`d'Zd(d)ddd*dad2Zdbd3Zdcd5Zddd6Zd_d7Zd8dded<Zdfd=Zdgd?Zddd@dhdBZd_dCZdd8dDdidFZdd8dGdHdjdKZdkdLZdldNZdmdOZd_dPZdndRZdSdTdUdodWZdS )pSandboxClienta  Client for interacting with the Sandbox Server API.

    This client provides a simple interface for managing sandboxes and templates.

    Example:
        # Uses LANGSMITH_ENDPOINT and LANGSMITH_API_KEY from environment
        client = SandboxClient()

        # Or with explicit configuration
        client = SandboxClient(
            api_endpoint="https://api.smith.langchain.com/v2/sandboxes",
            api_key="your-api-key",
        )

        # Create a sandbox and run commands
        with client.sandbox(template_name="python-sandbox") as sandbox:
            result = sandbox.run("python --version")
            print(result.stdout)
    Ng      $@   )api_endpointtimeoutapi_keymax_retriesr/   r'   r0   floatr1   r2   intc                   |pt                                          d          | _        |pt                      }|| _        i }|r||d<   t          |          }t          j        |||          | _        dS )ap  Initialize the SandboxClient.

        Args:
            api_endpoint: Full URL of the sandbox API endpoint. If not provided,
                          derived from LANGSMITH_ENDPOINT environment variable.
            timeout: Default HTTP timeout in seconds.
            api_key: API key for authentication. If not provided, uses
                     LANGSMITH_API_KEY environment variable.
            max_retries: Maximum number of retries for transient errors (502, 503,
                         504), rate limits (429), and connection failures. Set to 0
                         to disable retries. Default: 3.
        r   z	X-Api-Key)r2   )	transportr0   headersN)	r%   r"   	_base_urlr+   _api_keyr   httpxClient_http)selfr/   r0   r1   r2   resolved_api_keyr7   r6   s           r$   __init__zSandboxClient.__init__H   s    ( 'E*C*E*EMMcRR"<&:&<&<("$ 	4#3GK "{;;;	\IwPWXXX


r&   r   Nonec                8    | j                                          dS )zClose the HTTP client.N)r<   closer=   s    r$   rB   zSandboxClient.closee   s    
r&   c                x    	 | j         j        s| j                                          dS dS # t          $ r Y dS w xY w)z,Close the HTTP client on garbage collection.N)r<   	is_closedrB   	ExceptionrC   s    r$   __del__zSandboxClient.__del__i   sZ    	:' #
  """""# # 	 	 	DD	s   %+ 
99c                    | S )zEnter context manager.r*   rC   s    r$   	__enter__zSandboxClient.__enter__q   s    r&   exc_typeOptional[type]exc_valOptional[BaseException]exc_tbOptional[Any]c                .    |                                   dS )zExit context manager.N)rB   )r=   rJ   rL   rN   s       r$   __exit__zSandboxClient.__exit__u   s     	

r&   <   )r0   namer   sizer   c               (   | j          d}||d|d}	 | j                            |||dz             }|                                 t	          j        |                                          S # t          j        $ r}t          |            d}~ww xY w)a6  Create a new persistent volume.

        Creates a persistent storage volume that can be referenced in templates.

        Args:
            name: Volume name.
            size: Storage size (e.g., "1Gi", "10Gi").
            timeout: Timeout in seconds when waiting for ready (min: 5, max: 300).

        Returns:
            Created Volume.

        Raises:
            VolumeProvisioningError: If volume provisioning fails.
            ResourceTimeoutError: If volume doesn't become ready within timeout.
            SandboxClientError: For other errors.
        /volumesT)rS   rT   wait_for_readyr0      jsonr0   N)
r8   r<   postraise_for_statusr   	from_dictrZ   r:   HTTPStatusErrorr   )r=   rS   rT   r0   urlpayloadresponsees           r$   create_volumezSandboxClient.create_volume   s    0 ))) "	
 
	zs'B,OOH%%'''#HMMOO444$ 	 	 	(+++	s   AA- -B<BBc                ^   | j          d| }	 | j                            |          }|                                 t	          j        |                                          S # t          j        $ r;}|j	        j
        dk    rt          d| dd          |t          |            d}~ww xY w)zGet a volume by name.

        Args:
            name: Volume name.

        Returns:
            Volume.

        Raises:
            ResourceNotFoundError: If volume not found.
            SandboxClientError: For other errors.
        	/volumes/  Volume '' not foundvolumeresource_typeN)r8   r<   getr\   r   r]   rZ   r:   r^   ra   status_coder
   r   r=   rS   r_   ra   rb   s        r$   
get_volumezSandboxClient.get_volume   s     00$00
	z~~c**H%%'''#HMMOO444$ 	 	 	z%,,+0t000   %Q'''	   AA" "B,16B''B,list[Volume]c                r   | j          d}	 | j                            |          }|                                 |                                }d |                    dg           D             S # t
          j        $ r9}|j        j        dk    rt          d| d          |t          |            d}~ww xY w)zIList all volumes.

        Returns:
            List of Volumes.
        rV   c                6    g | ]}t          j        |          S r*   )r   r]   ).0vs     r$   
<listcomp>z.SandboxClient.list_volumes.<locals>.<listcomp>   s#    IIIAF$Q''IIIr&   volumesrf   API endpoint not found: %. Check that api_endpoint is correct.Nr8   r<   rl   r\   rZ   r:   r^   ra   rm   r   r   r=   r_   ra   datarb   s        r$   list_volumeszSandboxClient.list_volumes   s     )))	z~~c**H%%'''==??DII)R1H1HIIII$ 	 	 	z%,,%;s ; ; ;   %Q'''	   A!A. .B6=4B11B6c                   | j          d| }	 | j                            |          }|                                 d	S # t          j        $ rw}|j        j        dk    rt          d| dd          ||j        j        dk    r't          |          }t          |d         d          |t          |           Y d	}~d	S d	}~ww xY w)
a  Delete a volume.

        Args:
            name: Volume name.

        Raises:
            ResourceNotFoundError: If volume not found.
            ResourceInUseError: If volume is referenced by templates.
            SandboxClientError: For other errors.
        re   rf   rg   rh   ri   rj     messageNr8   r<   deleter\   r:   r^   ra   rm   r
   r   r   r   r=   rS   r_   ra   rb   r|   s         r$   delete_volumezSandboxClient.delete_volume   s     00$00	(z((--H%%'''''$ 	( 	( 	(z%,,+0t000   z%,,+A..(iQQQWXX$Q'''''''''	(   .> CA,B??C)new_namerT   r   c                  | j          d| }i }|||d<   |||d<   |s|                     |          S 	 | j                            ||          }|                                 t          j        |                                          S # t          j	        $ r}|j
        j        dk    rt          d| dd	
          ||j
        j        dk    r't          |          }t          |d         d          ||j
        j        dk    r't          |          }t          |d         d	
          |t!          |            d}~ww xY w)a  Update a volume's name and/or size.

        You can update the display name, size, or both in a single request.
        Only storage size increases are allowed (storage backend limitation).

        Args:
            name: Current volume name.
            new_name: New display name (optional).
            size: New storage size (must be >= current size). Optional.

        Returns:
            Updated Volume.

        Raises:
            ResourceNotFoundError: If volume not found.
            VolumeResizeError: If storage decrease attempted.
            ResourceNameConflictError: If new_name is already in use.
            SandboxQuotaExceededError: If storage quota would be exceeded.
            SandboxClientError: For other errors.
        re   NrS   rT   rZ   rf   rg   rh   ri   rj   i  r   VolumeResize)
error_typer   )r8   ro   r<   patchr\   r   r]   rZ   r:   r^   ra   rm   r
   r   r   r	   r   )	r=   rS   r   rT   r_   r`   ra   rb   r|   s	            r$   update_volumezSandboxClient.update_volume   s   6 00$00"$&GFO"GFO 	)??4(((	z''''::H%%'''#HMMOO444$ 	 	 	z%,,+0t000   z%,,+A..%d9o.QQQWXXz%,,+A../O8   %Q'''	s   AB EB$D>>E500m512Mi)cpumemorystoragevolume_mountsimager   r   r   r   Optional[list[VolumeMountSpec]]r   c               `   | j          d}||||dd}|r||d         d<   |rd |D             |d<   	 | j                            ||          }	|	                                 t	          j        |	                                          S # t          j        $ r}
t          |
            d	}
~
ww xY w)
a  Create a new SandboxTemplate.

        Only the container image, resource limits, and volume mounts can be
        configured. All other container details are handled by the server.

        Args:
            name: Template name.
            image: Container image (e.g., "python:3.12-slim").
            cpu: CPU limit (e.g., "500m", "1", "2"). Default: "500m".
            memory: Memory limit (e.g., "256Mi", "1Gi"). Default: "512Mi".
            storage: Ephemeral storage limit (e.g., "1Gi"). Optional.
            volume_mounts: List of volumes to mount in the sandbox. Optional.

        Returns:
            Created SandboxTemplate.

        Raises:
            SandboxClientError: If creation fails.
        
/templates)r   r   )rS   r   	resourcesr   r   c                ,    g | ]}|j         |j        d S )volume_name
mount_pathr   )rt   vms     r$   rv   z1SandboxClient.create_template.<locals>.<listcomp>^  s4     ( ( ( !#bmLL( ( (r&   r   r   N)
r8   r<   r[   r\   r   r]   rZ   r:   r^   r   )r=   rS   r   r   r   r   r   r_   r`   ra   rb   s              r$   create_templatezSandboxClient.create_template4  s    : +++   #
 #
  	6.5GK + 	( ('( ( (GO$
	zs99H%%'''",X]]__===$ 	 	 	$Q'''	s   AB	 	B-B((B-c                ^   | j          d| }	 | j                            |          }|                                 t	          j        |                                          S # t          j        $ r;}|j	        j
        dk    rt          d| dd          |t          |            d}~ww xY w)a  Get a SandboxTemplate by name.

        Args:
            name: Template name.

        Returns:
            SandboxTemplate.

        Raises:
            ResourceNotFoundError: If template not found.
            SandboxClientError: For other errors.
        /templates/rf   
Template 'rh   templaterj   N)r8   r<   rl   r\   r   r]   rZ   r:   r^   ra   rm   r
   r   rn   s        r$   get_templatezSandboxClient.get_templatek  s     22D22
	z~~c**H%%'''",X]]__===$ 	 	 	z%,,+2222*   %Q'''	rp   list[SandboxTemplate]c                r   | j          d}	 | j                            |          }|                                 |                                }d |                    dg           D             S # t
          j        $ r9}|j        j        dk    rt          d| d          |t          |            d}~ww xY w)z[List all SandboxTemplates.

        Returns:
            List of SandboxTemplates.
        r   c                6    g | ]}t          j        |          S r*   )r   r]   )rt   ts     r$   rv   z0SandboxClient.list_templates.<locals>.<listcomp>  s#    TTTQO-a00TTTr&   	templatesrf   rx   ry   Nrz   r{   s        r$   list_templateszSandboxClient.list_templates  s     +++	z~~c**H%%'''==??DTT$((;PR:S:STTTT$ 	 	 	z%,,%;s ; ; ;   %Q'''	r~   c                  | j          d| }d|i}	 | j                            ||          }|                                 t	          j        |                                          S # t          j        $ rr}|j	        j
        dk    rt          d| dd          ||j	        j
        d	k    r't          |          }t          |d
         d          |t          |            d}~ww xY w)a  Update a template's display name.

        Args:
            name: Current template name.
            new_name: New display name.

        Returns:
            Updated SandboxTemplate.

        Raises:
            ResourceNotFoundError: If template not found.
            ResourceNameConflictError: If new_name is already in use.
            SandboxClientError: For other errors.
        r   rS   r   rf   r   rh   r   rj   r   r   N)r8   r<   r   r\   r   r]   rZ   r:   r^   ra   rm   r
   r   r	   r   )r=   rS   r   r_   r`   ra   rb   r|   s           r$   update_templatezSandboxClient.update_template  s    22D228$	z''''::H%%'''",X]]__===$ 	 	 	z%,,+2222*   z%,,+A../O:   %Q'''	s   AA( (C)7A-C$$C)c                   | j          d| }	 | j                            |          }|                                 d	S # t          j        $ rw}|j        j        dk    rt          d| dd          ||j        j        dk    r't          |          }t          |d         d          |t          |           Y d	}~d	S d	}~ww xY w)
a   Delete a SandboxTemplate.

        Args:
            name: Template name.

        Raises:
            ResourceNotFoundError: If template not found.
            ResourceInUseError: If template is referenced by sandboxes or pools.
            SandboxClientError: For other errors.
        r   rf   r   rh   r   rj   r   r   Nr   r   s         r$   delete_templatezSandboxClient.delete_template  s    22D22	(z((--H%%'''''$ 
	( 
	( 
	(z%,,+2222*   z%,,+A..(O:   %Q'''''''''
	(r   rX   template_namereplicasr   c               .   | j          d}|||d|d}	 |dz   }| j                            |||          }|                                 t	          j        |                                          S # t          j        $ r}	t          |	            d}	~	ww xY w)a  Create a new Sandbox Pool.

        Pools pre-provision sandboxes from a template for faster startup.

        Args:
            name: Pool name (lowercase letters, numbers, hyphens; max 63 chars).
            template_name: Name of the SandboxTemplate to use (no volume mounts).
            replicas: Number of sandboxes to pre-provision (1-100).
            timeout: Timeout in seconds when waiting for ready (10-600).

        Returns:
            Created Pool.

        Raises:
            ResourceNotFoundError: If template not found.
            ValidationError: If template has volumes attached.
            ResourceAlreadyExistsError: If pool with this name already exists.
            ResourceTimeoutError: If pool doesn't reach ready state within timeout.
            SandboxQuotaExceededError: If organization quota is exceeded.
            SandboxClientError: For other errors.
        /poolsT)rS   r   r   rW   r0   rX   rY   N)
r8   r<   r[   r\   r   r]   rZ   r:   r^   r   )
r=   rS   r   r   r0   r_   r`   http_timeoutra   rb   s
             r$   create_poolzSandboxClient.create_pool  s    : ''' * "#
 #
	"R<Lzs,OOH%%'''>(--//222$ 	 	 	a   	s   AA0 0B?BBc                ^   | j          d| }	 | j                            |          }|                                 t	          j        |                                          S # t          j        $ r;}|j	        j
        dk    rt          d| dd          |t          |            d}~ww xY w)zGet a Pool by name.

        Args:
            name: Pool name.

        Returns:
            Pool.

        Raises:
            ResourceNotFoundError: If pool not found.
            SandboxClientError: For other errors.
        /pools/rf   Pool 'rh   poolrj   N)r8   r<   rl   r\   r   r]   rZ   r:   r^   ra   rm   r
   r   rn   s        r$   get_poolzSandboxClient.get_pool  s     ....
	z~~c**H%%'''>(--//222$ 	 	 	z%,,+.T...f   %Q'''	rp   
list[Pool]c                r   | j          d}	 | j                            |          }|                                 |                                }d |                    dg           D             S # t
          j        $ r9}|j        j        dk    rt          d| d          |t          |            d}~ww xY w)zEList all Pools.

        Returns:
            List of Pools.
        r   c                6    g | ]}t          j        |          S r*   )r   r]   )rt   ps     r$   rv   z,SandboxClient.list_pools.<locals>.<listcomp>6  s"    EEE!DN1%%EEEr&   poolsrf   rx   ry   Nrz   r{   s        r$   
list_poolszSandboxClient.list_pools*  s     '''	z~~c**H%%'''==??DEEtxx/D/DEEEE$ 	 	 	z%,,%;s ; ; ;   %Q'''	r~   )r   r   Optional[int]c                  | j          d| }i }|||d<   |||d<   |s|                     |          S 	 | j                            ||          }|                                 t          j        |                                          S # t          j	        $ rr}|j
        j        dk    rt          d| dd	
          ||j
        j        dk    r't          |          }t          |d         d	
          |t          |            d}~ww xY w)a  Update a Pool's name and/or replica count.

        You can update the display name, replica count, or both.
        The template reference cannot be changed after creation.

        Args:
            name: Current pool name.
            new_name: New display name (optional).
            replicas: New number of replicas (0-100). Set to 0 to pause.

        Returns:
            Updated Pool.

        Raises:
            ResourceNotFoundError: If pool not found.
            ValidationError: If template was deleted.
            ResourceNameConflictError: If new_name is already in use.
            SandboxQuotaExceededError: If quota exceeded when scaling up.
            SandboxClientError: For other errors.
        r   NrS   r   r   rf   r   rh   r   rj   r   r   )r8   r   r<   r   r\   r   r]   rZ   r:   r^   ra   rm   r
   r   r	   r   )	r=   rS   r   r   r_   r`   ra   rb   r|   s	            r$   update_poolzSandboxClient.update_pool@  sQ   6 ...."$&GFO"*GJ 	'==&&&	z''''::H%%'''>(--//222$ 	 	 	z%,,+.T...f   z%,,+A../O6   a   	s   AB DA-DDc                    | j          d| }	 | j                            |          }|                                 dS # t          j        $ r@}|j        j        dk    rt          d| dd          |t          |           Y d}~dS d}~ww xY w)zDelete a Pool.

        This will terminate all sandboxes in the pool.

        Args:
            name: Pool name.

        Raises:
            ResourceNotFoundError: If pool not found.
            SandboxClientError: For other errors.
        r   rf   r   rh   r   rj   N
r8   r<   r   r\   r:   r^   ra   rm   r
   r   rn   s        r$   delete_poolzSandboxClient.delete_poolx  s     ....	(z((--H%%'''''$ 	( 	( 	(z%,,+.T...f   %Q'''''''''	(   .> B5BB)rS   r0   r   c               D    |                      |||          }d|_        |S )a  Create a sandbox and return a Sandbox instance.

        This is the primary method for creating sandboxes. Use it as a
        context manager for automatic cleanup:

            with client.sandbox(template_name="my-template") as sandbox:
                result = sandbox.run("echo hello")

        The sandbox is automatically deleted when exiting the context manager.
        For sandboxes with manual lifecycle management, use create_sandbox().

        Args:
            template_name: Name of the SandboxTemplate to use.
            name: Optional sandbox name (auto-generated if not provided).
            timeout: Timeout in seconds when waiting for ready.

        Returns:
            Sandbox instance.

        Raises:
            ResourceTimeoutError: If timeout waiting for sandbox to be ready.
            ResourceCreationError: If sandbox creation fails.
            SandboxClientError: For other errors.
        )r   rS   r0   T)create_sandbox_auto_delete)r=   r   rS   r0   sbs        r$   sandboxzSandboxClient.sandbox  s5    >   ' ! 
 

 	r&   T)rS   r0   rW   rW   boolc               R   | j          d}||d}|r||d<   |r||d<   |r|dz   nd}	 | j                            |||          }|                                 t	          j        |                                | d          S # t          j        $ r}	t          |	            d	}	~	ww xY w)
a3  Create a new Sandbox.

        The sandbox is NOT automatically deleted. Use delete_sandbox() for cleanup,
        or use sandbox() for automatic cleanup with a context manager.

        Args:
            template_name: Name of the SandboxTemplate to use.
            name: Optional sandbox name (auto-generated if not provided).
            timeout: Timeout in seconds when waiting for ready (only used when
                wait_for_ready=True).
            wait_for_ready: If True (default), block until sandbox is ready.
                If False, return immediately with status "provisioning". Use
                get_sandbox_status() or wait_for_sandbox() to poll for readiness.

        Returns:
            Created Sandbox. When wait_for_ready=False, the sandbox will have
            status="provisioning" and cannot be used for operations until ready.

        Raises:
            ResourceTimeoutError: If timeout waiting for sandbox to be ready.
            ResourceCreationError: If sandbox creation fails.
            SandboxClientError: For other errors.
        /boxes)r   rW   r0   rS   rX   rY   Fclientauto_deleteN)
r8   r<   r[   r\   r   r]   rZ   r:   r^   r   )
r=   r   rS   r0   rW   r_   r`   r   ra   rb   s
             r$   r   zSandboxClient.create_sandbox  s    > ''' +,#
 #
  	)!(GI 	#"GFO)7?"R	zs,OOH%%'''$X]]__TuUUUU$ 	 	 	)!,,,	s   AB B&B!!B&c                d   | j          d| }	 | j                            |          }|                                 t	          j        |                                | d          S # t          j        $ r;}|j	        j
        dk    rt          d| dd          |t          |            d	}~ww xY w)
aF  Get a Sandbox by name.

        The sandbox is NOT automatically deleted. Use delete_sandbox() for cleanup.

        Args:
            name: Sandbox name.

        Returns:
            Sandbox.

        Raises:
            ResourceNotFoundError: If sandbox not found.
            SandboxClientError: For other errors.
        /boxes/Fr   rf   	Sandbox 'rh   r   rj   N)r8   r<   rl   r\   r   r]   rZ   r:   r^   ra   rm   r
   r   rn   s        r$   get_sandboxzSandboxClient.get_sandbox  s     ....
	z~~c**H%%'''$X]]__TuUUUU$ 	 	 	z%,,+1111   %Q'''	s   AA% %B/46B**B/list[Sandbox]c                x     j          d}	  j                            |          }|                                 |                                } fd|                    dg           D             S # t
          j        $ r9}|j        j        dk    rt          d| d          |t          |            d}~ww xY w)zMList all Sandboxes.

        Returns:
            List of Sandboxes.
        r   c                >    g | ]}t          j        |d           S )Fr   )r   r]   )rt   cr=   s     r$   rv   z0SandboxClient.list_sandboxes.<locals>.<listcomp>  s;        !!DeDDD  r&   	sandboxesrf   rx   ry   Nrz   r{   s   `    r$   list_sandboxeszSandboxClient.list_sandboxes  s     '''	z~~c**H%%'''==??D   +r22    $ 	 	 	z%,,%;s ; ; ;   %Q'''	s   A#A1 1B9 4B44B9c                  | j          d| }d|i}	 | j                            ||          }|                                 t	          j        |                                | d          S # t          j        $ ra}|j	        j
        dk    rt          d| dd	
          ||j	        j
        dk    rt          d| dd	
          |t          |            d}~ww xY w)ay  Update a sandbox's display name.

        Args:
            name: Current sandbox name.
            new_name: New display name.

        Returns:
            Updated Sandbox.

        Raises:
            ResourceNotFoundError: If sandbox not found.
            ResourceNameConflictError: If new_name is already in use.
            SandboxClientError: For other errors.
        r   rS   r   Fr   rf   r   rh   r   rj   r   zSandbox name 'z' already in useN)r8   r<   r   r\   r   r]   rZ   r:   r^   ra   rm   r
   r	   r   )r=   rS   r   r_   r`   ra   rb   s          r$   update_sandboxzSandboxClient.update_sandbox%  s"    ....8$	z''''::H%%'''$X]]__TuUUUU$ 	 	 	z%,,+1111   z%,,/?X???"+    %Q'''	s   AA+ +C:ACCc                    | j          d| }	 | j                            |          }|                                 dS # t          j        $ r@}|j        j        dk    rt          d| dd          |t          |           Y d}~dS d}~ww xY w)zDelete a Sandbox.

        Args:
            name: Sandbox name.

        Raises:
            ResourceNotFoundError: If sandbox not found.
            SandboxClientError: For other errors.
        r   rf   r   rh   r   rj   Nr   rn   s        r$   delete_sandboxzSandboxClient.delete_sandboxH  s     ....	(z((--H%%'''''$ 	( 	( 	(z%,,+1111   %Q'''''''''	(r   r   c                `   | j          d| d}	 | j                            |          }|                                 t	          j        |                                          S # t          j        $ r;}|j	        j
        dk    rt          d| dd          |t          |            d}~ww xY w)	a  Get the provisioning status of a sandbox.

        This is a lightweight endpoint designed for high-frequency polling
        during sandbox provisioning. It returns only the status fields
        without full sandbox data.

        Args:
            name: Sandbox name.

        Returns:
            ResourceStatus with status and status_message.

        Raises:
            ResourceNotFoundError: If sandbox not found.
            SandboxClientError: For other errors.
        r   z/statusrf   r   rh   r   rj   N)r8   r<   rl   r\   r   r]   rZ   r:   r^   ra   rm   r
   r   rn   s        r$   get_sandbox_statusz SandboxClient.get_sandbox_status^  s    " 55555
	z~~c**H%%'''!+HMMOO<<<$ 	 	 	z%,,+1111   %Q'''	s   AA# #B-26B((B-x   g      ?)r0   poll_intervalr   c                  ddl }|                                |z   }	 |                     |          }|j        dk    r|                     |          S |j        dk    rt          |j        pdd          ||                                z
  }|dk    rt          d	| d
| dd|j                  |                    t          ||                     )a  Poll until a sandbox reaches "ready" or "failed" status.

        Uses the lightweight status endpoint for polling, then fetches the
        full sandbox data once ready.

        Args:
            name: Sandbox name.
            timeout: Maximum time to wait in seconds.
            poll_interval: Time between status checks in seconds.

        Returns:
            Sandbox in "ready" status.

        Raises:
            ResourceCreationError: If sandbox status becomes "failed".
            ResourceTimeoutError: If timeout expires while still "provisioning".
            ResourceNotFoundError: If sandbox not found.
            SandboxClientError: For other errors.
        r   NTreadyfailedzSandbox provisioning failedr   rj   r   z' not ready after s)rk   last_status)
time	monotonicr   statusr   r   status_messager   sleepmin)r=   rS   r0   r   r   deadliner   	remainings           r$   wait_for_sandboxzSandboxClient.wait_for_sandbox}  s   4 	>>##g-	6,,T22F}''''---}((+)J-J"+    !4>>#3#33IA~~*BBBBBB"+ &   
 JJs=)44555!	6r&   )r/   r'   r0   r3   r1   r'   r2   r4   )r   r@   )r   r-   )rJ   rK   rL   rM   rN   rO   r   r@   )rS   r   rT   r   r0   r4   r   r   )rS   r   r   r   )r   rq   )rS   r   r   r@   )rS   r   r   r'   rT   r'   r   r   )rS   r   r   r   r   r   r   r   r   r'   r   r   r   r   )rS   r   r   r   )r   r   )rS   r   r   r   r   r   )
rS   r   r   r   r   r4   r0   r4   r   r   )rS   r   r   r   )r   r   )rS   r   r   r'   r   r   r   r   )r   r   rS   r'   r0   r4   r   r   )
r   r   rS   r'   r0   r4   rW   r   r   r   )rS   r   r   r   )r   r   )rS   r   r   r   r   r   )rS   r   r   r   )rS   r   r0   r4   r   r3   r   r   ) __name__
__module____qualname____doc__r?   rB   rG   rI   rQ   rc   ro   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r*   r&   r$   r-   r-   3   s        . '+!%Y Y Y Y Y Y:            $ ' ' ' ' ' 'R   6   ,( ( ( (< #'"8 8 8 8 8 8F !%9=5 5 5 5 5 5n   6   ,! ! ! !F( ( ( (L . . . . . .`   6   4 #'"&6 6 6 6 6 6p( ( ( (@ #% % % % % %V ##2 2 2 2 2 2h   :   2! ! ! !F( ( ( (,   F "-6 -6 -6 -6 -6 -6 -6 -6r&   r-   )r   r   )r   r'   )%r   
__future__r   typingr   r   r:   	langsmithr   r    langsmith.sandbox._exceptionsr   r   r	   r
   r   r   r   langsmith.sandbox._helpersr   r   r   r   r   langsmith.sandbox._modelsr   r   r   r   r   langsmith.sandbox._sandboxr   langsmith.sandbox._transportr   r%   r+   r-   r*   r&   r$   <module>r      s   K K " " " " " "                  ' ' ' ' ' '                                            / . . . . . 7 7 7 7 7 7. . . .+ + + +
w6 w6 w6 w6 w6 w6 w6 w6 w6 w6r&   