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.

15.2 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 { useHubDownloadQueue, useQueuedHubEntries } from "./use-hub-download-queue";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { useEngines } from "@/features/model-picker/hooks/use-engines";
import {
  audioCppDisplayName,
  isAudioCppFolderId,
} from "../../audio/audio-cpp-catalog";
import { hasAuthToken, mustChangePassword } from "@/features/auth/session";
import { isTauri } from "@/lib/api-base";
import { ChevronDownStandardIcon } from "@/lib/chevron-icons";
import { cn } from "@/lib/utils";
import {
  Alert02Icon,
  ArrowReloadHorizontalIcon,
  Cancel01Icon,
  CheckmarkCircle02Icon,
  Download01Icon,
} from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { useRouterState } from "@tanstack/react-router";
import { useEffect, useMemo, useState } from "react";
import {
  type ManagedDownload,
  downloadManager,
  hydrateDownloadManager,
  useDownloadManagerStore,
} from "./download-manager-controller";
import { DownloadProgressBar } from "./download-progress-bar";
import { presentedProgress } from "./download-presentation";
import { isRetryableDownload } from "./retry-download";
import { retryDownload } from "./retry-start";
import {
  type PendingStart,
  usePendingStarts,
  visiblePendingStarts,
} from "./pending-starts";
import { assetLabel } from "./required-assets";
import { queuedStagedEntries, useStagedDownloadQueues } from "./staged-download-queue";
import { isTrainingDownloadVariant } from "./training-downloads";
import { useTrainingDownloads } from "./use-training-downloads";

type RowSubject = Pick<
  ManagedDownload,
  "repoId" | "variant" | "presentation" | "checkpoint" | "scopedFiles"
>;

function createOrderedJobKeysSelector(): (state: {
  jobs: Record<string, ManagedDownload>;
}) => string[] {
  let cache: { signature: string; keys: string[] } = {
    signature: "",
    keys: [],
  };
  return (state) => {
    const ordered = Object.values(state.jobs)
      .map((job) => ({ key: job.key, startedAt: job.startedAt }))
      .sort((a, b) => a.startedAt - b.startedAt);
    const signature = ordered
      .map((job) => `${job.key}\u0001${job.startedAt}`)
      .join("\u0002");
    if (signature === cache.signature) {
      return cache.keys;
    }
    const keys = ordered.map((job) => job.key);
    cache = { signature, keys };
    return keys;
  };
}

function selectActiveJobCount(state: {
  jobs: Record<string, ManagedDownload>;
}): number {
  let count = 0;
  for (const job of Object.values(state.jobs)) {
    if (job.state === "running" || job.state === "cancelling") count += 1;
  }
  return count;
}

function canUseDownloadManager(pathname: string): boolean {
  if (isTauri) return true;
  if (
    pathname === "/login" ||
    pathname === "/change-password" ||
    pathname === "/signup"
  ) {
    return false;
  }
  return hasAuthToken() && !mustChangePassword();
}

function repoLabel(repoId: string): string {
  return isAudioCppFolderId(repoId) ? audioCppDisplayName(repoId) : repoId;
}

function isRequiredAssetJob(job: RowSubject): boolean {
  if (!job.variant?.startsWith("@")) return false;
  // The staging page's checkpoint tag is the only reliable answer, since the extension decides
  // nothing and companion repos carry .safetensors too; the old guess stays for jobs persisted
  // before the flag, so labels do not change after a restart.
  const isModelFile =
    job.checkpoint ??
    job.scopedFiles?.some((file) => file.toLowerCase().endsWith(".gguf"));
  return !isModelFile;
}

// Only staged media picks set checkpoint=false; other scoped jobs (Decision API model) carry no flag.
const REQUIRED_ASSET_NOTE =
  "Required to run this model. Downloaded once, shared across compatible variants.";

function variantSuffix(job: RowSubject): string {
  if (isTrainingDownloadVariant(job.variant)) return " · Training model";
  if (job.variant?.startsWith("@")) {
    return ` · ${
      isRequiredAssetJob(job)
        ? assetLabel(
            { repoId: job.repoId, files: job.scopedFiles, bytes: 0 },
            "Required assets",
          )
        : "Model file"
    }`;
  }
  return job.variant ? ` · ${job.variant}` : "";
}

function RowTitle({ job }: { job: RowSubject }) {
  return (
    <span className="min-w-0 flex-1 truncate text-ui-12p5 font-medium text-foreground">
      {job.presentation?.label ?? repoLabel(job.repoId)}
      <span className="text-muted-foreground">
        {job.presentation ? ` · ${repoLabel(job.repoId)}` : variantSuffix(job)}
      </span>
    </span>
  );
}

function RowDetail({ job }: { job: RowSubject }) {
  if (isTrainingDownloadVariant(job.variant)) {
    return <div className="text-ui-10p5 text-muted-foreground">Current files; additional model files may follow.</div>;
  }
  return job.presentation ? (
    <div className="truncate text-ui-10p5 text-muted-foreground">
      {job.presentation.filename}
    </div>
  ) : isRequiredAssetJob(job) && job.checkpoint === false ? (
    <div className="text-ui-10p5 text-muted-foreground">
      {REQUIRED_ASSET_NOTE}
    </div>
  ) : null;
}

