@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
@@ -5,6 +5,7 @@ let _langchain_core_utils_types = require("@langchain/core/utils/types");
5
5
  let langchain = require("langchain");
6
6
  let uuid = require("uuid");
7
7
  let zod = require("zod");
8
+ let _posthog_core = require("@posthog/core");
8
9
  let _langchain_core_callbacks_base = require("@langchain/core/callbacks/base");
9
10
  //#region src/typeGuards.ts
10
11
  const isObject = (value) => {
@@ -16,9 +17,77 @@ const isObject = (value) => {
16
17
  function isFullAiCaptureEnabled(client) {
17
18
  return client?.enableFullAiCapture === true;
18
19
  }
20
+ /**
21
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
22
+ *
23
+ * @internal
24
+ */
25
+ function userDistinctId(value) {
26
+ if (!value) return;
27
+ const id = String(value);
28
+ return id.trim() === "" ? void 0 : id;
29
+ }
30
+ /**
31
+ * The caller's per-event options when they are an object. A missing or `null` value means no
32
+ * options. An array or any other value is reported and ignored, so it is never spread.
33
+ *
34
+ * @internal
35
+ */
36
+ function readAiEventOptions(value) {
37
+ if (value === void 0 || value === null) return;
38
+ if (typeof value === "object" && !Array.isArray(value)) return value;
39
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
40
+ }
41
+ /**
42
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
43
+ * processing off and beats the caller's `process_person_profile`.
44
+ *
45
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
46
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
47
+ * reads, and an unknown key is dropped.
48
+ *
49
+ * @internal
50
+ */
51
+ function eventOptionFields(client, properties, options, personless) {
52
+ const userOptions = readAiEventOptions(options);
53
+ if (isPostHogNode6(client)) {
54
+ const eventOptions = {
55
+ ...userOptions,
56
+ ...personless ? { process_person_profile: false } : {}
57
+ };
58
+ return Object.keys(eventOptions).length > 0 ? {
59
+ properties,
60
+ options: eventOptions
61
+ } : { properties };
62
+ }
63
+ const withOptionProperties = {
64
+ ...properties,
65
+ ...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
66
+ };
67
+ if (personless) withOptionProperties.$process_person_profile = false;
68
+ return { properties: withOptionProperties };
69
+ }
70
+ /**
71
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
72
+ *
73
+ * @internal
74
+ */
75
+ function isPostHogNode6(client) {
76
+ return (0, _posthog_core.acceptsEventOptions)(client);
77
+ }
78
+ /**
79
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
80
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
81
+ * upgrade never changes how a 5.x client sends.
82
+ *
83
+ * @internal
84
+ */
85
+ function usesAiLane(client) {
86
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
87
+ }
19
88
  /** @internal */
20
89
  function captureAiEvent(client, event) {
21
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
90
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
22
91
  client.captureAi(event);
23
92
  return;
24
93
  }
@@ -323,7 +392,7 @@ function redactMcpToolCredentials(tools) {
323
392
  }
324
393
  //#endregion
325
394
  //#region package.json
326
- var version = "8.20.2";
395
+ var version = "8.21.1";
327
396
  //#endregion
328
397
  //#region src/serializeError.ts
329
398
  const DEFAULT_MAX_DEPTH = 3;
@@ -526,9 +595,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
526
595
  this.runs = {};
527
596
  this.parentTree = {};
528
597
  this.client = options.client;
529
- this.distinctId = options.distinctId;
598
+ this.distinctId = userDistinctId(options.distinctId);
530
599
  this.traceId = options.traceId;
531
600
  this.properties = options.properties || {};
601
+ this.eventOptions = options.options;
532
602
  this.privacyMode = options.privacyMode || false;
533
603
  this.groups = options.groups || {};
534
604
  this.debug = options.debug || false;
@@ -741,7 +811,6 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
741
811
  };
742
812
  if (parentRunId) eventProperties["$ai_parent_id"] = parentRunId;
743
813
  Object.assign(eventProperties, this.properties);
744
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
745
814
  if (outputs instanceof Error) {
746
815
  if (isLangGraphControlFlow(outputs)) {
747
816
  const interrupts = outputs.interrupts;
@@ -754,7 +823,7 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
754
823
  this._safeCapture({
755
824
  distinctId: this.distinctId ? this.distinctId.toString() : runId,
756
825
  event: eventName,
757
- properties: eventProperties,
826
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
758
827
  groups: this.groups
759
828
  });
760
829
  }
@@ -830,11 +899,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
830
899
  if (completions) eventProperties["$ai_output_choices"] = withPrivacyMode(this.client, this.privacyMode, completions);
831
900
  }
832
901
  Object.assign(eventProperties, this.properties);
833
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
834
902
  this._safeCapture({
835
903
  distinctId: this.distinctId ? this.distinctId.toString() : traceId,
836
904
  event: "$ai_generation",
837
- properties: eventProperties,
905
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
838
906
  groups: this.groups
839
907
  });
840
908
  }