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.

7.6 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 { useCallback, useEffect, useMemo, useRef } from "react";
import { useShallow } from "zustand/react/shallow";
import { useLatestRef } from "../hooks/use-latest-ref";
import type { ResolvedTransport } from "./constants";
import type { TransportConflictInfo } from "./types";
import {
  type DownloadKind,
  type DownloadRequest,
  type JobListeners,
  downloadManager,
  findActiveScopedJobForRepo,
  jobKeyOf,
  repoKeyOf,
  selectActiveJob,
  subscribeJobListeners,
  useDownloadManagerStore,
} from "./download-manager-controller";

export interface DownloadJobProgress {
  scopedFiles?: readonly string[];
  variant: string | null;
  expectedBytes: number;
  downloadedBytes: number;
  fraction: number;
  /** False when the last poll held `downloadedBytes` over from the previous reading (`resolveProgressUpdate`). */
  measuredTransfer?: boolean;
}

export interface DownloadJob {
  progress: DownloadJobProgress | null;
  bytesPerSec: number;
  etaSeconds: number;
  transport: ResolvedTransport | null;
  /** Set when a Xet run fell back to HTTP; decides the stop control instead of `transport`. */
  cancelTransport: ResolvedTransport | null;
  cancelling: boolean;
  repoPeerActive: boolean;
  transportConflict: TransportConflictInfo | null;
  requestStartDownload: (
    variant: string | null,
    expectedBytes: number,
    presentation?: DownloadRequest["presentation"],
  ) => Promise<void>;
  cancelDownload: (variant: string | null) => void;
  setExpectedBytes: (bytes: number, variant?: string | null) => void;
  resumeConflict: () => void;
  restartConflict: () => void;
  cancelConflict: () => void;
}

export interface RepoDownloadConfig {
  kind: DownloadKind;
  repoId: string;
  activeVariant?: string | null;
  onComplete?: JobListeners["onComplete"];
  onCancelled?: JobListeners["onCancelled"];
  onError?: JobListeners["onError"];
  // Attach to a running no-variant backend download (GGUF surfaces adopt their own variant).
  autoAdopt?: boolean;
  /** Non-GGUF scoped jobs only: GGUF file jobs belong to the GGUF card. */
  includeScopedJobs?: boolean;
}

