o
    ưi8                     @   s`  U d Z ddlZddlm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mZmZmZ ddlmZmZmZ ddlmZ erQddlmZ dd	lmZ eeedZeee
f e d
< defddZ!dede	e fddZ"dededeeef defddZ#de$deeef deeef fddZ%dedeeef de&fddZ'G dd dZ(e( Z)dS ) z
Generic container file handler for LiteLLM.

This module provides a single generic handler that can process any container file
endpoint defined in endpoints.json, eliminating the need for individual handler methods.
    N)Path)TYPE_CHECKINGAny	CoroutineDictOptionalTypeUnion)AsyncHTTPHandlerHTTPHandler_get_httpx_clientget_async_httpx_client)ContainerFileListResponseContainerFileObjectDeleteContainerFileResponse)GenericLiteLLMParams)Logging)BaseContainerConfigRESPONSE_TYPESreturnc                  C   sL   t tjjjd d } t| }t|W  d   S 1 sw   Y  dS )z0Load the endpoints configuration from JSON file.
containerszendpoints.jsonN)r   __file__parentopenjsonload)Zconfig_pathf r   b/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/custom_httpx/container_handler.py_load_endpoints_config)   s   
$r   endpoint_namec                 C   s8   t  }|d D ]}|d | ks|d | kr|  S qdS )z+Get config for a specific endpoint by name.Z	endpointsnameZ
async_nameN)r   )r    configZendpointr   r   r   _get_endpoint_config0   s   r#   api_basepath_templatepath_paramsc                 C   sV   | dr|tdd }| d | }| D ]\}}|d| d|}q|S )zBuild the full URL by substituting path parameters.
    
    The api_base from get_complete_url already includes /containers,
    so we need to strip that prefix from the path_template.
    z/containersN/{})
startswithlenrstripitemsreplace)r$   r%   r&   urlparamvaluer   r   r   
_build_url9   s   
r2   query_param_nameskwargsc                 C   s>   i }| D ]}| |}|durt|tst|n|||< q|S )z#Build query parameters from kwargs.N)get
isinstancestr)r3   r4   params
param_namer1   r   r   r   _build_query_paramsN   s   
r:   fileheadersc           	      C   st   ddl m} || }|dpd}|dpd}|dpd}d|||fi}| }|d	d
 |dd
 ||fS )z
    Prepare file and headers for multipart upload.
    
    Returns:
        Tuple of (files_dict, headers_without_content_type)
    r   )extract_file_datafilenamer;   content    content_typezapplication/octet-streamzcontent-typeNzContent-Type)Z8litellm.litellm_core_utils.prompt_templates.common_utilsr=   r5   copypop)	r;   r<   r=   Z	extractedr>   r?   rA   filesZheaders_copyr   r   r   _prepare_multipart_file_upload[   s   
rE   c                   @   sZ  e Zd ZdZ					ddeddded	d
deeeef  deeeef  de	e
ejf dedee	eef  de	eeeeef f fddZ				ddeddded	d
deeeef  deeeef  de	e
ejf dee	eef  defddZ				ddeddded	d
deeeef  deeeef  de	e
ejf dee	eef  defddZdS )GenericContainerHandlerz
    Generic handler for container file API endpoints.
    
    This single handler can process any endpoint defined in endpoints.json,
    eliminating the need for individual handler methods per endpoint.
    NX  Fr    container_provider_configr   litellm_paramslogging_objLiteLLMLoggingObjextra_headersextra_querytimeout	_is_asyncclientr   c
                 K   sH   |r| j d||||||||	d|
S | jd||||||||	d|
S )a  
        Generic handler for any container file endpoint.
        
        Args:
            endpoint_name: Name of the endpoint (e.g., "list_container_files")
            container_provider_config: Provider-specific configuration
            litellm_params: LiteLLM parameters including api_key, api_base
            logging_obj: Logging object for request logging
            extra_headers: Additional HTTP headers
            extra_query: Additional query parameters
            timeout: Request timeout
            _is_async: Whether to make async request
            client: Optional HTTP client
            **kwargs: Path params and query params (e.g., container_id, file_id, after, limit)
        )r    rH   rI   rJ   rL   rM   rN   rP   Nr   )_async_handle_sync_handle)selfr    rH   rI   rJ   rL   rM   rN   rO   rP   r4   r   r   r   handle   s2   		zGenericContainerHandler.handlec	              
      s>  t |}
|
std| |du st|ts"td|ddid}n|}|j|p)i |ddd}|r8|| |j|ddt	|d} fd	d
|
dg D }t
||
d |}t|
dg  }|rj|| |jdd|||dd |
d  }|
dd}|
dd}z|dkr|j|||d}n:|dkr|j|||d}n-|dkr|rd v rt d |\}}|j||||d}n|j|||d}ntd| |r|jW S | }d|v rddlm} |di dt|}||j|t	|jd t|
d! }|r|d"i |W S |W S  ty } z|d}~ww )#zSynchronous request handler.Unknown endpoint: N
ssl_verify)r8   api_keyr<   rW   r$   r$   rI   c                       i | ]	}|  |d qS  r5   .0pr4   r   r   
<dictcomp>       z8GenericContainerHandler._sync_handle.<locals>.<dictcomp>r&   pathquery_paramsr\   r$   r<   r8   inputrW   Zadditional_argsmethodreturns_binaryFis_multipartGETr/   r<   r8   DELETEPOSTr;   r/   r<   r8   rD   Unsupported HTTP method: errorr   BaseLLMExceptionmessagestatus_coderu   r<   response_typer   )r#   
ValueErrorr6   r   r   r5   validate_environmentupdateget_complete_urldictr2   r:   pre_callupperdeleterE   postr?   r   )litellm.llms.base_llm.chat.transformationrt   r7   rw   r<   r   	ExceptionrS   r    rH   rI   rJ   rL   rM   rN   rP   r4   Zendpoint_confighttp_clientr<   r$   r&   r/   re   ri   rj   rk   responserD   Zresponse_jsonrt   	error_msgrx   er   ra   r   rR      s   



z$GenericContainerHandler._sync_handlec	              
      s`  t |}
