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