@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,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
  }
@@ -161,13 +230,17 @@ var MediaTypeContext = class MediaTypeContext {
161
230
  //#region src/sanitization/binary_content_redactor.ts
162
231
  const STRONG_CONTEXT_MIN_LENGTH = 64;
163
232
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
233
+ const MAX_NODES = 1e4;
234
+ const TRUNCATED = "[Truncated]";
164
235
  var BinaryContentRedactor = class {
165
236
  constructor(recognizer = new Base64Recognizer()) {
166
237
  this.recognizer = recognizer;
167
- this.visited = /* @__PURE__ */ new WeakSet();
238
+ this.ancestors = /* @__PURE__ */ new WeakSet();
239
+ this.remainingNodes = MAX_NODES;
168
240
  }
169
241
  redact(value, mediaType) {
170
- this.visited = /* @__PURE__ */ new WeakSet();
242
+ this.ancestors = /* @__PURE__ */ new WeakSet();
243
+ this.remainingNodes = MAX_NODES;
171
244
  return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
172
245
  }
173
246
  /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
@@ -179,18 +252,24 @@ var BinaryContentRedactor = class {
179
252
  if (typeof value === "string") return this.redactLeaf(value, ctx);
180
253
  if (typeof value !== "object") return value;
181
254
  if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
182
- if (this.visited.has(value)) return null;
183
- this.visited.add(value);
184
- if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
185
- const obj = value;
186
- const out = {};
187
- for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
188
- value: this.walk(obj[k], new MediaTypeContext(obj, k)),
189
- enumerable: true,
190
- writable: true,
191
- configurable: true
192
- });
193
- return out;
255
+ if (this.ancestors.has(value)) return null;
256
+ if (this.remainingNodes <= 0) return TRUNCATED;
257
+ this.remainingNodes--;
258
+ this.ancestors.add(value);
259
+ try {
260
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
261
+ const obj = value;
262
+ const out = {};
263
+ for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
264
+ value: this.walk(obj[k], new MediaTypeContext(obj, k)),
265
+ enumerable: true,
266
+ writable: true,
267
+ configurable: true
268
+ });
269
+ return out;
270
+ } finally {
271
+ this.ancestors.delete(value);
272
+ }
194
273
  }
195
274
  redactString(value, ctx) {
196
275
  const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
@@ -308,7 +387,7 @@ function redactMcpToolCredentials(tools) {
308
387
  }
309
388
  //#endregion
310
389
  //#region package.json
311
- var version = "8.20.1";
390
+ var version = "8.21.0";
312
391
  //#endregion
313
392
  //#region src/serializeError.ts
314
393
  const DEFAULT_MAX_DEPTH = 3;
@@ -511,9 +590,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
511
590
  this.runs = {};
512
591
  this.parentTree = {};
513
592
  this.client = options.client;
514
- this.distinctId = options.distinctId;
593
+ this.distinctId = userDistinctId(options.distinctId);
515
594
  this.traceId = options.traceId;
516
595
  this.properties = options.properties || {};
596
+ this.eventOptions = options.options;
517
597
  this.privacyMode = options.privacyMode || false;
518
598
  this.groups = options.groups || {};
519
599
  this.debug = options.debug || false;
@@ -726,7 +806,6 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
726
806
  };
727
807
  if (parentRunId) eventProperties["$ai_parent_id"] = parentRunId;
728
808
  Object.assign(eventProperties, this.properties);
729
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
730
809
  if (outputs instanceof Error) {
731
810
  if (isLangGraphControlFlow(outputs)) {
732
811
  const interrupts = outputs.interrupts;
@@ -739,7 +818,7 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
739
818
  this._safeCapture({
740
819
  distinctId: this.distinctId ? this.distinctId.toString() : runId,
741
820
  event: eventName,
742
- properties: eventProperties,
821
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
743
822
  groups: this.groups
744
823
  });
745
824
  }
@@ -815,11 +894,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
815
894
  if (completions) eventProperties["$ai_output_choices"] = withPrivacyMode(this.client, this.privacyMode, completions);
816
895
  }
817
896
  Object.assign(eventProperties, this.properties);
818
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
819
897
  this._safeCapture({
820
898
  distinctId: this.distinctId ? this.distinctId.toString() : traceId,
821
899
  event: "$ai_generation",
822
- properties: eventProperties,
900
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
823
901
  groups: this.groups
824
902
  });
825
903
  }