From 585003640f8001f3bcc1e5e2b0257ebbf6ddd491 Mon Sep 17 00:00:00 2001 From: darkskygit Date: Thu, 18 Jul 2024 10:37:27 +0000 Subject: [PATCH] feat: improve workflow performance (#7539) --- .../block-suite-editor/ai/request.ts | 52 +++++++++++++++---- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/request.ts b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/request.ts index 6bc37b8363..93844c9a76 100644 --- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/request.ts +++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/request.ts @@ -44,6 +44,38 @@ export function createChatSession({ }); } +async function resizeImage(blob: Blob | File): Promise { + let src = ''; + try { + src = URL.createObjectURL(blob); + const img = new Image(); + img.src = src; + await new Promise(resolve => { + img.onload = resolve; + }); + + const canvas = document.createElement('canvas'); + // keep aspect ratio + const scale = Math.min(1024 / img.width, 1024 / img.height); + canvas.width = Math.floor(img.width * scale); + canvas.height = Math.floor(img.height * scale); + + const ctx = canvas.getContext('2d'); + if (ctx) { + ctx.imageSmoothingQuality = 'high'; + ctx.drawImage(img, 0, 0, canvas.width, canvas.height); + return new Promise(resolve => + canvas.toBlob(blob => resolve(blob), 'image/jpeg', 0.8) + ); + } + } catch (e) { + console.error(e); + } finally { + if (src) URL.revokeObjectURL(src); + } + return null; +} + async function createSessionMessage({ docId, workspaceId, @@ -77,17 +109,17 @@ async function createSessionMessage({ attachment => typeof attachment === 'string' ) as [string[], (Blob | File)[]]; options.attachments = stringAttachments; - options.blobs = await Promise.all( - blobs.map(async blob => { - if (blob instanceof File) { - return blob; - } else { - return new File([blob], sessionId, { - type: blob.type, + options.blobs = ( + await Promise.all( + blobs.map(resizeImage).map(async blob => { + const file = await blob; + if (!file) return null; + return new File([file], sessionId, { + type: file.type, }); - } - }) - ); + }) + ) + ).filter(Boolean) as File[]; } if (retry) return {