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.

42.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 { useCallback, useEffect, useRef, useState } from "react";
import { isAccountOwner } from "@/features/auth";
import {
  cancelPreStreamRunReservations,
  confirmStopRunningChatsIfNeeded,
  listGgufVariants,
  listLoras,
  loadModel,
  offloadCountsFrom,
  offloadWarning,
  requestLocalPromptQueueStop,
  syncKeepModelsLoaded,
  unloadModel,
  useChatRuntimeStore,
} from "@/features/chat";
import {
  fetchSttStatus,
  sttEngineStatusFor,
} from "@/features/chat/adapters/studio-model-dictation-adapter";
import { useStagedDownload } from "@/features/hub/download-manager";
import { getHfToken, hfApiToken } from "@/features/hub/stores/hf-token-store";
import type {
  ModelOption,
  ModelSelectorChangeMeta,
} from "@/features/model-picker/components/model-selector/types";
import { confirmRemoteCodeIfNeeded } from "@/features/security";
import { updateMultiModelMode } from "../../settings/api/multi-model";
import {
  evictedSlotsFrom,
  evictedToastOptions,
  resyncEvicted,
} from "@/lib/evicted-toast";
import { fetchSystemInfo } from "@/hooks/use-system";
import { toast } from "@/lib/toast";
import { audioSlotRoutingId, getAudioDownloadPlan } from "../api";
import {
  type AudioFailureContext,
  audioFailureActions,
  audioFailureOf,
} from "../audio-failure-actions";
import {
  audioCppRuntimeProblem,
  audioModelToReplace,
  canTransitionAudioMode,
  exactGgufLoadSelector,
  expectedGgufDownloadBytes,
  isGgufTtsTarget,
  isTtsAudioType,
  macTtsPickAction,
  resolveAudioPickTask,
  selectAutoGgufVariant,
  stagedTtsLoadIsOwned,
  trainedTtsCheckpointIsLoadable,
  trainedTtsCheckpointIsRunnableOnMac,
} from "../audio-page-policy";
import {
  TTS_MAX_TOKENS,
  TTS_PROMPT_CONTEXT_RESERVE,
} from "../audio-workspace-constants";
import {
  audioModelLabel,
  type CreateMode,
  loadedAudioKind,
  type RemoteCodeApproval,
} from "../audio-workspace-utils";
import {
  audioModelRequiresRemoteCode,
  audioTaskFor,
  ggufSiblingFor,
  isMusicGenerationModel,
  musicGenerationRequiresCuda,
  sttEngineForRepoId,
  sttSidecarKeyFor,
  usesNativeAudioRuntime,
} from "../catalog";
import { useAudioTranscribeStore } from "../stores/audio-transcribe-store";
import { useAudioWorkspaceStore } from "../stores/audio-workspace-store";
import {
  type AudioWorkflowId,
  slotForWorkflow,
  workflowForLoadedModel,
} from "../workflows";
import type { AudioHostState } from "./audio-host-state";
import type { SttSidecar } from "./use-stt-sidecar";

const STT_STATUS_POLL_MS = 30_000;

/** Offered after a speech load replaced the chat model under Off. */
async function keepBothLoaded(): Promise<void> {
  try {
    const mode = await updateMultiModelMode(
      "fit",
      "Failed to save model memory settings.",
    );
    syncKeepModelsLoaded(mode);
    toast.success("Keep multiple models loaded is now When they fit", {
      description:
        "Pick the chat model again and Unsloth keeps it beside the speech model.",
    });
  } catch (error) {
    toast.error(
      error instanceof Error
        ? error.message
        : "Failed to save model memory settings.",
    );
  }
}

/** What the page lends a failed load's toast action: it owns the selector, device and runtime update. */
export type AudioLoadFailureHandlers = Pick<
  AudioFailureContext,
  "canUpdateRuntime" | "onUpdateRuntime" | "onChooseModel"
> & { selectCpuDevice: () => void };

