char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Linting & Formatting scripts (#26819)92d1bb0c9

char/aliceai-t5-moe
4.6 KiB171 linesraw
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(
18	filename: string,
19	mimeType: string | undefined,
20	capabilities: ModalityCapabilities
21): boolean {
22	const category = mimeType ? getFileTypeCategory(mimeType) : null;
23
24	// If we can't determine the category from MIME type, fall back to general support check
25	if (!category) {
26		// For unknown types, only allow if they might be text files
27		// This is a conservative approach for edge cases
28		return true; // Let the existing isFileTypeSupported handle this
29	}
30
31	switch (category) {
32		case FileTypeCategory.TEXT:
33			// Text files are always supported
34			return true;
35
36		case FileTypeCategory.PDF:
37			// PDFs are always supported (will be processed as text for non-vision models)
38			return true;
39
40		case FileTypeCategory.IMAGE:
41			// Images require vision support
42			return capabilities.hasVision;
43
44		case FileTypeCategory.AUDIO:
45			// Audio files require audio support
46			return capabilities.hasAudio;
47
48		case FileTypeCategory.VIDEO:
49			// Video files require video support
50			return capabilities.hasVideo;
51
52		default:
53			// Unknown categories - be conservative and allow
54			return 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(
65	files: File[],
66	capabilities: ModalityCapabilities
67): {
68	supportedFiles: File[];
69	unsupportedFiles: File[];
70	modalityReasons: Record<string, string>;
71} {
72	const supportedFiles: File[] = [];
73	const unsupportedFiles: File[] = [];
74	const modalityReasons: Record<string, string> = {};
75	const { hasAudio, hasVideo, hasVision } = capabilities;
76
77	for (const file of files) {
78		const category = getFileTypeCategory(file.type);
79
80		let isSupported = true;
81		let reason = '';
82
83		switch (category) {
84			case FileTypeCategory.IMAGE:
85				if (!hasVision) {
86					isSupported = false;
87					reason = 'Images require a vision-capable model';
88				}
89
90				break;
91
92			case FileTypeCategory.AUDIO:
93				if (!hasAudio) {
94					isSupported = false;
95					reason = 'Audio files require an audio-capable model';
96				}
97
98				break;
99
100			case FileTypeCategory.VIDEO:
101				if (!hasVideo) {
102					isSupported = false;
103					reason = 'Video files require a video-capable model';
104				}
105
106				break;
107
108			case FileTypeCategory.TEXT:
109			case FileTypeCategory.PDF:
110				// Always supported
111				break;
112
113			default:
114				// For unknown types, check if it's a generally supported file type
115				// This handles edge cases and maintains backward compatibility
116				break;
117		}
118
119		if (isSupported) {
120			supportedFiles.push(file);
121		} else {
122			unsupportedFiles.push(file);
123			modalityReasons[file.name] = reason;
124		}
125	}
126
127	return { 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(
138	unsupportedFiles: File[],
139	modalityReasons: Record<string, string>,
140	capabilities: ModalityCapabilities
141): string {
142	if (unsupportedFiles.length === 0) return '';
143
144	const { hasAudio, hasVideo, hasVision } = capabilities;
145
146	let message = '';
147
148	if (unsupportedFiles.length === 1) {
149		const file = unsupportedFiles[0];
150		const reason = modalityReasons[file.name];
151
152		message = `The file "${file.name}" cannot be uploaded: ${reason}.`;
153	} else {
154		const fileNames = unsupportedFiles.map((f) => f.name).join(', ');
155
156		message = `The following files cannot be uploaded: ${fileNames}.`;
157	}
158
159	// Add helpful information about what is supported
160	const supportedTypes: string[] = ['text files', 'PDFs'];
161
162	if (hasVision) supportedTypes.push('images');
163
164	if (hasAudio) supportedTypes.push('audio files');
165
166	if (hasVideo) supportedTypes.push('video files');
167
168	message += ` This model supports: ${supportedTypes.join(', ')}.`;
169
170	return message;
171}