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.

1.3 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 { authFetch } from "@/features/auth";
// eslint-disable-next-line no-restricted-imports
import { hubTokenHeader } from "@/features/hub/lib/hub-token-header";

export type HfTokenValidationStatus =
  | "missing"
  | "valid"
  | "invalid"
  | "rate_limited"
  | "unavailable";

export interface HfTokenValidationResult {
  status: HfTokenValidationStatus;
  retryAfterSeconds: number | null;
}

export async function validateHfToken(
  token: string | null | undefined,
): Promise<HfTokenValidationResult> {
  const normalized = token?.trim() ?? "";
  if (!normalized) {
    return { status: "missing", retryAfterSeconds: null };
  }
  const response = await authFetch("/api/hub/token/validate", {
    method: "POST",
    headers: hubTokenHeader(normalized),
  });
  if (!response.ok) {
    return { status: "unavailable", retryAfterSeconds: null };
  }
  const body = (await response.json()) as {
    status?: HfTokenValidationStatus;
    retry_after_seconds?: number | null;
  };
  const status = body.status ?? "unavailable";
  // A cached "valid" may predate revocation, so it never clears a refusal a real read saw.
  return {
    status,
    retryAfterSeconds: body.retry_after_seconds ?? null,
  };
}