
    jKjS                        d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZmZmZ e G d d             Z	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ G d d      Zy)u  The capture pipeline: sanitize -> truncate -> fan out into ``$mcp_*`` /
``$exception`` payloads -> ``before_send`` -> ``Client.capture()``.

``process_mcp_event`` is the single source of truth for the transform, so tests
assert on exactly the payloads that reach ``capture()``. ``McpEventSink`` wraps a
user-supplied posthog ``Client`` and does the actual capture. The SDK never owns
the client lifecycle — the host constructs it and calls ``shutdown()``.
    )annotationsN)	dataclass)ListOptionalTuple)Client   )_uuid7new_prefixed_id)log)PostHogCaptureEventbuild_posthog_capture_events)sanitize_event)truncate_event)BeforeSendFnEventMcpEventc                  .    e Zd ZU dZdZded<   dZded<   y)McpCaptureOptionszBPer-event toggles consulted by the sink when fanning out an event.Tboolenable_exception_autocaptureNOptional[BeforeSendFn]before_send)__name__
__module____qualname____doc__r   __annotations__r        b/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/posthog/mcp/_sink.pyr   r      s    L)- $-*.K'.r    r   c                  K   | }	 t        |      }	 t        |      }|j	                  d      st        d      |d<   t        ||j                        }t        ||j                         d{   }||fS # t        $ r}t        d|        Y d}~yd}~ww xY w# t        $ r}t        d|        Y d}~yd}~ww xY w7 Pw)zRun an MCP event through the full transform. Returns ``None`` (and logs)
    if a transform stage raises, so the event is dropped rather than partially
    sent. Payloads dropped by ``before_send`` are filtered out.zFailed to sanitize event: NzFailed to truncate event: idevt)
r   	Exceptionr   r   getr   r   r   _apply_before_sendr   )eventoptions	processederrbuiltcapturess         r!   process_mcp_eventr.   &   s       I"9-	
"9-	
 ==)%0	$(777E (w/B/BCCHh#  (./  (./ Ds]   B>A4 B AB>+B<,B>4	B=BB>BB>	B9!B4/B>4B99B>c                  K   || S g }| D ]=  }	  ||      }t        j                  |      r
| d {   }|r|j                  |       ? |S 7 # t        $ r*}t	        d|j                  d       d|        Y d }~rd }~ww xY ww)Nzbefore_send threw for event r(   z; dropping it: )inspectisawaitableappendr%   r   r&   )r-   r   keptcaptureresultr+   s         r!   r'   r'   D   s      &(D		 )F""6*%F#  K &  	.w{{7/C.DOTWSXY 	s>   B"AAA	BA	B A=8B=BBc                       e Zd ZdZddZddZy)McpEventSinkzWraps a user-supplied posthog ``Client`` and pushes events through the
    pipeline. Errors at any stage are logged and the event dropped, never
    re-raised into tool code.c                    || _         y )N)_posthog)selfposthogs     r!   __init__zMcpEventSink.__init__^   s	    r    c                  K   t        ||       d {   }|y |\  }}	 |D ]B  }| j                  j                  |d   |d   |d   |j                  d      t	                      D t        d|j                  d       d|j                  d	       d|j                  d
       d|j                  d      xs d        y 7 # t        $ r*}t        d|j                  d       d|        Y d }~y d }~ww xY ww)Nr(   distinct_id
properties	timestamp)r>   r?   r@   uuidzCaptured PostHog event r#   z | 
event_typedurationz ms | identify_actor_given_id	anonymousz Failed to capture PostHog event z: )r.   r9   r4   r&   r
   r   r%   )r:   r(   r)   r5   
full_eventr-   capture_eventr+   s           r!   r4   zMcpEventSink.capturea   s    (88>%
H	R!)%%!'* -m <,\:+//< &  "* )*..*>)?s:>>R^C_B``c>>*-.fZ^^D]5^5mbm4np 9$  	R2:>>$3G2H3%PQQ	Rs9   C7B?C7BC >C7	C4
 C/*C7/C44C7N)r;   r   returnNone)r(   r   r)   r   rH   rI   )r   r   r   r   r<   r4   r   r    r!   r7   r7   Y   s    ! Rr    r7   )r(   r   r)   r   rH   z1Optional[Tuple[Event, List[PostHogCaptureEvent]]])r-   List[PostHogCaptureEvent]r   r   rH   rJ   ) r   
__future__r   r0   dataclassesr   typingr   r   r   posthog.clientr   _idsr
   r   loggerr   _posthog_eventsr   r   _sanitizationr   _truncationr   typesr   r   r   r   r.   r'   r7   r   r    r!   <module>rU      s   
 #  ! ( ( ! )  N ) ' 0 0 / / //6<'6L*R Rr    