@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 { Options, Query, SDKResultMessage, SDKUserMessage } from "@anthropic-ai/claude-agent-sdk";
2
+ import { JsonType } from "@posthog/core";
2
3
  import { PostHog } from "posthog-node";
3
4
  //#region src/claude-agent-sdk/processor.d.ts
4
5
  /**
@@ -22,6 +23,11 @@ interface ClaudeAgentTraceOptions {
22
23
  groups?: Record<string, string | number>;
23
24
  /** Extra properties merged into every captured AI event. */
24
25
  properties?: Record<string, unknown>;
26
+ /**
27
+ * Per-event options for posthog-node, such as `{ process_person_profile: false }`, sent with
28
+ * every event. An event with no distinct id always has person processing turned off.
29
+ */
30
+ options?: Record<string, JsonType>;
25
31
  }
26
32
  interface PostHogClaudeAgentProcessorOptions extends ClaudeAgentTraceOptions {
27
33
  /** The PostHog client used to capture events. */
@@ -1,17 +1,85 @@
1
1
  import { SYSTEM_PROMPT_DYNAMIC_BOUNDARY, query as query$1 } from "@anthropic-ai/claude-agent-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
  //#region package.json
5
- var version = "8.20.1";
5
+ var version = "8.21.0";
6
6
  //#endregion
7
7
  //#region src/captureAiEvent.ts
8
8
  /** @internal */
9
9
  function isFullAiCaptureEnabled(client) {
10
10
  return client?.enableFullAiCapture === true;
11
11
  }
12
+ /**
13
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
14
+ *
15
+ * @internal
16
+ */
17
+ function userDistinctId(value) {
18
+ if (!value) return;
19
+ const id = String(value);
20
+ return id.trim() === "" ? void 0 : id;
21
+ }
22
+ /**
23
+ * The caller's per-event options when they are an object. A missing or `null` value means no
24
+ * options. An array or any other value is reported and ignored, so it is never spread.
25
+ *
26
+ * @internal
27
+ */
28
+ function readAiEventOptions(value) {
29
+ if (value === void 0 || value === null) return;
30
+ if (typeof value === "object" && !Array.isArray(value)) return value;
31
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
32
+ }
33
+ /**
34
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
35
+ * processing off and beats the caller's `process_person_profile`.
36
+ *
37
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
38
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
39
+ * reads, and an unknown key is dropped.
40
+ *
41
+ * @internal
42
+ */
43
+ function eventOptionFields(client, properties, options, personless) {
44
+ const userOptions = readAiEventOptions(options);
45
+ if (isPostHogNode6(client)) {
46
+ const eventOptions = {
47
+ ...userOptions,
48
+ ...personless ? { process_person_profile: false } : {}
49
+ };
50
+ return Object.keys(eventOptions).length > 0 ? {
51
+ properties,
52
+ options: eventOptions
53
+ } : { properties };
54
+ }
55
+ const withOptionProperties = {
56
+ ...properties,
57
+ ...eventOptionsAsLegacyProperties(userOptions)
58
+ };
59
+ if (personless) withOptionProperties.$process_person_profile = false;
60
+ return { properties: withOptionProperties };
61
+ }
62
+ /**
63
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
64
+ *
65
+ * @internal
66
+ */
67
+ function isPostHogNode6(client) {
68
+ return acceptsEventOptions(client);
69
+ }
70
+ /**
71
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
72
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
73
+ * upgrade never changes how a 5.x client sends.
74
+ *
75
+ * @internal
76
+ */
77
+ function usesAiLane(client) {
78
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
79
+ }
12
80
  /** @internal */
13
81
  function captureAiEvent(client, event) {
14
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
82
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
15
83
  client.captureAi(event);
16
84
  return;
17
85
  }
@@ -19,7 +87,7 @@ function captureAiEvent(client, event) {
19
87
  }
20
88
  /** @internal */
21
89
  async function captureAiEventImmediate(client, event) {
22
- if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === "function") {
90
+ if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
23
91
  await client.captureAiImmediate(event);
24
92
  return;
25
93
  }
@@ -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$1 = 1e4;
244
+ const TRUNCATED$1 = "[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$1;
179
250
  }
