
    a
i                        d Z ddlmZ ddlZddlmZmZ ddlZddlm	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Z#ddZ$ G d d          Z%dS )zFAsync SandboxClient class for interacting with the sandbox server API.    )annotationsN)AnyOptional)utils)AsyncSandbox)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)AsyncRetryTransport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/_async_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AsyncSandboxClienta  Async client for interacting with the Sandbox Server API.

    This client provides an async interface for managing sandboxes and templates.

    Example:
        # Uses LANGSMITH_ENDPOINT and LANGSMITH_API_KEY from environment
        async with AsyncSandboxClient() as client:
            # Create a sandbox and run commands
            async with await client.sandbox(template_name="python-sandbox") as sandbox:
                result = await 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 )au  Initialize the AsyncSandboxClient.

        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AsyncClient_http)selfr/   r0   r1   r2   resolved_api_keyr7   r6   s           r$   __init__zAsyncSandboxClient.__init__B   s    ( 'E*C*E*EMMcRR"<&:&<&<("$ 	4#3GK 'K@@@	&'
 
 



r&   r   Nonec                H   K   | j                                          d{V  dS )zClose the async HTTP client.N)r<   acloser=   s    r$   rB   zAsyncSandboxClient.aclosea   s2      j!!!!!!!!!!!r&   c                
   	 | j         j        sd	 t          j                    }|                                s)|                    |                                            dS dS # t          $ r Y dS w xY wdS # t          $ r Y dS w xY w)a$  Best-effort cleanup of the async HTTP client on garbage collection.

        If an event loop is running, schedules ``aclose()`` as a task.
        Otherwise the underlying sockets will be closed by the GC.
        For deterministic cleanup, use ``async with`` or ``await aclose()``.
        N)r<   	is_closedasyncioget_running_loopcreate_taskrB   RuntimeError	Exception)r=   loops     r$   __del__zAsyncSandboxClient.__del__e   s    		:' "355D>>++ 8((777778 8#   DD   	 	 	DD	s/   A4 AA! !
A/+A4 .A//A4 4
BBc                
   K   | S )zEnter async context manager.r*   rC   s    r$   
__aenter__zAsyncSandboxClient.__aenter__w   s      r&   exc_typeOptional[type]exc_valOptional[BaseException]exc_tbOptional[Any]c                >   K   |                                   d{V  dS )zExit async context manager.N)rB   )r=   rO   rQ   rS   s       r$   	__aexit__zAsyncSandboxClient.__aexit__{   s.       kkmmr&   <   )r0   namer   sizer   c               8  K   | j          d}||d|d}	 | j                            |||dz              d{V }|                                 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)rX   rY   wait_for_readyr0      jsonr0   N)
r8   r<   postraise_for_statusr   	from_dictr_   r:   HTTPStatusErrorr   )r=   rX   rY   r0   urlpayloadresponsees           r$   create_volumez AsyncSandboxClient.create_volume   s      0 ))) "	
 
	!Z__SwRT_UUUUUUUUH%%'''#HMMOO444$ 	 	 	(+++	s   AA5 5BBBc                n  K   | j          d| }	 | j                            |           d{V }|                                 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/N  Volume '' not foundvolumeresource_type)r8   r<   getra   r   rb   r_   r:   rc   rf   status_coder   r   r=   rX   rd   rf   rg   s        r$   
get_volumezAsyncSandboxClient.get_volume   s       00$00
	!Z^^C00000000H%%'''#HMMOO444$ 	 	 	z%,,+0t000   %Q'''	   AA* *B496B//B4list[Volume]c                  K   | j          d}	 | j                            |           d{V }|                                 |                                }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.
        r[   Nc                6    g | ]}t          j        |          S r*   )r   rb   ).0vs     r$   
<listcomp>z3AsyncSandboxClient.list_volumes.<locals>.<listcomp>   s#    IIIAF$Q''IIIr&   volumesrk   API endpoint not found: %. Check that api_endpoint is correct.r8   r<   rq   ra   r_   r:   rc   rf   rr   r   r   r=   rd   rf   datarg   s        r$   list_volumeszAsyncSandboxClient.list_volumes   s       )))	!Z^^C00000000H%%'''==??DII)R1H1HIIII$ 	 	 	z%,,%;s ; ; ;   %Q'''	   A'A6 6B>4B99B>c                  K   | j          d| }	 | j                            |           d{V }|                                 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.
        rj   Nrk   rl   rm   rn   ro     messager8   r<   deletera   r:   rc   rf   rr   r   r   r	   r   r=   rX   rd   rf   rg   r   s         r$   delete_volumez AsyncSandboxClient.delete_volume   s      00$00	(!Z..s33333333H%%'''''$ 	( 	( 	(z%,,+0t000   z%,,+A..(iQQQWXX$Q'''''''''	(   4A CA,CC)new_namerY   r   c                 K   | j          d| }i }|||d<   |||d<   |s|                     |           d{V S 	 | j                            ||           d{V }|                                 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.
        rj   NrX   rY   r_   rk   rl   rm   rn   ro   i  r   VolumeResize)
