@posthog/ai 8.20.1 → 8.21.0

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 (66) hide show
  1. package/README.md +20 -0
  2. package/dist/adk/index.cjs +125 -36
  3. package/dist/adk/index.cjs.map +1 -1
  4. package/dist/adk/index.d.ts +7 -0
  5. package/dist/adk/index.mjs +126 -37
  6. package/dist/adk/index.mjs.map +1 -1
  7. package/dist/anthropic/index.cjs +115 -22
  8. package/dist/anthropic/index.cjs.map +1 -1
  9. package/dist/anthropic/index.d.ts +7 -0
  10. package/dist/anthropic/index.mjs +116 -23
  11. package/dist/anthropic/index.mjs.map +1 -1
  12. package/dist/claude-agent-sdk/index.cjs +131 -35
  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 +132 -36
  16. package/dist/claude-agent-sdk/index.mjs.map +1 -1
  17. package/dist/gemini/index.cjs +116 -23
  18. package/dist/gemini/index.cjs.map +1 -1
  19. package/dist/gemini/index.d.ts +7 -0
  20. package/dist/gemini/index.mjs +116 -23
  21. package/dist/gemini/index.mjs.map +1 -1
  22. package/dist/genkit/index.cjs +138 -43
  23. package/dist/genkit/index.cjs.map +1 -1
  24. package/dist/genkit/index.d.ts +7 -0
  25. package/dist/genkit/index.mjs +138 -43
  26. package/dist/genkit/index.mjs.map +1 -1
  27. package/dist/index.cjs +114 -21
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.ts +11 -0
  30. package/dist/index.mjs +115 -22
  31. package/dist/index.mjs.map +1 -1
  32. package/dist/langchain/index.cjs +99 -21
  33. package/dist/langchain/index.cjs.map +1 -1
  34. package/dist/langchain/index.d.ts +7 -0
  35. package/dist/langchain/index.mjs +99 -21
  36. package/dist/langchain/index.mjs.map +1 -1
  37. package/dist/langchain/middleware/index.cjs +99 -21
  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 +99 -21
  41. package/dist/langchain/middleware/index.mjs.map +1 -1
  42. package/dist/openai/index.cjs +115 -22
  43. package/dist/openai/index.cjs.map +1 -1
  44. package/dist/openai/index.d.ts +7 -0
  45. package/dist/openai/index.mjs +116 -23
  46. package/dist/openai/index.mjs.map +1 -1
  47. package/dist/openai-agents/index.cjs +139 -60
  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 +139 -60
  51. package/dist/openai-agents/index.mjs.map +1 -1
  52. package/dist/otel/index.browser.cjs +24 -14
  53. package/dist/otel/index.browser.cjs.map +1 -1
  54. package/dist/otel/index.browser.mjs +24 -14
  55. package/dist/otel/index.browser.mjs.map +1 -1
  56. package/dist/otel/index.cjs +24 -14
  57. package/dist/otel/index.cjs.map +1 -1
  58. package/dist/otel/index.d.ts +3 -3
  59. package/dist/otel/index.mjs +24 -14
  60. package/dist/otel/index.mjs.map +1 -1
  61. package/dist/vercel/index.cjs +114 -21
  62. package/dist/vercel/index.cjs.map +1 -1
  63. package/dist/vercel/index.d.ts +6 -0
  64. package/dist/vercel/index.mjs +115 -22
  65. package/dist/vercel/index.mjs.map +1 -1
  66. 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
  }
