model-names.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Normalizes a model name by extracting the filename from a path.
  3. *
  4. * Handles both forward slashes (/) and backslashes (\) as path separators.
  5. * If the model name is just a filename (no path), returns it as-is.
  6. *
  7. * @param modelName - The model name or path to normalize
  8. * @returns The normalized model name (filename only)
  9. *
  10. * @example
  11. * normalizeModelName('models/llama-3.1-8b') // Returns: 'llama-3.1-8b'
  12. * normalizeModelName('C:\\Models\\gpt-4') // Returns: 'gpt-4'
  13. * normalizeModelName('simple-model') // Returns: 'simple-model'
  14. * normalizeModelName(' spaced ') // Returns: 'spaced'
  15. * normalizeModelName('') // Returns: ''
  16. */
  17. export function normalizeModelName(modelName: string): string {
  18. const trimmed = modelName.trim();
  19. if (!trimmed) {
  20. return '';
  21. }
  22. const segments = trimmed.split(/[\\/]/);
  23. const candidate = segments.pop();
  24. const normalized = candidate?.trim();
  25. return normalized && normalized.length > 0 ? normalized : trimmed;
  26. }
  27. /**
  28. * Validates if a model name is valid (non-empty after normalization).
  29. *
  30. * @param modelName - The model name to validate
  31. * @returns true if valid, false otherwise
  32. */
  33. export function isValidModelName(modelName: string): boolean {
  34. return normalizeModelName(modelName).length > 0;
  35. }