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