
    &`i1                     &   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 d dlZd dl	m
Z
mZ d dlZd dlmc mZ d dlmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ  ej        e          Ze                     ej!                   d
 Z" G d de          Z#dS )    Nwraps)Optional)RequestResponse)ValidationError)CURRENT_VERSIONVersionResponse)SubprocessModule)SubprocessRouteTable)RayTaskErrorc                      d } | S )Nc                 <     t                      fd            }|S )Nc                 ~   K   	 ddl m} n!# t          $ r t          dd          cY S w xY w | g|R i | d {V S )Nr   servei  zLServe dependencies are not installed. Please run `pip install "ray[serve]"`.statustext)rayr   ImportErrorr   )selfargskwargsr   funcs       z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/dashboard/modules/serve/serve_head.pycheckz3validate_endpoint.<locals>.decorator.<locals>.check   s      	%%%%%%%   1      d4T444V444444444s    **r   )r   r   s   ` r   	decoratorz$validate_endpoint.<locals>.decorator   s3    	t	5 	5 	5 	5 
	5      )r   s    r   validate_endpointr!      s      " r   c                   n    e Zd Z fdZ ej        d          dedefd            Z ej        d           e	j
                     e            dedefd                                    Z ej        d           e	j
                    dedefd                        Z ej        d           e	j
                     e            dedefd	                                    Zd
edefdZ ej        d           e	j
                     e            dedefd                                    Zd Zd Z xZS )	ServeHeadc                     t                      j        |i | d | _        t          j                    | _        t          j                    | _        | j        J t          j	        j
                                        sJ d S )N)super__init___controllerasyncioLock_controller_lock_controller_start_lock
gcs_clientr   experimentalinternal_kv_internal_kv_initialized)r   r   r   	__class__s      r   r&   zServeHead.__init__0   sy    $)&))) ' '.lnn# ***+DDFFFFFFFr   z/api/ray/versionreqreturnc                    K   t          t          t          j        t          j        | j                  }t          t          j        t          j
        |                    dt          j        j        j                  S )N)versionray_version
ray_commitsession_nameapplication/json)r   content_typer   )r
   r	   r   __version__
__commit__r7   r   jsondumpsdataclassesasdictaiohttpwebHTTPOkstatus_code)r   r1   resps      r   get_versionzServeHead.get_versionA   sn       #~*	
 
 
 K.t4455+;%1
 
 
 	
r   z/api/serve/applications/c           	      n  K   ddl m}m} d }|j                            d          }|rb|                                }|                                }||vr+t          dd| dd                    |           d	          S  ||          }| 	                                 d {V }||
                                }	nX	 |j                            |
           d {V }	n5# t          j        j        $ r}
t          dd|
           cY d }
~
S d }
~
ww xY wt          t!          j        |	          d          S )Nr   )APITypeServeInstanceDetailsapi_type  zInvalid 'api_type' value: 'z'. Must be one of: z, z
text/plainr   r   r9   )source  zWFailed to get a response from the controller. The GCS may be down, please retry later: r   r8   )r   r9   )ray.serve.schemarG   rH   querygetlowerget_valid_user_valuesr   joinget_serve_controllerget_empty_schema_dictget_serve_instance_detailsremoter   
exceptionsr   r<   r=   )r   r1   rG   rH   rI   api_type_strapi_type_lowervalid_values
controllerdetailses              r   rV   z$ServeHead.get_serve_instance_detailsQ   s      	CBBBBBBB&*y}}Z00 	/)//11N"88::L\11El E E+/99\+B+BE E ".    w~..H4466666666
*@@BBGG * E L L# !M ! !       >. 
 
 
  HDEH H        	
 G$$+
 
 
 	
s   =!C D3DDDc                    K   ddl m} |                                  d {V |                                 t	                      S )Nr   r   )r   r   rT   shutdownr   )r   r1   r   s      r   delete_serve_applicationsz#ServeHead.delete_serve_applications   sX       	**,,,,,,,,8NNzzr   c                   K   ddl m}m} ddlm} ddlm} ddlm} 	 |	                    |
                                 d {V           }n5# t          $ r(}t          dt          |                    cY d }~S d }~ww xY w|j                                        }	|                    |j                  }
t          d|
ifi |	}|j                                        }| j        4 d {V   ||||j        	           d {V }d d d           d {V  n# 1 d {V swxY w Y   |                     ||           	 |j        r|                    |j                   |                    |            ||j        d
           t                      S # t2          $ r(}t          dt5          |                    cY d }~S d }~ww xY w)Nr   )TagKeyrecord_extra_usage_tag)serve_start_async)ProxyLocation)ServeDeploySchemarJ   r   location)http_optionsgrpc_optionsglobal_logging_configv2)ray._common.usage.usage_librc   rd   ray.serve._private.apire   ray.serve.configrf   rN   rg   	parse_objr<   r   r   reprri   dict_to_deployment_modeproxy_locationrj   r+   logging_configvalidate_http_optionsupdate_global_logging_configdeploy_appsSERVE_REST_API_VERSIONr   str)r   r1   rc   rd   re   rf   rg   configr^   config_http_optionsrh   full_http_optionsrj   clients                 r   put_all_applicationszServeHead.put_all_applications   s      	ONNNNNNN<<<<<<222222666666	(9(C(C#((**DTDTDTDTDTDT(U(UFF 	 	 	!WW        	 %16688 44V5JKK *h!7OO;NOO*//11. 	 	 	 	 	 	 	 	,,.)&,&;        F	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	""6+<===	$ K33F4IJJJv&&&""6#@$GGG ::  	 	 	VV        	sN   -A 
A>A93A>9A>)D
D"D=AF 
GF?9G?Gr   c                 J    t          |t          j        |          d          S )z6Create a JSON response with the given data and status.r8   rK   )r   r<   r=   )r   datar   s      r   _create_json_responsezServeHead._create_json_response   s,    D!!+
 
 
 	
r   zK/api/v1/applications/{application_name}/deployments/{deployment_name}/scalec           	        K   ddl m} ddlm}m} ddlm} |j                            d          }|j                            d          }|r|s| 	                    ddid	          S 	 |
                                 d {V } |di |}	n?# t          $ r2}
| 	                    dd
t          |
           id	          cY d }
~
S d }
~
ww xY w|                                  d {V }|| 	                    ddid          S 	  |||          }t                              d| d| d|	j         d           |j                            ||	j                   d {V  | 	                    ddid          S # t          $ r}
t'          |
|          r| 	                    ddid          cY d }
~
S t'          |
|          r/| 	                    dt          |
j                  id          cY d }
~
S t'          |
t*                    r.dt          |
          v r| 	                    ddid	          cY d }
~
S t                              d|
            | 	                    ddid          cY d }
~
S d }
~
ww xY w)Nr   )DeploymentID)DeploymentIsBeingDeletedErrorExternalScalerDisabledError)ScaleDeploymentRequestapplication_namedeployment_nameerrorz3Missing application_name or deployment_name in pathrJ   zInvalid request body: z!Serve controller is not availablerM   )nameapp_namezScaling deployment z, application z to z	 replicasmessagezDScaling request received. Deployment will get scaled asynchronously.   zDeployment is deletedi  z	not foundz#Application or Deployment not foundz>Got an Internal Server Error while scaling deployment, error: zInternal Server Errorr    )ray.serve._private.commonr   ray.serve._private.exceptionsr   r   rN   r   
match_inforP   r   r<   	Exceptionrz   rT   loggerinfotarget_num_replicasupdate_deployment_replicasrW   
isinstancecause
ValueErrorr   )r   r1   r   r   r   r   r   r   request_datascale_requestr^   r\   deployment_ids                r   scale_deploymentzServeHead.scale_deployment   s      	;:::::	
 	
 	
 	
 	
 	
 	
 	
 	<;;;;; >--.@AA.,,->?? 	 	--OPRU  	!$++++++L22BB\BBMM 	 	 	--;3q66;;<c       	
  4466666666
--=>  &	(L$/?  M
 KK Ho  H  HEU  H  H[h[|  H  H  H   7>>}@         --e 	    	 	 	!:;; P 1156        A:;; P117CLL2I3OOOOOOOO!Z(( 
[CFF-B-B11CDc        XUVXX   1156       #	sa   ("B 
C'C<CC?A3E3 3
I+='I&$I+*9I&#I+)=I&&I+,4I& I+&I+c                     g }|                                 D ]5\  }}t          |j        |          }||k    r|                    |           6|r t                              d| d           d S d S )NzServe is already running on this Ray cluster and it's not possible to update its HTTP options without restarting it. Following options are attempted to be updated: .)itemsgetattrhttp_configappendr   warning)r   r~   ri   divergent_http_optionsoption	new_value
prev_values          r   rv   zServeHead.validate_http_options  s    !#!-!3!3!5!5 	6 	6FI !3V<<JY&&&--f555! 	NN6 36 6 6    	 	r   c                 ,  K   | j         4 d{V  | j        y	 | j        j                                         d{V  | j        cddd          d{V  S # t          j        j        $ r t                              d           Y nw xY wd| _        ddl	m
}m} 	 t	          j        ||          | _        n4# t          $ r'}t                              d|            Y d}~nd}~ww xY w| j        cddd          d{V  S # 1 d{V swxY w Y   dS )zGets the ServeController to the this cluster's Serve app.

        return: If Serve is running on this Ray cluster, returns a client to
            the Serve controller. If Serve is not running, returns None.
        NzController is deadr   )SERVE_CONTROLLER_NAMESERVE_NAMESPACE)	namespacezhThere is no instance running on this Ray cluster. Please call `serve.start(detached=True) to start one: )r*   r'   check_aliverW   r   rX   RayActorErrorr   r   ray.serve._private.constantsr   r   	get_actorr   debug)r   r   r   r^   s       r   rT   zServeHead.get_serve_controller$  sR      ( 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$+6*6==?????????+		$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$
 ~3 6 6 6KK 4555556#' 
       
 $'=)_$ $ $                   #?	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$sX   D*A.BDBDB65D6
C' C"D"C''	D
DD)__name__
__module____qualname__r&   routesrP   r   r   rE   dashboard_optional_utilsinit_ray_and_catch_exceptionsr!   rV   deletera   putr   intr   postr   rv   rT   __classcell__)r0   s   @r   r#   r#   /   sR       G G G G G" VZ"##
W 
 
 
 
 $#
 VZ*++;;==0
G 0
 0
 0
 0
  >= ,+0
d V]-..;;==7 x    >= /. VZ*++;;==*g *( * * *  >= ,+*X
# 
( 
 
 
 
 V[U  <;==F' Fh F F F  >= 
FP   %$ %$ %$ %$ %$ %$ %$r   r#   )$r(   r>   r<   logging	functoolsr   typingr   r@   aiohttp.webr   r   r   ray.dashboard.optional_utils	dashboardoptional_utilsr   ray._common.pydantic_compatr   ray.dashboard.modules.versionr	   r
   !ray.dashboard.subprocesses.moduler   !ray.dashboard.subprocesses.routesr   r   ray.exceptionsr   	getLoggerr   r   setLevelINFOr!   r#   r    r   r   <module>r      si                       ) ) ) ) ) ) ) ) 



 ? ? ? ? ? ? ? ? ? 7 7 7 7 7 7 J J J J J J J J > > > > > > L L L L L L ' ' ' ' ' '		8	$	$      0Z$ Z$ Z$ Z$ Z$  Z$ Z$ Z$ Z$ Z$r   