@posthog/ai 7.17.4 → 7.18.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.
@@ -11,100 +11,157 @@ var core = require('@posthog/core');
11
11
  const isString = value => {
12
12
  return typeof value === 'string';
13
13
  };
14
- const isObject = value => {
15
- return value !== null && typeof value === 'object' && !Array.isArray(value);
16
- };
17
-
18
- const REDACTED_IMAGE_PLACEHOLDER = '[base64 image redacted]';
19
-
20
- // ============================================
21
- // Multimodal Feature Toggle
22
- // ============================================
23
-
24
- const isMultimodalEnabled = () => {
25
- const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
26
- return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
27
- };
28
14
 
29
- // ============================================
30
- // Base64 Detection Helpers
31
- // ============================================
15
+ const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
16
+ const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
17
+ class Base64Recognizer {
18
+ recognize(value, minLength) {
19
+ const dataUrl = DATA_URL_PREFIX_RE.exec(value);
20
+ if (dataUrl) return {
21
+ kind: 'data-url',
22
+ mediaType: dataUrl[1]
23
+ };
24
+ if (value.length < minLength) return {
25
+ kind: 'none'
26
+ };
27
+ const confidencePrefix = value.slice(0, minLength);
28
+ if (BASE64_ALPHABET_RE.test(confidencePrefix)) {
29
+ return {
30
+ kind: 'raw'
31
+ };
32
+ } else {
33
+ return {
34
+ kind: 'none'
35
+ };
36
+ }
37
+ }
38
+ }
32
39
 
