
    AHj\]                         d 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
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mZmZmZ dd	lmZmZmZmZmZm Z  dd
l!m"Z"  G d d      Z# e#       Z$y)zz
HTTP Handler for Interactions API requests.

This module handles the HTTP communication for the Google Interactions API.
    )AnyAsyncIterator	CoroutineDictIteratorOptionalUnionN)request_timeout) InteractionsAPIStreamingIterator$SyncInteractionsAPIStreamingIterator)Logging)BaseInteractionsAPIConfig)AsyncHTTPHandlerHTTPHandler_get_httpx_clientget_async_httpx_client)CancelInteractionResultDeleteInteractionResultInteractionInput$InteractionsAPIOptionalRequestParamsInteractionsAPIResponse InteractionsAPIStreamingResponse)GenericLiteLLMParamsc            (       $   e Zd ZdZdededefdZ	 	 	 	 	 	 	 	 	 d!deded	ed
e	de
dee   dee   dee   deeeef      deeeef      deeeej$                  f      dee   dedee   deeee   eeeeeee   f   f   f   fdZ	 	 	 	 	 	 	 	 d"deded	ed
e	de
dee   dee   dee   deeeef      deeeef      deeeej$                  f      dee   dee   deeee   f   fdZdej:                  dee   de
dedef
dZdej:                  dee   de
dede f
dZ!	 	 	 	 d#deded	ed
e	de
deeeef      deeeej$                  f      dee   dedeeeeeef   f   fdZ"	 	 	 d$deded	ed
e	de
deeeef      deeeej$                  f      dee   defdZ#	 	 	 	 d#deded	ed
e	de
deeeef      deeeej$                  f      dee   dedee$eeee$f   f   fdZ%	 	 	 d$deded	ed
e	de
deeeef      deeeej$                  f      dee   de$fdZ&	 	 	 	 d#deded	ed
e	de
deeeef      deeeej$                  f      dee   dedee'eeee'f   f   fdZ(	 	 	 d$deded	ed
e	de
deeeef      deeeej$                  f      dee   de'fd Z)y)%InteractionsHTTPHandlerz5
    HTTP handler for Interactions API requests.
    eprovider_configreturnc                     t        |t        j                        r_|j                  j                  }|j                  j
                  }t        |j                  j                        }|j                  |||      S |S )z!Handle errors from HTTP requests.)error_messagestatus_codeheaders)	
isinstancehttpxHTTPStatusErrorresponsetextr!   dictr"   get_error_class)selfr   r   r    r!   r"   s         Y/root/tools/cai/cai_env/lib/python3.12/site-packages/litellm/interactions/http_handler.py_handle_errorz%InteractionsHTTPHandler._handle_error1   sl     a../JJOOM**00K1::--.G"22+' 3  
     Ninteractions_api_configoptional_paramscustom_llm_providerlitellm_paramslogging_objmodelagentinputextra_headers
extra_bodytimeoutclient	_is_asyncstreamc                    |r| j                  |||||||||	|
||      S |t        d|j                  dd      i      }n|}|j                  |	xs i |xs d|      }|j	                  |j
                  xs d||t        |      |      }|j                  ||||||      }|
r|j                  |
       |j                  |d|||d	
       	 |r3|j                  ||||xs t        d      }| j                  ||||      S |j                  ||||xs t              }	 |j                  |||      S # t        $ r}| j                  ||      d}~ww xY w)z
        Create a new interaction (synchronous or async based on _is_async flag).

        Per Google's OpenAPI spec, the endpoint is POST /{api_version}/interactions
        )r3   r4   r5   r.   r/   r0   r1   r2   r6   r7   r8   r;   N
