|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { getFileTypeCategory } from '$lib/utils/file-type'; |
|
|
import { supportsVision, supportsAudio } from '$lib/stores/server.svelte'; |
|
|
import { |
|
|
FileExtensionAudio, |
|
|
FileExtensionImage, |
|
|
FileExtensionPdf, |
|
|
FileExtensionText, |
|
|
MimeTypeAudio, |
|
|
MimeTypeImage, |
|
|
MimeTypeApplication, |
|
|
MimeTypeText, |
|
|
FileTypeCategory |
|
|
} from '$lib/enums/files'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isFileTypeSupportedByModel(filename: string, mimeType?: string): boolean { |
|
|
const category = mimeType ? getFileTypeCategory(mimeType) : null; |
|
|
|
|
|
|
|
|
if (!category) { |
|
|
|
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
switch (category) { |
|
|
case FileTypeCategory.TEXT: |
|
|
|
|
|
return true; |
|
|
|
|
|
case FileTypeCategory.PDF: |
|
|
|
|
|
return true; |
|
|
|
|
|
case FileTypeCategory.IMAGE: |
|
|
|
|
|
return supportsVision(); |
|
|
|
|
|
case FileTypeCategory.AUDIO: |
|
|
|
|
|
return supportsAudio(); |
|
|
|
|
|
default: |
|
|
|
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function filterFilesByModalities(files: File[]): { |
|
|
supportedFiles: File[]; |
|
|
unsupportedFiles: File[]; |
|
|
modalityReasons: Record<string, string>; |
|
|
} { |
|
|
const supportedFiles: File[] = []; |
|
|
const unsupportedFiles: File[] = []; |
|
|
const modalityReasons: Record<string, string> = {}; |
|
|
|
|
|
const hasVision = supportsVision(); |
|
|
const hasAudio = supportsAudio(); |
|
|
|
|
|
for (const file of files) { |
|
|
const category = getFileTypeCategory(file.type); |
|
|
let isSupported = true; |
|
|
let reason = ''; |
|
|
|
|
|
switch (category) { |
|
|
case FileTypeCategory.IMAGE: |
|
|
if (!hasVision) { |
|
|
isSupported = false; |
|
|
reason = 'Images require a vision-capable model'; |
|
|
} |
|
|
break; |
|
|
|
|
|
case FileTypeCategory.AUDIO: |
|
|
if (!hasAudio) { |
|
|
isSupported = false; |
|
|
reason = 'Audio files require an audio-capable model'; |
|
|
} |
|
|
break; |
|
|
|
|
|
case FileTypeCategory.TEXT: |
|
|
case FileTypeCategory.PDF: |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
|
|
|
break; |
|
|
} |
|
|
|
|
|
if (isSupported) { |
|
|
supportedFiles.push(file); |
|
|
} else { |
|
|
unsupportedFiles.push(file); |
|
|
modalityReasons[file.name] = reason; |
|
|
} |
|
|
} |
|
|
|
|
|
return { supportedFiles, unsupportedFiles, modalityReasons }; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function generateModalityErrorMessage( |
|
|
unsupportedFiles: File[], |
|
|
modalityReasons: Record<string, string> |
|
|
): string { |
|
|
if (unsupportedFiles.length === 0) return ''; |
|
|
|
|
|
const hasVision = supportsVision(); |
|
|
const hasAudio = supportsAudio(); |
|
|
|
|
|
let message = ''; |
|
|
|
|
|
if (unsupportedFiles.length === 1) { |
|
|
const file = unsupportedFiles[0]; |
|
|
const reason = modalityReasons[file.name]; |
|
|
message = `The file "${file.name}" cannot be uploaded: ${reason}.`; |
|
|
} else { |
|
|
const fileNames = unsupportedFiles.map((f) => f.name).join(', '); |
|
|
message = `The following files cannot be uploaded: ${fileNames}.`; |
|
|
} |
|
|
|
|
|
|
|
|
const supportedTypes: string[] = ['text files', 'PDFs']; |
|
|
if (hasVision) supportedTypes.push('images'); |
|
|
if (hasAudio) supportedTypes.push('audio files'); |
|
|
|
|
|
message += ` This model supports: ${supportedTypes.join(', ')}.`; |
|
|
|
|
|
return message; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function generateModalityAwareAcceptString(): string { |
|
|
const hasVision = supportsVision(); |
|
|
const hasAudio = supportsAudio(); |
|
|
|
|
|
const acceptedExtensions: string[] = []; |
|
|
const acceptedMimeTypes: string[] = []; |
|
|
|
|
|
|
|
|
acceptedExtensions.push(...Object.values(FileExtensionText)); |
|
|
acceptedMimeTypes.push(...Object.values(MimeTypeText)); |
|
|
acceptedExtensions.push(...Object.values(FileExtensionPdf)); |
|
|
acceptedMimeTypes.push(...Object.values(MimeTypeApplication)); |
|
|
|
|
|
|
|
|
if (hasVision) { |
|
|
acceptedExtensions.push(...Object.values(FileExtensionImage)); |
|
|
acceptedMimeTypes.push(...Object.values(MimeTypeImage)); |
|
|
} |
|
|
|
|
|
|
|
|
if (hasAudio) { |
|
|
acceptedExtensions.push(...Object.values(FileExtensionAudio)); |
|
|
acceptedMimeTypes.push(...Object.values(MimeTypeAudio)); |
|
|
} |
|
|
|
|
|
return [...acceptedExtensions, ...acceptedMimeTypes].join(','); |
|
|
} |
|
|
|