/** Job state lives in the store, so a download keeps running after the card unmounts. */
export function useRepoDownload(config: RepoDownloadConfig): DownloadJob {
  const {
    kind,
    repoId,
    activeVariant,
    onComplete,
    onCancelled,
    onError,
    autoAdopt,
    includeScopedJobs = false,
  } = config;

  const handlersRef = useLatestRef<JobListeners>({
    onComplete,
    onCancelled,
    onError,
  });
  useEffect(() => {
    return subscribeJobListeners(kind, repoId, {
      onComplete: (variant, bytes) =>
        handlersRef.current.onComplete?.(variant, bytes),
      onCancelled: (variant) => handlersRef.current.onCancelled?.(variant),
      onError: (variant) => handlersRef.current.onError?.(variant),
    });
  }, [handlersRef, kind, repoId]);

  useEffect(() => {
    if (!autoAdopt) return;
    const controller = new AbortController();
    void downloadManager.probeAndAdopt(kind, repoId, controller.signal);
    return () => controller.abort();
  }, [autoAdopt, kind, repoId]);

  const activeState = useDownloadManagerStore(
    useShallow((state) => {
      if (activeVariant === undefined) {
        return {
          active: selectActiveJob(state, kind, repoId),
          repoPeerActive: false,
        };
      }
      const active =
        selectActiveJob(state, kind, repoId, activeVariant) ??
        (includeScopedJobs && activeVariant === null
          ? findActiveScopedJobForRepo(state.jobs, kind, repoId, "model")
          : null);
      const repoActive = selectActiveJob(state, kind, repoId);
      return {
        active,
        repoPeerActive: Boolean(repoActive && repoActive.key !== active?.key),
      };
    }),
  );
  const active = activeState.active;
  const conflictKey = useMemo(
    () => jobKeyOf(kind, repoId, activeVariant ?? null),
    [activeVariant, kind, repoId],
  );
  const repoConflictKey = useMemo(
    () => repoKeyOf(kind, repoId),
    [kind, repoId],
  );
  const visibleConflict = useDownloadManagerStore(
    useShallow((state) => {
      const exact = state.conflicts[conflictKey];
      if (exact) return { key: conflictKey, info: exact.info };
      const scoped = Object.entries(state.conflicts).find(([key]) =>
        key.startsWith(`${repoConflictKey}#`),
      );
      return scoped
        ? { key: scoped[0], info: scoped[1].info }
        : { key: conflictKey, info: null };
    }),
  );
  const visibleConflictKey = visibleConflict.key;
  const transportConflict = visibleConflict.info;

  // Chat/Video park this hook on an idle repo id; remember the key so a later real repo clears it.
  const repoIdRef = useRef(repoId);
  const preservedConflictKeyRef = useRef<string | null>(null);
  repoIdRef.current = repoId;
  useEffect(
    () => () => {
      const parked = repoIdRef.current;
      if (
        parked === "__staged_download_idle__" ||
        parked === "__hub_autoload_idle__"
      ) {
        preservedConflictKeyRef.current = conflictKey;
        return;
      }
      if (preservedConflictKeyRef.current) {
        downloadManager.cancelConflict(preservedConflictKeyRef.current);
        preservedConflictKeyRef.current = null;
      }
      downloadManager.cancelConflict(conflictKey);
    },
    [conflictKey],
  );

  const requestStartDownload = useCallback(
    async (
      variant: string | null,
      expectedBytes: number,
      presentation?: DownloadRequest["presentation"],
    ) => {
      // This surface renders the conflict resolver, so the awaited result is ignored.
      await downloadManager.requestStart({
        kind,
        repoId,
        variant,
        expectedBytes,
        ...(presentation ? { presentation } : {}),
      });
    },
    [kind, repoId],
  );

  const cancelDownload = useCallback(
    (variant: string | null) => {
      const state = useDownloadManagerStore.getState();
      const activeJob = selectActiveJob(state, kind, repoId, variant);
      void downloadManager.cancel(
        activeJob?.key ?? jobKeyOf(kind, repoId, variant),
      );
    },
    [kind, repoId],
  );

  const setExpectedBytes = useCallback(
    (bytes: number, variant: string | null = null) =>
      downloadManager.setExpected(kind, repoId, variant, bytes),
    [kind, repoId],
  );

  const resumeConflict = useCallback(
    () => downloadManager.resumeConflict(visibleConflictKey),
    [visibleConflictKey],
  );
  const restartConflict = useCallback(
    () => downloadManager.restartConflict(visibleConflictKey),
    [visibleConflictKey],
  );
  const cancelConflict = useCallback(
    () => downloadManager.cancelConflict(visibleConflictKey),
    [visibleConflictKey],
  );

  const progress = useMemo<DownloadJobProgress | null>(
    () =>
      active
        ? {
            variant: active.variant,
            scopedFiles: active.scopedFiles,
            expectedBytes: active.expectedBytes,
            downloadedBytes: active.downloadedBytes,
            fraction: active.fraction,
            measuredTransfer: active.measuredTransfer !== false,
          }
        : null,
    [active],
  );

  return {
    progress,
    bytesPerSec: active?.bytesPerSec ?? 0,
    etaSeconds: active?.etaSeconds ?? 0,
    transport: active?.transport ?? null,
    cancelTransport: active?.cancelTransport ?? null,
    cancelling: active?.state === "cancelling",
    repoPeerActive: activeState.repoPeerActive,
    transportConflict,
    requestStartDownload,
    cancelDownload,
    setExpectedBytes,
    resumeConflict,
    restartConflict,
    cancelConflict,
  };
}