@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
@@ -14,9 +14,77 @@ const isObject = (value) => {
14
14
  function isFullAiCaptureEnabled(client) {
15
15
  return client?.enableFullAiCapture === true;
16
16
  }
17
+ /**
18
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
19
+ *
20
+ * @internal
21
+ */
22
+ function userDistinctId(value) {
23
+ if (!value) return;
24
+ const id = String(value);
25
+ return id.trim() === "" ? void 0 : id;
26
+ }
27
+ /**
28
+ * The caller's per-event options when they are an object. A missing or `null` value means no
29
+ * options. An array or any other value is reported and ignored, so it is never spread.
30
+ *
31
+ * @internal
32
+ */
33
+ function readAiEventOptions(value) {
34
+ if (value === void 0 || value === null) return;
35
+ if (typeof value === "object" && !Array.isArray(value)) return value;
36
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
37
+ }
38
+ /**
39
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
40
+ * processing off and beats the caller's `process_person_profile`.
41
+ *
42
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
43
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
44
+ * reads, and an unknown key is dropped.
45
+ *
46
+ * @internal
47
+ */
48
+ function eventOptionFields(client, properties, options, personless) {
49
+ const userOptions = readAiEventOptions(options);
50
+ if (isPostHogNode6(client)) {
51
+ const eventOptions = {
52
+ ...userOptions,
53
+ ...personless ? { process_person_profile: false } : {}
54
+ };
55
+ return Object.keys(eventOptions).length > 0 ? {
56
+ properties,
57
+ options: eventOptions
58
+ } : { properties };
59
+ }
60
+ const withOptionProperties = {
61
+ ...properties,
62
+ ...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
63
+ };
64
+ if (personless) withOptionProperties.$process_person_profile = false;
65
+ return { properties: withOptionProperties };
66
+ }
67
+ /**
68
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
69
+ *
70
+ * @internal
71
+ */
72
+ function isPostHogNode6(client) {
73
+ return (0, _posthog_core.acceptsEventOptions)(client);
74
+ }
75
+ /**
76
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
77
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
78
+ * upgrade never changes how a 5.x client sends.
79
+ *
80
+ * @internal
81
+ */
82
+ function usesAiLane(client) {
83
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
84
+ }
17
85
  /** @internal */
18
86
  function captureAiEvent(client, event) {
19
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
87
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
20
88
  client.captureAi(event);
21
89
  return;
22
90
  }
@@ -24,7 +92,7 @@ function captureAiEvent(client, event) {
24
92
  }
25
93
  /** @internal */
26
94
  async function captureAiEventImmediate(client, event) {
27
- if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === "function") {
95
+ if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
28
96
  await client.captureAiImmediate(event);
29
97
  return;
30
98
  }
@@ -172,13 +240,17 @@ var MediaTypeContext = class MediaTypeContext {
172
240
  //#region src/sanitization/binary_content_redactor.ts
173
241
  const STRONG_CONTEXT_MIN_LENGTH = 64;
174
242
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
243
+ const MAX_NODES = 1e4;
244
+ const TRUNCATED = "[Truncated]";
175
245
  var BinaryContentRedactor = class {
176
246
  constructor(recognizer = new Base64Recognizer()) {
177
247
  this.recognizer = recognizer;
178
- this.visited = /* @__PURE__ */ new WeakSet();
248
+ this.ancestors = /* @__PURE__ */ new WeakSet();
249
+ this.remainingNodes = MAX_NODES;
179
250
  }
180
251
  redact(value, mediaType) {
181
- this.visited = /* @__PURE__ */ new WeakSet();
252
+ this.ancestors = /* @__PURE__ */ new WeakSet();
253
+ this.remainingNodes = MAX_NODES;
182
254
  return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
183
255
  }
184
256
  /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
@@ -190,18 +262,24 @@ var BinaryContentRedactor = class {
190
262
  if (typeof value === "string") return this.redactLeaf(value, ctx);
191
263
  if (typeof value !== "object") return value;
192
264
  if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
193
- if (this.visited.has(value)) return null;
194
- this.visited.add(value);
195
- if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
196
- const obj = value;
197
- const out = {};
198
- for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
199
- value: this.walk(obj[k], new MediaTypeContext(obj, k)),
200
- enumerable: true,
201
- writable: true,
202
- configurable: true
203
- });
204
- return out;
265
+ if (this.ancestors.has(value)) return null;
266
+ if (this.remainingNodes <= 0) return TRUNCATED;
267
+ this.remainingNodes--;
268
+ this.ancestors.add(value);
269
+ try {
270
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
271
+ const obj = value;
272
+ const out = {};
273
+ for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
274
+ value: this.walk(obj[k], new MediaTypeContext(obj, k)),
275
+ enumerable: true,
276
+ writable: true,
277
+ configurable: true
278
+ });
279
+ return out;
280
+ } finally {
281
+ this.ancestors.delete(value);
282
+ }
205
283
  }