error_typer   )r8   rt   r<   patchra   r   rb   r_   r:   rc   rf   rr   r   r   r   r
   r   )	r=   rX   r   rY   rd   re   rf   rg   r   s	            r$   update_volumez AsyncSandboxClient.update_volume   s     6 00$00"$&GFO"GFO 	/.........	!Z--c-@@@@@@@@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$EE500m512Mi)cpumemorystoragevolume_mountsimager   r   r   r   Optional[list[VolumeMountSpec]]r   c               p  K   | j          d}||||dd}|r||d         d<   |rd |D             |d<   	 | j                            ||           d	{V }	|	                                 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   )rX   r   	resourcesr   r   c                ,    g | ]}|j         |j        d S )volume_name
mount_pathr   )ry   vms     r$   r{   z6AsyncSandboxClient.create_template.<locals>.<listcomp>d  s4     ( ( ( !#bmLL( ( (r&   r   r   N)
r8   r<   r`   ra   r   rb   r_   r:   rc   r   )r=   rX   r   r   r   r   r   rd   re   rf   rg   s              r$   create_templatez"AsyncSandboxClient.create_template:  s     : +++   #
 #
  	6.5GK + 	( ('( ( (GO$
	!Z__Sw_????????H%%'''",X]]__===$ 	 	 	$Q'''	s   AB B5 B00B5c                n  K   | j          d| }	 | j                            |           d{V }|                                 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/Nrk   
Template 'rm   templatero   )r8   r<   rq   ra   r   rb   r_   r:   rc   rf   rr   r   r   rs   s        r$   get_templatezAsyncSandboxClient.get_templateq  s       22D22
	!Z^^C00000000H%%'''",X]]__===$ 	 	 	z%,,+2222*   %Q'''	ru   list[SandboxTemplate]c                  K   | j          d}	 | j                            |           d{V }|                                 |                                }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   Nc                6    g | ]}t          j        |          S r*   )r   rb   )ry   ts     r$   r{   z5AsyncSandboxClient.list_templates.<locals>.<listcomp>  s#    TTTQO-a00TTTr&   	templatesrk   r}   r~   r   r   s        r$   list_templatesz!AsyncSandboxClient.list_templates  s       +++	!Z^^C00000000H%%'''==??DTT$((;PR:S:STTTT$ 	 	 	z%,,%;s ; ; ;   %Q'''	r   c                 K   | j          d| }d|i}	 | j                            ||           d{V }|                                 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   rX   r   Nrk   r   rm   r   ro   r   r   )r8   r<   r   ra   r   rb   r_   r:   rc   rf   rr   r   r   r
   r   )r=   rX   r   rd   re   rf   rg   r   s           r$   update_templatez"AsyncSandboxClient.update_template  s4      22D228$	!Z--c-@@@@@@@@H%%'''",X]]__===$ 	 	 	z%,,+2222*   z%,,+A../O:   %Q'''	s   AA0 0C1?A-C,,C1c                  K   | j          d| }	 | j                            |           d{V }|                                 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   Nrk   r   rm   r   ro   r   r   r   r   s         r$   delete_templatez"AsyncSandboxClient.delete_template  s!      22D22	(!Z..s33333333H%%'''''$ 
	( 
	( 
	(z%,,+2222*   z%,,+A..(O:   %Q'''''''''
	(r   r]   template_namereplicasr   c               >  K   | j          d}|||d|d}	 |dz   }| j                            |||           d{V }|                                 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)rX   r   r   r\   r0   r]   r^   N)
r8   r<   r`   ra   r   rb   r_   r:   rc   r   )
r=   rX   r   r   r0   rd   re   http_timeoutrf   rg   s
             r$   create_poolzAsyncSandboxClient.create_pool  s      : ''' * "#
 #
	"R<L!Z__Sw_UUUUUUUUH%%'''>(--//222$ 	 	 	a   	s   A!A8 8BBBc                n  K   | j          d| }	 | j                            |           d{V }|                                 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/Nrk   Pool 'rm   poolro   )r8   r<   rq   ra   r   rb   r_   r:   rc   rf   rr   r   r   rs   s        r$   get_poolzAsyncSandboxClient.get_pool  s       ....
	!Z^^C00000000H%%'''>(--//222$ 	 	 	z%,,+.T...f   %Q'''	ru   
list[Pool]c                  K   | j          d}	 | j                            |           d{V }|                                 |                                }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   Nc                6    g | ]}t          j        |          S r*   )r   rb   )ry   ps     r$   r{   z1AsyncSandboxClient.list_pools.<locals>.<listcomp><  s"    EEE!DN1%%EEEr&   poolsrk   r}   r~   r   r   s        r$   
list_poolszAsyncSandboxClient.list_pools0  s       '''	!Z^^C00000000H%%'''==??DEEtxx/D/DEEEE$ 	 	 	z%,,%;s ; ; ;   %Q'''	r   )r   r   Optional[int]c               :  K   | j          d| }i }|||d<   |||d<   |s|                     |           d{V S 	 | j                            ||           d{V }|                                 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   NrX   r   r   rk   r   rm   r   ro   r   r   )r8   r   r<   r   ra   r   rb   r_   r:   rc   rf   rr   r   r   r
   r   )	r=   rX   r   r   rd   re   rf   rg   r   s	            r$   update_poolzAsyncSandboxClient.update_poolF  sy     6 ...."$&GFO"*GJ 	-t,,,,,,,,,	!Z--c-@@@@@@@@H%%'''>(--//222$ 	 	 	z%,,+.T...f   z%,,+A../O6   a   	s   AB D(A-DDc                0  K   | j          d| }	 | j                            |           d{V }|                                 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   Nrk   r   rm   r   ro   
r8   r<   r   ra   r:   rc   rf   rr   r   r   rs   s        r$   delete_poolzAsyncSandboxClient.delete_pool~  s       ....	(!Z..s33333333H%%'''''$ 	( 	( 	(z%,,+.T...f   %Q'''''''''	(   4A B5BB)rX   r0   r   c               T   K   |                      |||           d{V }d|_        |S )a  Create a sandbox and return an AsyncSandbox instance.

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

            async with await client.sandbox(template_name="my-template") as sandbox:
                result = await 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:
            AsyncSandbox instance.

        Raises:
            ResourceTimeoutError: If timeout waiting for sandbox to be ready.
            ResourceCreationError: If sandbox creation fails.
            SandboxClientError: For other errors.
        )r   rX   r0   NT)create_sandbox_auto_delete)r=   r   rX   r0   sbs        r$   sandboxzAsyncSandboxClient.sandbox  sW      > &&' ' 
 
 
 
 
 
 
 

 	r&   T)rX   r0   r\   r\   boolc               b  K   | j          d}||d}|r||d<   |r||d<   |r|dz   nd}	 | j                            |||           d{V }|                                 t	          j        |                                | d	          S # t          j        $ r}	t          |	            d}	~	ww xY w)
