o
    ưi]                     @   s   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 dZ#e# Z$dS )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                (   @   s   e Zd ZdZdededefd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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dZdejdee de
d	edef
ddZdejdee de
d	ede f
d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!d"Z"			d0d 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#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%d&Z%			d0d 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'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)d*Z(			d0d 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+d,Z)dS )1InteractionsHTTPHandlerz5
    HTTP handler for Interactions API requests.
    eprovider_configreturnc                 C   s<   t |tjr|jj}|jj}t|jj}|j|||dS |S )z!Handle errors from HTTP requests.)error_messagestatus_codeheaders)	
isinstancehttpxZHTTPStatusErrorresponsetextr   dictr   Zget_error_class)selfr   r   r   r   r    r&   X/home/app/Keep/.python/lib/python3.10/site-packages/litellm/interactions/http_handler.py_handle_error1   s   z%InteractionsHTTPHandler._handle_errorNFinteractions_api_configoptional_paramscustom_llm_providerlitellm_paramslogging_objmodelagentinputextra_headers
extra_bodytimeoutclient	_is_asyncstreamc                 C   s>  |r| j |||||||||	|
||dS |du r#td|ddid}n|}|j|	p*i |p-d|d}|j|jp7d||t||d}|j||||||d}|
rR||
 |j	|d|||d	d
 z%|rw|j
||||pitdd}| j||||dW S |j
||||ptd}W n ty } z| j||dd}~ww |j|||dS )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
        )r.   r/   r0   r)   r*   r+   r,   r-   r1   r2   r3   r6   N
ssl_verifyparams r   r.   r,   api_baser.   r/   r,   r6   r.   r/   r0   r*   r,   r   Zcomplete_input_dictr=   r   r0   Zapi_keyZadditional_argsTurlr   jsonr3   r6   r"   r.   r-   r)   rB   r   rC   r3   r   r   r.   raw_responser-   )async_create_interactionr   getvalidate_environmentget_complete_urlr=   r$   transform_requestupdatepre_callpostr	   _create_sync_streaming_iterator	Exceptionr(   transform_response)r%   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   sync_httpx_clientr   r=   datar"   r   r&   r&   r'   create_interactionF   s   	


z*InteractionsHTTPHandler.create_interactionc              
      s.  |du rt t|d|ddid}n|}|j|	pi |pd|d}|j|jp)d||t||d}|j||||||d}|
rD|	|
 |j
|d|||dd	 z+|rl|j||||p[td
dI dH }| j||||dW S |j||||pttdI dH }W n ty } z| j||dd}~ww |j|||dS )z;
        Create a new interaction (async version).
        Nr7   Zllm_providerr9   r:   r;   r<   r>   r?   r@   TrA   rD   rE   rF   rG   )r   litellmLlmProvidersrJ   rK   rL   r=   r$   rM   rN   rO   rP   r	    _create_async_streaming_iteratorrR   r(   rS   )r%   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r6   async_httpx_clientr   r=   rU   r"   r   r&   r&   r'   rI      s   	

z0InteractionsHTTPHandler.async_create_interactionr"   c                 C      t ||||d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"   r.   r)   r-   )r   r%   r"   r.   r-   r)   r&   r&   r'   rQ        z7InteractionsHTTPHandler._create_sync_streaming_iteratorc                 C   r\   )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.
        r]   )r
   r^   r&   r&   r'   rZ   -  r_   z8InteractionsHTTPHandler._create_async_streaming_iteratorinteraction_idc
              
   C   s   |	r| j |||||||dS |du rtd|ddid}
n|}
|j|p%i d|d}|j||jp1d||d\}}|j|d||dd	 z
|
j|||d
}W n ty` } z| j||dd}~ww |j	||dS )zGet an interaction by ID.r`   r)   r+   r,   r-   r1   r3   Nr7   r8   r:   r;   r`   r=   r,   r   r=   r   r@   rB   r   r9   rF   rH   r-   )
async_get_interactionr   rJ   rK   !transform_get_interaction_requestr=   rO   rR   r(   "transform_get_interaction_response)r%   r`   r)   r+   r,   r-   r1   r3   r4   r5   rT   r   rB   r9   r"   r   r&   r&   r'   get_interactionD  sZ   


z'InteractionsHTTPHandler.get_interactionc	              
      s   |du rt t|d|ddid}	n|}	|j|pi d|d}
