
    &`i.                     r   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mZmZmZ d dlZd dl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 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&m'Z'm(Z(  ej)        e          Z*de"defdZ+de"de,de&fdZ- G d de          Z. G d de          Z/dS )    N)wraps)AnyCallable	CoroutineDictOptionalTupleUnion)cloudpickle)DeploymentIDRequestMetadata) RAY_SERVE_RUN_SYNC_IN_THREADPOOLSERVE_LOGGER_NAME)UserCallableWrapper)ReplicaResult)Router))GENERATOR_COMPOSITION_NOT_SUPPORTED_ERROR)
Deployment)RequestCancelledError)DeploymentHandleDeploymentResponseDeploymentResponseGenerator
deploymentdeployment_idc                     d| j         v rt                              d| d           d| j         v r t                              d| d           d S d S )Nnum_gpuszDeployment zc has num_gpus configured. CUDA_VISIBLE_DEVICES is not managed automatically in local testing mode. runtime_envzL has runtime_env configured. runtime_envs are ignored in local testing mode.)ray_actor_optionsloggerwarning)r   r   s     y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/serve/_private/local_testing_mode.py_validate_deployment_optionsr"       s     Z111X- X X X	
 	
 	

 
444>- > > >	
 	
 	
 	
 	
 54    app_namereturnc           
      >   t          | j        |          }t          | |           t          | j        | j        | j        |t          dd| j                  	 t          
                    d| d                                                                                                | j        d           n-# t          $ r  t                              d| d            w xY wdt"          d	t           d
t$          dt&          ffd}t)          | j        ||          S )a  Constructs an in-process DeploymentHandle.

    This is used in the application build process for local testing mode,
    where all deployments of an app run in the local process which enables
    faster dev iterations and use of tooling like PDB.

    The user callable will be run on an asyncio loop in a separate thread
    (sharing the same code that's used in the replica).

    The constructor for the user callable is run eagerly in this function to
    ensure that any exceptions are raised during `serve.run`.
    T)r   run_sync_methods_in_threadpool run_user_code_in_separate_threadlocal_testing_modedeployment_configz%Initializing local replica class for .r   )rankz Failed to initialize deployment 	handle_idr   handle_optionsr%   c                 (    t          ||          S )N)r   r.   )LocalRouter)r-   r   r.   user_callable_wrappers      r!   _create_local_routerz:make_local_deployment_handle.<locals>._create_local_routerU   s%     !')
 
 
 	
r#   )_create_router)r   namer"   r   func_or_class	init_argsinit_kwargsr   _deployment_configr   infoinitialize_callableresultcall_reconfigureuser_config	Exception	exceptionstrr   r   r   )r   r$   r   r2   r1   s       @r!   make_local_deployment_handlerA   1   s_     !(;;M ];;;/ #'G)-$7	 	 	LMLLLMMM1133::<<<..z/EA.NNNN   LMLLLMMM

'3
EH
	
 
 
 
 
 
 +   s   A B9 9*C#c            
          e Zd ZdZ ed          Z ed          Zddddej        j	        de
d	ed
eej                 fdZedej	        fd            Zdeeef         fdZed             Zedee         fd            Zed             Zed             Zed             ZdefdZd Zdee         dej         fdZ!dej         fdZ"dej#        fdZ$dS )LocalReplicaResultz4ReplicaResult used by in-process Deployment Handles.zTConverting DeploymentResponses to ObjectRefs is not supported in local testing mode.z9Request rejection is not supported in local testing mode.FN)is_streaminggenerator_result_queuefuture
request_idrD   rE   c                    || _         d | _        || _        || _        || _        | j        r| j        J d            d S d S )Nz>generator_result_queue must be provided for streaming results.)_future_lazy_asyncio_future_request_id_is_streaming_generator_result_queue)selfrF   rG   rD   rE   s        r!   __init__zLocalReplicaResult.__init__p   se     $(!%) (>$ 	P,88O 988	P 	P88r#   r%   c                 Z    | j         t          j        | j                  | _         | j         S N)rJ   asynciowrap_futurerI   rN   s    r!   _asyncio_futurez"LocalReplicaResult._asyncio_future   s)    $,(/(;DL(I(ID%((r#   fc                      t                      fd            }t                      fd            }t          j                   r|S |S )Nc                     	  | g|R i |S # t           j        t          j        j        f$ r t	          | j                  w xY wrQ   rR   CancelledError
concurrentfuturesr   rK   rN   argskwargsrV   s      r!   wrapperz5LocalReplicaResult._process_response.<locals>.wrapper   sb    >q///////*J,>,MN > > >+D,<===>s	    5Ac                    K   	  | g|R i | d {V S # t           j        t          j        j        f$ r t	          | j                  w xY wrQ   rY   r]   s      r!   async_wrapperz;LocalReplicaResult._process_response.<locals>.async_wrapper   sx      >Qt5d555f555555555*J,>,MN > > >+D,<===>s	    5A)r   inspectiscoroutinefunction)rV   r`   rb   s   `  r!   _process_responsez$LocalReplicaResult._process_response   sw    	q	> 	> 	> 	> 
	> 
q	> 	> 	> 	> 
	> &q)) 	  Nr#   c                    K   | j         rQ   )REJECTION_NOT_SUPPORTED_ERRORrT   s    r!   get_rejection_responsez)LocalReplicaResult.get_rejection_response   s      00r#   	timeout_sc                     | j         r
J d            	 | j                            |          S # t          j        j        $ r t          d          w xY w)Nz3get() can only be called on a non-streaming result.timeoutzTimed out waiting for result.)rL   rI   r;   r[   r\   TimeoutErrorrN   ri   s     r!   getzLocalReplicaResult.get   s{     "	A 	A@	A 	A"	@<&&y&999!. 	@ 	@ 	@>???	@s	   . $Ac                 B   K   | j         r
J d            | j         d {V S )Nz9get_async() can only be called on a non-streaming result.)rL   rU   rT   s    r!   	get_asynczLocalReplicaResult.get_async   sI       "	G 	GF	G 	G" ))))))))r#   c                 `   | j         s
J d            	 | j                                        rR| j                                        r9| j                                        r| j                                        t          	 | j                            d          S # t          j	        $ r Y nw xY w)Nz0next() can only be called on a streaming result.T{Gz?rk   )
rL   rI   donerM   emptyr?   StopIterationro   queueEmptyrT   s    r!   __next__zLocalReplicaResult.__next__   s    !UU#UUU!
	|  "" (t'C'I'I'K'K (<))++ (,00222''3777EEE;   
	s   ?B B,+B,c                    K    j         s
J d             fd}t          j                                        t          j        |                    }t          j         j        |gt          j                   d {V \  }} j        	                                s j        
                                S  j                                        r j                                        t          )Nz1anext() can only be called on a streaming result.c                      	  j                                         s j                                        sd S t	          j        d           I)NTrs   )rI   rt   rM   ru   timesleeprT   s   r!   _wait_for_resultz6LocalReplicaResult.__anext__.<locals>._wait_for_result   sN    !<$$&& d.J.P.P.R.R F
