import type { AttachmentAdapter } from '@assistant-ui/react'; import type { PendingAttachment, CompleteAttachment, Attachment } from '@assistant-ui/react'; function getFileDataURL(file: File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => resolve(reader.result as string); reader.onerror = (error) => reject(error); reader.readAsDataURL(file); }); } /** * Generic file attachment adapter that accepts any file type. * Files are read as base64 data URLs and sent with their original MIME type. */ export class SimpleFileAttachmentAdapter implements AttachmentAdapter { accept = '*'; async add(state: { file: File }): Promise { return { id: state.file.name, type: 'file', name: state.file.name, contentType: state.file.type || 'application/octet-stream', file: state.file, status: { type: 'requires-action', reason: 'composer-send' }, }; } async send(attachment: PendingAttachment): Promise { const dataUrl = await getFileDataURL(attachment.file); const data = dataUrl.split(';base64,')[1]; return { ...attachment, status: { type: 'complete' }, content: [ { type: 'file' as const, data, mimeType: attachment.contentType || 'application/octet-stream', }, ], }; } // eslint-disable-next-line @typescript-eslint/no-unused-vars async remove(_a: Attachment): Promise {} }