
    &`i$,                        U d Z ddlZddl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 ddlZddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$  ej%        e          Z&da'de(d<   da)ee(d<   e$e G d d                                  Z*	 dAde+de+dee         fdZ,d Z-d Z.dddede	ded e"d!e/d"ee	e0e0gdf                  fd#Z1dBde+defd$Z2 ed%           G d& d'                      Z3 ej4        d(d)          Z5 ej4        d*d)          Z6d+ Z7d, Z8	 	 	 	 	 dCd.e0d/e0d0e0d1e0d2e0f
d3Z9d4 Z:d5ee3         fd6Z; ee<          Z=e
e0e
e0ej>        f         f         e(d7<   d8e0de
e0ej>        f         fd9Z?d8e0d:e0fd;Z@d8e0d:e0fd<ZA ee<          ZBe
e0e
e0ef         f         e(d=<   d> ZCd? ZDd@ ZEdS )Dz
This file stores global state for a Serve application. Deployment replicas
can use this state to access metadata or the Serve controller.
    N)defaultdict)	dataclass)CallableDictListOptional)RayActorError)ServeControllerClient)DeploymentID	ReplicaID)DeploymentConfig)SERVE_CONTROLLER_NAMESERVE_LOGGER_NAMESERVE_NAMESPACE)ReplicaResult)RayServeException)RayServegRPCContext)ReplicaRank)DeveloperAPIReplicaContext_INTERNAL_REPLICA_CONTEXT_global_clientc                       e Zd ZU dZeed<   eed<   eed<   eed<   e	ed<   dZ
eeegdf                  ed<   ed	efd
            Zed	efd            Zed	efd            ZdS )r   a  Stores runtime context info for replicas.

    Fields:
        - app_name: name of the application the replica is a part of.
        - deployment: name of the deployment the replica is a part of.
        - replica_tag: unique ID for the replica.
        - servable_object: instance of the user class/function this replica is running.
        - rank: the rank of the replica.
        - world_size: the number of replicas in the deployment.
    
replica_idservable_object_deployment_configrank
world_sizeN_handle_registration_callbackreturnc                 $    | j         j        j        S N)r   deployment_idapp_nameselfs    e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/serve/context.pyr$   zReplicaContext.app_name8   s    ,55    c                 $    | j         j        j        S r"   )r   r#   namer%   s    r'   
deploymentzReplicaContext.deployment<   s    ,11r(   c                     | j         j        S r"   )r   	unique_idr%   s    r'   replica_tagzReplicaContext.replica_tag@   s    ((r(   )__name__
__module____qualname____doc__r   __annotations__r   r   r   intr   r   r   propertystrr$   r+   r.    r(   r'   r   r   #   s         	 	 ((((
OOONR!8Hl^T5I,J#KRRR6# 6 6 6 X6 2C 2 2 2 X2 )S ) ) ) X) ) )r(   FT_health_check_controllerraise_if_no_controller_runningr    c                     	 t           >| r5t          j        t           j        j                                                   t           S n9# t          $ r, t                              d           t          d           Y nw xY wt          |          S )a  Gets the global client, which stores the controller's handle.

    Args:
        _health_check_controller: If True, run a health check on the
            cached controller if it exists. If the check fails, try reconnecting
            to the controller.
        raise_if_no_controller_running: Whether to raise an exception if
            there is no currently running Serve controller.

    Returns:
        ServeControllerClient to the running Serve controller. If there
        is no running controller and raise_if_no_controller_running is
        set to False, returns None.

    Raises:
        RayServeException: If there is no running Serve controller actor
            and raise_if_no_controller_running is set to True.
    Nz-The cached controller has died. Reconnecting.)r   rayget_controllercheck_aliveremoter	   loggerinfo_set_global_client_connect)r8   r9   s     r'   _get_global_clientrD   E   s    ,!%' I2>EEGGHHH!! &  ! ! !CDDD4     ! 2333s   AA 3A>=A>c                 
    | a d S r"   )r   )clients    r'   rB   rB   g   s    NNNr(   c                      t           S r"   )r   r7   r(   r'   _get_internal_replica_contextrH   l   s    $$r(   )handle_registration_callbackr   r   r   r   r   rI   c                 0    t          | |||||          ad S )N)r   r   r   r   r   r   )r   r   )r   r   r   r   r   rI   s         r'   _set_internal_replica_contextrK   p   s1     !/'-&B! ! !r(   c                 Z   dt           j        j        j        _        t          j                    st          j        t                     	 t          j        t          t                    }n"# t          $ r | rt          d          Y dS w xY wt          |          }t          |           |S )aO  Connect to an existing Serve application on this Ray cluster.

    If called from within a replica, this will connect to the same Serve
    app that the replica is running in.

    Returns:
        ServeControllerClient that encapsulates a Ray actor handle to the
        existing Serve application's Serve Controller. None if there is
        no running Serve controller actor and raise_if_no_controller_running
        is set to False.

    Raises:
        RayServeException: If there is no running Serve controller actor
            and raise_if_no_controller_running is set to True.
    F)	namespacez7There is no Serve instance running on this Ray cluster.N)r;   _privateworkerglobal_worker_filter_logs_by_jobis_initializedinitr   	get_actorr   
ValueErrorr   r
   rB   )r9   
controllerrF   s      r'   rC   rC      s    $ =BCL%9 ,?++++]#8OTTT

   ) 	#I   	 # F vMs   
 A+ +B
	B
)frozenc                       e Zd ZU dZeed<   dZeed<   dZeed<   dZeed<   dZ	eed<   dZ
ee         ed<   d	Zeed
<   d	Zeed<   dS )_RequestContext route
request_id_internal_request_idr$   multiplexed_model_idNgrpc_contextFis_http_requestcancel_on_parent_request_cancel)r/   r0   r1   r[   r6   r3   r\   r]   r$   r^   r_   r   r   r`   boolra   r7   r(   r'   rY   rY      s         E3OOOJ "#"""Hc "#"""26L(./666!OT!!!,1#T11111r(   rY   z'Serve internal request context variable)defaultz0Serve internal batching request context variablec                      t                                           &t                               t                                 t                                           S )zWGet the current request context.

    Returns:
        The current request context
    )_serve_request_contextr<   setrY   r7   r(   r'   _get_serve_request_contextrg      sB     !!##+""?#4#4555!%%'''r(   c                      t                                           t                               g            t                                           S )z7Get the list of request contexts for the current batch.)_serve_batch_request_contextr<   rf   r7   r(   r'    _get_serve_batch_request_contextrj      s:    #''))1$((,,,'++---r(   rZ   r[   r\   r]   r$   r^   c           	          t                      }t                              t          | p|j        |p|j        |p|j        |p|j        |p|j                             dS )z]Set the request context. If the value is not set,
    the current context value will be used.)r[   r\   r]   r$   r^   N)	rg   re   rf   rY   r[   r\   r]   r$   r^   )r[   r\   r]   r$   r^   current_request_contexts         r'   _set_request_contextrm      s     9::828!G%<%G!5 "<&;A!8!A!5 "<&;	
 	
 	

 
 
 
 
