Skip to content

The agent that grows with you

fix(webapp): give the drop warning a character budget

unsupportedpastels8974b3e67982
diff --git a/apps/desktop/src/app/chat/hooks/drop-warning.ts b/apps/desktop/src/app/chat/hooks/drop-warning.ts
new file mode 100644
index 00000000000..d67cb9d186e
--- /dev/null
+++ b/apps/desktop/src/app/chat/hooks/drop-warning.ts
@@ -0,0 +1,56 @@
+/** A drop item the composer could not attach: what was dropped, and why when it is known. */
+export interface DropFailure {
+ name: string
+ reason?: string
+}
+
+/** How many failed drop items the warning names before it counts the rest. */
+const DROP_FAILURES_SHOWN = 3
+
+/** The longest name the warning shows whole; a longer one keeps its start and its end. */
+const DROP_FAILURE_NAME_CHARS = 48
+
+/** The warning names failures until this many characters, then counts the rest. */
+const DROP_WARNING_CHARS = 240
+
+function middleEllipsis(text: string, max: number): string {
+ if (text.length <= max) {
+ return text
+ }
+
+ const head = Math.ceil((max - 1) / 2)
+
+ return `${text.slice(0, head)}…${text.slice(text.length - (max - 1 - head))}`
+}
+
+function failureText({ name, reason }: DropFailure, maxName = Infinity): string {
+ const label = `Could not attach ${middleEllipsis(name, maxName)}`
+
+ return reason ? `${label}: ${reason}` : label
+}
+
+/** One bounded warning for a drop's failures: shortened names, a few of them, and a count of the
+ * rest, so a large drop or long paths can't grow the toast off screen. Whenever anything is
+ * shortened or left out, the full list rides in the collapsible detail. */
+export function dropWarning(
+ failures: DropFailure[],
+ more: (count: number) => string
+): { detail?: string; message: string } {
+ const shown: string[] = []
+
+ for (const failure of failures.slice(0, DROP_FAILURES_SHOWN)) {
+ const text = failureText(failure, DROP_FAILURE_NAME_CHARS)
+
+ if (shown.length && [...shown, text].join('; ').length > DROP_WARNING_CHARS) {
+ break
+ }
+
+ shown.push(text)
+ }
+
+ const hidden = failures.length - shown.length
+ const message = hidden > 0 ? `${shown.join('; ')}; ${more(hidden)}` : shown.join('; ')
+ const full = failures.map(failure => failureText(failure))
+
+ return message === full.join('; ') ? { message } : { detail: full.join('\n'), message }
+}
diff --git a/apps/desktop/src/app/chat/hooks/use-composer-actions.drop-warnings.test.ts b/apps/desktop/src/app/chat/hooks/use-composer-actions.drop-warnings.test.ts
index 21ed119908c..a232f51ca1b 100644
--- a/apps/desktop/src/app/chat/hooks/use-composer-actions.drop-warnings.test.ts
+++ b/apps/desktop/src/app/chat/hooks/use-composer-actions.drop-warnings.test.ts
@@ -121,4 +121,33 @@ describe('useComposerActions drop warnings', () => {
expect(warning?.detail?.split('\n')).toHaveLength(50)
expect(warning?.detail).toContain('huge-49.zip')
})
+
+ // Three failures can still overflow when their names are long: shorten them, keep them whole in the detail.
+ it('bounds the warning for long names and keeps every full name in its detail', async () => {
+ const stageFileForAttach = vi.fn(async () => {
+ throw new Error('Upload request is too large')
+ })
+
+ const value = { getPathForFile: () => '', stageFileForAttach }
+ Object.defineProperty(window, 'hermesDesktop', { configurable: true, value })
+
+ const { result } = renderHook(() =>
+ useComposerActions({ activeSessionId: null, currentCwd: '/w', requestGateway: vi.fn() })
+ )
+
+ const names = Array.from({ length: 3 }, (_, index) => `long-${index}-${'x'.repeat(200)}.zip`)
+
+ await act(async () => {
+ await result.current.attachDroppedItems(names.map(name => ({ file: new File(['x'], name), path: '' })))
+ })
+
+ const warning = $notifications.get().find(notice => notice.message.includes('long-0-'))
+
+ expect(warning).toMatchObject({ kind: 'warning' })
+ expect(warning?.message.length).toBeLessThan(300)
+ expect(warning?.message).toContain('…')
+ expect(warning?.message).toContain('Upload request is too large')
+ expect(warning?.message).toMatch(/and \d+ more$/)
+ expect(warning?.detail?.split('\n')).toEqual(names.map(name => `Could not attach ${name}: Upload request is too large`))
+ })
})
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 451d11bbb1e..39fb2a34278 100644
--- a/apps/desktop/src/app/chat/hooks/use-composer-actions.ts
+++ b/apps/desktop/src/app/chat/hooks/use-composer-actions.ts
@@ -24,6 +24,8 @@ import { notify, notifyError, readableError } from '@/store/notifications'
import type { ImageDetachResponse } from '../../types'
+import { type DropFailure, dropWarning } from './drop-warning'
+
const IMAGE_EXTENSION_PATTERN = /\.(png|jpe?g|gif|webp|bmp|tiff?|svg|ico)$/i
const BLOB_MIME_EXTENSION: Record<string, string> = {
@@ -47,9 +49,6 @@ export function isImagePath(filePath: string): boolean {
return IMAGE_EXTENSION_PATTERN.test(filePath)
}
-/** How many failed drop items the warning names before it counts the rest. */
-const DROP_FAILURES_SHOWN = 3
-
/** A dropped File the composer sends as an image; never a folder, which has no bytes. */
function isDroppedImage(file: File, filePath: string, isDirectory?: boolean): boolean {
return !isDirectory && (file.type.startsWith('image/') || isImagePath(file.name) || isImagePath(filePath))
@@ -141,15 +140,13 @@ export async function stageDroppedFilePath(candidate: DroppedFile): Promise<stri
/** Stage a drop for the main composer's chips. A rejected stage keeps its
* reason in the failure the drop warning shows, not a bare "Could not attach". */
-async function stageDropForChip(candidate: DroppedFile, label: string): Promise<{ failure: string; path: string }> {
- const failure = `Could not attach ${label}`
-
+async function stageDropForChip(candidate: DroppedFile, name: string): Promise<{ failure: DropFailure; path: string }> {
try {
- return { failure, path: await stageDroppedFilePath(candidate) }
+ return { failure: { name }, path: await stageDroppedFilePath(candidate) }
} catch (error) {
const reason = readableError(error, '').message
- return { failure: reason ? `${failure}: ${reason}` : failure, path: '' }
+ return { failure: reason ? { name, reason } : { name }, path: '' }
}
}
@@ -812,7 +809,7 @@ export function useComposerActions({
let attached = false
// Every item that failed, so a drop that attached some files still names the rest.
- const failures: string[] = []
+ const failures: DropFailure[] = []
for (const candidate of candidates) {
const { file, isDirectory, path: knownPath } = candidate
@@ -826,7 +823,7 @@ export function useComposerActions({
continue
}
- failures.push(`Could not attach folder ${knownPath || ''}`)
+ failures.push({ name: `folder ${knownPath || ''}` })
continue
}
@@ -838,7 +835,7 @@ export function useComposerActions({
continue
}
- failures.push(`Could not attach ${knownPath}`)
+ failures.push({ name: knownPath })
continue
}
@@ -849,7 +846,7 @@ export function useComposerActions({
continue
}
- failures.push(`Could not attach ${knownPath || 'file'}`)
+ failures.push({ name: knownPath || 'file' })
continue
}
@@ -875,7 +872,7 @@ export function useComposerActions({
continue
}
- failures.push(`Could not attach ${file.name || 'image'}`)
+ failures.push({ name: file.name || 'image' })
continue
}
@@ -893,18 +890,7 @@ export function useComposerActions({
}
if (failures.length) {
- // Name the first few and count the rest so a large drop can't grow the toast
- // off screen; the full list rides in the collapsible detail.
- const hidden = failures.length - DROP_FAILURES_SHOWN
- const shown = failures.slice(0, DROP_FAILURES_SHOWN).join('; ')
- const more = copy.dropFilesMore
-
- notify({
- kind: 'warning',
- title: copy.dropFiles,
- message: hidden > 0 ? `${shown}; ${more(hidden)}` : shown,
- ...(hidden > 0 ? { detail: failures.join('\n') } : {})
- })
+ notify({ kind: 'warning', title: copy.dropFiles, ...dropWarning(failures, copy.dropFilesMore) })
}
return attached