char-fork/llama.cpp

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

Aleksander Grygierui: Agentic Content UX improvements (#25450)32beb244f

char/aliceai-t5-moe
2.1 KiB58 linesraw
1import { FILE_PATH_SEPARATOR_REGEX } from '$lib/constants';
2
3/**
4 * Normalizes a model name by extracting the filename from a path, but preserves Hugging Face repository format.
5 *
6 * Handles both forward slashes (/) and backslashes (\) as path separators.
7 * - If the model name has exactly one slash (org/model format), preserves the full "org/model" name
8 * - If the model name has no slash or multiple slashes, extracts just the filename
9 * - If the model name is just a filename (no path), returns it as-is.
10 *
11 * @param modelName - The model name or path to normalize
12 * @returns The normalized model name
13 *
14 * @example
15 * normalizeModelName('models/llama-3.1-8b') // Returns: 'llama-3.1-8b' (multiple slashes -> filename)
16 * normalizeModelName('C:\\Models\\gpt-4') // Returns: 'gpt-4' (multiple slashes -> filename)
17 * normalizeModelName('meta-llama/Llama-3.1-8B') // Returns: 'meta-llama/Llama-3.1-8B' (Hugging Face format)
18 * normalizeModelName('simple-model') // Returns: 'simple-model' (no slash)
19 * normalizeModelName('  spaced  ') // Returns: 'spaced'
20 * normalizeModelName('') // Returns: ''
21 */
22export function normalizeModelName(modelName: string): string {
23	const trimmed = modelName.trim();
24
25	if (!trimmed) {
26		return '';
27	}
28
29	const segments = trimmed.split(FILE_PATH_SEPARATOR_REGEX);
30
31	// If we have exactly 2 segments (one slash), treat it as Hugging Face repo format
32	// and preserve the full "org/model" format
33	if (segments.length === 2) {
34		const [org, model] = segments;
35		const trimmedOrg = org?.trim();
36		const trimmedModel = model?.trim();
37
38		if (trimmedOrg && trimmedModel) {
39			return `${trimmedOrg}/${trimmedModel}`;
40		}
41	}
42
43	// For other cases (no slash, or multiple slashes), extract just the filename
44	const candidate = segments.pop();
45	const normalized = candidate?.trim();
46
47	return normalized && normalized.length > 0 ? normalized : trimmed;
48}
49
50/**
51 * Validates if a model name is valid (non-empty after normalization).
52 *
53 * @param modelName - The model name to validate
54 * @returns true if valid, false otherwise
55 */
56export function isValidModelName(modelName: string): boolean {
57	return normalizeModelName(modelName).length > 0;
58}