function StartingLine() {
  return <div className="text-ui-11 text-muted-foreground">Starting…</div>;
}

function PendingStartRow({ start }: { start: PendingStart }) {
  return (
    <li className="flex flex-col gap-1.5 py-2.5 pl-4 pr-3">
      <div className="flex items-center gap-2">
        <RowTitle job={start} />
      </div>
      <RowDetail job={start} />
      <StartingLine />
    </li>
  );
}

function StatusLine({ job }: { job: ManagedDownload }) {
  if (job.state === "complete") {
    return <span className="text-status-success">Downloaded</span>;
  }
  if (job.state === "cancelled") {
    return <span>Cancelled. Partial files kept.</span>;
  }
  if (job.state === "error") {
    return (
      <span className="text-destructive">{job.error ?? "Download failed"}</span>
    );
  }
  if (job.state === "cancelling") {
    return <span>Cancelling…</span>;
  }
  if (job.error) {
    return <span className="text-status-warning">{job.error}</span>;
  }
  return null;
}

function DownloadRow({
  jobKey,
  pendingStart,
}: {
  jobKey: string;
  pendingStart?: PendingStart;
}) {
  const job = useDownloadManagerStore((state) => state.jobs[jobKey]);
  if (!job) return null;
  const starting = pendingStart !== undefined;
  const subject = pendingStart ?? job;
  const active = job.state === "running" || job.state === "cancelling";
  const terminal =
    job.state === "complete" ||
    job.state === "cancelled" ||
    job.state === "error";
  const canRetry =
    (job.state === "error" || job.state === "cancelled") &&
    !starting &&
    isRetryableDownload(job);
  const progress = presentedProgress(job);
  return (
    <li className="flex flex-col gap-1.5 py-2.5 pl-4 pr-3">
      <div className="flex items-center gap-2">
        <RowTitle job={subject} />
        {job.state === "complete" && !starting && (
          <HugeiconsIcon
            icon={CheckmarkCircle02Icon}
            strokeWidth={2}
            className="size-4 shrink-0 text-status-success"
          />
        )}
        {job.state === "error" && !starting && (
          <HugeiconsIcon
            icon={Alert02Icon}
            strokeWidth={2}
            className="size-4 shrink-0 text-destructive"
          />
        )}
        {canRetry && (
          <Tooltip>
            <TooltipTrigger asChild={true}>
              <button
                type="button"
                aria-label="Retry download"
                onClick={() => void retryDownload(job)}
                className={cn(
                  "inline-flex size-6 shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground transition-colors",
                  "hover:bg-[color-mix(in_oklab,var(--foreground)_calc(6%*var(--contrast-wash-gain,1)),transparent)] hover:text-foreground dark:hover:bg-[rgb(255_255_255_/_calc(0.06*var(--contrast-wash-gain,1)))]",
                )}
              >
                <HugeiconsIcon
                  icon={ArrowReloadHorizontalIcon}
                  strokeWidth={1.75}
                  className="size-3.5"
                />
              </button>
            </TooltipTrigger>
            <TooltipContent side="top" sideOffset={4}>
              Retry download
            </TooltipContent>
          </Tooltip>
        )}
        <Tooltip>
          <TooltipTrigger asChild={true}>
            <button
              type="button"
              aria-label={active ? "Cancel download" : "Dismiss"}
              disabled={job.state === "cancelling" || starting}
              onClick={() =>
                active
                  ? void downloadManager.cancel(job.key)
                  : downloadManager.dismiss(job.key)
              }
              className={cn(
                "inline-flex size-6 shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground transition-colors",
                "hover:bg-[color-mix(in_oklab,var(--foreground)_calc(6%*var(--contrast-wash-gain,1)),transparent)] hover:text-foreground disabled:cursor-default disabled:opacity-50 dark:hover:bg-[rgb(255_255_255_/_calc(0.06*var(--contrast-wash-gain,1)))]",
              )}
            >
              <HugeiconsIcon
                icon={Cancel01Icon}
                strokeWidth={1.75}
                className="size-3.5"
              />
            </button>
          </TooltipTrigger>
          <TooltipContent side="top" sideOffset={4}>
            {active ? "Cancel download" : "Dismiss"}
          </TooltipContent>
        </Tooltip>
      </div>
      <RowDetail job={subject} />
      {active ? (
        <DownloadProgressBar
          progress={progress}
          bytesPerSec={job.bytesPerSec}
          cancelling={job.state === "cancelling"}
          etaSeconds={job.etaSeconds}
          activity={job.activity ?? (isTrainingDownloadVariant(job.variant) ? "Preparing training model files…" : undefined)}
        />
      ) : null}
      {!starting && job.details?.length ? (
        <details className="text-ui-11">
          <summary>Installation details</summary>
          <pre className="max-h-40 overflow-auto whitespace-pre-wrap break-all">
            {job.details.join("\n")}
          </pre>
        </details>
      ) : null}
      {starting ? (
        <StartingLine />
      ) : terminal || job.state === "cancelling" || job.error ? (
        <div className="px-0 text-ui-11 text-muted-foreground tabular-nums">
          <StatusLine job={job} />
        </div>
      ) : null}
    </li>
  );
}

