@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,4 +1,5 @@
1
1
  import AnthropicOriginal, { APIPromise, ClientOptions } from "@anthropic-ai/sdk";
2
+ import { JsonType } from "@posthog/core";
2
3
  import { Stream } from "@anthropic-ai/sdk/streaming";
3
4
  import { PostHog } from "posthog-node";
4
5
  import "openai";
@@ -14,6 +15,12 @@ interface MonitoringEventPropertiesWithDefaults {
14
15
  providerOverride?: string;
15
16
  costOverride?: CostOverride;
16
17
  captureImmediate?: boolean;
18
+ /**
19
+ * Per-event options for posthog-node, such as `{ process_person_profile: false }`. posthog-node 5
20
+ * has no per-event options, so each option it knows is sent the way that version reads it, and
21
+ * other keys are dropped. An event with no distinct id always has person processing turned off.
22
+ */
23
+ options?: Record<string, JsonType>;
17
24
  }
18
25
  type MonitoringEventProperties = Partial<MonitoringEventPropertiesWithDefaults>;
19
26
  type MonitoringParams = { [K in keyof MonitoringEventProperties as `posthog${Capitalize<string & K>}`]: MonitoringEventProperties[K]; };
@@ -1,6 +1,6 @@
1
1
  import AnthropicOriginal from "@anthropic-ai/sdk";
2
2
  import { v4 } from "uuid";
3
- import { toJsonSafeValue, uuidv7 } from "@posthog/core";
3
+ import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
4
4
  import { Stream } from "@anthropic-ai/sdk/streaming";
5
5
  //#region src/typeGuards.ts
6
6
  const isObject = (value) => {
@@ -12,9 +12,77 @@ const isObject = (value) => {
12
12
  function isFullAiCaptureEnabled(client) {
13
13
  return client?.enableFullAiCapture === true;
14
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
+ ...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 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
+ }
15
83
  /** @internal */
16
84
  function captureAiEvent(client, event) {
17
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
85
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
18
86
  client.captureAi(event);
19
87
  return;
20
88
  }
@@ -22,7 +90,7 @@ function captureAiEvent(client, event) {
22
90
  }
23
91
  /** @internal */
24
92
  async function captureAiEventImmediate(client, event) {
25
- if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === "function") {
93
+ if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
26
94
  await client.captureAiImmediate(event);
27
95
  return;
28
96
  }
@@ -170,13 +238,17 @@ var MediaTypeContext = class MediaTypeContext {
170
238
  //#region src/sanitization/binary_content_redactor.ts
171
239
  const STRONG_CONTEXT_MIN_LENGTH = 64;
172
240
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
241
+ const MAX_NODES$1 = 1e4;
242
+ const TRUNCATED$1 = "[Truncated]";
173
243
  var BinaryContentRedactor = class {
174
244
  constructor(recognizer = new Base64Recognizer()) {
175
245
  this.recognizer = recognizer;
176
- this.visited = /* @__PURE__ */ new WeakSet();
246
+ this.ancestors = /* @__PURE__ */ new WeakSet();
247
+ this.remainingNodes = MAX_NODES$1;
177
248
  }
178
249
  redact(value, mediaType) {
179
- this.visited = /* @__PURE__ */ new WeakSet();
250
+ this.ancestors = /* @__PURE__ */ new WeakSet();
251
+ this.remainingNodes = MAX_NODES$1;
180
252
  return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
181
253
  }
182
254
  /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
@@ -188,18 +260,24 @@ var BinaryContentRedactor = class {
188
260
  if (typeof value === "string") return this.redactLeaf(value, ctx);
189
261
  if (typeof value !== "object") return value;
190
262
  if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
191
- if (this.visited.has(value)) return null;
192
- this.visited.add(value);
193
- if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
194
- const obj = value;
195
- const out = {};
196
- for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
197
- value: this.walk(obj[k], new MediaTypeContext(obj, k)),
198
- enumerable: true,
199
- writable: true,
200
- configurable: true
201
- });
202
- return out;
263
+ if (this.ancestors.has(value)) return null;
264
+ if (this.remainingNodes <= 0) return TRUNCATED$1;
265
+ this.remainingNodes--;
266
+ this.ancestors.add(value);
267
+ try {
268
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
269
+ const obj = value;
270
+ const out = {};
271
+ for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
272
+ value: this.walk(obj[k], new MediaTypeContext(obj, k)),
273
+ enumerable: true,
274
+ writable: true,
275
+ configurable: true
276
+ });
277
+ return out;
278
+ } finally {
279
+ this.ancestors.delete(value);
280
+ }
203
281
  }
204
282
  redactString(value, ctx) {
205
283
  const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
@@ -366,7 +444,8 @@ const POSTHOG_PARAMS_MAP = {
366
444
  posthogModelOverride: "modelOverride",
367
445
  posthogProviderOverride: "providerOverride",
368
446
  posthogCostOverride: "costOverride",
369
- posthogCaptureImmediate: "captureImmediate"
447
+ posthogCaptureImmediate: "captureImmediate",
448
+ posthogOptions: "options"
370
449
  };
371
450
  function extractPosthogParams(body) {
372
451
  const providerParams = {};
@@ -388,7 +467,7 @@ function addDefaults(params) {
388
467
  }
389
468
  //#endregion
390
469
  //#region package.json
391
- var version = "8.20.1";
470
+ var version = "8.21.0";
392
471
  //#endregion
393
472
  //#region src/serializeError.ts
394
473
  const DEFAULT_MAX_DEPTH = 3;
@@ -583,6 +662,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
583
662
  //#endregion
584
663
  //#region src/captureAiGeneration.ts
585
664
  /**
665
+ * posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
666
+ * arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
667
+ */
668
+ const captureAiException = (client, error, properties, uuid) => {
669
+ if (isPostHogNode6(client)) {
670
+ client.captureException(error, {
671
+ properties,
672
+ uuid
673
+ });
674
+ return;
675
+ }
676
+ client.captureException.call(client, error, void 0, properties, uuid);
677
+ };
678
+ /**
586
679
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
587
680
  *
588
681
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -618,7 +711,7 @@ const captureAiGeneration = async (client, options) => {
618
711
  if (client.options?.enableExceptionAutocapture) {
619
712
  if (!shouldRedact) {
620
713
  exceptionId = uuidv7();
621
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
714
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
622
715
  }
623
716
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
624
717
  }
@@ -644,6 +737,7 @@ const captureAiGeneration = async (client, options) => {
644
737
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
645
738
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
646
739
  };
740
+ const distinctId = userDistinctId(options.distinctId);
647
741
  const properties = {
648
742
  $ai_lib: "posthog-ai",
649
743
  $ai_lib_version: version,
@@ -663,7 +757,6 @@ const captureAiGeneration = async (client, options) => {
663
757
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
664
758
  ...options.properties,
665
759
  $ai_tokens_source: getTokensSource(options.properties),
666
- ...options.distinctId ? {} : { $process_person_profile: false },
667
760
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
668
761
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
669
762
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -672,9 +765,9 @@ const captureAiGeneration = async (client, options) => {
672
765
  ...costOverrideData
673
766
  };
674
767
  const event = {
675
- distinctId: options.distinctId ?? traceId,
768
+ distinctId: distinctId ?? traceId,
676
769
  event: eventType,
677
- properties,
770
+ ...eventOptionFields(client, properties, options.options, !distinctId),
678
771
  groups: options.groups
679
772
  };
680
773
  if (options.captureImmediate) await captureAiEventImmediate(client, event);