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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
// 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 { accountTransitionPending } from "@/lib/account-transition";
import { apiUrl, getApiPort, isTauri } from "@/lib/api-base";
import {
clearAuthTokens,
ensureAuthSessionMark,
getAuthToken,
getAuthSessionMark,
getRefreshToken,
mustChangePassword,
setMustChangePassword,
storeAuthTokens,
} from "./session";
type RefreshResponse = {
access_token: string;
refresh_token: string;
must_change_password: boolean;
};
type AuthFetchOptions = {
retryNetworkErrors?: boolean;
/** Synchronous policy check run immediately before any retry sends bytes. */
beforeRetry?: () => void;
};
let isRedirecting = false;
let refreshInflight: Promise<RefreshOutcome> | null = null;
let refreshInflightToken: string | null = null;
let logoutGeneration = 0;
// Sums to 10.5s, just past the launcher's 10s HEALTH_PROBE_TIMEOUT. Guarded by
// `the_frontend_retry_ladder_outlives_one_probe_budget` in src-tauri/src/commands.rs.
const TAURI_FETCH_RETRY_DELAYS_MS = [250, 750, 1500, 3000, 5000] as const;
// Retrying a committed POST duplicates it, so non-idempotent methods keep a shorter ladder.
const TAURI_FETCH_RETRY_DELAYS_UNSAFE_MS = [250, 750, 1500] as const;
const IDEMPOTENT_METHODS = new Set(["GET", "HEAD", "OPTIONS", "PUT", "DELETE"]);
function retryDelaysFor(
input: RequestInfo | URL,
init?: RequestInit,
): readonly number[] {
const method = (
init?.method ??
(typeof Request !== "undefined" && input instanceof Request
? input.method
: "GET")
).toUpperCase();
return IDEMPOTENT_METHODS.has(method)
? TAURI_FETCH_RETRY_DELAYS_MS
: TAURI_FETCH_RETRY_DELAYS_UNSAFE_MS;
}
const BROWSER_TIMEZONE_HEADER = "X-Unsloth-Timezone";
const BROWSER_TIMEZONE_OFFSET_HEADER = "X-Unsloth-Timezone-Offset-Minutes";
function addBrowserTimezoneHeaders(headers: Headers): void {
try {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (timezone) headers.set(BROWSER_TIMEZONE_HEADER, timezone);
headers.set(
BROWSER_TIMEZONE_OFFSET_HEADER,
String(new Date().getTimezoneOffset()),
);
} catch {
// runtimes without Intl keep the backend-local fallback.
}
}
function wait(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function clearAuthTokensIfCurrent(refreshToken: string | null): void {
if (!refreshToken || getRefreshToken() === refreshToken) clearAuthTokens();
}
async function fetchWithTauriNetworkRetry(
input: RequestInfo | URL,
init?: RequestInit,
retryNetworkErrors = true,
beforeRetry?: () => void,
): Promise<Response> {
const delays = retryDelaysFor(input, init);
for (let attempt = 0; ; attempt++) {
try {
return await fetch(input, init);
} catch (error) {
if (
!isTauri ||
!retryNetworkErrors ||
!(error instanceof TypeError) ||
attempt >= delays.length
) {
throw error;
}
// Ask the native side once on the first failure: fetch cannot tell refused from silent.
if (attempt === 0 && (await nativeBackendIsGone())) throw error;
await wait(delays[attempt]);
beforeRetry?.();
}
}
}
async function isPasswordChangeRequiredResponse(
response: Response,
): Promise<boolean> {
if (response.status !== 403) return false;
try {
const payload = (await response.clone().json()) as { detail?: string };
return payload.detail === "Password change required";
} catch {
return false;
}
}
async function redirectToAuth(passwordChangeRequired = false): Promise<void> {
if (isRedirecting) return;
isRedirecting = true;
let target = "/login";
try {
const res = await fetch(apiUrl("/api/auth/status"));
if (res.ok) {
const data = (await res.json()) as {
requires_password_change: boolean;
login_mode?: "single" | "multi";
};
// Public status describes the owner. A managed session carries its own requirement.
const requiresChange =
data.login_mode === "multi"
? passwordChangeRequired || mustChangePassword()
: data.requires_password_change;
if (requiresChange !== mustChangePassword()) {
setMustChangePassword(requiresChange);
}
if (requiresChange) target = "/change-password";
}
} catch {
// Fall through to /login on error
}
if (window.location.pathname === target) {
isRedirecting = false;
return;
}
window.location.href = target;
}
/** Copy shown when the backend really is unreachable and the launcher agrees. */
export const BACKEND_NOT_RUNNING_MESSAGE =
"Unsloth isn't running -- please relaunch it.";
/** Copy shown when the webview could not reach the backend but the launcher says it is up. */
export const BACKEND_NOT_ANSWERING_MESSAGE =
"Unsloth is running but did not answer in time. It may still be starting up. Please try again in a moment.";
/** `check_backend_present` and NOT `check_health`: the latter reports a probe that ran out of budget exactly as a refused connection. */
// The port is carried WITH the promise: `setApiBase` can move it inside the 10s budget.
let nativeHealthInflight: { port: number; probe: Promise<boolean> } | null = null;
async function nativeBackendIsAlive(): Promise<boolean> {
if (!isTauri) {
return false;
}
const port = getApiPort();
if (port === null) {
return false;
}
// Single flight: one 10s probe rather than one per panel. Not cached beyond the call.
if (nativeHealthInflight !== null && nativeHealthInflight.port === port) {
return nativeHealthInflight.probe;
}
const probe = (async () => {
try {
const { invoke } = await import("@tauri-apps/api/core");
return (
(await invoke<boolean>("check_backend_present", { port })) === true
);
} catch {
return false;
}
})();
const inflight = { port, probe };
nativeHealthInflight = inflight;
try {
return await probe;
} finally {
// Identity, not the port: a probe for a newer port owns the slot now.
if (nativeHealthInflight === inflight) {
nativeHealthInflight = null;
}
}
}
/** `check_backend_is_gone` and NOT `check_backend_present`: presence reports a backend of ours that has not bound its port yet as absent. */
let nativeGoneInflight: { port: number; probe: Promise<boolean> } | null = null;
/** True only on positive proof; every failure mode (browser, no port, old shell) returns false. */
async function nativeBackendIsGone(): Promise<boolean> {
if (!isTauri) {
return false;
}
const port = getApiPort();
if (port === null) {
return false;
}
// Single flight, like the presence probe: a hub losing the backend fails every panel at once.
if (nativeGoneInflight !== null && nativeGoneInflight.port === port) {
return nativeGoneInflight.probe;
}
const probe = (async () => {
try {
const { invoke } = await import("@tauri-apps/api/core");
return (
(await invoke<boolean>("check_backend_is_gone", { port })) === true
);
} catch {
return false;
}
})();
const inflight = { port, probe };
nativeGoneInflight = inflight;
try {
return await probe;
} finally {
if (nativeGoneInflight === inflight) {
nativeGoneInflight = null;
}
}
}
async function asTransportFailure(err: unknown): Promise<unknown> {
// TypeError means the request never reached the backend; Tauri asks the launcher first.
if (!(err instanceof TypeError)) return err;
if (
!isTauri &&
typeof navigator !== "undefined" &&
navigator.onLine === false
) {
return Object.assign(
new Error(
"You appear to be offline. Check your network connection and try again.",
),
{ unslothTransportFailure: true },
);
}
// A failed webview fetch does not prove the backend died; relaunching would kill its work.
if (await nativeBackendIsAlive()) {
return Object.assign(new Error(BACKEND_NOT_ANSWERING_MESSAGE), {
unslothTransportFailure: true,
unslothBackendStillRunning: true,
});
}
return Object.assign(new Error(BACKEND_NOT_RUNNING_MESSAGE), {
unslothTransportFailure: true,
});
}
async function retryWithCurrentToken(
input: RequestInfo | URL,
init?: RequestInit,
retryNetworkErrors = true,
beforeRetry?: () => void,
): Promise<Response> {
beforeRetry?.();
const retryHeaders = new Headers(init?.headers);
addBrowserTimezoneHeaders(retryHeaders);
const token = getAuthToken();
if (token) retryHeaders.set("Authorization", `Bearer ${token}`);
// Retries are tagged like the first attempt; an untagged TypeError reads as a rejection.
try {
return await fetchWithTauriNetworkRetry(
input,
{ ...init, headers: retryHeaders },
retryNetworkErrors,
beforeRetry,
);
} catch (err) {
throw await asTransportFailure(err);
}
}
async function retryWithTauriAutoAuth(
input: RequestInfo | URL,
init?: RequestInit,
retryNetworkErrors = true,
beforeRetry?: () => void,
): Promise<Response | null> {
clearAuthTokens();
const { tauriAutoAuth } = await import("./tauri-auto-auth");
if (await tauriAutoAuth()) {
return retryWithCurrentToken(input, init, retryNetworkErrors, beforeRetry);
}
return null;
}
/** "rejected": the server refused the token or the user signed out. "unavailable": no answer yet. */
export type RefreshOutcome = "ok" | "rejected" | "unavailable";
// Never abort an in-flight POST: if the server already rotated the pair, dropping its answer loses the new token.
const REFRESH_MAX_ATTEMPTS = 3;
const REFRESH_BUDGET_MS = 8000;
const REFRESH_LOCK_WAIT_MS = 8000;
const REFRESH_RETRY_DEFAULT_MS = 1000;
const REFRESH_RETRY_MIN_MS = 250;
const REFRESH_RETRY_MAX_MS = 5000;
const REFRESH_PEER_WAIT_MS = 3000;
const REFRESH_PEER_POLL_MS = 100;
function refreshRetryDelay(response: Response): number {
const header = response.headers.get("Retry-After")?.trim();
const seconds = header ? Number(header) : Number.NaN;
if (!Number.isFinite(seconds) || seconds < 0) {
return REFRESH_RETRY_DEFAULT_MS;
}
return Math.min(
Math.max(seconds * 1000, REFRESH_RETRY_MIN_MS),
REFRESH_RETRY_MAX_MS,
);
}
/** The winner's 200 committed before this 401 was sent, so its pair lands here within moments. */
async function peerRotated(
refreshToken: string,
startGeneration: number,
sessionMark: string,
): Promise<boolean> {
const deadline = Date.now() + REFRESH_PEER_WAIT_MS;
for (;;) {
if (
startGeneration !== logoutGeneration ||
accountTransitionPending() ||
getAuthSessionMark() !== sessionMark
)
return false;
const current = getRefreshToken();
if (current !== refreshToken) return current !== null;
if (Date.now() >= deadline) return false;
await wait(REFRESH_PEER_POLL_MS);
}
}
async function runRefresh(
refreshToken: string,
preserveSessionMark = false,
// No Web Locks: a peer tab may spend this token first. Only a rotation under this mark is theirs.
peerSessionMark?: string,
): Promise<RefreshOutcome> {
const startGeneration = logoutGeneration;
const startedAt = Date.now();
const superseded = (): RefreshOutcome | null => {
if (startGeneration !== logoutGeneration) return "rejected";
if (getRefreshToken() !== refreshToken) return "unavailable";
return null;
};
for (let attempt = 1; ; attempt++) {
let response: Response;
try {
response = await fetchWithTauriNetworkRetry(apiUrl("/api/auth/refresh"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ refresh_token: refreshToken }),
});
} catch {
return "unavailable";
}
// Only a 401 rejects the token; a 5xx, 429 or proxy error says nothing about the session.
if (response.status === 401) {
if (
peerSessionMark !== undefined &&
(await peerRotated(refreshToken, startGeneration, peerSessionMark))
)
return "ok";
clearAuthTokensIfCurrent(refreshToken);
return "rejected";
}
if (response.ok) {
let payload: RefreshResponse;
try {
payload = (await response.json()) as RefreshResponse;
} catch {
return "unavailable";
}
const stale = superseded();
if (stale) return stale;
storeAuthTokens(payload.access_token, payload.refresh_token, preserveSessionMark);
setMustChangePassword(payload.must_change_password ?? false);
return "ok";
}
if (response.status !== 503 && response.status !== 429) {
return "unavailable";
}
const delay = refreshRetryDelay(response);
if (
attempt >= REFRESH_MAX_ATTEMPTS ||
Date.now() - startedAt + delay > REFRESH_BUDGET_MS
) {
return "unavailable";
}
await wait(delay);
const stale = superseded();
if (stale) return stale;
}
}
export async function refreshSessionOutcome(): Promise<RefreshOutcome> {
const refreshToken = getRefreshToken();
if (!refreshToken) return "rejected";
if (refreshInflight && refreshInflightToken === refreshToken) {
return refreshInflight;
}
const generation = logoutGeneration;
const locks = typeof window !== "undefined" ? window.navigator?.locks : undefined;
// Without a mark at refresh start, a peer's backfill cannot be told from a logout and sign-in.
const sessionMark = locks ? ensureAuthSessionMark() : getAuthSessionMark();
if (getRefreshToken() !== refreshToken) return "unavailable";
const promise = locks
? (async (): Promise<RefreshOutcome> => {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), REFRESH_LOCK_WAIT_MS);
try {
return await locks.request(
"unsloth-auth-refresh",
{ signal: controller.signal },
async (): Promise<RefreshOutcome> => {
// Only bound the queue; a submitted POST may already have rotated.
clearTimeout(timer);
if (generation !== logoutGeneration) return "rejected";
const current = getRefreshToken();
if (!current) return "rejected";
if (accountTransitionPending() || getAuthSessionMark() !== sessionMark)
return "unavailable";
if (current !== refreshToken) return "ok";
return runRefresh(current, true);
},
);
} catch {
return "unavailable";
} finally {
clearTimeout(timer);
}
})()
: runRefresh(refreshToken, false, sessionMark ?? undefined);
refreshInflight = promise;
refreshInflightToken = refreshToken;
try {
return await promise;
} finally {
if (refreshInflight === promise) {
refreshInflight = null;
refreshInflightToken = null;
}
}
}
export async function refreshSession(): Promise<boolean> {
return (await refreshSessionOutcome()) === "ok";
}
export async function authFetch(
input: RequestInfo | URL,
init?: RequestInit,
options?: AuthFetchOptions,
): Promise<Response> {
// Another tab is mid-switch: a request now would carry this account's content under the
// next account's credentials.
if (accountTransitionPending())
throw new Error("Another tab is switching accounts; this tab will reload.");
const resolvedInput = typeof input === "string" ? apiUrl(input) : input;
const headers = new Headers(init?.headers);
addBrowserTimezoneHeaders(headers);
const accessToken = getAuthToken();
if (accessToken) {
headers.set("Authorization", `Bearer ${accessToken}`);
}
let response: Response;
try {
response = await fetchWithTauriNetworkRetry(
resolvedInput,
{
...init,
headers,
},
options?.retryNetworkErrors ?? true,
options?.beforeRetry,
);
} catch (err) {
throw await asTransportFailure(err);
}
if (await isPasswordChangeRequiredResponse(response)) {
if (isTauri) {
return (
(await retryWithTauriAutoAuth(
resolvedInput,
init,
options?.retryNetworkErrors ?? true,
options?.beforeRetry,
)) ?? response
);
}
void redirectToAuth(true);
return response;
}
if (response.status !== 401) return response;
const refreshToken = getRefreshToken();
const outcome = await refreshSessionOutcome();
if (outcome === "unavailable") return response;
if (outcome === "rejected") {
if (isTauri) {
return (
(await retryWithTauriAutoAuth(
resolvedInput,
init,
options?.retryNetworkErrors ?? true,
options?.beforeRetry,
)) ?? response
);
}
clearAuthTokensIfCurrent(refreshToken);
void redirectToAuth();
return response;
}
if (mustChangePassword()) {
if (isTauri) {
return (
(await retryWithTauriAutoAuth(
resolvedInput,
init,
options?.retryNetworkErrors ?? true,
options?.beforeRetry,
)) ?? response
);
}
void redirectToAuth();
return response;
}
if (!getAuthToken()) clearAuthTokens();
return retryWithCurrentToken(
resolvedInput,
init,
options?.retryNetworkErrors ?? true,
options?.beforeRetry,
);
}
async function postLogout(
accessToken: string | null,
): Promise<Response | null> {
try {
return await fetchWithTauriNetworkRetry(apiUrl("/api/auth/logout"), {
method: "POST",
headers: accessToken
? { Authorization: `Bearer ${accessToken}` }
: undefined,
});
} catch {
return null;
}
}
export async function logout(): Promise<void> {
// An expired access token 401s before revoke runs, so rotate via the refresh token and retry.
try {
let response = await postLogout(getAuthToken());
if (response && response.status === 401 && getRefreshToken()) {
const refreshed = await refreshSession();
if (refreshed) response = await postLogout(getAuthToken());
}
} finally {
logoutGeneration += 1;
clearAuthTokens();
}
}