@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,8 +1,8 @@
1
1
  import { BasePlugin } from "@google/adk";
2
2
  import { v4 } from "uuid";
3
- import { toJsonSafeValue, uuidv7 } from "@posthog/core";
3
+ import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
4
4
  //#region package.json
5
- var version = "8.20.2";
5
+ var version = "8.21.1";
6
6
  //#endregion
7
7
  //#region src/typeGuards.ts
8
8
  const isObject = (value) => {
@@ -14,9 +14,77 @@ const isObject = (value) => {
14
14
  function isFullAiCaptureEnabled(client) {
15
15
  return client?.enableFullAiCapture === true;
16
16
  }
17
+ /**
18
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
19
+ *
20
+ * @internal
21
+ */
22
+ function userDistinctId(value) {
23
+ if (!value) return;
24
+ const id = String(value);
25
+ return id.trim() === "" ? void 0 : id;
26
+ }
27
+ /**
28
+ * The caller's per-event options when they are an object. A missing or `null` value means no
29
+ * options. An array or any other value is reported and ignored, so it is never spread.
30
+ *
31
+ * @internal
32
+ */
33
+ function readAiEventOptions(value) {
34
+ if (value === void 0 || value === null) return;
35
+ if (typeof value === "object" && !Array.isArray(value)) return value;
36
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
37
+ }
38
+ /**
39
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
40
+ * processing off and beats the caller's `process_person_profile`.
41
+ *
42
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
43
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
44
+ * reads, and an unknown key is dropped.
45
+ *
46
+ * @internal
47
+ */
48
+ function eventOptionFields(client, properties, options, personless) {
49
+ const userOptions = readAiEventOptions(options);
50
+ if (isPostHogNode6(client)) {
51
+ const eventOptions = {
52
+ ...userOptions,
53
+ ...personless ? { process_person_profile: false } : {}
54
+ };
55
+ return Object.keys(eventOptions).length > 0 ? {
56
+ properties,
57
+ options: eventOptions
58
+ } : { properties };
59
+ }
60
+ const withOptionProperties = {
61
+ ...properties,
62
+ ...eventOptionsAsLegacyProperties(userOptions)
63
+ };
64
+ if (personless) withOptionProperties.$process_person_profile = false;
65
+ return { properties: withOptionProperties };
66
+ }
67
+ /**
68
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
69
+ *
70
+ * @internal
71
+ */
72
+ function isPostHogNode6(client) {
73
+ return acceptsEventOptions(client);
74
+ }
75
+ /**
76
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
77
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
78
+ * upgrade never changes how a 5.x client sends.
79
+ *
80
+ * @internal
81
+ */
82
+ function usesAiLane(client) {
83
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
84
+ }
17
85
  /** @internal */
18
86
  function captureAiEvent(client, event) {
19
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
87
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
20
88
  client.captureAi(event);
21
89
  return;
22
90
  }
@@ -24,7 +92,7 @@ function captureAiEvent(client, event) {
24
92
  }
25
93
  /** @internal */
26
94
  async function captureAiEventImmediate(client, event) {
27
- if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === "function") {
95
+ if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
28
96
  await client.captureAiImmediate(event);
29
97
  return;
30
98
  }
@@ -570,6 +638,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
570
638
  //#endregion
571
639
  //#region src/captureAiGeneration.ts
572
640
  /**
641
+ * posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
642
+ * arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
643
+ */
644
+ const captureAiException = (client, error, properties, uuid) => {
645
+ if (isPostHogNode6(client)) {
646
+ client.captureException(error, {
647
+ properties,
648
+ uuid
649
+ });
650
+ return;
651
+ }
652
+ client.captureException.call(client, error, void 0, properties, uuid);
653
+ };
654
+ /**
573
655
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
574
656
  *
575
657
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -605,7 +687,7 @@ const captureAiGeneration = async (client, options) => {
605
687
  if (client.options?.enableExceptionAutocapture) {
606
688
  if (!shouldRedact) {
607
689
  exceptionId = uuidv7();
608
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
690
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
609
691
  }
610
692
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
611
693
  }
@@ -631,6 +713,7 @@ const captureAiGeneration = async (client, options) => {
631
713
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
632
714
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
633
715
  };
716
+ const distinctId = userDistinctId(options.distinctId);
634
717
  const properties = {
635
718
  $ai_lib: "posthog-ai",
636
719
  $ai_lib_version: version,
@@ -650,7 +733,6 @@ const captureAiGeneration = async (client, options) => {
650
733
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
651
734
  ...options.properties,
652
735
  $ai_tokens_source: getTokensSource(options.properties),
653
- ...options.distinctId ? {} : { $process_person_profile: false },
654
736
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
655
737
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
656
738
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -659,9 +741,9 @@ const captureAiGeneration = async (client, options) => {
659
741
  ...costOverrideData
660
742
  };
661
743
  const event = {
662
- distinctId: options.distinctId ?? traceId,
744
+ distinctId: distinctId ?? traceId,
663
745
  event: eventType,
664
- properties,
746
+ ...eventOptionFields(client, properties, options.options, !distinctId),
665
747
  groups: options.groups
666
748
  };
667
749
  if (options.captureImmediate) await captureAiEventImmediate(client, event);
@@ -733,6 +815,7 @@ var PostHogADKPlugin = class extends BasePlugin {
733
815
  this._privacyMode = options.privacyMode ?? false;
734
816
  this._groups = options.groups;
735
817
  this._properties = options.properties ?? {};
818
+ this._eventOptions = options.options;
736
819
  this._captureImmediate = options.captureImmediate ?? false;
737
820
  this._onError = options.onError;
738
821
  }
@@ -922,6 +1005,7 @@ var PostHogADKPlugin = class extends BasePlugin {
922
1005
  stopReason,
923
1006
  tools: pending?.tools,
924
1007
  groups: this._groups,
1008
+ options: this._eventOptions,
925
1009
  privacyMode: this._privacyMode,
926
1010
  captureImmediate: this._captureImmediate,
927
1011
  onError: this._onError,
@@ -957,32 +1041,27 @@ var PostHogADKPlugin = class extends BasePlugin {
957
1041
  });
958
1042
  }
959
1043
  async _captureLifecycleEvent(event, distinctId, properties) {
1044
+ const eventProperties = {
1045
+ $ai_lib: "posthog-ai",
1046
+ $ai_lib_version: version,
1047
+ $ai_framework: "google-adk",
1048
+ ...properties,
1049
+ ...this._properties
1050
+ };
960
1051
  const message = {
961
1052
  distinctId: distinctId ?? String(properties.$ai_trace_id),
962
1053
  event,
963
- properties: {
964
- $ai_lib: "posthog-ai",
965
- $ai_lib_version: version,
966
- $ai_framework: "google-adk",
967
- ...properties,
968
- ...this._properties,
969
- ...distinctId ? {} : { $process_person_profile: false }
970
- },
1054
+ ...eventOptionFields(this._client, eventProperties, this._eventOptions, !distinctId),
971
1055
  groups: this._groups
972
1056
  };
973
1057
  if (this._captureImmediate) await captureAiEventImmediate(this._client, message);
974
1058
  else captureAiEvent(this._client, message);
975
1059
  }
976
1060
  _resolveDistinctId(context) {
977
- if (typeof this._distinctId === "function") {
978
- const resolved = this._distinctId(context);
979
- if (resolved) return String(resolved);
980
- } else if (this._distinctId) return String(this._distinctId);
981
- return context.userId ? String(context.userId) : void 0;
1061
+ return userDistinctId(typeof this._distinctId === "function" ? this._distinctId(context) : this._distinctId) ?? userDistinctId(context.userId);
982
1062
  }
983
1063
  _resolveInvocationDistinctId(context) {
984
- if (typeof this._distinctId === "string" && this._distinctId) return String(this._distinctId);
985
- return context.userId ? String(context.userId) : void 0;
1064
+ return userDistinctId(typeof this._distinctId === "string" ? this._distinctId : void 0) ?? userDistinctId(context.userId);
986
1065
  }
987
1066
  _rememberContext(context) {
988
1067
  const trace = this._traces.get(context.invocationId);