
    &`i                     b    d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	  G d de          Z
dS )    N)aiohttp)BaseRouteTable)SubprocessModuleHandle)ResponseTypec                       e Zd ZdZ ej        e          Zej	        
                                Zedd            Zeej        fdefd            ZdS )	SubprocessRouteTablea  
    A route table to bind http route to SubprocessModuleHandle and SubprocessModule.

    This class is used in cls object: all the decorator methods are @classmethod, and
    the routes are binded to the cls object.

    Note we have 2 handlers:
    1. the child side handler, that is `handler` that contains real logic and
        is executed in the child process. It's added with __route_method__ and
        __route_path__ attributes. The child process runs a standalone aiohttp
        server.
    2. the parent side handler, that just sends the request to the
        SubprocessModuleHandle at cls._bind_map[method][path].instance.

    With modifications:
    - __route_method__ and __route_path__ are added to both side's handlers.
    - method and path are added to self._bind_map.

    Lifecycle of a request:
    1. Parent receives an aiohttp request.
    2. Router finds by [method][path] and calls parent_side_handler.
    3. `parent_side_handler` bookkeeps the request with a Future and sends a
            request to the subprocess.
    4. Subprocesses receives the response and sends it back to the parent.
    5. Parent responds to the aiohttp request with the response from the subprocess.
    instancer   c                     d }t          j        |j        |          }|D ]'\  }}|| j        |j                 |j                 _        (d S )Nc                 n    t          j        |           r t          | d          ot          | d          S dS )N__route_method____route_path__F)inspect
isfunctionhasattr)os    u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/dashboard/subprocesses/routes.py	predicatez,SubprocessRouteTable.bind.<locals>.predicate-   s<    !!$$ Wq"455V'!EU:V:VV5    )r   
getmembers
module_cls	_bind_mapr   r   r	   )clsr	   r   handler_routes_hs         r   bindzSubprocessRouteTable.bind)   sf    	 	 	
 !+H,?KK" 	T 	TDAqKSCM!,-a.>?HH	T 	Tr   	resp_typec                       fd}|S )zR
        Register a route to the module and return the decorated handler.
        c                    j                  v r5j                           }t          d d|j         d|j                                       | j        j        | j        j        d           }|j                  <   dt          j	        j
        dt          j	        j        ffd}| _        | _        |_        |_        | j        |_         j                                      |           | S )NzDuplicated route path: z, previous one registered at :requestreturnc                 t   K   j                           }|j        }|                    |            d {V S )N)r   r	   proxy_request)r!   	bind_infosubprocess_module_handler   methodpathr   s      r   parent_side_handlerzSSubprocessRouteTable._register_route.<locals>._wrapper.<locals>.parent_side_handlerM   sK        M&1$7	+4+=(5CCGYWWWWWWWWWr   )r   	Exceptionfilenamelineno	_BindInfo__code__co_filenameco_firstlinenor   webRequestResponser   r   __name___routesroute)handlerr%   r)   r   r'   r(   r   s      r   _wrapperz6SubprocessRouteTable._register_route.<locals>._wrapper>   sN   s}V,,,M&1$7	?d ? ? )? ?,5,<? ?    ,g.>.Mt I +4CM&!$'X ,X%X X X X X X X X X (.G$%)G"39015.+2+;(+CKfd++,?@@@Nr    )r   r'   r(   r   kwargsr8   s   ````  r   _register_routez$SubprocessRouteTable._register_route6   s6     	  	  	  	  	  	  	  	D r   N)r	   r   )r4   
__module____qualname____doc__collectionsdefaultdictdictr   r   r1   RouteTableDefr5   classmethodr   r   HTTPr;   r9   r   r   r   r   
   s         6 ('--Ik''))G
T 
T 
T [
T 5A5F) )&2) ) ) [) ) )r   r   )r?   r   ray.dashboard.optional_depsr   ray.dashboard.routesr   !ray.dashboard.subprocesses.handler    ray.dashboard.subprocesses.utilsr   r   r9   r   r   <module>rI      s         / / / / / / / / / / / / D D D D D D 9 9 9 9 9 9V V V V V> V V V V Vr   