@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,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  require("uuid");
3
+ let _posthog_core = require("@posthog/core");
3
4
  let _langchain_core_callbacks_base = require("@langchain/core/callbacks/base");
4
5
  //#region src/typeGuards.ts
5
6
  const isObject = (value) => {
@@ -11,9 +12,77 @@ const isObject = (value) => {
11
12
  function isFullAiCaptureEnabled(client) {
12
13
  return client?.enableFullAiCapture === true;
13
14
  }
15
+ /**
16
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
17
+ *
18
+ * @internal
19
+ */
20
+ function userDistinctId(value) {
21
+ if (!value) return;
22
+ const id = String(value);
23
+ return id.trim() === "" ? void 0 : id;
24
+ }
25
+ /**
26
+ * The caller's per-event options when they are an object. A missing or `null` value means no
27
+ * options. An array or any other value is reported and ignored, so it is never spread.
28
+ *
29
+ * @internal
30
+ */
31
+ function readAiEventOptions(value) {
32
+ if (value === void 0 || value === null) return;
33
+ if (typeof value === "object" && !Array.isArray(value)) return value;
34
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
35
+ }
36
+ /**
37
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
38
+ * processing off and beats the caller's `process_person_profile`.
39
+ *
40
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
41
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
42
+ * reads, and an unknown key is dropped.
43
+ *
44
+ * @internal
45
+ */
46
+ function eventOptionFields(client, properties, options, personless) {
47
+ const userOptions = readAiEventOptions(options);
48
+ if (isPostHogNode6(client)) {
49
+ const eventOptions = {
50
+ ...userOptions,
51
+ ...personless ? { process_person_profile: false } : {}
52
+ };
53
+ return Object.keys(eventOptions).length > 0 ? {
54
+ properties,
55
+ options: eventOptions
56
+ } : { properties };
57
+ }
58
+ const withOptionProperties = {
59
+ ...properties,
60
+ ...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
61
+ };
62
+ if (personless) withOptionProperties.$process_person_profile = false;
63
+ return { properties: withOptionProperties };
64
+ }
65
+ /**
66
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
67
+ *
68
+ * @internal
69
+ */
70
+ function isPostHogNode6(client) {
71
+ return (0, _posthog_core.acceptsEventOptions)(client);
72
+ }
73
+ /**
74
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
75
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
76
+ * upgrade never changes how a 5.x client sends.
77
+ *
78
+ * @internal
79
+ */
80
+ function usesAiLane(client) {
81
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
82
+ }
14
83
  /** @internal */
15
84
  function captureAiEvent(client, event) {
16
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
85
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
17
86
  client.captureAi(event);
18
87
  return;
19
88
  }
@@ -318,7 +387,7 @@ function redactMcpToolCredentials(tools) {
318
387
  }
319
388
  //#endregion
320
389
  //#region package.json
321
- var version = "8.20.2";
390
+ var version = "8.21.1";
322
391
  //#endregion
323
392
  //#region src/serializeError.ts
324
393
  const DEFAULT_MAX_DEPTH = 3;
@@ -521,9 +590,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
521
590
  this.runs = {};
522
591
  this.parentTree = {};
523
592
  this.client = options.client;
524
- this.distinctId = options.distinctId;
593
+ this.distinctId = userDistinctId(options.distinctId);
525
594
  this.traceId = options.traceId;
526
595
  this.properties = options.properties || {};
596
+ this.eventOptions = options.options;
527
597
  this.privacyMode = options.privacyMode || false;
528
598
  this.groups = options.groups || {};
529
599
  this.debug = options.debug || false;
@@ -736,7 +806,6 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
736
806
  };
737
807
  if (parentRunId) eventProperties["$ai_parent_id"] = parentRunId;
738
808
  Object.assign(eventProperties, this.properties);
739
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
740
809
  if (outputs instanceof Error) {
741
810
  if (isLangGraphControlFlow(outputs)) {
742
811
  const interrupts = outputs.interrupts;
@@ -749,7 +818,7 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
749
818
  this._safeCapture({
750
819
  distinctId: this.distinctId ? this.distinctId.toString() : runId,
751
820
  event: eventName,
752
- properties: eventProperties,
821
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
753
822
  groups: this.groups
754
823
  });
755
824
  }
@@ -825,11 +894,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
825
894
  if (completions) eventProperties["$ai_output_choices"] = withPrivacyMode(this.client, this.privacyMode, completions);
826
895
  }
827
896
  Object.assign(eventProperties, this.properties);
828
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
829
897
  this._safeCapture({
830
898
  distinctId: this.distinctId ? this.distinctId.toString() : traceId,
831
899
  event: "$ai_generation",
832
- properties: eventProperties,
900
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
833
901
  groups: this.groups
834
902
  });
835
903
  }