@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
@@ -3,16 +3,84 @@ let _anthropic_ai_claude_agent_sdk = require("@anthropic-ai/claude-agent-sdk");
3
3
  let uuid = require("uuid");
4
4
  let _posthog_core = require("@posthog/core");
5
5
  //#region package.json
6
- var version = "8.20.1";
6
+ var version = "8.21.0";
7
7
  //#endregion
8
8
  //#region src/captureAiEvent.ts
9
9
  /** @internal */
10
10
  function isFullAiCaptureEnabled(client) {
11
11
  return client?.enableFullAiCapture === true;
12
12
  }
13
+ /**
14
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
15
+ *
16
+ * @internal
17
+ */
18
+ function userDistinctId(value) {
19
+ if (!value) return;
20
+ const id = String(value);
21
+ return id.trim() === "" ? void 0 : id;
22
+ }
23
+ /**
24
+ * The caller's per-event options when they are an object. A missing or `null` value means no
25
+ * options. An array or any other value is reported and ignored, so it is never spread.
26
+ *
27
+ * @internal
28
+ */
29
+ function readAiEventOptions(value) {
30
+ if (value === void 0 || value === null) return;
31
+ if (typeof value === "object" && !Array.isArray(value)) return value;
32
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
33
+ }
34
+ /**
35
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
36
+ * processing off and beats the caller's `process_person_profile`.
37
+ *
38
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
39
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
40
+ * reads, and an unknown key is dropped.
41
+ *
42
+ * @internal
43
+ */
44
+ function eventOptionFields(client, properties, options, personless) {
45
+ const userOptions = readAiEventOptions(options);
46
+ if (isPostHogNode6(client)) {
47
+ const eventOptions = {
48
+ ...userOptions,
49
+ ...personless ? { process_person_profile: false } : {}
50
+ };
51
+ return Object.keys(eventOptions).length > 0 ? {
52
+ properties,
53
+ options: eventOptions
54
+ } : { properties };
55
+ }
56
+ const withOptionProperties = {
57
+ ...properties,
58
+ ...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
59
+ };
60
+ if (personless) withOptionProperties.$process_person_profile = false;
61
+ return { properties: withOptionProperties };
62
+ }
63
+ /**
64
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
65
+ *
66
+ * @internal
67
+ */
68
+ function isPostHogNode6(client) {
69
+ return (0, _posthog_core.acceptsEventOptions)(client);
70
+ }
71
+ /**
72
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
73
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
74
+ * upgrade never changes how a 5.x client sends.
75
+ *
76
+ * @internal
77
+ */
78
+ function usesAiLane(client) {
79
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
80
+ }
13
81
  /** @internal */
14
82
  function captureAiEvent(client, event) {
15
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
83
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
16
84
  client.captureAi(event);
17
85
  return;
18
86
  }
@@ -20,7 +88,7 @@ function captureAiEvent(client, event) {
20
88
  }
21
89
  /** @internal */
22
90
  async function captureAiEventImmediate(client, event) {
23
- if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === "function") {
91
+ if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
24
92
  await client.captureAiImmediate(event);
25
93
  return;
26
94
  }
@@ -173,13 +241,17 @@ var MediaTypeContext = class MediaTypeContext {
173
241
  //#region src/sanitization/binary_content_redactor.ts
174
242
  const STRONG_CONTEXT_MIN_LENGTH = 64;
175
243
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
244
+ const MAX_NODES$1 = 1e4;
245
+ const TRUNCATED$1 = "[Truncated]";
176
246
  var BinaryContentRedactor = class {
177
247
  constructor(recognizer = new Base64Recognizer()) {
178
248
  this.recognizer = recognizer;
179
- this.visited = /* @__PURE__ */ new WeakSet();
249
+ this.ancestors = /* @__PURE__ */ new WeakSet();
250
+ this.remainingNodes = MAX_NODES$1;
180
251
  }
181
252
  redact(value, mediaType) {
182
- this.visited = /* @__PURE__ */ new WeakSet();
253
+ this.ancestors = /* @__PURE__ */ new WeakSet();
254
+ this.remainingNodes = MAX_NODES$1;
183
255
  return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
184
256
  }
185
257
  /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
@@ -191,18 +263,24 @@ var BinaryContentRedactor = class {
191
263
  if (typeof value === "string") return this.redactLeaf(value, ctx);
192
264
  if (typeof value !== "object") return value;
193
265
  if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
194
- if (this.visited.has(value)) return null;
195
- this.visited.add(value);
196
- if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
197
- const obj = value;
198
- const out = {};
199
- for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
200
- value: this.walk(obj[k], new MediaTypeContext(obj, k)),
201
- enumerable: true,
202
- writable: true,
203
- configurable: true
204
- });
205
- return out;
266
+ if (this.ancestors.has(value)) return null;
267
+ if (this.remainingNodes <= 0) return TRUNCATED$1;
268
+ this.remainingNodes--;
269
+ this.ancestors.add(value);
270
+ try {
271
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
272
+ const obj = value;
273
+ const out = {};
274
+ for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
275
+ value: this.walk(obj[k], new MediaTypeContext(obj, k)),
276
+ enumerable: true,
277
+ writable: true,
278
+ configurable: true
279
+ });
280
+ return out;
281
+ } finally {
282
+ this.ancestors.delete(value);
283
+ }
206
284
  }