export function DownloadManagerPanel({
  positioned = true,
}: { positioned?: boolean } = {}) {
  const pathname = useRouterState({ select: (s) => s.location.pathname });
  const enabled = canUseDownloadManager(pathname);
  useEngines(enabled, true);
  useTrainingDownloads(enabled);
  useHubDownloadQueue();
  const [collapsed, setCollapsed] = useState(false);

  useEffect(() => {
    if (!enabled) return;
    hydrateDownloadManager();
  }, [enabled]);

  const selectOrderedJobKeys = useMemo(createOrderedJobKeysSelector, []);
  const jobKeys = useDownloadManagerStore(selectOrderedJobKeys);
  const hubQueued = useQueuedHubEntries();
  const stagedQueues = useStagedDownloadQueues((s) => s.queues);
  const jobs = useDownloadManagerStore((s) => s.jobs);
  const queued = [...hubQueued, ...queuedStagedEntries(stagedQueues, jobs)];
  const pendingStarts = usePendingStarts((s) => s.starts);
  const pending = visiblePendingStarts(pendingStarts, jobs, queued);
  const unlistedStarts = pending.filter((start) => !(start.key in jobs));
  const startingByKey = new Map(
    pending.map((start) => [start.key, start] as const),
  );
  const activeCount =
    useDownloadManagerStore(selectActiveJobCount) +
    queued.length +
    pending.length;

  if (
    !enabled ||
    (jobKeys.length === 0 && queued.length === 0 && pending.length === 0)
  ) {
    return null;
  }

  const headerLabel =
    activeCount > 0
      ? `Downloading ${activeCount} ${activeCount === 1 ? "item" : "items"}`
      : "Downloads";

  return (
    <div
      className={cn(
        // min-h-0: a flex item's min-height defaults to auto, so the capped stack would squeeze the card.
        "pointer-events-none",
        positioned ? "fixed bottom-4 right-4 z-50" : "flex min-h-0 justify-end",
      )}
    >
      {collapsed ? (
        <Tooltip>
          <TooltipTrigger asChild={true}>
            <button
              type="button"
              aria-label={headerLabel}
              onClick={() => setCollapsed(false)}
              className="hub-download-fab pointer-events-auto"
            >
              <HugeiconsIcon
                icon={Download01Icon}
                strokeWidth={1.75}
                className="size-[calc(18px*var(--ui-space-scale,1))]"
              />
              {activeCount > 0 && (
                <span className="hub-download-fab-badge">{activeCount}</span>
              )}
            </button>
          </TooltipTrigger>
          <TooltipContent side="left" sideOffset={6}>
            {headerLabel}
          </TooltipContent>
        </Tooltip>
      ) : (
        <div className="hub-download-panel pointer-events-auto flex min-h-0 w-[min(400px,calc(100vw-2rem))] flex-col overflow-hidden">
          <div className="flex items-center gap-2 border-b border-[color-mix(in_oklab,var(--foreground)_calc(7%*var(--contrast-edge-gain,1)),transparent)] py-2 pl-4 pr-3">
            <span className="min-w-0 flex-1 truncate text-ui-12p5 font-semibold text-foreground">
              {headerLabel}
            </span>
            <button
              type="button"
              aria-label="Collapse downloads"
              onClick={() => setCollapsed(true)}
              className="inline-flex size-6 shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-[color-mix(in_oklab,var(--foreground)_calc(6%*var(--contrast-wash-gain,1)),transparent)] hover:text-foreground dark:hover:bg-[rgb(255_255_255_/_calc(0.06*var(--contrast-wash-gain,1)))]"
            >
              <HugeiconsIcon
                icon={ChevronDownStandardIcon}
                strokeWidth={1.75}
                className="size-3.5"
              />
            </button>
          </div>
          <ul className="max-h-[60dvh] divide-y divide-foreground/[0.06] overflow-y-auto [scrollbar-width:thin]">
            {jobKeys.map((jobKey) => (
              <DownloadRow
                key={jobKey}
                jobKey={jobKey}
                pendingStart={startingByKey.get(jobKey)}
              />
            ))}
            {queued.map((entry, i) => <li key={`${entry.planId}:${i}`} className="flex flex-col gap-1.5 py-2.5 pl-4 pr-3">
              <span className="truncate text-ui-12p5 font-medium">{entry.repoId}<span className="text-muted-foreground"> · {entry.checkpoint !== false ? "Model file" : assetLabel(entry, "Required assets")}</span></span>
              <span className="text-ui-11 text-muted-foreground">{entry.checkpoint !== false ? "Queued" : `Queued · ${REQUIRED_ASSET_NOTE}`}</span>
            </li>)}
            {unlistedStarts.map((start) => (
              <PendingStartRow key={start.key} start={start} />
            ))}
          </ul>
        </div>
      )}
    </div>
  );
}