@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
@@ -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
  }
@@ -403,7 +471,8 @@ const POSTHOG_PARAMS_MAP = {
403
471
  posthogModelOverride: "modelOverride",
404
472
  posthogProviderOverride: "providerOverride",
405
473
  posthogCostOverride: "costOverride",
406
- posthogCaptureImmediate: "captureImmediate"
474
+ posthogCaptureImmediate: "captureImmediate",
475
+ posthogOptions: "options"
407
476
  };
408
477
  function extractPosthogParams(body) {
409
478
  const providerParams = {};
@@ -425,7 +494,7 @@ function addDefaults(params) {
425
494
  }
426
495
  //#endregion
427
496
  //#region package.json
428
- var version = "8.20.2";
497
+ var version = "8.21.1";
429
498
  //#endregion
430
499
  //#region src/serializeError.ts
431
500
  const DEFAULT_MAX_DEPTH = 3;
@@ -620,6 +689,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
620
689
  //#endregion
621
690
  //#region src/captureAiGeneration.ts
622
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
+ /**
623
706
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
624
707
  *
625
708
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -655,7 +738,7 @@ const captureAiGeneration = async (client, options) => {
655
738
  if (client.options?.enableExceptionAutocapture) {
656
739
  if (!shouldRedact) {
657
740
  exceptionId = (0, _posthog_core.uuidv7)();
658
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
741
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
659
742
  }
660
743
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
661
744
  }
@@ -681,6 +764,7 @@ const captureAiGeneration = async (client, options) => {
681
764
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
682
765
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
683
766
  };
767
+ const distinctId = userDistinctId(options.distinctId);
684
768
  const properties = {
685
769
  $ai_lib: "posthog-ai",
686
770
  $ai_lib_version: version,
@@ -700,7 +784,6 @@ const captureAiGeneration = async (client, options) => {
700
784
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
701
785
  ...options.properties,
702
786
  $ai_tokens_source: getTokensSource(options.properties),
703
- ...options.distinctId ? {} : { $process_person_profile: false },
704
787
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
705
788
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
706
789
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -709,9 +792,9 @@ const captureAiGeneration = async (client, options) => {
709
792
  ...costOverrideData
710
793
  };
711
794
  const event = {
712
- distinctId: options.distinctId ?? traceId,
795
+ distinctId: distinctId ?? traceId,
713
796
  event: eventType,
714
- properties,
797
+ ...eventOptionFields(client, properties, options.options, !distinctId),
715
798
  groups: options.groups
716
799
  };
717
800
  if (options.captureImmediate) await captureAiEventImmediate(client, event);
@@ -1156,7 +1239,7 @@ var WrappedMessages = class extends _anthropic_ai_sdk.default.Messages {
1156
1239
  currentReasoningBlock = null;
1157
1240
  const tool = toolsInProgress.get(chunk.index);
1158
1241
  if (tool) {
1159
- try {
1242
+ if (tool.inputString) try {
1160
1243
  tool.block.function.arguments = JSON.parse(tool.inputString);
1161
1244
  } catch (e) {
1162
1245
  console.error("Error parsing tool input:", e);