@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.
@@ -1,143 +1,152 @@
1
1
  import * as uuid from 'uuid';
2
2
 
3
- // Type guards for safer type checking
4
-
5
- const isString = value => {
6
- return typeof value === 'string';
7
- };
8
- const isObject = value => {
9
- return value !== null && typeof value === 'object' && !Array.isArray(value);
10
- };
11
-
12
- const REDACTED_IMAGE_PLACEHOLDER = '[base64 image redacted]';
13
-
14
- // ============================================
15
- // Multimodal Feature Toggle
16
- // ============================================
17
-
18
- const isMultimodalEnabled = () => {
19
- const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
20
- return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
21
- };
22
-
23
- // ============================================
24
- // Base64 Detection Helpers
25
- // ============================================
3
+ const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
4
+ const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
5
+ class Base64Recognizer {
6
+ recognize(value, minLength) {
7
+ const dataUrl = DATA_URL_PREFIX_RE.exec(value);
8
+ if (dataUrl) return {
9
+ kind: 'data-url',
10
+ mediaType: dataUrl[1]
11
+ };
12
+ if (value.length < minLength) return {
13
+ kind: 'none'
14
+ };
15
+ const confidencePrefix = value.slice(0, minLength);
16
+ if (BASE64_ALPHABET_RE.test(confidencePrefix)) {
17
+ return {
18
+ kind: 'raw'
19
+ };
20
+ } else {
21
+ return {
22
+ kind: 'none'
23
+ };
24
+ }
25
+ }
26
+ }
26
27
 
