@posthog/ai 8.7.0 → 8.8.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.
@@ -7,6 +7,24 @@ const isObject = value => {
7
7
  return value !== null && typeof value === 'object' && !Array.isArray(value);
8
8
  };
9
9
 
10
+ /** @internal */
11
+
12
+ /** @internal */
13
+
14
+ /** @internal */
15
+ function isFullAiCaptureEnabled(client) {
16
+ return client?.enableFullAiCapture === true;
17
+ }
18
+
19
+ /** @internal */
20
+ function captureAiEvent(client, event) {
21
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAi === 'function') {
22
+ client.captureAi(event);
23
+ return;
24
+ }
25
+ client.capture(event);
26
+ }
27
+
10
28
  const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
11
29
  const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
12
30
  class Base64Recognizer {
@@ -112,7 +130,6 @@ class BinaryContentRedactor {
112
130
  this.recognizer = recognizer;
113
131
  }
114
132
  redact(value, mediaType) {
115
- if (this.isMultimodalEnabled()) return value;
116
133
  this.visited = new WeakSet();
117
134
  return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY);
118
135
  }
@@ -156,14 +173,11 @@ class BinaryContentRedactor {
156
173
  if (mediaType === 'application/octet-stream') return '[base64 file redacted]';
157
174
  return `[base64 ${mediaType} redacted]`;
158
175
  }
159
- isMultimodalEnabled() {
160
- const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
161
- return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
162
- }
163
176
  }
164
177
 
165
178
  const redactor = new BinaryContentRedactor();
166
- const sanitizeLangChain = data => redactor.redact(data);
179
+ const sanitize = (data, client) => isFullAiCaptureEnabled(client) ? data : redactor.redact(data);
180
+ const sanitizeLangChain = (data, client) => sanitize(data, client);
167
181
 
168
182
  const STRING_FORMAT = 'utf8';
169
183
 
@@ -231,7 +245,7 @@ function sanitizeValues(obj) {
231
245
  return jsonSafe;
232
246
  }
233
247
 
234
- var version = "8.7.0";
248
+ var version = "8.8.0";
235
249
 
236
250
  const DEFAULT_MAX_DEPTH = 3;
237
251
  const MAX_STACK_LINES = 20;
@@ -502,7 +516,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
502
516
  }) || 'generation';
503
517
  const generation = {
504
518
  name: runNameFound,
505
- input: sanitizeLangChain(messages),
519
+ input: sanitizeLangChain(messages, this.client),
506
520
  startTime: Date.now()
507
521
  };
508
522
  if (extraParams) {
@@ -547,7 +561,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
547
561
  }
548
562
  _safeCapture(message) {
549
563
  try {
550
- this.client.capture(message);
564
+ captureAiEvent(this.client, message);
551
565
  } catch {
552
566
  // Telemetry delivery must never affect the LangChain callback lifecycle.
553
567
  }
@@ -573,7 +587,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
573
587
  $ai_lib: 'posthog-ai',
574
588
  $ai_lib_version: version,
575
589
  $ai_trace_id: traceId,
576
- $ai_input_state: withPrivacyMode(this.client, this.privacyMode, sanitizeLangChain(run.input)),
590
+ $ai_input_state: withPrivacyMode(this.client, this.privacyMode, sanitizeLangChain(run.input, this.client)),
577
591
  $ai_latency: latency,
578
592
  $ai_span_name: run.name,
579
593
  $ai_span_id: runId,
@@ -595,14 +609,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
595
609
  if (interrupts !== undefined) {
596
610
  eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, sanitizeLangChain({
597
611
  __interrupt__: interrupts
598
- }));
612
+ }, this.client));
599
613
  }
600
614
  } else {
601
615
  eventProperties['$ai_error'] = stringifyError(outputs);
602
616
  eventProperties['$ai_is_error'] = true;
603
617
  }
604
618
  } else if (outputs !== undefined) {
605
- eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, sanitizeLangChain(outputs));
619
+ eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, sanitizeLangChain(outputs, this.client));
606
620
  }
607
621
  this._safeCapture({
608
622
  distinctId: this.distinctId ? this.distinctId.toString() : runId,
@@ -818,7 +832,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
818
832
  }
819
833
 
820
834
  // Sanitize the message content to redact base64 images
821
- return sanitizeLangChain(messageDict);
835
+ return sanitizeLangChain(messageDict, this.client);
822
836
  }
823
837
  _extractStopReason(output) {
824
838
  if (!output.generations || !Array.isArray(output.generations)) {