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 type { ChatPresetSource } from "../presets/preset-policy";
import type {
PermissionMode,
SandboxLevel,
RagAutoInject,
RagMode,
RagSource,
ReasoningEffort,
} from "../stores/chat-runtime-store";
import type {
ResearchMcpSource,
ResearchWebsitePolicy,
} from "../types/research";
import type { PersistedInferenceParams } from "../types/runtime";
import {
ChatSettingsRequestError,
asBodyTransportFailure,
isAbortOrTimeout,
isUnderKeepaliveBudget,
} from "../utils/settings-retry";
export type { PersistedInferenceParams };
export interface PersistedChatPreset {
name: string;
params: PersistedInferenceParams;
loadConfig?: Record<string, unknown>;
}
export interface PersistedChatSettings {
/** Ordered sidebar chat/pair IDs, shared by clients of this installation. */
pinnedChatIds?: string[];
inferenceParams?: PersistedInferenceParams;
inferenceParamsByModel?: Record<string, PersistedInferenceParams>;
/** Outgoing only: per model id, keys of the entry above the server drops before merging the rest. */
forgetInferenceParamsByModel?: Record<string, string[]>;
rememberParamsPerModel?: boolean;
customPresets?: PersistedChatPreset[];
activePreset?: string;
activePresetSource?: ChatPresetSource;
autoTitle?: boolean;
reasoningEffort?: ReasoningEffort;
preserveThinking?: boolean;
collapseHtmlArtifacts?: boolean;
allowArtifactNetworkAccess?: boolean;
/** web_search also returns image results the model can place inline. */
searchImages?: boolean;
autoHealToolCalls?: boolean;
nudgeToolCalls?: boolean;
deduplicateToolCalls?: boolean;
maxToolCallsPerMessage?: number;
toolCallTimeout?: number;
reasoningEnabled?: boolean;
toolsEnabled?: boolean;
codeToolsEnabled?: boolean;
imageToolsEnabled?: boolean;
webFetchToolsEnabled?: boolean;
deepResearchEnabled?: boolean;
researchWebsitePolicy?: ResearchWebsitePolicy;
researchMcpSources?: ResearchMcpSource[];
researchModelTimeoutSeconds?: number;
mcpEnabledForChat?: boolean;
confirmToolCalls?: boolean;
/** "full" (Full access) is session-only and never leaves the browser. */
permissionMode?: Exclude<PermissionMode, "full">;
sandboxLevel?: SandboxLevel;
ragSource?: RagSource;
ragMode?: RagMode;
ragTopK?: number;
ragAutoInject?: RagAutoInject;
ragAutoInjectMinScore?: number;
ragOcrScanned?: boolean;
ragCaptionFigures?: boolean;
speculativeType?: "auto" | "ngram" | "off";
gpuMemoryMode?: "auto" | "manual";
expandQuantizations?: boolean;
showAllQuantizations?: boolean;
fitOnDeviceOnly?: boolean;
/** Local GGUF chats: drop oldest turns instead of erroring at the window. */
autoCompactEnabled?: boolean;
/** Off: a reply or Deep Research run stops once no tab follows it, instead of finishing on the server. */
keepRunningWhenTabClosed?: boolean;
}
interface ChatSettingsResponse {
settings: PersistedChatSettings;
}
interface ConditionalChatSettingsResponse extends ChatSettingsResponse {
applied: boolean;
}
export type ChatSettingsPath = [keyof PersistedChatSettings, ...string[]];
function parseErrorText(status: number, body: unknown): string {
if (
body &&
typeof body === "object" &&
"detail" in body &&
typeof body.detail === "string"
) {
return body.detail;
}
if (
body &&
typeof body === "object" &&
"detail" in body &&
body.detail != null
) {
return `Request failed (${status}): ${JSON.stringify(body.detail)}`;
}
if (
body &&
typeof body === "object" &&
"message" in body &&
typeof body.message === "string"
) {
return body.message;
}
return `Request failed (${status})`;
}
async function parseJsonOrThrow<T>(response: Response): Promise<T> {
const body = await response.json().catch((error: unknown) => {
// A 200 cut off or aborted mid-body has no answer; invalid JSON (SyntaxError) still returns null.
if (response.ok && (error instanceof TypeError || isAbortOrTimeout(error))) {
throw asBodyTransportFailure(error);
}
return null;
});
if (!response.ok) {
// Typed so the settings queue can tell server-down (keep patch) from rejection (drop fields).
throw new ChatSettingsRequestError(
parseErrorText(response.status, body),
response.status,
body && typeof body === "object" && "detail" in body
? (body as { detail: unknown }).detail
: null,
);
}
return body as T;
}
export async function getChatSettings(
options: { signal?: AbortSignal } = {},
): Promise<PersistedChatSettings> {
const response = await authFetch("/api/chat/settings", options);
const data = await parseJsonOrThrow<ChatSettingsResponse>(response);
return data.settings;
}
export async function saveChatSettingsPatch(
patch: PersistedChatSettings,
options: { keepalive?: boolean } = {},
): Promise<PersistedChatSettings> {
const body = JSON.stringify(patch);
const response = await authFetch("/api/chat/settings", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body,
// keepalive only under the Fetch standard's 64 KiB in-flight budget; over it every engine fails.
keepalive: options.keepalive ? isUnderKeepaliveBudget(body) : undefined,
});
const data = await parseJsonOrThrow<ChatSettingsResponse>(response);
return data.settings;
}
export async function saveChatSettingsPatchIfCurrent(
expected: PersistedChatSettings,
patch: PersistedChatSettings,
expectedAbsent: Array<keyof PersistedChatSettings> = [],
expectedAbsentPaths: ChatSettingsPath[] = [],
options: {
signal?: AbortSignal;
requireSupport?: boolean;
operationId?: string;
unpinnedChatIds?: string[];
cancelledOperationIds?: string[];
} = {},
): Promise<{ settings: PersistedChatSettings; applied: boolean }> {
const response = await authFetch("/api/chat/settings/compare-and-set", {
signal: options.signal,
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
expected,
expectedAbsent,
expectedAbsentPaths,
patch,
...(options.operationId ? { operationId: options.operationId } : {}),
...(options.unpinnedChatIds
? { unpinnedChatIds: options.unpinnedChatIds }
: {}),
...(options.cancelledOperationIds?.length
? { cancelledOperationIds: options.cancelledOperationIds }
: {}),
}),
});
// 404 (--api-only) or 405 (browser SPA catch-all) means an older backend: report not applied.
if (
!options.requireSupport &&
(response.status === 404 || response.status === 405)
) {
return { settings: expected, applied: false };
}
return parseJsonOrThrow<ConditionalChatSettingsResponse>(response);
}