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