27
- const isBase64DataUrl = str => {
28
- return /^data:([^;]+);base64,/.test(str);
29
- };
30
- const isValidUrl = str => {
31
- try {
32
- new URL(str);
33
- return true;
34
- } catch {
35
- // Not an absolute URL, check if it's a relative URL or path
36
- return str.startsWith('/') || str.startsWith('./') || str.startsWith('../');
28
+ const MIME_HINT_KEYS = ['mediaType', 'media_type', 'mimeType', 'mime_type'];
29
+ 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']);
30
+ const STRONG_CONTEXT_TYPES = new Set(['image', 'image_url', 'input_image', 'audio', 'input_audio', 'video', 'video_url', 'file', 'input_file', 'document', 'media', 'file-data']);
31
+ const FILE_FAMILY_TYPES = new Set(['file', 'input_file', 'document', 'media', 'file-data']);
32
+ const KNOWN_AUDIO_FORMATS = new Set(['wav', 'mp3', 'ogg', 'flac', 'm4a', 'aac', 'webm']);
33
+ class MediaTypeContext {
34
+ static EMPTY = new MediaTypeContext(undefined, undefined);
35
+ constructor(parent, key) {
36
+ this.parent = parent;
37
+ this.key = key;
37
38
  }
38
- };
39
- const isRawBase64 = str => {
40
- // Skip if it's a valid URL or path
41
- if (isValidUrl(str)) {
42
- return false;
39
+ inferMediaType() {
40
+ return this.inferFromSiblingMime() ?? this.inferFromSiblingFormat() ?? this.inferFromParentType() ?? this.inferFromKey();
43
41
  }
44
-
45
- // Check if it's a valid base64 string
46
- // Base64 images are typically at least a few hundred chars, but we'll be conservative
47
- return str.length > 20 && /^[A-Za-z0-9+/]+=*$/.test(str);
48
- };
49
- function redactBase64DataUrl(str) {
50
- if (isMultimodalEnabled()) return str;
51
- if (!isString(str)) return str;
52
-
53
- // Check for data URL format
54
- if (isBase64DataUrl(str)) {
55
- return REDACTED_IMAGE_PLACEHOLDER;
42
+ inferFromSiblingMime() {
43
+ if (!this.parent) return undefined;
44
+ for (const hint of MIME_HINT_KEYS) {
45
+ const v = this.parent[hint];
46
+ if (typeof v === 'string') return v;
47
+ }
48
+ return undefined;
56
49
  }
57
-
58
- // Check for raw base64 (Vercel sends raw base64 for inline images)
59
- if (isRawBase64(str)) {
60
- return REDACTED_IMAGE_PLACEHOLDER;
50
+ inferFromSiblingFormat() {
51
+ if (!this.parent) return undefined;
52
+ const fmt = this.parent.format;
53
+ if (typeof fmt === 'string' && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) {
54
+ return `audio/${fmt.toLowerCase()}`;
55
+ }
56
+ return undefined;
57
+ }
58
+ inferFromParentType() {
59
+ if (!this.parent) return undefined;
60
+ const t = this.parent.type;
61
+ if (typeof t !== 'string') return undefined;
62
+ if (t === 'image' || t === 'image_url' || t === 'input_image') return 'image';
63
+ if (t === 'audio' || t === 'input_audio') return 'audio';
64
+ if (t === 'video' || t === 'video_url') return 'video';
65
+ if (FILE_FAMILY_TYPES.has(t)) return 'application/octet-stream';
66
+ return undefined;
67
+ }
68
+ inferFromKey() {
69
+ if (!this.key) return undefined;
70
+ const key = this.key.toLowerCase();
71
+ if (key.includes('audio')) return 'audio';
72
+ if (key.includes('video')) return 'video';
73
+ if (key.includes('image')) return 'image';
74
+ if (key.includes('file') || key.includes('document')) return 'application/octet-stream';
75
+ return undefined;
76
+ }
77
+ signalsBinary() {
78
+ if (this.parent) {
79
+ for (const hint of MIME_HINT_KEYS) {
80
+ if (typeof this.parent[hint] === 'string') return true;
81
+ }
82
+ const fmt = this.parent.format;
83
+ if (typeof fmt === 'string' && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) return true;
84
+ const t = this.parent.type;
85
+ if (typeof t === 'string' && STRONG_CONTEXT_TYPES.has(t)) return true;
86
+ }
87
+ if (this.key && STRONG_CONTEXT_KEYS.has(this.key)) return true;
88
+ return false;
61
89
  }
62
- return str;
63
90
  }
64
91
 
65
- // ============================================
66
- // Common Message Processing
67
- // ============================================
68
-
69
- const processMessages = (messages, transformContent) => {
70
- if (!messages) return messages;
71
- const processContent = content => {
72
- if (typeof content === 'string') return content;
73
- if (!content) return content;
74
- if (Array.isArray(content)) {
75
- return content.map(transformContent);
92
+ const STRONG_CONTEXT_MIN_LENGTH = 64;
93
+ const WEAK_CONTEXT_MIN_LENGTH = 1024;
94
+ class BinaryContentRedactor {
95
+ visited = new WeakSet();
96
+ constructor(recognizer = new Base64Recognizer()) {
97
+ this.recognizer = recognizer;
98
+ }
99
+ redact(value) {
100
+ if (this.isMultimodalEnabled()) return value;
101
+ this.visited = new WeakSet();
102
+ return this.walk(value, MediaTypeContext.EMPTY);
103
+ }
104
+ walk(value, ctx) {
105
+ if (value === null || value === undefined) return value;
106
+ if (typeof value === 'string') return this.redactString(value, ctx);
107
+ if (typeof value !== 'object') return value;
108
+
109
+ // Buffer extends Uint8Array, so this branch catches both.
110
+ if (typeof Uint8Array !== 'undefined' && value instanceof Uint8Array) {
111
+ return this.placeholderFor(ctx.inferMediaType());
76
112
  }
77
-
78
- // Handle single object content
79
- return transformContent(content);
80
- };
81
- const processMessage = msg => {
82
- if (!isObject(msg) || !('content' in msg)) return msg;
83
- return {
84
- ...msg,
85
- content: processContent(msg.content)
86
- };
87
- };
88
-
89
- // Handle both arrays and single messages
90
- if (Array.isArray(messages)) {
91
- return messages.map(processMessage);
113
+ if (this.visited.has(value)) return null;
114
+ this.visited.add(value);
115
+ if (Array.isArray(value)) {
116
+ return value.map(item => this.walk(item, ctx));
117
+ }
118
+ const obj = value;
119
+ const out = {};
120
+ for (const k of Object.keys(obj)) {
121
+ out[k] = this.walk(obj[k], new MediaTypeContext(obj, k));
122
+ }
123
+ return out;
92
124
  }
93
- return processMessage(messages);
94
- };
95
- const sanitizeLangChainImage = item => {
96
- if (!isObject(item)) return item;
97
-
98
- // OpenAI style
99
- if (item.type === 'image_url' && 'image_url' in item && isObject(item.image_url) && 'url' in item.image_url) {
100
- return {
101
- ...item,
102
- image_url: {
103
- ...item.image_url,
104
- url: redactBase64DataUrl(item.image_url.url)
105
- }
106
- };
125
+ redactString(value, ctx) {
126
+ const minLength = ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
127
+ const recognition = this.recognizer.recognize(value, minLength);
128
+ switch (recognition.kind) {
129
+ case 'data-url':
130
+ return this.placeholderFor(recognition.mediaType);
131
+ case 'raw':
132
+ return this.placeholderFor(ctx.inferMediaType());
133
+ case 'none':
134
+ return value;
135
+ }
107
136
  }
108
-
109
- // Direct image with data field
110
- if (item.type === 'image' && 'data' in item) {
111
- return {
112
- ...item,
113
- data: redactBase64DataUrl(item.data)
114
- };
137
+ placeholderFor(mediaType) {
138
+ if (!mediaType) return '[base64 redacted]';
139
+ if (mediaType === 'application/octet-stream') return '[base64 file redacted]';
140
+ return `[base64 ${mediaType} redacted]`;
115
141
  }
116
-
117
- // Anthropic style
118
- if (item.type === 'image' && 'source' in item && isObject(item.source) && 'data' in item.source) {
119
- if (isMultimodalEnabled()) return item;
120
- return {
121
- ...item,
122
- source: {
123
- ...item.source,
124
- data: redactBase64DataUrl(item.source.data)
125
- }
126
- };
142
+ isMultimodalEnabled() {
143
+ const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
144
+ return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
127
145
  }
146
+ }
128
147
 
129
- // Google style
130
- if (item.type === 'media' && 'data' in item) {
131
- return {
132
- ...item,
133
- data: redactBase64DataUrl(item.data)
134
- };
135
- }
136
- return item;
137
- };
138
- const sanitizeLangChain = data => {
139
- return processMessages(data, sanitizeLangChainImage);
140
- };
148
+ const redactor = new BinaryContentRedactor();
149
+ const sanitizeLangChain = data => redactor.redact(data);
141
150
 
142
151
  const STRING_FORMAT = 'utf8';
143
152
 
@@ -653,7 +662,7 @@ var BaseCallbackHandler = class extends BaseCallbackHandlerMethodsClass {
653
662
  }
654
663
  };
655
664
 
656
- var version = "7.17.4";
665
+ var version = "7.18.0";
657
666
 
658
667
  /** A run may either be a Span or a Generation */
659
668