Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

2.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

export const DEFAULT_PRESET_NAME = "Default";

export function configKey(value: unknown): string {
  return JSON.stringify(value ?? null);
}

export function getBuiltinVariantName(usedNames: Set<string>): string {
  let suffix = 1;
  let candidate = `${DEFAULT_PRESET_NAME} ${suffix}`;
  while (usedNames.has(candidate)) {
    suffix += 1;
    candidate = `${DEFAULT_PRESET_NAME} ${suffix}`;
  }
  return candidate;
}

export function closestResolutionIndex(
  presets: [number, number][],
  targetWidth: number,
  targetHeight: number,
): number {
  if (presets.length === 0) {
    return 0;
  }
  const targetAspect = targetWidth / targetHeight;
  const targetArea = targetWidth * targetHeight;
  let bestIndex = 0;
  let bestScore = Number.POSITIVE_INFINITY;
  presets.forEach(([width, height], index) => {
    const aspectDistance = Math.abs(Math.log(width / height / targetAspect));
    const areaDistance = Math.abs(Math.log((width * height) / targetArea));
    const score = aspectDistance * 4 + areaDistance;
    if (score < bestScore) {
      bestScore = score;
      bestIndex = index;
    }
  });
  return bestIndex;
}

export function closestDurationIndex(
  durations: Array<{ seconds: number }>,
  targetSeconds: number,
): number {
  if (durations.length === 0) {
    return 0;
  }
  let bestIndex = 0;
  let bestDistance = Number.POSITIVE_INFINITY;
  durations.forEach(({ seconds }, index) => {
    const distance = Math.abs(seconds - targetSeconds);
    if (distance < bestDistance) {
      bestDistance = distance;
      bestIndex = index;
    }
  });
  return bestIndex;
}

/** A stored recipe owns the first seed; later model defaults apply unless the user claimed
 *  the form after the pick. */
export function shouldApplyModelDefaults(
  alreadySeeded: boolean,
  storedRecipe: boolean,
  supersededByUser = false,
): boolean {
  return !supersededByUser && (alreadySeeded || !storedRecipe);
}

/** What hydration loads into the form. Without working-recipe restore only a named preset loads,
 *  so a store holding just Default leaves the form as a fresh page would. */
export function hydratedParams<Params>(
  currentParams: Params,
  active: { name: string; params: Params },
  restoreWorkingParams: boolean,
): Params | null {
  if (restoreWorkingParams) {
    return currentParams;
  }
  return active.name === DEFAULT_PRESET_NAME ? null : active.params;
}