The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): bound multi-image thumbnail raster cost
Jakub Wolniewiczd345f0831f06
diff --git a/apps/desktop/src/app/chat/composer/attachments.test.tsx b/apps/desktop/src/app/chat/composer/attachments.test.tsxindex b405b011d62..b635ab9ca9b 100644--- a/apps/desktop/src/app/chat/composer/attachments.test.tsx+++ b/apps/desktop/src/app/chat/composer/attachments.test.tsx@@ -8,6 +8,7 @@ import { $previewTabs } from '@/store/preview'import { AttachmentList } from './attachments'const DATA_URL = 'data:image/png;base64,iVBORw0KGgoAAAANS'+const THUMBNAIL_URL = 'data:image/png;base64,dGh1bWJuYWls'function makeAttachment(id: string, label = 'test.pdf'): ComposerAttachment {return { id, kind: 'file', label }@@ -70,7 +71,7 @@ describe('AttachmentList', () => {expect(screen.getByText('valid.txt')).toBeDefined()})- it('opens an attached image in the lightbox, not the preview rail', async () => {+ it('renders the thumbnail in the pill but opens the full-resolution image in the lightbox', async () => {$previewTabs.set([])const image: ComposerAttachment = {@@ -78,11 +79,14 @@ describe('AttachmentList', () => {kind: 'image',label: 'shot.png',path: '/tmp/shot.png',- previewUrl: DATA_URL+ previewUrl: DATA_URL,+ thumbnailUrl: THUMBNAIL_URL}await renderWithI18n(<AttachmentList attachments={[image]} />)+ expect(screen.getByAltText<HTMLImageElement>('shot.png').getAttribute('src')).toBe(THUMBNAIL_URL)+await act(async () => {fireEvent.click(screen.getByRole('button', { name: /shot\.png/ }))})diff --git a/apps/desktop/src/app/chat/hooks/use-composer-actions.test.ts b/apps/desktop/src/app/chat/hooks/use-composer-actions.test.tsindex d2ce0503405..7436a296f87 100644--- a/apps/desktop/src/app/chat/hooks/use-composer-actions.test.ts+++ b/apps/desktop/src/app/chat/hooks/use-composer-actions.test.ts@@ -324,7 +324,8 @@ describe('attachImagePath thumbnail separation', () => {// Full-resolution data URL the local bridge returns for a pasted screenshot.// Content is irrelevant — the mock bitmap below reports 4000×3000 so the// real downscale path runs (the data URL itself is never decoded in jsdom).- const FULL_RES = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+GkZcAAAAASUVORK5CYII='+ const FULL_RES =+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+GkZcAAAAASUVORK5CYII='afterEach(() => {vi.unstubAllGlobals()@@ -339,7 +340,7 @@ describe('attachImagePath thumbnail separation', () => {readFileDataUrl}- // Exercise the real downscale path: 4000×3000 bitmap → 2048×1536 canvas.+ // Exercise the real downscale path: 4000×3000 bitmap → 512×384 canvas.const drawImage = vi.fn()const close = vi.fn()@@ -349,7 +350,10 @@ describe('attachImagePath thumbnail separation', () => {blob: async () => new Blob([new Uint8Array([0])], { type: 'image/png' })})))- vi.stubGlobal('createImageBitmap', vi.fn(async () => ({ width: 4000, height: 3000, close })))+ vi.stubGlobal(+ 'createImageBitmap',+ vi.fn(async () => ({ width: 4000, height: 3000, close }))+ )class MockOffscreenCanvas {getContext = () => ({ drawImage })@@ -377,7 +381,7 @@ describe('attachImagePath thumbnail separation', () => {// original (which would re-introduce the main-thread decode freeze).expect(attachment?.thumbnailUrl).toBeDefined()expect(attachment?.thumbnailUrl).not.toBe(FULL_RES)- expect(drawImage).toHaveBeenCalledWith(expect.anything(), 0, 0, 2048, 1536)+ expect(drawImage).toHaveBeenCalledWith(expect.anything(), 0, 0, 512, 384)expect(close).toHaveBeenCalled()})diff --git a/apps/desktop/src/lib/image-resize.test.ts b/apps/desktop/src/lib/image-resize.test.tsindex 5d44a13c712..af9d9d8e72e 100644--- a/apps/desktop/src/lib/image-resize.test.ts+++ b/apps/desktop/src/lib/image-resize.test.ts@@ -3,8 +3,8 @@ import { afterEach, describe, expect, it, vi } from 'vitest'import { downscaleDataUrlForPreview } from './image-resize'// A minimal valid 1x1 red PNG (67 bytes) for testing-const TINY_PNG_B64 =- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+GkZcAAAAASUVORK5CYII='+const TINY_PNG_B64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+GkZcAAAAASUVORK5CYII='+const TINY_PNG_DATA_URL = `data:image/png;base64,${TINY_PNG_B64}`// A 1×1 transparent PNG used as the fallback placeholder@@ -19,21 +19,15 @@ describe('downscaleDataUrlForPreview', () => {describe('without createImageBitmap (jsdom default)', () => {it('returns the original when createImageBitmap is unavailable', async () => {- await expect(downscaleDataUrlForPreview(TINY_PNG_DATA_URL)).resolves.toBe(- TINY_PNG_DATA_URL- )+ await expect(downscaleDataUrlForPreview(TINY_PNG_DATA_URL)).resolves.toBe(TINY_PNG_DATA_URL)})it('returns the original for a non-data-URL string', async () => {- await expect(downscaleDataUrlForPreview('not-a-data-url')).resolves.toBe(- 'not-a-data-url'- )+ await expect(downscaleDataUrlForPreview('not-a-data-url')).resolves.toBe('not-a-data-url')})it('returns the original for a data URL without a comma', async () => {- await expect(downscaleDataUrlForPreview('data:text/plain')).resolves.toBe(- 'data:text/plain'- )+ await expect(downscaleDataUrlForPreview('data:text/plain')).resolves.toBe('data:text/plain')})})@@ -46,6 +40,7 @@ describe('downscaleDataUrlForPreview', () => {const close = vi.fn()const drawImage = vi.fn()const convertToBlob = vi.fn(async () => new Blob(['x'], { type: 'image/png' }))+ const canvasSizes: [number, number][] = []const bitmap = { width: bitmapWidth, height: bitmapHeight, close }const ctx = { drawImage }@@ -53,7 +48,9 @@ describe('downscaleDataUrlForPreview', () => {class MockOffscreenCanvas {getContext = vi.fn(() => ctx)convertToBlob = convertToBlob- constructor(_w: number, _h: number) {}+ constructor(width: number, height: number) {+ canvasSizes.push([width, height])+ }}// Mock fetch to return a Blob from the data URL@@ -63,10 +60,13 @@ describe('downscaleDataUrlForPreview', () => {blob: async () => new Blob([new Uint8Array([0])], { type: 'image/png' })})))- vi.stubGlobal('createImageBitmap', vi.fn(async () => bitmap))+ vi.stubGlobal(+ 'createImageBitmap',+ vi.fn(async () => bitmap)+ )vi.stubGlobal('OffscreenCanvas', MockOffscreenCanvas)- return { bitmap, close, drawImage, convertToBlob, ctx }+ return { bitmap, close, drawImage, convertToBlob, ctx, canvasSizes }}it('returns the original when image is smaller than maxLongEdge', async () => {@@ -76,17 +76,17 @@ describe('downscaleDataUrlForPreview', () => {expect(result).toBe(TINY_PNG_DATA_URL)})- it('downscales when image exceeds maxLongEdge', async () => {+ it('downscales when image exceeds the default preview edge', async () => {const { drawImage, close } = setupMocks(4000, 3000)// In jsdom, FileReader.readAsDataURL won't actually produce a data URL,// so the function falls through to the fallback placeholder. But the// important thing is that drawImage was called with scaled dimensions// and the bitmap was closed.- const result = await downscaleDataUrlForPreview(TINY_PNG_DATA_URL, 2048)+ const result = await downscaleDataUrlForPreview(TINY_PNG_DATA_URL)- // scale = 2048 / 4000 = 0.512 → width=2048, height=1536- expect(drawImage).toHaveBeenCalledWith(expect.anything(), 0, 0, 2048, 1536)+ // scale = 512 / 4000 = 0.128 → width=512, height=384+ expect(drawImage).toHaveBeenCalledWith(expect.anything(), 0, 0, 512, 384)expect(close).toHaveBeenCalled()// Result should be the downscaled data URL (from our mocked blob),@@ -97,6 +97,16 @@ describe('downscaleDataUrlForPreview', () => {expect(close).toHaveBeenCalled()})+ it('keeps every thumbnail bounded for a 72-image composer prompt', async () => {+ const { canvasSizes, drawImage } = setupMocks(6000, 6000)++ await Promise.all(Array.from({ length: 72 }, () => downscaleDataUrlForPreview(TINY_PNG_DATA_URL)))++ expect(canvasSizes).toHaveLength(72)+ expect(canvasSizes.every(([width, height]) => width === 512 && height === 512)).toBe(true)+ expect(drawImage).toHaveBeenCalledTimes(72)+ })+it('returns placeholder when createImageBitmap throws', async () => {vi.stubGlobal('fetch',@@ -110,7 +120,10 @@ describe('downscaleDataUrlForPreview', () => {throw new Error('decode failed')}))- vi.stubGlobal('OffscreenCanvas', vi.fn(() => ({})))+ vi.stubGlobal(+ 'OffscreenCanvas',+ vi.fn(() => ({}))+ )const result = await downscaleDataUrlForPreview(TINY_PNG_DATA_URL, 2048)expect(result).toBe(FALLBACK_PLACEHOLDER)@@ -131,7 +144,10 @@ describe('downscaleDataUrlForPreview', () => {blob: async () => new Blob([new Uint8Array([0])], { type: 'image/png' })})))- vi.stubGlobal('createImageBitmap', vi.fn(async () => bitmap))+ vi.stubGlobal(+ 'createImageBitmap',+ vi.fn(async () => bitmap)+ )vi.stubGlobal('OffscreenCanvas', NullCanvas)const result = await downscaleDataUrlForPreview(TINY_PNG_DATA_URL, 2048)@@ -157,7 +173,10 @@ describe('downscaleDataUrlForPreview', () => {blob: async () => new Blob([new Uint8Array([0])], { type: 'image/png' })})))- vi.stubGlobal('createImageBitmap', vi.fn(async () => bitmap))+ vi.stubGlobal(+ 'createImageBitmap',+ vi.fn(async () => bitmap)+ )vi.stubGlobal('OffscreenCanvas', BrokenCanvas)const result = await downscaleDataUrlForPreview(TINY_PNG_DATA_URL, 2048)diff --git a/apps/desktop/src/lib/image-resize.ts b/apps/desktop/src/lib/image-resize.tsindex 534dfa76591..9da24b951d9 100644--- a/apps/desktop/src/lib/image-resize.ts+++ b/apps/desktop/src/lib/image-resize.ts@@ -10,7 +10,7 @@* thumbnail is downscaled.** @param dataUrl The full-resolution data URL (data:image/...;base64,...)- * @param maxLongEdge Maximum pixel dimension on the longest side (default 2048)+ * @param maxLongEdge Maximum pixel dimension on the longest side (default 512)* @returns A downscaled data URL (PNG), the original if already small enough,* or a 1×1 transparent PNG placeholder if downscaling fails.*/@@ -19,9 +19,15 @@const FALLBACK_PLACEHOLDER ='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+GkZcAAAAASUVORK5CYII='+// Composer pills render at 32 CSS pixels. A 512px source stays sharp on+// high-density displays while keeping a square RGBA decode to 1 MiB. The old+// 2048px default could decode to 16 MiB per thumbnail, so a large multi-image+// prompt still put substantial pressure on Chromium's renderer.+const DEFAULT_MAX_LONG_EDGE = 512+export async function downscaleDataUrlForPreview(dataUrl: string,- maxLongEdge = 2048+ maxLongEdge = DEFAULT_MAX_LONG_EDGE): Promise<string> {// Guard: createImageBitmap and OffscreenCanvas are not available in jsdom// (test environment) or very old Chromium builds. Return the original if missing.@@ -77,6 +83,7 @@ export async function downscaleDataUrlForPreview(resolve(FALLBACK_PLACEHOLDER)}}+reader.onerror = () => resolve(FALLBACK_PLACEHOLDER)reader.readAsDataURL(resultBlob)})