Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

2.6 KB
Raw Blame
/** owner/repo with optional :variant[:path] qualifiers - the HF repo shape. */
const HF_REPO_PATTERN = /^[\w.-]+\/[\w.-]+(:[\w./-]*)*$/;
// 12 covers the longest real model extension, ".safetensors" (11 letters).
const FILE_EXTENSION_PATTERN = /\.[A-Za-z0-9]{1,12}$/;
/**
 * Extensions that mark a relative path as a model file rather than an HF repo id. Deliberately narrower than
 * FILE_EXTENSION_PATTERN: real repo ids end in dot-suffixes too ("stabilityai/stable-diffusion-2.1").
 */
const MODEL_FILE_EXTENSION_PATTERN = /\.(safetensors|ckpt|pt|pth|bin|gguf|onnx|pkl)$/i;
const RELATIVE_PATH_PATTERN = /^\.{1,2}[\\/]/;

/** isInstallable narrows to a source with a label key; searches never share installable source shapes. */
export type SourceKind =
  | {
      isInstallable: true;
      looksLocal: boolean;
      looksUrl: boolean;
      looksRepo: boolean;
      localKind: 'file' | 'folder' | null;
      /** i18n key for the detected source kind. */
      labelKey: string;
    }
  | {
      isInstallable: false;
      looksLocal: false;
      looksUrl: false;
      looksRepo: false;
      localKind: null;
      labelKey: null;
    };

const classifyLocalPath = (value: string): 'file' | 'folder' => {
  if (/[\\/]$/.test(value)) {
    return 'folder';
  }

  const lastSegment = value.split(/[\\/]/).at(-1) ?? '';

  return FILE_EXTENSION_PATTERN.test(lastSegment) ? 'file' : 'folder';
};

/** Classify the box's text: a search term, or an installable source to Pull. */
export const classifySource = (value: string): SourceKind => {
  const looksLocal = value.startsWith('/') || /^[A-Za-z]:[\\/]/.test(value);
  const looksUrl = /^https?:\/\//i.test(value);
  // Relative paths fall through to search even if repo-shaped; inspect extensions before colon qualifiers so valid repo
  // file selectors remain installable.
  const looksRelativePath =
    RELATIVE_PATH_PATTERN.test(value) || MODEL_FILE_EXTENSION_PATTERN.test(value.split(':', 1)[0] ?? '');
  const looksRepo = !looksLocal && !looksUrl && !looksRelativePath && HF_REPO_PATTERN.test(value);

  if (!looksLocal && !looksUrl && !looksRepo) {
    return {
      isInstallable: false,
      labelKey: null,
      localKind: null,
      looksLocal: false,
      looksRepo: false,
      looksUrl: false,
    };
  }

  const localKind = looksLocal ? classifyLocalPath(value) : null;

  return {
    isInstallable: true,
    labelKey: looksLocal
      ? localKind === 'file'
        ? 'models.sourceKind.filePath'
        : 'models.sourceKind.folderPath'
      : looksUrl
        ? 'models.sourceKind.url'
        : 'models.sourceKind.hfRepo',
    localKind,
    looksLocal,
    looksRepo,
    looksUrl,
  };
};