ssl_verifyparams r"   r3   r1   api_baser3   r4   r1   r;   r3   r4   r5   r/   r1   r"   complete_input_dictrC   r"   r5   api_keyadditional_argsTurlr"   jsonr8   r;   r&   r3   r2   r.   rK   r"   rL   r8   r   r   r3   raw_responser2   )async_create_interactionr   getvalidate_environmentget_complete_urlrC   r(   transform_requestupdatepre_callpostr
   _create_sync_streaming_iterator	Exceptionr,   transform_response)r*   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   sync_httpx_clientr"   rC   datar&   r   s                        r+   create_interactionz*InteractionsHTTPHandler.create_interactionF   s   @ 00(? /$7-'+% 1   > 1$n&8&8t&LM! !')>>!'R+2) ? 
 +;;#,,2/ < 
 '88+) 9 
 KK
# 	'+$" 	 	
	S,11 ##6 2  ;;% +,C	 <   -11 ##6	 2  '99!# : 
 	
  	S$$q:Q$RR	Ss   4D6 D6 6	E?EEc                   K   |3t        t        j                  |      d|j                  dd      i      }n|}|j	                  |	xs i |xs d|      }|j                  |j                  xs d||t        |      |      }|j                  ||||||      }|
r|j                  |
       |j                  |d|||d	       	 |r;|j                  ||||xs t        d
       d{   }| j                  ||||      S |j                  ||||xs t               d{   }	 |j!                  |||      S 7 S7 # t        $ r}| j                  ||      d}~ww xY ww)z;
        Create a new interaction (async version).
        Nr=   llm_providerr?   r@   rA   rB   rD   rE   rG   TrJ   rM   rN   rO   rP   )r   litellmLlmProvidersrS   rT   rU   rC   r(   rV   rW   rX   rY   r
    _create_async_streaming_iteratorr[   r,   r\   )r*   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r;   async_httpx_clientr"   rC   r^   r&   r   s                       r+   rR   z0InteractionsHTTPHandler.async_create_interaction   s    * >!7$112EF$n&8&8t&LM"
 "()>>!'R+2) ? 
 +;;#,,2/ < 
 '88+) 9 
 KK
# 	'+$" 	 	
	S!3!8!8 ##6 "9 "  <<% +,C	 =   "4!8!8 ##6	 "9 "  '99!# : 
 	
/  	S$$q:Q$RR	SsZ   CE%#E )D=*E E% E #D?$E (E%=E ?E 	E"
EE""E%r&   c                      t        ||||      S )zCreate a synchronous streaming iterator.

        Google AI's streaming format uses SSE (Server-Sent Events).
        Returns a proper streaming iterator that yields chunks as they arrive.
        r&   r3   r.   r2   )r   r*   r&   r3   r2   r.   s        r+   rZ   z7InteractionsHTTPHandler._create_sync_streaming_iterator"  s     4$;#	
 	
r-   c                      t        ||||      S )zCreate an asynchronous streaming iterator.

        Google AI's streaming format uses SSE (Server-Sent Events).
        Returns a proper streaming iterator that yields chunks as they arrive.
        rh   )r   ri   s        r+   re   z8InteractionsHTTPHandler._create_async_streaming_iterator5  s     0$;#	
 	
r-   interaction_idc
           	         |	r| j                  |||||||      S |t        d|j                  dd      i      }
n|}
|j                  |xs i d|      }|j	                  ||j
                  xs d||      \  }}|j                  |d||d	       	 |
j                  |||
      }|j                  ||      S # t        $ r}| j                  ||      d}~ww xY w)zGet an interaction by ID.rk   r.   r0   r1   r2   r6   r8   Nr=   r>   r@   rA   rk   rC   r1   r"   rC   r"   rG   rK   r"   r?   rO   rQ   r2   )
async_get_interactionr   rS   rT   !transform_get_interaction_requestrC   rX   r[   r,   "transform_get_interaction_response)r*   rk   r.   r0   r1   r2   r6   r8   r9   r:   r]   r"   rK   r?   r&   r   s                   r+   get_interactionz'InteractionsHTTPHandler.get_interactionL  sL    ---(?$7-'+ .   > 1$n&8&8t&LM! !')>>!'R) ? 
 .OO)#,,2)	 P 
