o
    ni                     @  sh   d Z ddlmZ ddlZddlmZ ddlZddlmZ ddl	m
Z
 eeZdZd	ZG d
d dZdS )z
Shared cache manager for Gemini explicit Context Caching.
Creates one cache with V2 system prompt, shared by all workers.
TTL: 6 days (518400s). First worker to start creates it, rest reuse.
    )annotationsN)Optional   )GEMINI_MODEL)get_cacheable_system_promptz0https://generativelanguage.googleapis.com/v1betai  c                   @  sJ   e Zd ZdddZefddd	ZdddZdddZdddZdd Z	dS )CacheManagerapi_keystrc                 C  s   || _ d | _d S )N)r   
cache_name)selfr    r   -/home/ubuntu/transcripts/src/cache_manager.py__init__   s   
zCacheManager.__init__ttl_sintreturnc                   sd   |   I dH }|r|| _td| j  | jS | |I dH | _td| j d| d | jS )z9Get existing cache or create new one. Returns cache name.NzReusing existing cache: zCreated new cache: z (TTL=zs))_find_existing_cacher
   loggerinfo_create_cache)r   r   existingr   r   r   ensure_cache   s   zCacheManager.ensure_cacheOptional[str]c              	     s  t  d| j }tjdd4 I dH m}||I dH }|jdkr>td|j d|jdd   	 W d  I dH  dS |	 }dt
 }|d	g D ]'}|d
|krt|d }td| d| d |  W  d  I dH  S qMW d  I dH  dS 1 I dH sw   Y  dS )z5Check if we already have a valid cache for our model./cachedContents?key=      >@timeoutN   zFailed to list caches:  models/cachedContentsmodelnamezFound existing cache: z (model=))AISTUDIO_BASEr   httpxAsyncClientgetstatus_coder   warningtextjsonr   r   )r   urlclientrespdata
model_fullcacher"   r   r   r   r   (   s,   
"
	z!CacheManager._find_existing_cachec           	   	     s   t  d| j }dt ddt igi| dd}tjdd4 I d	H I}|j||d
I d	H }|jdkrEtd|j d|j	d	d  |
 }|d }|di dd}td| d|  |W  d	  I d	H  S 1 I d	H stw   Y  d	S )z,Create explicit cache with V2 system prompt.r   r   partsr*   s)r!   systemInstructionttlr   r   N)r+   r   zCache creation failed: r   i  r"   usageMetadatatotalTokenCountunknownzCache created: z, tokens cached: )r$   r   r   r   r%   r&   postr(   RuntimeErrorr*   r+   r'   r   r   )	r   r   r,   bodyr-   r.   r/   r"   token_countr   r   r   r   :   s"   
 0zCacheManager._create_cacheOptional[dict]c              	     s   | j sdS t d| j  d| j }tjdd4 I dH '}||I dH }|jdkr8| W  d  I dH  S W d  I dH  dS 1 I dH sIw   Y  dS )z:Get info about current cache (TTL remaining, token count).N/?key=r   r   r   )r
   r$   r   r%   r&   r'   r(   r+   )r   r,   r-   r.   r   r   r   get_cache_infoN   s   
zCacheManager.get_cache_infoc              	     s   t  d| j }tjdd4 I dH Q}||I dH }|jdkr-	 W d  I dH  dS | dg D ]#}|d }|t  d| d	| j I dH }t	d
| d|j  q5W d  I dH  dS 1 I dH sjw   Y  dS )z5Delete all caches for this API key (cleanup utility).r   r   r   Nr   r    r"   r>   r?   zDeleted cache z: )
r$   r   r%   r&   r'   r(   r+   deleter   r   )r   r,   r-   r.   r1   r"   del_respr   r   r   delete_all_cachesY   s   
".zCacheManager.delete_all_cachesN)r   r	   )r   r   r   r	   )r   r   )r   r=   )
__name__
__module____qualname__r   DEFAULT_CACHE_TTL_Sr   r   r   r@   rC   r   r   r   r   r      s    



r   )__doc__
__future__r   loggingtypingr   r%   configr   prompt_builderr   	getLoggerrD   r   r$   rG   r   r   r   r   r   <module>   s    
