create-supyagent-app 0.1.24 → 0.1.26

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-supyagent-app",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "description": "Create a supyagent-powered chatbot app",
5
5
  "type": "module",
6
6
  "bin": {
@@ -25,6 +25,9 @@ export async function POST(req: Request) {
25
25
 
26
26
  return result.toUIMessageStreamResponse({
27
27
  originalMessages: messages,
28
+ onStepFinish: async ({ messages: updatedMessages }) => {
29
+ await adapter.saveChat(chatId, updatedMessages as any);
30
+ },
28
31
  onFinish: async ({ messages: updatedMessages }) => {
29
32
  await adapter.saveChat(chatId, updatedMessages as any);
30
33
  },
@@ -25,6 +25,9 @@ export async function POST(req: Request) {
25
25
 
26
26
  return result.toUIMessageStreamResponse({
27
27
  originalMessages: messages,
28
+ onStepFinish: async ({ messages: updatedMessages }) => {
29
+ await adapter.saveChat(chatId, updatedMessages as any);
30
+ },
28
31
  onFinish: async ({ messages: updatedMessages }) => {
29
32
  await adapter.saveChat(chatId, updatedMessages as any);
30
33
  },
@@ -13,12 +13,37 @@ interface ChatInputProps {
13
13
  stop: () => void;
14
14
  }
15
15
 
16
- function readFileAsDataURL(file: File): Promise<string> {
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 reader = new FileReader();
19
- reader.onload = () => resolve(reader.result as string);
20
- reader.onerror = reject;
21
- reader.readAsDataURL(file);
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
- type: "file" as const,
67
- url: await readFileAsDataURL(f),
68
- mediaType: f.type,
69
- filename: f.name,
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