
    qi                     L    d Z ddlZddlmZmZmZ ddlZddlmZ  G d d      Z	y)a  Gemini File API client for uploading and managing files.

This module provides a client for Google's Gemini File API, enabling file
uploads, metadata retrieval, listing, and deletion. Files uploaded through
this API can be referenced in Gemini generative model calls.
    N)AnyDictOptional)loggerc            	           e Zd ZdZ	 ddedefdZ	 ddedee   deeef   fd	Z	d
edeeef   fdZ
	 ddedee   deeef   fdZd
edefdZy)GeminiFileAPIaq  Client for the Gemini File API.

    This class provides methods for uploading, fetching, listing, and deleting files
    through Google's Gemini File API.

    Files uploaded through this API remain available for 48 hours and can be referenced
    in calls to the Gemini generative models. Maximum file size is 2GB, with total
    project storage limited to 20GB.
    api_keybase_urlc                 .    || _         || _        d| _        y)zInitialize the Gemini File API client.

        Args:
            api_key: Google AI API key
            base_url: Base URL for the Gemini File API (default is the v1beta endpoint)
        z=https://generativelanguage.googleapis.com/upload/v1beta/filesN)_api_key	_base_urlupload_base_url)selfr	   r
   s      ^/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/google/gemini_live/file_api.py__init__zGeminiFileAPI.__init__    s      !^    N	file_pathdisplay_namereturnc           	        K   t        j                  d|        t        j                         4 d{   }t	        j
                  |      \  }}|sd}t        |d      5 }|j                         }ddd       i }|rdd|ii}ddt        t                    |d	d
}	t        j                  d| j                          |j                  | j                   d| j                   |	|      4 d{   }
|
j                  dk7  rK|
j                          d{   }t        j                   d|        t#        d|
j                   d|       |
j$                  j'                  d      }|s6t        j                   dt)        |
j$                                t#        d      t        j                  d|        ddd      d{    t        t        |            ddd}t        j                  d        |j                  |||      4 d{   }
|
j                  dk7  rK|
j                          d{   }t        j                   d|        t#        d|
j                   d|       |
j+                          d{   }t        j                  d|j'                  di       j'                  d              |cddd      d{    cddd      d{    S 7 # 1 sw Y   xxY w7 7 7 5# 1 d{  7  sw Y   FxY w7 7 7 7 L7 =# 1 d{  7  sw Y   nxY wddd      d{  7   y# 1 d{  7  sw Y   yxY ww)a*  Upload a file to the Gemini File API using the correct resumable upload protocol.

        Args:
            file_path: Path to the file to upload
            display_name: Optional display name for the file

        Returns:
            File metadata including uri, name, and display_name
        zUploading file: Nzapplication/octet-streamrbfiler   	resumablestartzapplication/json)zX-Goog-Upload-ProtocolX-Goog-Upload-Commandz#X-Goog-Upload-Header-Content-Lengthz!X-Goog-Upload-Header-Content-TypezContent-Typez Step 1: Getting upload URL from ?key=)headersjson   zError initiating file upload: zFailed to initiate upload: z - zX-Goog-Upload-URLzResponse headers: z!No upload URL in response headerszGot upload URL: 0zupload, finalize)zContent-LengthzX-Goog-Upload-Offsetr   zStep 2: Uploading file data to )r   datazError uploading file data: zFailed to upload file: zFile uploaded successfully: name)r   infoaiohttpClientSession	mimetypes
guess_typeopenreadstrlendebugr   postr   statustexterror	Exceptionr   getdictr   )r   r   r   session	mime_type_f	file_datametadatar   response
error_text
upload_urlupload_headers	file_infos                  r   upload_filezGeminiFileAPI.upload_file.   s,     	&yk23((* 9	! 9	!g$//	:LIq6	 i& %!FFH	% H"^\$BC +6)07:3y>7J5> 2G LL;D<P<P;QRS||''(dmm_=wU] $  > >??c)'/}}!6JLL#A*!NO#&A(//ARRUV`Ua$bcc &--112EF
!LL#5d8;K;K6L5M!NO#$GHH/
|<=> >$ #&c)n"5(+);N LL::,GH||JY|W ! ![c??c)'/}}!6JLL#>zl!KL#&=hoo=NcR\Q]$^__"*--/1	:9==QS;T;X;XY_;`:abc ! ! !c9	! 9	! 9	!% %$> "7	> > > >0!!6 2!c9	!b! ! !c9	! 9	! 9	! 9	! 9	!s,  1MK$M)M K'1A?M0K41M4#K=K7B"K=:MK:AMLM#L9L:A
LL<LMLMMLM'K1	,	M7K=:M=L	LL		MLLMML0	$L'%L0	,M3M>M?MMMMMr"   c                   K   d|v r|j                  d      d   }t        j                         4 d{   }|j                  | j                   d| d| j
                         4 d{   }|j                  dk7  rH|j                          d{   }t        j                  d|        t        d|j                         |j                          d{   }|cddd      d{    cddd      d{    S 7 7 7 t7 .7 7 # 1 d{  7  sw Y   nxY wddd      d{  7   y# 1 d{  7  sw Y   yxY ww)zGet metadata for a file.

        Args:
            name: File name (or full path)

        Returns:
            File metadata
        /Nr   r   zError getting file metadata: zFailed to get file metadata: )splitr$   r%   r2   r   r   r.   r/   r   r0   r1   r   )r   r"   r4   r:   r;   r>   s         r   get_filezGeminiFileAPI.get_filew   s'     $;::c?2&D((* 	! 	!g{{dnn%5QtfE$--#QR ! !V^??c)'/}}!6JLL#@!MN#&CHOOCT$UVV"*--/1	 ! ! !	! 	! 	!!!6 2!	!! ! !	! 	! 	! 	! 	!s   1E	DE	2D4)D*D4-#DDADDDD4*D	+D4/E	;D<E	D4DD	D4E	D	DD	D4"E	-D0.E	4E:D=;EE		page_size
page_tokenc                 r  K   | j                   |d}|r||d<   t        j                         4 d{   }|j                  | j                  |      4 d{   }|j
                  dk7  rH|j                          d{   }t        j                  d|        t        d|j
                         |j                          d{   }|cddd      d{    cddd      d{    S 7 7 7 t7 .7 7 # 1 d{  7  sw Y   nxY wddd      d{  7   y# 1 d{  7  sw Y   yxY ww)zList uploaded files.

        Args:
            page_size: Number of files to return per page
            page_token: Token for pagination

        Returns:
            List of files and next page token if available
        )keypageSize	pageTokenN)paramsr   zError listing files: zFailed to list files: )r   r$   r%   r2   r   r.   r/   r   r0   r1   r   )r   rE   rF   rK   r4   r:   r;   results           r   
