@posthog/ai 8.7.1 → 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.
@@ -3,6 +3,33 @@ import { v4 } from 'uuid';
3
3
  import { toJsonSafeValue, uuidv7 } from '@posthog/core';
4
4
  import { Stream } from '@anthropic-ai/sdk/streaming';
5
5
 
6
+ /** @internal */
7
+
8
+ /** @internal */
9
+
10
+ /** @internal */
11
+ function isFullAiCaptureEnabled(client) {
12
+ return client?.enableFullAiCapture === true;
13
+ }
14
+
15
+ /** @internal */
16
+ function captureAiEvent(client, event) {
17
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAi === 'function') {
18
+ client.captureAi(event);
19
+ return;
20
+ }
21
+ client.capture(event);
22
+ }
23
+
24
+ /** @internal */
25
+ async function captureAiEventImmediate(client, event) {
26
+ if (isFullAiCaptureEnabled(client) && typeof client.captureAiImmediate === 'function') {
27
+ await client.captureAiImmediate(event);
28
+ return;
29
+ }
30
+ await client.captureImmediate(event);
31
+ }
32
+
6
33
  const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
7
34
  const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
8
35
  class Base64Recognizer {
@@ -108,7 +135,6 @@ class BinaryContentRedactor {
108
135
  this.recognizer = recognizer;
109
136
  }
110
137
  redact(value, mediaType) {
111
- if (this.isMultimodalEnabled()) return value;
112
138
  this.visited = new WeakSet();
113
139
  return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY);
114
140
  }
@@ -152,14 +178,11 @@ class BinaryContentRedactor {
152
178
  if (mediaType === 'application/octet-stream') return '[base64 file redacted]';
153
179
  return `[base64 ${mediaType} redacted]`;
154
180
  }
155
- isMultimodalEnabled() {
156
- const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
157
- return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
158
- }
159
181
  }
160
182
 
161
183
  const redactor = new BinaryContentRedactor();
162
- const sanitizeAnthropic = data => redactor.redact(data);
184
+ const sanitize = (data, client) => isFullAiCaptureEnabled(client) ? data : redactor.redact(data);
185
+ const sanitizeAnthropic = (data, client) => sanitize(data, client);
163
186
 
164
187
  const TOKEN_PROPERTY_KEYS = new Set(['$ai_input_tokens', '$ai_output_tokens', '$ai_cache_read_input_tokens', '$ai_cache_creation_input_tokens', '$ai_total_tokens', '$ai_reasoning_tokens']);
165
188
 
@@ -307,7 +330,7 @@ function addDefaults(params) {
307
330
  };
308
331
  }
309
332
 
310
- var version = "8.7.1";
333
+ var version = "8.8.0";
311
334
 
312
335
  const DEFAULT_MAX_DEPTH = 3;
313
336
  const MAX_STACK_LINES = 20;
@@ -556,9 +579,9 @@ const captureAiGeneration = async (client, options) => {
556
579
  groups: options.groups
557
580
  };
558
581
  if (options.captureImmediate) {
559
- await client.captureImmediate(event);
582
+ await captureAiEventImmediate(client, event);
560
583
  } else {
561
- client.capture(event);
584
+ captureAiEvent(client, event);
562
585
  }
563
586
  } catch (error) {
564
587
  // Telemetry failures must never affect the instrumented provider call.
@@ -1019,7 +1042,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
1019
1042
  ...posthogParams,
1020
1043
  model: anthropicParams.model,
1021
1044
  provider: 'anthropic',
1022
- input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
1045
+ input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic'), this.phClient),
1023
1046
  output: formattedOutput,
1024
1047
  latency,
1025
1048
  timeToFirstToken,
@@ -1035,7 +1058,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
1035
1058
  ...posthogParams,
1036
1059
  model: anthropicParams.model,
1037
1060
  provider: 'anthropic',
1038
- input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
1061
+ input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams), this.phClient),
1039
1062
  output: [],
1040
1063
  latency: 0,
1041
1064
  baseURL: this.baseURL,
@@ -1071,7 +1094,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
1071
1094
  ...posthogParams,
1072
1095
  model: anthropicParams.model,
1073
1096
  provider: 'anthropic',
1074
- input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
1097
+ input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams), this.phClient),
1075
1098
  output: formatResponseAnthropic(result),
1076
1099
  latency,
1077
1100
  baseURL: this.baseURL,
@@ -1095,7 +1118,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
1095
1118
  ...posthogParams,
1096
1119
  model: anthropicParams.model,
1097
1120
  provider: 'anthropic',
1098
- input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
1121
+ input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams), this.phClient),
1099
1122
  output: [],
1100
1123
  latency: 0,
1101
1124
  baseURL: this.baseURL,