180
251
  redact(value, mediaType) {
181
- this.visited = /* @__PURE__ */ new WeakSet();
252
+ this.ancestors = /* @__PURE__ */ new WeakSet();
253
+ this.remainingNodes = MAX_NODES$1;
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$1;
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();
@@ -496,6 +574,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
496
574
  //#endregion
497
575
  //#region src/captureAiGeneration.ts
498
576
  /**
577
+ * posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
578
+ * arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
579
+ */
580
+ const captureAiException = (client, error, properties, uuid) => {
581
+ if (isPostHogNode6(client)) {
582
+ client.captureException(error, {
583
+ properties,
584
+ uuid
585
+ });
586
+ return;
587
+ }
588
+ client.captureException.call(client, error, void 0, properties, uuid);
589
+ };
590
+ /**
499
591
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
500
592
  *
501
593
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -531,7 +623,7 @@ const captureAiGeneration = async (client, options) => {
531
623
  if (client.options?.enableExceptionAutocapture) {
532
624
  if (!shouldRedact) {
533
625
  exceptionId = uuidv7();
534
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
626
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
535
627
  }
536
628
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
537
629
  }
@@ -557,6 +649,7 @@ const captureAiGeneration = async (client, options) => {
557
649
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
558
650
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
559
651
  };
652
+ const distinctId = userDistinctId(options.distinctId);
560
653
  const properties = {
561
654
  $ai_lib: "posthog-ai",
562
655
  $ai_lib_version: version,
@@ -576,7 +669,6 @@ const captureAiGeneration = async (client, options) => {
576
669
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
577
670
  ...options.properties,
578
671
  $ai_tokens_source: getTokensSource(options.properties),
579
- ...options.distinctId ? {} : { $process_person_profile: false },
580
672
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
581
673
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
582
674
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -585,9 +677,9 @@ const captureAiGeneration = async (client, options) => {
585
677
  ...costOverrideData
586
678
  };
587
679
  const event = {
588
- distinctId: options.distinctId ?? traceId,
680
+ distinctId: distinctId ?? traceId,
589
681
  event: eventType,
590
- properties,
682
+ ...eventOptionFields(client, properties, options.options, !distinctId),
591
683
  groups: options.groups
592
684
  };
593
685
  if (options.captureImmediate) await captureAiEventImmediate(client, event);
@@ -889,6 +981,10 @@ var PostHogClaudeAgentProcessor = class {
889
981
  properties: {
890
982
  ...this._traceOptions.properties,
891
983
  ...posthog?.properties
984
+ },
985
+ options: {
986
+ ...readAiEventOptions(this._traceOptions.options),
987
+ ...readAiEventOptions(posthog?.options)
892
988
  }
893
989
  };
894
990
  for (const key of [
@@ -1131,6 +1227,7 @@ var PostHogClaudeAgentProcessor = class {
1131
1227
  usage: generation.usage,
1132
1228
  stopReason: generation.stopReason,
1133
1229
  groups: trace.groups,
1230
+ options: trace.options,
1134
1231
  privacyMode: trace.privacyMode ?? false,
1135
1232
  captureImmediate: this._captureImmediate,
1136
1233
  onError: this._onError,
@@ -1245,20 +1342,20 @@ var PostHogClaudeAgentProcessor = class {
1245
1342
  }
1246
1343
  }
1247
1344
  async _captureLifecycleEvent(event, distinctId, state, trace, properties) {
1345
+ const eventProperties = {
1346
+ $ai_lib: "posthog-ai",
1347
+ $ai_lib_version: version,
1348
+ $ai_framework: FRAMEWORK,
1349
+ $ai_provider: PROVIDER,
1350
+ $ai_trace_id: state.traceId,
1351
+ ...state.sessionId ? { $ai_session_id: state.sessionId } : {},
1352
+ ...properties,
1353
+ ...trace.properties
1354
+ };
1248
1355
  const message = {
1249
1356
  distinctId: distinctId ?? state.traceId,
1250
1357
  event,
1251
- properties: {
1252
- $ai_lib: "posthog-ai",
1253
- $ai_lib_version: version,
1254
- $ai_framework: FRAMEWORK,
1255
- $ai_provider: PROVIDER,
1256
- $ai_trace_id: state.traceId,
1257
- ...state.sessionId ? { $ai_session_id: state.sessionId } : {},
1258
- ...properties,
1259
- ...trace.properties,
1260
- ...distinctId ? {} : { $process_person_profile: false }
1261
- },
1358
+ ...eventOptionFields(this._client, eventProperties, trace.options, !distinctId),
1262
1359
  groups: trace.groups
1263
1360
  };
1264
1361
  try {
@@ -1298,10 +1395,9 @@ function readUsage(usage) {
1298
1395
  function resolveDistinctId(resolver, result) {
1299
1396
  if (typeof resolver === "function") {
1300
1397
  if (!result) return;
1301
- const resolved = resolver(result);
1302
- return resolved ? String(resolved) : void 0;
1398
+ return userDistinctId(resolver(result));
1303
1399
  }
1304
- return resolver ? String(resolver) : void 0;
1400
+ return userDistinctId(resolver);
1305
1401
  }
1306
1402
  //#endregion
1307
1403
  //#region src/claude-agent-sdk/index.ts