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.

Studio: keep Switch Back visible for a snapshot-path chat (#10511)

Chaitanya Laxman56aa1e139f19
diff --git a/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts b/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts
index cbf25e01cce..a841805cdd7 100644
--- a/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts
+++ b/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts
@@ -1,21 +1,89 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+// eslint-disable-next-line no-restricted-imports -- Avoid the hub barrel's React and download-manager exports.
+import {
+ isHfCacheSnapshotPath,
+ isOllamaModelId,
+ modelIdsMatch,
+ publicModelId,
+} from "@/features/hub/lib/model-identity";
import type {
LoraModelOption,
ModelSelectorChangeMeta,
} from "@/features/model-picker/components/model-selector/types";
import {
ggufQuantLabel,
+ ggufVariantsMatch,
normalizeGgufVariantIdentity,
+ residentModelIdMatches,
} from "../../model-picker/model-config/model-identity";
import { resolveOnlyRememberedGgufVariant } from "../../model-picker/model-config/per-model-config";
+import { isExternalModelId } from "../external-providers";
export type ChatModelSwitchTarget = {
modelId: string;
ggufVariant?: string | null;
};
+// External and Ollama ids are opaque and case-sensitive: folding them would merge distinct models.
+function isExactOnlyIdentity(id: string): boolean {
+ return isExternalModelId(id) || isOllamaModelId(id);
+}
+
+/** Same HF cache repo: snapshot path vs repo id, either direction, or two snapshots of one repo. */
+function sameHfCacheIdentity(left: string, right: string): boolean {
+ if (left === right) {
+ return true;
+ }
+ if (isExactOnlyIdentity(left) || isExactOnlyIdentity(right)) {
+ return false;
+ }
+ if (
+ residentModelIdMatches(left, right) ||
+ residentModelIdMatches(right, left)
+ ) {
+ return true;
+ }
+ if (!(isHfCacheSnapshotPath(left) && isHfCacheSnapshotPath(right))) {
+ return false;
+ }
+ const leftRepo = publicModelId(left);
+ return (
+ leftRepo.includes("/") && modelIdsMatch(leftRepo, publicModelId(right))
+ );
+}
+
+export function chatModelIsResident(
+ createdModel: ChatModelSwitchTarget,
+ checkpoint: string,
+ activeGgufVariant: string | null,
+): boolean {
+ if (!sameHfCacheIdentity(checkpoint, createdModel.modelId)) {
+ return false;
+ }
+ return (
+ createdModel.ggufVariant == null ||
+ ggufVariantsMatch(createdModel.ggufVariant, activeGgufVariant)
+ );
+}
+
+/** The live picker id Switch Back loads: the exact id, else a row of the same HF cache repo. */
+export function chatModelSelectableId(
+ modelId: string,
+ selectableModelIds: ReadonlySet<string>,
+): string | null {
+ if (selectableModelIds.has(modelId)) {
+ return modelId;
+ }
+ for (const id of selectableModelIds) {
+ if (sameHfCacheIdentity(id, modelId)) {
+ return id;
+ }
+ }
+ return null;
+}
+
type ChatModelThreadSnapshot = {
id: string;
modelId?: string | null;
diff --git a/studio/frontend/src/features/chat/components/chat-model-notice.tsx b/studio/frontend/src/features/chat/components/chat-model-notice.tsx
index d3490714e1f..2e8d14594fa 100644
--- a/studio/frontend/src/features/chat/components/chat-model-notice.tsx
+++ b/studio/frontend/src/features/chat/components/chat-model-notice.tsx
@@ -4,15 +4,17 @@
import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
-import { ggufVariantsMatch } from "@/features/hub";
+import { modelDisplayName } from "../../model-picker/model-config/model-identity";
import {
CHAT_HISTORY_UPDATED_EVENT,
type ChatHistoryUpdatedDetail,
} from "../api/chat-api";
-import { compareModelDisplayName } from "../lib/external-model-label";
+import { externalModelLabel } from "../lib/external-model-label";
import { getStoredChatThread } from "../utils/chat-history-storage";
import {
type ChatModelSwitchTarget,
+ chatModelIsResident,
+ chatModelSelectableId,
createChatModelHistoryReader,
} from "./chat-model-notice-switch";
@@ -73,17 +75,22 @@ export function ChatModelNotice({
}: ChatModelNoticeProps) {
const createdModel = useChatCreatedModel(threadId);
if (!createdModel) return null;
- if (
- createdModel.modelId === checkpoint &&
- (createdModel.ggufVariant == null ||
- ggufVariantsMatch(createdModel.ggufVariant, activeGgufVariant))
- ) {
+ if (chatModelIsResident(createdModel, checkpoint, activeGgufVariant)) {
return null;
}
// A model that has since been deleted, or a connection that is gone: the switch could not be
- // honoured, and saying so on every open is just noise.
- if (!selectableModelIds.has(createdModel.modelId)) return null;
- const label = compareModelDisplayName(createdModel.modelId);
+ // honoured, and saying so on every open is just noise. A gone snapshot loads its repo's live row.
+ const selectableId = chatModelSelectableId(
+ createdModel.modelId,
+ selectableModelIds,
+ );
+ if (!selectableId) {
+ return null;
+ }
+ const switchTarget = { ...createdModel, modelId: selectableId };
+ const label =
+ externalModelLabel(createdModel.modelId) ??
+ modelDisplayName(createdModel.modelId);
return (
// Positioned, not in flow. The chat header is `absolute ... z-40` with an opaque `bg-background`,
// so an in-flow sibling starts at y=0 UNDER it and the bar is invisible bar the scrollbar gutter
@@ -101,7 +108,7 @@ export function ChatModelNotice({
variant="ghost"
size="sm"
className="ml-auto h-6 shrink-0 px-2 text-ui-12"
- onClick={() => onSwitch(createdModel)}
+ onClick={() => onSwitch(switchTarget)}
>
Switch back
</Button>
diff --git a/studio/frontend/tests/chat-remembers-its-model.test.ts b/studio/frontend/tests/chat-remembers-its-model.test.ts
index 90c0e9b3c42..37442733b25 100644
--- a/studio/frontend/tests/chat-remembers-its-model.test.ts
+++ b/studio/frontend/tests/chat-remembers-its-model.test.ts
@@ -20,10 +20,15 @@ import {
registerBundlerResolver();
const { store } = installLocalStorageFake();
const {
+ chatModelIsResident,
+ chatModelSelectableId,
chatModelSwitchMeta,
createChatModelHistoryReader,
resolveChatModelSwitchTarget,
} = await import("../src/features/chat/components/chat-model-notice-switch.ts");
+const { modelDisplayName } = await import(
+ "../src/features/hub/lib/model-identity.ts"
+);
const { chatLocalModelOptions } = await import(
"../src/features/chat/local-model-options.ts"
);
@@ -86,21 +91,20 @@ test("the notice never switches a model on its own", () => {
// Opening a chat must not evict what is resident: a local load is multi-gigabyte.
assert.doesNotMatch(notice, /loadModel|setCheckpoint/);
// The only way out of it is the button.
- assert.match(notice, /onClick=\{\(\) => onSwitch\(createdModel\)\}/);
+ assert.match(notice, /onClick=\{\(\) => onSwitch\(switchTarget\)\}/);
});
test("the notice stays quiet when it has nothing to offer", () => {
const body = notice.slice(notice.indexOf("export function ChatModelNotice"));
- // no stamp, already on the exact pick, or a model that has since gone away
+ // no stamp, already on the pick (including snapshot vs repo id), or a model that has since gone away
assert.match(body, /if \(!createdModel\) return null;/);
- assert.match(body, /createdModel\.modelId === checkpoint/);
assert.match(
body,
- /ggufVariantsMatch\(createdModel\.ggufVariant, activeGgufVariant\)/,
+ /chatModelIsResident\(createdModel, checkpoint, activeGgufVariant\)/,
);
assert.match(
body,
- /if \(!selectableModelIds\.has\(createdModel\.modelId\)\) return null;/,
+ /chatModelSelectableId\(\s*createdModel\.modelId,\s*selectableModelIds,?\s*\)/,
);
});
@@ -186,6 +190,87 @@ test("only models that can actually be selected are offered", () => {
}
});
+const HUB = "/home/u/.cache/huggingface/hub";
+const REPO_ID = "unsloth/Repo-GGUF";
+const SNAPSHOT_A = `${HUB}/models--unsloth--Repo-GGUF/snapshots/aaa1111`;
+const SNAPSHOT_B = `${HUB}/models--unsloth--Repo-GGUF/snapshots/bbb2222`;
+
+test("Switch Back resolves a snapshot-path chat to the live picker row", () => {
+ assert.equal(
+ chatModelSelectableId(SNAPSHOT_A, new Set([SNAPSHOT_A, REPO_ID])),
+ SNAPSHOT_A,
+ );
+ assert.equal(chatModelSelectableId(SNAPSHOT_A, new Set([REPO_ID])), REPO_ID);
+ assert.equal(
+ chatModelSelectableId(SNAPSHOT_A, new Set([SNAPSHOT_B])),
+ SNAPSHOT_B,
+ );
+ assert.equal(
+ chatModelSelectableId(SNAPSHOT_A, new Set(["unsloth/Other-GGUF"])),
+ null,
+ );
+ assert.equal(
+ chatModelSelectableId(
+ SNAPSHOT_A,
+ new Set([`${HUB}/models--unsloth--Other-GGUF/snapshots/ccc3333`]),
+ ),
+ null,
+ );
+ // a plain local file collapses to a bare stem, which is no repo
+ assert.equal(
+ chatModelSelectableId(
+ "/srv/models/a/Repo-Q4_K_M.gguf",
+ new Set(["Repo-Q4_K_M"]),
+ ),
+ null,
+ );
+});
+
+test("a snapshot-path chat is already on its repo-id checkpoint", () => {
+ const created = { modelId: SNAPSHOT_A, ggufVariant: "Q4_K_M" };
+ assert.equal(chatModelIsResident(created, REPO_ID, "Q4_K_M"), true);
+ assert.equal(chatModelIsResident(created, SNAPSHOT_B, "Q4_K_M"), true);
+ assert.equal(
+ chatModelIsResident(
+ { modelId: REPO_ID, ggufVariant: "Q4_K_M" },
+ SNAPSHOT_A,
+ "Q4_K_M",
+ ),
+ true,
+ );
+ assert.equal(chatModelIsResident(created, REPO_ID, "Q8_0"), false);
+ assert.equal(
+ chatModelIsResident(created, "unsloth/Other-GGUF", "Q4_K_M"),
+ false,
+ );
+});
+
+test("external and ollama-manifest ids never alias across case", () => {
+ for (const [upper, lower] of [
+ [
+ `external::vendor::${encodeURIComponent("Vendor/Qwen3.8-27B")}`,
+ `external::vendor::${encodeURIComponent("vendor/qwen3.8-27b")}`,
+ ],
+ [
+ `ollama-manifest:${encodeURIComponent("/home/u/.ollama/manifests/Llama")}`,
+ `ollama-manifest:${encodeURIComponent("/home/u/.ollama/manifests/llama")}`,
+ ],
+ ]) {
+ assert.equal(chatModelIsResident({ modelId: upper }, upper, null), true);
+ assert.equal(chatModelIsResident({ modelId: upper }, lower, null), false);
+ assert.equal(chatModelSelectableId(upper, new Set([lower])), null);
+ assert.equal(chatModelSelectableId(upper, new Set([upper])), upper);
+ }
+});
+
+test("a snapshot-path chat is labelled by its repo name, not the revision sha", () => {
+ assert.equal(modelDisplayName(SNAPSHOT_A), "Repo-GGUF");
+ assert.equal(modelDisplayName("org/model.gguf"), "model.gguf");
+ const body = notice.slice(notice.indexOf("export function ChatModelNotice"));
+ assert.match(body, /externalModelLabel\(createdModel\.modelId\)/);
+ assert.match(body, /modelDisplayName\(createdModel\.modelId\)/);
+});
+
test("the notice clears the chat header instead of rendering underneath it", () => {
// The bug this pins: the notice was an in-flow sibling of the chat header, and the
// header is `absolute ... top-[--studio-content-top-inset] z-40` with an OPAQUE
@@ -206,7 +291,10 @@ test("the notice clears the chat header instead of rendering underneath it", ()
assert.match(div, /\babsolute\b/);
// Offset by the SAME header height the header, its fade and the drop overlay use,
// so a change to either variable moves all four together.
- assert.match(div, /top-\[calc\(var\(--studio-content-top-inset,0px\)\+var\(--studio-chat-header-height,48px\)\)\]/);
+ assert.match(
+ div,
+ /top-\[calc\(var\(--studio-content-top-inset,0px\)\+var\(--studio-chat-header-height,48px\)\)\]/,
+ );
// Between the header fade (z-20) and the header itself (z-40): over the gradient,
// under the model picker, whose menu must stay clickable.
const z = /\bz-(\d+)\b/.exec(div);
@@ -217,7 +305,11 @@ test("the notice clears the chat header instead of rendering underneath it", ()
);
// An overlay over the scrolling conversation must be opaque, or messages read
// through it. bg-muted/40 was fine only while the bar took its own row.
- assert.doesNotMatch(div, /bg-muted\//, "a translucent overlay lets messages show through");
+ assert.doesNotMatch(
+ div,
+ /bg-muted\//,
+ "a translucent overlay lets messages show through",
+ );
});
test("the conversation reserves the space the notice overlay takes", () => {
@@ -334,8 +426,15 @@ test("a chat started as New Chat gets the notice once its row exists", () => {
// ?new=<nonce> carries no thread in the URL and keeps none after the first send, so
// the notice saw nothing until the chat was reopened. The store's id is only this
// chat's after ThreadNewChatSwitch has blanked the previous one, hence the latch.
- const gate = slice(page, "const newChatBlankedRef", "const newChatThreadId =");
- assert.match(gate, /activeThreadId === null \|\| isAssistantLocalThreadId\(activeThreadId\)/);
+ const gate = slice(
+ page,
+ "const newChatBlankedRef",
+ "const newChatThreadId =",
+ );
+ assert.match(
+ gate,
+ /activeThreadId === null \|\| isAssistantLocalThreadId\(activeThreadId\)/,
+ );
assert.match(gate, /newChatBlankedRef\.current = search\.new;/);
const derived = slice(page, "const newChatThreadId =", "\n const");
@@ -345,7 +444,10 @@ test("a chat started as New Chat gets the notice once its row exists", () => {
assert.match(derived, /: null/);
const notice = slice(page, "<ChatModelNotice", "/>");
- assert.match(notice, /threadId=\{view\.threadId \?\? newChatThreadId \?\? undefined\}/);
+ assert.match(
+ notice,
+ /threadId=\{view\.threadId \?\? newChatThreadId \?\? undefined\}/,
+ );
});
test("a chat thread keeps the GGUF variant it started on", () => {
@@ -386,7 +488,10 @@ test("a queued empty-model send backfills its resolved GGUF variant", () => {
"../src/features/chat/utils/queued-chat-run-settings.ts",
);
assert.match(queuedSettings, /"activeGgufVariant"/);
- assert.match(thread, /modelGgufVariant: runSettingsAtQueueStart\.activeGgufVariant/);
+ assert.match(
+ thread,
+ /modelGgufVariant: runSettingsAtQueueStart\.activeGgufVariant/,
+ );
});
test("queued model backfill changes only a fresh empty thread row", () => {
@@ -398,7 +503,11 @@ test("queued model backfill changes only a fresh empty thread row", () => {
assert.equal(shouldPersistResolvedQueuedModel(undefined), false);
// The caller has already returned on a queued checkpoint four lines earlier.
assert.match(
- slice(adapter, "const persistResolvedQueuedModel", "if (queuedRunSettings)"),
+ slice(
+ adapter,
+ "const persistResolvedQueuedModel",
+ "if (queuedRunSettings)",
+ ),
/queuedRunSettings\.params\.checkpoint \|\|/,
);
});
@@ -493,7 +602,11 @@ test("switching back to a hub GGUF loads it instead of staging a download", () =
true,
);
// The selection mapping above and the predicate under test are the page's own.
- const built = slice(page, "const selection = {", "await stageOrLoad(selection);");
+ const built = slice(
+ page,
+ "const selection = {",
+ "await stageOrLoad(selection);",
+ );
for (const field of [
"source: meta?.source",
"isLora: meta?.isLora",
@@ -502,7 +615,10 @@ test("switching back to a hub GGUF loads it instead of staging a download", () =
]) {
assert.ok(built.includes(field), `selection lost ${field}`);
}
- assert.match(built, /isDownloaded: meta\?\.isDownloaded \|\| isSameLoadedModel/);
+ assert.match(
+ built,
+ /isDownloaded: meta\?\.isDownloaded \|\| isSameLoadedModel/,
+ );
assert.match(
page,
/const wantManagerStaging = wantsDownloadManagerStaging\(selection\);/,
@@ -618,7 +734,10 @@ test("a legacy GGUF directory does not infer a quant from config alone", () => {
customContextLength: 32768,
}),
);
- assert.equal(resolveChatModelSwitchTarget({ modelId }).ggufVariant, undefined);
+ assert.equal(
+ resolveChatModelSwitchTarget({ modelId }).ggufVariant,
+ undefined,
+ );
});
test("the switch back leaves the remembered config to stageOrLoad", () => {
@@ -644,8 +763,15 @@ test("the switch back leaves the remembered config to stageOrLoad", () => {
slice(switchSource, "import type {", "export type ChatModelSwitchTarget"),
/resolveInitialConfig/,
);
- const stage = slice(page, "const stageOrLoad = useCallback", "useRepoDownload(");
- assert.match(stage, /selection\.config \?\? rememberedConfigFor\(selection\)/);
+ const stage = slice(
+ page,
+ "const stageOrLoad = useCallback",
+ "useRepoDownload(",
+ );
+ assert.match(
+ stage,
+ /selection\.config \?\? rememberedConfigFor\(selection\)/,
+ );
const remembered = slice(
page,
"const rememberedConfigFor = useCallback",