@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
@@ -19,9 +19,77 @@ const isObject = (value) => {
19
19
  function isFullAiCaptureEnabled(client) {
20
20
  return client?.enableFullAiCapture === true;
21
21
  }
22
+ /**
23
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
24
+ *
25
+ * @internal
26
+ */
27
+ function userDistinctId(value) {
28
+ if (!value) return;
29
+ const id = String(value);
30
+ return id.trim() === "" ? void 0 : id;
31
+ }
32
+ /**
33
+ * The caller's per-event options when they are an object. A missing or `null` value means no
34
+ * options. An array or any other value is reported and ignored, so it is never spread.
35
+ *
36
+ * @internal
37
+ */
38
+ function readAiEventOptions(value) {
39
+ if (value === void 0 || value === null) return;
40
+ if (typeof value === "object" && !Array.isArray(value)) return value;
41
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
42
+ }
43
+ /**
44
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
45
+ * processing off and beats the caller's `process_person_profile`.
46
+ *
47
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
48
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
49
+ * reads, and an unknown key is dropped.
50
+ *
51
+ * @internal
52
+ */
53
+ function eventOptionFields(client, properties, options, personless) {
54
+ const userOptions = readAiEventOptions(options);
55
+ if (isPostHogNode6(client)) {
56
+ const eventOptions = {
57
+ ...userOptions,
58
+ ...personless ? { process_person_profile: false } : {}
59
+ };
60
+ return Object.keys(eventOptions).length > 0 ? {
61
+ properties,
62
+ options: eventOptions
63
+ } : { properties };
64
+ }
65
+ const withOptionProperties = {
66
+ ...properties,
67
+ ...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
68
+ };
69
+ if (personless) withOptionProperties.$process_person_profile = false;
70
+ return { properties: withOptionProperties };
71
+ }
72
+ /**
73
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
74
+ *
75
+ * @internal
76
+ */
77
+ function isPostHogNode6(client) {
78
+ return (0, _posthog_core.acceptsEventOptions)(client);
79
+ }
80
+ /**
81
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
82
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
83
+ * upgrade never changes how a 5.x client sends.
84
+ *
85
+ * @internal
86
+ */
87
+ function usesAiLane(client) {
88
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
89
+ }
22
90
  /** @internal */
23
91
  function captureAiEvent(client, event) {
24
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
92
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
25
93
  client.captureAi(event);
26
94
  return;
27
95
  }
@@ -29,7 +97,7 @@ function captureAiEvent(client, event) {
29
97
  }
30
98
  /** @internal */
31
99
  async function captureAiEventImmediate(client, event) {
32
- if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === "function") {
100
+ if (usesAiLane(client) && typeof client.captureAiImmediate === "function") {
33
101
  await client.captureAiImmediate(event);
34
102
  return;
35
103
  }
@@ -177,13 +245,17 @@ var MediaTypeContext = class MediaTypeContext {
177
245
  //#region src/sanitization/binary_content_redactor.ts
178
246
  const STRONG_CONTEXT_MIN_LENGTH = 64;
179
247
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
248
+ const MAX_NODES$1 = 1e4;
249
+ const TRUNCATED$1 = "[Truncated]";
180
250
  var BinaryContentRedactor = class {
181
251
  constructor(recognizer = new Base64Recognizer()) {
182
252
  this.recognizer = recognizer;
183
- this.visited = /* @__PURE__ */ new WeakSet();
253
+ this.ancestors = /* @__PURE__ */ new WeakSet();
254
+ this.remainingNodes = MAX_NODES$1;
184
255
  }
185
256
  redact(value, mediaType) {
186
- this.visited = /* @__PURE__ */ new WeakSet();
257
+ this.ancestors = /* @__PURE__ */ new WeakSet();
258
+ this.remainingNodes = MAX_NODES$1;
187
259
  return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
188
260
  }
189
261
  /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
@@ -195,18 +267,24 @@ var BinaryContentRedactor = class {
195
267
  if (typeof value === "string") return this.redactLeaf(value, ctx);
196
268
  if (typeof value !== "object") return value;
197
269
  if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
198
- if (this.visited.has(value)) return null;
199
- this.visited.add(value);
200
- if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
201
- const obj = value;
202
- const out = {};
203
- for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
204
- value: this.walk(obj[k], new MediaTypeContext(obj, k)),
205
- enumerable: true,
206
- writable: true,
207
- configurable: true
208
- });
209
- return out;
270
+ if (this.ancestors.has(value)) return null;
271
+ if (this.remainingNodes <= 0) return TRUNCATED$1;
272
+ this.remainingNodes--;
273
+ this.ancestors.add(value);
274
+ try {
275
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
276
+ const obj = value;
277
+ const out = {};
278
+ for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
279
+ value: this.walk(obj[k], new MediaTypeContext(obj, k)),
280
+ enumerable: true,
281
+ writable: true,
282
+ configurable: true
283
+ });
284
+ return out;
285
+ } finally {
286
+ this.ancestors.delete(value);
287
+ }
210
288
  }
