
    jKj                         d dl Z d dlZd dlmZmZmZ d dlmZ  e j                  d      Z	g dZ
dZ ej                  dej                        ZdZd	ed
ee   fdZd	ed
efdZd	ed
dfdZdeeeef      d
dfdZy)    N)AnyMappingOptional)urlparseposthog)zgateway.posthog.comzgateway.us.posthog.comzgateway.eu.posthog.comzai-gateway.us.posthog.comzai-gateway.eu.posthog.comz)https://posthog.com/docs/ai-observabilityz^[a-z][a-z0-9+.-]*://)zserver.addresszurl.fullbase_urlreturnc                     	 t         j                  |       r| nd|  }t        |      j                  }|r|j	                         S d S # t
        $ r Y y w xY w)Nzhttps://)
_SCHEME_REmatchr   hostnamelower	Exception)r   urlhosts      c/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/posthog/ai/gateway.py_extract_hostr   $   sV    $**84hHXJ:O}%%#tzz|-- s   AA A 	AAc                 L    | syt        t        |             }|duxr |t        v S )zBReturn True if base_url points at a known PostHog AI Gateway host.FN)r   strPOSTHOG_AI_GATEWAY_HOSTS)r   r   s     r   is_posthog_ai_gateway_urlr   .   s,    X'Dt@(@ @@    c                 P    t        |       syt        j                  dt               y)at  
    Warn when an AI wrapper is pointed at the PostHog AI Gateway.

    Warns on every gateway call by design: the misconfiguration is impossible to
    miss that way, and a doubled bill is worse than noisy logs. We only warn and
    never drop the event, because the wrapper event carries data the gateway
    never sees (groups, custom properties, trace hierarchy).
    Nu   [PostHog] The PostHog AI wrapper is pointed at the PostHog AI Gateway. Both capture $ai_generation, so every call is double-counted and double-billed. Use one or the other — see %s.)r   logwarning_GATEWAY_DOCS_URL)r   s    r   warn_if_posthog_ai_gatewayr   6   s%     %X.KK	: 		r   
attributesc                     | syt         D ]<  }| j                  |      }t        |t              s%t	        |      s1t        |        y y)zMWarn at most once per span when its host/URL attributes point at the gateway.N)_OTEL_GATEWAY_URL_ATTRIBUTESget
isinstancer   r   r   )r   keyvalues      r   *warn_if_posthog_ai_gateway_otel_attributesr%   I   s?     +s#eS!&?&F&u-	 ,r   )loggingretypingr   r   r   urllib.parser   	getLoggerr   r   r   compile
IGNORECASEr   r    r   r   boolr   r   r%    r   r   <module>r/      s     	 ) ) !g	"
  @ RZZ0"--@
  > C HSM A A A  &
c*+
	
r   