Ë
    lQêjõ  ã                   ó¨   — d dl mZmZmZ d dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ  ej$                  e«      Z G d„ d	«      Zy)
é    )ÚClassVarÚDictÚOptionalN)Ú	ServerAPI)ÚBaseHTTPClient)ÚSettingsÚSystem)ÚProductTelemetryClient)ÚClientStartEventc                   ó¾  — e Zd ZU i Zeeeef      ed<   i Z	eeee
f      ed<    ej                  «       Zeej                     ed<   eed<    e«       fdeddfd„Zed	ededefd
„«       Zededefd„«       Zededefd„«       Zededd fd„«       Zed	eddfd„«       Zed	ede
fd„«       Zed	eddfd„«       Zedd„«       Zedefd„«       Zdd„Zedee   fd„«       Zy)ÚSharedSystemClientÚ_identifier_to_systemÚ_identifier_to_refcountÚ_refcount_lockÚ_identifierÚsettingsÚreturnNc                 ó¶   — t         j                  |«      | _        t         j                  | j                  |«       t         j	                  | j                  «       y ©N)r   Ú_get_identifier_from_settingsr   Ú_create_system_if_not_existsÚ_increment_refcount)Úselfr   s     úf/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/chromadb/api/shared_system_client.pyÚ__init__zSharedSystemClient.__init__   sA   € ô .×KÑKÈHÓUˆÔÜ×7Ñ7¸×8HÑ8HÈ(ÔSÜ×.Ñ.¨t×/?Ñ/?Õ@ó    Ú
identifierc                 ó@  — || j                   vrUt        |«      }|| j                   |<   |j                  t        «       |j                  t        «       |j                  «        n-| j                   |   }|j                  |k7  rt        d|› d�«      ‚| j                   |   S )Nz)An instance of Chroma already exists for z with different settings)r   r	   Úinstancer
   r   Ústartr   Ú
ValueError)Úclsr   r   Ú
new_systemÚprevious_systems        r   r   z/SharedSystemClient._create_system_if_not_exists   s¢   € ð ˜S×6Ñ6Ñ6Ü Ó)ˆJØ4>ˆC×%Ñ% jÑ1à×ÑÔ 6Ô7Ø×Ñ¤	Ô*à×ÑÕà!×7Ñ7¸
ÑCˆOð ×'Ñ'¨8Ò3Ü Ø?À
¸|ÐKcÐdóð ð ×(Ñ(¨Ñ4Ð4r   c                 óÞ   — d}| j                   }|€t        d«      ‚|dv r| j                  r| j                  }|S d}|S |dv rt	        t        j                  «       «      }|S t        d|› �«      ‚)NÚ z1Chroma API implementation must be set in settings)zchromadb.api.segment.SegmentAPIz!chromadb.api.rust.RustBindingsAPIÚ	ephemeral)zchromadb.api.fastapi.FastAPIz'chromadb.api.async_fastapi.AsyncFastAPIz&Unsupported Chroma API implementation )Úchroma_api_implr!   Úis_persistentÚpersist_directoryÚstrÚuuidÚuuid4)r   r   Úapi_impls      r   r   z0SharedSystemClient._get_identifier_from_settings3   sŸ   € àˆ
Ø×+Ñ+ˆàÐÜÐPÓQÐQØð 
ñ 
ð ×%Ò%Ø%×7Ñ7�
ð Ðð  ð ð Ðð ð 
ñ 
ô
 œTŸZ™Z›\Ó*ˆJð Ðô ÐEÀhÀZÐPÓQÐQr   Úsystemc                 ój   — t         j                  | j                  «      }| t         j                  |<   |S r   )r   r   r   r   )r/   r   s     r   Ú_populate_data_from_systemz-SharedSystemClient._populate_data_from_systemO   s-   € ä'×EÑEÀfÇoÁoÓVˆ
