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.

4.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 { readFastApiError } from "@/lib/format-fastapi-error";
import { authFetch } from "@/features/auth";
import { fetchAuthStatus } from "@/features/auth/login-client";
import { normalizeAccountUsername } from "@/lib/account-transition";

export interface StudioAccount {
  account_id: string;
  username: string;
  role: "owner" | "user";
  is_active: boolean;
  created_at: string;
  // null = every model; set by the owner per account.
  model_allowlist?: string[] | null;
  login_alias?: string | null;
  display_name?: string | null;
  avatar_url?: string | null;
}
export interface AccountSetupCode {
  account_id: string;
  username: string;
  setup_code: string;
  expires_at: string;
}
interface AccountSetupResponse {
  account: StudioAccount;
  setup_code: string;
  setup_code_expires_at: string;
}
function toSetupCode(payload: AccountSetupResponse): AccountSetupCode {
  return {
    account_id: payload.account.account_id,
    username: payload.account.username,
    setup_code: payload.setup_code,
    expires_at: payload.setup_code_expires_at,
  };
}

async function accountsRequest(
  path = "",
  init?: RequestInit,
): Promise<Response> {
  const response = await authFetch(`/api/accounts${path}`, init);
  if (!response.ok) {
    throw new Error(await readFastApiError(response, "Account request failed"));
  }
  return response;
}
const accountPath = (accountId: string) => `/${encodeURIComponent(accountId)}`;
// A failed status read must not lose a one-time code.
async function refreshAccountPolicy(): Promise<void> {
  await fetchAuthStatus().catch(() => undefined);
}
export async function fetchAccounts(): Promise<StudioAccount[]> {
  const response = await accountsRequest();
  const data = (await response.json()) as { accounts: StudioAccount[] };
  return data.accounts;
}
export async function createAccount(
  username: string,
): Promise<AccountSetupCode> {
  const response = await accountsRequest("", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ username: normalizeAccountUsername(username) }),
  });
  const result = toSetupCode((await response.json()) as AccountSetupResponse);
  await refreshAccountPolicy();
  return result;
}
export async function regenerateSetupCode(
  accountId: string,
): Promise<AccountSetupCode> {
  const response = await accountsRequest(
    `${accountPath(accountId)}/setup-code`,
    { method: "POST" },
  );
  return toSetupCode((await response.json()) as AccountSetupResponse);
}
export async function setAccountActive(
  accountId: string,
  active: boolean,
): Promise<void> {
  await accountsRequest(accountPath(accountId), {
    method: "PATCH",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ is_active: active }),
  });
  await refreshAccountPolicy();
}
export async function deleteAccount(accountId: string): Promise<void> {
  await accountsRequest(accountPath(accountId), { method: "DELETE" });
  await refreshAccountPolicy();
}
export async function renameAccount(
  accountId: string,
  username: string,
): Promise<void> {
  await accountsRequest(`${accountPath(accountId)}/username`, {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ username: normalizeAccountUsername(username) }),
  });
}
export async function setAccountModelAllowlist(
  accountId: string,
  allowlist: string[] | null,
): Promise<void> {
  await accountsRequest(`${accountPath(accountId)}/model-access`, {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ model_allowlist: allowlist }),
  });
}
export async function setOwnerLoginAlias(
  alias: string | null,
): Promise<string | null> {
  const response = await accountsRequest("/owner/login-alias", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ login_alias: alias?.trim() || null }),
  });
  return ((await response.json()) as { login_alias: string | null }).login_alias;
}