@posthog/ai 8.1.11 → 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -530,7 +530,7 @@ function formatOpenAIResponsesInput(input, instructions) {
530
530
  return messages;
531
531
  }
532
532
 
533
- var version = "8.1.11";
533
+ var version = "8.2.0";
534
534
 
535
535
  const DEFAULT_MAX_DEPTH = 3;
536
536
  const MAX_STACK_LINES = 20;
@@ -583,6 +583,44 @@ function truncateStack(stack) {
583
583
  return [...lines.slice(0, MAX_STACK_LINES), '... (truncated)'].join('\n');
584
584
  }
585
585
 
586
+ // Warn when a wrapper's base_url points at the PostHog AI Gateway: the gateway
587
+ // emits its own $ai_generation, so each call would be captured (and, for billable
588
+ // products, billed) twice. We only warn — the wrapper's event carries data the
589
+ // gateway never sees (groups, custom properties, trace hierarchy).
590
+
591
+ // Keep in sync with the gateway's deployed hosts (see services/llm-gateway in the
592
+ // main repo). gateway.us.posthog.com is live today; the rest are listed ahead of
593
+ // any traffic moving to them.
594
+ const POSTHOG_AI_GATEWAY_HOSTS = ['gateway.posthog.com', 'gateway.us.posthog.com', 'gateway.eu.posthog.com', 'ai-gateway.us.posthog.com', 'ai-gateway.eu.posthog.com'];
595
+
596
+ // Swap for the dedicated AI Gateway page once it ships.
597
+ const GATEWAY_DOCS_URL = 'https://posthog.com/docs/ai-observability';
598
+ const extractHost = baseURL => {
599
+ try {
600
+ // Tolerate bare hosts that omit a scheme, e.g. "gateway.us.posthog.com/v1".
601
+ const hasScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(baseURL);
602
+ return new URL(hasScheme ? baseURL : `https://${baseURL}`).hostname.toLowerCase();
603
+ } catch {
604
+ return undefined;
605
+ }
606
+ };
607
+ const isPostHogAiGatewayUrl = baseURL => {
608
+ if (!baseURL) {
609
+ return false;
610
+ }
611
+ const host = extractHost(baseURL);
612
+ return host !== undefined && POSTHOG_AI_GATEWAY_HOSTS.includes(host);
613
+ };
614
+
615
+ // Warns on every gateway call by design: the misconfiguration is impossible to
616
+ // miss that way, and a doubled bill is worse than noisy logs.
617
+ const warnIfPostHogAiGateway = baseURL => {
618
+ if (!isPostHogAiGatewayUrl(baseURL)) {
619
+ return;
620
+ }
621
+ console.warn('[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 ${GATEWAY_DOCS_URL}.`);
622
+ };
623
+
586
624
  /**
587
625
  * Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
588
626
  * directly so that any caller — first-party SDK wrappers and external code
@@ -606,6 +644,7 @@ const captureAiGeneration = async (client, options) => {
606
644
  if (!client.capture) {
607
645
  return;
608
646
  }
647
+ warnIfPostHogAiGateway(options.baseURL);
609
648
  const traceId = options.traceId ?? v4();
610
649
  const eventType = options.eventType ?? AIEvent.Generation;
611
650
  const privacyMode = options.privacyMode ?? false;