Skip to content

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

TypeScript

fix(desktop-ui): compose a picked file name to NFC at intake

Alfredo Lopez0842d7c9faf3
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
index 5649a5985..ffd907dae 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx
@@ -115,6 +115,21 @@ describe('useComposerAttachments — documents (#678)', () => {
await waitFor(() => expect(statuses(result)).toEqual(['ready']))
})
+ it('attaches a file whose name the OS reports decomposed under its NFC name', async () => {
+ const { result } = render()
+ const decomposed = 'café.txt'
+ // Twin: the name as picked is not NFC, which the Host refuses.
+ expect(decomposed.normalize('NFC')).not.toBe(decomposed)
+
+ act(() => {
+ result.current.handleAddComposerFiles([textFile(decomposed, 'menu')], '')
+ })
+
+ await waitFor(() => expect(statuses(result)).toEqual(['ready']))
+ expect(result.current.composerFileAttachments[0]?.filename).toBe('café.txt')
+ expect(result.current.composerFileRefusals).toEqual([])
+ })
+
it('keeps two files that share a name but not their bytes', async () => {
const { result } = render()
diff --git a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
index 1b32ce142..5bdb491d0 100644
--- a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
+++ b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
@@ -1,7 +1,11 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { COMPOSER_MAX_ATTACHMENTS } from '@constants/attachments'
import { clearComposerDraft, clearComposerDraftAfterSend } from '@lib/composerDraftStore'
-import { composerFileAdmissionError, readComposerFile } from '@lib/composerFileAdmission'
+import {
+ composerFileAdmissionError,
+ composerFileName,
+ readComposerFile,
+} from '@lib/composerFileAdmission'
import { buildComposerFileReferences } from '@lib/composerFileReferences'
import { composerRequestBaseContent } from '@lib/composerHostRequest'
import { buildComposerRequestContent } from '@lib/composerReferencesPrompt'
@@ -264,7 +268,7 @@ export function useComposerAttachments({
id,
addedOrder: composerAttachmentOrderRef.current,
type: 'file' as const,
- filename: file.name,
+ filename: composerFileName(file),
sizeBytes: file.size,
declaredMediaType: file.type,
}
diff --git a/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts b/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts
index 380f37942..581c8d8c5 100644
--- a/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts
@@ -14,6 +14,7 @@ import {
composerFileAdmissionError,
composerFileBase64Bytes,
composerFileDetailBytes,
+ composerFileName,
composerNonImageShareBytes,
composerRequestBodyBytes,
fileNameProblem,
@@ -229,13 +230,20 @@ describe('composerFileAdmissionError (#678)', () => {
['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('composes a decomposed name at intake and still refuses one that bypassed intake', () => {
+ const decomposed = 'e\u0301.txt'
+ expect(composerFileName({ name: decomposed })).toBe('\u00e9.txt')
+ expect(composerFileAdmissionError({ name: decomposed, size: 1 }, EMPTY_CONTEXT)).toBeNull()
+ // Twin: a restored attachment carries its name as stored, with no intake.
+ expect(fileNameProblem(decomposed)).toBe('has a name that is not NFC-normalized')
+ })
+
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()
diff --git a/desktop-app/ui/src/lib/composerFileAdmission.ts b/desktop-app/ui/src/lib/composerFileAdmission.ts
index 7c6c419fe..88a2eea47 100644
--- a/desktop-app/ui/src/lib/composerFileAdmission.ts
+++ b/desktop-app/ui/src/lib/composerFileAdmission.ts
@@ -113,6 +113,16 @@ export function composerRequestBodyBytes(
)
}
+/**
+ * The name a picked file is attached under. macOS reports some names
+ * decomposed (NFD); the Host accepts only NFC, so the name is composed at
+ * intake. `fileNameProblem` still refuses a non-NFC name, which can only come
+ * from a restored or hand-built attachment.
+ */
+export function composerFileName(file: { name: string }): string {
+ return file.name.normalize('NFC')
+}
+
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'
@@ -132,21 +142,22 @@ 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}.`
+ const name = composerFileName(file)
+ const nameProblem = fileNameProblem(name)
+ if (nameProblem) return `${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)}.`
+ return `${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 files = [...context.files, { filename: file.name, sizeBytes: file.size }]
+ const files = [...context.files, { filename: name, sizeBytes: file.size }]
if (composerFileBase64Bytes(files) > COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES) {
- return `${file.name} does not fit: the files in a message can take at most ${formatFileSize(COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES)} once encoded.`
+ return `${name} does not fit: the files in a message can take at most ${formatFileSize(COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES)} once encoded.`
}
const bodyBytes = composerNonImageShareBytes({ ...context.request, files })
if (bodyBytes > COMPOSER_MAX_NON_IMAGE_BODY_BYTES) {
- return `${file.name} does not fit: the text and attachment details can take at most ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} per message once encoded.`
+ return `${name} does not fit: the text and attachment details can take at most ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} per message once encoded.`
}
return null
}
@@ -170,10 +181,11 @@ async function sha256Hex(bytes: Uint8Array<ArrayBuffer>): Promise<string> {
* file that changes on disk while it is read is not sent.
*/
export async function readComposerFile(file: File, id: string): Promise<ComposerFileReadResult> {
+ const name = composerFileName(file)
const base = {
id,
type: 'file' as const,
- filename: file.name,
+ filename: name,
sizeBytes: file.size,
declaredMediaType: file.type,
}
@@ -183,14 +195,14 @@ export async function readComposerFile(file: File, id: string): Promise<Composer
return {
...base,
status: 'failed',
- error: `${file.name} changed while it was being read. Attach it again.`,
+ error: `${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,
+ filename: name,
})
return {
...base,
@@ -201,6 +213,6 @@ export async function readComposerFile(file: File, id: string): Promise<Composer
}
} catch (error) {
const reason = error instanceof Error ? error.message : String(error)
- return { ...base, status: 'failed', error: `${file.name} could not be read: ${reason}` }
+ return { ...base, status: 'failed', error: `${name} could not be read: ${reason}` }
}
}