
    &`i                     
   d Z 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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Zddlmc mZ ddlmc mZ ddlm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"m#Z# 	 ej$        Z$n# e%$ r
 ej&        Z$Y nw xY w ej'        e(          Z) e             Z* e             Z+ e	dg d          Z,ej-        ej.        hZ/ej0        ej1         eej2        d           fdZ3dede4fdZ5defdZ6defdZ7dS )zT
Optional utils module contains utility methods
that require optional dependencies.
    N)
namedtuple)CallableUnion)RequestResponse)env_bool) RAY_INTERNAL_DASHBOARD_NAMESPACE)aiohttphdrs)method_route_table_factoryrest_response)DashboardAgentModuleDashboardHeadModuleAiohttpCacheValue)data
expirationtaskFc                      dk    sJ t          j                     fd}t          j                   r }t          j          ||          S |S )Nr   c                      r_t          j                   dt          j        j        f fd            }d d d}|xj        |z  c_        |xj        |z  c_        |S  S )Nreturnc                  z  K   | d         }|j                             d          dk    r |   d {V S |j        t          v r|j        n!|j        |                                 d {V f                              }|e                               |j                                        r|j	        t          j
                    k    rt          j        j        di |j        S 	fd}t           |            }|                    |           || d {V S t          j        j        di |j        S )Nnocache1c                    	 |                                  }nA# t          $ r4 t          t          j        j        t          j                              }Y nw xY w|j        t          |j
                  |j        d}t          |t          j                    z   |           <                                  t                    k    r                    d           |S )N)status_codemessage)statusheadersbodyF)last)result	Exceptionr   dashboard_utilsHTTPStatusCodeINTERNAL_ERROR	traceback
format_excr   dictr   r    _AiohttpCacheValuetimemove_to_endlenpopitem)r   responser   cachekeymaxsizettl_secondss      p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/dashboard/optional_utils.py_update_cachezNaiohttp_cache.<locals>._wrapper.<locals>._cache_handler.<locals>._update_cacheW   s    #';;==$   #0(7(F(U$-$8$:$:$ $ $ #+/#'(8#9#9 ( D
 "4dikkK7" "E#J %%c***5zzG++5111#Os    ;AA )querygetmethod_AIOHTTP_CACHE_NOBODY_METHODSpath_qsreadr,   r   doner   r+   r
   webr   r   create_taskadd_done_callback)
argsreqvaluer5   r   r1   r0   handlerr2   r3   s
        @r4   _cache_handlerz7aiohttp_cache.<locals>._wrapper.<locals>._cache_handler?   sx      2h9==++s22!($/////// :!>>>+CC;chhjj(8(8(8(8(8(89C		#$%%c*** :??,, B0@DIKK0O0O&{3AAejAAA$ $ $ $ $ $ $ $* #77D>22&&}555=!%::::::%";/==%*===    z[cache ttl=z, max_size=])	functoolswrapsr
   r>   r   __name____qualname__)rD   rE   suffixr0   enabler2   r3   s   `  r4   _wrapperzaiohttp_cache.<locals>._wrapper<   s     ;	_W%%1>w{/C 1> 1> 1> 1> 1> 1> 1> 1> &%1>f F;EE7EEEF##v-##''61''!!NrF   )collectionsOrderedDictinspectiscoroutinefunctiondashboard_constsAIOHTTP_CACHE_TTL_SECONDS)r3   r2   rM   rN   target_funcr0   s   ```  @r4   aiohttp_cacherV   4   s    
 Q;;;;#%%E< < < < < < < <| ";// !&@x$$$rF   rB   r   c                       j                             dd                              d          pt           fddD                       S )a_  Best-effort detection if the request was made by a browser.

    Uses three heuristics:
        1) If the `User-Agent` header starts with 'Mozilla'. This heuristic is weak,
        but hard for a browser to bypass e.g., fetch/xhr and friends cannot alter the
        user agent, but requests made with an HTTP library can stumble into this if
        they choose to user a browser-like user agent. At the time of writing, all
        common browsers' user agents start with 'Mozilla'.
        2) If any of the `Sec-Fetch-*` headers are present.
        3) If any of the various CORS headers are present
    z
