
    i>9                     r   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rddlmZ dd	lmZ eeedZeee
f   e d
<   defdZ!dede	e   fdZ"dededeeef   defdZ#de$deeef   deeef   fdZ%dedeeef   de&fdZ' G d d      Z( e(       Z)y)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                      t        t              j                  j                  j                  dz  dz  } t        |       5 }t	        j
                  |      cddd       S # 1 sw Y   yxY w)z0Load the endpoints configuration from JSON file.
containerszendpoints.jsonN)r   __file__parentopenjsonload)config_pathfs     |/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/llms/custom_httpx/container_handler.py_load_endpoints_configr!   )   sI    x.''..55DGWWK	k	ayy| 
		s   AA'endpoint_namec                 T    t               }|d   D ]  }|d   | k(  s	|d   | k(  s|c S  y)z+Get config for a specific endpoint by name.	endpointsname
async_nameN)r!   )r"   configendpoints      r    _get_endpoint_configr)   0   s<    #%F;'F},0F-0WO (     api_basepath_templatepath_paramsc                     |j                  d      r|t        d      d }| j                  d       | }|j                         D ]  \  }}|j	                  d| d|      } |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values         r    
_build_urlr:   9   sp     .%c-&8&:;__S!"=/
2C#))+ukkBugR.%0 ,Jr*   query_param_nameskwargsc                     i }| D ]6  }|j                  |      }|t        |t              st        |      n|||<   8 |S )z#Build query parameters from kwargs.)get
isinstancestr)r;   r<   params
param_namer9   s        r    _build_query_paramsrC   N   sG    
 F'


:&3=eS3IUuF: ( Mr*   fileheadersc                    ddl m}  ||       }|j                  d      xs d}|j                  d      xs d}|j                  d      xs d}d|||fi}|j                         }|j	                  d	d
       |j	                  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filenamerD   contentr*   content_typezapplication/octet-streamzcontent-typeNzContent-Type)8litellm.litellm_core_utils.prompt_templates.common_utilsrG   r>   copypop)	rD   rE   rG   	extractedrH   rI   rJ   filesheaders_copys	            r    _prepare_multipart_file_uploadrQ   [   s     "$'I}}Z(2FHmmI&-#G==0N4NLh67E <<>L^T*^T*,r*   c                      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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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Zy)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.
    Nr"   container_provider_configr   litellm_paramslogging_objLiteLLMLoggingObjextra_headersextra_querytimeout	_is_asyncclientr   c
                 r    |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"   rT   rU   rV   rX   rY   rZ   r\    )_async_handle_sync_handle)selfr"   rT   rU   rV   rX   rY   rZ   r[   r\   r<   s              r    handlezGenericContainerHandler.handle   s    8 %4%% 
+*C-'+'
 
 
 !t   

'&?)#'#

 

 
	
r*   c	                 j   t        |      }
|
st        d|       |t        |t              st	        d|j                  dd      i      }n|}|j                  |xs i |j                  dd            }|r|j                  |       |j                  |j                  dd      t        |            }|
j                  d	g       D ci c]  }||	j                  |d
       }}t        ||
d   |      }t        |
j                  dg       |	      }|r|j                  |       |j                  d
d
|||d       |
d   j                         }|
j                  dd      }|
j                  dd      }	 |dk(  r|j                  |||      }np|dk(  r|j                  |||      }nV|dk(  rC|r,d|	v r(t        |	d   |      \  }}|j!                  ||||      }n#|j!                  |||      }nt        d|       |r|j"                  S |j%                         }d|v rYddlm} |j                  di       j                  dt+        |            } ||j,                  |t        |j.                              t0        j                  |
d         }|r |d i |S |S c c}w # t2        $ r}|d}~ww xY w)!zSynchronous request handler.Unknown endpoint: N
ssl_verify)rA   api_keyrE   rf   r+   r+   rU   r-    pathquery_paramsr+   rE   rA   inputrf   additional_argsmethodreturns_binaryFis_multipartGETr7   rE   rA   DELETEPOSTrD   r7   rE   rA   rO   Unsupported HTTP method: errorr   BaseLLMExceptionmessagestatus_coder|   rE   response_typer^   )r)   
ValueErrorr?   r   r   r>   validate_environmentupdateget_complete_urldictr:   rC   pre_callupperdeleterQ   postrI   r   )litellm.llms.base_llm.chat.transformationr{   r@   r~   rE   r   	Exceptionra   r"   rT   rU   rV   rX   rY   rZ   r\   r<   endpoint_confighttp_clientrE   r+   pr-   r7   rk   rp   rq   rr   responserO   response_jsonr{   	error_msgr   es                               r    r`   z$GenericContainerHandler._sync_handle   s<    /}=1-ABB >FK!@+$n&8&8t&LMK !K ,@@!'R"&&y$7 A 
 NN=),==#''
