
    %`i8                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dl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mZ d dlmZmZ d dlmZ d dlmZmZ  ej         e!          Z"d	Z#ee G d
 de                                  Z$e G d d                      Z% G d de%          Z&de'dee'e'f         fdZ(dee'         de%fdZ)e	 ddee'         de*de%fd            Z+dS )    N)	dataclass)AnyDictOptionalTuple) RAY_ADDRESS_ENVIRONMENT_VARIABLE"RAY_NAMESPACE_ENVIRONMENT_VARIABLE$RAY_RUNTIME_ENV_ENVIRONMENT_VARIABLE)get_ray_client_dependency_errorsplit_address)BaseContextinit)	JobConfig)
Deprecated	PublicAPIzYhttps://docs.ray.io/en/latest/cluster/running-applications/job-submission/ray-client.htmlc                       e Zd ZU dZee         ed<   eed<   eed<   eed<   eed<   eej	        j
        j                 ed<   dd	ZddZddZd Zdedd
fdZd
S )ClientContextz@
    Basic context manager for a ClientBuilder connection.

    dashboard_urlpython_versionray_version
ray_commit_num_clients_context_to_restorereturnc                 .    |                                   | S N)_swap_contextselfs    f/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/client_builder.py	__enter__zClientContext.__enter__-   s        Nc                 X    |                      d           |                                  d S )NF)_disconnect_with_contextr   )r   excs     r    __exit__zClientContext.__exit__1   s.    %%e,,,r"   c                     |                                   |                     d           |                                   d S NT)r   r$   r   s    r    
disconnectzClientContext.disconnect5   s@    %%d+++r"   c                 ~    | j         5t          j        j        j                            | j                   | _         d S d S r   )r   rayutilclientset_contextr   s    r    r   zClientContext._swap_context:   s>    #/'*x':'F'F(( (D$$$ 0/r"   force_disconnectc                    t           j        j        j                                         rQt           j        j        j                                         s|r%t           j        j                                         dS dS t           j        j        j	        j
        dS t           j        j        j	        j
                                        rt                              d           dS t          j                     dS )z
        Disconnect Ray. If it's a ray client and created with `allow_multiple`,
        it will do nothing. For other cases this either disconnects from the
        remote Client Server or shuts the current driver down.
        NzqThe current Ray Cluster is scoped to this process. Disconnecting is not possible as it will shutdown the cluster.)r+   r,   r-   is_connected
is_defaultclient_connectr)   _privateworkerglobal_workernodeis_headloggerdebugshutdown)r   r/   s     r    r$   z&ClientContext._disconnect_with_context@   s     8?++-- 	x"--// 53C 5'22444445 5 \ .3;F\ .3;;== 	LL     LNNNNNr"   )r   r   r   N)__name__
__module____qualname____doc__r   str__annotations__intr+   r,   r-   
RayAPIStubr!   r&   r)   r   boolr$    r"   r    r   r      s          
 C=   OOO!#(/"<====         
   $      r"   r   c                       e Zd ZdZdee         ddfdZdeeef         dd fdZ	dedd fd	Z
defd
Zd ZddZddZdS )ClientBuilderz
    Builder for a Ray Client connection. This class can be subclassed by
    custom builder classes to modify connection behavior to include additional
    features or altered semantics. One example is the ``_LocalClientBuilder``.
    addressr   Nc                     t                      t          d          || _        t                      | _        i | _        d| _        d | _        d | _        d| _	        d S )NzRay Client requires pip package `ray[client]`. If you installed the minimal Ray (e.g. `pip install ray`), please reinstall by executing `pip install ray[client]`.FT)
r   
ValueErrorrI   r   _job_config_remote_init_kwargs_allow_multiple_connections_credentials	_metadata_deprecation_warn_enabled)r   rI   s     r    __init__zClientBuilder.__init__`   sl    *,,8K  
 $;;#%  ,1(  *.&&&r"   envc                 :    | j                             |           | S )a  
        Set an environment for the session.
        Args:
            env (Dict[st, Any]): A runtime environment to use for this
                connection. See :ref:`runtime-environments` for what values are
                accepted in this dict.
        )rL   set_runtime_env)r   rS   s     r    rS   zClientBuilder.envs   s      	((---r"   	namespacec                 :    | j                             |           | S )zl
        Sets the namespace for the session.
        Args:
            namespace: Namespace to use.
        )rL   set_ray_namespace)r   rV   s     r    rV   zClientBuilder.namespace~   s      	**9555r"   c           	      t   | j         r|                                  |                                  t          j        j        j                                        }t          j        j                                        dk    }| j        s|s|rt          d          d}| j        r)t          j        j        j        
                    d          }t          j        j                            | j        | j        | j        | j        | j                  }t          j        j        j                                        }t'          ||d         |d         |d         |d         t          j        j        j                                        	          }| j        r)t          j        j        j        
                    |           |S )
