Ë
    tPêj\  ã                   óR  — U d Z ddlZddl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mZ  edd	d
ddddœg d¢ddd¬«	      Zeed<    ee«      \  ZZdada e ej.                  dd«      «      dz  Zeed<   dedefd„Z	 	 	 	 	 	 	 	 	 ddeez  ee   z  ee   z  dz  fd„Zd„ Zy)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)ÚAsyncIterableÚIterable)ÚFinal)Úurlparse)Úverbose_logger)ÚMockClientConfigÚMockResponseÚcreate_mock_client_factoryÚ
BRAINTRUSTÚBRAINTRUST_MOCKéd   éÈ   zmock-project-idÚsuccess)ÚidÚstatus)ú.braintrustdata.comúbraintrustdata.comú.braintrust.devúbraintrust.devTF)Údefault_latency_msÚdefault_status_codeÚdefault_json_dataÚurl_matchersÚpatch_async_handlerÚpatch_sync_clientÚpatch_http_handlerÚ_configÚBRAINTRUST_MOCK_LATENCY_MSÚ100g     @�@Ú_MOCK_LATENCY_SECONDSÚurlÚreturnc                 óà   — t        | t        «      syt        | «      }|j                  xs dj	                  «       }|sy|dk(  xs+ |j                  d«      xs |dk(  xs |j                  d«      S )z%Check if URL is a Braintrust API URL.FÚ r   r   r   r   )Ú
isinstanceÚstrr   ÚhostnameÚlowerÚendswith)r!   ÚparsedÚhosts      úp/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/integrations/braintrust_mock_client.pyÚ_is_braintrust_urlr-   ;   sy   € ä�cœ3ÔØä˜S“M€FØ—?‘?Ò( b×/Ñ/Ó1€DáØð 	Ð$Ñ$ò 	,Ø�=‰=Ð.Ó/ò	,àÐ#Ñ#ò	,ð �=‰=Ð*Ó+ð	ó    Úcontentc                 óž  — t        |t        «      r—t        |«      rŒt        j                  d|«       t        j                  t        «       d|v r|r|j                  dd«      nd}d|› �|dœ}nd|v rdd	i}nt        j                  }t        t        j                  ||t        ¬
«      S t        �t        | |||||||||	|
¬«      S t        d«      ‚)zbMonkey-patched HTTPHandler.post that intercepts Braintrust calls with endpoint-specific responses.z[BRAINTRUST MOCK] POST to %sz/projectÚnameÚlitellmzmock-project-id-)r   r1   z/project_logsr   r   )Ústatus_codeÚ	json_datar!   Úelapsed_seconds)
r!   ÚdataÚjsonÚparamsÚheadersÚtimeoutÚstreamÚfilesr/   Úlogging_objz'Original HTTPHandler.post not available)r%   r&   r-   r   ÚinfoÚtimeÚsleepr    Úgetr   r   r	   r   Ú_original_http_handler_postÚRuntimeError)Úselfr!   r6   r7   r8   r9   r:   r;   r<   r/   r=   Úproject_nameÚ	mock_datas                r,   Ú_mock_http_handler_postrG   N   sá   € ô �#”sÔÔ 2°3Ô 7Ü×ÑÐ:¸CÔ@Ü�
‰
Ô(Ô)à˜ÑáAE $§(¡(¨6°9Ô"=È9ˆLØ!1°,°Ð@È,ÑW‰IØ Ñ#à! 9Ð-‰Iä×1Ñ1ˆIÜÜ×3Ñ3ØØÜ1ô	
ð 	
ô #Ð.Ü*ØØØØØØØØØØØ#ô
ð 	
ô Ð@Ó
AÐAr.   c                  ó0  — t         ryt        j                  d«       ddlm}  t
        €,| j                  at        | _        t        j                  d«       t        «        t        j                  dt        dz  d›d	�«       t        j                  d
«       da y)a\  
    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   ÚdebugÚ&litellm.llms.custom_httpx.http_handlerrJ   rB   ÚpostrG   Ú%create_mock_braintrust_factory_clientr    rI   s    r,   Úcreate_mock_braintrust_clientrQ   �   s†   € õ  Øä×ÑÐSÔTåBä"Ð*Ø&1×&6Ñ&6Ð#Ü2ˆÔÜ×ÑÐIÔJô *Ô+ä×ÑÐAÔBWÐZ^ÑB^Ð_bÐAcÐceÐfÔgÜ×ÑÐ[Ô\àÑr.   )	NNNNNFNNN)Ú__doc__Úosr?   Úcollections.abcr   r   Útypingr   Úurllib.parser   Úlitellm._loggingr   Ú(litellm.integrations.mock_client_factoryr   r	   r
   r   Ú__annotations__rP   Úshould_use_braintrust_mockrB   rL   ÚfloatÚgetenvr    r&   Úboolr-   ÚbytesrG   rQ   © r.   r,   ú<module>r`      s  ðòó 
Û ß 3Ý Ý !å +÷ñ ñ "ØØØØØ.¸)ÑDòð ØØô€ˆó ñ, ˜wÓ'ñØ)Øð #Ð ØÐ ñ  % Y R§Y¡YÐ/KÈUÓ%SÓTÐW]Ñ]Ð �uÓ ]ð˜Cð  Dó ð, 
Ø	ØØØØØ
ØKOØñ0Bð �5‰[˜8 E™?Ñ*¨]¸5Ñ-AÑAÀDÑHó0Bóf#r.   