Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(desktop): upload any file from the agent chat composer as an inline attachment

Alfredo Lopezfe906d6e78a3
diff --git a/desktop-app/src/__tests__/ipc.fileAttachments.test.ts b/desktop-app/src/__tests__/ipc.fileAttachments.test.ts
new file mode 100644
index 000000000..b87dbc148
--- /dev/null
+++ b/desktop-app/src/__tests__/ipc.fileAttachments.test.ts
@@ -0,0 +1,128 @@
+/**
+ * Issue #678 — the IPC boundary checks the shape of an inline `kind:'file'`
+ * attachment and refuses a malformed one before it reaches the host. Image
+ * attachments keep the contract they always had.
+ */
+import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
+import type { AppService } from '../appService.js'
+
+type Handler = (event: unknown, payload: unknown) => Promise<unknown>
+
+const electron = vi.hoisted(() => ({ handlers: new Map<string, Handler>() }))
+
+vi.mock('electron', () => ({
+ BrowserWindow: { fromWebContents: vi.fn(), getAllWindows: vi.fn(() => []) },
+ Notification: vi.fn(),
+ app: { on: vi.fn(), getPath: vi.fn(), getVersion: vi.fn() },
+ clipboard: { writeText: vi.fn() },
+ dialog: { showSaveDialog: vi.fn() },
+ ipcMain: {
+ handle: vi.fn((channel: string, handler: Handler) => {
+ electron.handlers.set(channel, handler)
+ }),
+ on: vi.fn(),
+ removeHandler: vi.fn(),
+ },
+}))
+// ipc.ts only uses AppService as a type at registration; the real module would
+// pull in the whole main-process graph.
+vi.mock('../appService.js', () => ({ AppService: class {} }))
+
+const service = {
+ invokeHostMessage: vi.fn(async (..._args: unknown[]) => ({ taskId: 'task-1' })),
+}
+const trusted = { senderFrame: { url: 'file:///app/index.html' } }
+
+function fileAttachment(overrides: Record<string, unknown> = {}): Record<string, unknown> {
+ return {
+ id: 'file-1',
+ kind: 'file',
+ filename: 'notes.txt',
+ mimeType: 'text/plain',
+ detectedMediaType: 'text/plain',
+ encoding: 'base64',
+ dataBase64: 'aGVsbG8=',
+ sizeBytes: 5,
+ digest: {
+ algorithm: 'sha256',
+ hex: '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824',
+ },
+ ...overrides,
+ }
+}
+
+function send(attachments: unknown) {
+ const registered = electron.handlers.get('rpc:invokeHostMessage')
+ if (!registered) throw new Error('no IPC handler registered for rpc:invokeHostMessage')
+ return registered(trusted, { hostRef: 'agent-x', payload: { content: 'hi', attachments } })
+}
+
+beforeAll(async () => {
+ const { registerIpcHandlers } = await import('../ipc.js')
+ registerIpcHandlers(service as unknown as AppService)
+})
+
+afterEach(() => {
+ service.invokeHostMessage.mockClear()
+})
+
+describe('rpc:invokeHostMessage file attachments', () => {
+ it('forwards a well-formed file attachment untouched', async () => {
+ const attachments = [fileAttachment()]
+ await send(attachments)
+ expect(service.invokeHostMessage).toHaveBeenCalledTimes(1)
+ const request = service.invokeHostMessage.mock.calls[0]?.[1] as Record<string, unknown>
+ expect(request.attachments).toEqual(attachments)
+ })
+
+ it('accepts an empty declared media type, which the host allows', async () => {
+ await send([fileAttachment({ mimeType: '' })])
+ expect(service.invokeHostMessage).toHaveBeenCalledTimes(1)
+ })
+
+ it('leaves image attachments to the contract they always had', async () => {
+ const image = {
+ id: 'img-1',
+ kind: 'image',
+ mimeType: 'image/png',
+ encoding: 'base64',
+ dataBase64: 'iVBORw0KGgo=',
+ }
+ await send([image])
+ expect(service.invokeHostMessage).toHaveBeenCalledTimes(1)
+ const request = service.invokeHostMessage.mock.calls[0]?.[1] as Record<string, unknown>
+ expect(request.attachments).toEqual([image])
+ })
+
+ it.each([
+ ['id', { id: ' ' }],
+ ['filename', { filename: '' }],
+ ['mimeType', { mimeType: 5 }],
+ ['detectedMediaType', { detectedMediaType: undefined }],
+ ['encoding', { encoding: 'utf8' }],
+ ['dataBase64', { dataBase64: 42 }],
+ ['sizeBytes', { sizeBytes: -1 }],
+ ['sizeBytes', { sizeBytes: 1.5 }],
+ ['digest', { digest: { algorithm: 'md5', hex: 'a'.repeat(64) } }],
+ ['digest', { digest: { algorithm: 'sha256', hex: 'not-hex' } }],
+ ['digest', { digest: undefined }],
+ ])('rejects a file attachment with a bad %s before it reaches the host', async (field, patch) => {
+ await expect(
+ send([fileAttachment(), fileAttachment({ id: 'file-2', ...patch })])
+ ).rejects.toThrow(
+ `Invalid host message request: COMPOSER_FILE_ATTACHMENT_INVALID attachments[1].${field}`
+ )
+ expect(service.invokeHostMessage).toHaveBeenCalledTimes(0)
+ // Control: the same request without the bad entry goes through.
+ await send([fileAttachment()])
+ expect(service.invokeHostMessage).toHaveBeenCalledTimes(1)
+ })
+
+ it('rejects an attachments field that is not a list', async () => {
+ await expect(send({ id: 'x' })).rejects.toThrow('Invalid host message request: attachments')
+ expect(service.invokeHostMessage).toHaveBeenCalledTimes(0)
+ // Control: a list goes through.
+ await send([])
+ expect(service.invokeHostMessage).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/desktop-app/src/ipc.ts b/desktop-app/src/ipc.ts
index e5540b7c3..6169c1b8d 100644
--- a/desktop-app/src/ipc.ts
+++ b/desktop-app/src/ipc.ts
@@ -25,6 +25,7 @@ import {
import {
DesktopRuntimeConfig,
HostActivityStreamEvent,
+ HostMessageAttachment,
HostMessageRequest,
HostStatusStreamEvent,
TaskProgressStreamEvent,
@@ -154,6 +155,54 @@ function isSelectionRevision(value: unknown): value is number {
return Number.isSafeInteger(value) && (value as number) >= 0
}
+const SHA256_HEX = /^[0-9a-f]{64}$/
+
+function isPlainObject(value: unknown): value is Record<string, unknown> {
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
+}
+
+/**
+ * Checks the shape of every `kind:'file'` attachment (#678). mcp-host enforces
+ * the size limits and recomputes the size and the digest; an image entry keeps
+ * the contract it always had and is not inspected here.
+ */
+function parseHostMessageAttachments(attachments: unknown): HostMessageAttachment[] {
+ if (!Array.isArray(attachments)) {
+ throw new Error('Invalid host message request: attachments')
+ }
+ attachments.forEach((entry, index) => {
+ if (!isPlainObject(entry) || entry.kind !== 'file') return
+ const digest = entry.digest
+ const problem =
+ typeof entry.id !== 'string' || !entry.id.trim()
+ ? 'id'
+ : typeof entry.filename !== 'string' || !entry.filename
+ ? 'filename'
+ : typeof entry.mimeType !== 'string'
+ ? 'mimeType'
+ : typeof entry.detectedMediaType !== 'string'
+ ? 'detectedMediaType'
+ : entry.encoding !== 'base64'
+ ? 'encoding'
+ : typeof entry.dataBase64 !== 'string'
+ ? 'dataBase64'
+ : !Number.isSafeInteger(entry.sizeBytes) || (entry.sizeBytes as number) < 0
+ ? 'sizeBytes'
+ : !isPlainObject(digest) ||
+ digest.algorithm !== 'sha256' ||
+ typeof digest.hex !== 'string' ||
+ !SHA256_HEX.test(digest.hex)
+ ? 'digest'
+ : null
+ if (problem) {
+ throw new Error(
+ `Invalid host message request: COMPOSER_FILE_ATTACHMENT_INVALID attachments[${index}].${problem}`
+ )
+ }
+ })
+ return attachments as HostMessageAttachment[]
+}
+
function parseHostMessageRequest(raw: unknown): HostMessageRequest {
const parsed = raw as HostMessageRequest
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
@@ -171,6 +220,9 @@ function parseHostMessageRequest(raw: unknown): HostMessageRequest {
) {
throw new Error('Invalid host message request: modelSelectionRevision')
}
+ if (parsed.attachments != null) {
+ parsed.attachments = parseHostMessageAttachments(parsed.attachments)
+ }
if (parsed.fileReferences !== undefined) {
// Checks shape only; mcp-host enforces the count limit and resolves each reference.
if (!Array.isArray(parsed.fileReferences)) {
diff --git a/desktop-app/src/types.ts b/desktop-app/src/types.ts
index bfcf48cf4..8ff4b8bab 100644
--- a/desktop-app/src/types.ts
+++ b/desktop-app/src/types.ts
@@ -547,7 +547,7 @@ export type HostMessageRequest = {
[key: string]: unknown
}
-export type HostMessageAttachment = {
+export type HostImageAttachment = {
id: string
kind: 'image'
mimeType: 'image/jpeg' | 'image/png'
@@ -556,6 +556,26 @@ export type HostMessageAttachment = {
filename?: string
}
+/**
+ * #678 — a document of any type sent inline. `sizeBytes` is the decoded length
+ * and `digest.hex` the sha256 of those bytes; mcp-host recomputes both and
+ * classifies the bytes itself, so `detectedMediaType` is the renderer's claim.
+ */
+export type HostFileAttachment = {
+ id: string
+ kind: 'file'
+ filename: string
+ /** Media type the OS or browser reported; empty when it reported none. */
+ mimeType: string
+ detectedMediaType: string
+ encoding: 'base64'
+ dataBase64: string
+ sizeBytes: number
+ digest: { algorithm: 'sha256'; hex: string }
+}
+
+export type HostMessageAttachment = HostImageAttachment | HostFileAttachment
+
/**
* Structured error returned by mcp-host when an LLM call fails.
* Mirrors the TaskError shape from mcp-host/src/queue/types.ts.
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsx
index 14be2aca8..465d0bed4 100644
--- a/desktop-app/ui/src/App.tsx
+++ b/desktop-app/ui/src/App.tsx
@@ -2290,6 +2290,7 @@ export function App() {
() => ({
activeChatId: vm.activeChatId,
composerImageAttachments: vm.composerImageAttachments,
+ composerFileAttachments: vm.composerFileAttachments,
composerReferenceAttachments: vm.composerReferenceAttachments,
agentSending: vm.agentSending,
agentError: vm.agentError,
@@ -2300,6 +2301,7 @@ export function App() {
[
vm.activeChatId,
vm.composerImageAttachments,
+ vm.composerFileAttachments,
vm.composerReferenceAttachments,
vm.agentSending,
vm.agentError,
diff --git a/desktop-app/ui/src/components/agents/ComposerPanel.tsx b/desktop-app/ui/src/components/agents/ComposerPanel.tsx
index 318169a32..a2035ac07 100644
--- a/desktop-app/ui/src/components/agents/ComposerPanel.tsx
+++ b/desktop-app/ui/src/components/agents/ComposerPanel.tsx
@@ -21,7 +21,7 @@ import {
} from '@constants/agentFeatures'
import {
COMPOSER_ACCEPT_IMAGE_MIME_TYPES,
- COMPOSER_MAX_IMAGE_ATTACHMENTS,
+ COMPOSER_MAX_ATTACHMENTS,
composerImageBudget,
} from '@constants/attachments'
import { useContextsDataController } from '@hooks/domain/useContextsDataController'
@@ -30,9 +30,14 @@ import { useClickOutside } from '@hooks/useClickOutside'
import { useComposerDraft } from '@hooks/useComposerDraft'
import { useFlyoutPosition } from '@hooks/useFlyoutPosition'
import { useHostModels } from '@hooks/useHostModels'
+import { formatFileSize } from '@lib/composerFileAdmission'
import { readImageHeaderDimensions } from '@lib/imageHeaderDimensions'
import type { WorkflowRecipeListResult } from '../../../../src/types'
-import type { ComposerImageAttachment, ComposerReferenceAttachment } from '../../uiTypes'
+import type {
+ ComposerFileAttachment,
+ ComposerImageAttachment,
+ ComposerReferenceAttachment,
+} from '../../uiTypes'
import { AnnotationCanvas } from './AnnotationCanvas'
import { ComposerAgentFilesModal } from './ComposerAgentFilesModal'
import { ComposerGlobalFilesModal } from './ComposerGlobalFilesModal'
@@ -110,10 +115,33 @@ function getComposerReferenceIcon(attachment: ComposerReferenceAttachment) {
return <IconContexts />
}
+/** Status line of a document chip: what the user needs to know before sending. */
+function getComposerFileMeta(file: ComposerFileAttachment): string {
+ if (file.status === 'failed') return 'Not attached'
+ if (file.status !== 'ready') return 'Reading...'
+ const size = formatFileSize(file.sizeBytes)
+ if (file.classification.reader === 'none') return `${size} · No reader available`
+ return `${size} · ${file.classification.class}`
+}
+
+/** Notice shown under the chips for a document that needs attention. */
+function getComposerFileNotice(file: ComposerFileAttachment): string | null {
+ if (file.status === 'failed') return file.error
+ if (file.status !== 'ready') return null
+ if (file.classification.mismatch) {
+ return `${file.filename} looks like a ${file.classification.class} file, not what its name or type says. It is attached as it is.`
+ }
+ if (file.classification.reader === 'none') {
+ return `${file.filename} is attached, but the agent has no reader for this kind of file.`
+ }
+ return null
+}
+
export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelProps) {
const { selectedAgent } = useNavigationContext()
const {
composerImageAttachments,
+ composerFileAttachments,
composerReferenceAttachments,
agentSending,
agentError,
@@ -127,6 +155,8 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
handleAddComposerImageAttachments: onAddComposerImageAttachments,
handleUpdateComposerImageAttachment: onUpdateComposerImageAttachment,
handleRemoveComposerImageAttachment: onRemoveComposerImageAttachment,
+ handleAddComposerFiles: onAddComposerFiles,
+ handleRemoveComposerFileAttachment: onRemoveComposerFileAttachment,
handleAddComposerReferenceAttachments: onAddComposerReferenceAttachments,
handleRemoveComposerReferenceAttachment: onRemoveComposerReferenceAttachment,
handleSendAgentMessage: onSend,
@@ -371,10 +401,14 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
composerImageAttachments.length > 0 && hostModelSelection.visualSendBlocked
// Per-instance id: the main panel and the chat drawer can both be mounted.
const imageNoticeId = useId()
+ // #678 — a document that is still being read, or that failed, is not part of
+ // the message. Sending now would leave it out without saying so, so send
+ // stays disabled until each chip is ready or removed.
+ const filesBlockedForSend = composerFileAttachments.some(file => file.status !== 'ready')
const handleSend = useCallback(() => {
- if (imagesBlockedForSend) return
+ if (imagesBlockedForSend || filesBlockedForSend) return
void onSend(draft)
- }, [onSend, draft, imagesBlockedForSend])
+ }, [onSend, draft, imagesBlockedForSend, filesBlockedForSend])
const handleComposerKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLTextAreaElement>) => {
@@ -509,9 +543,8 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
const candidates = Array.from(files || [])
if (!candidates.length) return
- // Drop and paste can carry any file. Unsupported files are refused here,
- // before the free image slots are counted, so they never take an image's
- // place or inflate the skipped count.
+ // `routeComposerFiles` sends only PNG/JPEG here; documents take the file
+ // path. Images and files share one per-message count.
const validationErrors: string[] = []
const imageCandidates: Array<{
file: File
@@ -521,19 +554,18 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
const mimeType = inferComposerImageMimeType(file)
if (mimeType && COMPOSER_ACCEPT_IMAGE_MIME_TYPES.includes(mimeType)) {
imageCandidates.push({ file, mimeType })
- } else {
- validationErrors.push(`${file.name || 'Image'} is not supported. Use PNG or JPEG.`)
}
}
- if (!imageCandidates.length) {
- setComposerAttachmentError(validationErrors[0] ?? null)
- return
- }
+ if (!imageCandidates.length) return
- const availableSlots = COMPOSER_MAX_IMAGE_ATTACHMENTS - composerImageAttachments.length
+ const attachedFileCount = composerFileAttachments.filter(
+ file => file.status !== 'failed'
+ ).length
+ const availableSlots =
+ COMPOSER_MAX_ATTACHMENTS - composerImageAttachments.length - attachedFileCount
if (availableSlots <= 0) {
setComposerAttachmentError(
- `You can attach up to ${COMPOSER_MAX_IMAGE_ATTACHMENTS} images per message.`
+ `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`
)
return
}
@@ -543,7 +575,7 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
if (imageCandidates.length > availableSlots) {
const skipped = imageCandidates.length - availableSlots
validationErrors.push(
- `You can attach up to ${COMPOSER_MAX_IMAGE_ATTACHMENTS} images per message; ${skipped} ${
+ `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments; ${skipped} ${
skipped === 1 ? 'image was' : 'images were'
} not added.`
)
@@ -625,6 +657,7 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
},
[
buildAttachmentName,
+ composerFileAttachments,
composerImageAttachments,
imageBudget,
inferComposerImageMimeType,
@@ -635,6 +668,29 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
]
)
+ /**
+ * One entry point for the picker, paste and drop (#678): PNG and JPEG keep the
+ * image path with its previews and image budget; every other file is read as
+ * an inline document attachment.
+ */
+ const routeComposerFiles = useCallback(
+ (files: File[] | FileList, source: 'picker' | 'clipboard') => {
+ const images: File[] = []
+ const documents: File[] = []
+ for (const file of Array.from(files || [])) {
+ const mimeType = inferComposerImageMimeType(file)
+ if (mimeType && COMPOSER_ACCEPT_IMAGE_MIME_TYPES.includes(mimeType)) images.push(file)
+ else documents.push(file)
+ }
+ setComposerAttachmentError(null)
+ if (documents.length > 0) {
+ onAddComposerFiles(documents, new TextEncoder().encode(draft).length)
+ }
+ if (images.length > 0) void prepareComposerImageAttachments(images, source)
+ },
+ [draft, inferComposerImageMimeType, onAddComposerFiles, prepareComposerImageAttachments]
+ )
+
/**
* An annotation save re-encodes the image, so the result can be larger than the
* file the picker accepted. Apply the same budget before the attachment is
@@ -708,15 +764,12 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
dedupedBySignature.set(signature, file)
}
}
- const imageFiles = [...dedupedBySignature.values()].filter(file => {
- const mimeType = inferComposerImageMimeType(file)
- return mimeType ? COMPOSER_ACCEPT_IMAGE_MIME_TYPES.includes(mimeType) : false
- })
- if (!imageFiles.length) return
+ const pastedFiles = [...dedupedBySignature.values()]
+ if (!pastedFiles.length) return
event.preventDefault()
- void prepareComposerImageAttachments(imageFiles, 'clipboard')
+ routeComposerFiles(pastedFiles, 'clipboard')
},
- [inferComposerImageMimeType, prepareComposerImageAttachments]
+ [routeComposerFiles]
)
const hasDragFiles = useCallback((event: React.DragEvent<HTMLElement>) => {
@@ -755,9 +808,9 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
setDragActive(false)
const files = Array.from(event.dataTransfer.files || [])
if (!files.length) return
- void prepareComposerImageAttachments(files, 'picker')
+ routeComposerFiles(files, 'picker')
},
- [hasDragFiles, prepareComposerImageAttachments]
+ [hasDragFiles, routeComposerFiles]
)
const composerAttachmentItems = useMemo(() => {
@@ -775,10 +828,25 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
order: attachment.addedOrder ?? referenceItems.length + index,
fallbackIndex: referenceItems.length + index,
}))
- return [...referenceItems, ...imageItems].sort(
+ const fileItems = composerFileAttachments.map((attachment, index) => ({
+ id: `file:${attachment.id}`,
+ kind: 'file' as const,
+ attachment,
+ order: attachment.addedOrder ?? referenceItems.length + imageItems.length + index,
+ fallbackIndex: referenceItems.length + imageItems.length + index,
+ }))
+ return [...referenceItems, ...imageItems, ...fileItems].sort(
(a, b) => a.order - b.order || a.fallbackIndex - b.fallbackIndex
)
- }, [composerImageAttachments, composerReferenceAttachments])
+ }, [composerFileAttachments, composerImageAttachments, composerReferenceAttachments])
+ const composerFileNotices = useMemo(
+ () =>
+ composerFileAttachments.flatMap(file => {
+ const notice = getComposerFileNotice(file)
+ return notice ? [{ id: file.id, failed: file.status === 'failed', text: notice }] : []
+ }),
+ [composerFileAttachments]
+ )
const isDegraded = hostRuntimeStatus?.degraded?.reason === 'llm_key_missing'
const hasComposerAttachments = composerAttachmentItems.length > 0
@@ -788,13 +856,12 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
<input
ref={composerFileInputRef}
type="file"
- accept={COMPOSER_ACCEPT_IMAGE_MIME_TYPES.join(',')}
multiple
className="composer-file-input"
onChange={event => {
const selectedFiles = event.target.files
if (selectedFiles && selectedFiles.length > 0) {
- void prepareComposerImageAttachments(selectedFiles, 'picker')
+ routeComposerFiles(selectedFiles, 'picker')
}
event.currentTarget.value = ''
}}
@@ -1033,6 +1100,48 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
</span>
)
}
+ if (item.kind === 'file') {
+ const attachment = item.attachment
+ const meta = getComposerFileMeta(attachment)
+ return (
+ <span
+ key={item.id}
+ className="composer-attachment-chip"
+ title={`${attachment.filename} - ${meta}`}
+ data-testid="composer-file-chip"
+ data-file-status={attachment.status}
+ >
+ <span
+ className="composer-reference-icon composer-reference-icon--uploaded-image"
+ aria-hidden="true"
+ >
+ <IconAttachFile />
+ </span>
+ <span className="composer-attachment-chip-body">
+ <strong>{attachment.filename}</strong>
+ <span
+ className={`composer-attachment-chip-meta${
+ attachment.status === 'failed'
+ ? ' composer-attachment-chip-meta--failed'
+ : ''
+ }`}
+ >
+ {meta}
+ </span>
+ </span>
+ <IconButton
+ className="composer-attachment-remove"
+ onClick={() => onRemoveComposerFileAttachment(attachment.id)}
+ aria-label={`Remove ${attachment.filename}`}
+ label={`Remove ${attachment.filename}`}
+ size="xs"
+ variant="ghost"
+ >
+ <IconClose />
+ </IconButton>
+ </span>
+ )
+ }
const attachment = item.attachment
const tooltip = getComposerImageTooltip(attachment)
return (
@@ -1084,8 +1193,10 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
isDegraded ||
agentSending ||
imagesBlockedForSend ||
+ filesBlockedForSend ||
(!draft.trim() &&
composerImageAttachments.length === 0 &&
+ composerFileAttachments.length === 0 &&
composerReferenceAttachments.length === 0)
}
aria-label={agentSending ? 'Sending message' : 'Send message'}
@@ -1122,6 +1233,19 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
</p>
</div>
) : null}
+ {composerFileNotices.length > 0 ? (
+ <div className="composer-attachments" data-testid="composer-file-notices">
+ {composerFileNotices.map(notice => (
+ <p
+ key={notice.id}
+ className={notice.failed ? 'composer-attachment-error' : 'composer-attachment-notice'}
+ role={notice.failed ? 'alert' : 'status'}
+ >
+ {notice.text}
+ </p>
+ ))}
+ </div>
+ ) : null}
{agentError ? (
<div className="composer-footer">
{failedAgentSend?.kind === 'waking' ? (
diff --git a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
index 2e9b9aaef..1a1810950 100644
--- a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
+++ b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
@@ -7,7 +7,7 @@ import { resolve } from 'node:path'
import {
CODEX_COMPOSER_MAX_IMAGE_BYTES,
CODEX_COMPOSER_MAX_IMAGE_DIMENSION,
- COMPOSER_MAX_IMAGE_ATTACHMENTS,
+ COMPOSER_MAX_ATTACHMENTS,
COMPOSER_MAX_IMAGE_BYTES,
COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,
} from '@constants/attachments'
@@ -17,7 +17,11 @@ import {
imageInputBlockMessage,
} from '../../../../../src/imageInputDecision'
import type { WorkflowRecipeListResult } from '../../../../../src/types'
-import type { ComposerImageAttachment, FailedAgentSend } from '../../../uiTypes'
+import type {
+ ComposerFileAttachment,
+ ComposerImageAttachment,
+ FailedAgentSend,
+} from '../../../uiTypes'
import { ComposerPanel } from '../ComposerPanel'
// Resolve the stylesheet relative to THIS test file (not process.cwd()) so the
@@ -28,6 +32,7 @@ const composerStyles = readFileSync(resolve(__dirname, '../../../styles.css'), '
const composerState: ChatComposerStateContextValue = {
composerImageAttachments: [],
+ composerFileAttachments: [],
composerReferenceAttachments: [],
agentSending: false,
agentError: null,
@@ -45,6 +50,8 @@ const actionsMock = {
handleAddComposerImageAttachments: vi.fn(),
handleUpdateComposerImageAttachment: vi.fn(),
handleRemoveComposerImageAttachment: vi.fn(),
+ handleAddComposerFiles: vi.fn(),
+ handleRemoveComposerFileAttachment: vi.fn(),
handleAddComposerReferenceAttachments: vi.fn(),
handleRemoveComposerReferenceAttachment: vi.fn(),
handleSendAgentMessage: vi.fn(),
@@ -173,6 +180,7 @@ afterEach(() => {
draftState.set.mockReset()
Object.assign(composerState, {
composerImageAttachments: [],
+ composerFileAttachments: [],
composerReferenceAttachments: [],
agentSending: false,
agentError: null,
@@ -568,7 +576,8 @@ describe('ComposerPanel with an image-capable model', () => {
expect(timesClicked()).toBe(1)
const fileInput = pickerInput(container)
- expect(fileInput.accept).toBe('image/jpeg,image/png')
+ // Any document can be picked (#678); the picker no longer filters to images.
+ expect(fileInput.accept).toBe('')
expect(fileInput.multiple).toBe(true)
fireEvent.change(fileInput, { target: { files: [imageFile('photo.png', 'image/png')] } })
@@ -643,7 +652,8 @@ describe('ComposerPanel with an image-capable model', () => {
expect(expectSinglePreparedImage().mimeType).toBe('image/png')
})
- it('explains a rejected file type and still attaches the valid image in the same batch', async () => {
+ it('routes a non-PNG/JPEG file to the document path and the valid image to the image path', async () => {
+ draftState.value = 'abc'
const { container } = render(<ComposerPanel inline />)
fireEvent.change(pickerInput(container), {
@@ -653,10 +663,16 @@ describe('ComposerPanel with an image-capable model', () => {
})
await waitFor(() => expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalled())
- expect(screen.getByRole('alert').textContent).toBe(
- 'animation.gif is not supported. Use PNG or JPEG.'
- )
expect(expectSinglePreparedImage().name).toBe('good.png')
+ // Liveness witness: the document path ran, with the draft's byte length for the body budget.
+ expect(actionsMock.handleAddComposerFiles).toHaveBeenCalledTimes(1)
+ const [documents, textBytes] = actionsMock.handleAddComposerFiles.mock.calls[0] as [
+ File[],
+ number,
+ ]
+ expect(documents.map(file => file.name)).toEqual(['animation.gif'])
+ expect(textBytes).toBe(3)
+ expect(screen.queryByRole('alert')).toBeNull()
})
it('explains an oversize image and attaches nothing', async () => {
@@ -674,7 +690,7 @@ describe('ComposerPanel with an image-capable model', () => {
})
it('attaches 20 images from one pick and says how many did not fit', async () => {
- expect(COMPOSER_MAX_IMAGE_ATTACHMENTS).toBe(20)
+ expect(COMPOSER_MAX_ATTACHMENTS).toBe(20)
const { container } = render(<ComposerPanel inline />)
const files = Array.from({ length: 22 }, (_, index) =>
imageFile(`photo-${index + 1}.png`, 'image/png', [...PNG_BYTES, index])
@@ -690,7 +706,7 @@ describe('ComposerPanel with an image-capable model', () => {
files.slice(0, 20).map(file => file.name)
)
expect(screen.getByRole('alert').textContent).toBe(
- 'You can attach up to 20 images per message; 2 images were not added.'
+ 'A message can carry at most 20 attachments; 2 images were not added.'
)
})
@@ -773,7 +789,7 @@ describe('ComposerPanel with an image-capable model', () => {
expect(actionsMock.handleAddComposerImageAttachments).not.toHaveBeenCalled()
})
- it('does not let a dropped non-image take one of the free image slots', async () => {
+ it('sends a dropped document to the document path without taking an image slot', async () => {
// 18 of the 20 slots are taken; the drop carries a PDF ahead of two PNGs.
composerState.composerImageAttachments = Array.from({ length: 18 }, (_, index) => ({
id: `attached-${index + 1}`,
@@ -802,8 +818,12 @@ describe('ComposerPanel with an image-capable model', () => {
)
const [batch] = addedBatches()
expect(batch?.map(attachment => attachment.name)).toEqual(['a.png', 'b.png'])
- // The only message is the PDF refusal: no image was counted as skipped.
- expect(screen.getByRole('alert').textContent).toBe('doc.pdf is not supported. Use PNG or JPEG.')
+ // Liveness witness: the PDF reached the document path, not the image path.
+ expect(actionsMock.handleAddComposerFiles).toHaveBeenCalledTimes(1)
+ const [documents] = actionsMock.handleAddComposerFiles.mock.calls[0] as [File[], number]
+ expect(documents.map(file => file.name)).toEqual(['doc.pdf'])
+ // No image was counted as skipped, so nothing is reported.
+ expect(screen.queryByRole('alert')).toBeNull()
})
})
@@ -953,6 +973,7 @@ describe('ComposerPanel failed-send recovery actions', () => {
const failedSend: FailedAgentSend = {
content: 'inspect this image',
attachments: [],
+ files: [],
references: [],
message: 'Image input evidence changed',
kind: 'upstream',
@@ -1004,3 +1025,134 @@ describe('ComposerPanel failed-send recovery actions', () => {
expect(actionsMock.handleRetryFailedAgentSend).toHaveBeenCalledTimes(1)
})
})
+
+describe('ComposerPanel document attachments (#678)', () => {
+ const readyClassification = {
+ class: 'text' as const,
+ detectedMediaType: 'text/plain',
+ detection: 'text_utf8' as const,
+ textReadable: true,
+ reader: 'text' as const,
+ modelImageInput: 'unsupported' as const,
+ mismatch: false,
+ }
+
+ function readyFile(
+ overrides: Partial<Extract<ComposerFileAttachment, { status: 'ready' }>> = {}
+ ) {
+ return {
+ id: 'file-1',
+ type: 'file' as const,
+ filename: 'notes.txt',
+ sizeBytes: 2048,
+ declaredMediaType: 'text/plain',
+ status: 'ready' as const,
+ classification: readyClassification,
+ dataBase64: 'aGVsbG8=',
+ digestHex: 'a'.repeat(64),
+ ...overrides,
+ }
+ }
+
+ function sendButton(): HTMLButtonElement {
+ return screen.getByRole('button', { name: 'Send message' }) as HTMLButtonElement
+ }
+
+ it('shows a ready document chip and lets a message with only that document be sent', () => {
+ composerState.composerFileAttachments = [readyFile()]
+ render(<ComposerPanel inline />)
+
+ const chip = screen.getByTestId('composer-file-chip')
+ expect(chip.textContent).toContain('notes.txt')
+ expect(chip.textContent).toContain('2.0 KiB · text')
+ expect(chip.getAttribute('data-file-status')).toBe('ready')
+ expect(sendButton().disabled).toBe(false)
+ })
+
+ it('removes a document through its chip button', () => {
+ composerState.composerFileAttachments = [readyFile()]
+ render(<ComposerPanel inline />)
+
+ fireEvent.click(screen.getByRole('button', { name: 'Remove notes.txt' }))
+ expect(actionsMock.handleRemoveComposerFileAttachment).toHaveBeenCalledWith('file-1')
+ })
+
+ it.each(['selected', 'reading'] as const)('blocks the send while a document is %s', status => {
+ composerState.composerFileAttachments = [
+ {
+ id: 'file-2',
+ type: 'file',
+ filename: 'big.pdf',
+ sizeBytes: 1024,
+ declaredMediaType: 'application/pdf',
+ status,
+ },
+ ]
+ draftState.value = 'read this'
+ render(<ComposerPanel inline />)
+
+ // Liveness witness: the chip for this very file is on screen with its reading state.
+ expect(screen.getByTestId('composer-file-chip').textContent).toContain('Reading...')
+ expect(sendButton().disabled).toBe(true)
+ })
+
+ it('keeps a failed document on screen with its reason and blocks the send until it is removed', () => {
+ composerState.composerFileAttachments = [
+ {
+ id: 'file-3',
+ type: 'file',
+ filename: 'broken.bin',
+ sizeBytes: 10,
+ declaredMediaType: '',
+ status: 'failed',
+ error: 'broken.bin could not be read.',
+ },
+ ]
+ draftState.value = 'hello'
+ render(<ComposerPanel inline />)
+
+ expect(screen.getByTestId('composer-file-chip').getAttribute('data-file-status')).toBe('failed')
+ expect(screen.getByRole('alert').textContent).toBe('broken.bin could not be read.')
+ expect(sendButton().disabled).toBe(true)
+ })
+
+ it('says when the agent has no reader for an attached document', () => {
+ composerState.composerFileAttachments = [
+ readyFile({
+ id: 'file-4',
+ filename: 'archive.zip',
+ classification: { ...readyClassification, class: 'binary_unsupported', reader: 'none' },
+ }),
+ ]
+ render(<ComposerPanel inline />)
+
+ expect(screen.getByTestId('composer-file-chip').textContent).toContain('No reader available')
+ expect(screen.getByRole('status').textContent).toBe(
+ 'archive.zip is attached, but the agent has no reader for this kind of file.'
+ )
+ expect(sendButton().disabled).toBe(false)
+ })
+
+ it('warns when the bytes do not match the name or type', () => {
+ composerState.composerFileAttachments = [
+ readyFile({
+ id: 'file-5',
+ filename: 'report.txt',
+ classification: { ...readyClassification, class: 'pdf', mismatch: true },
+ }),
+ ]
+ render(<ComposerPanel inline />)
+
+ expect(screen.getByRole('status').textContent).toBe(
+ 'report.txt looks like a pdf file, not what its name or type says. It is attached as it is.'
+ )
+ })
+
+ it('keeps send disabled with an empty draft and no attachment of any kind', () => {
+ render(<ComposerPanel inline />)
+
+ // Witness: nothing renders for documents, and the button itself is the guard under test.
+ expect(screen.queryByTestId('composer-file-chip')).toBeNull()
+ expect(sendButton().disabled).toBe(true)
+ })
+})
diff --git a/desktop-app/ui/src/constants/attachments.ts b/desktop-app/ui/src/constants/attachments.ts
index 0f3f75f0e..2990831a4 100644
--- a/desktop-app/ui/src/constants/attachments.ts
+++ b/desktop-app/ui/src/constants/attachments.ts
@@ -1,15 +1,43 @@
-// Must match INCOMING_ATTACHMENT_MAX_COUNT in mcp-host/src/agent/incomingAttachments.ts.
-export const COMPOSER_MAX_IMAGE_ATTACHMENTS = 20
+/**
+ * Images and files share one per-message count. Must match
+ * INCOMING_ATTACHMENT_MAX_COUNT in mcp-host/src/agent/incomingAttachments.ts.
+ */
+export const COMPOSER_MAX_ATTACHMENTS = 20
+
+/**
+ * Per-file ceiling for a document sent as `kind:'file'`. Must match the
+ * `CLERUM_ATTACHMENT_FILE_MAX_BYTES` default of mcp-host (`attachmentFileMaxBytes`).
+ */
+export const COMPOSER_MAX_FILE_BYTES = 3 * 1024 * 1024
+/**
+ * Share of one chat request body that is not credited to the image quota: the
+ * base64 of every `kind:'file'`, the text and the JSON envelope. Must match
+ * `MAX_NON_IMAGE_BODY_BYTES` in `rpc-proxy/src/middleware/chatJsonBody.ts` and
+ * in `mcp-host/src/server.ts`.
+ */
+export const COMPOSER_MAX_NON_IMAGE_BODY_BYTES = 6 * 1024 * 1024
+/**
+ * Whole chat request body. Must match `MAX_CHAT_BODY_BYTES` in
+ * `rpc-proxy/src/middleware/chatJsonBody.ts` and in `mcp-host/src/server.ts`.
+ */
+export const COMPOSER_MAX_REQUEST_BODY_BYTES = 24 * 1024 * 1024
+/** JSON envelope around the text and the attachments: model, revision, keys. */
+export const COMPOSER_REQUEST_ENVELOPE_BYTES = 4096
+/**
+ * JSON around one `kind:'file'` entry besides its base64 and its filename:
+ * the id, the two media types, the sha256 hex and the fixed keys.
+ */
+export const COMPOSER_FILE_ENTRY_METADATA_BYTES = 640
/**
* General (#654 / PR #669) per-image ceiling for every image-capable model
- * that is not Codex. Images travel inline in the ordinary 10 MB JSON body.
+ * that is not Codex.
*/
export const COMPOSER_MAX_IMAGE_BYTES = 3 * 1024 * 1024
/**
- * Combined base64 size of the images in one non-Codex message. rpc-proxy and
- * mcp-host parse ordinary JSON with a 10 MB body limit; 2 MB is left for the
- * text and the request envelope.
+ * Combined base64 size of the images in one non-Codex message. Images have
+ * their own quota in rpc-proxy and mcp-host (up to the 24 MiB request body);
+ * this is the composer's product ceiling for them.
*/
export const COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES = 8 * 1024 * 1024
diff --git a/desktop-app/ui/src/contexts/AgentChatActionsContext/types.ts b/desktop-app/ui/src/contexts/AgentChatActionsContext/types.ts
index 81beab82a..070d0e30b 100644
--- a/desktop-app/ui/src/contexts/AgentChatActionsContext/types.ts
+++ b/desktop-app/ui/src/contexts/AgentChatActionsContext/types.ts
@@ -1,5 +1,9 @@
import type { ReactNode, RefObject } from 'react'
-import type { ComposerImageAttachment, ComposerReferenceAttachment } from '../../uiTypes'
+import type {
+ ComposerFileAttachment,
+ ComposerImageAttachment,
+ ComposerReferenceAttachment,
+} from '../../uiTypes'
/**
* Stable, cross-cutting chat actions. These are `useCallback` handlers plus the
@@ -19,6 +23,9 @@ export interface AgentChatActionsContextValue {
handleAddComposerImageAttachments: (attachments: ComposerImageAttachment[]) => void
handleUpdateComposerImageAttachment: (attachment: ComposerImageAttachment) => void
handleRemoveComposerImageAttachment: (attachmentId: string) => void
+ handleAddComposerFiles: (files: File[], textBytes: number) => void
+ handleRemoveComposerFileAttachment: (attachmentId: string) => void
+ handleRestoreComposerFiles: (files: ComposerFileAttachment[]) => void
handleAddComposerReferenceAttachments: (attachments: ComposerReferenceAttachment[]) => void
handleRemoveComposerReferenceAttachment: (attachmentId: string) => void
handleSendAgentMessage: (text: string) => Promise<void>
diff --git a/desktop-app/ui/src/contexts/AgentTaskTrackerContext/__tests__/taskTracker.test.ts b/desktop-app/ui/src/contexts/AgentTaskTrackerContext/__tests__/taskTracker.test.ts
index bd21597ad..6d15aebf4 100644
--- a/desktop-app/ui/src/contexts/AgentTaskTrackerContext/__tests__/taskTracker.test.ts
+++ b/desktop-app/ui/src/contexts/AgentTaskTrackerContext/__tests__/taskTracker.test.ts
@@ -856,7 +856,7 @@ describe('TaskTracker', () => {
})
describe('TaskTracker — coordinator API (spec §4.2)', () => {
- const RESEND = { content: 'hi', attachments: [], references: [] }
+ const RESEND = { content: 'hi', attachments: [], files: [], references: [] }
it('attach() is idempotent for the same (key, taskId)', () => {
expect(tracker.attach(KEY, 'task-1', 'um-1')).toBe(true)
diff --git a/desktop-app/ui/src/contexts/AgentTaskTrackerContext/types.ts b/desktop-app/ui/src/contexts/AgentTaskTrackerContext/types.ts
index 9bc88667f..df6902ad4 100644
--- a/desktop-app/ui/src/contexts/AgentTaskTrackerContext/types.ts
+++ b/desktop-app/ui/src/contexts/AgentTaskTrackerContext/types.ts
@@ -1,4 +1,9 @@
-import type { ComposerImageAttachment, ComposerReferenceAttachment, ProgressStep } from '@/uiTypes'
+import type {
+ ComposerImageAttachment,
+ ComposerReferenceAttachment,
+ ProgressStep,
+ ReadyComposerFileAttachment,
+} from '@/uiTypes'
import type { ChatMessageAttachment } from '../../../../src/types'
/**
@@ -126,6 +131,7 @@ export interface TrackerCallbacks {
export interface ResendPayload {
content: string
attachments: ComposerImageAttachment[]
+ files: ReadyComposerFileAttachment[]
references: ComposerReferenceAttachment[]
}
diff --git a/desktop-app/ui/src/contexts/ChatComposerStateContext/types.ts b/desktop-app/ui/src/contexts/ChatComposerStateContext/types.ts
index b9188ee81..79d17009f 100644
--- a/desktop-app/ui/src/contexts/ChatComposerStateContext/types.ts
+++ b/desktop-app/ui/src/contexts/ChatComposerStateContext/types.ts
@@ -1,5 +1,6 @@
import type { ReactNode } from 'react'
import type {
+ ComposerFileAttachment,
ComposerImageAttachment,
ComposerReferenceAttachment,
FailedAgentSend,
@@ -14,6 +15,7 @@ import type {
export interface ChatComposerStateContextValue {
activeChatId: string | null
composerImageAttachments: ComposerImageAttachment[]
+ composerFileAttachments: ComposerFileAttachment[]
composerReferenceAttachments: ComposerReferenceAttachment[]
agentSending: boolean
agentError: string | null
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx
new file mode 100644
index 000000000..5a722fe42
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx
@@ -0,0 +1,295 @@
+// @vitest-environment jsdom
+/**
+ * Issue #678 — a document picked in the composer travels as an inline
+ * `kind:'file'` attachment. These tests drive the public controller API: add a
+ * file, send, and read what reached the Host and what the user is told.
+ */
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { act, waitFor } from '@testing-library/react'
+import { createHash } from 'node:crypto'
+import { renderController } from './__fixtures__/controllerHarness'
+import { type MockClerum, installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'
+
+let clerum: MockClerum
+let uuidCounter = 0
+
+;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+
+const NOTES_TEXT = 'first line\nsecond line\n'
+const NOTES_BYTES = new TextEncoder().encode(NOTES_TEXT)
+const NOTES_DIGEST = createHash('sha256').update(NOTES_BYTES).digest('hex')
+
+const UNSUPPORTED =
+ 'The Host does not accept file attachments yet; the message was not delivered with them. Your files are kept so you can retry once the Host is updated.'
+
+beforeEach(() => {
+ uuidCounter = 0
+ vi.spyOn(globalThis.crypto, 'randomUUID').mockImplementation(
+ () => `uuid-${++uuidCounter}` as `${string}-${string}-${string}-${string}-${string}`
+ )
+ clerum = installMockClerum()
+})
+
+afterEach(() => {
+ vi.restoreAllMocks()
+ uninstallMockClerum()
+})
+
+async function settleMount() {
+ await waitFor(() => expect(clerum.chat.getIndex).toHaveBeenCalled())
+}
+
+function notesFile(): File {
+ return new File([NOTES_BYTES], 'notes.txt', { type: 'text/plain' })
+}
+
+type Rendered = ReturnType<typeof renderController>
+
+/** Adds a file through the public action and waits until it is read and hashed. */
+async function addReadyFile(rendered: Rendered, file: File = notesFile()) {
+ act(() => {
+ rendered.result.current.handleAddComposerFiles([file], 0)
+ })
+ await waitFor(() =>
+ expect(rendered.result.current.composerFileAttachments.map(item => item.status)).toEqual([
+ 'ready',
+ ])
+ )
+ return rendered.result.current.composerFileAttachments[0]!
+}
+
+function sentAttachments(callIndex = 0): Array<Record<string, unknown>> {
+ const request = clerum.rpc.invokeHostMessage.mock.calls[callIndex]?.[1] as Record<string, unknown>
+ expect(request).toBeDefined()
+ return (request.attachments ?? []) as Array<Record<string, unknown>>
+}
+
+describe('sendAgentMessage — document attachments (#678)', () => {
+ it('sends a ready document as an inline file attachment with its sha256', async () => {
+ clerum.rpc.invokeHostMessage.mockResolvedValue({ response: 'done' })
+ const rendered = renderController()
+ await settleMount()
+ const file = await addReadyFile(rendered)
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('Summarize this')
+ })
+
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)
+ expect(sentAttachments()).toEqual([
+ {
+ id: file.id,
+ kind: 'file',
+ filename: 'notes.txt',
+ mimeType: 'text/plain',
+ detectedMediaType: 'text/plain',
+ encoding: 'base64',
+ dataBase64: Buffer.from(NOTES_BYTES).toString('base64'),
+ sizeBytes: NOTES_BYTES.byteLength,
+ digest: { algorithm: 'sha256', hex: NOTES_DIGEST },
+ },
+ ])
+ // The composer is cleared once the send is accepted for delivery.
+ expect(rendered.result.current.composerFileAttachments).toEqual([])
+ })
+
+ it('sends a message that has only a document and no text', async () => {
+ clerum.rpc.invokeHostMessage.mockResolvedValue({ response: 'done' })
+ const rendered = renderController()
+ await settleMount()
+ await addReadyFile(rendered)
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('')
+ })
+
+ // Witness: the send reached the Host and carried the document.
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)
+ expect(sentAttachments()).toHaveLength(1)
+ })
+
+ it('does not send while a document is not ready', async () => {
+ clerum.rpc.invokeHostMessage.mockResolvedValue({ response: 'done' })
+ const rendered = renderController()
+ await settleMount()
+ // A file whose read never settles stays in `reading`.
+ const pending = new File([NOTES_BYTES], 'slow.txt', { type: 'text/plain' })
+ vi.spyOn(pending, 'arrayBuffer').mockReturnValue(new Promise(() => {}))
+ act(() => {
+ rendered.result.current.handleAddComposerFiles([pending], 0)
+ })
+ // Liveness witness: the chip exists and is mid-read, so the send below is
+ // refused because of it and not because the composer is empty.
+ await waitFor(() =>
+ expect(rendered.result.current.composerFileAttachments.map(item => item.status)).toEqual([
+ 'reading',
+ ])
+ )
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('go')
+ })
+
+ expect(clerum.rpc.invokeHostMessage).not.toHaveBeenCalled()
+ expect(rendered.result.current.composerFileAttachments).toHaveLength(1)
+ })
+
+ it('lists the ids the Host admitted and shows no error when the file is among them', async () => {
+ const rendered = renderController()
+ await settleMount()
+ const file = await addReadyFile(rendered)
+ clerum.rpc.invokeHostMessage.mockResolvedValue({
+ response: 'done',
+ acceptedAttachmentIds: [file.id],
+ })
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('Summarize this')
+ })
+
+ // Witness: the accepted branch ran (success toast) and released the snapshot.
+ expect(rendered.spies.pushToast).toHaveBeenCalledWith('Message sent to agent-x.', 'success')
+ expect(rendered.result.current.agentError).toBeNull()
+ expect(rendered.result.current.failedAgentSend).toBeNull()
+ })
+})
+
+describe('sendAgentMessage — a Host without file attachments (#678 D13)', () => {
+ it('rejected with LLM_INVALID_ATTACHMENT: says so, keeps the files, and retries without rereading', async () => {
+ const rendered = renderController()
+ await settleMount()
+ const source = notesFile()
+ const arrayBuffer = vi.spyOn(source, 'arrayBuffer')
+ const file = await addReadyFile(rendered, source)
+ expect(arrayBuffer).toHaveBeenCalledTimes(1)
+
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({
+ success: false,
+ error: {
+ code: 'LLM_INVALID_ATTACHMENT',
+ message: 'Unsupported attachment kind: file',
+ retryable: false,
+ provider: 'unknown',
+ },
+ })
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('Summarize this')
+ })
+
+ expect(rendered.spies.pushToast).toHaveBeenCalledWith(
+ `Message to agent-x failed: ${UNSUPPORTED}`,
+ 'error'
+ )
+ const failed = rendered.result.current.failedAgentSend
+ expect(failed?.message).toBe(UNSUPPORTED)
+ expect(failed?.files.map(item => item.id)).toEqual([file.id])
+
+ // The Host was updated in the meantime: it now lists the file it admitted.
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({
+ response: 'done',
+ acceptedAttachmentIds: [file.id],
+ })
+ await act(async () => {
+ await rendered.result.current.handleRetryFailedAgentSend()
+ })
+
+ // The retry re-sent the very same document without reading it again.
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(2)
+ expect(sentAttachments(1)).toEqual(sentAttachments(0))
+ expect(sentAttachments(1)[0]).toMatchObject({ id: file.id, digest: { hex: NOTES_DIGEST } })
+ expect(arrayBuffer).toHaveBeenCalledTimes(1)
+ expect(rendered.result.current.failedAgentSend).toBeNull()
+ })
+
+ it('does not blame the files for an unrelated LLM_INVALID_ATTACHMENT on a send without any', async () => {
+ const rendered = renderController()
+ await settleMount()
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({
+ success: false,
+ error: { code: 'LLM_INVALID_ATTACHMENT', message: 'bad image', retryable: false },
+ })
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('no files here')
+ })
+
+ // Witness: the send was refused with its own message.
+ expect(rendered.spies.pushToast).toHaveBeenCalledWith(
+ 'Message to agent-x failed: bad image',
+ 'error'
+ )
+ expect(rendered.result.current.failedAgentSend?.message).toBe('bad image')
+ })
+
+ it('accepted without listing the file: reports it and keeps the snapshot', async () => {
+ const rendered = renderController()
+ await settleMount()
+ const file = await addReadyFile(rendered)
+ // An older Host drops the attachment and still answers, with no id list.
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({
+ response: 'done',
+ acceptedAttachmentIds: [],
+ })
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('Summarize this')
+ })
+
+ expect(rendered.result.current.agentError).toBe(UNSUPPORTED)
+ expect(rendered.spies.pushToast).toHaveBeenCalledWith(UNSUPPORTED, 'error')
+ expect(rendered.spies.pushToast).not.toHaveBeenCalledWith('Message sent to agent-x.', 'success')
+ expect(rendered.result.current.failedAgentSend?.files.map(item => item.id)).toEqual([file.id])
+ })
+
+ it('names how many files were dropped when the Host admitted only some', async () => {
+ const rendered = renderController()
+ await settleMount()
+ act(() => {
+ rendered.result.current.handleAddComposerFiles(
+ [
+ new File([NOTES_BYTES], 'a.txt', { type: 'text/plain' }),
+ new File([NOTES_BYTES], 'b.txt', { type: 'text/plain' }),
+ ],
+ 0
+ )
+ })
+ await waitFor(() =>
+ expect(rendered.result.current.composerFileAttachments.map(item => item.status)).toEqual([
+ 'ready',
+ 'ready',
+ ])
+ )
+ const [first] = rendered.result.current.composerFileAttachments
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({
+ response: 'done',
+ acceptedAttachmentIds: [first!.id],
+ })
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('Summarize these')
+ })
+
+ const partial =
+ 'The Host did not receive 1 of the attached files. Your files are kept so you can retry.'
+ expect(rendered.result.current.agentError).toBe(partial)
+ expect(rendered.result.current.failedAgentSend?.files).toHaveLength(2)
+ })
+
+ it('does not read an empty ack (an unreadable Host answer) as a dropped file', async () => {
+ const rendered = renderController()
+ await settleMount()
+ await addReadyFile(rendered)
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({})
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('Summarize this')
+ })
+
+ // Witness: the send went out with the document and was not marked failed.
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)
+ expect(sentAttachments()).toHaveLength(1)
+ expect(rendered.result.current.failedAgentSend).toBeNull()
+ expect(rendered.result.current.agentError).toBeNull()
+ expect(rendered.spies.pushToast).not.toHaveBeenCalledWith(UNSUPPORTED, 'error')
+ })
+})
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.tsx
new file mode 100644
index 000000000..c210adb16
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx
@@ -0,0 +1,224 @@
+// @vitest-environment jsdom
+/**
+ * Issue #678 — the composer's document state machine: a picked file is
+ * `reading` at once, then `ready` (or `failed` with a reason); the user can
+ * remove it at any point, and a removed file never comes back.
+ */
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { act, renderHook, waitFor } from '@testing-library/react'
+import { COMPOSER_MAX_ATTACHMENTS, COMPOSER_MAX_FILE_BYTES } from '@constants/attachments'
+import { useComposerAttachments } from '../useComposerAttachments'
+
+;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+
+afterEach(() => {
+ vi.restoreAllMocks()
+})
+
+function textFile(name: string, text: string): File {
+ return new File([new TextEncoder().encode(text)], name, { type: 'text/plain' })
+}
+
+function render(selectedAgent: string | null = 'agent-x') {
+ const clearSendError = vi.fn()
+ const hook = renderHook(
+ (props: { agent: string | null }) =>
+ useComposerAttachments({ selectedAgent: props.agent, clearSendError }),
+ { initialProps: { agent: selectedAgent } }
+ )
+ return { ...hook, clearSendError }
+}
+
+function statuses(result: ReturnType<typeof render>['result']): string[] {
+ return result.current.composerFileAttachments.map(file => file.status)
+}
+
+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()
+ const file = textFile('notes.txt', 'hello')
+ let release: (value: ArrayBuffer) => void = () => {}
+ vi.spyOn(file, 'arrayBuffer').mockReturnValue(
+ new Promise<ArrayBuffer>(resolve => {
+ release = resolve
+ })
+ )
+
+ act(() => {
+ result.current.handleAddComposerFiles([file], 0)
+ })
+ // The chip exists before any byte is read.
+ expect(statuses(result)).toEqual(['reading'])
+ expect(clearSendError).toHaveBeenCalled()
+
+ await act(async () => {
+ release(new TextEncoder().encode('hello').buffer as ArrayBuffer)
+ })
+ await waitFor(() => expect(statuses(result)).toEqual(['ready']))
+ const [ready] = result.current.composerFileAttachments
+ expect(ready).toMatchObject({ filename: 'notes.txt', sizeBytes: 5 })
+ expect(ready?.status === 'ready' && ready.digestHex).toBe(
+ '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
+ )
+ })
+
+ it('keeps the order in which files were added', async () => {
+ const { result } = render()
+
+ act(() => {
+ result.current.handleAddComposerFiles([textFile('a.txt', 'a'), textFile('b.txt', 'b')], 0)
+ })
+ await waitFor(() => expect(statuses(result)).toEqual(['ready', 'ready']))
+
+ expect(result.current.composerFileAttachments.map(file => file.filename)).toEqual([
+ 'a.txt',
+ 'b.txt',
+ ])
+ const orders = result.current.composerFileAttachments.map(file => file.addedOrder ?? 0)
+ expect(orders[0]).toBeLessThan(orders[1]!)
+ })
+
+ it('drops a second copy of the same file instead of attaching it twice', async () => {
+ const { result } = render()
+
+ act(() => {
+ result.current.handleAddComposerFiles([textFile('same.txt', 'same')], 0)
+ })
+ await waitFor(() => expect(statuses(result)).toEqual(['ready']))
+ act(() => {
+ result.current.handleAddComposerFiles([textFile('same.txt', 'same')], 0)
+ })
+ // Liveness witness: the second copy was admitted (reading) before it was dropped.
+ expect(statuses(result)).toEqual(['ready', 'reading'])
+ await waitFor(() => expect(statuses(result)).toEqual(['ready']))
+ })
+
+ it('keeps two files that share a name but not their bytes', async () => {
+ const { result } = render()
+
+ act(() => {
+ result.current.handleAddComposerFiles(
+ [textFile('same.txt', 'one'), textFile('same.txt', 'two')],
+ 0
+ )
+ })
+
+ await waitFor(() => expect(statuses(result)).toEqual(['ready', 'ready']))
+ })
+
+ it('shows a file over the size limit as failed with the reason and never reads it', () => {
+ const { result } = render()
+ const huge = textFile('huge.bin', 'x')
+ Object.defineProperty(huge, 'size', { configurable: true, value: COMPOSER_MAX_FILE_BYTES + 1 })
+ const read = vi.spyOn(huge, 'arrayBuffer')
+
+ act(() => {
+ result.current.handleAddComposerFiles([huge], 0)
+ })
+
+ const [failed] = result.current.composerFileAttachments
+ expect(failed?.status).toBe('failed')
+ expect(failed?.status === 'failed' && failed.error).toMatch(/^huge\.bin is .* at most 3\.0 MiB/)
+ // Witness for "never reads it": the chip exists, and the reader was not called.
+ expect(read).not.toHaveBeenCalled()
+ })
+
+ it('shows the 21st attachment as failed and keeps the first 20', async () => {
+ const { result } = render()
+ const files = Array.from({ length: COMPOSER_MAX_ATTACHMENTS + 1 }, (_, index) =>
+ textFile(`f${index}.txt`, `content ${index}`)
+ )
+
+ act(() => {
+ result.current.handleAddComposerFiles(files, 0)
+ })
+
+ await waitFor(() =>
+ expect(statuses(result).filter(status => status === 'ready')).toHaveLength(
+ COMPOSER_MAX_ATTACHMENTS
+ )
+ )
+ const last = result.current.composerFileAttachments.at(-1)
+ expect(last?.status).toBe('failed')
+ expect(last?.status === 'failed' && last.error).toBe(
+ `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`
+ )
+ })
+
+ it('does not bring back a file removed while it was being read', async () => {
+ const { result } = render()
+ const file = textFile('gone.txt', 'gone')
+ let release: (value: ArrayBuffer) => void = () => {}
+ vi.spyOn(file, 'arrayBuffer').mockReturnValue(
+ new Promise<ArrayBuffer>(resolve => {
+ release = resolve
+ })
+ )
+ act(() => {
+ result.current.handleAddComposerFiles([file], 0)
+ })
+ const [reading] = result.current.composerFileAttachments
+ expect(reading?.status).toBe('reading')
+
+ act(() => {
+ result.current.handleRemoveComposerFileAttachment(reading!.id)
+ })
+ expect(result.current.composerFileAttachments).toEqual([])
+ await act(async () => {
+ release(new TextEncoder().encode('gone').buffer as ArrayBuffer)
+ // Let the read finish and its continuation run.
+ await Promise.resolve()
+ await new Promise(resolve => setTimeout(resolve, 20))
+ })
+
+ expect(result.current.composerFileAttachments).toEqual([])
+ })
+
+ it('removes a failed file so the composer can send again', () => {
+ const { result } = render()
+ const bad = textFile('bad/name.txt', 'x')
+
+ act(() => {
+ result.current.handleAddComposerFiles([bad], 0)
+ })
+ expect(statuses(result)).toEqual(['failed'])
+
+ act(() => {
+ result.current.handleRemoveComposerFileAttachment(
+ result.current.composerFileAttachments[0]!.id
+ )
+ })
+ expect(result.current.composerFileAttachments).toEqual([])
+ })
+
+ it('clears the documents when the selected agent changes', async () => {
+ const { result, rerender } = render('agent-x')
+ act(() => {
+ result.current.handleAddComposerFiles([textFile('a.txt', 'a')], 0)
+ })
+ await waitFor(() => expect(statuses(result)).toEqual(['ready']))
+
+ rerender({ agent: 'agent-y' })
+
+ await waitFor(() => expect(result.current.composerFileAttachments).toEqual([]))
+ })
+
+ it('puts back the files of a failed send exactly as they were', async () => {
+ const { result } = render()
+ act(() => {
+ result.current.handleAddComposerFiles([textFile('a.txt', 'a')], 0)
+ })
+ await waitFor(() => expect(statuses(result)).toEqual(['ready']))
+ const kept = result.current.composerFileAttachments
+ act(() => {
+ result.current.resetComposerAttachments()
+ })
+ expect(result.current.composerFileAttachments).toEqual([])
+
+ act(() => {
+ result.current.handleRestoreComposerFiles(kept)
+ })
+
+ expect(result.current.composerFileAttachments).toEqual(kept)
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 2f97a56eb..264d75b80 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -6,6 +6,7 @@ import {
parseTaskKey,
useAgentTaskTracker,
} from '@contexts/AgentTaskTrackerContext'
+import { COMPOSER_MAX_ATTACHMENTS, COMPOSER_MAX_NON_IMAGE_BODY_BYTES } from '@constants/attachments'
import {
buildChatMessageAttachments,
buildResponseFileAttachments,
@@ -16,6 +17,7 @@ import {
getComposerDraftRevision,
setComposerDraft,
} from '@lib/composerDraftStore'
+import { composerNonImageBodyBytes, formatFileSize } from '@lib/composerFileAdmission'
import { buildComposerFileReferences } from '@lib/composerFileReferences'
import { buildComposerRequestContent } from '@lib/composerReferencesPrompt'
import {
@@ -59,6 +61,7 @@ import type {
FailedAgentSend,
NavItem,
ProgressStep,
+ ReadyComposerFileAttachment,
TaskProgress,
Tone,
} from '../../uiTypes'
@@ -123,6 +126,11 @@ const SERVER_TURN_PAGE_SIZE = 40
const MAX_RECONCILE_DELTA_PAGES = 5
const FILE_REFERENCES_NOT_RECEIVED_MESSAGE =
'The Host did not receive the selected Global Files; the message was sent without them.'
+// #678 D13 — a Host that predates `kind:'file'` either rejects the attachment
+// with LLM_INVALID_ATTACHMENT or drops it and still accepts the message.
+const HOST_FILE_ATTACHMENTS_UNSUPPORTED_MESSAGE =
+ 'The Host does not accept file attachments yet; the message was not delivered with them. Your files are kept so you can retry once the Host is updated.'
+const HOST_INVALID_ATTACHMENT_CODE = 'LLM_INVALID_ATTACHMENT'
const LATEST_PAGE_FALLBACK_WINDOW = Symbol('latest-page-fallback-window')
const DELTA_RECONCILIATION_WINDOW = Symbol('delta-reconciliation-window')
type ReconciliationMessagesResult = SessionMessagesResult & {
@@ -629,6 +637,7 @@ export function useAgentChatController({
const {
composerImageAttachments,
+ composerFileAttachments,
composerAttachmentRevisionRef,
composerReferenceAttachments,
resetComposerAttachments,
@@ -637,10 +646,23 @@ export function useAgentChatController({
handleAddComposerImageAttachments,
handleUpdateComposerImageAttachment,
handleRemoveComposerImageAttachment,
+ handleAddComposerFiles,
+ handleRemoveComposerFileAttachment,
+ handleRestoreComposerFiles,
handleAddComposerReferenceAttachments,
handleRemoveComposerReferenceAttachment,
} = useComposerAttachments({ selectedAgent, clearSendError: clearComposerSendError })
+ // Only a fully read and hashed document can travel with a message (#678).
+ const readyComposerFiles = useMemo(
+ () =>
+ composerFileAttachments.filter(
+ (file): file is ReadyComposerFileAttachment => file.status === 'ready'
+ ),
+ [composerFileAttachments]
+ )
+ const composerFilesBlockSend = composerFileAttachments.some(file => file.status !== 'ready')
+
const { chatEndRef, scrollChatToBottom } = useChatScroll({
selectedAgent,
activeChatId,
@@ -1282,16 +1304,33 @@ export function useAgentChatController({
}, [cancelOlderMessagesLoad, currentTeamId, navItem, selectedAgent])
const mapComposerAttachmentsToHostRequest = (
- attachments: ComposerImageAttachment[]
- ): HostMessageAttachment[] =>
- attachments.map(att => ({
- id: att.id,
- kind: 'image',
- mimeType: att.mimeType,
- encoding: 'base64',
- dataBase64: att.dataBase64,
- filename: att.name,
- }))
+ attachments: ComposerImageAttachment[],
+ files: ReadyComposerFileAttachment[]
+ ): HostMessageAttachment[] => [
+ ...attachments.map(
+ (att): HostMessageAttachment => ({
+ id: att.id,
+ kind: 'image',
+ mimeType: att.mimeType,
+ encoding: 'base64',
+ dataBase64: att.dataBase64,
+ filename: att.name,
+ })
+ ),
+ ...files.map(
+ (file): HostMessageAttachment => ({
+ id: file.id,
+ kind: 'file',
+ filename: file.filename,
+ mimeType: file.declaredMediaType,
+ detectedMediaType: file.classification.detectedMediaType,
+ encoding: 'base64',
+ dataBase64: file.dataBase64,
+ sizeBytes: file.sizeBytes,
+ digest: { algorithm: 'sha256', hex: file.digestHex },
+ })
+ ),
+ ]
// ─── Activity / progress state updaters (hoisted from sendAgentMessage so the
// tracker subscription effect and tracker callbacks can share them) ───
@@ -2002,6 +2041,7 @@ export function useAgentChatController({
setFailedAgentSend({
content: resend.content,
attachments: resend.attachments,
+ files: resend.files,
references: resend.references,
message: result.message,
kind: 'network',
@@ -2494,6 +2534,7 @@ export function useAgentChatController({
async (
content: string,
attachments: ComposerImageAttachment[] = composerImageAttachments,
+ files: ReadyComposerFileAttachment[] = readyComposerFiles,
references: ComposerReferenceAttachment[] = composerReferenceAttachments,
preserveComposer = false
) => {
@@ -2503,10 +2544,14 @@ export function useAgentChatController({
const originalAttachmentRevision = composerAttachmentRevisionRef.current
const trimmedContent = content.trim()
const effectiveAttachments = [...attachments]
+ const effectiveFiles = [...files]
const effectiveReferences = [...references]
if (
!selectedAgent ||
- (!trimmedContent && !effectiveAttachments.length && !effectiveReferences.length)
+ (!trimmedContent &&
+ !effectiveAttachments.length &&
+ !effectiveFiles.length &&
+ !effectiveReferences.length)
)
return
// Per-(agent, chat) re-entry guard: a chat with a running task can't start
@@ -2559,6 +2604,41 @@ export function useAgentChatController({
agentSendInFlightRef.current = false
return
}
+ // #678 — the content the Host receives is fixed before anything is created
+ // or cleared, so the shared per-message limits are checked against the
+ // exact request and a refusal leaves the composer as the user typed it.
+ const baseContentForRequest =
+ trimmedContent ||
+ (effectiveAttachments.length && effectiveFiles.length
+ ? 'Please analyze the attached image(s) and file(s).'
+ : effectiveFiles.length
+ ? 'Please analyze the attached file(s).'
+ : effectiveAttachments.length
+ ? 'Please analyze the attached image(s).'
+ : 'Please use the attached context.')
+ const effectiveContentForRequest = buildComposerRequestContent(
+ baseContentForRequest,
+ effectiveReferences
+ )
+ if (effectiveFiles.length > 0) {
+ const attachmentCount = effectiveAttachments.length + effectiveFiles.length
+ const nonImageBodyBytes = composerNonImageBodyBytes({
+ files: effectiveFiles,
+ textBytes: new TextEncoder().encode(effectiveContentForRequest).length,
+ })
+ const blocker =
+ attachmentCount > COMPOSER_MAX_ATTACHMENTS
+ ? `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`
+ : nonImageBodyBytes > COMPOSER_MAX_NON_IMAGE_BODY_BYTES
+ ? `The attached files and text take more than ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} once encoded. Remove a file or shorten the message.`
+ : null
+ if (blocker) {
+ setAgentError(blocker)
+ pushToast(blocker, 'error')
+ agentSendInFlightRef.current = false
+ return
+ }
+ }
let sendChatId = activeChatId
// Captured for the retention snapshot: the model this attempt actually
// asked for (issue #654), readable from the catch below.
@@ -2607,18 +2687,10 @@ export function useAgentChatController({
}
}
const userMessageId = crypto.randomUUID()
- const baseContentForRequest =
- trimmedContent ||
- (effectiveAttachments.length
- ? 'Please analyze the attached image(s).'
- : 'Please use the attached context.')
- const effectiveContentForRequest = buildComposerRequestContent(
- baseContentForRequest,
- effectiveReferences
- )
const displayAttachments = buildChatMessageAttachments(
effectiveAttachments,
- effectiveReferences
+ effectiveReferences,
+ effectiveFiles
)
const userMessage: AgentChatMessage = {
id: userMessageId,
@@ -2648,9 +2720,11 @@ export function useAgentChatController({
trimmedContent ||
(effectiveAttachments.length > 0
? `Images: ${effectiveAttachments.map(att => att.name).join(', ')}`
- : effectiveReferences.length > 0
- ? `Context: ${effectiveReferences.map(ref => ref.label).join(', ')}`
- : '')
+ : effectiveFiles.length > 0
+ ? `Files: ${effectiveFiles.map(file => file.filename).join(', ')}`
+ : effectiveReferences.length > 0
+ ? `Context: ${effectiveReferences.map(ref => ref.label).join(', ')}`
+ : '')
const autoTitle = truncateTitle(autoTitleSeed)
if (autoTitle) {
// spec 15 §2.5/B19: the client-derived auto-title stays LOCAL only —
@@ -2692,6 +2766,7 @@ export function useAgentChatController({
userMessageId,
content: trimmedContent,
attachments: effectiveAttachments,
+ files: effectiveFiles,
references: effectiveReferences,
model: requestModelForRetention,
reason: 'awaiting_terminal',
@@ -2727,8 +2802,8 @@ export function useAgentChatController({
channelId: sendAgent,
threadId: sendChatId || undefined,
attachments:
- effectiveAttachments.length > 0
- ? mapComposerAttachmentsToHostRequest(effectiveAttachments)
+ effectiveAttachments.length > 0 || effectiveFiles.length > 0
+ ? mapComposerAttachmentsToHostRequest(effectiveAttachments, effectiveFiles)
: undefined,
...(requestModel ? { model: requestModel } : {}),
...(visualModelRevisionForSend === undefined
@@ -2817,6 +2892,26 @@ export function useAgentChatController({
pushToast(droppedMessage, 'error')
}
}
+ // #678 D13 — the same rule for documents. An ack that lists which
+ // attachments the Host admitted and omits a sent file means an older
+ // Host dropped it. Unlike a Global File reference, the document exists
+ // only in this composer, so the send keeps its snapshot and shows the
+ // failure instead of looking delivered.
+ let filesNotDeliveredMessage: string | null = null
+ if (ackOk && !ackIsEmpty && effectiveFiles.length > 0) {
+ const acceptedAttachmentIds = new Set<string>(
+ Array.isArray(response.acceptedAttachmentIds) ? response.acceptedAttachmentIds : []
+ )
+ const undelivered = effectiveFiles.filter(file => !acceptedAttachmentIds.has(file.id))
+ if (undelivered.length > 0) {
+ filesNotDeliveredMessage =
+ undelivered.length === effectiveFiles.length
+ ? HOST_FILE_ATTACHMENTS_UNSUPPORTED_MESSAGE
+ : `The Host did not receive ${undelivered.length} of the attached files. Your files are kept so you can retry.`
+ setAgentError(filesNotDeliveredMessage)
+ pushToast(filesNotDeliveredMessage, 'error')
+ }
+ }
if (!taskId) {
// Synchronous (non-async) response — a direct reply or a structured error.
@@ -2869,13 +2964,20 @@ export function useAgentChatController({
// The input was cleared before the round-trip: retain it (memory
// only) so the failure is recoverable, and restore the text into the
// composer only when it is still the same chat with no newer draft.
- const failureMessage =
- typeof errorRecord?.message === 'string' ? errorRecord.message : content
+ // #678 D13 — a Host that predates `kind:'file'` rejects the send
+ // with LLM_INVALID_ATTACHMENT; say what that means for the files.
+ const hostRejectedFiles =
+ effectiveFiles.length > 0 && errorRecord?.code === HOST_INVALID_ATTACHMENT_CODE
+ const failureMessage = hostRejectedFiles
+ ? HOST_FILE_ATTACHMENTS_UNSUPPORTED_MESSAGE
+ : typeof errorRecord?.message === 'string'
+ ? errorRecord.message
+ : content
failRetainedSend(
sendAgent,
sendChatId ?? null,
userMessageId,
- 'sync_error_envelope',
+ hostRejectedFiles ? 'host_files_unsupported' : 'sync_error_envelope',
failureMessage,
'upstream'
)
@@ -2885,6 +2987,22 @@ export function useAgentChatController({
errorMessage: failureMessage,
}))
pushToast(`Message to ${sendAgent} failed: ${failureMessage || 'error'}`, 'error')
+ } else if (filesNotDeliveredMessage) {
+ // The Host answered but never received the documents: keep the
+ // snapshot (and its files) with the failure instead of releasing it.
+ failRetainedSend(
+ sendAgent,
+ sendChatId ?? null,
+ userMessageId,
+ 'host_files_unsupported',
+ filesNotDeliveredMessage,
+ 'upstream'
+ )
+ updateMessageActivity(sendAgent, userMessageId, previous => ({
+ ...previous,
+ status: 'error',
+ errorMessage: filesNotDeliveredMessage,
+ }))
} else {
// Terminal success: release this payload and the older failures of
// the chat it supersedes.
@@ -2913,6 +3031,16 @@ export function useAgentChatController({
await chatStore.appendMessages(sendAgent, sendChatId, [persistedUserMessage])
}
attachTaskIdToRetainedSend(sendAgent, sendChatId ?? null, userMessageId, taskId)
+ if (filesNotDeliveredMessage) {
+ // The task runs without the documents; the snapshot survives its
+ // success (see releaseSucceededRetainedSendsForTask).
+ markRetainedSendReason(
+ taskId,
+ 'host_files_unsupported',
+ filesNotDeliveredMessage,
+ 'upstream'
+ )
+ }
activityTaskToMessageByAgentRef.current[sendAgent] = {
...(activityTaskToMessageByAgentRef.current[sendAgent] || {}),
[taskId]: userMessageId,
@@ -2935,6 +3063,7 @@ export function useAgentChatController({
tracker.setResend(taskId, {
content: trimmedContent,
attachments: effectiveAttachments,
+ files: effectiveFiles,
references: effectiveReferences,
})
const taskKey = makeTaskKey(sendAgent, sendChatId)
@@ -2975,6 +3104,7 @@ export function useAgentChatController({
setFailedAgentSend({
content: trimmedContent,
attachments: effectiveAttachments,
+ files: effectiveFiles,
references: effectiveReferences,
message,
kind,
@@ -3029,10 +3159,12 @@ export function useAgentChatController({
selectedAgent,
activeChatId,
composerImageAttachments,
+ readyComposerFiles,
composerReferenceAttachments,
chatList,
chatStore,
clearComposerAfterSend,
+ markRetainedSendReason,
fsm,
applyLocalTitleOnly,
pushToast,
@@ -3050,9 +3182,23 @@ export function useAgentChatController({
const handleSendAgentMessage = useCallback(
async (text: string) => {
- await sendAgentMessage(text, composerImageAttachments, composerReferenceAttachments)
+ // A document still being read, or one that failed, is not part of the
+ // message: sending now would silently leave it out.
+ if (composerFilesBlockSend) return
+ await sendAgentMessage(
+ text,
+ composerImageAttachments,
+ readyComposerFiles,
+ composerReferenceAttachments
+ )
},
- [composerImageAttachments, composerReferenceAttachments, sendAgentMessage]
+ [
+ composerFilesBlockSend,
+ composerImageAttachments,
+ readyComposerFiles,
+ composerReferenceAttachments,
+ sendAgentMessage,
+ ]
)
const visibleRetainedFailure = useMemo(() => {
@@ -3083,7 +3229,14 @@ export function useAgentChatController({
if (!visibleFailure) return
const previous = visibleFailure
const retainedBefore = lastRetainedSendIdRef.current
- await sendAgentMessage(previous.content, previous.attachments, previous.references, true)
+ // The retained files are already read and hashed: a retry never reads them again.
+ await sendAgentMessage(
+ previous.content,
+ previous.attachments,
+ previous.files,
+ previous.references,
+ true
+ )
// A retry refused before dispatch (image guard, running task, a send already
// in flight) retained nothing, so the old snapshot is still the only copy of
// the payload: keep it and the failure it shows. Once a newer send has
@@ -3125,6 +3278,7 @@ export function useAgentChatController({
if (
getComposerDraft(activeChatId) ||
composerImageAttachments.length ||
+ composerFileAttachments.length ||
composerReferenceAttachments.length
) {
pushToast('Keep or clear the current draft before recovering the earlier input.', 'error')
@@ -3137,14 +3291,17 @@ export function useAgentChatController({
previewDataUrl: `data:${attachment.mimeType};base64,${attachment.dataBase64}`,
}))
)
+ handleRestoreComposerFiles(visibleFailure.files)
handleAddComposerReferenceAttachments(visibleFailure.references)
handleDiscardFailedAgentSend()
}, [
visibleFailure,
activeChatId,
composerImageAttachments,
+ composerFileAttachments,
composerReferenceAttachments,
pushToast,
+ handleRestoreComposerFiles,
handleAddComposerImageAttachments,
handleAddComposerReferenceAttachments,
handleDiscardFailedAgentSend,
@@ -3322,6 +3479,7 @@ export function useAgentChatController({
sessionFsmStore: fsm,
progressByAgentMessage,
composerImageAttachments,
+ composerFileAttachments,
composerReferenceAttachments,
agentSending,
// A fresh send-time error (a blocked image send, a new POST failure) wins;
@@ -3357,6 +3515,9 @@ export function useAgentChatController({
handleAddComposerImageAttachments,
handleUpdateComposerImageAttachment,
handleRemoveComposerImageAttachment,
+ handleAddComposerFiles,
+ handleRemoveComposerFileAttachment,
+ handleRestoreComposerFiles,
handleAddComposerReferenceAttachments,
handleRemoveComposerReferenceAttachment,
cancelTask,
diff --git a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
index 63a6812c3..99c5b7652 100644
--- a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
+++ b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
@@ -1,7 +1,12 @@
import { useCallback, useEffect, useRef, useState } from 'react'
-import { COMPOSER_MAX_IMAGE_ATTACHMENTS } from '@constants/attachments'
+import { COMPOSER_MAX_ATTACHMENTS } from '@constants/attachments'
import { clearComposerDraft, clearComposerDraftAfterSend } from '@lib/composerDraftStore'
-import type { ComposerImageAttachment, ComposerReferenceAttachment } from '../../uiTypes'
+import { composerFileAdmissionError, readComposerFile } from '@lib/composerFileAdmission'
+import type {
+ ComposerFileAttachment,
+ ComposerImageAttachment,
+ ComposerReferenceAttachment,
+} from '../../uiTypes'
interface UseComposerAttachmentsParams {
/** Attachments are per-agent; switching agents clears the pending composer. */
@@ -32,8 +37,26 @@ export function useComposerAttachments({
const [composerReferenceAttachments, setComposerReferenceAttachments] = useState<
ComposerReferenceAttachment[]
>([])
+ const [composerFileAttachments, setComposerFileAttachments] = useState<ComposerFileAttachment[]>(
+ []
+ )
const composerAttachmentOrderRef = useRef(0)
const composerAttachmentRevisionRef = useRef(0)
+ // Mirrors of the state above. Admission of a file and the completion of its
+ // asynchronous read decide from the latest attachments, not from the render
+ // that started them.
+ const composerFilesRef = useRef<ComposerFileAttachment[]>([])
+ const composerImageCountRef = useRef(0)
+ composerImageCountRef.current = composerImageAttachments.length
+
+ const commitComposerFiles = useCallback(
+ (update: (previous: ComposerFileAttachment[]) => ComposerFileAttachment[]) => {
+ const next = update(composerFilesRef.current)
+ composerFilesRef.current = next
+ setComposerFileAttachments(next)
+ },
+ []
+ )
const revokeComposerPreviewUrls = useCallback((attachments: ComposerImageAttachment[]) => {
composerAttachmentRevisionRef.current += 1
@@ -58,11 +81,18 @@ export function useComposerAttachments({
})
}, [revokeComposerPreviewUrls])
- /** Clear BOTH pending attachment kinds (revoking image blob URLs). */
+ const clearComposerFileAttachments = useCallback(() => {
+ composerAttachmentRevisionRef.current += 1
+ // A read still in flight finds no entry for its id and is dropped.
+ commitComposerFiles(() => [])
+ }, [commitComposerFiles])
+
+ /** Clear every pending attachment kind (revoking image blob URLs). */
const resetComposerAttachments = useCallback(() => {
clearComposerImageAttachments()
+ clearComposerFileAttachments()
setComposerReferenceAttachments([])
- }, [clearComposerImageAttachments])
+ }, [clearComposerFileAttachments, clearComposerImageAttachments])
/** Post-send cleanup: clear the persisted draft for this chat, then the pending
* attachments. Combines the three original send-path calls into one. */
@@ -89,7 +119,11 @@ export function useComposerAttachments({
setComposerImageAttachments(previous => {
const next = [...previous]
for (const attachment of attachments) {
- if (next.length >= COMPOSER_MAX_IMAGE_ATTACHMENTS) {
+ // Images and files share one per-message count.
+ const attachedFiles = composerFilesRef.current.filter(
+ file => file.status !== 'failed'
+ ).length
+ if (next.length + attachedFiles >= COMPOSER_MAX_ATTACHMENTS) {
revokeComposerPreviewUrls([attachment])
continue
}
@@ -152,6 +186,83 @@ export function useComposerAttachments({
[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 appears as `failed` with the reason, so a refusal is
+ * never silent; the user removes it to continue.
+ */
+ const handleAddComposerFiles = useCallback(
+ (files: File[], textBytes: number) => {
+ composerAttachmentRevisionRef.current += 1
+ if (!files.length) return
+ for (const file of files) {
+ const id = crypto.randomUUID()
+ const current = composerFilesRef.current
+ const accepted = current.filter(item => item.status !== 'failed')
+ const error = composerFileAdmissionError(file, {
+ attachedCount: composerImageCountRef.current + accepted.length,
+ files: accepted,
+ textBytes,
+ })
+ composerAttachmentOrderRef.current += 1
+ const base = {
+ id,
+ addedOrder: composerAttachmentOrderRef.current,
+ type: 'file' as const,
+ filename: file.name,
+ sizeBytes: file.size,
+ declaredMediaType: file.type,
+ }
+ if (error) {
+ commitComposerFiles(previous => [...previous, { ...base, status: 'failed', error }])
+ continue
+ }
+ commitComposerFiles(previous => [...previous, { ...base, status: 'reading' }])
+ void readComposerFile(file, id).then(result => {
+ commitComposerFiles(previous => {
+ if (!previous.some(item => item.id === id)) return previous
+ if (
+ result.status === 'ready' &&
+ previous.some(
+ item =>
+ item.id !== id &&
+ item.status === 'ready' &&
+ item.filename === result.filename &&
+ item.digestHex === result.digestHex
+ )
+ ) {
+ return previous.filter(item => item.id !== id)
+ }
+ return previous.map(item =>
+ item.id === id ? { ...result, addedOrder: item.addedOrder } : item
+ )
+ })
+ })
+ }
+ clearSendError()
+ },
+ [clearSendError, commitComposerFiles]
+ )
+
+ const handleRemoveComposerFileAttachment = useCallback(
+ (attachmentId: string) => {
+ composerAttachmentRevisionRef.current += 1
+ commitComposerFiles(previous => previous.filter(item => item.id !== attachmentId))
+ clearSendError()
+ },
+ [clearSendError, commitComposerFiles]
+ )
+
+ /** Puts back files that were attached to a send that failed. */
+ const handleRestoreComposerFiles = useCallback(
+ (files: ComposerFileAttachment[]) => {
+ composerAttachmentRevisionRef.current += 1
+ commitComposerFiles(() => files)
+ },
+ [commitComposerFiles]
+ )
+
const handleAddComposerReferenceAttachments = useCallback(
(attachments: ComposerReferenceAttachment[]) => {
composerAttachmentRevisionRef.current += 1
@@ -187,6 +298,7 @@ export function useComposerAttachments({
return {
composerImageAttachments,
+ composerFileAttachments,
composerAttachmentRevisionRef,
composerReferenceAttachments,
resetComposerAttachments,
@@ -195,6 +307,9 @@ export function useComposerAttachments({
handleAddComposerImageAttachments,
handleUpdateComposerImageAttachment,
handleRemoveComposerImageAttachment,
+ handleAddComposerFiles,
+ handleRemoveComposerFileAttachment,
+ handleRestoreComposerFiles,
handleAddComposerReferenceAttachments,
handleRemoveComposerReferenceAttachment,
}
diff --git a/desktop-app/ui/src/hooks/useAgentChatActionsValue.ts b/desktop-app/ui/src/hooks/useAgentChatActionsValue.ts
index 739d9c2dc..54c185ee5 100644
--- a/desktop-app/ui/src/hooks/useAgentChatActionsValue.ts
+++ b/desktop-app/ui/src/hooks/useAgentChatActionsValue.ts
@@ -29,6 +29,11 @@ export function useAgentChatActionsValue(vm: AppVm): AgentChatActionsContextValu
const handleRemoveComposerImageAttachment = useStableCallback(
vm.handleRemoveComposerImageAttachment
)
+ const handleAddComposerFiles = useStableCallback(vm.handleAddComposerFiles)
+ const handleRemoveComposerFileAttachment = useStableCallback(
+ vm.handleRemoveComposerFileAttachment
+ )
+ const handleRestoreComposerFiles = useStableCallback(vm.handleRestoreComposerFiles)
const handleAddComposerReferenceAttachments = useStableCallback(
vm.handleAddComposerReferenceAttachments
)
@@ -54,6 +59,9 @@ export function useAgentChatActionsValue(vm: AppVm): AgentChatActionsContextValu
handleAddComposerImageAttachments,
handleUpdateComposerImageAttachment,
handleRemoveComposerImageAttachment,
+ handleAddComposerFiles,
+ handleRemoveComposerFileAttachment,
+ handleRestoreComposerFiles,
handleAddComposerReferenceAttachments,
handleRemoveComposerReferenceAttachment,
handleSendAgentMessage,
@@ -74,6 +82,9 @@ export function useAgentChatActionsValue(vm: AppVm): AgentChatActionsContextValu
handleAddComposerImageAttachments,
handleUpdateComposerImageAttachment,
handleRemoveComposerImageAttachment,
+ handleAddComposerFiles,
+ handleRemoveComposerFileAttachment,
+ handleRestoreComposerFiles,
handleAddComposerReferenceAttachments,
handleRemoveComposerReferenceAttachment,
handleSendAgentMessage,
diff --git a/desktop-app/ui/src/hooks/useAppController.ts b/desktop-app/ui/src/hooks/useAppController.ts
index 99965d61f..b9a34d81a 100644
--- a/desktop-app/ui/src/hooks/useAppController.ts
+++ b/desktop-app/ui/src/hooks/useAppController.ts
@@ -1406,6 +1406,7 @@ export function useAppController() {
hasOlderMessages: chat.hasOlderMessages,
olderMessagesLoading: chat.olderMessagesLoading,
composerImageAttachments: chat.composerImageAttachments,
+ composerFileAttachments: chat.composerFileAttachments,
composerReferenceAttachments: chat.composerReferenceAttachments,
agentSending: chat.agentSending,
agentError: chat.agentError,
@@ -1432,6 +1433,9 @@ export function useAppController() {
handleAddComposerImageAttachments: chat.handleAddComposerImageAttachments,
handleUpdateComposerImageAttachment: chat.handleUpdateComposerImageAttachment,
handleRemoveComposerImageAttachment: chat.handleRemoveComposerImageAttachment,
+ handleAddComposerFiles: chat.handleAddComposerFiles,
+ handleRemoveComposerFileAttachment: chat.handleRemoveComposerFileAttachment,
+ handleRestoreComposerFiles: chat.handleRestoreComposerFiles,
handleAddComposerReferenceAttachments: chat.handleAddComposerReferenceAttachments,
handleRemoveComposerReferenceAttachment: chat.handleRemoveComposerReferenceAttachment,
cancelTask: chat.cancelTask,
diff --git a/desktop-app/ui/src/lib/__tests__/chatMessageAttachments.test.ts b/desktop-app/ui/src/lib/__tests__/chatMessageAttachments.test.ts
index b7aedb3d9..7d0e8d49c 100644
--- a/desktop-app/ui/src/lib/__tests__/chatMessageAttachments.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/chatMessageAttachments.test.ts
@@ -148,7 +148,7 @@ describe('chat message attachments', () => {
]
expect(
- buildChatMessageAttachments(images, references).map(attachment => attachment.label)
+ buildChatMessageAttachments(images, references, []).map(attachment => attachment.label)
).toEqual(['first', 'logo.png', 'second'])
})
diff --git a/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts b/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts
new file mode 100644
index 000000000..07f93cd61
--- /dev/null
+++ b/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts
@@ -0,0 +1,202 @@
+import { describe, expect, it } from 'vitest'
+import { createHash } from 'node:crypto'
+import {
+ COMPOSER_FILE_ENTRY_METADATA_BYTES,
+ COMPOSER_MAX_ATTACHMENTS,
+ COMPOSER_MAX_FILE_BYTES,
+ COMPOSER_MAX_NON_IMAGE_BODY_BYTES,
+ COMPOSER_REQUEST_ENVELOPE_BYTES,
+} from '@constants/attachments'
+import {
+ base64Length,
+ composerFileAdmissionError,
+ composerFileEntryBytes,
+ composerNonImageBodyBytes,
+ fileNameProblem,
+ readComposerFile,
+} from '../composerFileAdmission'
+
+const EMPTY_CONTEXT = { attachedCount: 0, files: [], textBytes: 0 }
+
+function pdfBytes(): Uint8Array {
+ return new TextEncoder().encode('%PDF-1.7\n1 0 obj\n<<>>\nendobj\n%%EOF\n')
+}
+
+function toBuffer(bytes: Uint8Array): ArrayBuffer {
+ return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer
+}
+
+describe('readComposerFile (#678)', () => {
+ it('reads a PDF into base64 with the sha256 the host recomputes', async () => {
+ const bytes = pdfBytes()
+ const file = new File([toBuffer(bytes)], 'report.pdf', { type: 'application/pdf' })
+
+ const result = await readComposerFile(file, 'file-1')
+
+ expect(result.status).toBe('ready')
+ if (result.status !== 'ready') throw new Error('unreachable: asserted ready above')
+ expect(result.dataBase64).toBe(Buffer.from(bytes).toString('base64'))
+ expect(result.digestHex).toBe(createHash('sha256').update(bytes).digest('hex'))
+ expect(result.sizeBytes).toBe(bytes.byteLength)
+ expect(result.declaredMediaType).toBe('application/pdf')
+ expect(result.classification.class).toBe('pdf')
+ expect(result.classification.mismatch).toBe(false)
+ })
+
+ it('reports a mismatch when the bytes are not what the name and type claim', async () => {
+ const file = new File([toBuffer(pdfBytes())], 'notes.txt', { type: 'text/plain' })
+
+ const result = await readComposerFile(file, 'file-2')
+
+ if (result.status !== 'ready') throw new Error(`expected ready, got ${result.status}`)
+ expect(result.classification.class).toBe('pdf')
+ expect(result.classification.mismatch).toBe(true)
+ })
+
+ it('accepts a zero-byte file with the canonical empty base64', async () => {
+ const result = await readComposerFile(
+ new File([], 'empty.txt', { type: 'text/plain' }),
+ 'file-3'
+ )
+
+ if (result.status !== 'ready') throw new Error(`expected ready, got ${result.status}`)
+ expect(result.dataBase64).toBe('')
+ expect(result.sizeBytes).toBe(0)
+ expect(result.digestHex).toBe(createHash('sha256').update(new Uint8Array(0)).digest('hex'))
+ })
+
+ it('encodes a file larger than one base64 chunk without altering it', async () => {
+ const bytes = new Uint8Array(0x8000 * 3 + 5).map((_, index) => (index * 31) % 251)
+ const result = await readComposerFile(new File([toBuffer(bytes)], 'blob.bin'), 'file-4')
+
+ if (result.status !== 'ready') throw new Error(`expected ready, got ${result.status}`)
+ expect(result.dataBase64).toBe(Buffer.from(bytes).toString('base64'))
+ expect(result.dataBase64.length).toBe(base64Length(bytes.byteLength))
+ })
+
+ it('fails, and never sends, a file whose bytes differ in length from the reported size', async () => {
+ const bytes = pdfBytes()
+ const file = new File([toBuffer(bytes)], 'moving.pdf', { type: 'application/pdf' })
+ Object.defineProperty(file, 'size', { value: bytes.byteLength + 7 })
+
+ const result = await readComposerFile(file, 'file-5')
+
+ expect(result.status).toBe('failed')
+ if (result.status !== 'failed') throw new Error('unreachable: asserted failed above')
+ expect(result.error).toContain('changed while it was being read')
+ })
+
+ it('fails with the read error when the file cannot be read', async () => {
+ const file = new File([toBuffer(pdfBytes())], 'gone.pdf')
+ Object.defineProperty(file, 'arrayBuffer', {
+ value: () => Promise.reject(new Error('NotReadableError')),
+ })
+
+ const result = await readComposerFile(file, 'file-6')
+
+ expect(result.status).toBe('failed')
+ if (result.status !== 'failed') throw new Error('unreachable: asserted failed above')
+ expect(result.error).toBe('gone.pdf could not be read: NotReadableError')
+ })
+})
+
+describe('composerFileAdmissionError (#678)', () => {
+ it('admits a file that fits every limit', () => {
+ expect(composerFileAdmissionError({ name: 'a.pdf', size: 1024 }, EMPTY_CONTEXT)).toBeNull()
+ })
+
+ it('admits exactly the per-file maximum and rejects one byte more', () => {
+ expect(
+ composerFileAdmissionError({ name: 'a.bin', size: COMPOSER_MAX_FILE_BYTES }, EMPTY_CONTEXT)
+ ).toBeNull()
+ expect(
+ composerFileAdmissionError(
+ { name: 'a.bin', size: COMPOSER_MAX_FILE_BYTES + 1 },
+ EMPTY_CONTEXT
+ )
+ ).toContain('a file can be at most 3.0 MiB')
+ })
+
+ it('rejects the 21st attachment and admits the 20th', () => {
+ expect(
+ composerFileAdmissionError(
+ { name: 'a.pdf', size: 10 },
+ { ...EMPTY_CONTEXT, attachedCount: COMPOSER_MAX_ATTACHMENTS - 1 }
+ )
+ ).toBeNull()
+ expect(
+ composerFileAdmissionError(
+ { name: 'a.pdf', size: 10 },
+ { ...EMPTY_CONTEXT, attachedCount: COMPOSER_MAX_ATTACHMENTS }
+ )
+ ).toContain(`at most ${COMPOSER_MAX_ATTACHMENTS} attachments`)
+ })
+
+ it('rejects a file whose base64 no longer fits the non-image share of the body', () => {
+ const first = { filename: 'first.bin', sizeBytes: COMPOSER_MAX_FILE_BYTES }
+ // Liveness witness: the first file alone is admitted, so the rejection below
+ // comes from the budget check and not from a per-file or count limit.
+ expect(
+ composerFileAdmissionError(
+ { name: 'first.bin', size: COMPOSER_MAX_FILE_BYTES },
+ EMPTY_CONTEXT
+ )
+ ).toBeNull()
+ expect(
+ composerFileAdmissionError(
+ { name: 'second.bin', size: COMPOSER_MAX_FILE_BYTES },
+ { attachedCount: 1, files: [first], textBytes: 0 }
+ )
+ ).toContain('does not fit')
+ })
+
+ it('counts the message text against the same share', () => {
+ const file = { name: 'a.bin', size: 1024 }
+ expect(composerFileAdmissionError(file, EMPTY_CONTEXT)).toBeNull()
+ expect(
+ composerFileAdmissionError(file, {
+ ...EMPTY_CONTEXT,
+ textBytes: COMPOSER_MAX_NON_IMAGE_BODY_BYTES,
+ })
+ ).toContain('does not fit')
+ })
+
+ it.each([
+ ['', 'has no name'],
+ ['a/b.pdf', 'has a name with "/" or control characters'],
+ ['a\u0007.pdf', 'has a name with "/" or control characters'],
+ ['..', 'has an invalid name'],
+ ['é.txt', 'has a name that is not NFC-normalized'],
+ ['x'.repeat(256), 'has a name longer than 255 characters'],
+ ])('rejects the name %j', (name, problem) => {
+ expect(fileNameProblem(name)).toBe(problem)
+ expect(composerFileAdmissionError({ name, size: 1 }, EMPTY_CONTEXT)).toContain(problem)
+ })
+
+ it('accepts a 255 code point name and a non-ASCII NFC name', () => {
+ expect(fileNameProblem('x'.repeat(255))).toBeNull()
+ expect(fileNameProblem('informe-ñandú.docx')).toBeNull()
+ })
+})
+
+describe('composerNonImageBodyBytes (#678)', () => {
+ it('adds the envelope, the text and the base64 plus metadata of each file', () => {
+ const file = { filename: 'a.pdf', sizeBytes: 1000 }
+ expect(composerNonImageBodyBytes({ files: [file], textBytes: 50 })).toBe(
+ COMPOSER_REQUEST_ENVELOPE_BYTES +
+ 50 +
+ base64Length(1000) +
+ JSON.stringify('a.pdf').length +
+ COMPOSER_FILE_ENTRY_METADATA_BYTES
+ )
+ expect(composerFileEntryBytes(file)).toBe(
+ base64Length(1000) + JSON.stringify('a.pdf').length + COMPOSER_FILE_ENTRY_METADATA_BYTES
+ )
+ })
+
+ it('measures a multi-byte name in UTF-8 bytes', () => {
+ const ascii = composerFileEntryBytes({ filename: 'aaaa.txt', sizeBytes: 0 })
+ const accented = composerFileEntryBytes({ filename: 'ññññ.txt', sizeBytes: 0 })
+ expect(accented - ascii).toBe(4)
+ })
+})
diff --git a/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts b/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
index a8bfc86e4..4480b9242 100644
--- a/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
@@ -12,6 +12,7 @@ function snapshot(
userMessageId,
content: userMessageId,
attachments: [],
+ files: [],
references: [],
reason: 'post_failed',
timestamp,
@@ -89,3 +90,44 @@ describe('retainedSendStore — superseded failures (#654 M2)', () => {
expect(store.getLatestRetainedSendSnapshotForChat('agent-x', 'chat-1')).toBeUndefined()
})
})
+
+describe('retainedSendStore — documents the Host never received (#678 D13)', () => {
+ it('keeps the snapshot of a task that succeeded without its files', () => {
+ const store = createRetainedSendStore(vi.fn())
+ store.retainSendSnapshot(snapshot('older', 100))
+ store.retainSendSnapshot(
+ snapshot('with-files', 200, { reason: 'awaiting_terminal', failure: undefined })
+ )
+ store.attachTaskIdToRetainedSend('agent-x', 'chat-1', 'with-files', 'task-files')
+ store.markRetainedSendReason(
+ 'task-files',
+ 'host_files_unsupported',
+ 'The Host does not accept file attachments yet.',
+ 'upstream'
+ )
+
+ store.releaseSucceededRetainedSendsForTask('task-files')
+
+ // Liveness witness: the flagged snapshot is still there, marked with its reason.
+ const kept = store.getRetainedSendSnapshot('agent-x', 'chat-1', 'with-files')
+ expect(kept?.reason).toBe('host_files_unsupported')
+ // The success released nobody, not even the older failure it would supersede.
+ expect(held(store, ['chat-1/older', 'chat-1/with-files'])).toEqual([
+ 'chat-1/older',
+ 'chat-1/with-files',
+ ])
+ })
+
+ it('still releases a succeeded task whose files reached the Host', () => {
+ const store = createRetainedSendStore(vi.fn())
+ store.retainSendSnapshot(
+ snapshot('delivered', 200, { reason: 'awaiting_terminal', failure: undefined })
+ )
+ store.attachTaskIdToRetainedSend('agent-x', 'chat-1', 'delivered', 'task-ok')
+ expect(held(store, ['chat-1/delivered'])).toEqual(['chat-1/delivered'])
+
+ store.releaseSucceededRetainedSendsForTask('task-ok')
+
+ expect(held(store, ['chat-1/delivered'])).toEqual([])
+ })
+})
diff --git a/desktop-app/ui/src/lib/chatMessageAttachments.ts b/desktop-app/ui/src/lib/chatMessageAttachments.ts
index e89c8aeaa..39c0da8bb 100644
--- a/desktop-app/ui/src/lib/chatMessageAttachments.ts
+++ b/desktop-app/ui/src/lib/chatMessageAttachments.ts
@@ -1,5 +1,9 @@
import type { ChatMessageAttachment } from '../../../src/types'
-import type { ComposerImageAttachment, ComposerReferenceAttachment } from '../uiTypes'
+import type {
+ ComposerImageAttachment,
+ ComposerReferenceAttachment,
+ ReadyComposerFileAttachment,
+} from '../uiTypes'
type ParsedChatMessageDisplay = {
content: string
@@ -10,7 +14,7 @@ function attachmentOrder(value: { addedOrder?: number }, fallbackIndex: number):
return value.addedOrder ?? fallbackIndex
}
-function formatUploadedFileTooltip(attachment: ComposerImageAttachment): string {
+function formatUploadedFileTooltip(attachment: { sizeBytes: number }): string {
return `Uploaded File - ${Math.max(1, Math.round(attachment.sizeBytes / 1024))} KB`
}
@@ -131,7 +135,8 @@ function parseAttachmentList(value: string): ParsedAttachmentList {
export function buildChatMessageAttachments(
imageAttachments: ComposerImageAttachment[],
- referenceAttachments: ComposerReferenceAttachment[]
+ referenceAttachments: ComposerReferenceAttachment[],
+ fileAttachments: ReadyComposerFileAttachment[]
): ChatMessageAttachment[] {
const referenceItems = referenceAttachments.map((attachment, index): ChatMessageAttachment => {
const type = attachment.type
@@ -160,7 +165,17 @@ export function buildChatMessageAttachments(
addedOrder: attachmentOrder(attachment, fallbackIndex),
}
})
- return [...referenceItems, ...imageItems].sort(
+ const fileItems = fileAttachments.map((attachment, index): ChatMessageAttachment => {
+ const fallbackIndex = referenceItems.length + imageItems.length + index
+ return {
+ id: attachment.id,
+ type: 'uploaded_file',
+ label: attachment.filename,
+ tooltip: formatUploadedFileTooltip(attachment),
+ addedOrder: attachmentOrder(attachment, fallbackIndex),
+ }
+ })
+ return [...referenceItems, ...imageItems, ...fileItems].sort(
(a, b) => attachmentOrder(a, 0) - attachmentOrder(b, 0)
)
}
diff --git a/desktop-app/ui/src/lib/composerFileAdmission.ts b/desktop-app/ui/src/lib/composerFileAdmission.ts
new file mode 100644
index 000000000..30a897e05
--- /dev/null
+++ b/desktop-app/ui/src/lib/composerFileAdmission.ts
@@ -0,0 +1,151 @@
+import { classifyBytes } from '@clerum/gfs-interaction-policy'
+import {
+ COMPOSER_FILE_ENTRY_METADATA_BYTES,
+ COMPOSER_MAX_ATTACHMENTS,
+ COMPOSER_MAX_FILE_BYTES,
+ COMPOSER_MAX_NON_IMAGE_BODY_BYTES,
+ COMPOSER_REQUEST_ENVELOPE_BYTES,
+} from '@constants/attachments'
+import type { ComposerFileAttachment } from '../uiTypes'
+
+const FILE_NAME_MAX_CODE_POINTS = 255
+const BASE64_CHUNK_BYTES = 0x8000
+// eslint-disable-next-line no-control-regex -- the host rejects control characters in a file name
+const CONTROL_OR_SEPARATOR = /[\u0000-\u001f\u007f/]/
+
+const textEncoder = new TextEncoder()
+
+export type ComposerFileAdmissionContext = {
+ /** Images and files already attached to the message. */
+ attachedCount: number
+ /** Files already attached; their base64 counts against the non-image share. */
+ files: ReadonlyArray<Pick<ComposerFileAttachment, 'filename' | 'sizeBytes'>>
+ /** UTF-8 length of the message text as it will be sent. */
+ textBytes: number
+}
+
+export function formatFileSize(bytes: number): string {
+ if (bytes < 1024) return `${bytes} B`
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KiB`
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MiB`
+}
+
+/** Length of the canonical base64 of `sizeBytes` bytes. */
+export function base64Length(sizeBytes: number): number {
+ return Math.ceil(sizeBytes / 3) * 4
+}
+
+/** Bytes one `kind:'file'` entry adds to the JSON request body. */
+export function composerFileEntryBytes(file: { filename: string; sizeBytes: number }): number {
+ return (
+ base64Length(file.sizeBytes) +
+ textEncoder.encode(JSON.stringify(file.filename)).length +
+ COMPOSER_FILE_ENTRY_METADATA_BYTES
+ )
+}
+
+/**
+ * Bytes of one request body that rpc-proxy and mcp-host do not credit to the
+ * image quota: the JSON envelope, the message text and every `kind:'file'`.
+ */
+export function composerNonImageBodyBytes(input: {
+ files: ReadonlyArray<{ filename: string; sizeBytes: number }>
+ textBytes: number
+}): number {
+ return (
+ COMPOSER_REQUEST_ENVELOPE_BYTES +
+ input.textBytes +
+ input.files.reduce((total, file) => total + composerFileEntryBytes(file), 0)
+ )
+}
+
+export function fileNameProblem(filename: string): string | null {
+ if (filename.length === 0) return 'has no name'
+ if (filename.normalize('NFC') !== filename) return 'has a name that is not NFC-normalized'
+ if ([...filename].length > FILE_NAME_MAX_CODE_POINTS) {
+ return `has a name longer than ${FILE_NAME_MAX_CODE_POINTS} characters`
+ }
+ if (filename === '.' || filename === '..') return 'has an invalid name'
+ if (CONTROL_OR_SEPARATOR.test(filename)) return 'has a name with "/" or control characters'
+ return null
+}
+
+/**
+ * Checks a picked file against the per-message limits before any byte is
+ * read. Returns the reason to show the user, or `null` when it can be read.
+ */
+export function composerFileAdmissionError(
+ file: { name: string; size: number },
+ context: ComposerFileAdmissionContext
+): string | null {
+ const nameProblem = fileNameProblem(file.name)
+ if (nameProblem) return `${file.name || 'The file'} ${nameProblem}.`
+ if (file.size > COMPOSER_MAX_FILE_BYTES) {
+ return `${file.name} is ${formatFileSize(file.size)}; a file can be at most ${formatFileSize(COMPOSER_MAX_FILE_BYTES)}.`
+ }
+ if (context.attachedCount >= COMPOSER_MAX_ATTACHMENTS) {
+ return `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`
+ }
+ const bodyBytes = composerNonImageBodyBytes({
+ files: [...context.files, { filename: file.name, sizeBytes: file.size }],
+ textBytes: context.textBytes,
+ })
+ if (bodyBytes > COMPOSER_MAX_NON_IMAGE_BODY_BYTES) {
+ return `${file.name} does not fit: files and text can take at most ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} per message once encoded.`
+ }
+ return null
+}
+
+function toBase64(bytes: Uint8Array): string {
+ let binary = ''
+ for (let offset = 0; offset < bytes.length; offset += BASE64_CHUNK_BYTES) {
+ binary += String.fromCharCode(...bytes.subarray(offset, offset + BASE64_CHUNK_BYTES))
+ }
+ return btoa(binary)
+}
+
+async function sha256Hex(bytes: Uint8Array<ArrayBuffer>): Promise<string> {
+ const digest = await crypto.subtle.digest('SHA-256', bytes)
+ return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('')
+}
+
+/**
+ * Reads a picked file completely and returns it `ready` to send, or `failed`
+ * with the reason. The bytes read must match the size the picker reported: a
+ * file that changes on disk while it is read is not sent.
+ */
+export async function readComposerFile(file: File, id: string): Promise<ComposerFileAttachment> {
+ const base = {
+ id,
+ type: 'file' as const,
+ filename: file.name,
+ sizeBytes: file.size,
+ declaredMediaType: file.type,
+ }
+ try {
+ const bytes = new Uint8Array(await file.arrayBuffer())
+ if (bytes.byteLength !== file.size) {
+ return {
+ ...base,
+ status: 'failed',
+ error: `${file.name} changed while it was being read. Attach it again.`,
+ }
+ }
+ const classification = classifyBytes({
+ bytes,
+ totalByteLength: bytes.byteLength,
+ declaredMediaType: file.type === '' ? null : file.type,
+ filename: file.name,
+ })
+ return {
+ ...base,
+ status: 'ready',
+ classification,
+ dataBase64: toBase64(bytes),
+ digestHex: await sha256Hex(bytes),
+ }
+ } catch (error) {
+ const reason = error instanceof Error ? error.message : String(error)
+ return { ...base, status: 'failed', error: `${file.name} could not be read: ${reason}` }
+ }
+}
diff --git a/desktop-app/ui/src/lib/retainedSendStore.ts b/desktop-app/ui/src/lib/retainedSendStore.ts
index e12d7d4ea..647f3d4b4 100644
--- a/desktop-app/ui/src/lib/retainedSendStore.ts
+++ b/desktop-app/ui/src/lib/retainedSendStore.ts
@@ -23,6 +23,7 @@ import type {
ComposerImageAttachment,
ComposerReferenceAttachment,
FailedAgentSend,
+ ReadyComposerFileAttachment,
} from '../uiTypes'
export interface RetainedSendSnapshot {
@@ -33,6 +34,8 @@ export interface RetainedSendSnapshot {
taskId?: string
content: string
attachments: ComposerImageAttachment[]
+ /** Documents already read and hashed; a retry sends them without reading again. */
+ files: ReadyComposerFileAttachment[]
references: ComposerReferenceAttachment[]
/** Model the guard validated for this send, when one was captured. */
model?: string
@@ -45,7 +48,13 @@ export interface RetainedSendSnapshot {
export type RetainedSendReason =
/** Retained as soon as the composer cleared; outcome still unknown. */
- 'awaiting_terminal' | 'post_failed' | 'sync_error_envelope' | 'async_task_failed' | 'stream_lost'
+ | 'awaiting_terminal'
+ | 'post_failed'
+ | 'sync_error_envelope'
+ | 'async_task_failed'
+ | 'stream_lost'
+ /** The Host did not confirm every attached document (#678, D13). */
+ | 'host_files_unsupported'
/** Each controller owns its bytes; no module-global data survives an identity change. */
export function createRetainedSendStore(changed: () => void) {
@@ -154,13 +163,19 @@ export function createRetainedSendStore(changed: () => void) {
releaseRetainedSend(agentRef, chatId, userMessageId)
}
- /** Task-based counterpart of `releaseSucceededRetainedSend`. */
+ /**
+ * Task-based counterpart of `releaseSucceededRetainedSend`. A task that
+ * succeeded without the documents the Host never received keeps its snapshot:
+ * those files exist nowhere else.
+ */
function releaseSucceededRetainedSendsForTask(taskId: string): void {
- const succeeded = [...snapshots.values()].filter(snapshot => snapshot.taskId === taskId)
+ const succeeded = [...snapshots.values()].filter(
+ snapshot => snapshot.taskId === taskId && snapshot.reason !== 'host_files_unsupported'
+ )
for (const snapshot of succeeded) {
releaseRetainedFailuresForChat(snapshot.agentRef, snapshot.chatId, snapshot.timestamp)
+ releaseRetainedSend(snapshot.agentRef, snapshot.chatId, snapshot.userMessageId)
}
- releaseRetainedSendsForTask(taskId)
}
/**
diff --git a/desktop-app/ui/src/styles.css b/desktop-app/ui/src/styles.css
index 661811eba..e4deeb422 100644
--- a/desktop-app/ui/src/styles.css
+++ b/desktop-app/ui/src/styles.css
@@ -8058,6 +8058,20 @@ h3 {
text-overflow: ellipsis;
}
+.composer-attachment-chip-meta {
+ display: block;
+ font-size: var(--font-size-2xs);
+ line-height: 1.2;
+ color: var(--text-dim);
+ white-space: nowrap;
+ overflow: hidden;
+ text-overflow: ellipsis;
+}
+
+.composer-attachment-chip-meta--failed {
+ color: var(--danger);
+}
+
.composer-attachment-remove {
border: none;
background: transparent;
diff --git a/desktop-app/ui/src/uiTypes.ts b/desktop-app/ui/src/uiTypes.ts
index 0cca0a293..028d8f32d 100644
--- a/desktop-app/ui/src/uiTypes.ts
+++ b/desktop-app/ui/src/uiTypes.ts
@@ -1,3 +1,4 @@
+import type { FileClassification } from '@clerum/gfs-interaction-policy'
import type {
ChatMessageAttachment,
HostActivityEvent,
@@ -159,6 +160,7 @@ export type AppErrorKind = 'network' | 'auth' | 'validation' | 'upstream' | 'wak
export type FailedAgentSend = {
content: string
attachments: ComposerImageAttachment[]
+ files: ReadyComposerFileAttachment[]
references: ComposerReferenceAttachment[]
message: string
kind: AppErrorKind
@@ -259,6 +261,38 @@ export type ComposerImageAttachment = {
previewDataUrl: string
}
+type ComposerFileAttachmentBase = {
+ id: string
+ addedOrder?: number
+ type: 'file'
+ filename: string
+ /** Size the picker reported; the bytes read must match it. */
+ sizeBytes: number
+ /** Media type the browser attached to the file; empty when it has none. */
+ declaredMediaType: string
+}
+
+/**
+ * A document picked for the current message (#678). It is read completely in
+ * the renderer (`selected` → `reading`) before it can be sent; only a `ready`
+ * file travels, as an inline `kind:'file'` attachment. `failed` keeps the
+ * reason on screen until the user removes the chip.
+ */
+export type ComposerFileAttachment = ComposerFileAttachmentBase &
+ (
+ | { status: 'selected' | 'reading' }
+ | { status: 'failed'; error: string }
+ | {
+ status: 'ready'
+ classification: FileClassification
+ dataBase64: string
+ digestHex: string
+ }
+ )
+
+/** A document that is fully read and hashed: the only state that can be sent. */
+export type ReadyComposerFileAttachment = Extract<ComposerFileAttachment, { status: 'ready' }>
+
export type ComposerPluginReference = {
id: string
addedOrder?: number