Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-ui): keep the notices of one attach gesture and report images dropped at the cap
Alfredo Lopezaaabff8d0cfd
diff --git a/desktop-app/ui/src/components/agents/ComposerPanel.tsx b/desktop-app/ui/src/components/agents/ComposerPanel.tsxindex 8b957e3b9..81e9c8bad 100644--- a/desktop-app/ui/src/components/agents/ComposerPanel.tsx+++ b/desktop-app/ui/src/components/agents/ComposerPanel.tsx@@ -679,9 +679,9 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPrelse documents.push(file)}setComposerAttachmentError(null)- if (documents.length > 0) {- onAddComposerFiles(documents, draft)- }+ // Called for every gesture, with `[]` when it carried only images: this+ // is what replaces the document notices of the previous gesture.+ onAddComposerFiles(documents, draft)if (images.length > 0) void prepareComposerImageAttachments(images, source)},[draft, inferComposerImageMimeType, onAddComposerFiles, prepareComposerImageAttachments]diff --git a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsxindex b93c7a972..8339e0404 100644--- a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx+++ b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx@@ -824,6 +824,27 @@ describe('ComposerPanel with an image-capable model', () => {// No image was counted as skipped, so nothing is reported.expect(screen.queryByRole('alert')).toBeNull()})++ it('routes an image-only drop through the document path once, with no documents', async () => {+ draftState.value = 'caption'+ const { container } = render(<ComposerPanel inline />)+ const shell = container.querySelector('.composer-input-shell') as HTMLElement++ fireEvent.drop(shell, {+ dataTransfer: {+ files: [imageFile('only.png', 'image/png', [...PNG_BYTES, 7])],+ types: ['Files'],+ },+ })++ // Witness: the image of the drop was attached.+ await waitFor(() =>+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)+ )+ expect(expectSinglePreparedImage().name).toBe('only.png')+ // The empty call is what replaces the notices of the previous gesture.+ expect(actionsMock.handleAddComposerFiles.mock.calls).toEqual([[[], 'caption']])+ })})describe('ComposerPanel Codex image budgets', () => {diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsxindex 24131de36..5649a5985 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx@@ -11,7 +11,7 @@ import { COMPOSER_MAX_ATTACHMENTS, COMPOSER_MAX_FILE_BYTES } from '@constants/atimport { buildComposerFileReferences } from '@lib/composerFileReferences'import { composerRequestBaseContent } from '@lib/composerHostRequest'import { buildComposerRequestContent } from '@lib/composerReferencesPrompt'-import type { ComposerGlobalFileReference } from '../../../uiTypes'+import type { ComposerGlobalFileReference, ComposerImageAttachment } from '../../../uiTypes'import { useComposerAttachments } from '../useComposerAttachments';(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true@@ -38,6 +38,23 @@ function statuses(result: ReturnType<typeof render>['result']): string[] {return result.current.composerFileAttachments.map(file => file.status)}+function refusalTexts(result: ReturnType<typeof render>['result']): string[] {+ return result.current.composerFileRefusals.map(refusal => refusal.text)+}++/** A prepared image whose bytes differ from every other index. */+function image(index: number): ComposerImageAttachment {+ const dataBase64 = btoa(`image ${index}`)+ return {+ id: `img-${index}`,+ name: `image-${index}.png`,+ mimeType: 'image/png',+ dataBase64,+ sizeBytes: `image ${index}`.length,+ previewDataUrl: `data:image/png;base64,${dataBase64}`,+ }+}+describe('useComposerAttachments — documents (#678)', () => {it('shows a file as reading at once and as ready once it is read and hashed', async () => {const { result, clearSendError } = render()@@ -264,18 +281,8 @@ describe('useComposerAttachments — documents (#678)', () => {(hook: ReturnType<typeof render>) => hook.rerender({ agent: 'agent-other' }),],[- 'an image attach',- (hook: ReturnType<typeof render>) =>- hook.result.current.handleAddComposerImageAttachments([- {- id: 'img-1',- name: 'a.png',- mimeType: 'image/png',- dataBase64: 'aGVsbG8=',- sizeBytes: 5,- previewDataUrl: 'data:image/png;base64,aGVsbG8=',- },- ]),+ 'an attach with no documents',+ (hook: ReturnType<typeof render>) => hook.result.current.handleAddComposerFiles([], ''),],])('clears the refusal on %s', (_label, clear) => {const hook = render()@@ -292,6 +299,47 @@ describe('useComposerAttachments — documents (#678)', () => {expect(hook.result.current.composerFileRefusals).toEqual([])})+ it('keeps the refusal of a document when the same gesture also attaches an image', () => {+ const { result } = render()+ const huge = textFile('huge.bin', 'x')+ Object.defineProperty(huge, 'size', { configurable: true, value: COMPOSER_MAX_FILE_BYTES + 1 })++ // One drop: the panel routes the documents first, then the images.+ act(() => {+ result.current.handleAddComposerFiles([huge], '')+ result.current.handleAddComposerImageAttachments([image(1)])+ })++ // Witness: the image of the same gesture was attached.+ expect(result.current.composerImageAttachments.map(item => item.id)).toEqual(['img-1'])+ expect(refusalTexts(result)).toHaveLength(1)+ expect(refusalTexts(result)[0]).toMatch(/^huge\.bin is .* at most 11\.0 MiB/)+ })++ it('refuses with a notice an image that arrives after the last free slot was taken', async () => {+ const { result } = render()+ act(() => {+ result.current.handleAddComposerImageAttachments(+ Array.from({ length: COMPOSER_MAX_ATTACHMENTS - 1 }, (_, index) => image(index))+ )+ })+ // Twin: 19 images are attached and one slot is free.+ expect(result.current.composerImageAttachments).toHaveLength(COMPOSER_MAX_ATTACHMENTS - 1)++ act(() => {+ result.current.handleAddComposerFiles([textFile('doc.txt', 'doc')], '')+ result.current.handleAddComposerImageAttachments([+ { ...image(COMPOSER_MAX_ATTACHMENTS), name: 'late.png' },+ ])+ })++ await waitFor(() => expect(statuses(result)).toEqual(['ready']))+ expect(result.current.composerImageAttachments).toHaveLength(COMPOSER_MAX_ATTACHMENTS - 1)+ expect(refusalTexts(result)).toEqual([+ `"late.png" was not attached: a message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`,+ ])+ })+it('clears the documents when the selected agent changes', async () => {const { result, rerender } = render('agent-x')act(() => {@@ -339,10 +387,6 @@ describe('useComposerAttachments — documents (#678)', () => {})}- function refusalTexts(result: ReturnType<typeof render>['result']): string[] {- return result.current.composerFileRefusals.map(refusal => refusal.text)- }-it('counts the selected references and the agent against the 6 MiB share', () => {const { result } = render()const references = globalReferences(4, () => `${'文'.repeat(251)}.txt`)diff --git a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts b/desktop-app/ui/src/hooks/domain/useComposerAttachments.tsindex 543faa5f9..1b32ce142 100644--- a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts+++ b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts@@ -52,7 +52,6 @@ export function useComposerAttachments({// that started them.const composerFilesRef = useRef<ComposerFileAttachment[]>([])const composerImagesRef = useRef<ComposerImageAttachment[]>([])- composerImagesRef.current = composerImageAttachmentsconst composerReferencesRef = useRef<ComposerReferenceAttachment[]>([])composerReferencesRef.current = composerReferenceAttachments@@ -65,6 +64,17 @@ export function useComposerAttachments({[])+ // Images and files share one per-message count, so both mirrors must hold+ // what a gesture has already added before its next attach is decided.+ const commitComposerImages = useCallback(+ (update: (previous: ComposerImageAttachment[]) => ComposerImageAttachment[]) => {+ const next = update(composerImagesRef.current)+ composerImagesRef.current = next+ setComposerImageAttachments(next)+ },+ []+ )+const revokeComposerPreviewUrls = useCallback((attachments: ComposerImageAttachment[]) => {composerAttachmentRevisionRef.current += 1const canRevokeObjectUrl =@@ -82,11 +92,11 @@ export function useComposerAttachments({const clearComposerImageAttachments = useCallback(() => {composerAttachmentRevisionRef.current += 1- setComposerImageAttachments(previous => {+ commitComposerImages(previous => {revokeComposerPreviewUrls(previous)return []})- }, [revokeComposerPreviewUrls])+ }, [commitComposerImages, revokeComposerPreviewUrls])const clearComposerFileAttachments = useCallback(() => {composerAttachmentRevisionRef.current += 1@@ -124,14 +134,19 @@ export function useComposerAttachments({(attachments: ComposerImageAttachment[]) => {composerAttachmentRevisionRef.current += 1if (!attachments.length) return- // A new attach replaces the notice about documents refused earlier.- setComposerFileRefusals([])- setComposerImageAttachments(previous => {+ // The documents of the same gesture were routed first and replaced the+ // notices; an image refused here is added to them, never replaces them.+ const refusals: ComposerFileRefusal[] = []+ commitComposerImages(previous => {const next = [...previous]for (const attachment of attachments) {// Images and files share one per-message count.if (next.length + composerFilesRef.current.length >= COMPOSER_MAX_ATTACHMENTS) {revokeComposerPreviewUrls([attachment])+ refusals.push({+ id: attachment.id,+ text: `"${attachment.name}" was not attached: a message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`,+ })continue}const duplicate = next.some(@@ -152,15 +167,16 @@ export function useComposerAttachments({}return next})+ if (refusals.length) setComposerFileRefusals(previous => [...previous, ...refusals])clearSendError()},- [clearSendError, revokeComposerPreviewUrls]+ [clearSendError, commitComposerImages, revokeComposerPreviewUrls])const handleUpdateComposerImageAttachment = useCallback((attachment: ComposerImageAttachment) => {composerAttachmentRevisionRef.current += 1- setComposerImageAttachments(previous => {+ commitComposerImages(previous => {const index = previous.findIndex(item => item.id === attachment.id)if (index === -1) {revokeComposerPreviewUrls([attachment])@@ -176,13 +192,13 @@ export function useComposerAttachments({})clearSendError()},- [clearSendError, revokeComposerPreviewUrls]+ [clearSendError, commitComposerImages, revokeComposerPreviewUrls])const handleRemoveComposerImageAttachment = useCallback((attachmentId: string) => {composerAttachmentRevisionRef.current += 1- setComposerImageAttachments(previous => {+ commitComposerImages(previous => {const removed = previous.filter(att => att.id === attachmentId)if (removed.length) {revokeComposerPreviewUrls(removed)@@ -190,21 +206,26 @@ export function useComposerAttachments({return previous.filter(att => att.id !== attachmentId)})},- [revokeComposerPreviewUrls]+ [commitComposerImages, revokeComposerPreviewUrls])/*** Adds picked documents (#678). Each accepted file appears at once as* `reading` and becomes `ready` when its bytes are read and hashed. A file* that breaks a limit, or cannot be read, gets no chip: its reason is shown- * as a refusal notice instead, so a refusal is never silent. Each attach- * replaces the refusals of the previous one.+ * as a refusal notice instead, so a refusal is never silent. The composer+ * calls this once per gesture, with `[]` when the gesture carried only+ * images, so each gesture replaces the refusals of the previous one; images+ * of the same gesture refused later add to them.*/const handleAddComposerFiles = useCallback((files: File[], draft: string) => {composerAttachmentRevisionRef.current += 1- if (!files.length) returnconst refusals: ComposerFileRefusal[] = []+ if (!files.length) {+ setComposerFileRefusals(refusals)+ return+ }// Admission measures the request the files would be sent with: the// content with its references section, the structured references and the// agent it is posted to. Without an agent, or with references the send