Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): polish image previews and error-only resend
Mc834fa529fb1
diff --git a/desktop-app/ui/src/components/GfsImagePreview/Body.tsx b/desktop-app/ui/src/components/GfsImagePreview/Body.tsxindex 67cc4276f..0f175053b 100644--- a/desktop-app/ui/src/components/GfsImagePreview/Body.tsx+++ b/desktop-app/ui/src/components/GfsImagePreview/Body.tsx@@ -5,6 +5,7 @@ import { GFS_IMAGE_PREVIEW_MAX_BYTES } from '@constants/gfsImagePreview'import { estimateBase64DecodedLength } from '@lib/base64Size'import { describeGfsReadError } from '@lib/gfsGrantErrors'import { assertGfsImagePreviewSize } from '@lib/gfsImagePreview'+import { copyImageBlobToClipboard } from '@lib/imageClipboard'import type { GfsImagePreviewBodyProps } from './types'/**@@ -126,40 +127,17 @@ export function GfsImagePreviewBody({async function copyImageToClipboard(): Promise<void> {if (!sourceBlob || !mountedRef.current) returntry {- if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {- let clipboardBlob: Blob | null = sourceBlob- if (!sourceBlob.type.includes('png')) {- clipboardBlob = await convertBlobToPng(sourceBlob)- }- if (!mountedRef.current) return- if (clipboardBlob) {- await navigator.clipboard.write([new ClipboardItem({ 'image/png': clipboardBlob })])- if (!mountedRef.current) return- markCopyState('copied')- return- }- }- if (navigator.clipboard?.writeText) {- const dataUrl = await blobToDataUrl(sourceBlob)- if (!mountedRef.current) return- await navigator.clipboard.writeText(dataUrl)- if (!mountedRef.current) return- markCopyState('copied')- } else {- markCopyState('error')- }+ const copied = await copyImageBlobToClipboard(sourceBlob, () => mountedRef.current)+ if (copied && mountedRef.current) markCopyState('copied')} catch {- markCopyState('error')+ if (mountedRef.current) markCopyState('error')}}return (<><header className="da-gfs-image-preview-dialog__header">- <HeadingTag className="da-gfs-preview-title" id={headingId}>- {fileName}- </HeadingTag>- <div className="da-gfs-image-preview-dialog__header-actions">+ <div className="da-gfs-image-preview-dialog__header-main"><ButtonclassName="da-gfs-image-preview-dialog__copy"aria-label={@@ -175,8 +153,11 @@ export function GfsImagePreviewBody({{copyState === 'copied' ? 'Copied' : 'Copy'}</span></Button>- {headerActions}+ <HeadingTag className="da-gfs-preview-title" id={headingId}>+ {fileName}+ </HeadingTag></div>+ <div className="da-gfs-image-preview-dialog__header-actions">{headerActions}</div></header><div className="da-gfs-image-preview-dialog__body">{previewError ? <StatusBanner tone="error" text={previewError} /> : null}@@ -206,30 +187,3 @@ function decodeBase64ToArrayBuffer(dataBase64: string): ArrayBuffer {}return bytes.buffer}--async function convertBlobToPng(blob: Blob): Promise<Blob | null> {- if (typeof createImageBitmap === 'undefined') return null- try {- const bitmap = await createImageBitmap(blob)- const canvas = document.createElement('canvas')- canvas.width = bitmap.width- canvas.height = bitmap.height- const ctx = canvas.getContext('2d')- if (!ctx) return null- ctx.drawImage(bitmap, 0, 0)- return await new Promise<Blob | null>(resolve => canvas.toBlob(resolve, 'image/png'))- } catch {- return null- }-}--async function blobToDataUrl(blob: Blob): Promise<string> {- const result = await new Promise<string | ArrayBuffer | null>((resolve, reject) => {- const reader = new FileReader()- reader.onload = () => resolve(reader.result)- reader.onerror = () => reject(reader.error ?? new Error('Could not read the image'))- reader.readAsDataURL(blob)- })- if (typeof result !== 'string') throw new Error('Could not read the image')- return result-}diff --git a/desktop-app/ui/src/components/GfsImagePreview/GfsImagePreview.test.tsx b/desktop-app/ui/src/components/GfsImagePreview/GfsImagePreview.test.tsxindex 64eba5421..3f256b5a8 100644--- a/desktop-app/ui/src/components/GfsImagePreview/GfsImagePreview.test.tsx+++ b/desktop-app/ui/src/components/GfsImagePreview/GfsImagePreview.test.tsx@@ -80,6 +80,12 @@ describe('GfsImagePreview layout', () => {const closeButton = screen.getByRole('button', { name: 'Close image preview' })expect(closeButton.classList.contains('ui-button')).toBe(true)expect(closeButton.classList.contains('da-gfs-image-preview-dialog__close')).toBe(true)+ const headerMain = screen+ .getByRole('dialog', { name: 'diagram.png' })+ .querySelector('.da-gfs-image-preview-dialog__header-main')+ expect(headerMain?.contains(copyButton)).toBe(true)+ expect(headerMain?.firstElementChild).toBe(copyButton)+ expect(headerMain?.contains(closeButton)).toBe(false)expect((modal as HTMLElement).style.left).toBe('74px')expect((modal as HTMLElement).style.right).toBe('0px')diff --git a/desktop-app/ui/src/components/agents/AnnotationCanvas.tsx b/desktop-app/ui/src/components/agents/AnnotationCanvas.tsxindex 1c0ba77d1..e7f73d283 100644--- a/desktop-app/ui/src/components/agents/AnnotationCanvas.tsx+++ b/desktop-app/ui/src/components/agents/AnnotationCanvas.tsx@@ -1,6 +1,8 @@import React, { useCallback, useEffect, useRef, useState } from 'react'import { createPortal } from 'react-dom'import { Button, IconButton } from '@components/Common'+import { IconCopy, IconEdit } from '@components/SidebarNav/icons'+import { copyImageBlobToClipboard } from '@lib/imageClipboard'import type { ComposerImageAttachment } from '../../uiTypes'const COLOR_INPUT_FALLBACK = `#${'0'.repeat(6)}`@@ -44,9 +46,25 @@ export function AnnotationCanvas({ attachment, onSave, onClose }: AnnotationCanvconst [annotationBrushPx, setAnnotationBrushPx] = useState(5)const [annotationBusy, setAnnotationBusy] = useState(false)const [annotationError, setAnnotationError] = useState<string | null>(null)+ const [copyBusy, setCopyBusy] = useState(false)+ const [copyState, setCopyState] = useState<'idle' | 'copied' | 'error'>('idle')const annotationCanvasRef = useRef<HTMLCanvasElement | null>(null)const annotationPointerActiveRef = useRef(false)const annotationLastPointRef = useRef<{ x: number; y: number } | null>(null)+ const mountedRef = useRef(true)++ useEffect(() => {+ mountedRef.current = true+ return () => {+ mountedRef.current = false+ }+ }, [])++ useEffect(() => {+ if (copyState === 'idle') return+ const timeout = window.setTimeout(() => setCopyState('idle'), 2000)+ return () => window.clearTimeout(timeout)+ }, [copyState])const loadImage = useCallback((src: string) => {return new Promise<HTMLImageElement>((resolve, reject) => {@@ -193,6 +211,29 @@ export function AnnotationCanvas({ attachment, onSave, onClose }: AnnotationCanv}}, [onSave, attachment, readBlobAsDataUrl])+ const handleCopyImage = useCallback(async () => {+ if (copyBusy) return+ setCopyBusy(true)+ setCopyState('idle')+ try {+ const binary = window.atob(attachment.dataBase64)+ const buffer = new ArrayBuffer(binary.length)+ const bytes = new Uint8Array(buffer)+ for (let index = 0; index < binary.length; index += 1) {+ bytes[index] = binary.charCodeAt(index)+ }+ const copied = await copyImageBlobToClipboard(+ new Blob([buffer], { type: attachment.mimeType }),+ () => mountedRef.current+ )+ if (copied && mountedRef.current) setCopyState('copied')+ } catch {+ if (mountedRef.current) setCopyState('error')+ } finally {+ if (mountedRef.current) setCopyBusy(false)+ }+ }, [attachment.dataBase64, attachment.mimeType, copyBusy])+return createPortal(<div className="composer-image-preview-overlay" onClick={onClose} role="presentation"><div@@ -213,17 +254,43 @@ export function AnnotationCanvas({ attachment, onSave, onClose }: AnnotationCanv</IconButton><div className="composer-image-preview-actions">{!previewIsAnnotating ? (- <Button- color="neutral"- onClick={() => {- setAnnotationError(null)- setPreviewIsAnnotating(true)- }}- size="xs"- variant="ghost"- >- Annotate- </Button>+ <>+ <Button+ color="neutral"+ onClick={() => {+ setAnnotationError(null)+ setPreviewIsAnnotating(true)+ }}+ size="xs"+ variant="ghost"+ >+ <IconEdit width={16} height={16} />+ Annotate+ </Button>+ <Button+ aria-label={+ copyState === 'copied'+ ? 'Copied image to clipboard'+ : copyState === 'error'+ ? 'Could not copy image'+ : 'Copy image to clipboard'+ }+ color="neutral"+ disabled={copyBusy}+ onClick={() => void handleCopyImage()}+ size="xs"+ variant="ghost"+ >+ <IconCopy width={16} height={16} />+ <span aria-live="polite">+ {copyState === 'copied'+ ? 'Copied'+ : copyState === 'error'+ ? 'Copy failed'+ : 'Copy'}+ </span>+ </Button>+ </>) : (<><label className="composer-image-preview-color">diff --git a/desktop-app/ui/src/components/agents/ChatThread.tsx b/desktop-app/ui/src/components/agents/ChatThread.tsxindex 7448c10bd..306e3e60c 100644--- a/desktop-app/ui/src/components/agents/ChatThread.tsx+++ b/desktop-app/ui/src/components/agents/ChatThread.tsx@@ -748,13 +748,13 @@ export function ChatThread({ showAgentLabel = false, onScrollPositionChange }: C/\b[a-zA-Z0-9][a-zA-Z0-9._-]*\.html?\b/i.test(displayContent)const showMetaRow = message.role === 'assistant' || message.role === 'user'const copyContent = message.role === 'user' ? displayContent : message.content- // TASK-42: a user message resends itself; an assistant- // message (including error bubbles) resends the user- // prompt that produced it.- const resendSource =- message.role === 'user'+ // Only error bubbles offer retry. An assistant error+ // restores the user prompt that produced it.+ const resendSource = message.isError+ ? message.role === 'user'? message: findNearestPrecedingUserMessage(groupedWithKeys, groupIndex)+ : nullconst metaRowRoleClass =message.role === 'user'? 'chat-message-meta-row--user'diff --git a/desktop-app/ui/src/components/agents/__tests__/AnnotationCanvas.test.tsx b/desktop-app/ui/src/components/agents/__tests__/AnnotationCanvas.test.tsxindex a3d0c5f2c..1413ebdce 100644--- a/desktop-app/ui/src/components/agents/__tests__/AnnotationCanvas.test.tsx+++ b/desktop-app/ui/src/components/agents/__tests__/AnnotationCanvas.test.tsx@@ -1,7 +1,8 @@// @vitest-environment jsdomimport { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'import type { ComposerImageAttachment } from '../../../uiTypes'-import { commitAnnotatedPreview } from '../AnnotationCanvas'+import { AnnotationCanvas, commitAnnotatedPreview } from '../AnnotationCanvas'const attachment = (previewDataUrl: string): ComposerImageAttachment => ({id: 'att-1',@@ -57,3 +58,49 @@ describe('commitAnnotatedPreview', () => {expect(revoke).not.toHaveBeenCalled()})})++describe('AnnotationCanvas copy action', () => {+ const originalClipboard = Object.getOwnPropertyDescriptor(navigator, 'clipboard')++ afterEach(() => {+ cleanup()+ vi.unstubAllGlobals()+ if (originalClipboard) {+ Object.defineProperty(navigator, 'clipboard', originalClipboard)+ } else {+ Reflect.deleteProperty(navigator, 'clipboard')+ }+ })++ it('copies the attached image bytes and confirms success', async () => {+ const write = vi.fn(async () => undefined)+ const clipboardItems: Array<Record<string, Blob>> = []+ vi.stubGlobal(+ 'ClipboardItem',+ class {+ constructor(data: Record<string, Blob>) {+ clipboardItems.push(data)+ }+ }+ )+ Object.defineProperty(navigator, 'clipboard', {+ configurable: true,+ value: { write },+ })++ render(+ <AnnotationCanvas+ attachment={attachment('data:image/png;base64,AAAA')}+ onSave={vi.fn()}+ onClose={vi.fn()}+ />+ )++ expect(screen.getByRole('button', { name: 'Annotate' }).querySelector('svg')).not.toBeNull()+ fireEvent.click(screen.getByRole('button', { name: 'Copy image to clipboard' }))++ await waitFor(() => expect(write).toHaveBeenCalledTimes(1))+ expect(clipboardItems[0]?.['image/png']?.size).toBe(3)+ expect(screen.getByRole('button', { name: 'Copied image to clipboard' })).toBeTruthy()+ })+})diff --git a/desktop-app/ui/src/components/agents/__tests__/ChatThread.resend.test.tsx b/desktop-app/ui/src/components/agents/__tests__/ChatThread.resend.test.tsxindex c893b1d82..080c49df5 100644--- a/desktop-app/ui/src/components/agents/__tests__/ChatThread.resend.test.tsx+++ b/desktop-app/ui/src/components/agents/__tests__/ChatThread.resend.test.tsx@@ -115,9 +115,20 @@ function userMessageWithAttachments(): AgentChatMessage {}}-function renderWithUserMessage(user: AgentChatMessage) {- messages = [user]- groupedMessages = [{ role: 'user', items: [user] }]+function renderWithErrorReply(user: AgentChatMessage) {+ const errorReply: AgentChatMessage = {+ id: 'assistant-error',+ role: 'assistant',+ content: 'The agent could not finish this request.',+ timestamp: 2,+ isError: true,+ errorCode: 'LLM_MODEL_OVERLOADED',+ }+ messages = [user, errorReply]+ groupedMessages = [+ { role: 'user', items: [user] },+ { role: 'assistant', items: [errorReply] },+ ]render(<ChatThread />)}@@ -134,10 +145,11 @@ afterEach(() => {})describe('ChatThread resend action (TASK-42)', () => {- it('re-populates the composer draft and re-attaches files + plugin indicators from a user message', () => {- renderWithUserMessage(userMessageWithAttachments())+ it('restores the original prompt and attachments from an assistant error', () => {+ renderWithErrorReply(userMessageWithAttachments())- fireEvent.click(screen.getByRole('button', { name: 'Resend message' }))+ expect(screen.queryByRole('button', { name: 'Resend message' })).toBeNull()+ fireEvent.click(screen.getByRole('button', { name: 'Resend prompt' }))expect(getComposerDraft('chat-1')).toBe('Summarize the chart with the plugin')expect(addComposerImageAttachments).toHaveBeenCalledTimes(1)@@ -164,7 +176,7 @@ describe('ChatThread resend action (TASK-42)', () => {expect(requestComposerFocus).toHaveBeenCalledTimes(1)})- it('resending from an assistant message re-issues the originating user prompt (text + attachments + indicators)', () => {+ it('hides resend on successful user and assistant messages', () => {const user = userMessageWithAttachments()const assistant: AgentChatMessage = {id: 'assistant-1',@@ -191,27 +203,17 @@ describe('ChatThread resend action (TASK-42)', () => {render(<ChatThread />)- fireEvent.click(screen.getByRole('button', { name: 'Resend prompt' }))-- // The draft carries the USER prompt, not the assistant reply.- expect(getComposerDraft('chat-1')).toBe('Summarize the chart with the plugin')- expect(addComposerImageAttachments).toHaveBeenCalledTimes(1)- expect(addComposerImageAttachments.mock.calls[0]![0]).toEqual([- expect.objectContaining({ name: 'chart.png', dataBase64: PNG_BASE64 }),- ])- expect(addComposerReferenceAttachments).toHaveBeenCalledTimes(1)- expect(addComposerReferenceAttachments.mock.calls[0]![0]).toEqual([- expect.objectContaining({ type: 'plugin', namespace: 'profits', name: 'revenue' }),- ])- expect(requestComposerFocus).toHaveBeenCalledTimes(1)+ expect(screen.queryByRole('button', { name: 'Resend prompt' })).toBeNull()+ expect(screen.queryByRole('button', { name: 'Resend message' })).toBeNull()})- it('hides the resend action on an assistant message with no preceding user prompt', () => {+ it('hides resend on an error with no preceding user prompt', () => {const assistant: AgentChatMessage = {id: 'assistant-only',role: 'assistant',content: 'A reply with no user turn before it.',timestamp: 1,+ isError: true,}messages = [assistant]groupedMessages = [{ role: 'assistant', items: [assistant] }]@@ -248,9 +250,9 @@ describe('ChatThread resend action (TASK-42)', () => {describe('dirty-composer contract (PR #859 review: refuse like handleRecoverFailedAgentSend)', () => {it('refuses with an error toast when the composer holds draft text', () => {setComposerDraft('chat-1', 'a half-written draft')- renderWithUserMessage(userMessageWithAttachments())+ renderWithErrorReply(userMessageWithAttachments())- fireEvent.click(screen.getByRole('button', { name: 'Resend message' }))+ fireEvent.click(screen.getByRole('button', { name: 'Resend prompt' }))expect(pushToast).toHaveBeenCalledTimes(1)expect(pushToast.mock.calls[0]![0]).toBe(@@ -275,9 +277,9 @@ describe('ChatThread resend action (TASK-42)', () => {previewDataUrl: 'data:image/png;base64,cGVuZGluZw==',},]- renderWithUserMessage(userMessageWithAttachments())+ renderWithErrorReply(userMessageWithAttachments())- fireEvent.click(screen.getByRole('button', { name: 'Resend message' }))+ fireEvent.click(screen.getByRole('button', { name: 'Resend prompt' }))expect(pushToast).toHaveBeenCalledTimes(1)expect(pushToast.mock.calls[0]![1]).toBe('error')@@ -298,9 +300,9 @@ describe('ChatThread resend action (TASK-42)', () => {{ id: 'legacy-2', type: 'uploaded_file', label: 'older-shot.png', addedOrder: 1 },],}- renderWithUserMessage(user)+ renderWithErrorReply(user)- fireEvent.click(screen.getByRole('button', { name: 'Resend message' }))+ fireEvent.click(screen.getByRole('button', { name: 'Resend prompt' }))// The resend itself still happens (text restored, focus nudged)…expect(getComposerDraft('chat-1')).toBe('Analyze these')@@ -337,8 +339,8 @@ describe('ChatThread resend action (TASK-42)', () => {expect(merged.map(message => message.id)).toEqual(['optimistic-user', 'turn-10-user'])expect(merged[0]?.attachments?.map(attachment => attachment.type)).toEqual(['uploaded_file'])expect(merged[1]?.attachments?.map(attachment => attachment.type)).toEqual(['plugin'])- renderWithUserMessage(merged[0]!)- fireEvent.click(screen.getByRole('button', { name: 'Resend message' }))+ renderWithErrorReply(merged[0]!)+ fireEvent.click(screen.getByRole('button', { name: 'Resend prompt' }))expect(pushToast).toHaveBeenCalledWith("1 attachment from the original message couldn't be restored.",'warn'diff --git a/desktop-app/ui/src/lib/imageClipboard.ts b/desktop-app/ui/src/lib/imageClipboard.tsnew file mode 100644index 000000000..049ce0da6--- /dev/null+++ b/desktop-app/ui/src/lib/imageClipboard.ts@@ -0,0 +1,51 @@+/** Copy image bytes as PNG when the browser supports image clipboard writes. */+export async function copyImageBlobToClipboard(+ sourceBlob: Blob,+ isActive: () => boolean = () => true+): Promise<boolean> {+ if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {+ const clipboardBlob =+ sourceBlob.type === 'image/png' ? sourceBlob : await convertBlobToPng(sourceBlob)+ if (!isActive()) return false+ if (clipboardBlob) {+ await navigator.clipboard.write([new ClipboardItem({ 'image/png': clipboardBlob })])+ return true+ }+ }++ if (navigator.clipboard?.writeText) {+ const dataUrl = await blobToDataUrl(sourceBlob)+ if (!isActive()) return false+ await navigator.clipboard.writeText(dataUrl)+ return true+ }++ throw new Error('Image clipboard unavailable')+}++async function convertBlobToPng(blob: Blob): Promise<Blob | null> {+ if (typeof createImageBitmap === 'undefined') return null+ try {+ const bitmap = await createImageBitmap(blob)+ const canvas = document.createElement('canvas')+ canvas.width = bitmap.width+ canvas.height = bitmap.height+ const ctx = canvas.getContext('2d')+ if (!ctx) return null+ ctx.drawImage(bitmap, 0, 0)+ return await new Promise<Blob | null>(resolve => canvas.toBlob(resolve, 'image/png'))+ } catch {+ return null+ }+}++async function blobToDataUrl(blob: Blob): Promise<string> {+ const result = await new Promise<string | ArrayBuffer | null>((resolve, reject) => {+ const reader = new FileReader()+ reader.onload = () => resolve(reader.result)+ reader.onerror = () => reject(reader.error ?? new Error('Could not read the image'))+ reader.readAsDataURL(blob)+ })+ if (typeof result !== 'string') throw new Error('Could not read the image')+ return result+}diff --git a/desktop-app/ui/src/styles.css b/desktop-app/ui/src/styles.cssindex 10e13e6a5..7f8abca23 100644--- a/desktop-app/ui/src/styles.css+++ b/desktop-app/ui/src/styles.css@@ -8062,6 +8062,21 @@ h3 {outline-offset: 2px;}+/* Match the Settings menu hover across both themes. Highlight the chip frame+ only while its preview button is hovered, leaving the remove action alone. */+.message-attachment-chip:has(> .message-attachment-preview-trigger:hover),+.composer-attachment-chip:has(> .composer-attachment-preview-trigger:hover) {+ background: var(--list-item-hover-bg);+ border-color: rgba(var(--accent-rgb), 0.34);+ color: var(--text);+}++.message-attachment-preview-trigger:hover:enabled,+.composer-attachment-preview-trigger:hover:enabled {+ background: transparent;+ color: inherit;+}+.composer-attachment-chip img {width: 28px;height: 28px;@@ -8284,13 +8299,17 @@ h3 {padding: var(--space-5);}+:root[data-theme='light'] .composer-image-preview-overlay {+ background: rgba(0, 0, 0, 0.72);+}+.composer-image-preview-dialog {position: relative;width: min(96vw, 1120px);max-height: 92vh;border-radius: var(--radius-md);border: 1px solid rgba(var(--edge-rgb), 0.45);- background: rgba(var(--surface-rgb), 0.97);+ background: var(--panel);box-shadow: var(--shadow-hard);padding: var(--space-4);display: grid;@@ -8332,7 +8351,7 @@ h3 {max-height: calc(92vh - 120px);object-fit: contain;border-radius: var(--radius-sm);- background: rgba(var(--surface-alt-rgb), 0.45);+ background: var(--media-surface);}.composer-image-preview-canvas {@@ -8340,7 +8359,7 @@ h3 {width: 100%;max-height: calc(92vh - 170px);border-radius: var(--radius-sm);- background: rgba(var(--surface-alt-rgb), 0.45);+ background: var(--media-surface);touch-action: none;cursor: crosshair;}@@ -15694,6 +15713,21 @@ pre {gap: var(--space-2);}+.da-gfs-image-preview-dialog__header-main {+ display: flex;+ align-items: center;+ gap: var(--space-2);+ min-width: 0;+}++.da-gfs-image-preview-dialog__header-main .da-gfs-preview-title {+ flex: 0 1 auto;+}++.da-gfs-image-preview-dialog__header-main .da-gfs-image-preview-dialog__copy {+ flex: 0 0 auto;+}+.da-gfs-image-preview-dialog__close.ui-button,.da-gfs-markdown-preview-dialog__close.ui-button,.da-gfs-video-preview-dialog__close.ui-button {