| import { AttachmentType, FileTypeCategory, SpecialFileType } from '$lib/enums'; |
| import { getFileTypeCategory, getFileTypeCategoryByExtension, isImageFile } from '$lib/utils'; |
| import type { |
| AttachmentDisplayItemsOptions, |
| ChatUploadedFile, |
| DatabaseMessageExtra |
| } from '$lib/types'; |
|
|
| |
| |
| |
| function isMcpPromptUpload(file: ChatUploadedFile): boolean { |
| return file.type === SpecialFileType.MCP_PROMPT && !!file.mcpPrompt; |
| } |
|
|
| |
| |
| |
| function isMcpPromptAttachment(attachment: DatabaseMessageExtra): boolean { |
| return attachment.type === AttachmentType.MCP_PROMPT; |
| } |
|
|
| |
| |
| |
| function isMcpResourceAttachment(attachment: DatabaseMessageExtra): boolean { |
| return attachment.type === AttachmentType.MCP_RESOURCE; |
| } |
|
|
| |
| |
| |
| function getUploadedFileCategory(file: ChatUploadedFile): FileTypeCategory | null { |
| const categoryByMime = getFileTypeCategory(file.type); |
|
|
| if (categoryByMime) { |
| return categoryByMime; |
| } |
|
|
| return getFileTypeCategoryByExtension(file.name); |
| } |
|
|
| |
| |
| |
| |
| export function getAttachmentDisplayItems( |
| options: AttachmentDisplayItemsOptions |
| ): ChatAttachmentDisplayItem[] { |
| const { uploadedFiles = [], attachments = [] } = options; |
| const items: ChatAttachmentDisplayItem[] = []; |
|
|
| |
| for (const file of uploadedFiles) { |
| items.push({ |
| id: file.id, |
| name: file.name, |
| size: file.size, |
| preview: file.preview, |
| isImage: getUploadedFileCategory(file) === FileTypeCategory.IMAGE, |
| isMcpPrompt: isMcpPromptUpload(file), |
| isLoading: file.isLoading, |
| loadError: file.loadError, |
| uploadedFile: file, |
| textContent: file.textContent |
| }); |
| } |
|
|
| |
| for (const [index, attachment] of attachments.entries()) { |
| const isImage = isImageFile(attachment); |
| const isMcpPrompt = isMcpPromptAttachment(attachment); |
| const isMcpResource = isMcpResourceAttachment(attachment); |
|
|
| items.push({ |
| id: `attachment-${index}`, |
| name: attachment.name, |
| preview: isImage && 'base64Url' in attachment ? attachment.base64Url : undefined, |
| isImage, |
| isMcpPrompt, |
| isMcpResource, |
| attachment, |
| attachmentIndex: index, |
| textContent: 'content' in attachment ? attachment.content : undefined |
| }); |
| } |
|
|
| return items.reverse(); |
| } |
|
|