diff --git a/package.json b/package.json index 3c1979d5ff0e..915a51e4aa5d 100644 --- a/package.json +++ b/package.json @@ -216,6 +216,7 @@ "@sanity/diff-match-patch": "^3.2.1", "@shadcn/lint": "~0.1.5", "@stablyai/playwright-test": "^2.1.16", + "@streamparser/json": "0.0.26", "@tailwindcss/vite": "^4.2.4", "@tanstack/react-virtual": "^3.14.13", "@testing-library/jest-dom": "^6.9.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fbf12ea1ae17..9f833c400ca9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -293,6 +293,9 @@ importers: '@stablyai/playwright-test': specifier: ^2.1.16 version: 2.1.16(@playwright/test@1.63.0)(zod@4.6.5) + '@streamparser/json': + specifier: 0.0.26 + version: 0.0.26 '@tailwindcss/vite': specifier: ^4.2.4 version: 4.2.4(rolldown-vite@7.3.1(@emnapi/core@1.11.2)(@emnapi/runtime@1.11.2)(@types/node@25.9.5)(esbuild@0.28.2)(jiti@2.7.0)(yaml@2.9.1)) @@ -3188,6 +3191,9 @@ packages: '@standard-schema/spec@1.1.0': resolution: {integrity: sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==} + '@streamparser/json@0.0.26': + resolution: {integrity: sha512-46597LNFI+MFdUnzX2QJWwmdTRdq0XVD+vVNJTtGVzIrnCuhG9pFo1OAzbNBqci8UJgk/X5KJZ6LcV+y7PTuDQ==} + '@swc/core-darwin-arm64@1.15.46': resolution: {integrity: sha512-IsISIT22EfktVJrlvIpnAxG2u/A9aob9l99HMlx80x72WlFmFPk1V3UhkEzx86eJP8hw049KTFv/RISho2cq2Q==} engines: {node: '>=10'} @@ -10214,6 +10220,8 @@ snapshots: '@standard-schema/spec@1.1.0': {} + '@streamparser/json@0.0.26': {} + '@swc/core-darwin-arm64@1.15.46': optional: true diff --git a/src/main/native-chat/agent-session-journal/journal-submission-positions.test.ts b/src/main/native-chat/agent-session-journal/journal-submission-positions.test.ts index 2ae99a42b45b..374163786053 100644 --- a/src/main/native-chat/agent-session-journal/journal-submission-positions.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-submission-positions.test.ts @@ -7,7 +7,6 @@ import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, describe, expect, it } from 'vitest' -import { codexProviderHandle } from '../../../shared/agent-session-provider-handle-encoding' import { agentSessionFailureFact } from '../../../shared/agent-session-failure' import { agentSessionFailureWords } from '../../../shared/agent-session-failure-words' import { @@ -19,6 +18,7 @@ import { type AgentJournalMessageItem, type AgentSessionJournalIdentity } from '../../../shared/agent-session-journal-types' +import { codexProviderHandle } from '../../../shared/agent-session-provider-handle-encoding' import { readAgentSessionHydrationPage } from '../agent-session-wire/agent-session-history-page' import { createTrackedJournalOpener } from './journal-host-database-test-support' diff --git a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx index 61121936c404..9cd8a9f3b502 100644 --- a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx +++ b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx @@ -2,6 +2,8 @@ import React, { act, type ReactNode } from 'react' import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import type * as FeedbackImageAttachments from '@/lib/feedback-image-attachments' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const mocks = vi.hoisted(() => ({ @@ -15,6 +17,7 @@ const mocks = vi.hoisted(() => ({ vi.mock('sonner', () => ({ toast: { error: vi.fn(), + info: vi.fn(), success: vi.fn(), warning: mocks.toastWarning } @@ -194,6 +197,100 @@ describe('SidebarFeedbackDialog environment prefill', () => { }) describe('SidebarFeedbackDialog image submission', () => { + it.each(['refused', 'shrunk'] as const)( + 'preserves text and its caret when an oversized image is %s after paste', + async (result) => { + const user = userEvent.setup() + const bytes = new Uint8Array(53) + bytes.set([137, 80, 78, 71, 13, 10, 26, 10]) + const header = new DataView(bytes.buffer) + header.setUint32(8, 13) + bytes.set(new TextEncoder().encode('IHDR'), 12) + header.setUint32(16, 1) + header.setUint32(20, 1) + header.setUint32(33, 8) + bytes.set(new TextEncoder().encode(result === 'refused' ? 'acTL' : 'IDAT'), 37) + const file = new File([bytes], 'capture.png', { type: 'image/png' }) + Object.defineProperty(file, 'size', { value: 6_000_000 }) + let finishRead: + | (( + value: Awaited> + ) => void) + | undefined + mocks.readFeedbackImageFiles.mockReturnValue( + new Promise((resolve) => { + finishRead = resolve + }) + ) + render() + const textarea = screen.getByPlaceholderText('What could we improve?') + await waitFor(() => expect(textarea.value).toContain('Orca:')) + fireEvent.change(textarea, { target: { value: 'before after' } }) + textarea.focus() + textarea.setSelectionRange(7, 7) + const clipboard = new DataTransfer() + clipboard.setData('text/plain', 'report') + Object.defineProperty(clipboard, 'files', { value: [file] }) + + await user.paste(clipboard) + expect(textarea.value).toBe('before reportafter') + expect(textarea.selectionStart).toBe(13) + await user.keyboard('!') + expect(textarea.value).toBe('before report!after') + await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalled()) + const actual = await vi.importActual( + '@/lib/feedback-image-attachments' + ) + const readResult = + result === 'refused' + ? await actual.readFeedbackImageFiles([file], 0) + : { + images: [ + { + id: 'shrunk', + name: 'capture.png', + contentType: 'image/png', + bytes: 100, + data: new Uint8Array([1]), + previewUrl: 'blob:shrunk' + } + ], + errors: [], + notices: [] + } + await act(async () => { + finishRead?.(readResult) + }) + expect(textarea.value).toBe('before report!after') + expect(textarea.selectionStart).toBe(14) + if (result === 'refused') { + expect(mocks.toastWarning).toHaveBeenCalledWith('capture.png is larger than 4.0 MB.') + expect(screen.queryByRole('button', { name: 'Remove capture.png' })).toBeNull() + } else { + expect(screen.getByRole('button', { name: 'Remove capture.png' })).not.toBeNull() + } + } + ) + + it('still consumes mixed text when the image fits without shrinking', async () => { + const user = userEvent.setup() + mocks.readFeedbackImageFiles.mockResolvedValue({ images: [], errors: [], notices: [] }) + render() + const textarea = screen.getByPlaceholderText('What could we improve?') + await waitFor(() => expect(textarea.value).toContain('Orca:')) + fireEvent.change(textarea, { target: { value: 'report' } }) + textarea.focus() + textarea.setSelectionRange(6, 6) + const clipboard = new DataTransfer() + clipboard.setData('text/plain', 'image metadata') + const file = new File(['image'], 'small.png', { type: 'image/png' }) + Object.defineProperty(clipboard, 'files', { value: [file] }) + await user.paste(clipboard) + expect(textarea.value).toBe('report') + expect(textarea.selectionStart).toBe(6) + await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledWith([file], 0, 0)) + }) + it('keeps the dialog scrollable within short windows', () => { const { container } = render() const content = container.querySelector('.scrollbar-sleek') @@ -222,6 +319,8 @@ describe('SidebarFeedbackDialog image submission', () => { expect((send as HTMLButtonElement).disabled).toBe(true) fireEvent.click(send) expect(mocks.submit).not.toHaveBeenCalled() + // Why: a shrink can run for a while, and no thumbnail yet reads as a dropped file. + expect(await screen.findByText('Preparing attachments…')).not.toBeNull() await act(async () => { finishRead?.({ @@ -235,11 +334,13 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) }) await waitFor(() => expect((send as HTMLButtonElement).disabled).toBe(false)) + await waitFor(() => expect(screen.queryByText('Preparing attachments…')).toBeNull()) const remove = screen.getByRole('button', { name: 'Remove shot.png' }) expect(remove.dataset.slot).toBe('button') expect(remove.dataset.size).toBe('icon-xs') @@ -250,6 +351,36 @@ describe('SidebarFeedbackDialog image submission', () => { ]) }) + it('does not flash the preparing hint for an image that reads at once', async () => { + mocks.readFeedbackImageFiles.mockResolvedValue({ + images: [ + { + id: 'small', + name: 'small.png', + contentType: 'image/png', + bytes: 5, + data: new Uint8Array([1]), + previewUrl: 'blob:small' + } + ], + errors: [], + notices: [] + }) + const { container } = render() + const input = container.querySelector('input[type="file"]') + fireEvent.change(input!, { + target: { files: [new File(['image'], 'small.png', { type: 'image/png' })] } + }) + + expect(screen.queryByText('Preparing attachments…')).toBeNull() + await screen.findByRole('button', { name: 'Remove small.png' }) + // Past the show delay: a completed read must not leave a stale hint. + await act(async () => { + await new Promise((resolve) => setTimeout(resolve, 300)) + }) + expect(screen.queryByText('Preparing attachments…')).toBeNull() + }) + it('warns when the server cannot confirm image delivery', async () => { mocks.readFeedbackImageFiles.mockResolvedValue({ images: [ @@ -262,7 +393,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) mocks.submit.mockResolvedValue({ ok: true, imagesDelivered: false }) const onOpenChange = vi.fn() @@ -298,7 +430,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) mocks.submit.mockResolvedValue({ ok: true, @@ -341,7 +474,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) mocks.submit.mockResolvedValue({ ok: true, @@ -379,7 +513,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) const { container, unmount } = render() const input = container.querySelector('input[type="file"]') @@ -393,24 +528,26 @@ describe('SidebarFeedbackDialog image submission', () => { expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:shot') }) - it('does not consume text when the pasted image cannot be attached', () => { + it('does not consume text when the pasted image cannot be attached', async () => { mocks.readFeedbackImageFiles.mockResolvedValue({ images: [], - errors: ['huge.png is larger than 4.0 MB.'] + errors: ['huge.gif is larger than 4.0 MB.'], + notices: [] }) render() const textarea = screen.getByPlaceholderText('What could we improve?') - const file = new File(['image'], 'huge.png', { type: 'image/png' }) + // Why: PNGs over budget are shrunk; GIFs are not, so this one stays unattachable. + const file = new File(['image'], 'huge.gif', { type: 'image/gif' }) Object.defineProperty(file, 'size', { value: 4 * 1024 * 1024 + 1 }) const paste = new Event('paste', { bubbles: true, cancelable: true }) Object.defineProperty(paste, 'clipboardData', { - value: { files: [file] } + value: { files: [file], getData: () => '' } }) fireEvent(textarea, paste) expect(paste.defaultPrevented).toBe(false) - expect(mocks.readFeedbackImageFiles).toHaveBeenCalledWith([file], 0, 0) + await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledWith([file], 0, 0)) }) // Why: the byte budget, not the count, is what binds after one full-screen @@ -428,7 +565,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:full' } ], - errors: [] + errors: [], + notices: [] }) const { container } = render() const input = container.querySelector('input[type="file"]') @@ -440,12 +578,121 @@ describe('SidebarFeedbackDialog image submission', () => { const small = new File(['x'], 'small.png', { type: 'image/png' }) Object.defineProperty(small, 'size', { value: 1024 }) const paste = new Event('paste', { bubbles: true, cancelable: true }) - Object.defineProperty(paste, 'clipboardData', { value: { files: [small] } }) + Object.defineProperty(paste, 'clipboardData', { value: { files: [small], getData: () => '' } }) fireEvent(screen.getByPlaceholderText('What could we improve?'), paste) expect(paste.defaultPrevented).toBe(false) }) + it('preserves pasted text while a mixed pending batch can fill the byte budget', async () => { + const user = userEvent.setup() + let finishFirstRead: ((value: unknown) => void) | undefined + mocks.readFeedbackImageFiles + .mockReturnValueOnce( + new Promise((resolve) => { + finishFirstRead = resolve + }) + ) + .mockResolvedValueOnce({ images: [], errors: ['No room for second.png'], notices: [] }) + const { container } = render() + const textarea = screen.getByPlaceholderText('What could we improve?') + await waitFor(() => expect(textarea.value).toContain('Orca:')) + fireEvent.change(textarea, { target: { value: 'before after' } }) + const retina = new File(['x'], 'retina.png', { type: 'image/png' }) + const gif = new File(['x'], 'anim.gif', { type: 'image/gif' }) + Object.defineProperty(retina, 'size', { value: 6_400_000 }) + Object.defineProperty(gif, 'size', { value: 2_444_304 }) + fireEvent.change(container.querySelector('input[type="file"]')!, { + target: { files: [retina, gif] } + }) + await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledTimes(1)) + textarea.focus() + textarea.setSelectionRange(7, 7) + const clipboard = new DataTransfer() + clipboard.setData('text/plain', 'report') + const second = new File(['x'], 'second.png', { type: 'image/png' }) + Object.defineProperty(second, 'size', { value: 1_500_000 }) + Object.defineProperty(clipboard, 'files', { value: [second] }) + + await user.paste(clipboard) + expect(textarea.value).toBe('before reportafter') + await user.keyboard('!') + expect(textarea.value).toBe('before report!after') + await act(async () => + finishFirstRead?.({ + images: [ + { + id: 'retina', + name: 'retina.png', + contentType: 'image/png', + bytes: 1_750_000, + data: new Uint8Array([1]), + previewUrl: 'blob:retina' + }, + { + id: 'gif', + name: 'anim.gif', + contentType: 'image/gif', + bytes: 2_444_304, + data: new Uint8Array([1]), + previewUrl: 'blob:gif' + } + ], + errors: [], + notices: [] + }) + ) + await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledTimes(2)) + expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(2, [second], 2, 4_194_304) + expect(textarea.value).toBe('before report!after') + expect(textarea.selectionStart).toBe(14) + }) + + // Why: a screenshot still shrinking will commit at most half the budget; holding + // its raw size against the gate would let a second paste spill into the textarea. + it('consumes a second oversized paste while the first is still shrinking', async () => { + const shrunk = (name: string) => ({ + images: [ + { + id: name, + name: `${name}.png`, + contentType: 'image/png', + bytes: 1_750_000, + data: new Uint8Array([1]), + previewUrl: `blob:${name}` + } + ], + errors: [], + notices: [] + }) + let finishFirstShrink: ((value: unknown) => void) | undefined + mocks.readFeedbackImageFiles + .mockReturnValueOnce( + new Promise((resolve) => { + finishFirstShrink = resolve + }) + ) + .mockResolvedValueOnce(shrunk('second')) + render() + const textarea = screen.getByPlaceholderText('What could we improve?') + const pasteScreenshot = (name: string): Event => { + const file = new File(['x'], `${name}.png`, { type: 'image/png' }) + Object.defineProperty(file, 'size', { value: 6 * 1024 * 1024 }) + const paste = new Event('paste', { bubbles: true, cancelable: true }) + Object.defineProperty(paste, 'clipboardData', { value: { files: [file], getData: () => '' } }) + fireEvent(textarea, paste) + return paste + } + + expect(pasteScreenshot('first').defaultPrevented).toBe(true) + expect(pasteScreenshot('second').defaultPrevented).toBe(true) + await act(async () => finishFirstShrink?.(shrunk('first'))) + + await screen.findByRole('button', { name: 'Remove first.png' }) + await screen.findByRole('button', { name: 'Remove second.png' }) + expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(2, expect.any(Array), 1, 1_750_000) + }) + it('stops offering Attach once the byte budget is spent, below the count limit', async () => { mocks.readFeedbackImageFiles.mockResolvedValue({ images: [ @@ -458,7 +705,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:full' } ], - errors: [] + errors: [], + notices: [] }) const { container } = render() expect(screen.getByRole('button', { name: 'Attach' }).hasAttribute('disabled')).toBe(false) @@ -473,21 +721,26 @@ describe('SidebarFeedbackDialog image submission', () => { expect(screen.getByRole('button', { name: 'Attach' }).hasAttribute('disabled')).toBe(true) }) - it('counts committed and in-flight image bytes against the total budget', async () => { - mocks.readFeedbackImageFiles.mockResolvedValueOnce({ + it('sizes each queued batch against what the batches before it committed', async () => { + const committed = (name: string, bytes: number) => ({ images: [ { - id: 'first', - name: 'first.png', + id: name, + name: `${name}.png`, contentType: 'image/png', - bytes: 1000, + bytes, data: new Uint8Array([1]), - previewUrl: 'blob:first' + previewUrl: `blob:${name}` } ], - errors: [] - }) - mocks.readFeedbackImageFiles.mockReturnValue(new Promise(() => {})) + errors: [], + notices: [] + }) + mocks.readFeedbackImageFiles + .mockResolvedValueOnce(committed('first', 1000)) + // A shrunk image commits far fewer bytes than its file size. + .mockResolvedValueOnce(committed('second', 150)) + .mockReturnValue(new Promise(() => {})) const { container } = render() const input = container.querySelector('input[type="file"]') fireEvent.change(input!, { @@ -501,8 +754,9 @@ describe('SidebarFeedbackDialog image submission', () => { fireEvent.change(input!, { target: { files: [second] } }) fireEvent.change(input!, { target: { files: [third] } }) + await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledTimes(3)) expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(2, [second], 1, 1000) - expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(3, [third], 2, 1200) + expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(3, [third], 2, 1150) }) it('rejects images added after submission starts instead of clearing them unsent', async () => { diff --git a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx index 9224d3f85bd5..7be16e4d9eaf 100644 --- a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx +++ b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx @@ -263,7 +263,12 @@ export function SidebarFeedbackDialog({ // An unsupported image still routes through for its rejection toast, // but preventing default there would silently eat co-pasted text. const reserved = getReservedImageCapacity() - if (hasAttachableFeedbackImage(pasted, reserved.count, reserved.bytes)) { + // An optimistic shrink may fail; let co-pasted text insert now rather than restore it later. + if ( + hasAttachableFeedbackImage(pasted, reserved.count, reserved.bytes, { + allowShrinking: event.clipboardData.getData('text/plain').length === 0 + }) + ) { event.preventDefault() } handleAddFiles(pasted) @@ -352,6 +357,7 @@ export function SidebarFeedbackDialog({ void @@ -21,6 +26,7 @@ type SidebarFeedbackImageAttachmentsProps = { export function SidebarFeedbackImageAttachments({ images, + pendingCount, disabled, isDragActive, onAddFiles, @@ -33,6 +39,17 @@ export function SidebarFeedbackImageAttachments({ const attachedBytes = images.reduce((total, image) => total + image.bytes, 0) const atCapacity = images.length >= MAX_FEEDBACK_IMAGE_COUNT || attachedBytes >= MAX_FEEDBACK_IMAGE_TOTAL_BYTES + const hasPendingReads = pendingCount > 0 + const [showPreparing, setShowPreparing] = React.useState(false) + React.useEffect(() => { + if (!hasPendingReads) { + setShowPreparing(false) + return + } + const timer = window.setTimeout(() => setShowPreparing(true), PREPARING_HINT_DELAY_MS) + return () => window.clearTimeout(timer) + }, [hasPendingReads]) + const isPreparing = hasPendingReads && showPreparing return (
- {translate( - 'auto.components.sidebar.SidebarFeedbackImageAttachments.screenshotsHint', - 'Attach up to {{count}} screenshots, {{maxSize}} total', - { - count: MAX_FEEDBACK_IMAGE_COUNT, - maxSize: formatFeedbackImageSize(MAX_FEEDBACK_IMAGE_TOTAL_BYTES) - } - )} + {/* Why: shrinking an oversized screenshot is slow enough that a silent gap + between the pick and the thumbnail reads as a dropped attachment. */} + {isPreparing + ? translate( + 'auto.components.sidebar.SidebarFeedbackImageAttachments.preparing', + 'Preparing attachments…' + ) + : translate( + 'auto.components.sidebar.SidebarFeedbackImageAttachments.screenshotsHint', + 'Attach up to {{count}} screenshots, {{maxSize}} total', + { + count: MAX_FEEDBACK_IMAGE_COUNT, + maxSize: formatFeedbackImageSize(MAX_FEEDBACK_IMAGE_TOTAL_BYTES) + } + )}