@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.
- package/dist/anthropic/index.cjs +142 -61
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +142 -61
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +142 -85
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +142 -85
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +142 -210
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +142 -210
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +135 -126
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +135 -126
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +136 -134
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +136 -134
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +65 -1
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.mjs +65 -1
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +141 -44
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +141 -44
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/langchain/index.mjs
CHANGED
|
@@ -1,143 +1,152 @@
|
|
|
1
1
|
import * as uuid from 'uuid';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
110
|
-
|
|
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
|
-
|
|
118
|
-
|
|
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
|
-
|
|
130
|
-
|
|
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.
|
|
665
|
+
var version = "7.18.0";
|
|
657
666
|
|
|
658
667
|
/** A run may either be a Span or a Generation */
|
|
659
668
|
|