a  
        Begin a connection to the address passed in via ray.client(...).

        Returns:
            ClientInfo: Dataclass with information about the setting. This
                includes the server's version of Python & Ray as well as the
                dashboard_url.
        r   zsThe client has already connected to the cluster with allow_multiple=True. Please set allow_multiple=True to proceedN)
job_configrO   ray_init_kwargsmetadatar   r   r   num_clientsr   r   r   r   r   r   )rQ   _client_deprecation_warn_fill_defaults_from_envr+   r,   r-   r1   num_connected_contextsrN   rK   r.   r3   connectrI   rL   rO   rM   rP   _get_dashboard_urlr   get_context)r   default_cli_connectedhas_cli_connectedold_ray_cxtclient_info_dictr   cxts          r    rb   zClientBuilder.connect   s    ) 	,))+++ 	$$&&& !$ 3 @ @ B BHOBBDDqH0		)		 "		
    + 	@(/-99$??K82::L'* 4^ ; 
 
 +>>@@'+,<=(7'5)-8 # 3 ? ? A A
 
 
 + 	9HO++K888
r"   c                 N   t           j                            t                    }|r!| j        j        |                     |           t           j                            t                    }|r5| j        j        +| 	                    t          j        |                     d S d S d S r   )osenvirongetr	   rL   ray_namespacerV   r
   runtime_envrS   jsonloads)r   namespace_env_varruntime_env_vars      r    r`   z%ClientBuilder._fill_defaults_from_env   s    JNN+MNN 	.!1!?!GNN,---*..)MNN 	2t/;CHHTZ0011111	2 	2CCr"   c                 z   |                     d          |                     |d                    |d= |                     d          |                     |d                    |d= |                     d          du r
d| _        |d= d|                                v r|d         | _        |d= d|                                v r|d         | _        |d= |rt          j        t                    }t          |                                                              |j                                                  }t          |          dk    r5t          d	                    d
                    |                              || _        d
                    |          }t$                              d|            | S )aa  
        When a client builder is constructed through ray.init, for example
        `ray.init(ray://..., namespace=...)`, all of the
        arguments passed into ray.init with non-default values are passed
        again into this method. Custom client builders can override this method
        to do their own handling/validation of arguments.
        rV   Nro   allow_multipleTrO   rP   r   zGot unexpected kwargs: {}, z:Passing the following kwargs to ray.init() on the server: )rm   rV   rS   rN   keysrO   rP   inspect	signatureray_driver_initset
difference
parameterslenRuntimeErrorformatjoinrM   r9   info)r   kwargsexpected_sig
extra_argsunknowns        r    
_init_argszClientBuilder._init_args   s    ::k"".NN6+.///{#::m$$0HHVM*+++}%::&''4///3D,'(V[[]]** &~ 6D~&&++--''#K0DN{# 	",_==LV[[]]++66|7N7S7S7U7UVVJ:"""/66tyy7L7LMM   (.D$ii''GKK,"), ,   r"   c                    | j         j        }| j         j        }g }| j        rRt	          | t
                    r|                    d| j         d           n|                    d| j         d           |r|                    d| d           |r|                    d           d                    |          }d| d}t          j	        dt           d	| d
t          d           dS )z
        Generates a warning for user's if this ClientBuilder instance was
        created directly or through ray.client, instead of relying on
        internal methods (ray.init, or auto init)
        "z"ray://znamespace="zruntime_env=<your_runtime_env>rv   z	ray.init()zStarting a connection through `ray.client` will be deprecated in future ray versions in favor of `ray.init`. See the docs for more details: zR. You can replace your call to `ray.client().connect()` with the following:
      
   )
