Skip to content

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

TypeScript

refactor(desktop): move the host request mapper and admission inputs to lib

Alfredo Lopez55de9feb4231
diff --git a/desktop-app/ui/src/components/agents/ComposerPanel.tsx b/desktop-app/ui/src/components/agents/ComposerPanel.tsx
index dfdd0a2ed..8b957e3b9 100644
--- a/desktop-app/ui/src/components/agents/ComposerPanel.tsx
+++ b/desktop-app/ui/src/components/agents/ComposerPanel.tsx
@@ -680,7 +680,7 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
}
setComposerAttachmentError(null)
if (documents.length > 0) {
- onAddComposerFiles(documents, new TextEncoder().encode(draft).length)
+ onAddComposerFiles(documents, draft)
}
if (images.length > 0) void prepareComposerImageAttachments(images, source)
},
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 2747f0a31..b93c7a972 100644
--- a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
+++ b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
@@ -666,14 +666,11 @@ describe('ComposerPanel with an image-capable model', () => {
await waitFor(() => expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalled())
expect(expectSinglePreparedImage().name).toBe('good.png')
- // Liveness witness: the document path ran, with the draft's byte length for the body budget.
+ // Liveness witness: the document path ran, with the draft for the body budget.
expect(actionsMock.handleAddComposerFiles).toHaveBeenCalledTimes(1)
- const [documents, textBytes] = actionsMock.handleAddComposerFiles.mock.calls[0] as [
- File[],
- number,
- ]
+ const [documents, draft] = actionsMock.handleAddComposerFiles.mock.calls[0] as [File[], string]
expect(documents.map(file => file.name)).toEqual(['animation.gif'])
- expect(textBytes).toBe(3)
+ expect(draft).toBe('abc')
expect(screen.queryByRole('alert')).toBeNull()
})
@@ -822,7 +819,7 @@ describe('ComposerPanel with an image-capable model', () => {
expect(batch?.map(attachment => attachment.name)).toEqual(['a.png', 'b.png'])
// 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]
+ const [documents] = actionsMock.handleAddComposerFiles.mock.calls[0] as [File[], string]
expect(documents.map(file => file.name)).toEqual(['doc.pdf'])
// No image was counted as skipped, so nothing is reported.
expect(screen.queryByRole('alert')).toBeNull()
diff --git a/desktop-app/ui/src/contexts/AgentChatActionsContext/types.ts b/desktop-app/ui/src/contexts/AgentChatActionsContext/types.ts
index 3b9102f19..8f4f2bbd9 100644
--- a/desktop-app/ui/src/contexts/AgentChatActionsContext/types.ts
+++ b/desktop-app/ui/src/contexts/AgentChatActionsContext/types.ts
@@ -35,7 +35,7 @@ export interface AgentChatActionsContextValue {
handleAddComposerImageAttachments: (attachments: ComposerImageAttachment[]) => void
handleUpdateComposerImageAttachment: (attachment: ComposerImageAttachment) => void
handleRemoveComposerImageAttachment: (attachmentId: string) => void
- handleAddComposerFiles: (files: File[], textBytes: number) => void
+ handleAddComposerFiles: (files: File[], draft: string) => void
handleRemoveComposerFileAttachment: (attachmentId: string) => void
handleRestoreComposerFiles: (files: ComposerFileAttachment[]) => void
handleAddComposerReferenceAttachments: (attachments: ComposerReferenceAttachment[]) => void
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
index 5a722fe42..350bf52c1 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx
@@ -48,7 +48,7 @@ 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)
+ rendered.result.current.handleAddComposerFiles([file], '')
})
await waitFor(() =>
expect(rendered.result.current.composerFileAttachments.map(item => item.status)).toEqual([
@@ -116,7 +116,7 @@ describe('sendAgentMessage — document attachments (#678)', () => {
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)
+ rendered.result.current.handleAddComposerFiles([pending], '')
})
// 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.
@@ -250,7 +250,7 @@ describe('sendAgentMessage — a Host without file attachments (#678 D13)', () =
new File([NOTES_BYTES], 'a.txt', { type: 'text/plain' }),
new File([NOTES_BYTES], 'b.txt', { type: 'text/plain' }),
],
- 0
+ ''
)
})
await waitFor(() =>
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 aecfa6859..1b0f4b695 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
@@ -46,7 +46,7 @@ describe('useComposerAttachments — documents (#678)', () => {
)
act(() => {
- result.current.handleAddComposerFiles([file], 0)
+ result.current.handleAddComposerFiles([file], '')
})
// The chip exists before any byte is read.
expect(statuses(result)).toEqual(['reading'])
@@ -67,7 +67,7 @@ describe('useComposerAttachments — documents (#678)', () => {
const { result } = render()
act(() => {
- result.current.handleAddComposerFiles([textFile('a.txt', 'a'), textFile('b.txt', 'b')], 0)
+ result.current.handleAddComposerFiles([textFile('a.txt', 'a'), textFile('b.txt', 'b')], '')
})
await waitFor(() => expect(statuses(result)).toEqual(['ready', 'ready']))
@@ -83,11 +83,11 @@ describe('useComposerAttachments — documents (#678)', () => {
const { result } = render()
act(() => {
- result.current.handleAddComposerFiles([textFile('same.txt', 'same')], 0)
+ result.current.handleAddComposerFiles([textFile('same.txt', 'same')], '')
})
await waitFor(() => expect(statuses(result)).toEqual(['ready']))
act(() => {
- result.current.handleAddComposerFiles([textFile('same.txt', 'same')], 0)
+ result.current.handleAddComposerFiles([textFile('same.txt', 'same')], '')
})
// Liveness witness: the second copy was admitted (reading) before it was dropped.
expect(statuses(result)).toEqual(['ready', 'reading'])
@@ -100,7 +100,7 @@ describe('useComposerAttachments — documents (#678)', () => {
act(() => {
result.current.handleAddComposerFiles(
[textFile('same.txt', 'one'), textFile('same.txt', 'two')],
- 0
+ ''
)
})
@@ -114,7 +114,7 @@ describe('useComposerAttachments — documents (#678)', () => {
const read = vi.spyOn(huge, 'arrayBuffer')
act(() => {
- result.current.handleAddComposerFiles([huge], 0)
+ result.current.handleAddComposerFiles([huge], '')
})
// Witness for the two negative checks below: the refusal names this file.
@@ -133,7 +133,7 @@ describe('useComposerAttachments — documents (#678)', () => {
)
act(() => {
- result.current.handleAddComposerFiles(files, 0)
+ result.current.handleAddComposerFiles(files, '')
})
await waitFor(() =>
@@ -160,7 +160,7 @@ describe('useComposerAttachments — documents (#678)', () => {
})
)
act(() => {
- result.current.handleAddComposerFiles([file], 0)
+ result.current.handleAddComposerFiles([file], '')
})
const [reading] = result.current.composerFileAttachments
expect(reading?.status).toBe('reading')
@@ -184,14 +184,14 @@ describe('useComposerAttachments — documents (#678)', () => {
const bad = textFile('bad/name.txt', 'x')
act(() => {
- result.current.handleAddComposerFiles([bad], 0)
+ result.current.handleAddComposerFiles([bad], '')
})
// Twin: the refusal is on screen before the next attach.
expect(result.current.composerFileRefusals).toHaveLength(1)
expect(result.current.composerFileAttachments).toEqual([])
act(() => {
- result.current.handleAddComposerFiles([textFile('good.txt', 'good')], 0)
+ result.current.handleAddComposerFiles([textFile('good.txt', 'good')], '')
})
await waitFor(() => expect(statuses(result)).toEqual(['ready']))
expect(result.current.composerFileRefusals).toEqual([])
@@ -207,7 +207,7 @@ describe('useComposerAttachments — documents (#678)', () => {
})
)
act(() => {
- result.current.handleAddComposerFiles([file], 0)
+ result.current.handleAddComposerFiles([file], '')
})
// Witness: the chip existed while the file was being read.
expect(statuses(result)).toEqual(['reading'])
@@ -232,7 +232,7 @@ describe('useComposerAttachments — documents (#678)', () => {
})
)
act(() => {
- result.current.handleAddComposerFiles([file], 0)
+ result.current.handleAddComposerFiles([file], '')
})
const [reading] = result.current.composerFileAttachments
// Witness: the file was reading when the user removed it.
@@ -276,7 +276,7 @@ describe('useComposerAttachments — documents (#678)', () => {
])('clears the refusal on %s', (_label, clear) => {
const hook = render()
act(() => {
- hook.result.current.handleAddComposerFiles([textFile('bad/name.txt', 'x')], 0)
+ hook.result.current.handleAddComposerFiles([textFile('bad/name.txt', 'x')], '')
})
// Twin: the refusal is on screen before the clearing action.
expect(hook.result.current.composerFileRefusals).toHaveLength(1)
@@ -291,7 +291,7 @@ describe('useComposerAttachments — documents (#678)', () => {
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)
+ result.current.handleAddComposerFiles([textFile('a.txt', 'a')], '')
})
await waitFor(() => expect(statuses(result)).toEqual(['ready']))
@@ -303,7 +303,7 @@ describe('useComposerAttachments — documents (#678)', () => {
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)
+ result.current.handleAddComposerFiles([textFile('a.txt', 'a')], '')
})
await waitFor(() => expect(statuses(result)).toEqual(['ready']))
const kept = result.current.composerFileAttachments
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 53bc63e09..0505f486c 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -26,11 +26,15 @@ import {
} from '@lib/composerDraftStore'
import {
composerFileBase64Bytes,
- composerNonImageBodyBytes,
+ composerNonImageShareBytes,
composerRequestBodyBytes,
formatFileSize,
} from '@lib/composerFileAdmission'
import { buildComposerFileReferences } from '@lib/composerFileReferences'
+import {
+ composerRequestBaseContent,
+ mapComposerAttachmentsToHostRequest,
+} from '@lib/composerHostRequest'
import { buildComposerRequestContent } from '@lib/composerReferencesPrompt'
import {
confirmHostModelSelectionFromSend,
@@ -48,7 +52,6 @@ import type {
ChatMessageAttachment,
HostActivityEvent,
HostActivityStreamEvent,
- HostMessageAttachment,
MessageToolStep,
SessionMessagesQuery,
} from '../../../../src/types'
@@ -1426,35 +1429,6 @@ export function useAgentChatController({
}
}, [cancelOlderMessagesLoad, currentTeamId, navItem, selectedAgent, isHostAccessBlocked])
- const mapComposerAttachmentsToHostRequest = (
- 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) ───
@@ -2882,15 +2856,11 @@ export function useAgentChatController({
// #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 baseContentForRequest = composerRequestBaseContent(
+ trimmedContent,
+ effectiveAttachments.length,
+ effectiveFiles.length
+ )
const effectiveContentForRequest = buildComposerRequestContent(
baseContentForRequest,
effectiveReferences
@@ -2899,27 +2869,27 @@ export function useAgentChatController({
// The limits rpc-proxy and mcp-host enforce, most specific first: the
// count, the file quota, the share left for text and file details, then
// the whole body (images included), so the reason names what to remove.
- const textBytes = new TextEncoder().encode(effectiveContentForRequest).length
+ const requestForBudget = {
+ content: effectiveContentForRequest,
+ fileReferences: fileReferencesForSend,
+ hostRef: sendAgent,
+ files: effectiveFiles,
+ images: effectiveAttachments,
+ }
let blocker: string | null = null
if (effectiveAttachments.length + effectiveFiles.length > COMPOSER_MAX_ATTACHMENTS) {
blocker = `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`
} else if (composerFileBase64Bytes(effectiveFiles) > COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES) {
blocker = `The attached files take more than ${formatFileSize(COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES)} once encoded. Remove a file.`
} else if (
- composerNonImageBodyBytes({ files: effectiveFiles, textBytes }) >
- COMPOSER_MAX_NON_IMAGE_BODY_BYTES
+ composerNonImageShareBytes(requestForBudget) > COMPOSER_MAX_NON_IMAGE_BODY_BYTES
) {
blocker = `The message text and file details take more than ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} once encoded. Shorten the message or remove a file.`
} else if (
- composerRequestBodyBytes({
- files: effectiveFiles,
- textBytes,
- imageBase64Bytes: effectiveAttachments.reduce(
- (total, image) => total + image.dataBase64.length,
- 0
- ),
- imageCount: effectiveAttachments.length,
- }) > COMPOSER_MAX_REQUEST_BODY_BYTES
+ composerRequestBodyBytes(
+ requestForBudget,
+ effectiveAttachments.reduce((total, image) => total + image.dataBase64.length, 0)
+ ) > COMPOSER_MAX_REQUEST_BODY_BYTES
) {
blocker = `The attachments and text take more than ${formatFileSize(COMPOSER_MAX_REQUEST_BODY_BYTES)} once encoded. Remove an attachment or shorten the message.`
}
diff --git a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
index 550ae63b4..e7939f4eb 100644
--- a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
+++ b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
@@ -196,7 +196,7 @@ export function useComposerAttachments({
* replaces the refusals of the previous one.
*/
const handleAddComposerFiles = useCallback(
- (files: File[], textBytes: number) => {
+ (files: File[], draft: string) => {
composerAttachmentRevisionRef.current += 1
if (!files.length) return
const refusals: ComposerFileRefusal[] = []
@@ -206,7 +206,7 @@ export function useComposerAttachments({
const error = composerFileAdmissionError(file, {
attachedCount: composerImageCountRef.current + current.length,
files: current,
- textBytes,
+ request: { content: draft, fileReferences: [], hostRef: selectedAgent ?? '', images: [] },
})
composerAttachmentOrderRef.current += 1
const base = {
@@ -252,7 +252,7 @@ export function useComposerAttachments({
setComposerFileRefusals(refusals)
clearSendError()
},
- [clearSendError, commitComposerFiles]
+ [clearSendError, commitComposerFiles, selectedAgent]
)
const handleRemoveComposerFileAttachment = useCallback(
diff --git a/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts b/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts
index 61798ba5b..266f498fe 100644
--- a/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts
@@ -13,13 +13,18 @@ import {
composerFileAdmissionError,
composerFileBase64Bytes,
composerFileDetailBytes,
- composerNonImageBodyBytes,
+ composerNonImageShareBytes,
composerRequestBodyBytes,
fileNameProblem,
readComposerFile,
} from '../composerFileAdmission'
-const EMPTY_CONTEXT = { attachedCount: 0, files: [], textBytes: 0 }
+/** The rest of a posted request whose message text is `content`. */
+function requestWithText(content = '') {
+ return { content, fileReferences: [], hostRef: 'agent-1', images: [] }
+}
+
+const EMPTY_CONTEXT = { attachedCount: 0, files: [], request: requestWithText() }
function pdfBytes(): Uint8Array {
return new TextEncoder().encode('%PDF-1.7\n1 0 obj\n<<>>\nendobj\n%%EOF\n')
@@ -148,7 +153,7 @@ describe('composerFileAdmissionError (#678)', () => {
expect(
composerFileAdmissionError(
{ name: 'second.bin', size: COMPOSER_MAX_FILE_BYTES },
- { attachedCount: 1, files: [first], textBytes: 0 }
+ { attachedCount: 1, files: [first], request: requestWithText() }
)
).toBe(
'second.bin does not fit: the files in a message can take at most 16.0 MiB once encoded.'
@@ -157,7 +162,7 @@ describe('composerFileAdmissionError (#678)', () => {
it('fills the 16 MiB file quota exactly and refuses one byte more (#678)', () => {
const first = { filename: 'first.bin', sizeBytes: COMPOSER_MAX_FILE_BYTES }
- const context = { attachedCount: 1, files: [first], textBytes: 0 }
+ const context = { attachedCount: 1, files: [first], request: requestWithText() }
// 11 MiB encodes to 15_379_116 chars; 1_048_575 B adds 1_398_100 and
// lands on 16_777_216; 1_048_576 B adds 1_398_104.
expect(base64Length(COMPOSER_MAX_FILE_BYTES) + base64Length(1_048_575)).toBe(
@@ -175,7 +180,7 @@ describe('composerFileAdmissionError (#678)', () => {
expect(
composerFileAdmissionError(file, {
...EMPTY_CONTEXT,
- textBytes: COMPOSER_MAX_NON_IMAGE_BODY_BYTES,
+ request: requestWithText('a'.repeat(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)),
})
).toBe(
'a.bin does not fit: the text and file details can take at most 6.0 MiB per message once encoded.'
@@ -192,10 +197,16 @@ describe('composerFileAdmissionError (#678)', () => {
JSON.stringify('big.bin').length -
COMPOSER_FILE_ENTRY_METADATA_BYTES
expect(
- composerFileAdmissionError(file, { ...EMPTY_CONTEXT, textBytes: fullShareText })
+ composerFileAdmissionError(file, {
+ ...EMPTY_CONTEXT,
+ request: requestWithText('a'.repeat(fullShareText)),
+ })
).toBeNull()
expect(
- composerFileAdmissionError(file, { ...EMPTY_CONTEXT, textBytes: fullShareText + 1 })
+ composerFileAdmissionError(file, {
+ ...EMPTY_CONTEXT,
+ request: requestWithText('a'.repeat(fullShareText + 1)),
+ })
).toContain('the text and file details can take at most 6.0 MiB')
})
@@ -220,7 +231,7 @@ describe('composerFileAdmissionError (#678)', () => {
describe('composer request body accounting (#678)', () => {
it('charges the envelope, the text and the file details, but not the file base64, to the share', () => {
const file = { filename: 'a.pdf', sizeBytes: 1000 }
- expect(composerNonImageBodyBytes({ files: [file], textBytes: 50 })).toBe(
+ expect(composerNonImageShareBytes({ ...requestWithText('a'.repeat(50)), files: [file] })).toBe(
COMPOSER_REQUEST_ENVELOPE_BYTES +
50 +
JSON.stringify('a.pdf').length +
@@ -239,15 +250,13 @@ describe('composer request body accounting (#678)', () => {
it('adds the share, the file base64 and the images to size the whole body', () => {
const files = [{ filename: 'a.pdf', sizeBytes: 1000 }]
- expect(
- composerRequestBodyBytes({
- files,
- textBytes: 50,
- imageBase64Bytes: 8_000,
- imageCount: 2,
- })
- ).toBe(
- composerNonImageBodyBytes({ files, textBytes: 50 }) +
+ const request = {
+ ...requestWithText('a'.repeat(50)),
+ files,
+ images: [{ name: 'one.png' }, { name: 'two.png' }],
+ }
+ expect(composerRequestBodyBytes(request, 8_000)).toBe(
+ composerNonImageShareBytes(request) +
base64Length(1000) +
8_000 +
2 * COMPOSER_FILE_ENTRY_METADATA_BYTES
diff --git a/desktop-app/ui/src/lib/__tests__/composerHostRequest.test.ts b/desktop-app/ui/src/lib/__tests__/composerHostRequest.test.ts
new file mode 100644
index 000000000..8f778dc75
--- /dev/null
+++ b/desktop-app/ui/src/lib/__tests__/composerHostRequest.test.ts
@@ -0,0 +1,98 @@
+import { describe, expect, it } from 'vitest'
+import { classifyBytes } from '@clerum/gfs-interaction-policy'
+import type { ComposerImageAttachment, ReadyComposerFileAttachment } from '../../uiTypes'
+import {
+ composerRequestBaseContent,
+ mapComposerAttachmentsToHostRequest,
+} from '../composerHostRequest'
+
+const NOTES = new TextEncoder().encode('meeting notes\n')
+
+const image: ComposerImageAttachment = {
+ id: 'img-1',
+ name: 'photo.png',
+ mimeType: 'image/png',
+ dataBase64: 'iVBORw0KGgo=',
+ sizeBytes: 8,
+ previewDataUrl: 'blob:preview',
+}
+
+const file: ReadyComposerFileAttachment = {
+ id: 'file-1',
+ type: 'file',
+ status: 'ready',
+ filename: 'notes.txt',
+ sizeBytes: NOTES.length,
+ declaredMediaType: 'text/plain',
+ classification: classifyBytes({
+ bytes: NOTES,
+ totalByteLength: NOTES.length,
+ declaredMediaType: 'text/plain',
+ filename: 'notes.txt',
+ }),
+ dataBase64: 'bWVldGluZyBub3Rlcwo=',
+ digestHex: 'a'.repeat(64),
+}
+
+describe('mapComposerAttachmentsToHostRequest (#678)', () => {
+ it('posts images first, then files, in the Host wire shape', () => {
+ const mapped = mapComposerAttachmentsToHostRequest([image], [file])
+
+ expect(mapped).toEqual([
+ {
+ id: 'img-1',
+ kind: 'image',
+ mimeType: 'image/png',
+ encoding: 'base64',
+ dataBase64: 'iVBORw0KGgo=',
+ filename: 'photo.png',
+ },
+ {
+ id: 'file-1',
+ kind: 'file',
+ filename: 'notes.txt',
+ mimeType: 'text/plain',
+ detectedMediaType: file.classification.detectedMediaType,
+ encoding: 'base64',
+ dataBase64: 'bWVldGluZyBub3Rlcwo=',
+ sizeBytes: NOTES.length,
+ digest: { algorithm: 'sha256', hex: 'a'.repeat(64) },
+ },
+ ])
+ // The serialized key order is what the proxies measure; pin it.
+ expect(Object.keys(mapped[1]!)).toEqual([
+ 'id',
+ 'kind',
+ 'filename',
+ 'mimeType',
+ 'detectedMediaType',
+ 'encoding',
+ 'dataBase64',
+ 'sizeBytes',
+ 'digest',
+ ])
+ expect(file.classification.detectedMediaType).toBe('text/plain')
+ })
+
+ it('posts nothing for an empty composer', () => {
+ expect(mapComposerAttachmentsToHostRequest([], [])).toEqual([])
+ })
+})
+
+describe('composerRequestBaseContent (#678)', () => {
+ it('keeps typed text whatever is attached', () => {
+ expect(composerRequestBaseContent('hello', 1, 1)).toBe('hello')
+ })
+
+ it.each([
+ [1, 1, 'Please analyze the attached image(s) and file(s).'],
+ [0, 2, 'Please analyze the attached file(s).'],
+ [3, 0, 'Please analyze the attached image(s).'],
+ [0, 0, 'Please use the attached context.'],
+ ])(
+ 'names what is attached when the draft is empty (%i images, %i files)',
+ (images, files, text) => {
+ expect(composerRequestBaseContent('', images, files)).toBe(text)
+ }
+ )
+})
diff --git a/desktop-app/ui/src/lib/composerFileAdmission.ts b/desktop-app/ui/src/lib/composerFileAdmission.ts
index 2ff023239..553971c4e 100644
--- a/desktop-app/ui/src/lib/composerFileAdmission.ts
+++ b/desktop-app/ui/src/lib/composerFileAdmission.ts
@@ -1,4 +1,4 @@
-import { classifyBytes } from '@clerum/gfs-interaction-policy'
+import { type FileReferenceV1, classifyBytes } from '@clerum/gfs-interaction-policy'
import {
COMPOSER_FILE_ENTRY_METADATA_BYTES,
COMPOSER_MAX_ATTACHMENTS,
@@ -16,6 +16,21 @@ const CONTROL_OR_SEPARATOR = /[\u0000-\u001f\u007f/]/
const textEncoder = new TextEncoder()
+/**
+ * The parts of a posted Host message request that neither the image quota nor
+ * the file quota credits: they are charged to the non-image share.
+ */
+export type ComposerNonImageShareRequest = {
+ /** The message text as it will be posted, references section included. */
+ content: string
+ /** The structured Global Files references posted next to the text. */
+ fileReferences: ReadonlyArray<FileReferenceV1>
+ /** The agent the message is posted to. */
+ hostRef: string
+ files: ReadonlyArray<{ filename: string }>
+ images: ReadonlyArray<{ name: string }>
+}
+
export type ComposerFileAdmissionContext = {
/** Images and files already attached to the message. */
attachedCount: number
@@ -24,8 +39,8 @@ export type ComposerFileAdmissionContext = {
* name and fixed fields count 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
+ /** The rest of the request the files would be posted with. */
+ request: Omit<ComposerNonImageShareRequest, 'files'>
}
export function formatFileSize(bytes: number): string {
@@ -60,14 +75,11 @@ export function composerFileBase64Bytes(files: ReadonlyArray<{ sizeBytes: number
* image quota nor the file quota: the JSON envelope, the message text and the
* non-base64 fields of every `kind:'file'`.
*/
-export function composerNonImageBodyBytes(input: {
- files: ReadonlyArray<{ filename: string }>
- textBytes: number
-}): number {
+export function composerNonImageShareBytes(request: ComposerNonImageShareRequest): number {
return (
COMPOSER_REQUEST_ENVELOPE_BYTES +
- input.textBytes +
- input.files.reduce((total, file) => total + composerFileDetailBytes(file), 0)
+ textEncoder.encode(request.content).length +
+ request.files.reduce((total, file) => total + composerFileDetailBytes(file), 0)
)
}
@@ -76,17 +88,17 @@ export function composerNonImageBodyBytes(input: {
* files and of the images. An image entry is counted with the same fixed-field
* allowance as a file entry, which is an upper bound.
*/
-export function composerRequestBodyBytes(input: {
- files: ReadonlyArray<{ filename: string; sizeBytes: number }>
- textBytes: number
+export function composerRequestBodyBytes(
+ request: ComposerNonImageShareRequest & {
+ files: ReadonlyArray<{ filename: string; sizeBytes: number }>
+ },
imageBase64Bytes: number
- imageCount: number
-}): number {
+): number {
return (
- composerNonImageBodyBytes(input) +
- composerFileBase64Bytes(input.files) +
- input.imageBase64Bytes +
- input.imageCount * COMPOSER_FILE_ENTRY_METADATA_BYTES
+ composerNonImageShareBytes(request) +
+ composerFileBase64Bytes(request.files) +
+ imageBase64Bytes +
+ request.images.length * COMPOSER_FILE_ENTRY_METADATA_BYTES
)
}
@@ -121,7 +133,7 @@ export function composerFileAdmissionError(
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.`
}
- const bodyBytes = composerNonImageBodyBytes({ files, textBytes: context.textBytes })
+ const bodyBytes = composerNonImageShareBytes({ ...context.request, files })
if (bodyBytes > COMPOSER_MAX_NON_IMAGE_BODY_BYTES) {
return `${file.name} does not fit: the text and file details can take at most ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} per message once encoded.`
}
diff --git a/desktop-app/ui/src/lib/composerHostRequest.ts b/desktop-app/ui/src/lib/composerHostRequest.ts
new file mode 100644
index 000000000..9f248972a
--- /dev/null
+++ b/desktop-app/ui/src/lib/composerHostRequest.ts
@@ -0,0 +1,60 @@
+import type { HostMessageAttachment } from '../../../src/types'
+import type { ComposerImageAttachment, ReadyComposerFileAttachment } from '../uiTypes'
+
+/**
+ * The `attachments` array of a Host message request, built from the composer's
+ * images and ready files (#678). Images come first, then files, each in the
+ * order the composer holds them.
+ */
+export function mapComposerAttachmentsToHostRequest(
+ attachments: ComposerImageAttachment[],
+ files: ReadyComposerFileAttachment[]
+): HostMessageAttachment[] {
+ return [
+ ...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 },
+ })
+ ),
+ ]
+}
+
+/**
+ * The message text a send posts before the references section is appended:
+ * the trimmed draft, or a fixed prompt naming what is attached when the draft
+ * is empty.
+ */
+export function composerRequestBaseContent(
+ trimmedContent: string,
+ imageCount: number,
+ fileCount: number
+): string {
+ return (
+ trimmedContent ||
+ (imageCount && fileCount
+ ? 'Please analyze the attached image(s) and file(s).'
+ : fileCount
+ ? 'Please analyze the attached file(s).'
+ : imageCount
+ ? 'Please analyze the attached image(s).'
+ : 'Please use the attached context.')
+ )
+}