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