
    &`i;                         d 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	m
Z
mZ ddlmZ ddlmZ erddlmZ ddlmZ dd	lmZ dd
lmZmZmZ  ej        e          Zd Z G d d          ZdS )z^This file defines the interface between the ray client worker
and the overall ray module API.
    N)Future)TYPE_CHECKINGAnyCallableListOptionalUnion)ray_option_utils)_ClientWorkerPropertyAPI)
ActorClass)DataResponse)RemoteFunction)ClientActorHandleClientObjectRef
ClientStubc                 Z    t          | t                    r|                     d          S | S )Nzutf-8)
isinstancestrencode)values    g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/client/api.py	_as_bytesr      s+    % %||G$$$L    c                   r   e Zd ZdZdFdZdddZd Zd Zd Zd	d
de	e
         fdZdeddfdZdeddfdZdGdZ	 dFdedee         ddfdZdHdede	e         fdZdddIdZddddJdZdefd Zd! Zd" Zd# Zd$ Zd% Zdefd&ZdFd'ee         dee	e                  fd(Z defd)Z!dd*d+e"eef         dee"eef                  defd,Z#dd*d+e"eef         dee"eef                  defd-Z$	 dKdd*d+e"eef         d.e"eef         d/edee"eef                  def
d0Z%ddd1d+e"eef         d2edee"eef                  de&fd3Z'dd*d4e"eef         dee"eef                  de	e         fd5Z(d6ed7e&ddfd8Z)dd9defd:Z*d;d<defd=Z+d+edd
fd>Z,d+edefd?Z-d+efd@Z.dAddBe/dCgdf         ddfdDZ0defdEZ1dS )L
_ClientAPIzThe Client-side methods corresponding to the ray API. Delegates
    to the Client Worker that contains the connection to the ClientServer.
    Nc                     || _         d S N)worker)selfr   s     r   __init__z_ClientAPI.__init__    s    r   timeoutc                :    | j                             ||          S )zget is the hook stub passed on to replace `ray.get`

        Args:
            vals: [Client]ObjectRef or list of these refs to retrieve.
            timeout: Optional timeout in milliseconds
        r!   )r   get)r   valsr"   s      r   r$   z_ClientAPI.get#   s     {tW555r   c                 &     | j         j        |i |S )zput is the hook stub passed on to replace `ray.put`

        Args:
            val: The value to `put`.
            args: opaque arguments
            kwargs: opaque keyword arguments
        )r   putr   argskwargss      r   r'   z_ClientAPI.put,   s     t{////r   c                 &     | j         j        |i |S )zwait is the hook stub passed on to replace `ray.wait`

        Args:
            args: opaque arguments
            kwargs: opaque keyword arguments
        )r   waitr(   s      r   r,   z_ClientAPI.wait6   s      t{0000r   c                 F   ddl m} t          |          dk    rCt          |          dk    r0t          |d                   r  |d          |d                   S t          |          dk    rt          |          dk    sJ t          j                     ||          S )a	  remote is the hook stub passed on to replace `ray.remote`.

        This sets up remote functions or actors, as the decorator,
        but does not execute them.

        Args:
            args: opaque arguments
            kwargs: opaque keyword arguments
        r   )remote_decorator   N)options)ray.util.client.commonr.   lencallabler
   remote_args_error_string)r   r)   r*   r.   s       r   remotez_ClientAPI.remote?   s     	<;;;;;t99>>c&kkQ..8DG3D3D.1##D111$q':::IINNs6{{Q4  /.////r   instancer   returnc                 .     | j         j        |g|R i |S )a2  call_remote is called by stub objects to execute them remotely.

        This is used by stub objects in situations where they're called
        with .remote, eg, `f.remote()` or `actor_cls.remote()`.
        This allows the client stub objects to delegate execution to be
        implemented in the most effective way whether it's in the client,
        clientserver, or raylet worker.

        Args:
            instance: The Client-side stub reference to a remote object
            args: opaque arguments
            kwargs: opaque keyword arguments
        )r   call_remote)r   r6   r)   r*   s       r   r9   z_ClientAPI.call_remoteV   s)     't{&xA$AAA&AAAr   idc                 6    | j                             |          S )ap  Attempts to release an object reference.

        When client references are destructed, they release their reference,
        which can opportunistically send a notification through the datachannel
        to release the reference being held for that object on the server.

        Args:
            id: The id of the reference to release on the server side.
        )r   call_releaser   r:   s     r   r<   z_ClientAPI.call_releasef   s     {''+++r   c                 6    | j                             |          S )a  Attempts to retain a client object reference.

        Increments the reference count on the client side, to prevent
        the client worker from attempting to release the server reference.

        Args:
            id: The id of the reference to retain on the client side.
        )r   call_retainr=   s     r   r?   z_ClientAPI.call_retainr   s     {&&r***r   c                 4    | j                                         S )zsclose cleans up an API connection by closing any channels or
        shutting down any servers gracefully.
        )r   closer   s    r   rA   z_ClientAPI.close}   s     {  """r   name	namespacer   c                 8    | j                             ||          S )zReturns a handle to an actor by name.

        Args:
            name: The name passed to this actor by
              Actor.options(name="name").remote()
        )r   	get_actor)r   rC   rD   s      r   rF   z_ClientAPI.get_actor   s     {$$T9555r   Fall_namespacesc                 6    | j                             |          S )a  List all named actors in the system.

        Actors must have been created with Actor.options(name="name").remote().
        This works for both detached & non-detached actors.

        By default, only actors in the current namespace will be returned
        and the returned entries will simply be their name.

        If `all_namespaces` is set to True, all actors in the cluster will be
        returned regardless of namespace, and the retunred entries will be of
        the form '<namespace>/<name>'.
        )r   list_named_actors)r   rG   s     r   rI   z_ClientAPI.list_named_actors   s     {,,^<<<r   T)
no_restartactorc                8    | j                             ||          S )zkill forcibly stops an actor running in the cluster

        Args:
            no_restart: Whether this actor should be restarted if it's a
              restartable actor.
        )r   terminate_actor)r   rK   rJ   s      r   killz_ClientAPI.kill   s     {**5*===r   )force	recursiveobjr   c                :    | j                             |||          S )a  Cancels a task on the cluster.

        If the specified task is pending execution, it will not be executed. If
        the task is currently executing, the behavior depends on the ``force``
        flag, as per `ray.cancel()`

        Only non-actor tasks can be canceled. Canceled tasks will not be
        retried (max_retries will not be respected).

        Args:
            object_ref: ObjectRef returned by the task
                that should be canceled.
            force: Whether to force-kill a running task by killing
                the worker that is running the task.
            recursive: Whether to try to cancel tasks submitted by
                the task specified.
        )r   terminate_task)r   rQ   rO   rP   s       r   cancelz_ClientAPI.cancel   s    $ {))#ui@@@r   c                 4    | j                                         S )zTrue if our client is connected, and if the server is initialized.
        Returns:
            A boolean determining if the client is connected and
            server initialized.
        )r   is_initializedrB   s    r   rV   z_ClientAPI.is_initialized   s     {))+++r   c                 b    ddl mc mc m} | j                            |j        j                  S )zGet a list of the nodes in the cluster (for debugging only).

        Returns:
            Information about the Ray clients in the cluster.
        r   N)!ray.core.generated.ray_client_pb2core	generatedray_client_pb2r   get_cluster_infoClusterInfoTypeNODESr   r[   s     r   nodesz_ClientAPI.nodes   sE     	CBBBBBBBBBBB{++N,J,PQQQr   c                     ddg}d| d}t          |          dk    rt                    dk    s
J |            D ]}d| d| }||v s
J |            fd}|S )	zAnnotate an actor method

        Args:
            num_returns: The number of object refs that should be returned by
                invocations of this actor method.
        num_returnsconcurrency_groupzZThe @ray.method decorator must be applied using at least one of the arguments in the list z+, for example '@ray.method(num_returns=2)'.r   z-Unexpected keyword argument to @ray.method: "z'". The supported keyword arguments are c                 L    dv rd         | _         dv rd         | _        | S )Nrb   rc   )__ray_num_returns____ray_concurrency_group__)methodr*   s    r   annotate_methodz*_ClientAPI.method.<locals>.annotate_method   s9    &&-3M-B*"f,,39:M3N0Mr   )r2   )r   r)   r*   valid_kwargserror_stringkeykey_error_stringrh   s     `     r   rg   z_ClientAPI.method   s     &':;,)5, , , 	
 4yyA~~#f++///<//1 	9 	9CB B B3?B B  ,&&&(8&&&&	 	 	 	 	 r   c                 b    ddl mc mc m} | j                            |j        j                  S )a'  Get the current total cluster resources.

        Note that this information can grow stale as nodes are added to or
        removed from the cluster.

        Returns:
            A dictionary mapping resource name to the total quantity of that
                resource in the cluster.
        r   N)rX   rY   rZ   r[   r   r\   r]   CLUSTER_RESOURCESr_   s     r   cluster_resourcesz_ClientAPI.cluster_resources   sL     	CBBBBBBBBBBB{++*<
 
 	
r   c                 b    ddl mc mc m} | j                            |j        j                  S )a  Get the current available cluster resources.

        This is different from `cluster_resources` in that this will return
        idle (available) resources rather than total resources.

        Note that this information can grow stale as tasks start and finish.

        Returns:
            A dictionary mapping resource name to the total quantity of that
                resource in the cluster.
        r   N)rX   rY   rZ   r[   r   r\   r]   AVAILABLE_RESOURCESr_   s     r   available_resourcesz_ClientAPI.available_resources  sL     	CBBBBBBBBBBB{++*>
 
 	
r   c                 N    t          | j                                                  S )zReturn a Ray RuntimeContext describing the state on the server

        Returns:
            A RuntimeContext wrapping a client making get_cluster_info calls.
        )r   r   build_runtime_contextrB   s    r   get_runtime_contextz_ClientAPI.get_runtime_context  s      (44JJLLLr   c                     g S r    rB   s    r   get_gpu_idsz_ClientAPI.get_gpu_ids!  s    	r   filenamec                    t                               d           dd lmc mc m} | j                            |j        j	                  }|@t          |d          5 }t          j        ||           d d d            d S # 1 swxY w Y   d S |S )NzBTimeline will include events from other clients using this server.r   w)loggerwarningrX   rY   rZ   r[   r   r\   r]   TIMELINEopenjsondump)r   ry   r[   
