@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
@@ -2,17 +2,85 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
+ let _posthog_core = require("@posthog/core");
5
6
  let _google_genai = require("@google/genai");
6
7
  let uuid = require("uuid");
7
- let _posthog_core = require("@posthog/core");
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
  }
@@ -453,7 +521,8 @@ const POSTHOG_PARAMS_MAP = {
453
521
  posthogModelOverride: "modelOverride",
454
522
  posthogProviderOverride: "providerOverride",
455
523
  posthogCostOverride: "costOverride",
456
- posthogCaptureImmediate: "captureImmediate"
524
+ posthogCaptureImmediate: "captureImmediate",
525
+ posthogOptions: "options"
457
526
  };
458
527
  function extractPosthogParams(body) {
459
528
  const providerParams = {};
@@ -475,7 +544,7 @@ function addDefaults(params) {
475
544
  }
476
545
  //#endregion
477
546
  //#region package.json
478
- var version = "8.20.2";
547
+ var version = "8.21.1";
479
548
  //#endregion
480
549
  //#region src/serializeError.ts
481
550
  const DEFAULT_MAX_DEPTH = 3;
@@ -670,6 +739,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
670
739
  //#endregion
671
740
  //#region src/captureAiGeneration.ts
672
741
  /**
742
+ * posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
743
+ * arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
744
+ */
745
+ const captureAiException = (client, error, properties, uuid$1) => {
746
+ if (isPostHogNode6(client)) {
747
+ client.captureException(error, {
748
+ properties,
749
+ uuid: uuid$1
750
+ });
751
+ return;
752
+ }
753
+ client.captureException.call(client, error, void 0, properties, uuid$1);
754
+ };
755
+ /**
673
756
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
674
757
  *
675
758
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -705,7 +788,7 @@ const captureAiGeneration = async (client, options) => {
705
788
  if (client.options?.enableExceptionAutocapture) {
706
789
  if (!shouldRedact) {
707
790
  exceptionId = (0, _posthog_core.uuidv7)();
708
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
791
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
709
792
  }
710
793
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
711
794
  }
@@ -731,6 +814,7 @@ const captureAiGeneration = async (client, options) => {
731
814
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
732
815
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
733
816
  };
817
+ const distinctId = userDistinctId(options.distinctId);
734
818
  const properties = {
735
819
  $ai_lib: "posthog-ai",
736
820
  $ai_lib_version: version,
@@ -750,7 +834,6 @@ const captureAiGeneration = async (client, options) => {
750
834
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
751
835
  ...options.properties,
752
836
  $ai_tokens_source: getTokensSource(options.properties),
753
- ...options.distinctId ? {} : { $process_person_profile: false },
754
837
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
755
838
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
756
839
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -759,9 +842,9 @@ const captureAiGeneration = async (client, options) => {
759
842
  ...costOverrideData
760
843
  };
761
844
  const event = {
762
- distinctId: options.distinctId ?? traceId,
845
+ distinctId: distinctId ?? traceId,
763
846
  event: eventType,
764
- properties,
847
+ ...eventOptionFields(client, properties, options.options, !distinctId),
765
848
  groups: options.groups
766
849
  };
767
850
  if (options.captureImmediate) await captureAiEventImmediate(client, event);