
    i9                        U 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mZm	Z	m
Z
mZ d dlZd dlZd dlZd dlZd dlmZmZ d dlZd dlmZ d dlmZ i e j0                  ej2                  ej4                  ej2                  ej6                  ej8                  ej:                  ej<                  ej>                  ej@                  ejB                  ejD                  ejF                  ejD                  ejH                  ejD                  ejJ                  ejL                  ejN                  ejL                  ejP                  ejR                  ejT                  ejR                  ejV                  ejX                  ejP                  ejR                  ejZ                  ejR                  ej\                  ej\                  ej^                  ej^                  ej`                  ejb                  iZ2e	e3d<   	 d dlZejR                  e2ejh                  jP                  <   ejl                  dejn                  d   fd       Z8 G d	 d
ejr                        Z: G d dejv                        Z< G d de<      Z=y# e5$ r Y _w xY w)    N)AnyCallableDictOptionalUnion)ClientResponseClientSession)verbose_logger)str_to_boolAIOHTTP_EXC_MAPreturnc               #      K   	 d  y # t         $ rX} d }t        j                         D ]#  \  }}t        | |      s|t	        ||      s"|}% | t        |       } ||      | d } ~ ww xY wwN)	Exceptionr   items
isinstance
issubclassstr)exc
mapped_excfrom_excto_excmessages        |/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/llms/custom_httpx/aiohttp_transport.pymap_aiohttp_exceptionsr   :   s|     + +
 / 5 5 7Hfc8,!Z
%C#
	 !8 c(!s*+s*   A-	 A-	A*7A%
A%%A**A-c                   N    e Zd ZdZdeddfdZdej                  e   fdZ	ddZ
y)	AiohttpResponseStreami @  aiohttp_responser   Nc                     || _         y r   )_aiohttp_response)selfr   s     r   __init__zAiohttpResponseStream.__init__Q   s
    !1    c                X  K   	 | j                   j                  j                  | j                        2 3 d {   }| 7 
6 y # t        j
                  t        j                  j
                  f$ r"}t        j                  d|        Y d }~y d }~wt        $ r-}dt        |      v rt        j                  d       Y d }~y  d }~wt        j                  j                  $ r"}t        j                  d|        Y d }~y d }~wt        $ r t               5   # 1 sw Y   Y y xY ww xY ww)Nz%Transfer incomplete, but continuing: zConnection closedz@Upstream closed streaming connection; ending iterator gracefullyz)Transfer encoding error, but continuing: )r    contentiter_chunked
CHUNK_SIZEaiohttpClientPayloadErrorclient_exceptionsr
   debugRuntimeErrorr   http_exceptionsTransferEncodingErrorr   r   )r!   chunkes      r   	__aiter__zAiohttpResponseStream.__aiter__T   s    !	#55==JJ  e   
 &&%%88
 		   #H!LM  
	
 #c!f,$$V &&<< 	  #LQC!PQ 	') *)	s   D*/A AAAA AA D*-D'3BD*D'"C>D*C D'$D<D*D'DD#	D'!D*#D''D*c                    K   t               5  | j                  j                  d d d        d {    d d d        y 7 # 1 sw Y   y xY wwr   )r   r    	__aexit__r!   s    r   aclosezAiohttpResponseStream.aclosex   s>     #%((224tDDD &%D &%s%   A
!><>	A
>AA
r   N)__name__
__module____qualname__r'   r   r"   typingAsyncIteratorbytesr1   r5    r#   r   r   r   N   s8    J2 2D 2"!5!5e!< "HEr#   r   c                   @    e Zd Z	 ddeeeg ef   f   deddfdZddZy)	AiohttpTransportclientowns_sessionr   Nc                 .    || _         || _        i | _        y r   )r@   _owns_sessionproxy_cache)r!   r@   rA   s      r   r"   zAiohttpTransport.__init__~   s    
 )
 68r#   c                    K   | j                   r>t        | j                  t              r#| j                  j	                          d {    y y y 7 wr   )rC   r   r@   r	   closer4   s    r   r5   zAiohttpTransport.aclose   s>     *T[[-"H++##%%% #I%s   AAAA)Tr6   )	r7   r8   r9   r   r	   r   boolr"   r5   r=   r#   r   r?   r?   }   sA     "8mXb-.?%@@A8 8 
	8&r#   r?   c                   |    e Zd ZdZ	 	 ddeeeg ef   f   deeee	j                  f      def fdZdefdZ	 dded	ej                  d
edee   dee   deeee	j                  f      defdZd	ej                  dej&                  fdZd	ej                  fdZdej,                  dej                  e   fdZ xZS )LiteLLMAiohttpTransportz
    LiteLLM wrapper around AiohttpTransport to handle %-encodings in URLs
    and event loop lifecycle issues in CI/CD environments

    Credit to: https://github.com/karpetrosyan/httpx-aiohttp for this implementation
    r@   
ssl_verifyrA   c                 j    || _         || _        t        |   ||       t	        |      r|| _        y y )N)r@   rA   )r@   _ssl_verifysuperr"   callable_client_factory)r!   r@   rJ   rA   	__class__s       r   r"   z LiteLLMAiohttpTransport.__init__   s;     %\BF#)D  r#   r   c                    ddl m} t        | j                  |      sCt	        | d      r+t        | j                        r| j                         | _        n |       | _        | j                  j                  rot        j                  d       t	        | d      r6t        | j                        r!| j                         | _        | j                  S  |       | _        | j                  S 	 t        | j                  dd      }t        j                         }|||k7  s|j                         r| j                  }	 |j                  s$	 t        j                  |j                                t	        | d      r6t        | j                        r!| j                         | _        | j                  S  |       | _        | j                  S # t         $ r t        j                  d       Y zw xY w# t"        $ r"}t        j                  d|        Y d}~d}~ww xY w# t         t$        f$ rQ t	        | d      r+t        | j                        r| j                         | _        n |       | _        Y | j                  S w xY w)	z
        Helper to get a valid ClientSession for the current event loop.

        This handles the case where the session was created in a different
        event loop that may have been closed (common in CI/CD environments).
        r   )r	   rO   z'Session is closed, creating new session_loopNz.Old session from different loop, relying on GCzError closing old session: )aiohttp.clientr	   r   r@   hasattrrN   rO   closedr
   r+   getattrasyncioget_running_loop	is_closedcreate_taskrF   r,   r   AttributeError)r!   r	   session_loopcurrent_loopold_sessionr0   s         r   _get_valid_client_sessionz1LiteLLMAiohttpTransport._get_valid_client_session   s    	1 $++}5t./HT=Q=Q4R"224+o ;;  !JKt./HT=Q=Q4R"224 ;; ,o;;#	."4;;>L"335L $</))+ #kk
L&--#//0A0A0CD 4!23AUAU8V"&"6"6"8DK {{ #0/DK {{+  , *00 P
 ! L"((+Fqc)JKKL n- 	.t./HT=Q=Q4R"224+o{{	.sb   &AH 5G# #F? %6H 'H ?G G# G  G# #	H,H	H 	HH AI10I1client_sessionrequesttimeoutproxysni_hostnamec                   K   ddl m} ddlm} 	 |j                  }	|j                   |t        |j                        d      |j                  |	dd ||j                  d	      |j                  d
      |j                  d            ||d	}
|||
d<    |j                  di |
j                          d{   }|S # t
        j                  $ r+ |j                  }	|j                  j                  dd       Y w xY w7 Gw)a  
        Helper function to make an aiohttp request with the given parameters.

        Args:
            client_session: The aiohttp ClientSession to use
            request: The httpx Request to send
            timeout: Timeout settings dict with 'connect', 'read', 'pool' keys
            proxy: Optional proxy URL
            sni_hostname: Optional SNI hostname for SSL
            ssl_verify: Optional SSL verification setting (False to disable, SSLContext for custom)

        Returns:
            ClientResponse from aiohttp
        r   )ClientTimeout)URLztransfer-encodingNT)encodedFconnectreadpool)sock_connect	sock_readri   )	methodurlheadersdataallow_redirectsauto_decompressrb   rc   server_hostnamesslr=   )r(   rf   yarlrg   r%   httpxRequestNotReadstreamrp   poprn   r   ro   getra   
__aenter__)r!   r`   ra   rb   rc   rd   rJ   rf   YarlURLrq   request_kwargsresponses               r   _make_aiohttp_requestz-LiteLLMAiohttpTransport._make_aiohttp_request   s     . 	*'	;??D nn3w{{+T:$$$$[[3!++f-F+
 +*
 !$.N5!///A.ALLNN7 ## 	;>>DOO 3T:	;2 Os4   DB> BD7C?8D>;C<9D;C<<Dc           
      r  K   |j                   j                  di       }|j                   j                  d      }| j                         }| j                  |       d {   }| j                  }	 t               5  | j                  ||||||       d {   }d d d        t!        j"                  j$                  |j&                  t)        |      |      S 7 y7 D# 1 sw Y   CxY w# t        $ r}dt        |      v rt        j                  d|        t        | d      r+t        | j                        r| j                         | _        nt               | _        | j                  }t               5  | j                  ||||||       d {  7  }d d d        n# 1 sw Y   nxY w Y d }~d }~ww xY ww)Nrb   rd   )r`   ra   rb   rc   rd   rJ   zSession is closedz:Session closed during request, retrying with new session: rO   )status_coderp   ry   ra   )