4   !r#   )return_when)rL   rR   get_running_loopcreate_task	to_threadwaitrU   FIRST_COMPLETEDrM   ru   ro   r?   StopAsyncIteration)rN   r~   wait_for_result_taskrt   _s   `    r!   	__anext__zLocalReplicaResult.__anext__   s     !VV#VVV!
	! 	! 	! 	! 	!  '799EE.// 
  
  !#78/
 
 
 
 
 
 
 
 
a
 +1133 	6/33555))++ 	3&00222  r#   callbackc                 :    | j                             |           d S rQ   )rI   add_done_callback)rN   r   s     r!   r   z$LocalReplicaResult.add_done_callback   s    &&x00000r#   c                 8    | j                                          d S rQ   )rI   cancelrT   s    r!   r   zLocalReplicaResult.cancel   s    r#   c                     | j         rQ   OBJ_REF_NOT_SUPPORTED_ERRORrn   s     r!   to_object_refz LocalReplicaResult.to_object_ref       ..r#   c                    K   | j         rQ   r   rT   s    r!   to_object_ref_asyncz&LocalReplicaResult.to_object_ref_async   s      ..r#   c                     | j         rQ   r   rT   s    r!   to_object_ref_genz$LocalReplicaResult.to_object_ref_gen   r   r#   )%__name__
__module____qualname____doc__RuntimeErrorr   rg   r[   r\   Futurer@   boolr   rw   QueuerO   propertyrR   rU   r
   r   r   re   rh   floatro   rq   ry   r   r   r   ray	ObjectRefr   r   ObjectRefGeneratorr    r#   r!   rC   rC   e   s'       >>".,	!# # %1LC% %! #8<P P P")P 	P
 P !) 5P P P P* ) ) ) ) X)U8Y#67    ( 1 1 1 @Xe_ @ @ @ @ * * *    ! ! !81( 1 1 1 1  /x /3= / / / //3= / / / //3#9 / / / / / /r#   rC   c            
           e Zd ZdededefdZdefdZde	e         de