|
std| |du st|ts&ttjjd|ddid}n|}|j	|p-i |ddd}|r<|
| |j|ddt|d} fd	d
|
dg D }t||
d |}t|
dg  }|rn|
| |jdd|||dd |
d  }|
dd}|
dd}z|dkr|j|||dI dH }nC|dkr|j|||dI dH }n3|dkr|rd v rt d |\}}|j||||dI dH }n|j|||dI dH }ntd| |r|jW S | }d|v rddlm} |di dt|}||j|t|jd t|
d! }|r|d"i |W S |W S  ty/ } z|d}~ww )#zAsynchronous request handler.rU   NrV   )Zllm_providerr8   rW   rX   r$   rY   c                    rZ   r[   r]   r^   ra   r   r   rb   A  rc   z9GenericContainerHandler._async_handle.<locals>.<dictcomp>r&   rd   re   r\   rf   rg   ri   rj   Frk   rl   rm   rn   ro   r;   rp   rq   rr   r   rs   ru   rv   rx   r   )r#   ry   r6   r
   r   litellmZLlmProvidersZOPENAIr5   rz   r{   r|   r}   r2   r:   r~   r   r   rE   r   r?   r   r   rt   r7   rw   r<   r   r   r   r   ra   r   rQ     s   




z%GenericContainerHandler._async_handle)NNrG   FN)NNrG   N)__name__
__module____qualname____doc__r7   r   r   r   r   r	   floathttpxTimeoutboolr   r
   r   rT   rR   rQ   r   r   r   r   rF   w   s    	

;	
k	rF   )*r   r   pathlibr   typingr   r   r   r   r   r   r	   r   r   Z&litellm.llms.custom_httpx.http_handlerr
   r   r   r   Zlitellm.types.containers.mainr   r   r   Zlitellm.types.routerr   Z*litellm.litellm_core_utils.litellm_loggingr   rK   Z/litellm.llms.base_llm.containers.transformationr   r   r7   __annotations__r   r#   r2   listr:   tuplerE   rF   Zgeneric_container_handlerr   r   r   r   <module>   sZ    $	






  