all_eventsoutfiles        r   timelinez_ClientAPI.timeline$  s    P	
 	
 	
 	CBBBBBBBBBBB[11*3
 

 h$$ /	*g.../ / / / / / / / / / / / / / / / / / s   B  BBc                     dS )z.Hook for internal_kv._internal_kv_initialized.Trw   rB   s    r   _internal_kv_initializedz#_ClientAPI._internal_kv_initialized4  s	    
 tr   rD   rk   c                n    | j                             t          |          t          |                    S )z)Hook for internal_kv._internal_kv_exists.r   )r   internal_kv_existsr   r   rk   rD   s      r   _internal_kv_existsz_ClientAPI._internal_kv_exists;  s6     {--cNNi	&:&: . 
 
 	
r   c                n    | j                             t          |          t          |                    S )z&Hook for internal_kv._internal_kv_get.r   )r   internal_kv_getr   r   s      r   _internal_kv_getz_ClientAPI._internal_kv_getC  s6     {**cNNi	&:&: + 
 
 	
r   r   	overwritec                    | j                             t          |          t          |          |t          |                    S )z&Hook for internal_kv._internal_kv_put.r   )r   internal_kv_putr   )r   rk   r   r   rD   s        r   _internal_kv_putz_ClientAPI._internal_kv_putK  sB     {**cNNIe,,i9YCWCW + 
 
 	
r   del_by_prefixrD   r   c                p    | j                             t          |          |t          |                    S )z&Hook for internal_kv._internal_kv_del.r   )r   internal_kv_delr   )r   rk   r   rD   s       r   _internal_kv_delz_ClientAPI._internal_kv_delX  s8     {**cNN-9YCWCW + 
 
 	
r   prefixc                n    | j                             t          |          t          |                    S )z'Hook for internal_kv._internal_kv_list.r   )r   internal_kv_listr   )r   r   rD   s      r   _internal_kv_listz_ClientAPI._internal_kv_listd  s8     {++f9)=)= , 
 
 	
r   uriexpiration_sc                 8    | j                             ||          S )z*Hook for internal_kv._pin_runtime_env_uri.)r   pin_runtime_env_uri)r   r   r   s      r   _pin_runtime_env_uriz_ClientAPI._pin_runtime_env_urio  s    {..sLAAAr   r   c                 6    | j                             |          S )z@Register a ClientActorClass for the ActorClass and return a UUID)r   _convert_actor)r   rK   s     r   r   z_ClientAPI._convert_actors  s    {))%000r   funcr   c                 6    | j                             |          S )z@Register a ClientRemoteFunc for the ActorClass and return a UUID)r   _convert_function)r   r   s     r   r   z_ClientAPI._convert_functionw  s    {,,T222r   c                 6    | j                             |          S )z)Given a UUID, return the converted object)r   _get_convertedr   rk   s     r   r   z_ClientAPI._get_converted{  s    {))#...r   c                 6    | j                             |          S )zACheck if a key UUID is present in the store of converted objects.)r   _converted_key_existsr   s     r   r   z _ClientAPI._converted_key_exists  s    {00555r   c                     |                     d          s"t          d                    |                    |                     |          S )N_zNot available in Ray client: `ray.{}`. This method is only available within Ray remote functions and is not yet implemented in the client API.)
startswithNotImplementedErrorformat__getattribute__r   s     r   __getattr__z_ClientAPI.__getattr__  sN    ~~c"" 	%117  
 $$S)))r   refcallbackr   c                 <    | j                             ||           d S r   )r   register_callback)r   r   r   s      r   _register_callbackz_ClientAPI._register_callback  s"     	%%c844444r   c                     dd l mc mc m} | j                            |j        j                                      dd          S )Nr   dashboard_url )	rX   rY   rZ   r[   r   r\   r]   DASHBOARD_URLr$   r_   s     r   _get_dashboard_urlz_ClientAPI._get_dashboard_url  sX    BBBBBBBBBBBB{++*8
 

