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
Studio: turn on Code when Full access is picked (#11283)
oobabooga8084541caeb1
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsxindex 391e6250d84..1ba3c4868b3 100644--- a/studio/frontend/src/components/assistant-ui/thread.tsx+++ b/studio/frontend/src/components/assistant-ui/thread.tsx@@ -188,6 +188,7 @@ import { useRagToolDisabled } from "@/features/chat/hooks/use-rag-tool-disabled"import { BypassPermissionsMenuItem } from "@/features/chat/bypass-permissions-menu-item";import { PermissionModeComposerPill } from "@/features/chat/permission-mode-select";import {+ codeToolsOn,settleThreadScopedSettingsForCopy,useChatRuntimeStore,} from "@/features/chat/stores/chat-runtime-store";@@ -5973,7 +5974,7 @@ const CodeToolsToggle: FC = () => {const supportsBuiltinCodeExecution = useChatRuntimeStore((s) => s.supportsBuiltinCodeExecution,);- const codeToolsEnabled = useChatRuntimeStore((s) => s.codeToolsEnabled);+ const codeToolsEnabled = useChatRuntimeStore(codeToolsOn);const setCodeToolsEnabled = useChatRuntimeStore((s) => s.setCodeToolsEnabled);// Disable only when a loaded model lacks the capability; with no model the// tool can still be pre-selected, matching the + menu.@@ -6185,7 +6186,7 @@ const ComposerToolsMenu: FC<{const navigate = useNavigate();const toolsEnabled = useChatRuntimeStore((s) => s.toolsEnabled);const setToolsEnabled = useChatRuntimeStore((s) => s.setToolsEnabled);- const codeToolsEnabled = useChatRuntimeStore((s) => s.codeToolsEnabled);+ const codeToolsEnabled = useChatRuntimeStore(codeToolsOn);const setCodeToolsEnabled = useChatRuntimeStore((s) => s.setCodeToolsEnabled);const artifactsEnabled = useChatRuntimeStore((s) => s.artifactsEnabled);const setArtifactsEnabled = useChatRuntimeStore((s) => s.setArtifactsEnabled);diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex 0094fa5aa78..bfa169f6622 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -211,6 +211,7 @@ import {awaitThreadScopedPairing,awaitPendingQwenDefaultsMigration,flushPendingChatSettings,+ codeToolsOn,useChatRuntimeStore,} from "../stores/chat-runtime-store";import {@@ -1927,10 +1928,10 @@ export function buildLocalTokenCountReasoning(): Record<string, unknown> {export async function buildLocalTokenCountExtras(threadId: string | undefined,): Promise<Record<string, unknown>> {+ const state = useChatRuntimeStore.getState();const {supportsTools,toolsEnabled,- codeToolsEnabled,artifactsEnabled,mcpEnabledForChat,ragEnabled,@@ -1945,7 +1946,8 @@ export async function buildLocalTokenCountExtras(ragAutoInject,ragAutoInjectMinScore,residentCheckpoint,- } = useChatRuntimeStore.getState();+ } = state;+ const codeToolsEnabled = codeToolsOn(state);// Explicit false, as the completion sends: an omitted field lets the launcher's// tools-on default answer and the server renders a catalog the completion does not.// No budget, because the completion sends none either, so a policy that injects tools@@ -4721,7 +4723,6 @@ export function createOpenAIStreamAdapter(const {supportsTools,toolsEnabled,- codeToolsEnabled,imageToolsEnabled,artifactsEnabled,mcpEnabledForChat,@@ -4736,6 +4737,7 @@ export function createOpenAIStreamAdapter(ragAutoInject,ragAutoInjectMinScore,} = runtime;+ const codeToolsEnabled = codeToolsOn(runtime);if (deepResearchArmed &&!supportsTools &&diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsxindex f127e532b27..fab574cec79 100644--- a/studio/frontend/src/features/chat/shared-composer.tsx+++ b/studio/frontend/src/features/chat/shared-composer.tsx@@ -183,6 +183,7 @@ import {persistGpuMemoryModeOnLoad,resolveSpeculativeSettingsForLoad,saveSpeculativeType,+ codeToolsOn,useChatRuntimeStore,} from "./stores/chat-runtime-store";import {@@ -683,7 +684,7 @@ export function SharedComposer({);const toolsEnabled = useChatRuntimeStore((s) => s.toolsEnabled);const setToolsEnabled = useChatRuntimeStore((s) => s.setToolsEnabled);- const codeToolsEnabled = useChatRuntimeStore((s) => s.codeToolsEnabled);+ const codeToolsEnabled = useChatRuntimeStore(codeToolsOn);const setCodeToolsEnabled = useChatRuntimeStore((s) => s.setCodeToolsEnabled);const imageToolsEnabled = useChatRuntimeStore((s) => s.imageToolsEnabled);const setImageToolsEnabled = useChatRuntimeStore(diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.tsindex 0d5c877fa07..f67ca02f132 100644--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts@@ -1878,6 +1878,35 @@ function savePermissionMode(mode: PermissionMode): void {const INITIAL_PERMISSION_MODE: PermissionMode = loadPermissionMode();+/** Re-picking the level already in force is not a fresh grant, so a manual Code-off stands. */+function codeDeclinedOnEnteringFullAccess(state: ChatRuntimeStore): boolean {+ return state.permissionMode === "full"+ ? state.codeToolsDeclinedUnderFullAccess+ : false;+}++/** Effective Code setting. Full access auto-enables it only for local models.+ * Read the level live, never a flag armed on entry: Full access outlives a chat switch+ * (applyThreadScopedSettings) but codeToolsEnabled does not, so a snapshotted grant missed+ * every chat opened afterwards. */+export function codeToolsOn(+ state: Pick<+ ChatRuntimeStore,+ | "codeToolsEnabled"+ | "codeToolsDeclinedUnderFullAccess"+ | "permissionMode"+ | "supportsTools"+ > & { params: { checkpoint: string } },+): boolean {+ return (+ state.codeToolsEnabled ||+ (!state.codeToolsDeclinedUnderFullAccess &&+ state.permissionMode === "full" &&+ state.supportsTools &&+ !isExternalModelId(state.params.checkpoint))+ );+}+function loadString(key: string, fallback: string): string {return readStorageValue(key) ?? fallback;}@@ -2295,7 +2324,11 @@ type ChatRuntimeStore = {* composer's Fetch pill, independent of Search. */supportsBuiltinWebFetch: boolean;toolsEnabled: boolean;+ /** Persisted Code preference. Use codeToolsOn() for the effective value. */codeToolsEnabled: boolean;+ /** Session-only: a manual Code-off under Full access, so the grant is not re-applied over it.+ * Cleared on entering or leaving the level. */+ codeToolsDeclinedUnderFullAccess: boolean;imageToolsEnabled: boolean;deepResearchEnabled: boolean;researchWebsitePolicy: ResearchWebsitePolicy;@@ -4098,6 +4131,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({supportsBuiltinWebFetch: false,toolsEnabled: loadBool(CHAT_TOOLS_ENABLED_KEY, false),codeToolsEnabled: loadBool(CHAT_CODE_TOOLS_ENABLED_KEY, false),+ codeToolsDeclinedUnderFullAccess: false,imageToolsEnabled: loadBool(CHAT_IMAGE_TOOLS_ENABLED_KEY, false),deepResearchEnabled: loadBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, false),researchWebsitePolicy: loadResearchWebsitePolicy(),@@ -5289,6 +5323,8 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({...(codeToolsEnabled? { codeToolsEnabled, deepResearchEnabled: false }: { codeToolsEnabled }),+ codeToolsDeclinedUnderFullAccess:+ !codeToolsEnabled && state.permissionMode === "full",queuedSettingsEpoch: state.queuedSettingsEpoch + 1,};}),@@ -5322,6 +5358,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({deepResearchEnabled,toolsEnabled: false,codeToolsEnabled: false,+ codeToolsDeclinedUnderFullAccess: false,imageToolsEnabled: false,artifactsEnabled: false,mcpEnabledForChat: false,@@ -5436,6 +5473,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({bypassPermissions: true,confirmToolCalls: false,deepResearchEnabled: false,+ codeToolsDeclinedUnderFullAccess: codeDeclinedOnEnteringFullAccess(state),queuedSettingsEpoch: state.queuedSettingsEpoch + 1,};}@@ -5446,6 +5484,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({permissionMode,bypassPermissions: false,confirmToolCalls,+ codeToolsDeclinedUnderFullAccess: false,queuedSettingsEpoch: state.queuedSettingsEpoch + 1,};}),@@ -5461,6 +5500,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({permissionMode: "full" as PermissionMode,confirmToolCalls: false,deepResearchEnabled: false,+ codeToolsDeclinedUnderFullAccess: codeDeclinedOnEnteringFullAccess(state),queuedSettingsEpoch: state.queuedSettingsEpoch + 1,};}@@ -5471,6 +5511,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({bypassPermissions,permissionMode,confirmToolCalls: permissionMode === "ask" || permissionMode === "auto",+ codeToolsDeclinedUnderFullAccess: false,queuedSettingsEpoch: state.queuedSettingsEpoch + 1,};}),diff --git a/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts b/studio/frontend/src/features/chat/utils/queued-chat-run-settings.tsindex cf96fcb9222..67a50573e5d 100644--- a/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts+++ b/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts@@ -17,6 +17,7 @@ const QUEUED_SETTING_KEYS = ["preserveThinking","toolsEnabled","codeToolsEnabled",+ "codeToolsDeclinedUnderFullAccess","imageToolsEnabled","artifactsEnabled","mcpEnabledForChat",diff --git a/studio/frontend/tests/full-access-code-pill.test.ts b/studio/frontend/tests/full-access-code-pill.test.tsnew file mode 100644index 00000000000..a3bfb3e8b8b--- /dev/null+++ b/studio/frontend/tests/full-access-code-pill.test.ts@@ -0,0 +1,229 @@+// 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 assert from "node:assert/strict";+import { register } from "node:module";+import test from "node:test";++import { installLocalStorageFake } from "./helpers/kit.ts";+import {+ drainMockedTimers,+ enableCountedTimers,+} from "./helpers/mock-timer-drain.ts";++const { store: localStorageFake } = installLocalStorageFake();+localStorageFake.set("unsloth_chat_settings_imported_to_studio_db", "true");+register("./thread-sampling-resolver.mjs", import.meta.url);++const { settingsHttp } = await import("./helpers/store-stubs/settings-http.ts");+const { threadRows } = await import(+ "./helpers/store-stubs/chat-history-storage.ts"+);++const STORE_URL = new URL(+ "../src/features/chat/stores/chat-runtime-store.ts",+ import.meta.url,+).href;+const CODE_KEY = "unsloth_chat_code_tools_enabled";+const LOCAL = "unsloth/Qwen3-1.7B-GGUF";+const EXTERNAL = "external::anthropic::claude-opus-5";++type Store = Record<string, (...args: never[]) => unknown> & {+ params: { checkpoint: string };+ codeToolsEnabled: boolean;+ permissionMode: string;+};++interface StoreModule {+ useChatRuntimeStore: {+ getState: () => Store;+ setState: (patch: Record<string, unknown>) => void;+ };+ codeToolsOn: (state: Store) => boolean;+ beginThreadScopedPairing: (threadId: string) => void;+ awaitStartedThreadScopedSettingsWrites: () => Promise<void>;+}++let scenario = 0;++/** A fresh store with a tool-capable model selected and Code stored as `codeOn`. */+async function freshStore(codeOn: boolean, checkpoint = LOCAL) {+ scenario += 1;+ localStorageFake.set(CODE_KEY, String(codeOn));+ settingsHttp.settings = { codeToolsEnabled: codeOn };+ settingsHttp.puts.length = 0;+ threadRows.reset();+ const mod: StoreModule = await import(+ `${STORE_URL}?scenario=fullaccess${scenario}`+ );+ const store = mod.useChatRuntimeStore;+ store.setState({+ params: { ...store.getState().params, checkpoint },+ supportsTools: true,+ });+ const on = () => mod.codeToolsOn(store.getState());+ return { mod, store, on, state: () => store.getState() };+}++function pick(state: Store, mode: string) {+ (state.setPermissionMode as (mode: string) => void)(mode);+}++function clickCode(state: Store, next: boolean) {+ (state.setCodeToolsEnabled as (enabled: boolean) => void)(next);+}++test("picking Full access turns Code on, and leaving it turns Code back off", async () => {+ const { on, state } = await freshStore(false);+ assert.equal(on(), false);+ pick(state(), "full");+ assert.equal(on(), true);+ // The user's own choice is untouched, so nothing is stored.+ assert.equal(state().codeToolsEnabled, false);+ assert.equal(localStorageFake.get(CODE_KEY), "false");+ for (const mode of ["auto", "ask", "off"]) {+ pick(state(), "full");+ assert.equal(on(), true);+ pick(state(), mode);+ assert.equal(on(), false, `leaving Full access for ${mode}`);+ }+});++test("the legacy bypass toggle behaves the same way", async () => {+ const { on, state } = await freshStore(false);+ (state().setBypassPermissions as (on: boolean) => void)(true);+ assert.equal(on(), true);+ (state().setBypassPermissions as (on: boolean) => void)(false);+ assert.equal(on(), false);+});++test("Code the user already had on stays on after Full access", async () => {+ const { on, state } = await freshStore(true);+ pick(state(), "full");+ assert.equal(on(), true);+ pick(state(), "auto");+ assert.equal(on(), true);+ assert.equal(localStorageFake.get(CODE_KEY), "true");+});++test("a Code click under Full access is the user's and survives leaving it", async () => {+ const off = await freshStore(false);+ pick(off.state(), "full");+ clickCode(off.state(), !off.on());+ assert.equal(off.on(), false, "the click turns the pill off");+ // Re-picking the level already on does not undo the click.+ pick(off.state(), "full");+ assert.equal(off.on(), false);+ pick(off.state(), "auto");+ assert.equal(off.on(), false);++ const offThenOn = await freshStore(false);+ pick(offThenOn.state(), "full");+ clickCode(offThenOn.state(), false);+ clickCode(offThenOn.state(), true);+ pick(offThenOn.state(), "auto");+ assert.equal(offThenOn.on(), true);+ assert.equal(localStorageFake.get(CODE_KEY), "true");+});++test("Full access does not turn on an external provider's code sandbox", async () => {+ const { on, state } = await freshStore(false, EXTERNAL);+ pick(state(), "full");+ assert.equal(on(), false);+});++test("the turn-on reaches neither the installation nor the open chat's snapshot", async (t) => {+ enableCountedTimers(t);+ const tick = (ms: number) => t.mock.timers.tick(ms);+ const { mod, on, state } = await freshStore(false);+ const drain = () =>+ drainMockedTimers(tick, {+ label: "full access drain",+ barrier: () => mod.awaitStartedThreadScopedSettingsWrites(),+ });+ await (state().hydratePersistedSettings as () => Promise<void>)();+ await drain();+ (state().setActiveThreadId as (id: string) => void)("A");+ mod.beginThreadScopedPairing("A");+ (state().applyThreadScopedSettings as (id: string, s: null) => void)(+ "A",+ null,+ );+ pick(state(), "full");+ assert.equal(on(), true);+ // Any per-chat edit writes the chat's whole snapshot, Code included.+ (state().setToolsEnabled as (on: boolean) => void)(true);+ await drain();+ const row = threadRows.rows.get("A") as Record<string, unknown> | undefined;+ assert.ok(row, "the edit wrote chat A's snapshot");+ assert.notEqual(row.codeToolsEnabled, true);+ assert.equal(localStorageFake.get(CODE_KEY), "false");+ for (const put of settingsHttp.puts) {+ assert.notEqual(put.codeToolsEnabled, true);+ }+});++test("a chat opened after Full access was picked still gets the code tools", async (t) => {+ // Full access outlives a chat switch but Code does not, so a grant armed once+ // on entry left the incoming chat on Full access with no code tools.+ enableCountedTimers(t);+ const tick = (ms: number) => t.mock.timers.tick(ms);+ // Code already on where Full access is picked: the case that arms nothing.+ const { mod, store, on, state } = await freshStore(true);+ const drain = () =>+ drainMockedTimers(tick, {+ label: "cross-chat drain",+ barrier: () => mod.awaitStartedThreadScopedSettingsWrites(),+ });+ await (state().hydratePersistedSettings as () => Promise<void>)();+ await drain();+ (state().setActiveThreadId as (id: string) => void)("A");+ mod.beginThreadScopedPairing("A");+ (state().applyThreadScopedSettings as (id: string, s: null) => void)("A", null);+ pick(state(), "full");+ assert.equal(on(), true);++ // An older chat whose own settings are Code off and the ordinary level.+ (state().setActiveThreadId as (id: string) => void)("B");+ mod.beginThreadScopedPairing("B");+ (+ state().applyThreadScopedSettings as (+ id: string,+ s: Record<string, unknown>,+ ) => void+ )("B", { codeToolsEnabled: false, permissionMode: "auto" });+ await drain();+ assert.equal(state().permissionMode, "full", "Full access survives the switch");+ assert.equal(store.getState().params.checkpoint, LOCAL);+ assert.equal(on(), true, "so the code tools come with it");+});++test("a Code click under Full access survives a chat switch too", async (t) => {+ enableCountedTimers(t);+ const tick = (ms: number) => t.mock.timers.tick(ms);+ const { mod, on, state } = await freshStore(false);+ const drain = () =>+ drainMockedTimers(tick, {+ label: "cross-chat decline drain",+ barrier: () => mod.awaitStartedThreadScopedSettingsWrites(),+ });+ await (state().hydratePersistedSettings as () => Promise<void>)();+ await drain();+ (state().setActiveThreadId as (id: string) => void)("A");+ mod.beginThreadScopedPairing("A");+ (state().applyThreadScopedSettings as (id: string, s: null) => void)("A", null);+ pick(state(), "full");+ clickCode(state(), false); // the user's own no+ assert.equal(on(), false);++ (state().setActiveThreadId as (id: string) => void)("B");+ mod.beginThreadScopedPairing("B");+ (+ state().applyThreadScopedSettings as (+ id: string,+ s: Record<string, unknown>,+ ) => void+ )("B", { codeToolsEnabled: false, permissionMode: "auto" });+ await drain();+ assert.equal(on(), false, "a switch does not undo it");+});