@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
@@ -5,6 +5,7 @@ let _langchain_core_utils_types = require("@langchain/core/utils/types");
5
5
  let langchain = require("langchain");
6
6
  let uuid = require("uuid");
7
7
  let zod = require("zod");
8
+ let _posthog_core = require("@posthog/core");
8
9
  let _langchain_core_callbacks_base = require("@langchain/core/callbacks/base");
9
10
  //#region src/typeGuards.ts
10
11
  const isObject = (value) => {
@@ -16,9 +17,77 @@ const isObject = (value) => {
16
17
  function isFullAiCaptureEnabled(client) {
17
18
  return client?.enableFullAiCapture === true;
18
19
  }
20
+ /**
21
+ * The caller's distinct id as a string, or undefined when it is missing, empty or only whitespace.
22
+ *
23
+ * @internal
24
+ */
25
+ function userDistinctId(value) {
26
+ if (!value) return;
27
+ const id = String(value);
28
+ return id.trim() === "" ? void 0 : id;
29
+ }
30
+ /**
31
+ * The caller's per-event options when they are an object. A missing or `null` value means no
32
+ * options. An array or any other value is reported and ignored, so it is never spread.
33
+ *
34
+ * @internal
35
+ */
36
+ function readAiEventOptions(value) {
37
+ if (value === void 0 || value === null) return;
38
+ if (typeof value === "object" && !Array.isArray(value)) return value;
39
+ console.warn(`[PostHog AI] options must be an object, got ${Array.isArray(value) ? "array" : typeof value}; ignoring them`);
40
+ }
41
+ /**
42
+ * The `properties` and `options` for an AI event. `personless` (no user distinct id) turns person
43
+ * processing off and beats the caller's `process_person_profile`.
44
+ *
45
+ * posthog-node 6 takes the options as they are, so no option is written as a property.
46
+ * posthog-node 5 has no per-event options: each known option is sent as the property that version
47
+ * reads, and an unknown key is dropped.
48
+ *
49
+ * @internal
50
+ */
51
+ function eventOptionFields(client, properties, options, personless) {
52
+ const userOptions = readAiEventOptions(options);
53
+ if (isPostHogNode6(client)) {
54
+ const eventOptions = {
55
+ ...userOptions,
56
+ ...personless ? { process_person_profile: false } : {}
57
+ };
58
+ return Object.keys(eventOptions).length > 0 ? {
59
+ properties,
60
+ options: eventOptions
61
+ } : { properties };
62
+ }
63
+ const withOptionProperties = {
64
+ ...properties,
65
+ ...(0, _posthog_core.eventOptionsAsLegacyProperties)(userOptions)
66
+ };
67
+ if (personless) withOptionProperties.$process_person_profile = false;
68
+ return { properties: withOptionProperties };
69
+ }
70
+ /**
71
+ * Whether the client is posthog-node 6 or later, the first major with per-event options.
72
+ *
73
+ * @internal
74
+ */
75
+ function isPostHogNode6(client) {
76
+ return (0, _posthog_core.acceptsEventOptions)(client);
77
+ }
78
+ /**
79
+ * posthog-node 6 sends every AI event on its capture v1 AI lane; `enableFullAiCapture` then only
80
+ * controls truncation and media redaction. posthog-node 5 keeps the opt-in, so an `@posthog/ai`
81
+ * upgrade never changes how a 5.x client sends.
82
+ *
83
+ * @internal
84
+ */
85
+ function usesAiLane(client) {
86
+ return isPostHogNode6(client) || isFullAiCaptureEnabled(client);
87
+ }
19
88
  /** @internal */
20
89
  function captureAiEvent(client, event) {
21
- if (isFullAiCaptureEnabled(client) && typeof client.captureAi === "function") {
90
+ if (usesAiLane(client) && typeof client.captureAi === "function") {
22
91
  client.captureAi(event);
23
92
  return;
24
93
  }
@@ -166,13 +235,17 @@ var MediaTypeContext = class MediaTypeContext {
166
235
  //#region src/sanitization/binary_content_redactor.ts
167
236
  const STRONG_CONTEXT_MIN_LENGTH = 64;
168
237
  const WEAK_CONTEXT_MIN_LENGTH = 1024;
238
+ const MAX_NODES = 1e4;
239
+ const TRUNCATED = "[Truncated]";
169
240
  var BinaryContentRedactor = class {
170
241
  constructor(recognizer = new Base64Recognizer()) {
171
242
  this.recognizer = recognizer;
172
- this.visited = /* @__PURE__ */ new WeakSet();
243
+ this.ancestors = /* @__PURE__ */ new WeakSet();
244
+ this.remainingNodes = MAX_NODES;
173
245
  }
174
246
  redact(value, mediaType) {
175
- this.visited = /* @__PURE__ */ new WeakSet();
247
+ this.ancestors = /* @__PURE__ */ new WeakSet();
248
+ this.remainingNodes = MAX_NODES;
176
249
  return this.walk(value, mediaType ? new MediaTypeContext(void 0, void 0, mediaType) : MediaTypeContext.EMPTY);
177
250
  }
178
251
  /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */
@@ -184,18 +257,24 @@ var BinaryContentRedactor = class {
184
257
  if (typeof value === "string") return this.redactLeaf(value, ctx);
185
258
  if (typeof value !== "object") return value;
186
259
  if (typeof Uint8Array !== "undefined" && value instanceof Uint8Array) return this.redactLeaf(value, ctx);
187
- if (this.visited.has(value)) return null;
188
- this.visited.add(value);
189
- if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
190
- const obj = value;
191
- const out = {};
192
- for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
193
- value: this.walk(obj[k], new MediaTypeContext(obj, k)),
194
- enumerable: true,
195
- writable: true,
196
- configurable: true
197
- });
198
- return out;
260
+ if (this.ancestors.has(value)) return null;
261
+ if (this.remainingNodes <= 0) return TRUNCATED;
262
+ this.remainingNodes--;
263
+ this.ancestors.add(value);
264
+ try {
265
+ if (Array.isArray(value)) return value.map((item) => this.walk(item, ctx));
266
+ const obj = value;
267
+ const out = {};
268
+ for (const k of Object.keys(obj)) Object.defineProperty(out, k, {
269
+ value: this.walk(obj[k], new MediaTypeContext(obj, k)),
270
+ enumerable: true,
271
+ writable: true,
272
+ configurable: true
273
+ });
274
+ return out;
275
+ } finally {
276
+ this.ancestors.delete(value);
277
+ }
199
278
  }
200
279
  redactString(value, ctx) {
201
280
  const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
@@ -313,7 +392,7 @@ function redactMcpToolCredentials(tools) {
313
392
  }
314
393
  //#endregion
315
394
  //#region package.json
316
- var version = "8.20.1";
395
+ var version = "8.21.0";
317
396
  //#endregion
318
397
  //#region src/serializeError.ts
319
398
  const DEFAULT_MAX_DEPTH = 3;
@@ -516,9 +595,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
516
595
  this.runs = {};
517
596
  this.parentTree = {};
518
597
  this.client = options.client;
519
- this.distinctId = options.distinctId;
598
+ this.distinctId = userDistinctId(options.distinctId);
520
599
  this.traceId = options.traceId;
521
600
  this.properties = options.properties || {};
601
+ this.eventOptions = options.options;
522
602
  this.privacyMode = options.privacyMode || false;
523
603
  this.groups = options.groups || {};
524
604
  this.debug = options.debug || false;
@@ -731,7 +811,6 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
731
811
  };
732
812
  if (parentRunId) eventProperties["$ai_parent_id"] = parentRunId;
733
813
  Object.assign(eventProperties, this.properties);
734
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
735
814
  if (outputs instanceof Error) {
736
815
  if (isLangGraphControlFlow(outputs)) {
737
816
  const interrupts = outputs.interrupts;
@@ -744,7 +823,7 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
744
823
  this._safeCapture({
745
824
  distinctId: this.distinctId ? this.distinctId.toString() : runId,
746
825
  event: eventName,
747
- properties: eventProperties,
826
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
748
827
  groups: this.groups
749
828
  });
750
829
  }
@@ -820,11 +899,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
820
899
  if (completions) eventProperties["$ai_output_choices"] = withPrivacyMode(this.client, this.privacyMode, completions);
821
900
  }
822
901
  Object.assign(eventProperties, this.properties);
823
- if (!this.distinctId) eventProperties["$process_person_profile"] = false;
824
902
  this._safeCapture({
825
903
  distinctId: this.distinctId ? this.distinctId.toString() : traceId,
826
904
  event: "$ai_generation",
827
- properties: eventProperties,
905
+ ...eventOptionFields(this.client, eventProperties, this.eventOptions, !this.distinctId),
828
906
  groups: this.groups
829
907
  });
830
908
  }