extensionsr{   r_   _get_proxy_settingsrL   r   r   r,   r   r
   r+   rT   rN   rO   r@   r	   rw   Responsestatusrp   r   )	r!   ra   rb   rd   r`   rc   
ssl_configr   r0   s	            r   handle_async_requestz,LiteLLMAiohttpTransport.handle_async_request!  s     $$((B7))--n= 779 ..w77 %%
#	')!%!;!;#1##!-) "< "  *H ~~ $$(2	
 	
U 8 *)  	"c!f,$$PQRPST 4!23AUAU8V"&"6"6"8DK"//DK!% ,-%)%?%?'5 ' '#%1#- &@ &    H .-- 3	s   AF7CF70
C( :CCCC( "7F7CC%!C( (	F41BF/2FFF	F/F%	!F/)F7/F44F7c                 
  K   d }t         j                  s=t        t        j                  dd            s	 | j                  |j                        }|S |S # t        $ r#}t        j                  d|        Y d }~|S d }~ww xY ww)NDISABLE_AIOHTTP_TRUST_ENVFalsezError reading proxy env: )
litellmdisable_aiohttp_trust_envr   osgetenv_proxy_from_envro   r   r
   r+   )r!   ra   rc   r0   s       r   r   z+LiteLLMAiohttpTransport._get_proxy_settings]  s~     --299%@'JKF,,W[[9 u  F$$'@%DEEFs.   2BA B	B A;5B;B  Bro   c                    |j                   }|| j                  v r| j                  |   S t        j                  j	                         }t        j                  j                  |j                         rd}n;|j                  |j                        xs |j                  d      }|r	d|vrd| }|}|| j                  |<   |S )z
        Return proxy URL from env for the given request URL

        Only check the proxy env settings once, this is a costly operation for CPU % usage

        .Nallz://zhttp://)hostrD   urllibra   
getproxiesproxy_bypassr{   scheme)r!   ro   proxy_cache_keyproxies	proxy_urlrc   s         r   r   z'LiteLLMAiohttpTransport._proxy_from_envj  s     ((d...##O44..++->>&&sxx0IKK

+Aw{{5/AEe+!%)I,5)r#   )NTr   )r7   r8   r9   __doc__r   r	   r   r   rG   ru   
SSLContextr"   r_   rw   Requestdictr   r   r   r   r   r   rg   r:   r   __classcell__)rP   s   @r   rI   rI      s'    =A!	*mXb-.?%@@A* U4#789* 	*A= AT =A7%7 7 	7
 }7 sm7 U4#7897 
7r:
:
 
:
x 599 1E r#   rI   )>rW   
contextlibr   ru   r:   urllib.requestr   r   r   r   r   r   r(   aiohttp.client_exceptionsaiohttp.http_exceptionsrw   rS   r   r	   r   litellm._loggingr
   litellm.secret_managers.mainr   TimeoutErrorTimeoutExceptionServerTimeoutErrorConnectionTimeoutErrorConnectTimeoutSocketTimeoutErrorReadTimeoutClientProxyConnectionError
ProxyErrorClientConnectorCertificateErrorProtocolErrorClientSSLErrorServerFingerprintMismatchClientConnectorErrorConnectErrorClientOSErrorr)   	ReadErrorServerDisconnectedErrorClientConnectionErrorNetworkErrorContentTypeErrorTooManyRedirects
InvalidURLClientErrorRequestErrorr   __annotations__r*   ImportErrorcontextmanagerIteratorr   AsyncByteStreamr   AsyncBaseTransportr?   rI   r=   r#   r   <module>r      s     	 
   7 7      8  + 4 %00  6 6	
 ""E$8$8  1 1 &&(8(8 ++U-@-@ E// %%u':':   %"4"4 5--  !$ ##U__%( !!5#5#5)* +, eoo-. e44/2 ((36 ++7 >	$DIOOOG--@@A
 + 5 + +&,EE11 ,E^&u// &&t. tu  		s   7'I6 6I>=I>