#or
"
"	#r   r   )r7   N)F)rK   r   )rQ   r   )T)2__name__
__module____qualname____doc__r    r$   r'   r,   r5   r   r   r9   bytesr<   r?   rA   r   r   rF   boolrI   rN   rT   rV   r`   rg   ro   rr   ru   listrx   r   r   r   r	   r   r   r   intr   r   r   r   r   r   r   r   r   r   r   rw   r   r   r   r      s            $( 6 6 6 6 60 0 01 1 10 0 0.BL Bd6l B B B B 
,u 
, 
, 
, 
, 
,	+e 	+ 	+ 	+ 	+ 	+# # # # 59	6 	6	6$,SM	6		6 	6 	6 	6= = =c = = = = >B > > > > > > 7<t A A A A A A*, , , , ,	R 	R 	R$ $ $L
 
 
"
 
 
&M M MT     # (49:M     $     SW
 
 
e$
4<U3:=N4O
	
 
 
 
 SW
 
 
e$
4<U3:=N4O
	
 
 
 
 	
 26
 
 
3:
 S%Z 
 	
 E#u*-.
 

 
 
 
" $15

 

 

3:

 	


 E#u*-.

 


 

 

 

  26		
 	
 	
c5j!	
 E#u*-.		

 
e	
 	
 	
 	
B B3 B4 B B B B1L 1S 1 1 1 13&6 33 3 3 3 3/# /, / / / /6 6 6 6 6 6*s * * * *5$508.9I49O0P5	5 5 5 5
#C # # # # # #r   r   )r   r   loggingconcurrent.futuresr   typingr   r   r   r   r   r	   ray._commonr
   ray.util.client.runtime_contextr   	ray.actorr   rX   r   ray.remote_functionr   r1   r   r   r   	getLoggerr   r|   r   r   rw   r   r   <module>r      sC      % % % % % % F F F F F F F F F F F F F F F F ( ( ( ( ( ( D D D D D D V$$$$$$>>>>>>222222UUUUUUUUUU		8	$	$  {# {# {# {# {# {# {# {# {# {#r   