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.

14.9 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 { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import { useT } from "@/i18n";
import { type KeyboardEvent, useEffect, useState } from "react";
import {
  type OpenAIAutoSwitchSettings,
  type OpenAIAutoSwitchUpdate,
  loadOpenAIAutoSwitchSettings,
  updateOpenAIAutoSwitchSettings,
} from "../api/openai-auto-switch";
import { SettingsRow } from "./settings-row";
import { SettingsSection } from "./settings-section";

// Mirrors MIN_AUTO_UNLOAD_IDLE_SECONDS in the backend settings store.
export const MIN_IDLE_SECONDS = 60;

export const parseIdleSeconds = (draft: string): number | null => {
  if (!draft.trim()) {
    return null;
  }
  const parsed = Number(draft);
  if (!Number.isInteger(parsed)) {
    return null;
  }
  return parsed === 0 || parsed >= MIN_IDLE_SECONDS ? parsed : null;
};

// Chromium still delivers keydown to a focused input once it turns disabled.
const saveOnEnter =
  (save: () => void, disabled: boolean) =>
  (event: KeyboardEvent<HTMLInputElement>) => {
    if (event.key === "Enter" && !event.nativeEvent.isComposing && !disabled) {
      save();
    }
  };

export function MediaIdleUnloadRow({
  draftSeconds,
  onDraftChange,
  onSave,
  settings,
  isSaving,
  error,
}: {
  draftSeconds: string;
  onDraftChange: (value: string) => void;
  onSave: () => void;
  settings: OpenAIAutoSwitchSettings | null;
  isSaving: boolean;
  error: string | null;
}) {
  const t = useT();
  const disabled = !settings || isSaving;
  const paused =
    settings !== null &&
    settings.mediaAutoUnloadIdleSeconds > 0 &&
    !settings.mediaIdleUnloadActive;
  return (
    <SettingsRow
      label={t("settings.general.modelAutoSwitch.mediaIdleUnload")}
      description={t("settings.resources.modelMemory.mediaIdleUnloadDescription")}
      below={
        error ? (
          <span className="max-w-[calc(260px*var(--ui-space-scale,1))] text-right text-xs text-destructive">
            {error}
          </span>
        ) : paused ? (
          <span className="max-w-[calc(260px*var(--ui-space-scale,1))] text-right text-xs text-muted-foreground">
            {t("settings.general.modelAutoSwitch.mediaIdlePaused")}
          </span>
        ) : null
      }
    >
      <div className="flex items-center gap-2">
        <div className="flex items-center gap-1.5">
          <Input
            type="number"
            min={0}
            step={1}
            value={draftSeconds}
            aria-label={t(
              "settings.general.modelAutoSwitch.mediaIdleSecondsAriaLabel",
            )}
            disabled={disabled}
            onChange={(event) => onDraftChange(event.target.value)}
            onKeyDown={saveOnEnter(onSave, disabled)}
            className="h-control w-24"
          />
          <span className="text-xs font-medium text-muted-foreground">s</span>
        </div>
        <Button
          variant="outline"
          size="sm"
          disabled={disabled}
          onClick={onSave}
        >
          {isSaving ? t("common.saving") : t("common.save")}
        </Button>
      </div>
    </SettingsRow>
  );
}

export function MediaIdleUnloadSetting({
  residencyKey,
}: { residencyKey?: unknown }) {
  const t = useT();
  const [settings, setSettings] = useState<OpenAIAutoSwitchSettings | null>(
    null,
  );
  const [draft, setDraft] = useState("0");
  const [error, setError] = useState<string | null>(null);
  const [isSaving, setIsSaving] = useState(false);

  // biome-ignore lint/correctness/useExhaustiveDependencies: residencyKey only triggers the re-read
  useEffect(() => {
    let cancelled = false;
    void loadOpenAIAutoSwitchSettings()
      .then((loaded) => {
        if (cancelled) return;
        setSettings(loaded);
        setDraft(String(loaded.mediaAutoUnloadIdleSeconds));
        setError(null);
      })
      .catch((loadError) => {
        if (cancelled) return;
        setError(
          loadError instanceof Error
            ? loadError.message
            : t("settings.general.modelAutoSwitch.loadError"),
        );
      });
    return () => {
      cancelled = true;
    };
  }, [t, residencyKey]);

  const handleSave = async () => {
    const mediaAutoUnloadIdleSeconds = parseIdleSeconds(draft);
    if (mediaAutoUnloadIdleSeconds === null) {
      setError(t("settings.general.modelAutoSwitch.idleError"));
      return;
    }
    setIsSaving(true);
    setError(null);
    try {
      const saved = await updateOpenAIAutoSwitchSettings({
        mediaAutoUnloadIdleSeconds,
      });
      setSettings(saved);
      setDraft(String(saved.mediaAutoUnloadIdleSeconds));
    } catch (saveError) {
      setError(
        saveError instanceof Error
          ? saveError.message
          : t("settings.general.modelAutoSwitch.saveError"),
      );
    } finally {
      setIsSaving(false);
    }
  };

  return (
    <MediaIdleUnloadRow
      draftSeconds={draft}
      onDraftChange={setDraft}
      onSave={() => void handleSave()}
      settings={settings}
      isSaving={isSaving}
      error={error}
    />
  );
}

// Also shown in the Images / Video API section; each copy reloads when the other saves.
const AUTO_SWITCH_UPDATED_EVENT = "unsloth-auto-switch-updated";

export function MediaAutoSwitchRow() {
  const t = useT();
  const [settings, setSettings] = useState<OpenAIAutoSwitchSettings | null>(
    null,
  );
  const [isSaving, setIsSaving] = useState(false);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    let cancelled = false;
    const load = () =>
      void loadOpenAIAutoSwitchSettings()
        .then((loaded) => {
          if (!cancelled) setSettings(loaded);
        })
        .catch(() => undefined);
    load();
    window.addEventListener(AUTO_SWITCH_UPDATED_EVENT, load);
    return () => {
      cancelled = true;
      window.removeEventListener(AUTO_SWITCH_UPDATED_EVENT, load);
    };
  }, []);

  const toggle = async (mediaAutoSwitchModel: boolean) => {
    if (!settings) return;
    setIsSaving(true);
    setError(null);
    try {
      setSettings(
        await updateOpenAIAutoSwitchSettings({
          enabled: settings.enabled,
          mediaAutoSwitchModel,
        }),
      );
      window.dispatchEvent(new Event(AUTO_SWITCH_UPDATED_EVENT));
    } catch (saveError) {
      setError(
        saveError instanceof Error
          ? saveError.message
          : t("settings.general.modelAutoSwitch.saveError"),
      );
    } finally {
      setIsSaving(false);
    }
  };

  return (
    <SettingsRow
      label={t("settings.general.modelAutoSwitch.mediaEnable")}
      description={t("settings.general.modelAutoSwitch.mediaEnableDescription")}
      below={
        error ? (
          <span className="text-right text-xs text-destructive">{error}</span>
        ) : null
      }
    >
      <Switch
        checked={settings?.mediaAutoSwitchModel ?? false}
        disabled={!settings || isSaving}
        onCheckedChange={(on) => void toggle(on)}
        aria-label={t("settings.general.modelAutoSwitch.mediaEnable")}
      />
    </SettingsRow>
  );
}