V 	 ),A 	 	
	S(,, - H 'II!# J 
 	
  	S$$q:Q$RR	Ss   B; ;	CCCc	                   K   |3t        t        j                  |      d|j                  dd      i      }	n|}	|j	                  |xs i d|      }
|j                  ||j                  xs d||
      \  }}|j                  |d||
d       	 |	j                  ||
|	       d{   }|j                  ||      S 7 # t        $ r}| j                  ||
      d}~ww xY ww)z)Get an interaction by ID (async version).Nr=   ra   r@   rA   rn   ro   rG   rp   rO   rq   )r   rc   rd   rS   rT   rs   rC   rX   r[   r,   rt   )r*   rk   r.   r0   r1   r2   r6   r8   r9   rf   r"   rK   r?   r&   r   s                  r+   rr   z-InteractionsHTTPHandler.async_get_interaction  s6     >!7$112EF$n&8&8t&LM"
 "()>>!'R) ? 
 .OO)#,,2)	 P 
V 	 ),A 	 	
	S/33 4  H 'II!# J 
 	

  	S$$q:Q$RR	Ss<   BC%C 'B?(C ,C%?C 	C"
CC""C%c
           	         |	r| j                  |||||||      S |t        d|j                  dd      i      }
n|}
|j                  |xs i d|      }|j	                  ||j
                  xs d||      \  }}|j                  |d||d	       	 |
j                  |||xs t        
      }|j                  |||      S # t        $ r}| j                  ||      d}~ww xY w)zDelete an interaction by ID.rm   Nr=   r>   r@   rA   rn   ro   rG   rK   r"   r8   rO   rQ   r2   rk   )async_delete_interactionr   rS   rT   $transform_delete_interaction_requestrC   rX   deleter
   r[   r,   %transform_delete_interaction_responser*   rk   r.   r0   r1   r2   r6   r8   r9   r:   r]   r"   rK   r^   r&   r   s                   r+   delete_interactionz*InteractionsHTTPHandler.delete_interaction  sT    00-(?$7-'+ 1   > 1$n&8&8t&LM! !')>>!'R) ? 
 ,PP)#,,2)	 Q 
	T 	 ),A 	 	
	S(//2? 0 H 'LL!#) M 
 	
  	S$$q:Q$RR	Ss   C 	C%C  C%c	                   K   |3t        t        j                  |      d|j                  dd      i      }	n|}	|j	                  |xs i d|      }
|j                  ||j                  xs d||
      \  }}|j                  |d||
d       	 |	j                  ||
|xs t        	       d{   }|j                  |||      S 7 # t        $ r}| j                  ||
      d}~ww xY ww)z,Delete an interaction by ID (async version).Nr=   ra   r@   rA   rn   ro   rG   rx   rO   ry   )r   rc   rd   rS   rT   r{   rC   rX   r|   r
   r[   r,   r}   r*   rk   r.   r0   r1   r2   r6   r8   r9   rf   r"   rK   r^   r&   r   s                  r+   rz   z0InteractionsHTTPHandler.async_delete_interaction  s>     >!7$112EF$n&8&8t&LM"
 "()>>!'R) ? 
 ,PP)#,,2)	 Q 
	T 	 ),A 	 	
	S/662? 7  H 'LL!#) M 
 	

  	S$$q:Q$RR	Ss<   BC.C
 /C0C
 4C.C
 
	C+C&&C++C.c
           	         |	r| j                  |||||||      S |t        d|j                  dd      i      }
n|}
|j                  |xs i d|      }|j	                  ||j
                  xs d||      \  }}|j                  |d||d	       	 |
