o
    ưi                     @   s   d 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	m
Z
 edddd	d
ddg ddddd	Ze
e\ZZdadaeeddd ZdedefddZdddZdd ZdS )a)  
Mock HTTP client for Braintrust integration testing.

This module intercepts Braintrust API calls and returns successful mock responses,
allowing full code execution without making actual network calls.

Usage:
    Set BRAINTRUST_MOCK=true in environment variables or config to enable mock mode.
    N)urlparse)verbose_logger)MockClientConfigMockResponsecreate_mock_client_factoryZ
BRAINTRUSTZBRAINTRUST_MOCKd      zmock-project-idsuccess)idstatus).braintrustdata.combraintrustdata.com.braintrust.devbraintrust.devTF)Zdefault_latency_msdefault_status_codedefault_json_dataZurl_matchersZpatch_async_handlerZpatch_sync_clientZpatch_http_handlerZBRAINTRUST_MOCK_LATENCY_MS100g     @@urlreturnc                 C   sP   t | tsdS t| }|jpd }|sdS |dkp'|dp'|dkp'|dS )z%Check if URL is a Braintrust API URL.F r   r   r   r   )
isinstancestrr   hostnamelowerendswith)r   parsedhost r   b/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/braintrust_mock_client.py_is_braintrust_url2   s   
r   c                 C   s   t |trBt|rBtd|  tt d|v r-|r"|ddnd}d| |d}nd|v r6dd	i}nt	j
}tt	j||td
S tdurUt| |||||||||	|
dS td)zbMonkey-patched HTTPHandler.post that intercepts Braintrust calls with endpoint-specific responses.z[BRAINTRUST MOCK] POST to z/projectnameZlitellmzmock-project-id-)r
   r    z/project_logsr   r	   )status_codeZ	json_datar   Zelapsed_secondsN)
r   datajsonparamsheaderstimeoutstreamfilescontentlogging_objz'Original HTTPHandler.post not available)r   r   r   r   infotimesleep_MOCK_LATENCY_SECONDSget_configr   r   r   _original_http_handler_postRuntimeError)selfr   r"   r#   r$   r%   r&   r'   r(   r)   r*   project_nameZ	mock_datar   r   r   _mock_http_handler_postE   s$   

r5   c                  C   sl   t rdS td ddlm}  tdu r| jat| _td t  tdt	d dd	 td
 da dS )ah  
    Monkey-patch HTTPHandler.post to intercept Braintrust sync calls.
    
    Braintrust uses HTTPHandler for sync calls and AsyncHTTPHandler for async calls.
    HTTPHandler.post uses self.client.send(), not self.client.post(), so we need
    custom patching for sync (similar to Helicone).
    AsyncHTTPHandler.post is patched by the factory.
    
    We use custom patching instead of factory's patch_http_handler because we need
    endpoint-specific responses (different for /project vs /project_logs).
    
    This function is idempotent - it only initializes mocks once, even if called multiple times.
    Nz8[BRAINTRUST MOCK] Initializing Braintrust mock client...r   HTTPHandlerz*[BRAINTRUST MOCK] Patched HTTPHandler.postz&[BRAINTRUST MOCK] Mock latency set to i  z.0fmsz@[BRAINTRUST MOCK] Braintrust mock client initialization completeT)
_mocks_initializedr   debugZ&litellm.llms.custom_httpx.http_handlerr7   r1   postr5   %create_mock_braintrust_factory_clientr.   r6   r   r   r   create_mock_braintrust_client`   s   


r=   )	NNNNNFNNN)__doc__osr,   urllib.parser   Zlitellm._loggingr   Z(litellm.integrations.mock_client_factoryr   r   r   r0   r<   Zshould_use_braintrust_mockr1   r9   floatgetenvr.   r   boolr   r5   r=   r   r   r   r   <module>   s0    