stacklevelN)rL   rn   ro   rI   
isinstance_LocalClientBuilderappendr   warningswarnCLIENT_DOCS_URLDeprecationWarning)r   rV   ro   replacement_argsargs_strreplacement_calls         r    r_   z&ClientBuilder._client_deprecation_warn   sG    $2	&2< 	C$ 344 C
 !''(;DL(;(;(;<<<< ''(A$,(A(A(ABBB 	@##$>)$>$>$>??? 	F ##$DEEE99-..2x222 	*,* * &	* * *
 	
 	
 	
 	
 	
 	
r"   )r   rH   r<   )r=   r>   r?   r@   r   rA   rR   r   r   rS   rV   r   rb   r`   r   r_   rF   r"   r    rH   rH   X   s         . .$ . . . .&	tCH~ 	/ 	 	 	 	3 ?    9 9 9 9 9v2 2 2) ) ) )V'
 '
 '
 '
 '
 '
r"   rH   c                       e Zd ZdefdZdS )r   r   c           	         | j         r|                                  |                                  t          j        | j        | j                  }t          |d         d                    t          j
        d         t          j
        d         t          j
        d                   t          j        t          j        dd          S )	zQ
        Begin a connection to the address passed in via ray.client(...)
        )rI   rZ   	webui_urlz{}.{}.{}r         Nr^   )rQ   r_   r`   r+   r   rI   rL   r   r   sysversion_info__version__
__commit__)r   connection_dicts     r    rb   z_LocalClientBuilder.connect"  s     ) 	,))+++ 	$$&&&(4<DDTUUU)+6%,, #S%5a%8#:J1:M  ~ $	
 	
 	
 		
r"   N)r=   r>   r?   r   rb   rF   r"   r    r   r   !  s/        
 
 
 
 
 
 
r"   r   rI   r   c                 2    d| vrd| z   } t          |           S )z
    Splits address into a module string (scheme) and an inner_address.

    If the scheme is not present, then "ray://" is prepended to the address.
    z://zray://)r   )rI   s    r    _split_addressr   :  s'     GW$!!!r"   c                    | dk    rt          d          S | 3t          j        j                            |           } t          |           S t          |           \  }}	 t          j        |          }n(# t          $ r}t          d| d|            |d }~ww xY wdt          |          v sJ d| d            |                    |          S )NlocalzModule: z6 does not exist.
This module was parsed from Address: rH   z does not have ClientBuilder.)r   r+   r4   servicescanonicalize_bootstrap_addressr   	importlibimport_module	Exceptionr   dirrH   )rI   module_stringinner_addressmodulees        r    _get_builder_from_addressr   E  s   '"7+++ ,'FFwOO"7+++#1'#:#: M=(77   >} > >4;> >
 
 	
 c    >->>>   ...s   A3 3
B=BBTrQ   c                     t           j                            t                    }|r*| (t                              d| dt           d           |} t          |           }||_        |S )a[  
    Creates a ClientBuilder based on the provided address. The address can be
    of the following forms:

        * None: Connects to or creates a local cluster and connects to it.
        * ``"local"``: Creates a new cluster locally and connects to it.
        * ``"IP:Port"``: Connects to a Ray Client Server at the given address.
        * ``"module://inner_address"``: load module.ClientBuilder & pass
            inner_address

    The _deprecation_warn_enabled flag enables deprecation warnings, and is
    for internal use only. Set it to False to suppress client deprecation
    warnings.
    NzUsing address (z$) instead of auto-detection because z is set.)rk   rl   rm   r   r9   r:   r   rQ   )rI   rQ   env_addressbuilders       r    r-   r-   [  s    $ *..!ABBK wBk B B7B B B	
 	
 	
 '00G(AG%Nr"   r(   ),r   rx   rp   loggingrk   r   r   dataclassesr   typingr   r   r   r   ray.util.client_connectr+   ray._private.ray_constantsr   r	   r
   ray._private.utilsr   r   ray._private.workerr   r   rz   ray.job_configr   ray.util.annotationsr   r   	getLoggerr=   r9   r   r   rH   r   rA   r   r   rE   r-   rF   r"   r    <module>r      sz          				 



  ! ! ! ! ! ! - - - - - - - - - - - -             
 N M M M M M M M D D D D D D D D $ $ $ $ $ $ 6 6 6 6 6 6 6 6		8	$	$%  
5 5 5 5 5K 5 5  5p E
 E
 E
 E
 E
 E
 E
 E
P
 
 
 
 
- 
 
 
2"C "E#s(O " " " "/x} / / / / /, EI c]>B     r"   