
    &`i<                        d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZ dZ	 	 	 ddedede	eeegef         f         d	e	eeef         eegeeef         f         f         d
eeee         gef                  defdZdS )    N)AnyCallableDictListOptionalUnion)RequestMetadata)DeploymentHandle   handlemethod_nameargskwargscombinereturnc                 *   |d}|i }| j         s|                     d           t          j                    }|                                 sVt          j                    |z
  t          k    rt          d          t          j        d           |                                 V| j        }|t          d          d}t          |d          r|j
        j        }nt          |d	          r|j        }|t          d
          |j        }g }	t          |          }
|
D ]P}|                                }	 t          j        |d          }n# t"          $ r Y ;w xY w|}|}t%          |          r ||          }t%          |          r ||          }t'          |t          t(          f          st#          dt+          |                     t'          |t,                    s$|i }nt#          dt+          |                     dt/          j                     }t3          |||          }t5          j        |          }|	                     |j        j        |g|R i |           Rg }|	rt          j        |	          }|r ||          S |S )al  
    Dispatches a method call to all replicas of the given handle.

    This is useful for dispatching a control plane message such as kv-cache
    reset or weight update to all replicas of the given handle.

    NOTE: This API is experimental and may later be promoted to a public API in
    Ray Serve directly. For now, it is only available in Ray LLM and is
    intended to enable control plane operations during RL training which is
    required when orchestrating trianing and inference loops.

    Args:
        handle: The DeploymentHandle to dispatch to.
        method_name: The name of the method to call on the deployment.
        args: The arguments to pass to the method. Can be a list/tuple of args,
              or a callable that takes the replica object and returns args.
        kwargs: The keyword arguments to pass to the method. Can be a dict,
                or a callable that takes the replica object and returns kwargs.
        combine: An optional callable that takes the list of results from all
            replicas and returns an aggregated result. If not provided, returns
            the list of results. The default combine function is to return the
            list of results.

    Returns:
        The result of the method call to all replicas. If combine is provided,
        returns the aggregated result. Otherwise, returns the list of results.
    N T)_run_router_in_separate_loopz:Timed out waiting for deployment replicas to be populated.g?z DeploymentHandle router is None._asyncio_router_request_routerz7Request router not initialized. No replicas accessible.serve)	namespacez"args must be a list or tuple, got zkwargs must be a dict, got z	dispatch-)
request_idinternal_request_idcall_method) is_initialized_inittimerunning_replicas_populated%DISPATCH_REPLICA_POPULATION_TIMEOUT_STimeoutErrorsleep_routerRuntimeErrorhasattrr   r   _replica_id_setlistto_full_id_strray	get_actor
ValueErrorcallable
isinstancetupletypedictuuiduuid4r	   pickledumpsappendhandle_requestremoteget)r   r   r   r   r   
start_timerouterrequest_routerreplica_setfuturesreplicasreplica
actor_nameactor_handle	call_argscall_kwargsr   dummy_rm
pickled_rmresultss                       z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/llm/_internal/serve/utils/dispatch.pydispatchrH      s   D |~  8
 	$777 J//11 9;;#&KKKL   	
3 //11  ^F~=>>> Nv()) 0/?	*	+	+ 0/TUUU 0K G K  H *
 *
++--
	=wGGGLL 	 	 	H	
 	D>> 	&WIF 	* &//K)dE]33 	US$y//SSTTT+t,, 	T "  !RtK?P?P!R!RSSS 0//
"! *#
 
 
 \(++
 	.L'.zUIUUUUU	
 	
 	
 	

 G #''""  wwNs   4E
EE)NNN)r3   r   r1   typingr   r   r   r   r   r   r)   ray.serve._private.commonr	   ray.serve.handler
   r    strrH   r       rG   <module>rN      sH      = = = = = = = = = = = = = = = = 



 5 5 5 5 5 5 - - - - - - )+ % .2EI48B BBB XseSj))
*B $sCx.(C5$sCx.+@"AAB	B
 hS	{C/01B 	B B B B B BrM   