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 { authFetch } from "@/features/auth";
import { readFastApiError } from "@/lib/format-fastapi-error";
/** Caches are named by key, never path, so nothing the browser sends can widen a purge. */
export const CACHE_KEYS = [
"uv",
"pip",
"npm",
"bun",
"torch_inductor",
"torch_extensions",
"triton",
"cuda",
"numba",
"matplotlib",
"vllm",
"unsloth_compiled",
"hf_xet",
"hf_assets",
"hf_datasets",
"hf_hub",
] as const;
export type CacheKey = (typeof CACHE_KEYS)[number];
export type CacheGroup = "packages" | "compile" | "models";
export type CacheEntry = {
key: CacheKey;
group: CacheGroup;
optIn: boolean;
paths: string[];
measuredPaths?: string[];
clearSizeBytes?: number | null;
clearEntryCount?: number | null;
systemDiskBytes?: number | null;
sizeBytes: number;
entryCount: number;
present: boolean;
purgeable: boolean;
blockedReason: string | null;
};
export type CacheInventory = {
caches: CacheEntry[];
totalBytes: number;
reclaimableBytes: number;
freeBytes: number | null;
totalDiskBytes: number | null;
refreshing: boolean;
};
export type CachePurgeResult = {
key: CacheKey;
freedBytes: number;
removedEntries: number;
errors: string[];
};
export type CachePurgeOutcome = {
results: CachePurgeResult[];
freedBytes: number;
inventory: CacheInventory;
};
type ApiCacheEntry = {
key: CacheKey;
group: CacheGroup;
// biome-ignore lint/style/useNamingConvention: API schema
opt_in: boolean;
paths: string[];
// biome-ignore lint/style/useNamingConvention: API schema
measured_paths?: string[];
// biome-ignore lint/style/useNamingConvention: API schema
clear_size_bytes?: number | null;
// biome-ignore lint/style/useNamingConvention: API schema
clear_entry_count?: number | null;
// biome-ignore lint/style/useNamingConvention: API schema
system_disk_bytes?: number | null;
// biome-ignore lint/style/useNamingConvention: API schema
size_bytes: number;
// biome-ignore lint/style/useNamingConvention: API schema
entry_count: number;
present: boolean;
purgeable: boolean;
// biome-ignore lint/style/useNamingConvention: API schema
blocked_reason: string | null;
};
type ApiCacheInventory = {
caches: ApiCacheEntry[];
// biome-ignore lint/style/useNamingConvention: API schema
total_bytes: number;
// biome-ignore lint/style/useNamingConvention: API schema
reclaimable_bytes: number;
// biome-ignore lint/style/useNamingConvention: API schema
free_bytes: number | null;
// biome-ignore lint/style/useNamingConvention: API schema
total_disk_bytes: number | null;
refreshing?: boolean;
};
type ApiCachePurgeResult = {
key: CacheKey;
// biome-ignore lint/style/useNamingConvention: API schema
freed_bytes: number;
// biome-ignore lint/style/useNamingConvention: API schema
removed_entries: number;
errors: string[];
};
type ApiCachePurgeOutcome = {
results: ApiCachePurgeResult[];
// biome-ignore lint/style/useNamingConvention: API schema
freed_bytes: number;
inventory: ApiCacheInventory;
};
function entryFromApi(value: ApiCacheEntry): CacheEntry {
return {
key: value.key,
group: value.group,
optIn: value.opt_in,
paths: value.paths,
measuredPaths: value.measured_paths,
clearSizeBytes: value.clear_size_bytes,
clearEntryCount: value.clear_entry_count,
systemDiskBytes: value.system_disk_bytes,
sizeBytes: value.size_bytes,
entryCount: value.entry_count,
present: value.present,
purgeable: value.purgeable,
blockedReason: value.blocked_reason,
};
}
export function inventoryFromApi(value: ApiCacheInventory): CacheInventory {
return {
caches: value.caches.map(entryFromApi),
totalBytes: value.total_bytes,
reclaimableBytes: value.reclaimable_bytes,
freeBytes: value.free_bytes,
totalDiskBytes: value.total_disk_bytes,
refreshing: value.refreshing === true,
};
}
export function purgeOutcomeFromApi(
value: ApiCachePurgeOutcome,
): CachePurgeOutcome {
return {
results: value.results.map((result) => ({
key: result.key,
freedBytes: result.freed_bytes,
removedEntries: result.removed_entries,
errors: result.errors,
})),
freedBytes: value.freed_bytes,
inventory: inventoryFromApi(value.inventory),
};
}
export async function loadCacheInventory(
options: { refresh?: boolean } = {},
): Promise<CacheInventory> {
// refresh re-walks instead of reusing a recent size; UI sessions only.
const response = await authFetch(
options.refresh
? "/api/settings/caches?refresh=true"
: "/api/settings/caches",
);
if (!response.ok) {
throw new Error(
await readFastApiError(response, "Failed to measure the caches"),
);
}
return inventoryFromApi(await response.json());
}
export type CacheBreakdownEntry = {
name: string;
path: string;
sizeBytes: number;
repoId?: string;
repoType?: "model";
cachePath?: string;
deleteAllowed?: boolean;
};
export async function loadCacheBreakdown(
key: CacheKey,
): Promise<CacheBreakdownEntry[]> {
const response = await authFetch(`/api/settings/caches/${key}/breakdown`);
if (!response.ok) {
throw new Error(
await readFastApiError(response, "Failed to measure cache folders"),
);
}
const result = (await response.json()) as {
entries: {
name: string;
path: string;
// biome-ignore lint/style/useNamingConvention: API schema
size_bytes: number;
repo_id?: string;
repo_type?: "model";
cache_path?: string;
delete_allowed?: boolean;
}[];
};
return result.entries.map((entry) => ({
name: entry.name,
path: entry.path,
sizeBytes: entry.size_bytes,
repoId: entry.repo_id,
repoType: entry.repo_type,
cachePath: entry.cache_path,
deleteAllowed: entry.delete_allowed === true,
}));
}
export async function purgeCaches(
keys: readonly CacheKey[],
): Promise<CachePurgeOutcome> {
const response = await authFetch("/api/settings/caches/purge", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ keys }),
});
if (!response.ok) {
throw new Error(
await readFastApiError(response, "Failed to clear the caches"),
);
}
return purgeOutcomeFromApi(await response.json());
}
/** Present, allowed and free to rebuild; counted by entries since empty dirs measure zero. */
export function bulkPurgeKeys(inventory: CacheInventory): CacheKey[] {
return inventory.caches
.filter(
(entry) =>
entry.present &&
entry.purgeable &&
!entry.optIn &&
(entry.clearEntryCount ?? entry.entryCount) > 0,
)
.map((entry) => entry.key);
}