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