211
289
  redactString(value, ctx) {
212
290
  const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
@@ -502,7 +580,8 @@ const POSTHOG_PARAMS_MAP = {
502
580
  posthogModelOverride: "modelOverride",
503
581
  posthogProviderOverride: "providerOverride",
504
582
  posthogCostOverride: "costOverride",
505
- posthogCaptureImmediate: "captureImmediate"
583
+ posthogCaptureImmediate: "captureImmediate",
584
+ posthogOptions: "options"
506
585
  };
507
586
  function extractPosthogParams(body) {
508
587
  const providerParams = {};
@@ -556,7 +635,7 @@ function formatOpenAIResponsesInput(input, instructions) {
556
635
  }
557
636
  //#endregion
558
637
  //#region package.json
559
- var version = "8.20.1";
638
+ var version = "8.21.0";
560
639
  //#endregion
561
640
  //#region src/serializeError.ts
562
641
  const DEFAULT_MAX_DEPTH = 3;
@@ -751,6 +830,20 @@ const warnIfPostHogAiGateway = (baseURL) => {
751
830
  //#endregion
752
831
  //#region src/captureAiGeneration.ts
753
832
  /**
833
+ * posthog-node 6 takes an options object; 5.x, which has no `registerOptions`, takes positional
834
+ * arguments. The 6.x positional fallback works too, but logs a deprecation error in the app.
835
+ */
836
+ const captureAiException = (client, error, properties, uuid$1) => {
837
+ if (isPostHogNode6(client)) {
838
+ client.captureException(error, {
839
+ properties,
840
+ uuid: uuid$1
841
+ });
842
+ return;
843
+ }
844
+ client.captureException.call(client, error, void 0, properties, uuid$1);
845
+ };
846
+ /**
754
847
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
755
848
  *
756
849
  * This is the canonical primitive that every `@posthog/ai` wrapper
@@ -786,7 +879,7 @@ const captureAiGeneration$1 = async (client, options) => {
786
879
  if (client.options?.enableExceptionAutocapture) {
787
880
  if (!shouldRedact) {
788
881
  exceptionId = (0, _posthog_core.uuidv7)();
789
- client.captureException(options.error, void 0, { $ai_trace_id: traceId }, exceptionId);
882
+ captureAiException(client, options.error, { $ai_trace_id: traceId }, exceptionId);
790
883
  }
791
884
  if (typeof options.error === "object") options.error.__posthog_previously_captured_error = true;
792
885
  }
@@ -812,6 +905,7 @@ const captureAiGeneration$1 = async (client, options) => {
812
905
  ...usage.webSearchCount ? { $ai_web_search_count: usage.webSearchCount } : {},
813
906
  ...usage.rawUsage ? { $ai_usage: usage.rawUsage } : {}
814
907
  };
908
+ const distinctId = userDistinctId(options.distinctId);
815
909
  const properties = {
816
910
  $ai_lib: "posthog-ai",
817
911
  $ai_lib_version: version,
@@ -831,7 +925,6 @@ const captureAiGeneration$1 = async (client, options) => {
831
925
  ...options.baseURL === null ? {} : { $ai_base_url: options.baseURL ?? "" },
832
926
  ...options.properties,
833
927
  $ai_tokens_source: getTokensSource(options.properties),
834
- ...options.distinctId ? {} : { $process_person_profile: false },
835
928
  ...options.stopReason ? { $ai_stop_reason: options.stopReason } : {},
836
929
  ...options.tools ? { $ai_tools: redactMcpToolCredentials(options.tools) } : {},
837
930
  ...options.completionId ? { $ai_completion_id: options.completionId } : {},
@@ -840,9 +933,9 @@ const captureAiGeneration$1 = async (client, options) => {
840
933
  ...costOverrideData
841
934
  };
842
935
  const event = {
843
- distinctId: options.distinctId ?? traceId,
936
+ distinctId: distinctId ?? traceId,
844
937
  event: eventType,
845
- properties,
938
+ ...eventOptionFields(client, properties, options.options, !distinctId),
846
939
  groups: options.groups
847
940
  };
848
941
  if (options.captureImmediate) await captureAiEventImmediate(client, event);