a8  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 AsyncSandbox. 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   r\   r0   rX   r]   r^   NFclientauto_delete)
r8   r<   r`   ra   r   rb   r_   r:   rc   r   )
r=   r   rX   r0   r\   rd   re   r   rf   rg   s
             r$   r   z!AsyncSandboxClient.create_sandbox  s     > ''' +,#
 #
  	)!(GI 	#"GFO)7?"R	!Z__Sw_UUUUUUUUH%%''')%    $ 	 	 	)!,,,	s   AB
 
B.B))B.c                t  K   | j          d| }	 | j                            |           d{V }|                                 t	          j        |                                | d          S # t          j        $ r;}|j	        j
        dk    rt          d| dd	          |t          |            d}~ww xY w)
aK  Get a Sandbox by name.

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

        Args:
            name: Sandbox name.

        Returns:
            AsyncSandbox.

        Raises:
            ResourceNotFoundError: If sandbox not found.
            SandboxClientError: For other errors.
        /boxes/NFr   rk   	Sandbox 'rm   r   ro   )r8   r<   rq   ra   r   rb   r_   r:   rc   rf   rr   r   r   rs   s        r$   get_sandboxzAsyncSandboxClient.get_sandbox  s       ....	!Z^^C00000000H%%''')%    $ 	 	 	z%,,+1111   %Q'''	s   AA- -B7<6B22B7list[AsyncSandbox]c                   K    j          d}	  j                            |           d{V }|                                 |                                } fd|                    dg           D             S # t
          j        $ r9}|j        j        dk    rt          d| d          |t          |            d}~ww xY w)zRList all Sandboxes.

        Returns:
            List of AsyncSandboxes.
        r   Nc                >    g | ]}t          j        |d           S )Fr   )r   rb   )ry   cr=   s     r$   r{   z5AsyncSandboxClient.list_sandboxes.<locals>.<listcomp>"  s;        &q5III  r&   	sandboxesrk   r}   r~   r   r   s   `    r$   list_sandboxesz!AsyncSandboxClient.list_sandboxes  s      '''	!Z^^C00000000H%%'''==??D   +r22    $ 	 	 	z%,,%;s ; ; ;   %Q'''	s   A)A9 9C4B<<Cc                 K   | j          d| }d|i}	 | j                            ||           d{V }|                                 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)a~  Update a sandbox's display name.

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

        Returns:
            Updated AsyncSandbox.

        Raises:
            ResourceNotFoundError: If sandbox not found.
            ResourceNameConflictError: If new_name is already in use.
            SandboxClientError: For other errors.
        r   rX   r   NFr   rk   r   rm   r   ro   r   zSandbox name 'z' already in use)r8   r<   r   ra   r   rb   r_   r:   rc   rf   rr   r   r
   r   )r=   rX   r   rd   re   rf   rg   s          r$   update_sandboxz!AsyncSandboxClient.update_sandbox/  sC      ....8$	!Z--c-@@@@@@@@H%%''')%    $ 	 	 	z%,,+1111   z%,,/?X???"+    %Q'''	s   AA3 3C#ACC#c                0  K   | j          d| }	 | j                            |           d{V }|                                 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   Nrk   r   rm   r   ro   r   rs   s        r$   delete_sandboxz!AsyncSandboxClient.delete_sandboxT  s       ....	(!Z..s33333333H%%'''''$ 	( 	( 	(z%,,+1111   %Q'''''''''	(r   r   c                p  K   | j          d| d}	 | j                            |           d{V }|                                 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/statusNrk   r   rm   r   ro   )r8   r<   rq   ra   r   rb   r_   r:   rc   rf   rr   r   r   rs   s        r$   get_sandbox_statusz%AsyncSandboxClient.get_sandbox_statusj  s      " 55555
	!Z^^C00000000H%%'''!+HMMOO<<<$ 	 	 	z%,,+1111   %Q'''	s   AA+ +B5:6B00B5x   g      ?)r0   poll_intervalr   c                 K   ddl }|                                |z   }	 |                     |           d{V }|j        dk    r|                     |           d{V S |j        dk    rt          |j        pdd          ||                                z
  }|dk    rt          d	| d
