char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1/** 2* File validation utilities based on model modalities 3* Ensures only compatible file types are processed based on model capabilities 4*/ 5 6import { FileTypeCategory } from '$lib/enums' ; 7import type { ModalityCapabilities } from '$lib/types' ; 8import { getFileTypeCategory } from '$lib/utils' ; 9 10/** 11* Check if a file type is supported by the given modalities 12* @param filename - The filename to check 13* @param mimeType - The MIME type of the file 14* @param capabilities - The modality capabilities to check against 15* @returns true if the file type is supported 16*/ 17export function isFileTypeSupportedByModel ( 18filename :string , 19mimeType :string | undefined , 20capabilities :ModalityCapabilities 21) :boolean { 22const category = mimeType ?getFileTypeCategory ( mimeType ) :null ; 23 24// If we can't determine the category from MIME type, fall back to general support check 25if ( ! category ) { 26// For unknown types, only allow if they might be text files 27// This is a conservative approach for edge cases 28return true ; // Let the existing isFileTypeSupported handle this 29} 30 31switch ( category ) { 32case FileTypeCategory . TEXT : 33// Text files are always supported 34return true ; 35 36case FileTypeCategory . // PDFs are always supported (will be processed as text for non-vision models) 38return true ; 39 40case FileTypeCategory . IMAGE : 41// Images require vision support 42return capabilities . hasVision ; 43 44case FileTypeCategory . AUDIO : 45// Audio files require audio support 46return capabilities . hasAudio ; 47 48case FileTypeCategory . VIDEO : 49// Video files require video support 50return capabilities . hasVideo ; 51 52default : 53// Unknown categories - be conservative and allow 54return true ; 55} 56} 57 58/** 59* Filter files based on model modalities and return supported/unsupported lists 60* @param files - Array of files to filter 61* @param capabilities - The modality capabilities to check against 62* @returns Object with supportedFiles and unsupportedFiles arrays 63*/ 64export function filterFilesByModalities ( 65files :File [], 66capabilities :ModalityCapabilities 67) :{ 68supportedFiles :File []; 69unsupportedFiles :File []; 70modalityReasons :Record < string , string >; 71} { 72const supportedFiles :File [] = []; 73const unsupportedFiles :File [] = []; 74const modalityReasons :Record < string , string > = {}; 75const { hasAudio, hasVideo, hasVision} = capabilities ; 76 77for ( const file of files ) { 78const category = getFileTypeCategory ( file . type ); 79 80let isSupported = true ; 81let reason = '' ; 82 83switch ( category ) { 84case FileTypeCategory . IMAGE : 85if ( ! hasVision ) { 86isSupported = false ; 87reason = 'Images require a vision-capable model' ; 88} 89 90break ; 91 92case FileTypeCategory . AUDIO : 93if ( ! hasAudio ) { 94isSupported = false ; 95reason = 'Audio files require an audio-capable model' ; 96} 97 98break ; 99 100case FileTypeCategory . VIDEO : 101if ( ! hasVideo ) { 102isSupported = false ; 103reason = 'Video files require a video-capable model' ; 104} 105 106break ; 107 108case FileTypeCategory . TEXT : 109case FileTypeCategory . // Always supported 111break ; 112 113default : 114// For unknown types, check if it's a generally supported file type 115// This handles edge cases and maintains backward compatibility 116break ; 117} 118 119if ( isSupported ) { 120supportedFiles . push ( file ); 121} else { 122unsupportedFiles . push ( file ); 123modalityReasons [ file . name ] = reason ; 124} 125} 126 127return { modalityReasons, supportedFiles, unsupportedFiles}; 128} 129 130/** 131* Generate a user-friendly error message for unsupported files 132* @param unsupportedFiles - Array of unsupported files 133* @param modalityReasons - Reasons why files are unsupported 134* @param capabilities - The modality capabilities to check against 135* @returns Formatted error message 136*/ 137export function generateModalityErrorMessage ( 138unsupportedFiles :File [], 139modalityReasons :Record < string , string >, 140capabilities :ModalityCapabilities 141) :string { 142if ( unsupportedFiles . length === 0 ) return '' ; 143 144const { hasAudio, hasVideo, hasVision} = capabilities ; 145 146let message = '' ; 147 148if ( unsupportedFiles . length === 1 ) { 149const file = unsupportedFiles [ 0 ]; 150const reason = modalityReasons [ file . name ]; 151 152message = `The file " ${ file . name } " cannot be uploaded: ${ reason } .` ; 153} else { 154const fileNames = unsupportedFiles . map (( f ) => f . name ). join ( ', ' ); 155 156message = `The following files cannot be uploaded: ${ fileNames } .` ; 157} 158 159// Add helpful information about what is supported 160const supportedTypes :string [] = [ 'text files' , 'PDFs' ]; 161 162if ( hasVision ) supportedTypes . push ( 'images' ); 163 164if ( hasAudio ) supportedTypes . push ( 'audio files' ); 165 166if ( hasVideo ) supportedTypes . push ( 'video files' ); 167 168message += ` This model supports: ${ supportedTypes . join ( ', ' )} .` ; 169 170return message ; 171}