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.

5.1 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

import { Progress } from "@/components/ui/progress";
import { Spinner } from "@/components/ui/spinner";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";

export const MODEL_LOAD_TOAST_CLASSNAMES = {
  toast: "chat-model-load-toast items-center gap-2.5",
  content: "gap-0.5 flex-1 min-w-0",
  title: "leading-5",
  description: "mt-0 w-full",
};

type ModelLoadDescriptionProps = {
  title?: string | null;
  message?: string | null;
  progressPercent?: number | null;
  progressLabel?: string | null;
  className?: string;
};

function clampProgress(value: number): number {
  return Math.max(0, Math.min(100, value));
}

/** Split a progress label into a primary chunk and a secondary row (rate, ETA). */
function splitProgressLabel(
  label: string | null | undefined,
): { primary: string; secondary: string } {
  if (!label) return { primary: "", secondary: "" };
  const idx = label.indexOf(" \u2022 ");
  if (idx < 0) return { primary: label, secondary: "" };
  return {
    primary: label.slice(0, idx),
    secondary: label.slice(idx + 3),
  };
}

export function ModelLoadDescription({
  title,
  message,
  progressPercent,
  progressLabel,
  className,
}: ModelLoadDescriptionProps) {
  const hasProgress = typeof progressPercent === "number";
  const { primary: labelPrimary, secondary: labelSecondary } =
    splitProgressLabel(progressLabel);

  return (
    <div
      className={cn(
        "relative grid min-h-12 w-full grid-cols-[auto_minmax(0,1fr)] content-center items-start gap-x-3",
        className,
      )}
    >
      {/* With a title the spinner sits on its line (1.25em = leading-tight), so a description
          below it or a wrapped title leaves the spinner level with the first line. */}
      <div
        className={cn(
          "flex shrink-0 items-center",
          title ? "h-[1.25em]" : "self-center",
        )}
      >
        <Spinner className="size-3.5 text-muted-foreground" />
      </div>
      <div className="flex min-w-0 flex-col">
        {title ? <p className="text-foreground leading-tight font-semibold">{title}</p> : null}
        {hasProgress ? (
          <div className="w-full pt-1">
            <div className="flex items-center justify-between gap-2 text-ui-10 font-medium tracking-[0.08em] text-muted-foreground/80">
              <span className="min-w-0 truncate">{labelPrimary}</span>
              <span className="shrink-0 tabular-nums">
                {Math.round(clampProgress(progressPercent))}%
              </span>
            </div>
            {labelSecondary ? (
              <div className="truncate pt-0.5 text-ui-10 font-medium tracking-[0.08em] text-muted-foreground/60">
                {labelSecondary}
              </div>
            ) : null}
            <Progress
              value={clampProgress(progressPercent)}
              className="mt-1 h-1 bg-[color-mix(in_oklab,var(--foreground)_calc(8%*var(--contrast-wash-gain,1)),transparent)]"
            />
          </div>
        ) : message ? (
          <p className="pt-1 text-xs leading-relaxed text-muted-foreground">{message}</p>
        ) : null}
      </div>
    </div>
  );
}

type ModelLoadInlineStatusProps = {
  label: string;
  title: string;
  progressPercent?: number | null;
  progressLabel?: string | null;
  onStop?: () => void;
};

export function ModelLoadInlineStatus({
  label,
  title,
  progressPercent,
  progressLabel,
  onStop,
}: ModelLoadInlineStatusProps) {
  const hasProgress = typeof progressPercent === "number";

  return (
    <div className="flex min-w-[min(calc(20rem*var(--ui-space-scale,1)),100%)] max-sm:min-w-0 max-sm:flex-1 items-center gap-2.5 text-muted-foreground" title={title}>
      <div className="flex items-center gap-1.5 shrink-0">
        <Spinner className="size-3.5 shrink-0" />
        <span className="text-xs">{label}</span>
      </div>
      {hasProgress ? (
        <div className="flex min-w-0 flex-[1.35] items-center gap-2.5">
          <div className="min-w-[calc(7rem*var(--ui-space-scale,1))] max-sm:min-w-[calc(3rem*var(--ui-space-scale,1))] flex-1">
            <Progress value={clampProgress(progressPercent)} className="h-1 bg-[color-mix(in_oklab,var(--foreground)_calc(8%*var(--contrast-wash-gain,1)),transparent)]" />
          </div>
          <div
            className="flex shrink-0 items-center gap-1 text-ui-10 font-medium tracking-[0.08em] text-muted-foreground/80"
            title={progressLabel ?? undefined}
          >
            {/* Tight inline layout: show only the primary (bytes) chunk;
                full label (rate/ETA) stays in the tooltip. */}
            <span>{splitProgressLabel(progressLabel).primary}</span>
            <span className="tabular-nums">
              {Math.round(clampProgress(progressPercent))}%
            </span>
          </div>
        </div>
      ) : null}
      {onStop ? (
        <Button
          type="button"
          size="xs"
          variant="outline"
          className="shrink-0 text-ui-11"
          onClick={onStop}
        >
          Stop
        </Button>
      ) : null}
    </div>
  );
}