User-Agent Mozillac              3   *   K   | ]}|j         v V  d S )N)r   ).0hrB   s     r4   	<genexpr>z%is_browser_request.<locals>.<genexpr>   sF       J J 	
S[J J J J J JrF   )RefererOriginzSec-Fetch-ModezSec-Fetch-DestzSec-Fetch-SitezSec-Fetch-UserzAccess-Control-Request-MethodzAccess-Control-Request-Headers)r   r8   
startswithany)rB   s   `r4   is_browser_requestrb      sm     ;??<,,77	BB c J J J J
J J J G G rF   c                  *    dt           dt           fd} | S )z8Reject any requests that appear to be made by a browser.fr   c                 V     t          j                   dt          f fd            }|S )NrB   c                    K   t          |          r%t          dt          j        j        j                  S  | |           d {V S )NzBrowser requests not allowedtextr   )rb   r   r
   r>   HTTPMethodNotAllowedr   )selfrB   rd   s     r4   	decoratorzCdeny_browser_requests.<locals>.decorator_factory.<locals>.decorator   s`      !#&& 7";;G   
 4%%%%%%%rF   )rH   rI   r   rd   rk   s   ` r4   decorator_factoryz0deny_browser_requests.<locals>.decorator_factory   sE    				&w 	& 	& 	& 	& 	& 
		& rF   r   rm   s    r4   deny_browser_requestsrp      s,    X (     rF   c                  *    dt           dt           fd} | S )zDDecorator to be used on methods that require being connected to Ray.rd   r   c                 z     t          j                   dt          t          t          f         f fd            }|S )Nrj   c                 L  K   	 t          j                    s	 | j        }t                              d|            t          t          j                  t          j	        d<   t          j
        |ddt          d           n(# t          $ r}t          j                     |d d }~ww xY w | g|R i | d {V S # t          $ r]}t                              d|            t          t!          j                    t$          j        j        j                  cY d }~S d }~ww xY w)NzConnecting to ray with address=&RAY_gcs_server_request_timeout_secondsFT)addresslog_to_driverconfigure_logging	namespace_skip_env_hookzUnexpected error in handler: rg   )rayis_initializedgcs_addressloggerinfostrrS   GCS_RPC_TIMEOUT_SECONDSosenvironinitr	   r#   shutdown	exceptionr   r'   r(   r
   r>   HTTPInternalServerErrorr   )rj   rA   kwargsru   erd   s        r4   rk   zKinit_ray_and_catch_exceptions.<locals>.decorator_factory.<locals>.decorator   s~     )++ **"&"2$Og$O$OPPPOR,DP P
#KL
 $+*/.3&F+/     % * * *T)* Qt5d555f555555555     !D!D!DEEE"-//";>J        sB   B< A(B B< 
B'B""B''B< <
D#ADD#D#)rH   rI   r   r   r   rl   s   ` r4   rm   z8init_ray_and_catch_exceptions.<locals>.decorator_factory   sS    				,.AAB	 	 	 	 	 
		@ rF   rn   ro   s    r4   init_ray_and_catch_exceptionsr      s-    "X "( " " " "H rF   )8__doc__asynciorO   rH   rQ   loggingr   r+   r'   r   typingr   r   aiohttp.webr   r   rz   ray.dashboard.consts	dashboardconstsrS   ray.dashboard.utilsutilsr$   ray._private.ray_constantsr   ray._rayletr	   ray.dashboard.optional_depsr
   r   ray.dashboard.routesr   r   r   r   r?   AttributeErrorensure_future	getLoggerrJ   r}   DashboardHeadRouteTableDashboardAgentRouteTabler*   METH_GETMETH_DELETEr:   rT   AIOHTTP_CACHE_MAX_SIZE%AIOHTTP_CACHE_DISABLE_ENVIRONMENT_KEYrV   boolrb   rp   r   r6   rF   r4   <module>r      s               				      " " " " " " " " " " " " " " ) ) ) ) ) ) ) ) 



 / / / / / / / / / - - - - - - - - - / / / / / / 8 8 8 8 8 8
 6 5 5 5 5 5 5 5 J J J J J J J J       
(%KK ( ( ('KKK( 
	8	$	$4466 5577   Z 35S5S5STT !%0@ A  !:3x(NPUVVVK K K K\G     @x    &'x ' ' ' ' ' 's   4A< <B
B