@posthog/ai 8.20.2 → 8.21.1

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.
Files changed (62) hide show
  1. package/README.md +20 -0
  2. package/dist/adk/index.cjs +101 -22
  3. package/dist/adk/index.cjs.map +1 -1
  4. package/dist/adk/index.d.ts +7 -0
  5. package/dist/adk/index.mjs +102 -23
  6. package/dist/adk/index.mjs.map +1 -1
  7. package/dist/anthropic/index.cjs +92 -9
  8. package/dist/anthropic/index.cjs.map +1 -1
  9. package/dist/anthropic/index.d.ts +7 -0
  10. package/dist/anthropic/index.mjs +93 -10
  11. package/dist/anthropic/index.mjs.map +1 -1
  12. package/dist/claude-agent-sdk/index.cjs +107 -21
  13. package/dist/claude-agent-sdk/index.cjs.map +1 -1
  14. package/dist/claude-agent-sdk/index.d.ts +6 -0
  15. package/dist/claude-agent-sdk/index.mjs +108 -22
  16. package/dist/claude-agent-sdk/index.mjs.map +1 -1
  17. package/dist/gemini/index.cjs +92 -9
  18. package/dist/gemini/index.cjs.map +1 -1
  19. package/dist/gemini/index.d.ts +7 -0
  20. package/dist/gemini/index.mjs +92 -9
  21. package/dist/gemini/index.mjs.map +1 -1
  22. package/dist/genkit/index.cjs +114 -29
  23. package/dist/genkit/index.cjs.map +1 -1
  24. package/dist/genkit/index.d.ts +7 -0
  25. package/dist/genkit/index.mjs +114 -29
  26. package/dist/genkit/index.mjs.map +1 -1
  27. package/dist/index.cjs +90 -7
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.ts +11 -0
  30. package/dist/index.mjs +91 -8
  31. package/dist/index.mjs.map +1 -1
  32. package/dist/langchain/index.cjs +75 -7
  33. package/dist/langchain/index.cjs.map +1 -1
  34. package/dist/langchain/index.d.ts +7 -0
  35. package/dist/langchain/index.mjs +75 -7
  36. package/dist/langchain/index.mjs.map +1 -1
  37. package/dist/langchain/middleware/index.cjs +75 -7
  38. package/dist/langchain/middleware/index.cjs.map +1 -1
  39. package/dist/langchain/middleware/index.d.ts +6 -0
  40. package/dist/langchain/middleware/index.mjs +75 -7
  41. package/dist/langchain/middleware/index.mjs.map +1 -1
  42. package/dist/openai/index.cjs +91 -8
  43. package/dist/openai/index.cjs.map +1 -1
  44. package/dist/openai/index.d.ts +7 -0
  45. package/dist/openai/index.mjs +92 -9
  46. package/dist/openai/index.mjs.map +1 -1
  47. package/dist/openai-agents/index.cjs +115 -46
  48. package/dist/openai-agents/index.cjs.map +1 -1
  49. package/dist/openai-agents/index.d.ts +7 -0
  50. package/dist/openai-agents/index.mjs +115 -46
  51. package/dist/openai-agents/index.mjs.map +1 -1
  52. package/dist/otel/index.browser.cjs.map +1 -1
  53. package/dist/otel/index.browser.mjs.map +1 -1
  54. package/dist/otel/index.cjs.map +1 -1
  55. package/dist/otel/index.d.ts +3 -3
  56. package/dist/otel/index.mjs.map +1 -1
  57. package/dist/vercel/index.cjs +90 -7
  58. package/dist/vercel/index.cjs.map +1 -1
  59. package/dist/vercel/index.d.ts +6 -0
  60. package/dist/vercel/index.mjs +91 -8
  61. package/dist/vercel/index.mjs.map +1 -1
  62. package/package.json +5 -5
@@ -1,13 +1,82 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  require("uuid");
3
+ let _posthog_core = require("@posthog/core");
3
4
  //#region src/captureAiEvent.ts
4
5
  /** @internal */
5
6
  function isFullAiCaptureEnabled(client) {
6
7
  return client?.enableFullAiCapture === true;
7
8
  }
9
+ /**
10
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
11
+ *
12
+ * @internal
13
+ */
14
+ function userDistinctId(value) {
15
+ if (!value) return;
16
+ const id = String(value);
17
+ return id.trim() === "" ? void 0 : id;
18
+ }
19
+ /**
20
+ * The caller's per-event options when they are an object. A missing or `null` value means no
21
+ * options. An array or any other value is reported and ignored, so it is never spread.
22
+ *
23
+ * @internal
24
+ */
25
+ function readAiEventOptions(value) {
26
+ if (value === void 0 || value === null) return;
27
+ if (typeof value === "object" && !Array.isArray(value)) return value;
28
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
29
+ }
30
+ /**
31
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
32
+ * processing off and beats the caller's `process_person_profile`.
33
+ *
34
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
35
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
36
+ * reads, and an unknown key is dropped.
37
+ *
38
+ * @internal
39
+ */
40
+ function eventOptionFields(client, properties, options, personless) {
41
+ const userOptions = readAiEventOptions(options);
42
+ if (isPostHogNode6(client)) {
43
+ const eventOptions = {
44
+ ...userOptions,
45
+ ...personless ? { process_person_profile: false } : {}
46
+ };
47
+ return Object.keys(eventOptions).length > 0 ? {
48
+ properties,
49
+ options: eventOptions
50
+ } : { properties };
51
+ }
52
+ const withOptionProperties = {
53
+ ...properties,
54
+ ...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
55
+ };
56
+ if (personless) withOptionProperties.$process_person_profile = false;
57
+ return { properties: withOptionProperties };
58
+ }
59
+ /**
60
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
61
+ *
62
+ * @internal
63
+ */
64
+ function isPostHogNode6(client) {
65
+ return (0, _posthog_core.acceptsEventOptions)(client);
66
+ }
67
+ /**
68
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
69
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
70
+ * upgrade never changes how a 5.x client sends.
71
+ *
72
+ * @internal
73
+ */
74
+ function usesAiLane(client) {
75
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
76
+ }
8
77
  /** @internal */
9
78
  function captureAiEvent(client, event) {
10
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
79
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
11
80
  client.captureAi(event);
12
81
  return;
13
82
  }
@@ -373,7 +442,7 @@ function summarizeErrorForPrivacy(error) {
373
442
  }
374
443
  //#endregion
375
444
  //#region package.json
376
- var version = "8.20.2";
445
+ var version = "8.21.1";
377
446
  //#endregion
378
447
  //#region src/gatewayWarning.ts