export function ModelAutoSwitchSection() {
  const t = useT();
  const [settings, setSettings] = useState<OpenAIAutoSwitchSettings | null>(
    null,
  );
  const [draftIdleSeconds, setDraftIdleSeconds] = useState("0");
  const [error, setError] = useState<string | null>(null);
  const [isSaving, setIsSaving] = useState(false);

  useEffect(() => {
    let cancelled = false;
    void loadOpenAIAutoSwitchSettings()
      .then((loaded) => {
        if (cancelled) return;
        setSettings(loaded);
        setDraftIdleSeconds(String(loaded.autoUnloadIdleSeconds));
        setError(null);
      })
      .catch((loadError) => {
        if (cancelled) return;
        setError(
          loadError instanceof Error
            ? loadError.message
            : t("settings.general.modelAutoSwitch.loadError"),
        );
      });
    const reload = () =>
      void loadOpenAIAutoSwitchSettings()
        .then((loaded) => {
          if (!cancelled) setSettings(loaded);
        })
        .catch(() => undefined);
    window.addEventListener(AUTO_SWITCH_UPDATED_EVENT, reload);
    return () => {
      cancelled = true;
      window.removeEventListener(AUTO_SWITCH_UPDATED_EVENT, reload);
    };
  }, [t]);

  // Other rows must preserve the unsaved idle-seconds draft.
  const persist = async (
    update: OpenAIAutoSwitchUpdate,
    { syncDraft = false }: { syncDraft?: boolean } = {},
  ) => {
    setIsSaving(true);
    setError(null);
    try {
      const saved = await updateOpenAIAutoSwitchSettings(update);
      setSettings(saved);
      window.dispatchEvent(new Event(AUTO_SWITCH_UPDATED_EVENT));
      if (syncDraft) {
        setDraftIdleSeconds(String(saved.autoUnloadIdleSeconds));
      }
    } catch (saveError) {
      setError(
        saveError instanceof Error
          ? saveError.message
          : t("settings.general.modelAutoSwitch.saveError"),
      );
    } finally {
      setIsSaving(false);
    }
  };

  const handleToggle = (enabled: boolean) => {
    const savedIdleSeconds = settings?.autoUnloadIdleSeconds ?? 0;
    if (!enabled) {
      void persist({ enabled: false, autoUnloadIdleSeconds: savedIdleSeconds });
      return;
    }
    void persist(
      {
        enabled: true,
        autoUnloadIdleSeconds:
          parseIdleSeconds(draftIdleSeconds) ?? savedIdleSeconds,
      },
      { syncDraft: true },
    );
  };

  const handleSaveIdle = () => {
    const idleSeconds = parseIdleSeconds(draftIdleSeconds);
    if (idleSeconds === null) {
      setError(t("settings.general.modelAutoSwitch.idleError"));
      return;
    }
    void persist(
      { enabled: true, autoUnloadIdleSeconds: idleSeconds },
      { syncDraft: true },
    );
  };

  const handleKeepKvToggle = (keepKv: boolean) => {
    if (!settings) return;
    void persist({ enabled: settings.enabled, autoUnloadKeepKv: keepKv });
  };

  const handleAutoDownloadToggle = (autoDownload: boolean) => {
    if (!settings) return;
    void persist({
      enabled: settings.enabled,
      autoDownloadModel: autoDownload,
    });
  };

  const handleMediaAutoSwitchToggle = (mediaAutoSwitch: boolean) => {
    if (!settings) return;
    void persist({
      enabled: settings.enabled,
      mediaAutoSwitchModel: mediaAutoSwitch,
    });
  };

  const handleApiOnlyToggle = (apiOnly: boolean) => {
    if (!settings) return;
    void persist({ enabled: settings.enabled, autoUnloadApiOnly: apiOnly });
  };

  const idleDisabled = !settings?.enabled || isSaving;

  return (
    <SettingsSection title={t("settings.general.modelAutoSwitch.sectionTitle")}>
      <SettingsRow
        label={t("settings.general.modelAutoSwitch.enable")}
        description={t("settings.general.modelAutoSwitch.enableDescription")}
      >
        <Switch
          checked={settings?.enabled ?? false}
          disabled={!settings || isSaving}
          onCheckedChange={handleToggle}
        />
      </SettingsRow>
      <SettingsRow
        label={t("settings.general.modelAutoSwitch.autoDownload")}
        description={t(
          "settings.general.modelAutoSwitch.autoDownloadDescription",
        )}
      >
        <Switch
          checked={settings?.autoDownloadModel ?? false}
          disabled={!settings?.enabled || isSaving}
          onCheckedChange={handleAutoDownloadToggle}
        />
      </SettingsRow>
      <SettingsRow
        label={t("settings.general.modelAutoSwitch.idleUnload")}
        description={t(
          "settings.general.modelAutoSwitch.idleUnloadDescription",
        )}
        below={
          error ? (
            <span className="max-w-[calc(260px*var(--ui-space-scale,1))] text-right text-xs text-destructive">
              {error}
            </span>
          ) : settings && !settings.enabled && settings.idleUnloadActive ? (
            <span className="max-w-[calc(260px*var(--ui-space-scale,1))] text-right text-xs text-muted-foreground">
              {t("settings.general.modelAutoSwitch.idleActiveViaEnv")}
            </span>
          ) : settings && !settings.enabled ? (
            <span className="max-w-[calc(260px*var(--ui-space-scale,1))] text-right text-xs text-muted-foreground">
              {t("settings.general.modelAutoSwitch.idleNeedsEnable")}
            </span>
          ) : settings &&
            !isSaving &&
            parseIdleSeconds(draftIdleSeconds) !==
              settings.autoUnloadIdleSeconds ? (
            <span className="max-w-[calc(260px*var(--ui-space-scale,1))] text-right text-xs text-muted-foreground">
              {t("settings.general.modelAutoSwitch.idleUnsaved")}
            </span>
          ) : null
        }
      >
        <div className="flex items-center gap-2">
          <div className="flex items-center gap-1.5">
            <Input
              type="number"
              min={0}
              step={1}
              value={draftIdleSeconds}
              aria-label={t(
                "settings.general.modelAutoSwitch.idleSecondsAriaLabel",
              )}
              disabled={idleDisabled}
              onChange={(event) => setDraftIdleSeconds(event.target.value)}
              onKeyDown={saveOnEnter(handleSaveIdle, idleDisabled)}
              className="h-control w-24"
            />
            <span className="text-xs font-medium text-muted-foreground">s</span>
          </div>
          <Button
            variant="outline"
            size="sm"
            disabled={idleDisabled}
            onClick={handleSaveIdle}
          >
            {isSaving ? t("common.saving") : t("common.save")}
          </Button>
        </div>
      </SettingsRow>
      <SettingsRow
        label={t("settings.general.modelAutoSwitch.mediaEnable")}
        description={t(
          "settings.general.modelAutoSwitch.mediaEnableDescription",
        )}
      >
        <Switch
          checked={settings?.mediaAutoSwitchModel ?? false}
          disabled={!settings || isSaving}
          onCheckedChange={handleMediaAutoSwitchToggle}
        />
      </SettingsRow>
      <SettingsRow
        label={t("settings.general.modelAutoSwitch.keepKv")}
        description={t("settings.general.modelAutoSwitch.keepKvDescription")}
      >
        <Switch
          aria-label={t("settings.general.modelAutoSwitch.keepKv")}
          checked={settings?.autoUnloadKeepKv ?? true}
          disabled={!settings || isSaving}
          onCheckedChange={handleKeepKvToggle}
        />
      </SettingsRow>
      {settings &&
      (settings.idleUnloadActive || settings.mediaAutoUnloadIdleSeconds > 0) ? (
        <SettingsRow
          label={t("settings.general.modelAutoSwitch.apiOnly")}
          description={t("settings.general.modelAutoSwitch.apiOnlyDescription")}
        >
          <Switch
            checked={settings.autoUnloadApiOnly}
            disabled={isSaving}
            onCheckedChange={handleApiOnlyToggle}
          />
        </SettingsRow>
      ) : null}
    </SettingsSection>
  );
}