Skip to content

The agent that grows with you

fix(webapp): name the 16 MiB limit when a browser pick or drop is too large

unsupportedpastelsfacefdfb24e0
diff --git a/apps/desktop/src/app/chat/hooks/use-composer-actions.ts b/apps/desktop/src/app/chat/hooks/use-composer-actions.ts
index b5821cbacf4..5ce2a90c173 100644
--- a/apps/desktop/src/app/chat/hooks/use-composer-actions.ts
+++ b/apps/desktop/src/app/chat/hooks/use-composer-actions.ts
@@ -675,16 +675,26 @@ export function useComposerActions({
)
const pickImages = useCallback(async () => {
- const paths = await selectDesktopPaths({
- title: copy.attachImages,
- defaultPath: currentCwd || undefined,
- filters: [
- {
- name: t.composer.images,
- extensions: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'tiff']
- }
- ]
- })
+ let paths: string[]
+
+ try {
+ paths = await selectDesktopPaths({
+ title: copy.attachImages,
+ defaultPath: currentCwd || undefined,
+ filters: [
+ {
+ name: t.composer.images,
+ extensions: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'tiff']
+ }
+ ]
+ })
+ } catch (err) {
+ // The browser stages a pick before returning its path; a refused stage (an image over the
+ // upload limit) is reported here, not left as an unhandled rejection.
+ notifyError(err, copy.imageAttachFailed)
+
+ return
+ }
if (!paths?.length) {
return
@@ -693,7 +703,7 @@ export function useComposerActions({
for (const path of paths) {
await attachImagePath(path)
}
- }, [attachImagePath, copy.attachImages, currentCwd, t.composer.images])
+ }, [attachImagePath, copy.attachImages, copy.imageAttachFailed, currentCwd, t.composer.images])
const pasteClipboardImage = useCallback(
async ({ silent = false }: { silent?: boolean } = {}) => {
diff --git a/apps/desktop/src/lib/browser-uploads.test.ts b/apps/desktop/src/lib/browser-uploads.test.ts
index ba83fb2a6bf..5a676ef3715 100644
--- a/apps/desktop/src/lib/browser-uploads.test.ts
+++ b/apps/desktop/src/lib/browser-uploads.test.ts
@@ -1,5 +1,8 @@
+import { act, cleanup, renderHook } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
+import { useComposerActions } from '@/app/chat/hooks/use-composer-actions'
+import { $notifications, clearNotifications } from '@/store/notifications'
import { $connection } from '@/store/session'
import { clearWebappSession, seedWebappSession } from '@/test/webapp-session'
@@ -7,6 +10,8 @@ import { installBrowserDesktopBridge } from './browser-desktop-bridge'
import { BROWSER_UPLOAD_MAX_BYTES } from './browser-uploads'
afterEach(() => {
+ cleanup()
+ clearNotifications()
clearWebappSession()
Reflect.deleteProperty(window, 'hermesDesktop')
document.documentElement.removeAttribute('data-hermes-desktop-host')
@@ -54,3 +59,68 @@ describe('browser image paste size', () => {
expect((fetchMock.mock.calls[0] as [URL])[0].pathname).toBe('/api/chat/image-upload')
})
})
+
+describe('browser file stage size', () => {
+ // A picked or dropped file over the upload limit gets the server's 413 "Upload request is too large",
+ // which doesn't say what the limit is, and a refused image pick used to show nothing at all.
+ const tooLarge = (name: string, type: string) => new File([new Uint8Array(BROWSER_UPLOAD_MAX_BYTES + 1)], name, { type })
+ const limitMessage = (name: string) => `${name} is larger than 16 MiB, the largest file the browser can attach.`
+
+ async function installBrowser() {
+ seedWebappSession('served-token')
+
+ const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
+ const url = new URL(String(input), window.location.origin)
+
+ if (url.pathname === '/api/chat/file-upload') {
+ return new Response(JSON.stringify({ detail: 'Upload request is too large' }), { status: 413 })
+ }
+
+ throw new Error(`Unexpected fetch: ${url}`)
+ })
+
+ vi.stubGlobal('fetch', fetchMock)
+ expect(installBrowserDesktopBridge()).toBe(true)
+ $connection.set(await window.hermesDesktop.getConnection('owner'))
+
+ const uploads = () => fetchMock.mock.calls.filter(([input]) => String(input).includes('/api/chat/file-upload'))
+
+ return { uploads }
+ }
+
+ const renderComposer = () =>
+ renderHook(() => useComposerActions({ activeSessionId: null, currentCwd: '/workspace', requestGateway: vi.fn() }))
+
+ it('refuses a picked image over the limit, names the limit, and uploads nothing', async () => {
+ const { uploads } = await installBrowser()
+ const { result } = renderComposer()
+
+ vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(function (this: HTMLInputElement) {
+ Object.defineProperty(this, 'files', { configurable: true, value: [tooLarge('huge.png', 'image/png')] })
+ this.dispatchEvent(new Event('change'))
+ })
+
+ await act(async () => {
+ await result.current.pickImages()
+ })
+
+ expect(uploads()).toHaveLength(0)
+ expect($notifications.get()).toContainEqual(expect.objectContaining({ kind: 'error', message: limitMessage('huge.png') }))
+ })
+
+ it('names the limit when a dropped file over it cannot be attached', async () => {
+ const { uploads } = await installBrowser()
+ const { result } = renderComposer()
+ let attached = true
+
+ await act(async () => {
+ attached = await result.current.attachDroppedItems([{ file: tooLarge('huge.zip', 'application/zip'), path: '' }])
+ })
+
+ expect(attached).toBe(false)
+ expect(uploads()).toHaveLength(0)
+ expect($notifications.get()).toContainEqual(
+ expect.objectContaining({ kind: 'warning', message: `Could not attach huge.zip: ${limitMessage('huge.zip')}` })
+ )
+ })
+})
diff --git a/apps/desktop/src/lib/browser-uploads.ts b/apps/desktop/src/lib/browser-uploads.ts
index 651cc3b6e53..0e5b8fccb0d 100644
--- a/apps/desktop/src/lib/browser-uploads.ts
+++ b/apps/desktop/src/lib/browser-uploads.ts
@@ -94,6 +94,13 @@ async function stageBrowserFile(
profile: null | string,
onStaged: OnStaged
): Promise<string> {
+ // The server refuses a larger file anyway, with a 413 that doesn't name the limit: say it here.
+ if (file.size > BROWSER_UPLOAD_MAX_BYTES) {
+ throw new Error(
+ `${file.name || 'The file'} is larger than ${BROWSER_UPLOAD_MAX_BYTES / (1024 * 1024)} MiB, the largest file the browser can attach.`
+ )
+ }
+
const form = new FormData()
form.append('file', file, file.name || 'attachment')