206
284
  redactString(value, ctx) {
207
285
  const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
@@ -414,7 +492,7 @@ function sanitizeValues(obj) {
414
492
  }
415
493
  //#endregion
416
494
  //#region package.json
417
- var version = "8.20.1";
495
+ var version = "8.21.0";
418
496
  //#endregion
419
497
  //#region src/serializeError.ts
420
498
  const DEFAULT_MAX_DEPTH = 3;
@@ -609,6 +687,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
609
687
  //#endregion
610
688
  //#region src/captureAiGeneration.ts
611
689
  /**
690
+ * posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
691
+ * arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
692
+ */
693
+ const captureAiException = (client, error, properties, uuid$1) => {
694
+ if (isPostHogNode6(client)) {
695
+ client.captureException(error, {
696
+ properties,
697
+ uuid: uuid$1
698
+ });
699
+ return;
700
+ }
701
+ client.captureException.call(client, error, void 0, properties, uuid$1);
702
+ };
703
+ /**
612
704
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
613
705
  *
614
706
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -644,7 +736,7 @@ const captureAiGeneration = async (client, options) => {
644
736
  if (client.options?.enableExceptionAutocapture) {
645
737
  if (!shouldRedact) {
646
738
  exceptionId = (0, _posthog_core.uuidv7)();
647
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
739
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
648
740
  }
649
741
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
650
742
  }
@@ -670,6 +762,7 @@ const captureAiGeneration = async (client, options) => {
670
762
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
671
763
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
672
764
  };
765
+ const distinctId = userDistinctId(options.distinctId);
673
766
  const properties = {
674
767
  $ai_lib: "posthog-ai",
675
768
  $ai_lib_version: version,
@@ -689,7 +782,6 @@ const captureAiGeneration = async (client, options) => {
689
782
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
690
783
  ...options.properties,
691
784
  $ai_tokens_source: getTokensSource(options.properties),
692
- ...options.distinctId ? {} : { $process_person_profile: false },
693
785
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
694
786
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
695
787
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -698,9 +790,9 @@ const captureAiGeneration = async (client, options) => {
698
790
  ...costOverrideData
699
791
  };
700
792
  const event = {
701
- distinctId: options.distinctId ?? traceId,
793
+ distinctId: distinctId ?? traceId,
702
794
  event: eventType,
703
- properties,
795
+ ...eventOptionFields(client, properties, options.options, !distinctId),
704
796
  groups: options.groups
705
797
  };
706
798
  if (options.captureImmediate) await captureAiEventImmediate(client, event);
@@ -981,6 +1073,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
981
1073
  traceId,
982
1074
  properties: mergedOptions.posthogProperties,
983
1075
  groups: mergedOptions.posthogGroups,
1076
+ options: mergedOptions.posthogOptions,
984
1077
  privacyMode,
985
1078
  modelOverride: mergedOptions.posthogModelOverride,
986
1079
  providerOverride: mergedOptions.posthogProviderOverride,