D9/ > 
 +:*=*=mR*P
*PQAvzz!R  *P 	 
 ?6#:KH +3V
 , 	"& 	 	
 !*002(,,-=uE&**>5A1	&??W\ +  8#&--W\ .  6!Ff$4%Cv&NE7  +//U  0  H  +//  0  H !#<VH!EFF ''' %MMOM-'V)--gr:>>s=1	 ' ( 4 4% !1!12  +../OPM$5}55  U
X  	G	s,   	J4BJ" BJ" J" "	J2+J--J2c	                   K   t        |      }
|
st        d|       |t        |t              s8t	        t
        j                  j                  d|j                  dd      i      }n|}|j                  |xs i |j                  dd            }|r|j                  |       |j                  |j                  dd      t        |            }|
j                  d	g       D ci c]  }||	j                  |d
       }}t        ||
d   |      }t        |
j                  dg       |	      }|r|j                  |       |j                  d
d
|||d       |
d   j!                         }|
j                  dd      }|
j                  dd      }	 |dk(  r|j                  |||       d{   }n|dk(  r|j#                  |||       d{   }nf|dk(  rS|r4d|	v r0t%        |	d   |      \  }}|j'                  ||||       d{   }n+|j'                  |||       d{   }nt        d|       |r|j(                  S |j+                         }d|v rYddlm} |j                  di       j                  dt1        |            } ||j2                  |t        |j4                              t6        j                  |
d         }|r |d i |S |S c c}w 7 27 7 7 # t8        $ r}|d}~ww xY ww)!zAsynchronous request handler.rd   Nre   )llm_providerrA   rf   rg   r+   rh   r-   ri   rj   rk   rl   rm   rp   rq   Frr   rs   rt   ru   rv   rD   rw   rx   ry   r   rz   r|   r}   r   r^   )r)   r   r?   r   r   litellmLlmProvidersOPENAIr>   r   r   r   r   r:   rC   r   r   r   rQ   r   rI   r   r   r{   r@   r~   rE   r   r   r   s                               r    r_   z%GenericContainerHandler._async_handle*  sy     /}=1-ABB >F4D!E0$1188$n&8&8t&LMK
 !K ,@@!'R"&&y$7 A 
 NN=),==#''
D9/ > 
 +:*=*=mR*P
*PQAvzz!R  *P 	 
 ?6#:KH +3V
 , 	"& 	 	
 !*002(,,-=uE&**>5A1	!,W\ "1 "  8#!,!3!3W\ "4 "  6!Ff$4%Cv&NE7 &1%5%5U &6 &  H &1%5%5 &6 &  H !#<VH!EFF ''' %MMOM-'V)--gr:>>s=1	 ' ( 4 4% !1!12  +../OPM$5}55  U
<  <  	G	s   C"K:$K>BK:K' +K,!K' K :K' K#	K' %K%& K' K:BK' K:K' K:K'  K' #K' %K' '	K70K22K77K:)NNX  FN)NNr   N)__name__
__module____qualname____doc__r@   r   r   r   r   r
   floathttpxTimeoutboolr   r   r   rb   r`   r_   r^   r*   r    rS   rS   w   s(    3704/2AE3
3
 $93
 -	3

 )3
  S#X/3
 d38n-3
 uemm+,3
 3
 {,<<=>3
 
sIc3m,,	-3
v 3704/2AEtt $9t -	t
 )t  S#X/t d38n-t uemm+,t {,<<=>t 
tx 3704/2AEuu $9u -	u
 )u  S#X/u d38n-u uemm+,u {,<<=>u 
ur*   rS   )*r   r   pathlibr   typingr   r   r   r   r   r	   r
   r   r   &litellm.llms.custom_httpx.http_handlerr   r   r   r   litellm.types.containers.mainr   r   r   litellm.types.routerr   *litellm.litellm_core_utils.litellm_loggingr   rW   /litellm.llms.base_llm.containers.transformationr   r   r@   __annotations__r!   r)   r:   listrC   tuplerQ   rS   generic_container_handlerr^   r*   r    <module>r      s&     M M M    
 6WS
 ";.#>#S$Y     c3h 		*

cN
 
#s(^

#s(^ 8h hX	 45 r*   