Skip to content

Commit cccf345

Browse files
committed
update
1 parent 4807888 commit cccf345

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { getClient } from '../../currentScopes';
2+
3+
const MEDIA_MIME_TYPES = new Set([
4+
'image/jpeg',
5+
'image/jpg',
6+
'image/png',
7+
'image/gif',
8+
'image/webp',
9+
'image/svg+xml',
10+
'image/bmp',
11+
'image/tiff',
12+
'video/mp4',
13+
'video/avi',
14+
'video/mov',
15+
'video/wmv',
16+
'video/flv',
17+
'video/webm',
18+
'video/mkv',
19+
'audio/mp3',
20+
'audio/wav',
21+
'audio/ogg',
22+
'audio/mpeg',
23+
'audio/aac',
24+
'audio/flac',
25+
'application/pdf',
26+
'application/zip',
27+
'application/x-zip-compressed',
28+
]);
29+
30+
function isMediaContent(item: unknown): boolean {
31+
if (typeof item !== 'object' || item === null) {
32+
return false;
33+
}
34+
35+
const obj = item as Record<string, unknown>;
36+
37+
if (typeof obj.type === 'string' && (obj.type === 'image' || obj.type === 'image_url')) {
38+
return true;
39+
}
40+
41+
if (typeof obj.mime_type === 'string' && MEDIA_MIME_TYPES.has(obj.mime_type.toLowerCase())) {
42+
return true;
43+
}
44+
45+
if (typeof obj.mimeType === 'string' && MEDIA_MIME_TYPES.has(obj.mimeType.toLowerCase())) {
46+
return true;
47+
}
48+
49+
if (typeof obj.data === 'string' && obj.data.length > 1000) {
50+
const dataStart = obj.data.substring(0, 50).toLowerCase();
51+
if (dataStart.includes('data:image/') || dataStart.includes('/9j/') || dataStart.includes('ivborw0kggo')) {
52+
return true;
53+
}
54+
}
55+
56+
if (typeof obj.source === 'object' && obj.source !== null) {
57+
const source = obj.source as Record<string, unknown>;
58+
if (typeof source.type === 'string' && source.type === 'base64' && typeof source.data === 'string') {
59+
return true;
60+
}
61+
}
62+
63+
return false;
64+
}
65+
66+
function recordDroppedMedia(count: number = 1): void {
67+
const client = getClient();
68+
if (client) {
69+
client.recordDroppedEvent('media_content_dropped' as any, 'attachment', count);
70+
}
71+
}
72+
73+
export function filterMediaFromMessages(messages: unknown): unknown {
74+
if (!Array.isArray(messages)) {
75+
return messages;
76+
}
77+
78+
let droppedCount = 0;
79+
80+
const filtered = messages.map(message => {
81+
if (typeof message !== 'object' || message === null) {
82+
return message;
83+
}
84+
85+
const msg = message as Record<string, unknown>;
86+
87+
if (Array.isArray(msg.content)) {
88+
const filteredContent = msg.content.filter(item => {
89+
if (isMediaContent(item)) {
90+
droppedCount++;
91+
return false;
92+
}
93+
return true;
94+
});
95+
96+
if (filteredContent.length === 0) {
97+
return { ...msg, content: '' };
98+
}
99+
100+
return { ...msg, content: filteredContent };
101+
}
102+
103+
if (isMediaContent(msg.content)) {
104+
droppedCount++;
105+
return { ...msg, content: '' };
106+
}
107+
108+
return message;
109+
});
110+
111+
if (droppedCount > 0) {
112+
recordDroppedMedia(droppedCount);
113+
}
114+
115+
return filtered;
116+
}
117+

0 commit comments

Comments
 (0)