|j||jp(d||
d\}}|j|d||
dd z|	j||
|d	I dH }W n tyZ } z| j	||d
d}~ww |j
||dS )z)Get an interaction by ID (async version).Nr7   rW   r:   r;   rb   rc   r@   rd   rF   re   )r   rX   rY   rJ   rK   rg   r=   rO   rR   r(   rh   )r%   r`   r)   r+   r,   r-   r1   r3   r4   r[   r   rB   r9   r"   r   r&   r&   r'   rf     sJ   
z-InteractionsHTTPHandler.async_get_interactionc
              
   C   s   |	r| j |||||||dS |du rtd|ddid}
n|}
|j|p%i d|d}|j||jp1d||d\}}|j|d||dd	 z|
j|||pKtd
}W n t	yb } z| j
||dd}~ww |j|||dS )zDelete an interaction by ID.ra   Nr7   r8   r:   r;   rb   rc   r@   rB   r   r3   rF   rH   r-   r`   )async_delete_interactionr   rJ   rK   $transform_delete_interaction_requestr=   rO   deleter	   rR   r(   %transform_delete_interaction_responser%   r`   r)   r+   r,   r-   r1   r3   r4   r5   rT   r   rB   rU   r"   r   r&   r&   r'   delete_interaction  s\   


z*InteractionsHTTPHandler.delete_interactionc	              
      s   |du rt t|d|ddid}	n|}	|j|pi d|d}
|j||jp(d||
d\}}|j|d||
dd z|	j||
|pBt	d	I dH }W n t
y\ } z| j||d
d}~ww |j|||dS )z,Delete an interaction by ID (async version).Nr7   rW   r:   r;   rb   rc   r@   rj   rF   rk   )r   rX   rY   rJ   rK   rm   r=   rO   rn   r	   rR   r(   ro   r%   r`   r)   r+   r,   r-   r1   r3   r4   r[   r   rB   rU   r"   r   r&   r&   r'   rl     sL   
z0InteractionsHTTPHandler.async_delete_interactionc
              
   C   s   |	r| j |||||||dS |du rtd|ddid}
n|}
|j|p%i d|d}|j||jp1d||d\}}|j|d||dd	 z|
j||||pLtd
}W n t	yc } z| j
||dd}~ww |j||dS )zCancel an interaction by ID.ra   Nr7   r8   r:   r;   rb   rc   r@   rE   rF   re   )async_cancel_interactionr   rJ   rK   $transform_cancel_interaction_requestr=   rO   rP   r	   rR   r(   %transform_cancel_interaction_responserp   r&   r&   r'   cancel_interaction8  s\   


z*InteractionsHTTPHandler.cancel_interactionc	              
      s   |du rt t|d|ddid}	n|}	|j|pi d|d}
|j||jp(d||
d\}}|j|d||
dd z|	j||
||pCt	d	I dH }W n t
y] } z| j||d
d}~ww |j||dS )z,Cancel an interaction by ID (async version).Nr7   rW   r:   r;   rb   rc   r@   rE   rF   re   )r   rX   rY   rJ   rK   rt   r=   rO   rP   r	   rR   r(   ru   rr   r&   r&   r'   rs   y  sL   
z0InteractionsHTTPHandler.async_cancel_interaction)	NNNNNNNFN)NNNNNNNN)NNNF)NNN)*__name__
__module____qualname____doc__rR   r   r(   r   strr   LiteLLMLoggingObjr   r   r   r   r   floatr!   Timeoutr   boolr   r   r   r   r   rV   r   rI   Responser   rQ   r
   rZ   ri   rf   r   rq   rl   r   rv   rs   r&   r&   r&   r'   r   ,   s   
	

|	

_

	

G	

@	

H	

A	

H	
r   )%rz   typingr   r   r   r   r   r   r   r!   rX   Zlitellm.constantsr	   Z'litellm.interactions.streaming_iteratorr
   r   Z*litellm.litellm_core_utils.litellm_loggingr   r|   Z1litellm.llms.base_llm.interactions.transformationr   Z&litellm.llms.custom_httpx.http_handlerr   r   r   r   Zlitellm.types.interactionsr   r   r   r   r   r   Zlitellm.types.routerr   r   Zinteractions_http_handlerr&   r&   r&   r'   <module>   s$    $
      

