@posthog/ai 8.20.2 → 8.21.1

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 (62) hide show
  1. package/README.md +20 -0
  2. package/dist/adk/index.cjs +101 -22
  3. package/dist/adk/index.cjs.map +1 -1
  4. package/dist/adk/index.d.ts +7 -0
  5. package/dist/adk/index.mjs +102 -23
  6. package/dist/adk/index.mjs.map +1 -1
  7. package/dist/anthropic/index.cjs +92 -9
  8. package/dist/anthropic/index.cjs.map +1 -1
  9. package/dist/anthropic/index.d.ts +7 -0
  10. package/dist/anthropic/index.mjs +93 -10
  11. package/dist/anthropic/index.mjs.map +1 -1
  12. package/dist/claude-agent-sdk/index.cjs +107 -21
  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 +108 -22
  16. package/dist/claude-agent-sdk/index.mjs.map +1 -1
  17. package/dist/gemini/index.cjs +92 -9
  18. package/dist/gemini/index.cjs.map +1 -1
  19. package/dist/gemini/index.d.ts +7 -0
  20. package/dist/gemini/index.mjs +92 -9
  21. package/dist/gemini/index.mjs.map +1 -1
  22. package/dist/genkit/index.cjs +114 -29
  23. package/dist/genkit/index.cjs.map +1 -1
  24. package/dist/genkit/index.d.ts +7 -0
  25. package/dist/genkit/index.mjs +114 -29
  26. package/dist/genkit/index.mjs.map +1 -1
  27. package/dist/index.cjs +90 -7
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.ts +11 -0
  30. package/dist/index.mjs +91 -8
  31. package/dist/index.mjs.map +1 -1
  32. package/dist/langchain/index.cjs +75 -7
  33. package/dist/langchain/index.cjs.map +1 -1
  34. package/dist/langchain/index.d.ts +7 -0
  35. package/dist/langchain/index.mjs +75 -7
  36. package/dist/langchain/index.mjs.map +1 -1
  37. package/dist/langchain/middleware/index.cjs +75 -7
  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 +75 -7
  41. package/dist/langchain/middleware/index.mjs.map +1 -1
  42. package/dist/openai/index.cjs +91 -8
  43. package/dist/openai/index.cjs.map +1 -1
  44. package/dist/openai/index.d.ts +7 -0
  45. package/dist/openai/index.mjs +92 -9
  46. package/dist/openai/index.mjs.map +1 -1
  47. package/dist/openai-agents/index.cjs +115 -46
  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 +115 -46
  51. package/dist/openai-agents/index.mjs.map +1 -1
  52. package/dist/otel/index.browser.cjs.map +1 -1
  53. package/dist/otel/index.browser.mjs.map +1 -1
  54. package/dist/otel/index.cjs.map +1 -1
  55. package/dist/otel/index.d.ts +3 -3
  56. package/dist/otel/index.mjs.map +1 -1
  57. package/dist/vercel/index.cjs +90 -7
  58. package/dist/vercel/index.cjs.map +1 -1
  59. package/dist/vercel/index.d.ts +6 -0
  60. package/dist/vercel/index.mjs +91 -8
  61. package/dist/vercel/index.mjs.map +1 -1
  62. 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.2";
5
+ var version = "8.21.1";
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
  }
@@ -506,6 +574,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
506
574
  //#endregion
507
575
  //#region src/captureAiGeneration.ts
508
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
+ /**
509
591
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
510
592
  *
511
593
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -541,7 +623,7 @@ const captureAiGeneration = async (client, options) => {
541
623
  if (client.options?.enableExceptionAutocapture) {
542
624
  if (!shouldRedact) {
543
625
  exceptionId = uuidv7();
544
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
626
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
545
627
  }
546
628
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
547
629
  }
@@ -567,6 +649,7 @@ const captureAiGeneration = async (client, options) => {
567
649
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
568
650
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
569
651
  };
652
+ const distinctId = userDistinctId(options.distinctId);
570
653
  const properties = {
571
654
  $ai_lib: "posthog-ai",
572
655
  $ai_lib_version: version,
@@ -586,7 +669,6 @@ const captureAiGeneration = async (client, options) => {
586
669
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
587
670
  ...options.properties,
588
671
  $ai_tokens_source: getTokensSource(options.properties),
589
- ...options.distinctId ? {} : { $process_person_profile: false },
590
672
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
591
673
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
592
674
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -595,9 +677,9 @@ const captureAiGeneration = async (client, options) => {
595
677
  ...costOverrideData
596
678
  };
597
679
  const event = {
598
- distinctId: options.distinctId ?? traceId,
680
+ distinctId: distinctId ?? traceId,
599
681
  event: eventType,
600
- properties,
682
+ ...eventOptionFields(client, properties, options.options, !distinctId),
601
683
  groups: options.groups
602
684
  };
603
685
  if (options.captureImmediate) await captureAiEventImmediate(client, event);
@@ -899,6 +981,10 @@ var PostHogClaudeAgentProcessor = class {
899
981
  properties: {
900
982
  ...this._traceOptions.properties,
901
983
  ...posthog?.properties
984
+ },
985
+ options: {
986
+ ...readAiEventOptions(this._traceOptions.options),
987
+ ...readAiEventOptions(posthog?.options)
902
988
  }
903
989
  };
904
990
  for (const key of [
@@ -1141,6 +1227,7 @@ var PostHogClaudeAgentProcessor = class {
1141
1227
  usage: generation.usage,
1142
1228
  stopReason: generation.stopReason,
1143
1229
  groups: trace.groups,
1230
+ options: trace.options,
1144
1231
  privacyMode: trace.privacyMode ?? false,
1145
1232
  captureImmediate: this._captureImmediate,
1146
1233
  onError: this._onError,
@@ -1255,20 +1342,20 @@ var PostHogClaudeAgentProcessor = class {
1255
1342
  }
1256
1343
  }
1257
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
+ };
1258
1355
  const message = {
1259
1356
  distinctId: distinctId ?? state.traceId,
1260
1357
  event,
1261
- properties: {
1262
- $ai_lib: "posthog-ai",
1263
- $ai_lib_version: version,
1264
- $ai_framework: FRAMEWORK,
1265
- $ai_provider: PROVIDER,
1266
- $ai_trace_id: state.traceId,
1267
- ...state.sessionId ? { $ai_session_id: state.sessionId } : {},
1268
- ...properties,
1269
- ...trace.properties,
1270
- ...distinctId ? {} : { $process_person_profile: false }
1271
- },
1358
+ ...eventOptionFields(this._client, eventProperties, trace.options, !distinctId),
1272
1359
  groups: trace.groups
1273
1360
  };
1274
1361
  try {
@@ -1308,10 +1395,9 @@ function readUsage(usage) {
1308
1395
  function resolveDistinctId(resolver, result) {
1309
1396
  if (typeof resolver === "function") {
1310
1397
  if (!result) return;
1311
- const resolved = resolver(result);
1312
- return resolved ? String(resolved) : void 0;
1398
+ return userDistinctId(resolver(result));
1313
1399
  }
1314
- return resolver ? String(resolver) : void 0;
1400
+ return userDistinctId(resolver);
1315
1401
  }
1316
1402
  //#endregion
1317
1403
  //#region src/claude-agent-sdk/index.ts