eef         de	e	e         e
eef         f         fd	Zd
edej        j        e         fdZd ZdS )r0   r1   r   r.   c                 N    || _         || _        | j        j        
J d            d S )Nz*User callable must already be initialized.)_deployment_id_user_callable_wrapper	_callable)rN   r1   r   r.   s       r!   rO   zLocalRouter.__init__   s7     ,&;#'1==7 >====r#   r%   c                     dS )NTr   rT   s    r!   running_replicas_populatedz&LocalRouter.running_replicas_populated  s    tr#   request_argsrequest_kwargsc                     dt           dt           fdt          j        t          j        t	          fd|D                       fd|                                D             f                    S )a!  Replace DeploymentResponse objects with their results.

        NOTE(edoakes): this currently calls the blocking `.result()` method
        on the responses to resolve them to their values. This is a divergence
        from the remote codepath where they're resolved concurrently.
        argr%   c                     t          | t                    r|                     d          }nt          | t                    rt          | }|S )NT)_skip_asyncio_check)
isinstancer   r;   r   r   )r   new_args     r!   _new_argz;LocalRouter._resolve_deployment_responses.<locals>._new_arg  sM    #122 ***>>C!<== ??Nr#   c              3   .   K   | ]} |          V  d S rQ   r   ).0r   r   s     r!   	<genexpr>z<LocalRouter._resolve_deployment_responses.<locals>.<genexpr>  s+      @@C((3--@@@@@@r#   c                 .    i | ]\  }}| |          S r   r   )r   kvr   s      r!   
<dictcomp>z=LocalRouter._resolve_deployment_responses.<locals>.<dictcomp>  s'    GGG1QGGGr#   )r   r   loadsdumpstupleitems)rN   r   r   r   s      @r!   _resolve_deployment_responsesz)LocalRouter._resolve_deployment_responses  s    	# 	# 	 	 	 	  @@@@<@@@@@GGGG0D0D0F0FGGG 
 
 	
r#   request_metac                    |                      ||          \  }}|j        r!t          j                    dt          ffd}nd d }|j        r| j                            ||||          }nB|j        r| j                            ||||          }n| j        	                    |||          }t          j                                        }|                    t          ||j        |j                             |S )Nitemc                 2                         |            d S rQ   )
put_nowait)r   rE   s    r!   generator_result_callbackz=LocalRouter.assign_request.<locals>.generator_result_callback/  s    &11$77777r#   )r   )enqueue)rG   rD   rE   )r   rD   rw   r   r   is_http_requestr   _call_http_entrypoint_call_user_generatorcall_user_methodr[   r\   r   
set_resultrC   rG   )rN   r   r   r   r   futnoop_futurerE   s          @r!   assign_requestzLocalRouter.assign_request"  sd    (,'I'I.(
 (
$n $ 	-%*[]]"8 8 8 8 8 8 8 8 &*"(,% ' 	-CC*C	 D  CC & 	-BB1	 C  CC ->> C
 !(//11'2)6'=	  	
 	
 	
 r#   c                 l    t           j                                        }|                    d            |S rQ   )r[   r\   r   r   )rN   r   s     r!   shutdownzLocalRouter.shutdownV  s0     (//11t$$$r#   N)r   r   r   r   r   r   rO   r   r   r	   r   r@   r   r   r[   r\   r   rC   r   r   r   r#   r!   r0   r0      s        
82
8 $
8 	
8 
8 
8 
8D    
!#J
8<S#X
	uSz4S>)	*
 
 
 
<2%2
 
		"#5	62 2 2 2h    r#   r0   )0rR   concurrent.futuresr[   rc   loggingrw   r|   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   ray.serve._private.commonr   r   ray.serve._private.constantsr   r   ray.serve._private.replicar   !ray.serve._private.replica_resultr   ray.serve._private.routerr   ray.serve._private.utilsr   ray.serve.deploymentr   ray.serve.exceptionsr   ray.serve.handler   r   r   	getLoggerr   r"   r@   rA   rC   r0   r   r#   r!   <module>r      sZ                  I I I I I I I I I I I I I I I I I I 



       C C C C C C C C        ; : : : : : ; ; ; ; ; ; , , , , , , N N N N N N + + + + + + 6 6 6 6 6 6          
	,	-	-


 
 
 
"111 1 1 1 1hL/ L/ L/ L/ L/ L/ L/ L/^e e e e e& e e e e er#   