@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
@@ -39,9 +39,77 @@ const isObject = (value) => {
39
39
  function isFullAiCaptureEnabled(client) {
40
40
  return client?.enableFullAiCapture === true;
41
41
  }
42
+ /**
43
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
44
+ *
45
+ * @internal
46
+ */
47
+ function userDistinctId(value) {
48
+ if (!value) return;
49
+ const id = String(value);
50
+ return id.trim() === "" ? void 0 : id;
51
+ }
52
+ /**
53
+ * The caller's per-event options when they are an object. A missing or `null` value means no
54
+ * options. An array or any other value is reported and ignored, so it is never spread.
55
+ *
56
+ * @internal
57
+ */
58
+ function readAiEventOptions(value) {
59
+ if (value === void 0 || value === null) return;
60
+ if (typeof value === "object" && !Array.isArray(value)) return value;
61
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
62
+ }
63
+ /**
64
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
65
+ * processing off and beats the caller's `process_person_profile`.
66
+ *
67
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
68
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
69
+ * reads, and an unknown key is dropped.
70
+ *
71
+ * @internal
72
+ */
73
+ function eventOptionFields(client, properties, options, personless) {
74
+ const userOptions = readAiEventOptions(options);
75
+ if (isPostHogNode6(client)) {
76
+ const eventOptions = {
77
+ ...userOptions,
78
+ ...personless ? { process_person_profile: false } : {}
79
+ };
80
+ return Object.keys(eventOptions).length > 0 ? {
81
+ properties,
82
+ options: eventOptions
83
+ } : { properties };
84
+ }
85
+ const withOptionProperties = {
86
+ ...properties,
87
+ ...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
88
+ };
89
+ if (personless) withOptionProperties.$process_person_profile = false;
90
+ return { properties: withOptionProperties };
91
+ }
92
+ /**
93
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
94
+ *
95
+ * @internal
96
+ */
97
+ function isPostHogNode6(client) {
98
+ return (0, _posthog_core.acceptsEventOptions)(client);
99
+ }
100
+ /**
101
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
102
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
103
+ * upgrade never changes how a 5.x client sends.
104
+ *
105
+ * @internal
106
+ */
107
+ function usesAiLane(client) {
108
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
109
+ }
42
110
  /** @internal */
43
111
  function captureAiEvent(client, event) {
44
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
112
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
45
113
  client.captureAi(event);
46
114
  return;
47
115
  }
@@ -49,7 +117,7 @@ function captureAiEvent(client, event) {
49
117
  }
50
118
  /** @internal */
51
119
  async function captureAiEventImmediate(client, event) {
52
- if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === "function") {
120
+ if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
53
121
  await client.captureAiImmediate(event);
54
122
  return;
55
123
  }
@@ -197,13 +265,17 @@ var MediaTypeContext = class MediaTypeContext {
197
265
  //#region src/sanitization/binary_content_redactor.ts
198
266
  const STRONG_CONTEXT_MIN_LENGTH = 64;
199
267
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
268
+ const MAX_NODES$1 = 1e4;
269
+ const TRUNCATED$1 = "[Truncated]";
200
270
  var BinaryContentRedactor = class {
201
271
  constructor(recognizer = new Base64Recognizer()) {
202
272
  this.recognizer = recognizer;
203
- this.visited = /* @__PURE__ */ new WeakSet();
273
+ this.ancestors = /* @__PURE__ */ new WeakSet();
274
+ this.remainingNodes = MAX_NODES$1;
204
275
  }
205
276
  redact(value, mediaType) {
206
- this.visited = /* @__PURE__ */ new WeakSet();
277
+ this.ancestors = /* @__PURE__ */ new WeakSet();
278
+ this.remainingNodes = MAX_NODES$1;
207
279
  return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
208
280
  }
209
281
  /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
@@ -215,18 +287,24 @@ var BinaryContentRedactor = class {
215
287
  if (typeof value === "string") return this.redactLeaf(value, ctx);
216
288
  if (typeof value !== "object") return value;
217
289
  if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
218
- if (this.visited.has(value)) return null;
219
- this.visited.add(value);
220
- if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
221
- const obj = value;
222
- const out = {};
223
- for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
224
- value: this.walk(obj[k], new MediaTypeContext(obj, k)),
225
- enumerable: true,
226
- writable: true,
227
- configurable: true
228
- });
229
- return out;
290
+ if (this.ancestors.has(value)) return null;
291
+ if (this.remainingNodes <= 0) return TRUNCATED$1;
292
+ this.remainingNodes--;
293
+ this.ancestors.add(value);
294
+ try {
295
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
296
+ const obj = value;
297
+ const out = {};
298
+ for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
299
+ value: this.walk(obj[k], new MediaTypeContext(obj, k)),
300
+ enumerable: true,
301
+ writable: true,
302
+ configurable: true
303
+ });
304
+ return out;
305
+ } finally {
306
+ this.ancestors.delete(value);
307
+ }
230
308
  }
