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: remember chat preferences per account across logins (#15025)

Nilay1052e93d9f51
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.ts
index 2da8659fa43..dcff9a82f87 100644
--- a/studio/frontend/scripts/check-bundle-budget.ts
+++ b/studio/frontend/scripts/check-bundle-budget.ts
@@ -415,8 +415,9 @@ export const BUDGET = {
// Language and platform tabs on the API page examples (#15084) add 138 B raw / 53 B transfer: Vite's preload map
// in the entry and settings chunks naming the example box, now a lazy chunk shared by the API page and Settings
// (6,932,829 -> 6,932,967 B raw, 2,088,616 -> 2,088,669 B transfer against cfc593f0bd, Node 26, 99 chunks).
- transferBytes: 2_088_701,
- rawBytes: 6_933_109,
+ // Per-account chat preferences (#15025): +338 B raw / +93 B transfer over the line above (Node 22.23.2, 99 chunks).
+ transferBytes: 2_088_800,
+ rawBytes: 6_933_450,
};
// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering the
diff --git a/studio/frontend/src/lib/account-transition.ts b/studio/frontend/src/lib/account-transition.ts
index 67605df8e27..c2026791195 100644
--- a/studio/frontend/src/lib/account-transition.ts
+++ b/studio/frontend/src/lib/account-transition.ts
@@ -21,6 +21,40 @@ export const OWNER_BROWSER_ACCOUNT = "unsloth";
export const APPEARANCE_KEY = "unsloth_appearance_customization";
+const CHAT_PREFERENCES_KEY = "unsloth_chat_preferences";
+// Only chat UI preferences survive here, never credentials, drafts or chat content.
+const ACCOUNT_CHAT_PREFERENCES_PREFIX = "unsloth.account-chat-preferences.v1:";
+
+function chatPreferencesKey(account: MarkedAccount): string {
+ const identity = isOwnerAccount(account)
+ ? ["id", "owner"]
+ : account.accountId
+ ? ["id", account.accountId]
+ : ["username", account.username];
+ return ACCOUNT_CHAT_PREFERENCES_PREFIX + JSON.stringify(identity);
+}
+
+/** Keep the active key compatible with the preference store, migrations and Reset preferences. */
+function switchChatPreferences(
+ storage: Storage,
+ previous: MarkedAccount,
+ next: MarkedAccount,
+): void {
+ try {
+ const current = storage.getItem(CHAT_PREFERENCES_KEY);
+ const saved = storage.getItem(chatPreferencesKey(next));
+ if (current === null) {
+ if (storage.getItem(chatPreferencesKey(previous)) !== null)
+ storage.removeItem(chatPreferencesKey(previous));
+ } else storage.setItem(chatPreferencesKey(previous), current);
+ if (saved === null && current !== null) storage.removeItem(CHAT_PREFERENCES_KEY);
+ else if (saved !== null) storage.setItem(CHAT_PREFERENCES_KEY, saved);
+ } catch {
+ // Optional, so a full storage never fails a sign-in; defaults beat the previous account's.
+ storage.removeItem(CHAT_PREFERENCES_KEY);
+ }
+}
+
/** Browser chrome only. Never add credentials, content, model choices or profile data. */
export const ACCOUNT_CHROME_KEYS = new Set([
"theme",
@@ -82,13 +116,18 @@ export function accountDatabaseName(
const account = parseAccountMarker(
storage?.getItem(BROWSER_ACCOUNT_KEY) ?? OWNER_BROWSER_ACCOUNT,
);
- const owner =
- account.accountId === "owner" ||
- (!account.accountId && account.username === OWNER_BROWSER_ACCOUNT);
- return owner ? name : `${name}:${account.accountId ?? account.username}`;
+ return isOwnerAccount(account)
+ ? name
+ : `${name}:${account.accountId ?? account.username}`;
}
type MarkedAccount = { accountId: string | null; username: string };
+function isOwnerAccount(account: MarkedAccount): boolean {
+ return (
+ account.accountId === "owner" ||
+ (!account.accountId && account.username === OWNER_BROWSER_ACCOUNT)
+ );
+}
function parseAccountMarker(marker: string): MarkedAccount {
const legacy = {
accountId: null,
@@ -202,12 +241,11 @@ export async function transitionBrowserAccount(
): Promise<boolean> {
const marker = browserAccountMarker(account);
const storage = browser.localStorage;
- const changed = !isSameAccount(
- parseAccountMarker(
- storage.getItem(BROWSER_ACCOUNT_KEY) ?? OWNER_BROWSER_ACCOUNT,
- ),
- parseAccountMarker(marker),
+ const previous = parseAccountMarker(
+ storage.getItem(BROWSER_ACCOUNT_KEY) ?? OWNER_BROWSER_ACCOUNT,
);
+ const next = parseAccountMarker(marker);
+ const changed = !isSameAccount(previous, next);
if (changed) {
// First, as it can fail: a failure then leaves this account's data in place, not half purged.
await clearSiteData();
@@ -218,6 +256,8 @@ export async function transitionBrowserAccount(
if (
!key ||
key === BROWSER_ACCOUNT_KEY ||
+ key === CHAT_PREFERENCES_KEY ||
+ key.startsWith(ACCOUNT_CHAT_PREFERENCES_PREFIX) ||
ACCOUNT_CHROME_KEYS.has(key) ||
// About this machine's install, not the account signed in to it.
key === RUNTIME_REPAIR_KEY ||
@@ -241,6 +281,8 @@ export async function transitionBrowserAccount(
commitSession();
if (storage.getItem(BROWSER_ACCOUNT_KEY) !== marker)
storage.setItem(BROWSER_ACCOUNT_KEY, marker);
+ // After the marker: until then the active preferences stay the account it names, so a retry is safe.
+ if (changed) switchChatPreferences(storage, previous, next);
if (changed) storage.removeItem(BROWSER_ACCOUNT_FENCE_KEY);
if (changed) browser.location.replace(postAuthRoute);
return changed;
diff --git a/studio/frontend/tests/account-transition.test.ts b/studio/frontend/tests/account-transition.test.ts
index ab90b1d10cd..cd180a5ee07 100644
--- a/studio/frontend/tests/account-transition.test.ts
+++ b/studio/frontend/tests/account-transition.test.ts
@@ -631,3 +631,120 @@ test("transcript recovery survives reauthentication but is cleared on account ch
}
}
});
+
+test("chat preferences survive account switches without being shared", async () => {
+ const alice = { username: "alice", accountId: "alice-id" };
+ const bob = { username: "bob", accountId: "bob-id" };
+ const b = browserWith({ [BROWSER_ACCOUNT_KEY]: browserAccountMarker(alice) });
+ const originalWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
+ Object.defineProperty(globalThis, "window", { configurable: true, value: b.browser });
+ const load = async (name: string) => {
+ const { useChatPreferencesStore } = await import(
+ `../src/features/chat/stores/chat-preferences-store.ts?account-test=${name}`
+ );
+ return useChatPreferencesStore;
+ };
+ const choices = (store: Awaited<ReturnType<typeof load>>) => {
+ const state = store.getState();
+ return [state.sendShortcut, state.autoScrollWhileGenerating, state.thinkingVisibility];
+ };
+ try {
+ const a = await load("alice");
+ a.getState().setSendShortcut("mod-enter");
+ a.getState().setAutoScrollWhileGenerating(false);
+ a.getState().setThinkingVisibility("collapsed");
+ assert.deepEqual(choices(await load("alice-reload")), ["mod-enter", false, "collapsed"]);
+ await transitionBrowserAccount(bob, "/chat", () => {}, b.browser);
+ const other = await load("bob");
+ assert.deepEqual(choices(other), ["enter", true, "auto"]);
+ other.getState().setThinkingVisibility("expanded");
+ await transitionBrowserAccount(alice, "/chat", () => {}, b.browser);
+ assert.deepEqual(choices(await load("alice-return")), ["mod-enter", false, "collapsed"]);
+ await transitionBrowserAccount(bob, "/chat", () => {}, b.browser);
+ assert.deepEqual(choices(await load("bob-return")), ["enter", true, "expanded"]);
+ // Reset preferences removes the active entry; the old snapshot must not resurrect it.
+ b.data.delete("unsloth_chat_preferences");
+ await transitionBrowserAccount(alice, "/chat", () => {}, b.browser);
+ await transitionBrowserAccount(bob, "/chat", () => {}, b.browser);
+ assert.deepEqual(choices(await load("bob-reset")), ["enter", true, "auto"]);
+ } finally {
+ if (originalWindow) Object.defineProperty(globalThis, "window", originalWindow);
+ else Reflect.deleteProperty(globalThis, "window");
+ }
+});
+
+test("chat preference snapshots follow account IDs, including rename and recreation", async () => {
+ const saved = JSON.stringify({ state: { sendShortcut: "mod-enter" }, version: 1 });
+ const alice = { username: "alice", accountId: "alice-id" };
+ const bob = { username: "bob", accountId: "bob-id" };
+ const b = browserWith({
+ [BROWSER_ACCOUNT_KEY]: browserAccountMarker(alice),
+ unsloth_chat_preferences: saved,
+ });
+ await transitionBrowserAccount(bob, "/chat", () => {}, b.browser);
+ await transitionBrowserAccount({ username: "renamed", accountId: "alice-id" }, "/chat", () => {}, b.browser);
+ assert.equal(b.data.get("unsloth_chat_preferences"), saved);
+ await transitionBrowserAccount(bob, "/chat", () => {}, b.browser);
+ await transitionBrowserAccount({ username: "alice", accountId: "new-alice-id" }, "/chat", () => {}, b.browser);
+ assert.equal(b.data.has("unsloth_chat_preferences"), false);
+});
+
+test("owner preferences survive the legacy marker upgrade", async () => {
+ const saved = JSON.stringify({ state: { thinkingVisibility: "collapsed" }, version: 1 });
+ const b = browserWith({ unsloth_chat_preferences: saved });
+ await transitionBrowserAccount("alice", "/chat", () => {}, b.browser);
+ await transitionBrowserAccount({ username: "unsloth", accountId: "owner" }, "/chat", () => {}, b.browser);
+ assert.equal(b.data.get("unsloth_chat_preferences"), saved);
+});
+
+test("failed account cleanup leaves chat preferences available for a retry", async () => {
+ const saved = JSON.stringify({ state: { sendShortcut: "mod-enter" }, version: 1 });
+ const b = browserWith({
+ [BROWSER_ACCOUNT_KEY]: "account:alice-id:alice",
+ unsloth_chat_preferences: saved,
+ }, "blocked");
+ await assert.rejects(transitionBrowserAccount(
+ { username: "bob", accountId: "bob-id" }, "/chat", () => {}, b.browser,
+ ));
+ assert.equal(b.data.get("unsloth_chat_preferences"), saved);
+});
+
+test("a failed session publication keeps each account's chat preferences its own", async () => {
+ const alicePrefs = JSON.stringify({ state: { sendShortcut: "mod-enter" }, version: 1 });
+ const bobPrefs = JSON.stringify({ state: { sendShortcut: "mod-enter-multiline" }, version: 1 });
+ const alice = { username: "alice", accountId: "alice-id" };
+ const bob = { username: "bob", accountId: "bob-id" };
+ const b = browserWith({ [BROWSER_ACCOUNT_KEY]: browserAccountMarker(alice) });
+ await transitionBrowserAccount(bob, "/chat", () => {}, b.browser);
+ b.data.set("unsloth_chat_preferences", bobPrefs);
+ await transitionBrowserAccount(alice, "/chat", () => {}, b.browser);
+ b.data.set("unsloth_chat_preferences", alicePrefs);
+ await assert.rejects(transitionBrowserAccount(bob, "/chat", () => {
+ throw new Error("token storage failed");
+ }, b.browser));
+ assert.equal(b.data.get("unsloth_chat_preferences"), alicePrefs);
+ await transitionBrowserAccount(bob, "/chat", () => {}, b.browser);
+ assert.equal(b.data.get("unsloth_chat_preferences"), bobPrefs);
+ await transitionBrowserAccount(alice, "/chat", () => {}, b.browser);
+ assert.equal(b.data.get("unsloth_chat_preferences"), alicePrefs);
+});
+
+test("a full storage never blocks an account switch or leaks preferences", async () => {
+ const alicePrefs = JSON.stringify({ state: { sendShortcut: "mod-enter" }, version: 1 });
+ const b = browserWith({
+ [BROWSER_ACCOUNT_KEY]: "account:alice-id:alice",
+ unsloth_chat_preferences: alicePrefs,
+ });
+ const setItem = b.browser.localStorage.setItem;
+ b.browser.localStorage.setItem = (key: string, value: string) => {
+ if (key.startsWith("unsloth.account-chat-preferences.v1:"))
+ throw new DOMException("full", "QuotaExceededError");
+ setItem(key, value);
+ };
+ assert.equal(
+ await transitionBrowserAccount({ username: "bob", accountId: "bob-id" }, "/chat", () => {}, b.browser),
+ true,
+ );
+ assert.equal(b.data.get(BROWSER_ACCOUNT_KEY), "account:bob-id:bob");
+ assert.equal(b.data.has("unsloth_chat_preferences"), false);
+});