| dd|j                  t          j	        t          ||                     d{V  )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:
            AsyncSandbox 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   ro   r   z' not ready after s)rp   last_status)time	monotonicr   statusr   r   status_messager   rF   sleepmin)r=   rX   r0   r   r   deadliner   	remainings           r$   wait_for_sandboxz#AsyncSandboxClient.wait_for_sandbox  s<     4 	>>##g-	?22488888888F}''!--d333333333}((+)J-J"+    !4>>#3#33IA~~*BBBBBB"+ &   
 -M9 = =>>>>>>>>>!	?r&   )r/   r'   r0   r3   r1   r'   r2   r4   )r   r@   )r   r-   )rO   rP   rQ   rR   rS   rT   r   r@   )rX   r   rY   r   r0   r4   r   r   )rX   r   r   r   )r   rv   )rX   r   r   r@   )rX   r   r   r'   rY   r'   r   r   )rX   r   r   r   r   r   r   r   r   r'   r   r   r   r   )rX   r   r   r   )r   r   )rX   r   r   r   r   r   )
rX   r   r   r   r   r4   r0   r4   r   r   )rX   r   r   r   )r   r   )rX   r   r   r'   r   r   r   r   )r   r   rX   r'   r0   r4   r   r   )
r   r   rX   r'   r0   r4   r\   r   r   r   )rX   r   r   r   )r   r   )rX   r   r   r   r   r   )rX   r   r   r   )rX   r   r0   r4   r   r3   r   r   ) __name__
__module____qualname____doc__r?   rB   rL   rN   rV   rh   rt   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-   4   s          '+!%
 
 
 
 
 
>" " " "   $      $ ' ' ' ' ' '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 ##4 4 4 4 4 4l   >   2# # # #J( ( ( (,   F "-? -? -? -? -? -? -? -?r&   r-   )r   r   )r   r'   )&r   
__future__r   rF   typingr   r   r:   	langsmithr   r     langsmith.sandbox._async_sandboxr   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._transportr   r%   r+   r-   r*   r&   r$   <module>r     s   L L " " " " " "                   ' ' ' ' ' ' 9 9 9 9 9 9                                            = < < < < <. . . .+ + + +
B? B? B? B? B? B? B? B? B? B?r&   