j                  ||||xs t        
      }|j                  ||      S # t        $ r}| j                  ||      d}~ww xY w)zCancel an interaction by ID.rm   Nr=   r>   r@   rA   rn   ro   rG   rN   rO   rq   )async_cancel_interactionr   rS   rT   $transform_cancel_interaction_requestrC   rX   rY   r
   r[   r,   %transform_cancel_interaction_responser~   s                   r+   cancel_interactionz*InteractionsHTTPHandler.cancel_interaction@  sT    00-(?$7-'+ 1   > 1$n&8&8t&LM! !')>>!'R) ? 
 ,PP)#,,2)	 Q 
	T 	 ),A 	 	
	S(--2?	 . H 'LL!# M 
 	
  	S$$q:Q$RR	Ss   C 	C%C  C%c	                   K   |3t        t        j                  |      d|j                  dd      i      }	n|}	|j	                  |xs i d|      }
|j                  ||j                  xs d||
      \  }}|j                  |d||
d       	 |	j                  ||
||xs t        	       d{   }|j                  ||      S 7 # t        $ r}| j                  ||
      d}~ww xY ww)z,Cancel an interaction by ID (async version).Nr=   ra   r@   rA   rn   ro   rG   rN   rO   rq   )r   rc   rd   rS   rT   r   rC   rX   rY   r
   r[   r,   r   r   s                  r+   r   z0InteractionsHTTPHandler.async_cancel_interaction  s>     >!7$112EF$n&8&8t&LM"
 "()>>!'R) ? 
 ,PP)#,,2)	 Q 
	T 	 ),A 	 	
	S/442?	 5  H 'LL!# M 
 	
  	S$$q:Q$RR	Ss<   BC. C
 0C1C
 5C.C
 
	C+C&&C++C.)	NNNNNNNFN)NNNNNNNN)NNNF)NNN)*__name__
__module____qualname____doc__r[   r   r,   r   strr   LiteLLMLoggingObjr   r   r   r   r	   floatr$   Timeoutr   boolr   r   r   r   r   r_   r   rR   Responser   rZ   r   re   ru   rr   r   r   rz   r   r   r    r-   r+   r   r   ,   s    3 
	8  $#,026/39=(,!%y
!:y
 >y
 !	y

 -y
 'y
 }y
 }y
 ()y
  S#X/y
 T#s(^,y
 %u}} 456y
 %y
 y
 y
  
12'7W)XX	
	

!y
D  $#,026/39=-1!%_
!:_
 >_
 !	_

 -_
 '_
 }_
 }_
 ()_
  S#X/_
 T#s(^,_
 %u}} 456_
 )*_
 _
 
/O!PP
_
B
..
 }
 '	

 ";
 
.
&
..
 }
 '	

 ";
 
*
< 379=(,>
>
 ";>
 !	>

 ->
 '>
  S#X/>
 %u}} 456>
 %>
 >
 
&	#s<S2S(TT	U>
N 379=-13
3
 ";3
 !	3

 -3
 '3
  S#X/3
 %u}} 4563
 )*3
 
!3
@ 379=(,?
?
 ";?
 !	?

 -?
 '?
  S#X/?
 %u}} 456?
 %?
 ?
 
&	#s<S2S(TT	U?
P 379=-14
4
 ";4
 !	4

 -4
 '4
  S#X/4
 %u}} 4564
 )*4
 
!4
B 379=(,?
?
 ";?
 !	?

 -?
 '?
  S#X/?
 %u}} 456?
 %?
 ?
 
&	#s<S2S(TT	U?
P 379=-14
4
 ";4
 !	4

 -4
 '4
  S#X/4
 %u}} 4564
 )*4
 
!4
r-   r   )%r   typingr   r   r   r   r   r   r	   r$   rc   litellm.constantsr
   'litellm.interactions.streaming_iteratorr   r   *litellm.litellm_core_utils.litellm_loggingr   r   1litellm.llms.base_llm.interactions.transformationr   &litellm.llms.custom_httpx.http_handlerr   r   r   r   litellm.types.interactionsr   r   r   r   r   r   litellm.types.routerr   r   interactions_http_handlerr   r-   r+   <module>r      sc        - T W   6I

 I

Z 45 r-   