33
- const isBase64DataUrl = str => {
34
- return /^data:([^;]+);base64,/.test(str);
35
- };
36
- const isValidUrl = str => {
37
- try {
38
- new URL(str);
39
- return true;
40
- } catch {
41
- // Not an absolute URL, check if it's a relative URL or path
42
- return str.startsWith('/') || str.startsWith('./') || str.startsWith('../');
40
+ const MIME_HINT_KEYS = ['mediaType', 'media_type', 'mimeType', 'mime_type'];
41
+ const STRONG_CONTEXT_KEYS = new Set(['data', 'file_data', 'fileData', 'image_url', 'imageUrl', 'video_url', 'videoUrl', 'audio', 'audio_data', 'audioData', 'inline_data', 'inlineData', 'source', 'result']);
42
+ const STRONG_CONTEXT_TYPES = new Set(['image', 'image_url', 'input_image', 'audio', 'input_audio', 'video', 'video_url', 'file', 'input_file', 'document', 'media', 'file-data']);
43
+ const FILE_FAMILY_TYPES = new Set(['file', 'input_file', 'document', 'media', 'file-data']);
44
+ const KNOWN_AUDIO_FORMATS = new Set(['wav', 'mp3', 'ogg', 'flac', 'm4a', 'aac', 'webm']);
45
+ class MediaTypeContext {
46
+ static EMPTY = new MediaTypeContext(undefined, undefined);
47
+ constructor(parent, key) {
48
+ this.parent = parent;
49
+ this.key = key;
50
+ }
51
+ inferMediaType() {
52
+ return this.inferFromSiblingMime() ?? this.inferFromSiblingFormat() ?? this.inferFromParentType() ?? this.inferFromKey();
53
+ }
54
+ inferFromSiblingMime() {
55
+ if (!this.parent) return undefined;
56
+ for (const hint of MIME_HINT_KEYS) {
57
+ const v = this.parent[hint];
58
+ if (typeof v === 'string') return v;
59
+ }
60
+ return undefined;
43
61
  }
44
- };
45
- const isRawBase64 = str => {
46
- // Skip if it's a valid URL or path
47
- if (isValidUrl(str)) {
62
+ inferFromSiblingFormat() {
63
+ if (!this.parent) return undefined;
64
+ const fmt = this.parent.format;
65
+ if (typeof fmt === 'string' && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) {
66
+ return `audio/${fmt.toLowerCase()}`;
67
+ }
68
+ return undefined;
69
+ }
70
+ inferFromParentType() {
71
+ if (!this.parent) return undefined;
72
+ const t = this.parent.type;
73
+ if (typeof t !== 'string') return undefined;
74
+ if (t === 'image' || t === 'image_url' || t === 'input_image') return 'image';
75
+ if (t === 'audio' || t === 'input_audio') return 'audio';
76
+ if (t === 'video' || t === 'video_url') return 'video';
77
+ if (FILE_FAMILY_TYPES.has(t)) return 'application/octet-stream';
78
+ return undefined;
79
+ }
80
+ inferFromKey() {
81
+ if (!this.key) return undefined;
82
+ const key = this.key.toLowerCase();
83
+ if (key.includes('audio')) return 'audio';
84
+ if (key.includes('video')) return 'video';
85
+ if (key.includes('image')) return 'image';
86
+ if (key.includes('file') || key.includes('document')) return 'application/octet-stream';
87
+ return undefined;
88
+ }
89
+ signalsBinary() {
90
+ if (this.parent) {
91
+ for (const hint of MIME_HINT_KEYS) {
92
+ if (typeof this.parent[hint] === 'string') return true;
93
+ }
94
+ const fmt = this.parent.format;
95
+ if (typeof fmt === 'string' && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) return true;
96
+ const t = this.parent.type;
97
+ if (typeof t === 'string' && STRONG_CONTEXT_TYPES.has(t)) return true;
98
+ }
99
+ if (this.key && STRONG_CONTEXT_KEYS.has(this.key)) return true;
48
100
  return false;
49
101
  }
102
+ }
50
103
 
51
- // Check if it's a valid base64 string
52
- // Base64 images are typically at least a few hundred chars, but we'll be conservative
53
- return str.length > 20 && /^[A-Za-z0-9+/]+=*$/.test(str);
54
- };
55
- function redactBase64DataUrl(str) {
56
- if (isMultimodalEnabled()) return str;
57
- if (!isString(str)) return str;
58
-
59
- // Check for data URL format
60
- if (isBase64DataUrl(str)) {
61
- return REDACTED_IMAGE_PLACEHOLDER;
104
+ const STRONG_CONTEXT_MIN_LENGTH = 64;
105
+ const WEAK_CONTEXT_MIN_LENGTH = 1024;
106
+ class BinaryContentRedactor {
107
+ visited = new WeakSet();
108
+ constructor(recognizer = new Base64Recognizer()) {
109
+ this.recognizer = recognizer;
110
+ }
111
+ redact(value) {
112
+ if (this.isMultimodalEnabled()) return value;
113
+ this.visited = new WeakSet();
114
+ return this.walk(value, MediaTypeContext.EMPTY);
115
+ }
116
+ walk(value, ctx) {
117
+ if (value === null || value === undefined) return value;
118
+ if (typeof value === 'string') return this.redactString(value, ctx);
119
+ if (typeof value !== 'object') return value;
120
+
121
+ // Buffer extends Uint8Array, so this branch catches both.
122
+ if (typeof Uint8Array !== 'undefined' && value instanceof Uint8Array) {
123
+ return this.placeholderFor(ctx.inferMediaType());
124
+ }
125
+ if (this.visited.has(value)) return null;
126
+ this.visited.add(value);
127
+ if (Array.isArray(value)) {
128
+ return value.map(item => this.walk(item, ctx));
129
+ }
130
+ const obj = value;
131
+ const out = {};
132
+ for (const k of Object.keys(obj)) {
133
+ out[k] = this.walk(obj[k], new MediaTypeContext(obj, k));
134
+ }
135
+ return out;
136
+ }
137
+ redactString(value, ctx) {
138
+ const minLength = ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
139
+ const recognition = this.recognizer.recognize(value, minLength);
140
+ switch (recognition.kind) {
141
+ case 'data-url':
142
+ return this.placeholderFor(recognition.mediaType);
143
+ case 'raw':
144
+ return this.placeholderFor(ctx.inferMediaType());
145
+ case 'none':
146
+ return value;
147
+ }
62
148
  }
63
-
64
- // Check for raw base64 (Vercel sends raw base64 for inline images)
65
- if (isRawBase64(str)) {
66
- return REDACTED_IMAGE_PLACEHOLDER;
149
+ placeholderFor(mediaType) {
150
+ if (!mediaType) return '[base64 redacted]';
151
+ if (mediaType === 'application/octet-stream') return '[base64 file redacted]';
152
+ return `[base64 ${mediaType} redacted]`;
67
153
  }
68
- return str;
69
- }
70
- const sanitizeGeminiPart = part => {
71
- if (isMultimodalEnabled()) return part;
72
- if (!isObject(part)) return part;
73
-
74
- // Handle Gemini's inline data format (images, audio, PDFs all use inlineData)
75
- if ('inlineData' in part && isObject(part.inlineData) && 'data' in part.inlineData) {
76
- return {
77
- ...part,
78
- inlineData: {
79
- ...part.inlineData,
80
- data: REDACTED_IMAGE_PLACEHOLDER
81
- }
82
- };
154
+ isMultimodalEnabled() {
155
+ const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
156
+ return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
83
157
  }
84
- return part;
85
- };
86
- const processGeminiItem = item => {
87
- if (!isObject(item)) return item;
158
+ }
88
159
 
89
- // If it has parts, process them
90
- if ('parts' in item && item.parts) {
91
- const parts = Array.isArray(item.parts) ? item.parts.map(sanitizeGeminiPart) : sanitizeGeminiPart(item.parts);
92
- return {
93
- ...item,
94
- parts
95
- };
96
- }
97
- return item;
98
- };
99
- const sanitizeGemini = data => {
100
- // Gemini has a different structure with 'parts' directly on items instead of 'content'
101
- // So we need custom processing instead of using processMessages
102
- if (!data) return data;
103
- if (Array.isArray(data)) {
104
- return data.map(processGeminiItem);
105
- }
106
- return processGeminiItem(data);
107
- };
160
+ const redactor = new BinaryContentRedactor();
161
+ function redactBase64DataUrl(str) {
162
+ return redactor.redact(str);
163
+ }
164
+ const sanitizeGemini = data => redactor.redact(data);
108
165
 
109
166
  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']);
110
167
  function getTokensSource(posthogProperties) {
@@ -327,7 +384,7 @@ function addDefaults(params) {
327
384
  };
328
385
  }
329
386
 
330
- var version = "7.17.4";
387
+ var version = "7.18.0";
331
388
 
332
389
  /**
333
390
  * Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape