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