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.

3.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

// One toast per Hub pick, from planning through download to loading.

import { toast } from "@/lib/toast";

export type PickToastPhase = "preparing" | "queued" | "downloading" | "waiting" | "ready";

export interface PickToastProgress {
  downloadedBytes: number;
  totalBytes: number;
}

export interface StagedPlanProgress extends PickToastProgress {
  plan: number;
}

export interface PickToastOptions {
  describe: (phase: PickToastPhase, progress: PickToastProgress | null, modelName?: string) => unknown;
  classNames?: Record<string, string>;
}

export interface PickToast {
  show: (modelName?: string) => string;
  dismiss: (id: string | undefined) => void;
  dismissAll: () => void;
  setPhase: (id: string | undefined, phase: PickToastPhase, plan?: number) => void;
  progress: (progress: StagedPlanProgress | null) => void;
  /** Transfer ownership to the load; return undefined if the toast is gone. */
  take: (id: string | undefined) => string | undefined;
}

// Images and Video share Sonner's store, so IDs must be unique across controllers.
let seq = 0;

export function createPickToast({ describe, classNames }: PickToastOptions): PickToast {
  let live: {
    id: string;
    phase: PickToastPhase;
    plan?: number;
    progress: PickToastProgress | null;
    modelName?: string;
  } | null = null;

  const render = () => {
    if (!live) return;
    const id = live.id;
    toast(null, {
      id,
      description: describe(live.phase, live.progress, live.modelName) as never,
      duration: Infinity,
      closeButton: true,
      ...(classNames ? { classNames } : {}),
      // Prevent updates from reopening a user-dismissed toast.
      onDismiss: () => {
        if (live?.id === id) live = null;
      },
    });
  };

  return {
    show: (modelName) => {
      if (live) toast.dismiss(live.id);
      seq += 1;
      live = { id: `diffusion-pick:${seq}`, phase: "preparing", progress: null, modelName };
      render();
      return live.id;
    },
    dismiss: (id) => {
      if (id === undefined || live?.id !== id) return;
      live = null;
      toast.dismiss(id);
    },
    dismissAll: () => {
      if (!live) return;
      const gone = live.id;
      live = null;
      toast.dismiss(gone);
    },
    setPhase: (id, phase, plan) => {
      if (id === undefined || live?.id !== id) return;
      live = { ...live, phase, plan: phase === "downloading" ? plan : undefined, progress: null };
      render();
    },
    progress: (progress) => {
      if (!progress || live?.phase !== "downloading" || live.plan !== progress.plan) return;
      if (
        live.progress?.downloadedBytes === progress.downloadedBytes &&
        live.progress?.totalBytes === progress.totalBytes
      ) {
        return;
      }
      live = {
        ...live,
        progress: { downloadedBytes: progress.downloadedBytes, totalBytes: progress.totalBytes },
      };
      render();
    },
    take: (id) => {
      if (id === undefined || live?.id !== id) return undefined;
      live = null;
      return id;
    },
  };
}