export function useAudioModelSlot({
  active,
  activeRef,
  busy,
  busyRef,
  setBusy,
  mode,
  setMode,
  modeRef,
  generationPhaseRef,
  handleStopGeneration,
  refreshStatus,
  audioDevice,
  isMac,
  status,
  releaseTranscribeSelection,
  ejectTranscribeModel,
  ensureSttLoaded,
  sttGgufVariants,
  sttEngines,
  sttEngineFor,
  refreshSttStatus,
  sttLoadedModel,
  selectedSttRepoRef,
  setSelectedSttRepo,
  deferredSttLoad,
  sttLoadingGeneration,
  sttLoadGeneration,
  sttLoadAbort,
  audioCppRuntime,
  selectedSttRepo,
  sttReady,
  sttStatusRefreshGeneration,
}: Pick<
  AudioHostState,
  | "active"
  | "activeRef"
  | "busy"
  | "busyRef"
  | "setBusy"
  | "mode"
  | "setMode"
  | "modeRef"
  | "generationPhaseRef"
  | "handleStopGeneration"
  | "refreshStatus"
  | "audioDevice"
  | "isMac"
  | "status"
> &
  Pick<
    SttSidecar,
    | "releaseTranscribeSelection"
    | "ejectTranscribeModel"
    | "ensureSttLoaded"
    | "sttGgufVariants"
    | "sttEngines"
    | "sttEngineFor"
    | "refreshSttStatus"
    | "sttLoadedModel"
    | "selectedSttRepoRef"
    | "setSelectedSttRepo"
    | "deferredSttLoad"
    | "sttLoadingGeneration"
    | "sttLoadGeneration"
    | "sttLoadAbort"
    | "audioCppRuntime"
    | "selectedSttRepo"
    | "sttReady"
    | "sttStatusRefreshGeneration"
  >) {
  const ttsLoadInFlight = useRef(false);
  const loadFailureHandlers = useRef<AudioLoadFailureHandlers | null>(null);
  const setLoadFailureHandlers = useCallback(
    (handlers: AudioLoadFailureHandlers) => {
      loadFailureHandlers.current = handlers;
    },
    [],
  );
  const pendingRoutedTtsPick = useRef<{
    repoId: string;
    ggufFilename?: string | null;
    loadId?: string | null;
    audioType?: string | null;
    remoteCodeApproval?: RemoteCodeApproval;
    isGguf?: boolean | null;
    deviceOverride?: "cpu";
  } | null>(null);
  const ttsLoadGeneration = useRef(0);
  const statusRef = useRef(status);
  useEffect(() => {
    statusRef.current = status;
  }, [status]);
  const pendingTtsLoad = useRef<{
    generation: number;
    repoId: string;
    loadTarget: string;
    loadRequestId: string;
    controller: AbortController;
    requestStarted: boolean;
  } | null>(null);

  const ttsPickGeneration = useRef(0);
  const ttsInspectionGeneration = useRef<number | null>(null);
  const stagedTtsGeneration = useRef(0);
  const pendingStagedTtsLoad = useRef<{
    repoId: string;
    ggufFilename: string | null;
    loadId?: string | null;
    audioType?: string | null;
    remoteCodeApproval?: RemoteCodeApproval;
    isGguf?: boolean | null;
    generation: number;
  } | null>(null);
  const stagedTtsLoadDeferred = useRef(false);
  const pickModelVariantRef = useRef<
    (id: string, quant?: string) => Promise<void>
  >(async () => {});
  // Repeats the last successful load, so a generation failure's "Load into CPU RAM" reloads the resident model.
  const lastTtsLoad = useRef<((device: "cpu") => Promise<void>) | null>(null);

  const loadTtsModel = useCallback(
    async (
      repoId: string,
      ggufFilename?: string | null,
      // Rows in a non-active HF cache load only by snapshot path (meta.loadId), as in chat-page.tsx.
      loadId?: string | null,
      audioType?: string | null,
      remoteCodeApproval?: RemoteCodeApproval,
      isGguf?: boolean | null,
      // "Load into CPU RAM" from a failed load: the device choice has not re-rendered yet.
      deviceOverride?: "cpu",
    ) => {
      // ?model= is already cleared, so a pick arriving mid-teardown is replayed from the finally below.
      if (ttsLoadInFlight.current || busyRef.current === "generating") {
        pendingRoutedTtsPick.current = {
          repoId,
          ggufFilename,
          loadId,
          audioType,
          remoteCodeApproval,
          isGguf,
          deviceOverride,
        };
        return;
      }
      // A load stops every chat on the shared llama-server: ask as Chat does, gate claimed before the await so picks queue.
      ttsLoadInFlight.current = true;
      const lifecycleLease = useChatRuntimeStore.getState().beginModelLoading();
      if (lifecycleLease === null) {
        ttsLoadInFlight.current = false;
        pendingRoutedTtsPick.current = null;
        toast.info("Wait for the current model to finish loading.");
        return;
      }
      const releaseLifecycle = () =>
        useChatRuntimeStore.getState().endModelLoading(lifecycleLease);
      // A load kept beside the chat model leaves its chats running, as Chat's own keep-others load does.
      const keepsChat = useChatRuntimeStore.getState().keepModelsLoaded;
      const stopDecision = keepsChat
        ? {
            proceed: true,
            forceCancelActive: false,
            promptQueueThreadIds: [],
            preStreamRunTokens: [],
          }
        : await confirmStopRunningChatsIfNeeded();
      if (!stopDecision.proceed) {
        releaseLifecycle();
        ttsLoadInFlight.current = false;
        // Declining refuses the swap, so a queued pick must not reopen the dialog.
        pendingRoutedTtsPick.current = null;
        return;
      }
      // Hidden while the dialog is open: nothing to abort yet, so queue for the activation replay.
      if (!activeRef.current) {
        releaseLifecycle();
        ttsLoadInFlight.current = false;
        pendingRoutedTtsPick.current = {
          repoId,
          ggufFilename,
          loadId,
          audioType,
          remoteCodeApproval,
          isGguf,
        };
        return;
      }
      const generation = ++ttsLoadGeneration.current;
      const retryLoad = (device: "cpu") =>
        loadTtsModelRef.current(
          repoId,
          ggufFilename,
          loadId,
          audioType,
          remoteCodeApproval,
          isGguf,
          device,
        );
      const controller = new AbortController();
      const loadRequestId = crypto.randomUUID();
      const pending = {
        generation,
        repoId,
        // Cancel must name what was sent: for a pinned dir the display id makes _cancel_scoped_load_attempt refuse.
        loadTarget: loadId || repoId,
        loadRequestId,
        controller,
        requestStarted: false,
      };
      pendingTtsLoad.current = pending;
      const isCurrent = () =>
        generation === ttsLoadGeneration.current && activeRef.current;
      ttsLoadInFlight.current = true;
      busyRef.current = "loading";
      setBusy("loading");
      const replacesChat =
        !useChatRuntimeStore.getState().keepModelsLoaded &&
        !statusRef.current?.is_audio &&
        (statusRef.current?.serving?.length ?? 0) > 0;
      const toastId = toast.loading(`Loading ${audioModelLabel(repoId)}…`, {
        description: replacesChat
          ? "Keep multiple models loaded is Off, so this unloads the chat model."
          : undefined,
      });
      try {
        const hfToken = hfApiToken(getHfToken()) ?? null;
        let trustRemoteCode = remoteCodeApproval?.trustRemoteCode ?? false;
        let approvedRemoteCodeFingerprint =
          remoteCodeApproval?.approvedRemoteCodeFingerprint ?? null;
        if (
          audioModelRequiresRemoteCode(repoId, audioType) &&
          !remoteCodeApproval
        ) {
          const approved = await confirmRemoteCodeIfNeeded({
            modelName: loadId || repoId,
            hfToken,
            requiresTrustRemoteCode: true,
            onApprove: (fingerprint) => {
              trustRemoteCode = true;
              approvedRemoteCodeFingerprint = fingerprint;
            },
          });
          if (!approved)
            throw new Error(
              "Custom code approval is required to load this model.",
            );
          if (controller.signal.aborted || !isCurrent()) return;
        }
        const wantsCpu = deviceOverride === "cpu" || audioDevice === "cpu";
        const isGgufLoad = isGgufTtsTarget({ repoId, ggufFilename, loadId, isGguf });
        // Unloaded just before /load goes out: as a third model, Always refuses a load that only
        // fits in its place, and a load cancelled before then (token prompt, abort) keeps it.
        const previous = keepsChat
          ? audioModelToReplace(statusRef.current, loadId || repoId)
          : null;
        // A refused unload (e.g. 409 while it reads aloud) stops the switch: loaded beside it, the
        // old model would keep answering Read aloud and the Audio page.
        const replacePrevious = previous
          ? () =>
              unloadModel({
                model_path: audioSlotRoutingId(previous) ?? previous,
              })
          : undefined;
        const res = await loadModel(
          {
            model_path: loadId || repoId,
            load_request_id: loadRequestId,
            force_cancel_active: stopDecision.forceCancelActive,
            hf_token: hfToken,
            max_seq_length: TTS_MAX_TOKENS + TTS_PROMPT_CONTEXT_RESERVE,
            load_in_4bit: false,
            is_lora: false,
            gguf_variant: ggufFilename ?? null,
            trust_remote_code: trustRemoteCode,
            approved_remote_code_fingerprint: approvedRemoteCodeFingerprint,
            // The server ignores this under Off.
            alongside: true,
            audio_device: wantsCpu ? "cpu" : "auto",
            // GGUF ignores audio_device; an absent speculative_type means auto, which may attach a GPU drafter.
            ...(wantsCpu && isGgufLoad
              ? // biome-ignore lint/style/useNamingConvention: API schema
                {
                  gpu_memory_mode: "manual" as const,
                  gpu_layers: 0,
                  speculative_type: "off" as const,
                }
              : {}),
          },
          {
            signal: controller.signal,
            runtime: "tts",
            beforeRequest: replacePrevious,
            onRequestStart: () => {
              pending.requestStarted = true;
              // Cancel queued prompts only once /load is really sent: loadModel can return without sending.
              if (!keepsChat) {
                cancelPreStreamRunReservations(stopDecision.preStreamRunTokens);
                requestLocalPromptQueueStop(stopDecision.promptQueueThreadIds);
              }
            },
          },
        );
        if (!isCurrent()) return;
        if (res.is_audio && isTtsAudioType(res.audio_type)) {
          const offloadNotice = offloadWarning(offloadCountsFrom(res));
          const showToast = offloadNotice ? toast.warning : toast.success;
          const evicted = evictedSlotsFrom(res);
          showToast(
            `Model loaded (${loadedAudioKind(res.audio_type)})${offloadNotice?.titleSuffix ?? ""}`,
            {
              id: toastId,
              description:
                offloadNotice?.description ??
                evictedToastOptions(evicted).description,
              duration: offloadNotice || replacesChat ? 8000 : undefined,
              // The mode is an installation setting: a managed account's save is refused.
              action:
                replacesChat && isAccountOwner()
                  ? { label: "Keep both loaded", onClick: () => void keepBothLoaded() }
                  : undefined,
            },
          );
          resyncEvicted(evicted, "audio");
          // Only on success: a failed attempt leaves the previous model resident.
          lastTtsLoad.current = retryLoad;
          const loadedWorkflow = workflowForLoadedModel({
            current: useAudioWorkspaceStore.getState().workflow,
            audioWorkflows: res.audio_workflows,
            music: isMusicGenerationModel(repoId, res.audio_type),
          });
          const workspace = useAudioWorkspaceStore.getState();
          workspace.rememberModel(loadedWorkflow, repoId);
          if (modeRef.current === "speak") workspace.commitWorkflow(loadedWorkflow);
          if (res.audio_device_fallback === "cpu" && !wantsCpu) {
            toast.warning(
              "No usable GPU, so the model loaded into CPU RAM. Generation will be slower.",
              { duration: 8000 },
            );
          }
          if (
            wantsCpu &&
            !isGgufLoad &&
            !usesNativeAudioRuntime(repoId, res.audio_type)
          ) {
            toast.info(
              "This model does not support CPU RAM yet, so it loaded on the GPU.",
              { duration: 6000 },
            );
          }
        } else {
          toast.error(`${repoId} loaded but is not a supported TTS model.`, {
            id: toastId,
          });
        }
      } catch (error) {
        const failure = audioFailureOf(error);
        const handlers = loadFailureHandlers.current;
        if (isCurrent() && failure && handlers) {
          const [action] = audioFailureActions(failure, {
            ...handlers,
            deviceIsCpu: deviceOverride === "cpu" || audioDevice === "cpu",
            cpuUnsupported: musicGenerationRequiresCuda(repoId, audioType),
            onUseVariant: (variant) =>
              void pickModelVariantRef.current(repoId, variant),
            onUseCpu: () => {
              handlers.selectCpuDevice();
              void retryLoad("cpu");
            },
          });
          toast.error((error as Error).message, {
            id: toastId,
            duration: 12000,
            action: action && { label: action.label, onClick: action.onClick },
          });
        } else if (isCurrent()) {
          toast.error(
            error instanceof Error ? error.message : "Model load failed.",
            { id: toastId },
          );
        } else {
          toast.dismiss(toastId);
        }
      } finally {
        if (pendingTtsLoad.current?.generation === generation)
          pendingTtsLoad.current = null;
        if (activeRef.current) await refreshStatus();
        ttsLoadInFlight.current = false;
        releaseLifecycle();
        busyRef.current = null;
        setBusy(null);
        // Only while visible: a hidden replay escapes the deactivation cancel and can replace Chat's model.
        if (activeRef.current) replayQueuedTtsPick();
      }
    },
    [refreshStatus, audioDevice],
  );

  const loadTtsModelRef = useRef(loadTtsModel);
  loadTtsModelRef.current = loadTtsModel;
  /** Repeat the last load into CPU RAM; false when there is none to repeat. */
  const reloadOnCpu = useCallback(() => {
    void lastTtsLoad.current?.("cpu");
    return lastTtsLoad.current !== null;
  }, []);
  /** Start a pick that lost the race with a load still settling. Visible pages only: a load started
   *  while hidden outlives the deactivation effect that would cancel it. */
  const replayQueuedTtsPick = useCallback(() => {
    const queued = pendingRoutedTtsPick.current;
    if (!queued) return;
    pendingRoutedTtsPick.current = null;
    void loadTtsModelRef.current(
      queued.repoId,
      queued.ggufFilename,
      queued.loadId,
      queued.audioType,
      queued.remoteCodeApproval,
      queued.isGguf,
      queued.deviceOverride,
    );
  }, []);
  const invalidatePendingStagedTts = useCallback(() => {
    stagedTtsGeneration.current += 1;
    pendingStagedTtsLoad.current = null;
    stagedTtsLoadDeferred.current = false;
  }, []);
  const invalidatePendingTtsSelection = useCallback(() => {
    ttsPickGeneration.current += 1;
    pendingRoutedTtsPick.current = null;
    invalidatePendingStagedTts();
  }, [invalidatePendingStagedTts]);
  const transitionMode = useCallback(
    (nextMode: CreateMode) => {
      if (nextMode === mode) {
        if (nextMode === "transcribe") invalidatePendingTtsSelection();
        return true;
      }
      if (
        !canTransitionAudioMode(busyRef.current, generationPhaseRef.current)
      ) {
        toast.info(
          "Wait for the active audio task to finish before switching modes.",
        );
        return false;
      }

      if (nextMode === "transcribe") invalidatePendingTtsSelection();
      if (busyRef.current === "generating") handleStopGeneration();
      setMode(nextMode);
      if (mode === "transcribe") {
        // Resolves to whether the sidecar is gone: a swallowed failed unload let the speech load OOM.
        const release = releaseTranscribeSelection().then(
          () => true,
          (error) => {
            toast.error(
              error instanceof Error
                ? error.message
                : "Failed to release the transcription model.",
            );
            return false;
          },
        );
        pendingTranscribeRelease.current = release;
        void release.then((released) => {
          if (pendingTranscribeRelease.current !== release) return;
          pendingTranscribeRelease.current = null;
          // Sidecar still resident: back to Transcribe so Eject can retry, unless something moved on.
          if (!released && modeRef.current === "speak") setMode("transcribe");
        });
      }
      return true;
    },
    [
      invalidatePendingTtsSelection,
      handleStopGeneration,
      mode,
      releaseTranscribeSelection,
    ],
  );
  const transitionWorkflow = useCallback(
    (next: AudioWorkflowId) => {
      const store = useAudioWorkspaceStore.getState();
      // Speak and Music share the slot, so transitionMode would wave a switch between them through.
      if (
        next !== store.workflow &&
        mode === "speak" &&
        slotForWorkflow(next) === "speak"
      ) {
        if (
          !canTransitionAudioMode(busyRef.current, generationPhaseRef.current)
        ) {
          toast.info(
            "Wait for the active audio task to finish before switching modes.",
          );
          return false;
        }
        if (busyRef.current === "generating") handleStopGeneration();
        invalidatePendingTtsSelection();
      } else if (!transitionMode(slotForWorkflow(next))) {
        return false;
      }
      store.commitWorkflow(next);
      return true;
    },
    [
      transitionMode,
      mode,
      busyRef,
      generationPhaseRef,
      handleStopGeneration,
      invalidatePendingTtsSelection,
    ],
  );
  const { stage: stageTtsDownload } = useStagedDownload({
    scopeId: "audio",
    onReady: () => {
      const pending = pendingStagedTtsLoad.current;
      if (
        !pending ||
        !stagedTtsLoadIsOwned(
          pending.generation,
          stagedTtsGeneration.current,
          modeRef.current,
        )
      ) {
        pendingStagedTtsLoad.current = null;
        stagedTtsLoadDeferred.current = false;
        return;
      }
      // Stays mounted: a hidden load would evict the visible page's model; also wait out generation.
      if (!active || busyRef.current !== null) {
        stagedTtsLoadDeferred.current = true;
        return;
      }
      pendingStagedTtsLoad.current = null;
      void loadTtsModelRef.current(
        pending.repoId,
        pending.ggufFilename,
        pending.loadId,
        pending.audioType,
        pending.remoteCodeApproval,
        pending.isGguf,
      );
    },
    onCancelled: invalidatePendingStagedTts,
    onReset: invalidatePendingStagedTts,
  });

  useEffect(() => {
    if (!active || busy !== null || !stagedTtsLoadDeferred.current) return;
    stagedTtsLoadDeferred.current = false;
    const pending = pendingStagedTtsLoad.current;
    if (
      !pending ||
      !stagedTtsLoadIsOwned(
        pending.generation,
        stagedTtsGeneration.current,
        modeRef.current,
      )
    ) {
      pendingStagedTtsLoad.current = null;
      return;
    }
    pendingStagedTtsLoad.current = null;
    void loadTtsModelRef.current(
      pending.repoId,
      pending.ggufFilename,
      pending.loadId,
      pending.audioType,
      pending.remoteCodeApproval,
      pending.isGguf,
    );
  }, [active, busy]);

  const loadOrStageTtsModel = useCallback(
    async (
      repoId: string,
      ggufFilename: string | null,
      meta: ModelSelectorChangeMeta,
    ) => {
      const generation = ++stagedTtsGeneration.current;
      pendingStagedTtsLoad.current = null;
      stagedTtsLoadDeferred.current = false;
      stageTtsDownload([]);

      let remoteCodeApproval: RemoteCodeApproval | undefined;
      const hfToken = hfApiToken(getHfToken()) ?? null;
      if (
        meta.source === "hub" &&
        !ggufFilename &&
        audioModelRequiresRemoteCode(repoId, meta.audioType)
      ) {
        try {
          const approved = await confirmRemoteCodeIfNeeded({
            modelName: meta.loadId || repoId,
            hfToken,
            requiresTrustRemoteCode: true,
            onApprove: (fingerprint) => {
              remoteCodeApproval = {
                trustRemoteCode: true,
                approvedRemoteCodeFingerprint: fingerprint,
              };
            },
          });
          if (generation !== stagedTtsGeneration.current) return;
          if (!approved) {
            toast.error("Custom code approval is required to load this model.");
            return;
          }
        } catch (error) {
          if (generation !== stagedTtsGeneration.current) return;
          toast.error(
            error instanceof Error
              ? error.message
              : `Could not verify the code for ${repoId}.`,
          );
          return;
        }
      }

      // Native models can need a second codec repo, so the backend plan owns every missing file.
      if (meta.source === "hub" && !ggufFilename) {
        let plan;
        try {
          plan = await getAudioDownloadPlan(
            meta.loadId || repoId,
            hfToken ?? undefined,
          );
        } catch (error) {
          if (generation !== stagedTtsGeneration.current) return;
          if (meta.isDownloaded === true) {
            void loadTtsModelRef.current(
              repoId,
              ggufFilename,
              meta.loadId,
              meta.audioType,
              remoteCodeApproval,
              meta.isGguf,
            );
            return;
          }
          toast.error(
            error instanceof Error
              ? error.message
              : `Could not prepare the download for ${repoId}.`,
          );
          return;
        }
        if (generation !== stagedTtsGeneration.current) return;
        const plannedEntries = plan.entries;
        if (plannedEntries.length > 0) {
          pendingStagedTtsLoad.current = {
            repoId,
            ggufFilename,
            loadId: meta.loadId,
            audioType: meta.audioType,
            remoteCodeApproval,
            isGguf: meta.isGguf,
            generation,
          };
          stageTtsDownload(
            plannedEntries.map((entry) => ({
              repoId: entry.repo_id,
              files: entry.files,
              bytes: entry.bytes,
              ggufFilename: entry.gguf_filename,
              checkpoint: entry.checkpoint,
            })),
          );
          return;
        }
      }

      if (
        meta.source === "hub" &&
        meta.isDownloaded === false &&
        ggufFilename
      ) {
        pendingStagedTtsLoad.current = {
          repoId,
          ggufFilename,
          loadId: meta.loadId,
          audioType: meta.audioType,
          remoteCodeApproval,
          isGguf: meta.isGguf,
          generation,
        };
        stageTtsDownload([
          meta.ggufVariant
            ? {
                repoId,
                files: [],
                bytes: meta.expectedBytes ?? 0,
                ggufFilename,
                ggufVariant: meta.ggufVariant,
              }
            : {
                repoId,
                files: [ggufFilename],
                bytes: meta.expectedBytes ?? 0,
                ggufFilename,
              },
        ]);
        return;
      }

      void loadTtsModelRef.current(
        repoId,
        ggufFilename,
        meta.loadId,
        meta.audioType,
        remoteCodeApproval,
        meta.isGguf,
      );
    },
    [stageTtsDownload],
  );

  // A hidden page may let the shared download continue, but it must not load the sidecar. Returning
  // to the same still-selected repo resumes preparation once; a different selection clears it.
  useEffect(() => {
    if (!active) {
      ttsPickGeneration.current += 1;
      const pending = pendingTtsLoad.current;
      if (pending) {
        ttsLoadGeneration.current += 1;
        pendingTtsLoad.current = null;
        pending.controller.abort();
        if (pending.requestStarted)
          void unloadModel({
            model_path: pending.loadTarget,
            cancel_load_request_id: pending.loadRequestId,
          }).catch(() => {});
      }
      if (ttsInspectionGeneration.current !== null) {
        ttsInspectionGeneration.current = null;
        busyRef.current = null;
        setBusy(null);
      }

      if (sttLoadingGeneration.current !== null) {
        const repoId = selectedSttRepoRef.current;
        deferredSttLoad.current = repoId
          ? {
              repoId,
              sidecarKey: sttSidecarKeyFor(repoId),
              engine: sttEngineFor(repoId),
            }
          : null;
        sttLoadGeneration.current += 1;
        sttLoadingGeneration.current = null;
        sttLoadAbort.current?.abort();
        sttLoadAbort.current = null;
        busyRef.current = null;
        setBusy(null);
      }
      return;
    }

    // Replay a pick held back while the page was away; now visible, the attempt is cancellable.
    replayQueuedTtsPick();

    const deferred = deferredSttLoad.current;
    deferredSttLoad.current = null;
    if (deferred && selectedSttRepoRef.current === deferred.repoId) {
      void (async () => {
        try {
          const status = await fetchSttStatus(
            undefined,
            deferred.engine === "transformers"
              ? deferred.sidecarKey
              : undefined,
          );
          const download = sttEngineStatusFor(
            status,
            deferred.sidecarKey,
            deferred.engine,
          )?.download;
          // `model` is null once the download stopped, so a cancel made while hidden matched nothing.
          if (
            download?.cancelled &&
            (download.model ?? download.cancelled_model) === deferred.sidecarKey
          )
            return;
        } catch {
        }
        if (activeRef.current && selectedSttRepoRef.current === deferred.repoId)
          void ensureSttLoaded(
            deferred.repoId,
            deferred.sidecarKey,
            deferred.engine,
          );
      })();
    }
  }, [active, ensureSttLoaded]);

  const pendingTranscribeRelease = useRef<Promise<boolean> | null>(null);

  const handleModelSelect = useCallback(
    async (id: string, meta: ModelSelectorChangeMeta) => {
      if (busyRef.current !== null) return;
      // Uncurated picks fall back to the pipeline tag, else community ASR repos load into the TTS slot.
      const task = resolveAudioPickTask(audioTaskFor(id), meta.pipelineTag);
      const runtimeProblem =
        task === "stt" ? null : audioCppRuntimeProblem(id, audioCppRuntime.current);
      if (runtimeProblem) {
        toast.error(runtimeProblem, { duration: 7000 });
        return;
      }
      const cudaMusicPick =
        task !== "stt" && musicGenerationRequiresCuda(id, meta.audioType);
      const selectionGeneration = ++ttsPickGeneration.current;
      if (cudaMusicPick) {
        const system = await fetchSystemInfo();
        if (selectionGeneration !== ttsPickGeneration.current) return;
        if (system?.device_backend !== "cuda") {
          toast.error(
            `${id} requires a verified NVIDIA CUDA GPU for local generation.`,
            { duration: 7000 },
          );
          return;
        }
      }
      deferredSttLoad.current = null;
      if (task === "stt") {
        if (!transitionMode("transcribe")) return;
        const sidecarKey = sttSidecarKeyFor(id);
        const engine = sttEngineForRepoId(id, meta.isGguf);
        sttEngines.current.set(id.toLowerCase(), engine);
        if (meta.ggufVariant) {
          sttGgufVariants.current.set(id.toLowerCase(), meta.ggufVariant);
        } else {
          sttGgufVariants.current.delete(id.toLowerCase());
        }
        deferredSttLoad.current = null;
        useAudioTranscribeStore.setState({ connectionModel: null });
        selectedSttRepoRef.current = id;
        setSelectedSttRepo(id);
        void ensureSttLoaded(id, sidecarKey, engine);
        return;
      }
      if (!transitionMode("speak")) return;
      const releaseInFlight = pendingTranscribeRelease.current;
      // A failed release leaves the sidecar resident; claimed before the await since the button only disables on `busy`.
      if (releaseInFlight && !(await releaseInFlight)) {
        setMode("transcribe");
        return;
      }
      if (ttsPickGeneration.current !== selectionGeneration) return;
      const exactGguf = exactGgufLoadSelector(meta);
      const isGguf = isGgufTtsTarget({
        repoId: id,
        ggufFilename: exactGguf,
        isGguf: meta.isGguf,
      });
      const ggufSibling = isGguf ? null : ggufSiblingFor(id);
      const nativeRuntime =
        usesNativeAudioRuntime(id, meta.audioType) && !cudaMusicPick;
      const macAction = macTtsPickAction({
        isMac,
        isGguf,
        ggufSibling,
        nativeRuntime,
      });
      if (macAction === "reject") {
        toast.error(
          cudaMusicPick
            ? `${id} currently requires an NVIDIA CUDA GPU and cannot run locally on this Mac.`
            : `${id} has no runnable GGUF TTS build. MLX cannot generate text-to-speech from its safetensors checkpoint on this Mac.`,
          { duration: 7000 },
        );
        return;
      }
      if (macAction === "use-gguf-sibling" && ggufSibling) {
        toast.info(
          `Loading the GGUF build of ${id}. MLX has no text-to-speech decoder, so the safetensors build cannot generate on this Mac.`,
          { duration: 7000 },
        );
        ttsInspectionGeneration.current = selectionGeneration;
        busyRef.current = "loading";
        setBusy("loading");
        try {
          const listing = await listGgufVariants(
            ggufSibling,
            hfApiToken(getHfToken()),
          );
          if (selectionGeneration !== ttsPickGeneration.current) return;
          const variant = selectAutoGgufVariant(
            listing.variants,
            listing.default_variant,
          );
          if (!variant) {
            toast.error(
              `${ggufSibling} does not publish a runnable GGUF file.`,
            );
            return;
          }
          if (ttsInspectionGeneration.current === selectionGeneration) {
            ttsInspectionGeneration.current = null;
            busyRef.current = null;
            setBusy(null);
          }
          await loadOrStageTtsModel(ggufSibling, variant.filename, {
            ...meta,
            source: "hub",
            isGguf: true,
            ggufFilename: variant.filename,
            ggufVariant: variant.quant,
            isDownloaded: variant.downloaded === true && !variant.partial,
            expectedBytes: expectedGgufDownloadBytes(variant),
          });
        } catch (error) {
          if (selectionGeneration !== ttsPickGeneration.current) return;
          toast.error(
            error instanceof Error
              ? error.message
              : `Could not inspect ${ggufSibling}.`,
          );
        } finally {
          if (ttsInspectionGeneration.current === selectionGeneration) {
            ttsInspectionGeneration.current = null;
            busyRef.current = null;
            setBusy(null);
          }
        }
        return;
      }
      await loadOrStageTtsModel(id, exactGguf, meta);
    },
    [
      ensureSttLoaded,
      isMac,
      loadOrStageTtsModel,
      transitionMode,
    ],
  );

  // The sidecar unloads itself after its idle keep-alive and emits nothing, so re-read status
  // while a model shows as loaded here.
  useEffect(() => {
    if (!active || mode !== "transcribe" || busy !== null || !sttLoadedModel)
      return;
    const refresh = () => {
      if (!document.hidden) void refreshSttStatus();
    };
    const interval = window.setInterval(refresh, STT_STATUS_POLL_MS);
    window.addEventListener("focus", refresh);
    document.addEventListener("visibilitychange", refresh);
    return () => {
      window.clearInterval(interval);
      window.removeEventListener("focus", refresh);
      document.removeEventListener("visibilitychange", refresh);
    };
  }, [active, busy, mode, refreshSttStatus, sttLoadedModel]);

  const handleEject = useCallback(() => {
    if (busy !== null) {
      toast.info("Stop the active audio task before ejecting its model.");
      return;
    }

    if (mode === "transcribe") {
      if (!selectedSttRepo) return;
      if (!sttReady) {
        sttStatusRefreshGeneration.current += 1;
        void releaseTranscribeSelection();
        return;
      }

      setBusy("unloading");
      const toastId = toast.loading("Unloading transcription model…");
      void (async () => {
        try {
          const outcome = await ejectTranscribeModel();
          if (outcome === "unloaded") {
            toast.success("Transcription model unloaded", {
              id: toastId,
              duration: 1200,
            });
          } else if (outcome === "kept") {
            toast.info(
              "A different transcription model is loaded, so nothing was unloaded.",
              { id: toastId },
            );
          } else {
            toast.dismiss(toastId);
          }
        } catch (error) {
          toast.error(
            error instanceof Error
              ? error.message
              : "Failed to unload transcription model.",
            { id: toastId },
          );
        } finally {
          setBusy(null);
        }
      })();
      return;
    }

    const activeModel = status?.active_model;
    if (!activeModel) return;

    const lifecycleLease = useChatRuntimeStore.getState().beginModelLoading();
    if (lifecycleLease === null) {
      toast.info("Wait for the current model to finish loading.");
      return;
    }

    setBusy("unloading");
    void (async () => {
      try {
        // A speech model beside the chat model unloads alone, so the chats keep running.
        const slotModel = audioSlotRoutingId(activeModel);
        const slotEject =
          slotModel !== null || (statusRef.current?.serving?.length ?? 0) > 1;
        const stopDecision = slotEject
          ? {
              proceed: true,
              forceCancelActive: false,
              promptQueueThreadIds: [],
              preStreamRunTokens: [],
            }
          : await confirmStopRunningChatsIfNeeded("Unloading the model", "unload");
        if (!stopDecision.proceed) {
          setBusy(null);
          return;
        }

        // An old managed completion must not replace the model the user just ejected.
        invalidatePendingStagedTts();
        stageTtsDownload([]);

        const toastId = toast.loading("Unloading model…");
        try {
          if (!slotEject) {
            cancelPreStreamRunReservations(stopDecision.preStreamRunTokens);
            requestLocalPromptQueueStop(stopDecision.promptQueueThreadIds);
          }
          await unloadModel({
            model_path: slotModel ?? activeModel,
            force_cancel_active: stopDecision.forceCancelActive,
          });
          if (!slotEject) requestLocalPromptQueueStop();
          await refreshStatus();
          toast.success("Model unloaded", { id: toastId, duration: 1200 });
        } catch (error) {
          toast.error(
            error instanceof Error ? error.message : "Failed to unload model.",
            { id: toastId },
          );
        } finally {
          setBusy(null);
        }
      } finally {
        useChatRuntimeStore.getState().endModelLoading(lifecycleLease);
      }
    })();
  }, [
    busy,
    ejectTranscribeModel,
    mode,
    refreshStatus,
    releaseTranscribeSelection,
    selectedSttRepo,
    invalidatePendingStagedTts,
    stageTtsDownload,
    status?.active_model,
    sttReady,
  ]);

  // Scan rows carry no modality until the backend tags them; without this trained checkpoints are unreachable.
  const [trainedTtsModels, setTrainedTtsModels] = useState<ModelOption[]>([]);
  useEffect(() => {
    if (!active) return;
    let cancelled = false;
    listLoras()
      .then((res) => {
        if (cancelled) return;
        setTrainedTtsModels(
          res.loras
            .filter(
              (lora) =>
                !isMac ||
                trainedTtsCheckpointIsRunnableOnMac(
                  lora.audio_type,
                  lora.export_type,
                ),
            )
            // GGUF_TTS_AUDIO_TYPES omits csm: llama.cpp has no CSM decoder.
            .filter((lora) =>
              isTtsAudioType(lora.audio_type, lora.export_type === "gguf"),
            )
            .filter((lora) =>
              trainedTtsCheckpointIsLoadable(lora.audio_type, lora.export_type),
            )
            .map((lora) => ({
              id: lora.adapter_path,
              name: audioModelLabel(lora.adapter_path),
              description:
                lora.export_type === "merged"
                  ? `Fine-tuned - ${lora.base_model || "unknown base"}`
                  : `LoRA - ${lora.base_model || "unknown base"}`,
              audioType: lora.audio_type ?? null,
            })),
        );
      })
      .catch(() => {
        if (!cancelled) setTrainedTtsModels([]);
      });
    return () => {
      cancelled = true;
    };
  }, [active, isMac]);

  /** Pick one quant of a repo (a failed load's smaller variant), or the auto choice without one. */
  const pickModelVariant = useCallback(
    async (id: string, quant?: string) => {
      if (busyRef.current !== null) return;
      try {
        const listing = await listGgufVariants(id, hfApiToken(getHfToken()));
        const wanted = quant?.toLowerCase();
        const variant = wanted
          ? listing.variants.find((v) => v.quant.toLowerCase() === wanted)
          : selectAutoGgufVariant(listing.variants, listing.default_variant);
        if (!variant) {
          toast.error(`${id} does not publish a ${quant ? `${quant} file` : "runnable GGUF file"}.`);
          return;
        }
        await handleModelSelect(id, {
          source: "hub",
          isLora: false,
          isGguf: true,
          ggufFilename: variant.filename,
          ggufVariant: variant.quant,
          isDownloaded: variant.downloaded === true && !variant.partial,
          expectedBytes: expectedGgufDownloadBytes(variant),
        });
      } catch (error) {
        toast.error(
          error instanceof Error ? error.message : `Could not inspect ${id}.`,
        );
      }
    },
    [busyRef, handleModelSelect],
  );
  useEffect(() => {
    pickModelVariantRef.current = pickModelVariant;
  }, [pickModelVariant]);
  const pickRecommendedModel = useCallback(
    (id: string) => pickModelVariant(id),
    [pickModelVariant],
  );

  return {
    replayQueuedTtsPick,
    transitionMode,
    transitionWorkflow,
    pendingTranscribeRelease,
    handleModelSelect,
    pickRecommendedModel,
    pickModelVariant,
    reloadOnCpu,
    setLoadFailureHandlers,
    handleEject,
    invalidatePendingTtsSelection,
    trainedTtsModels,
  };
}

export type AudioModelSlot = ReturnType<typeof useAudioModelSlot>;