create-supyagent-app 0.1.24 → 0.1.25
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/package.json
CHANGED
|
@@ -13,12 +13,37 @@ interface ChatInputProps {
|
|
|
13
13
|
stop: () => void;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
const MAX_BASE64_BYTES = 4.5 * 1024 * 1024; // stay well under provider 5MB limit
|
|
17
|
+
const MAX_DIMENSION = 2048;
|
|
18
|
+
|
|
19
|
+
function compressImage(file: File): Promise<{ url: string; mediaType: string }> {
|
|
17
20
|
return new Promise((resolve, reject) => {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
const img = new Image();
|
|
22
|
+
img.onload = () => {
|
|
23
|
+
let { width, height } = img;
|
|
24
|
+
// Scale down if too large
|
|
25
|
+
if (width > MAX_DIMENSION || height > MAX_DIMENSION) {
|
|
26
|
+
const scale = MAX_DIMENSION / Math.max(width, height);
|
|
27
|
+
width = Math.round(width * scale);
|
|
28
|
+
height = Math.round(height * scale);
|
|
29
|
+
}
|
|
30
|
+
const canvas = document.createElement("canvas");
|
|
31
|
+
canvas.width = width;
|
|
32
|
+
canvas.height = height;
|
|
33
|
+
const ctx = canvas.getContext("2d")!;
|
|
34
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
35
|
+
|
|
36
|
+
// Try JPEG at decreasing quality until under size limit
|
|
37
|
+
let quality = 0.85;
|
|
38
|
+
let dataUrl = canvas.toDataURL("image/jpeg", quality);
|
|
39
|
+
while (dataUrl.length * 0.75 > MAX_BASE64_BYTES && quality > 0.1) {
|
|
40
|
+
quality -= 0.15;
|
|
41
|
+
dataUrl = canvas.toDataURL("image/jpeg", quality);
|
|
42
|
+
}
|
|
43
|
+
resolve({ url: dataUrl, mediaType: "image/jpeg" });
|
|
44
|
+
};
|
|
45
|
+
img.onerror = reject;
|
|
46
|
+
img.src = URL.createObjectURL(file);
|
|
22
47
|
});
|
|
23
48
|
}
|
|
24
49
|
|
|
@@ -62,12 +87,15 @@ export function ChatInput({ sendMessage, isLoading, stop }: ChatInputProps) {
|
|
|
62
87
|
| undefined;
|
|
63
88
|
if (files.length > 0) {
|
|
64
89
|
fileUIParts = await Promise.all(
|
|
65
|
-
files.map(async (f) =>
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
90
|
+
files.map(async (f) => {
|
|
91
|
+
const { url, mediaType } = await compressImage(f);
|
|
92
|
+
return {
|
|
93
|
+
type: "file" as const,
|
|
94
|
+
url,
|
|
95
|
+
mediaType,
|
|
96
|
+
filename: f.name,
|
|
97
|
+
};
|
|
98
|
+
})
|
|
71
99
|
);
|
|
72
100
|
}
|
|
73
101
|
|