Ë
    uPêjÊ  ã                   óN   — d Z ddlZddlmZ ddlmZmZ ddlmZ  G d„ de«      Z	y)	zK
Add the event loop to the cache key, to prevent event loop closed errors.
é    N)ÚFinalé   )ÚEvictedClientCloserÚdefault_evicted_client_closer)ÚInMemoryCachec                   ó¼   ‡ — e Zd ZdZ	 	 	 	 ddedz  dedz  dedz  dedz  ddf
ˆ fd„Zd	eddfˆ fd
„Zd„ Z	dd	ede
defˆ fd„Zdd	ede
defˆ fd„Zˆ fd„Zˆ fd„Zˆ xZS )ÚLLMClientCachea›  Cache for LLM HTTP clients (OpenAI, Azure, httpx, etc.).

    An evicted client is never closed on the spot: a request handed the client
    just before eviction is still using it, and closing it there raises
    ``RuntimeError: Cannot send a request, as the client has been closed.``

    Nor can eviction be left to rely on garbage collection. The SDK clients are
    reference cycles, so an evicted client and its open TCP connections survive
    until a generational collection runs. Instead a client litellm created is
    handed to ``EvictedClientCloser``, which closes it once a grace window has
    passed. Clients the caller supplied are left untouched.
    NÚmax_size_in_memoryÚdefault_ttlÚmax_size_per_itemÚevicted_client_closerÚreturnc                 óH   •— t         ‰| �  |||¬«       |xs t        | _        y )N)r
   r   r   )ÚsuperÚ__init__r   r   )Úselfr
   r   r   r   Ú	__class__s        €úh/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/caching/llm_caching_handler.pyr   zLLMClientCache.__init__   s0   ø€ ô 	‰ÑØ1Ø#Ø/ð 	ô 	
ð
 &;Ò%[Ô>[ˆÕ"ó    Úkeyc                 óÄ   •— | j                   j                  |«      }t        ‰| �  |«       | j                  j                  |«       | j                  j                  «        y ©N)Ú
cache_dictÚgetr   Ú_remove_keyr   ÚscheduleÚreap)r   r   Úevictedr   s      €r   r   zLLMClientCache._remove_key(   sJ   ø€ Ø!%§¡×!4Ñ!4°SÓ!9ˆÜ‰Ñ˜CÔ Ø×"Ñ"×+Ñ+¨GÔ4Ø×"Ñ"×'Ñ'Õ)r   c                 ó„   — 	 t        j                  «       }t        t        |«      «      }|› d|› �S # t        $ r |cY S w xY w)z
        Add the event loop to the cache key, to prevent event loop closed errors.
        If none, use the key as is.
        ú-)ÚasyncioÚget_running_loopÚstrÚidÚRuntimeError)r   r   Ú
event_loopÚstringified_event_loops       r   Ú update_cache_key_with_event_loopz/LLMClientCache.update_cache_key_with_event_loop.   sK   € ð
	Ü '× 8Ñ 8Ó :ˆJÜ,/´°:³Ó,?Ð"Ø�U˜!Ð2Ð3Ð4Ð4øÜò 	ØŠJð	ús   ‚.1 ±?¾?ÚvalueÚlitellm_owned_clientc                 ó‚   •— |r| j                   j                  |«       | j                  |«      }t        ‰| �  ||fi |¤ŽS )zX``litellm_owned_client`` marks a client litellm built, so it may be closed once evicted.)r   Ú
mark_ownedr(   r   Ú	set_cache©r   r   r)   r*   Úkwargsr   s        €r   r-   zLLMClientCache.set_cache:   sA   ø€ áØ×&Ñ&×1Ñ1°%Ô8Ø×3Ñ3°CÓ8ˆÜ‰wÑ   eÑ6¨vÑ6Ð6r   c              ‹   óž   •K  — |r| j                   j                  |«       | j                  |«      }t        ‰| �  ||fi |¤Žƒ d {  –—† S 7 Œ­wr   )r   r,   r(   r   Úasync_set_cacher.   s        €r   r1   zLLMClientCache.async_set_cacheA   sK   øè ø€ ÙØ×&Ñ&×1Ñ1°%Ô8Ø×3Ñ3°CÓ8ˆÜ‘WÑ,¨S°%ÑB¸6ÑB×BÐBÐBús   ƒAAÁAÁAc                 óz   •— | j                  |«      }| j                  j                  «        t        ‰| �  |fi |¤ŽS r   )r(   r   r   r   Ú	get_cache©r   r   r/   r   s      €r   r3   zLLMClientCache.get_cacheG   s:   ø€ Ø×3Ñ3°CÓ8ˆØ×"Ñ"×'Ñ'Ô)ä‰wÑ  Ñ/¨Ñ/Ð/r   c              ‹   ób   •K  — | j                  |«      }t        ‰| �  |fi |¤Žƒ d {  –—† S 7 Œ­wr   )r(   r   Úasync_get_cacher4   s      €r   r6   zLLMClientCache.async_get_cacheM   s2   øè ø€ Ø×3Ñ3°CÓ8ˆä‘WÑ,¨SÑ;°FÑ;×;Ð;Ð;ús   ƒ%/¨-©/)éÈ   iX  i   N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   r   r#   r   r(   ÚobjectÚboolr-   r1   r3   r6   Ú__classcell__)r   s   @r   r	   r	      sÂ   ø„ ñð *-Ø"%Ø(,Ø<@ñ\à $™Jð\ð ˜4‘Zð\ð  ™:ð	\ð
  3°TÑ9ð\ð 
õ\ð*˜sð * tõ *ò
ñ7˜Sð 7¨ð 7Àtõ 7ñC¨ð C°Vð CÐSWõ Cô0÷<ð <r   r	   )
r;   r!   Útypingr   r   r   r   Úin_memory_cacher   r	   © r   r   ú<module>rC      s&   ðñó Ý ç UÝ *ôD<�]õ D<r   