list_fileszGeminiFileAPI.list_files   s     I>",F;((* 	 	g{{4>>&{A  X??c)'/}}!6JLL#8!EF#&<X__<M$NOO'}}.  	 	 	!6 /	  	 	 	 	 	s   /D7C/D7"D"C1D"#C;>C3?AC;C5C;D"C7D"D7)C9*D71D"3C;5C;7D"9D7;D	DD		D"D7DD7"D4(D+)D40D7c                 ^  K   d|v r|j                  d      d   }t        j                         4 d{   }|j                  | j                   d| d| j
                         4 d{   }|j                  dk7  rH|j                          d{   }t        j                  d|        t        d|j                         	 ddd      d{    ddd      d{    y7 7 7 Z7 7 # 1 d{  7  sw Y   nxY wddd      d{  7   y# 1 d{  7  sw Y   yxY ww)	zDelete a file.

        Args:
            name: File name (or full path)

        Returns:
            True if deleted successfully
        rA   rB   Nr   r   zError deleting file: zFailed to delete file: T)rC   r$   r%   deleter   r   r.   r/   r   r0   r1   )r   r"   r4   r:   r;   s        r   delete_filezGeminiFileAPI.delete_file   s     $;::c?2&D((* 	 	g~~(8$uT]]O&TU  Ya??c)'/}}!6JLL#8!EF#&=hoo=N$OPP 	 	 	!6	  	 	 	 	 	s   1D-C'D-2D)C)*D-#C1C+5C1DC-DD-!C/"D-)D+C1-D/D-1D	7C:8D	?DD-DD-D*D!D*&D-)z6https://generativelanguage.googleapis.com/v1beta/files)N)
   N)__name__
__module____qualname____doc__r*   r   r   r   r   r?   rD   intrM   boolrP    r   r   r   r      s     -e__&)_ =AG!G!,4SMG!	c3hG!R!3 !4S> !0 @D/7}	c3h6c d r   r   )
rU   r&   typingr   r   r   r$   logurur   r   rX   r   r   <module>r[      s'     & &  h hr   