
    yj                        d dl mZ d dl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 d dlmZ d dlmZmZ g d	Z G d
 de          ZdddddZ G d d          Z e            ZdS )    )annotationsN)OrderedDict)datetimetimezone)Lock)Any)	BaseModel)ModelConfigPromptCachePolicy)GeminiCacheHandleInMemoryGeminiCacheStorer   build_cache_keygemini_cache_storec                  .    e Zd ZU ded<   ded<   ded<   dS )r   strkeycached_content_namer   
expires_atN)__name__
__module____qualname____annotations__     7/DATA/AppData/hermes/projects/honcho/src/llm/caching.pyr   r      s3         HHHr   r   )system_instructiontool_configconfigr
   cache_policyr   cacheable_messageslist[dict[str, Any]]toolslist[dict[str, Any]] | Noner   
str | Noner   dict[str, Any] | Nonereturnr   c                   | j         | j        |                    d          ||||d}t          j        |dd          }t          j        |                    d                                                    }d|j	         d	| S )
u  Deterministic key over the cacheable shape of a request.

    ``system_instruction`` and ``tool_config`` must be part of the key
    because the provider's cached-content handle captures them at creation
    time — two requests that differ only by system prompt or tool
    constraints would otherwise hit the same cached handle and silently get
    the wrong system prompt / tool policy.
    json)mode)	transportmodelr   messagesr"   r   r   T),:)	sort_keys
separatorszutf-8z
llm-cache:r.   )
r*   r+   
model_dumpr(   dumpshashlibsha256encode	hexdigestkey_version)	r   r   r    r"   r   r   payloadencodeddigests	            r   r   r      s    $ %$//V/<<&0" G jDZHHHG^GNN73344>>@@F;0;;6;;;r   c                  :    e Zd ZU dZdZded<   ddZddZddZdS )r   zBest-effort local cache for Gemini cached-content handles.

    Uses LRU eviction with a max entry limit to prevent unbounded growth.
    i   intMAX_ENTRIESr&   Nonec                R    t                      | _        t                      | _        d S N)r   _handlesr   _lock)selfs    r   __init__z!InMemoryGeminiCacheStore.__init__D   s    =H]]66


r   r   r   GeminiCacheHandle | Nonec                p   | j         5  | j                            |          }|	 d d d            d S |j        t	          j        t          j                  k    r)| j                            |d            	 d d d            d S | j        	                    |           |cd d d            S # 1 swxY w Y   d S r@   )
rB   rA   getr   r   nowr   utcpopmove_to_end)rC   r   handles      r   rG   zInMemoryGeminiCacheStore.getH   s-   Z 	 	]&&s++F~	 	 	 	 	 	 	 	  HL$>$>>>!!#t,,,	 	 	 	 	 	 	 	 M%%c***	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B+AB+B++B/2B/rL   r   c                .   | j         5  t          j        t          j                  fd| j                                        D             }|D ]}| j                            |d            |j        | j        v r| j        	                    |j                   || j        |j        <   t          | j                  | j        k    r8| j                            d           t          | j                  | j        k    8d d d            n# 1 swxY w Y   |S )Nc                0    g | ]\  }}|j         k    |S r   )r   ).0khrH   s      r   
<listcomp>z0InMemoryGeminiCacheStore.set.<locals>.<listcomp>V   s)    RRRTQalc>Q>Qq>Q>Q>Qr   F)last)rB   r   rH   r   rI   rA   itemsrJ   r   rK   lenr=   popitem)rC   rL   expiredrP   rH   s       @r   setzInMemoryGeminiCacheStore.setS   sI   Z 		2 		2,x|,,CRRRRT]%8%8%:%:RRRG + +!!!T****zT]**))&*555(.DM&*%dm$$t'777%%5%111 dm$$t'777		2 		2 		2 		2 		2 		2 		2 		2 		2 		2 		2 		2 		2 		2 		2 s   C5D

DDN)r&   r>   )r   r   r&   rE   )rL   r   r&   r   )	r   r   r   __doc__r=   r   rD   rG   rX   r   r   r   r   r   <   so          
 K" " " "	 	 	 	     r   r   )r   r
   r   r   r    r!   r"   r#   r   r$   r   r%   r&   r   )
__future__r   r3   r(   collectionsr   r   r   	threadingr   typingr   pydanticr	   
src.configr
   r   __all__r   r   r   r   r   r   r   <module>ra      sB   " " " " " "   # # # # # # ' ' ' ' ' ' ' '                   5 5 5 5 5 5 5 5      	    &*)-< < < < < <>" " " " " " " "J .-//   r   