379
448
  const POSTHOG_AI_GATEWAY_HOSTS = [
@@ -497,16 +566,12 @@ var PostHogTracingProcessor = class {
497
566
  this._privacyMode = options.privacyMode ?? false;
498
567
  this._groups = options.groups ?? {};
499
568
  this._properties = options.properties ?? {};
569
+ this._eventOptions = options.options;
500
570
  this._onError = options.onError;
501
571
  }
502
572
  _getDistinctId(trace) {
503
- if (typeof this._distinctId === "function") {
504
- if (trace) {
505
- const result = this._distinctId(trace);
506
- if (result) return String(result);
507
- }
508
- return;
509
- } else if (this._distinctId) return String(this._distinctId);
573
+ if (typeof this._distinctId === "function") return trace ? userDistinctId(this._distinctId(trace)) : void 0;
574
+ return userDistinctId(this._distinctId);
510
575
  }
511
576
  _withPrivacyMode(value) {
512
577
  return withPrivacyMode(this._client, this._privacyMode, value);
@@ -534,7 +599,7 @@ var PostHogTracingProcessor = class {
534
599
  this._onError?.(error, context);
535
600
  } catch (handlerError) {}
536
601
  }
537
- _captureEvent(event, properties, distinctId) {
602
+ _captureEvent(event, properties, identity) {
538
603
  try {
539
604
  if (!this._client?.capture) return;
540
605
  const finalProperties = {
@@ -542,9 +607,9 @@ var PostHogTracingProcessor = class {
542
607
  ...properties
543
608
  };
544
609
  const eventMessage = {
545
- distinctId: distinctId || "unknown",
610
+ distinctId: identity.distinctId || "unknown",
546
611
  event,
547
- properties: finalProperties,
612
+ ...eventOptionFields(this._client, finalProperties, this._eventOptions, identity.personless),
548
613
  groups: Object.keys(this._groups).length > 0 ? this._groups : void 0
549
614
  };
550
615
  captureAiEvent(this._client, eventMessage);
@@ -629,8 +694,10 @@ var PostHogTracingProcessor = class {
629
694
  properties.$ai_group_id = groupId;
630
695
  }
631
696
  if (metadata && Object.keys(metadata).length > 0) properties.$ai_trace_metadata = this._prepareCapturedValue(metadata);
632
- if (distinctId == null) properties.$process_person_profile = false;
633
- this._captureEvent("$ai_trace", properties, distinctId ?? traceId);
697
+ this._captureEvent("$ai_trace", properties, {
698
+ distinctId: distinctId ?? traceId,
699
+ personless: distinctId == null
700
+ });
634
701
  } catch (error) {
635
702
  this._handleError(error, "onTraceEnd");
636
703
  }
@@ -662,39 +729,41 @@ var PostHogTracingProcessor = class {
662
729
  const userDistinctId = traceInfo?.distinctId ?? this._getDistinctId(null);
663
730
  const groupId = traceInfo?.groupId ?? null;
664
731
  const errorProperties = this._getErrorProperties(span.error);
665
- if (userDistinctId == null) errorProperties.$process_person_profile = false;
666
- const distinctId = userDistinctId ?? traceId;
732
+ const identity = {
733
+ distinctId: userDistinctId ?? traceId,
734
+ personless: userDistinctId == null
735
+ };
667
736
  switch (spanData.type) {
668
737
  case "generation":
669
- this._handleGenerationSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
738
+ this._handleGenerationSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
670
739
  break;
671
740
  case "response":
672
- this._handleResponseSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
741
+ this._handleResponseSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
673
742
  break;
674
743
  case "function":
675
- this._handleFunctionSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
744
+ this._handleFunctionSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
676
745
  break;
677
746
  case "agent":
678
- this._handleAgentSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
747
+ this._handleAgentSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
679
748
  break;
680
749
  case "handoff":
681
- this._handleHandoffSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
750
+ this._handleHandoffSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
682
751
  break;
683
752
  case "guardrail":
684
- this._handleGuardrailSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
753
+ this._handleGuardrailSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
685
754
  break;
686
755
  case "custom":
687
- this._handleCustomSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
756
+ this._handleCustomSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
688
757
  break;
689
758
  case "transcription":
690
759
  case "speech":
691
760
  case "speech_group":
692
- this._handleAudioSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
761
+ this._handleAudioSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
693
762
  break;
694
763
  case "mcp_tools":
695
- this._handleMcpSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
764
+ this._handleMcpSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
696
765
  break;
697
- default: this._handleGenericSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties);
766
+ default: this._handleGenericSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties);
698
767
  }
699
768
  } catch (error) {
700
769
  this._handleError(error, "onSpanEnd");
@@ -716,7 +785,7 @@ var PostHogTracingProcessor = class {
716
785
  this._handleError(error, "forceFlush");
717
786
  }
718
787
  }
719
- _handleGenerationSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
788
+ _handleGenerationSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
720
789
  const rawResponse = spanData.output?.[0];
721
790
  const rawResponseUsage = rawResponse?.usage;
722
791
  const usage = spanData.usage ?? rawResponseUsage ?? {};
@@ -760,9 +829,9 @@ var PostHogTracingProcessor = class {
760
829
  if (usage.reasoning_tokens) properties.$ai_reasoning_tokens = usage.reasoning_tokens;
761
830
  if (usage.cache_read_input_tokens) properties.$ai_cache_read_input_tokens = usage.cache_read_input_tokens;
762
831
  if (usage.cache_creation_input_tokens) properties.$ai_cache_creation_input_tokens = usage.cache_creation_input_tokens;
763
- this._captureEvent("$ai_generation", properties, distinctId);
832
+ this._captureEvent("$ai_generation", properties, identity);
764
833
  }
765
- _handleResponseSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
834
+ _handleResponseSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
766
835
  const responseSpanData = spanData;
767
836
  const response = responseSpanData._response;
768
837
  const responseId = spanData.response_id ?? response?.id;
@@ -786,9 +855,9 @@ var PostHogTracingProcessor = class {
786
855
  const outputTokenDetails = usage.output_tokens_details;
787
856
  if (outputTokenDetails?.reasoning_tokens) properties.$ai_reasoning_tokens = outputTokenDetails.reasoning_tokens;
788
857
  if (response?.output) properties.$ai_output_choices = this._prepareCapturedValue(response.output);
789
- this._captureEvent("$ai_generation", properties, distinctId);
858
+ this._captureEvent("$ai_generation", properties, identity);
790
859
  }
791
- _handleFunctionSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
860
+ _handleFunctionSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
792
861
  const properties = {
793
862
  ...this._baseProperties(traceId, spanId, parentId, latency, groupId, errorProperties),
794
863
  $ai_span_name: spanData.name,
@@ -797,9 +866,9 @@ var PostHogTracingProcessor = class {
797
866
  $ai_output_state: this._prepareCapturedValue(spanData.output)
798
867
  };
799
868
  if (spanData.mcp_data) properties.$ai_mcp_data = this._prepareCapturedValue(spanData.mcp_data);
800
- this._captureEvent("$ai_span", properties, distinctId);
869
+ this._captureEvent("$ai_span", properties, identity);
801
870
  }
802
- _handleAgentSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
871
+ _handleAgentSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
803
872
  const properties = {
804
873
  ...this._baseProperties(traceId, spanId, parentId, latency, groupId, errorProperties),
805
874
  $ai_span_name: spanData.name,
@@ -808,9 +877,9 @@ var PostHogTracingProcessor = class {
808
877
  if (spanData.handoffs) properties.$ai_agent_handoffs = spanData.handoffs;
809
878
  if (spanData.tools) properties.$ai_agent_tools = spanData.tools;
810
879
  if (spanData.output_type) properties.$ai_agent_output_type = spanData.output_type;
811
- this._captureEvent("$ai_span", properties, distinctId);
880
+ this._captureEvent("$ai_span", properties, identity);
812
881
  }
813
- _handleHandoffSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
882
+ _handleHandoffSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
814
883
  const properties = {
815
884
  ...this._baseProperties(traceId, spanId, parentId, latency, groupId, errorProperties),
816
885
  $ai_span_name: `${spanData.from_agent} -> ${spanData.to_agent}`,
@@ -818,27 +887,27 @@ var PostHogTracingProcessor = class {
818
887
  $ai_handoff_from_agent: spanData.from_agent,
819
888
  $ai_handoff_to_agent: spanData.to_agent
820
889
  };
821
- this._captureEvent("$ai_span", properties, distinctId);
890
+ this._captureEvent("$ai_span", properties, identity);
822
891
  }
823
- _handleGuardrailSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
892
+ _handleGuardrailSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
824
893
  const properties = {
825
894
  ...this._baseProperties(traceId, spanId, parentId, latency, groupId, errorProperties),
826
895
  $ai_span_name: spanData.name,
827
896
  $ai_span_type: "guardrail",
828
897
  $ai_guardrail_triggered: spanData.triggered
829
898
  };
830
- this._captureEvent("$ai_span", properties, distinctId);
899
+ this._captureEvent("$ai_span", properties, identity);
831
900
  }
832
- _handleCustomSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
901
+ _handleCustomSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
833
902
  const properties = {
834
903
  ...this._baseProperties(traceId, spanId, parentId, latency, groupId, errorProperties),
835
904
  $ai_span_name: spanData.name,
836
905
  $ai_span_type: "custom",
837
906
  $ai_custom_data: this._prepareCapturedValue(spanData.data)
838
907
  };
839
- this._captureEvent("$ai_span", properties, distinctId);
908
+ this._captureEvent("$ai_span", properties, identity);
840
909
  }
841
- _handleAudioSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
910
+ _handleAudioSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
842
911
  const spanType = spanData.type;
843
912
  const properties = {
844
913
  ...this._baseProperties(traceId, spanId, parentId, latency, groupId, errorProperties),
@@ -859,9 +928,9 @@ var PostHogTracingProcessor = class {
859
928
  const speechGroup = spanData;
860
929
  if (speechGroup.input) properties.$ai_input = this._prepareCapturedValue(speechGroup.input);
861
930
  }
862
- this._captureEvent("$ai_span", properties, distinctId);
931
+ this._captureEvent("$ai_span", properties, identity);
863
932
  }
864
- _handleMcpSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
933
+ _handleMcpSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
865
934
  const properties = {
866
935
  ...this._baseProperties(traceId, spanId, parentId, latency, groupId, errorProperties),
867
936
  $ai_span_name: `mcp:${spanData.server}`,
@@ -869,16 +938,16 @@ var PostHogTracingProcessor = class {
869
938
  $ai_mcp_server: spanData.server,
870
939
  $ai_mcp_tools: this._prepareCapturedValue(spanData.result)
871
940
  };
872
- this._captureEvent("$ai_span", properties, distinctId);
941
+ this._captureEvent("$ai_span", properties, identity);
873
942
  }
874
- _handleGenericSpan(spanData, traceId, spanId, parentId, latency, distinctId, groupId, errorProperties) {
943
+ _handleGenericSpan(spanData, traceId, spanId, parentId, latency, identity, groupId, errorProperties) {
875
944
  const spanType = spanData.type || "unknown";
876
945
  const properties = {
877
946
  ...this._baseProperties(traceId, spanId, parentId, latency, groupId, errorProperties),
878
947
  $ai_span_name: spanType,
879
948
  $ai_span_type: spanType
880
949
  };
881
- this._captureEvent("$ai_span", properties, distinctId);
950
+ this._captureEvent("$ai_span", properties, identity);
882
951
  }
883
952
  };
884
953
  //#endregion