o
    ưi"                     @   s   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
mZ d dlmZmZ d dlmZ d dlmZmZmZmZ d dlmZ dd	lmZ e ZG d
d deZdS )    N)Any	CoroutineOptionalTupleUnion)LlmProviders)GCSBucketBaseGCSLoggingConfig)get_async_httpx_client)CreateFileRequestFileContentRequestHttpxBinaryResponseContentOpenAIFileObject)VERTEX_CREDENTIALS_TYPES   ) VertexAIJsonlFilesTransformationc                       s~  e Zd ZdZ fddZdedee dee dee dee d	e	e
ejf d
ee defddZdededee dee dee dee d	e	e
ejf d
ee de	eeeeef f fddZdedeeef fddZdedee dee dee d	e	e
ejf d
ee defddZdededee dee dee dee d	e	e
ejf d
ee de	eeeeef f fddZ  ZS )VertexAIFilesHandlerz~
    Handles Calling VertexAI in OpenAI Files API format v1/files/*

    This implementation uploads files on GCS Buckets
    c                    s   t    ttjd| _d S )N)Zllm_provider)super__init__r
   r   Z	VERTEX_AIZasync_httpx_client)self	__class__ [/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/vertex_ai/files/handler.pyr   !   s   
zVertexAIFilesHandler.__init__create_file_dataapi_basevertex_credentialsvertex_projectvertex_locationtimeoutmax_retriesreturnc                    st   | j i dI d H }| j|d |d dI d H }	|d }
tj|dd\}}| j|	|
||dI d H }tj||d	S )
N)kwargsvertex_instanceZpath_service_account)r#   Zservice_account_jsonbucket_namefile)Zopenai_file_content)headersr$   object_namelogging_payload)r   gcs_upload_response)Zget_gcs_logging_configZconstruct_request_headersvertex_ai_files_transformationZ7transform_openai_file_content_to_vertex_ai_file_contentgetZ_log_json_data_on_gcsZ3transform_gcs_bucket_response_to_openai_file_object)r   r   r   r   r   r   r   r    Zgcs_logging_configr&   r$   r(   r'   r)   r   r   r   async_create_file'   s2   
z&VertexAIFilesHandler.async_create_file	_is_asyncc	           	      C   s:   |r| j |||||||dS t| j |||||||dS )zn
        Creates a file on VertexAI GCS Bucket

        Only supported for Async litellm.acreate_file
        )r   r   r   r   r   r   r    )r,   asynciorun)	r   r-   r   r   r   r   r   r   r    r   r   r   create_fileK   s*   
z VertexAIFilesHandler.create_filefile_idc                 C   sb   t j|}|dr|dd }n|}d|v r!|dd\}}n|}d}t jj|dd}||fS )az  
        Extract bucket name and object path from URL-encoded file_id.

        Expected format: gs%3A%2F%2Fbucket-name%2Fpath%2Fto%2Ffile
        Which decodes to: gs://bucket-name/path/to/file

        Returns:
            tuple: (bucket_name, url_encoded_object_path)
            - bucket_name: "bucket-name"
            - url_encoded_object_path: "path%2Fto%2Ffile"
        zgs://   N/r    )safe)urllibparseunquote
startswithsplitquote)r   r1   decoded_path	full_pathr$   Zobject_pathencoded_object_pathr   r   r   '_extract_bucket_and_object_from_file_ids   s   
z<VertexAIFilesHandler._extract_bucket_and_object_from_file_idfile_content_requestc                    s   | d}|std| |\}}	dd|ii}
| jdd|	i|
I dH }|du r7tj|}td| tj|}tjd|d	d
itj	d|dd}t
|dS )a  
        Download file content from GCS bucket for VertexAI files.

        Args:
            file_content_request: Contains file_id (URL-encoded GCS path)
            vertex_credentials: VertexAI credentials
            vertex_project: VertexAI project ID
            vertex_location: VertexAI location
            timeout: Request timeout
            max_retries: Max retry attempts

        Returns:
            HttpxBinaryResponseContent: Binary content wrapped in compatible response format
        r1   z+file_id is required in file_content_requestZ standard_callback_dynamic_paramsZgcs_bucket_namer'   Nz"Failed to download file from GCS:    zcontent-typezapplication/octet-streamGET)methodurl)status_codecontentr&   request)responser   )r+   
ValueErrorr?   Zdownload_gcs_objectr6   r7   r8   httpxResponseRequestr   )r   r@   r   r   r   r   r    r1   r$   r>   Zdownload_kwargsfile_contentr<   Zmock_responser   r   r   afile_content   s2   

z"VertexAIFilesHandler.afile_contentc	           	   
   C   s6   |r| j ||||||dS t| j ||||||dS )a  
        Download file content from GCS bucket for VertexAI files.
        Supports both sync and async operations.

        Args:
            _is_async: Whether to run asynchronously
            file_content_request: Contains file_id (URL-encoded GCS path)
            api_base: API base (unused for GCS operations)
            vertex_credentials: VertexAI credentials
            vertex_project: VertexAI project ID
            vertex_location: VertexAI location
            timeout: Request timeout
            max_retries: Max retry attempts

        Returns:
            HttpxBinaryResponseContent or Coroutine: Binary content wrapped in compatible response format
        )r@   r   r   r   r   r    )rN   r.   r/   )	r   r-   r@   r   r   r   r   r   r    r   r   r   rM      s&   	z!VertexAIFilesHandler.file_content)__name__
__module____qualname____doc__r   r   r   strr   r   floatrJ   Timeoutintr   r,   boolr   r   r0   r   r?   r   r   rN   rM   __classcell__r   r   r   r   r      s    	
$	

(
5	
r   )r.   urllib.parser6   typingr   r   r   r   r   rJ   Zlitellmr   Z/litellm.integrations.gcs_bucket.gcs_bucket_baser   r	   Z&litellm.llms.custom_httpx.http_handlerr
   Zlitellm.types.llms.openair   r   r   r   Zlitellm.types.llms.vertex_air   Ztransformationr   r*   r   r   r   r   r   <module>   s    