231
309
  redactString(value, ctx) {
232
310
  const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
@@ -393,7 +471,8 @@ const POSTHOG_PARAMS_MAP = {
393
471
  posthogModelOverride: "modelOverride",
394
472
  posthogProviderOverride: "providerOverride",
395
473
  posthogCostOverride: "costOverride",
396
- posthogCaptureImmediate: "captureImmediate"
474
+ posthogCaptureImmediate: "captureImmediate",
475
+ posthogOptions: "options"
397
476
  };
398
477
  function extractPosthogParams(body) {
399
478
  const providerParams = {};
@@ -415,7 +494,7 @@ function addDefaults(params) {
415
494
  }
416
495
  //#endregion
417
496
  //#region package.json
418
- var version = "8.20.1";
497
+ var version = "8.21.0";
419
498
  //#endregion
420
499
  //#region src/serializeError.ts
421
500
  const DEFAULT_MAX_DEPTH = 3;
@@ -610,6 +689,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
610
689
  //#endregion
611
690
  //#region src/captureAiGeneration.ts
612
691
  /**
692
+ * posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
693
+ * arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
694
+ */
695
+ const captureAiException = (client, error, properties, uuid$1) => {
696
+ if (isPostHogNode6(client)) {
697
+ client.captureException(error, {
698
+ properties,
699
+ uuid: uuid$1
700
+ });
701
+ return;
702
+ }
703
+ client.captureException.call(client, error, void 0, properties, uuid$1);
704
+ };
705
+ /**
613
706
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
614
707
  *
615
708
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -645,7 +738,7 @@ const captureAiGeneration = async (client, options) => {
645
738
  if (client.options?.enableExceptionAutocapture) {
646
739
  if (!shouldRedact) {
647
740
  exceptionId = (0, _posthog_core.uuidv7)();
648
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
741
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
649
742
  }
650
743
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
651
744
  }
@@ -671,6 +764,7 @@ const captureAiGeneration = async (client, options) => {
671
764
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
672
765
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
673
766
  };
767
+ const distinctId = userDistinctId(options.distinctId);
674
768
  const properties = {
675
769
  $ai_lib: "posthog-ai",
676
770
  $ai_lib_version: version,
@@ -690,7 +784,6 @@ const captureAiGeneration = async (client, options) => {
690
784
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
691
785
  ...options.properties,
692
786
  $ai_tokens_source: getTokensSource(options.properties),
693
- ...options.distinctId ? {} : { $process_person_profile: false },
694
787
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
695
788
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
696
789
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -699,9 +792,9 @@ const captureAiGeneration = async (client, options) => {
699
792
  ...costOverrideData
700
793
  };
701
794
  const event = {
702
- distinctId: options.distinctId ?? traceId,
795
+ distinctId: distinctId ?? traceId,
703
796
  event: eventType,
704
- properties,
797
+ ...eventOptionFields(client, properties, options.options, !distinctId),
705
798
  groups: options.groups
706
799
  };
707
800
  if (options.captureImmediate) await captureAiEventImmediate(client, event);