207
285
  redactString(value, ctx) {
208
286
  const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
@@ -497,6 +575,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
497
575
  //#endregion
498
576
  //#region src/captureAiGeneration.ts
499
577
  /**
578
+ * posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
579
+ * arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
580
+ */
581
+ const captureAiException = (client, error, properties, uuid$1) => {
582
+ if (isPostHogNode6(client)) {
583
+ client.captureException(error, {
584
+ properties,
585
+ uuid: uuid$1
586
+ });
587
+ return;
588
+ }
589
+ client.captureException.call(client, error, void 0, properties, uuid$1);
590
+ };
591
+ /**
500
592
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
501
593
  *
502
594
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -532,7 +624,7 @@ const captureAiGeneration = async (client, options) => {
532
624
  if (client.options?.enableExceptionAutocapture) {
533
625
  if (!shouldRedact) {
534
626
  exceptionId = (0, _posthog_core.uuidv7)();
535
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
627
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
536
628
  }
537
629
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
538
630
  }
@@ -558,6 +650,7 @@ const captureAiGeneration = async (client, options) => {
558
650
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
559
651
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
560
652
  };
653
+ const distinctId = userDistinctId(options.distinctId);
561
654
  const properties = {
562
655
  $ai_lib: "posthog-ai",
563
656
  $ai_lib_version: version,
@@ -577,7 +670,6 @@ const captureAiGeneration = async (client, options) => {
577
670
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
578
671
  ...options.properties,
579
672
  $ai_tokens_source: getTokensSource(options.properties),
580
- ...options.distinctId ? {} : { $process_person_profile: false },
581
673
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
582
674
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
583
675
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -586,9 +678,9 @@ const captureAiGeneration = async (client, options) => {
586
678
  ...costOverrideData
587
679
  };
588
680
  const event = {
589
- distinctId: options.distinctId ?? traceId,
681
+ distinctId: distinctId ?? traceId,
590
682
  event: eventType,
591
- properties,
683
+ ...eventOptionFields(client, properties, options.options, !distinctId),
592
684
  groups: options.groups
593
685
  };
594
686
  if (options.captureImmediate) await captureAiEventImmediate(client, event);
@@ -890,6 +982,10 @@ var PostHogClaudeAgentProcessor = class {
890
982
  properties: {
891
983
  ...this._traceOptions.properties,
892
984
  ...posthog?.properties
985
+ },
986
+ options: {
987
+ ...readAiEventOptions(this._traceOptions.options),
988
+ ...readAiEventOptions(posthog?.options)
893
989
  }
894
990
  };
895
991
  for (const key of [
@@ -1132,6 +1228,7 @@ var PostHogClaudeAgentProcessor = class {
1132
1228
  usage: generation.usage,
1133
1229
  stopReason: generation.stopReason,
1134
1230
  groups: trace.groups,
1231
+ options: trace.options,
1135
1232
  privacyMode: trace.privacyMode ?? false,
1136
1233
  captureImmediate: this._captureImmediate,
1137
1234
  onError: this._onError,
@@ -1246,20 +1343,20 @@ var PostHogClaudeAgentProcessor = class {
1246
1343
  }
1247
1344
  }
1248
1345
  async _captureLifecycleEvent(event, distinctId, state, trace, properties) {
1346
+ const eventProperties = {
1347
+ $ai_lib: "posthog-ai",
1348
+ $ai_lib_version: version,
1349
+ $ai_framework: FRAMEWORK,
1350
+ $ai_provider: PROVIDER,
1351
+ $ai_trace_id: state.traceId,
1352
+ ...state.sessionId ? { $ai_session_id: state.sessionId } : {},
1353
+ ...properties,
1354
+ ...trace.properties
1355
+ };
1249
1356
  const message = {
1250
1357
  distinctId: distinctId ?? state.traceId,
1251
1358
  event,
1252
- properties: {
1253
- $ai_lib: "posthog-ai",
1254
- $ai_lib_version: version,
1255
- $ai_framework: FRAMEWORK,
1256
- $ai_provider: PROVIDER,
1257
- $ai_trace_id: state.traceId,
1258
- ...state.sessionId ? { $ai_session_id: state.sessionId } : {},
1259
- ...properties,
1260
- ...trace.properties,
1261
- ...distinctId ? {} : { $process_person_profile: false }
1262
- },
1359
+ ...eventOptionFields(this._client, eventProperties, trace.options, !distinctId),
1263
1360
  groups: trace.groups
1264
1361
  };
1265
1362
  try {
@@ -1299,10 +1396,9 @@ function readUsage(usage) {
1299
1396
  function resolveDistinctId(resolver, result) {
1300
1397
  if (typeof resolver === "function") {
1301
1398
  if (!result) return;
1302
- const resolved = resolver(result);
1303
- return resolved ? String(resolved) : void 0;
1399
+ return userDistinctId(resolver(result));
1304
1400
  }
1305
- return resolver ? String(resolver) : void 0;
1401
+ return userDistinctId(resolver);
1306
1402
  }
1307
1403
  //#endregion
1308
1404
  //#region src/claude-agent-sdk/index.ts