r(   c                  R    t                               t                                 dS )zUnset the request context.N)re   rf   rY   r7   r(   r'   _unset_request_contextro      s"    0011111r(   request_contextsc                 :    t                               |            dS )z5Add the request context to the batch request context.N)ri   rf   )rp   s    r'   _set_batch_request_contextrr      s     $$%566666r(   _requests_pending_assignmentparent_request_idc                 2    | t           v rt           |          S i S r"   rs   rt   s    r'    _get_requests_pending_assignmentrx     s    888+,=>>Ir(   response_idc                 &    |t           |          |<   d S r"   rv   )rt   ry   tasks      r'   _add_request_pending_assignmentr|     s     DH !23K@@@r(   c                     |t           |          v rt           |          |= t          t           |                    dk    r
t           | = d S d S Nr   )rs   lenrt   ry   s     r'   "_remove_request_pending_assignmentr   $  sT    23DEEE():;KH
'(9:;;q@@():;;; A@r(   _in_flight_requestsc                 2    | t           v rt           |          S i S r"   r   rw   s    r'   _get_in_flight_requestsr   ;  s    ///"#455Ir(   c                 &    |t           |          |<   d S r"   r   )rt   ry   replica_results      r'   _add_in_flight_requestr   B  s    :H)*;777r(   c                     |t           |          v rt           |          |= t          t           |                    dk    r
t           | = d S d S r~   )r   r   r   s     r'   _remove_in_flight_requestr   F  sS    )*;<<< 12;?
0122a77 1222 87r(   )FT)T)rZ   rZ   rZ   rZ   rZ   )Fr2   asynciocontextvarsloggingcollectionsr   dataclassesr   typingr   r   r   r   r;   ray.exceptionsr	   ray.serve._private.clientr
   ray.serve._private.commonr   r   ray.serve._private.configr   ray.serve._private.constantsr   r   r   !ray.serve._private.replica_resultr   ray.serve.exceptionsr   ray.serve.grpc_utilr   ray.serve.schemar   ray.util.annotationsr   	getLoggerr@   r   r3   r   r   rb   rD   rB   rH   r4   r6   rK   rC   rY   
ContextVarre   ri   rg   rj   rm   ro   rr   dictrs   Taskrx   r|   r   r   r   r   r   r7   r(   r'   <module>r      s    
       # # # # # # ! ! ! ! ! ! 1 1 1 1 1 1 1 1 1 1 1 1 



 ( ( ( ( ( ( ; ; ; ; ; ; = = = = = = = = 6 6 6 6 6 6         
 < ; ; ; ; ; 2 2 2 2 2 2 3 3 3 3 3 3 ( ( ( ( ( ( - - - - - -		,	-	-.2 + 2 2 2(,% , , , 
) ) ) ) ) ) )  )B TX4 4"4LP4#$4 4 4 4D  
% % % JN    )	
   #+8S#J4D+E"F   ($ $T $=R $ $ $ $l $2 2 2 2 2 2 2 2 0/-t     6{56      
	( 	( 	(. . .  " "   	
    22 2 2
7o1F 7 7 7 7 DO;tCTCT d3S',->(?#?@ T T T S',EV@W    Hs H H H H H<# <C < < < <  <G;t;L;L T#tC$6778 L L L  I I I3 3 3 3 3r(   