@@ -160,13 +229,17 @@ var MediaTypeContext = class MediaTypeContext {
160
229
  //#region src/sanitization/binary_content_redactor.ts
161
230
  const STRONG_CONTEXT_MIN_LENGTH = 64;
162
231
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
232
+ const MAX_NODES = 1e4;
233
+ const TRUNCATED = "[Truncated]";
163
234
  var BinaryContentRedactor = class {
164
235
  constructor(recognizer = new Base64Recognizer()) {
165
236
  this.recognizer = recognizer;
166
- this.visited = /* @__PURE__ */ new WeakSet();
237
+ this.ancestors = /* @__PURE__ */ new WeakSet();
238
+ this.remainingNodes = MAX_NODES;
167
239
  }
168
240
  redact(value, mediaType) {
169
- this.visited = /* @__PURE__ */ new WeakSet();
241
+ this.ancestors = /* @__PURE__ */ new WeakSet();
242
+ this.remainingNodes = MAX_NODES;
170
243
  return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
171
244
  }
172
245
  /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
@@ -178,18 +251,24 @@ var BinaryContentRedactor = class {
178
251
  if (typeof value === "string") return this.redactLeaf(value, ctx);
179
252
  if (typeof value !== "object") return value;
180
253
  if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
181
- if (this.visited.has(value)) return null;
182
- this.visited.add(value);
183
- if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
184
- const obj = value;
185
- const out = {};
186
- for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
187
- value: this.walk(obj[k], new MediaTypeContext(obj, k)),
188
- enumerable: true,
189
- writable: true,
190
- configurable: true
191
- });
192
- return out;
254
+ if (this.ancestors.has(value)) return null;
255
+ if (this.remainingNodes <= 0) return TRUNCATED;
256
+ this.remainingNodes--;
257
+ this.ancestors.add(value);
258
+ try {
259
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
260
+ const obj = value;
261
+ const out = {};
262
+ for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
263
+ value: this.walk(obj[k], new MediaTypeContext(obj, k)),
264
+ enumerable: true,
265
+ writable: true,
266
+ configurable: true
267
+ });
268
+ return out;
269
+ } finally {
270
+ this.ancestors.delete(value);
271
+ }
193
272
  }
194
273
  redactString(value, ctx) {
195
274
  const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
@@ -307,7 +386,7 @@ function redactMcpToolCredentials(tools) {
307
386
  }
308
387
  //#endregion
309
388
  //#region package.json
310
- var version = "8.20.1";
389
+ var version = "8.21.0";
311
390
  //#endregion
312
391
  //#region src/serializeError.ts
313
392
  const DEFAULT_MAX_DEPTH = 3;
@@ -510,9 +589,10 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
510
589
  this.runs = {};
511
590
  this.parentTree = {};
512
591
  this.client = options.client;
513
- this.distinctId = options.distinctId;
592
+ this.distinctId = userDistinctId(options.distinctId);
514
593
  this.traceId = options.traceId;
515
594
  this.properties = options.properties || {};
595
+ this.eventOptions = options.options;
516
596
  this.privacyMode = options.privacyMode || false;
517
597
  this.groups = options.groups || {};
518
598
  this.debug = options.debug || false;
@@ -725,7 +805,6 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
725
805
  };
726
806
  if (parentRunId) eventProperties["$ai_parent_id"] = parentRunId;
727
807
  Object.assign(eventProperties, this.properties);
728
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
729
808
  if (outputs instanceof Error) {
730
809
  if (isLangGraphControlFlow(outputs)) {
731
810
  const interrupts = outputs.interrupts;
@@ -738,7 +817,7 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
738
817
  this._safeCapture({
739
818
  distinctId: this.distinctId ? this.distinctId.toString() : runId,
740
819
  event: eventName,
741
- properties: eventProperties,
820
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
742
821
  groups: this.groups
743
822
  });
744
823
  }
@@ -814,11 +893,10 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
814
893
  if (completions) eventProperties["$ai_output_choices"] = withPrivacyMode(this.client, this.privacyMode, completions);
815
894
  }
816
895
  Object.assign(eventProperties, this.properties);
817
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
818
896
  this._safeCapture({
819
897
  distinctId: this.distinctId ? this.distinctId.toString() : traceId,
820
898
  event: "$ai_generation",
821
- properties: eventProperties,
899
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
822
900
  groups: this.groups
823
901
  });
824
902
  }