
    Zi                         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dS )
    )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dS )SharedSystemClient_identifier_to_system_identifier_to_refcount_refcount_lock_identifiersettingsreturnNc                     t                               |          | _        t                               | j        |           t                               | j                   d S N)r   _get_identifier_from_settingsr   _create_system_if_not_exists_increment_refcount)selfr   s     ^/root/projects/butler/venv/lib64/python3.11/site-packages/chromadb/api/shared_system_client.py__init__zSharedSystemClient.__init__   sP     .KKHUU778H(SSS..t/?@@@@@    
identifierc                 H   || j         vrbt          |          }|| j         |<   |                    t                     |                    t                     |                                 n+| j         |         }|j        |k    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))J4>C%j1 6777	***!7
CO '833 d
ddd   (44r   c                     d}| j         }|t          d          |dv r| j        r| j        }n:d}n7|dv r!t	          t          j                              }nt          d|           |S )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Q 
 
 
 % %7

   
  
 
 

 TZ\\**JJPhPPQQQr   systemc                 b    t                               | j                  }| t           j        |<   |S r   )r   r   r   r   )r/   r   s     r   _populate_data_from_systemz-SharedSystemClient._populate_data_from_systemO   s+    'EEfoVV
?E0<r   c                 Z    t                               |            | |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5f===3v''r   c                     | j         5  || j        vr
d| j        |<   | j        |xx         dz  cc<   ddd           dS # 1 swxY w Y   dS )z6Increment the reference count for a system identifier.r      Nr   r   )r"   r   s     r   r   z&SharedSystemClient._increment_refcount]   s      	9 	9!<<<:;+J7'
333q8333	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9s   )>AAc                     | j         5  || j        v r>| j        |xx         dz  cc<   | j        |         }|dk    r| j        |= |cddd           S 	 ddd           dS # 1 swxY w Y   dS )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771<7773J?A::3J?	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ;AAA"%A"c                     |                      |          }|dk    r3| j                            |d          }||                                 dS dS dS )zDecrement 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   s\     **:66q==.22:tDDF! =!!r   c                  6    i t           _        i t           _        d S r   )r   r   r    r   r   clear_system_cachez%SharedSystemClient.clear_system_cache~   s    35057222r   c                 0    t           j        | j                 S r   )r   r   r   )r   s    r   _systemzSharedSystemClient._system   s    !78HIIr   c                     | j                             t                    }|                    t	                                 d S r   )rC   r   r
   capturer   )r   telemetry_clients     r   _submit_client_start_eventz-SharedSystemClient._submit_client_start_event   s:    <001GHH  !1!3!344444r   c                  ~   g } t          t          j                                                  }|D ]}	 |                    t
                    }t          |t                    s3|                                }d|vrd|vrP|	                                }d}|
                                D ]!\  }}|                                dk    r|} n"|r|                     |           # t          $ r Y w xY w| sdS t          |           dk    r,t                              d| d         dd          d	           | d         S )
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 O O Q QRR& 	 	F#__Y77
!*n==  %0022'w66+7::$88::")--//  JCyy{{&666"' 7  -OOG,,,     	4 x==1KKhT\]^T_`bab`bTchhh   {s   /C$#C$ A#C$$
C10C1)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;8Df$56;;;8:Xd38n5:::/=y~/?/?NHY^,??? &XZZA AA 
A A A A 55(05	5 5 5 [5,  S    \6 6 c    \
  ,@    [ 9S 9T 9 9 9 [9 	S 	S 	 	 	 [	 
 
 
 
 
 [
 8 8 8 \8 J J J J XJ5 5 5 5 58C= 5 5 5 \5 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      s    + + + + + + + + + +       " " " " " " 8 8 8 8 8 8 , , , , , , , , = = = = = = > > > > > >		8	$	$s s s s s s s s s sr   