Ø?EÔ×0Ñ0°Ñ<ØÐr   c                 óT   — t         j                  |«        | |j                  «      }|S )zRCreate a client from an existing system. This is useful for testing and debugging.)r   r1   r   )r"   r/   r   s      r   Úfrom_systemzSharedSystemClient.from_systemU   s%   € ô 	×5Ñ5°fÔ=Ù�v—‘Ó'ˆØˆr   c                 ó®   — | j                   5  || j                  vrd| j                  |<   | j                  |xx   dz  cc<   ddd«       y# 1 sw Y   yxY w)z6Increment the reference count for a system identifier.r   é   N©r   r   )r"   r   s     r   r   z&SharedSystemClient._increment_refcount]   sV   € ð ×Ññ 	9Ø ×!<Ñ!<Ñ<Ø:;�×+Ñ+¨JÑ7Ø×'Ñ'¨
Ó3°qÑ8Ó3÷	9÷ 	9ñ 	9ús   �5AÁAc                 óê   — | j                   5  || j                  v rC| j                  |xx   dz  cc<   | j                  |   }|dk  r| j                  |= |cddd«       S 	 ddd«       y# 1 sw Y   yxY w)zODecrement the reference count for a system identifier and return the new count.r5   r   Nr6   )r"   r   Úcounts      r   Ú_decrement_refcountz&SharedSystemClient._decrement_refcounte   s}   € ð ×Ññ 	Ø˜S×8Ñ8Ñ8Ø×+Ñ+¨JÓ7¸1Ñ<Ó7Ø×3Ñ3°JÑ?�Ø˜A’:Ø×3Ñ3°JÐ?Ø÷	ñ 	ð ÷	÷ 	ñ 	ús   �AA)ÁA)Á)A2c                 ó�   — | j                  |«      }|dk  r0| j                  j                  |d«      }|�|j                  «        yyy)zäDecrement refcount and stop the system if this was the last reference.

        This consolidates the "decrement + conditional stop" pattern used in
        both Client.close() and the Client.__init__ exception handler.
        r   N)r9   r   ÚpopÚstop)r"   r   Úrefcountr/   s       r   Ú_release_systemz"SharedSystemClient._release_systemq   sL   € ð ×*Ñ*¨:Ó6ˆØ�qŠ=Ø×.Ñ.×2Ñ2°:¸tÓDˆFØÐ!Ø—‘•ð "ð r   c                  ó0   — i t         _        i t         _        y r   )r   r   r   © r   r   Úclear_system_cachez%SharedSystemClient.clear_system_cache~   s   € à35ÔÔ0Ø57ÔÕ2r   c                 ó<   — t         j                  | j                     S r   )r   r   r   )r   s    r   Ú_systemzSharedSystemClient._systemƒ   s   € ä!×7Ñ7¸×8HÑ8HÑIÐIr   c                 ót   — | j                   j                  t        «      }|j                  t	        «       «       y r   )rC   r   r
   Úcapturer   )r   Útelemetry_clients     r   Ú_submit_client_start_eventz-SharedSystemClient._submit_client_start_event‡   s*   € ØŸ<™<×0Ñ0Ô1GÓHÐØ× Ñ Ô!1Ó!3Õ4r   c                  ó  — g } t        t        j                  j                  «       «      }|D ]–  }	 |j	                  t
        «      }t        |t        «      sŒ*|j                  «       }d|vrd|vrŒC|j                  «       }d}|j                  «       D ]  \  }}|j                  «       dk(  sŒ|} n |r| j                  |«       Œ˜ | syt        | «      dkD  rt        j!                  d| d   dd › d	�«       | d   S # t        $ r Y ŒÙw xY w)
a
  
        Try to extract api key from existing client instances by checking httpx session headers.

        Requirements to pull api key:
        - must be a BaseHTTPClient instance (ignore RustBindingsAPI and SegmentAPI)
        - must have "api.trychroma.com" or "gcp.trychroma.com" in the _api_url (ignore local/self-hosted instances)
        - must have "x-chroma-token" or "X-Chroma-Token" in the headers

        Returns:
            The first api key found, or None if no client instances have api keys set.
        zapi.trychroma.comzgcp.trychroma.comNzx-chroma-tokenr5   zAMultiple Chroma Cloud clients found, using API key starting with r   é   z...)Úlistr   r   Úvaluesr   r   Ú
isinstancer   Úget_api_urlÚget_request_headersÚitemsÚlowerÚappendÚ	ExceptionÚlenÚloggerÚinfo)	Úapi_keysÚsystems_snapshotr/   Ú
server_apiÚapi_urlÚheadersÚapi_keyÚkeyÚvalues	            r   Ú%get_chroma_cloud_api_key_from_clientsz8SharedSystemClient.get_chroma_cloud_api_key_from_clients‹   s+  € ð !ˆÜÔ 2× HÑ H× OÑ OÓ QÓRÐØ&ò 	ˆFðØ#Ÿ_™_¬YÓ7�
ä! *¬nÔ=àð %×0Ñ0Ó2�à'¨wÑ6Ø+°7Ñ:àà$×8Ñ8Ó:�Ø�Ø")§-¡-£/ò ‘J�C˜Ø—y‘y“{Ð&6Ó6Ø"'˜Ùðñ
 Ø—O‘O GÔ,øð1	ñ: Øô ˆx‹=˜1ÒÜ�K‰KØSÐT\Ð]^ÑT_Ð`bÐabÐTcÐSdÐdgÐhôð ˜‰{Ðøô ò áðús#   °%C:ÁC:Á/;C:Â+C:Ã:	DÄD)r   N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r+   r	   Ú__annotations__r   ÚintÚ	threadingÚLockr   r   r   Úclassmethodr   Ústaticmethodr   r1   r3   r   r9   r>   rA   ÚpropertyrC   rG   r   r^   r@   r   r   r   r      sÅ  … Ø9;Ð˜8 D¨¨f¨Ñ$5Ñ6Ó;Ø8:Ð˜X d¨3°¨8¡nÑ5Ó:Ø/=¨y¯~©~Ó/?€N�H˜YŸ^™^Ñ,Ó?ØÓñ &›ZñAàðAð 
óAð ð5Øð5Ø(0ð5à	ò5ó ð5ð, ð°ð ¸Sò ó ðð6 ð¨6ð °cò ó ðð
 ð ð Ð,@ò ó ðð ð9¨Sð 9°Tò 9ó ð9ð ð	¨Sð 	°Sò 	ó ð	ð ð
¨ð 
°ò 
ó ð
ð ò8ó ð8ð ðJ˜ò Jó ðJó5ð ð5°8¸C±=ò 5ó ñ5r   r   )Útypingr   r   r   Úloggingrd   r,   Úchromadb.apir   Úchromadb.api.base_http_clientr   Úchromadb.configr   r	   Úchromadb.telemetry.productr
   Ú!chromadb.telemetry.product.eventsr   Ú	getLoggerr_   rT   r   r@   r   r   ú<module>rq      sA   ðß +Ñ +Û Û Û Ý "Ý 8ß ,Ý =Ý >à	ˆ×	Ñ	˜8Ó	$€÷sò sr   