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: stop the chat composer draft mirroring into other open tabs (#15795)

Debojyoti Banerjeec99ccea59b6b
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index bdd165b9593..eb2bc062ecd 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -328,13 +328,13 @@ import {
snapshotQueuedChatRunSettings,
adoptLegacyDraft,
latestDraft,
+ latestPasteDraft,
publishDraft,
subscribeDraft,
composerDraftKey,
composerPasteDraftKey,
createPastedTextFile,
pastedTextOf,
- readPasteDraft,
writePasteDraft,
markThreadIncognito,
markChatThreadDeleted,
@@ -3933,7 +3933,7 @@ const Composer: FC<{
const incognito = useChatRuntimeStore((s) => s.incognito);
const draftScope = incognito ? INCOGNITO_DRAFT_SCOPE : referenceThreadId;
const draftKey = composerDraftKey();
- // Text that came from Compare or another tab, so saving it back can't overwrite a newer write.
+ // Text that came from Compare, so saving it back can't overwrite a newer write.
const remoteDraftRef = useRef<string | null>(null);
const [draftSource] = useState(() => Symbol("chat composer"));
// Whose pastes are on screen: a temporary chat's never cross into the saved draft, nor back.
@@ -3971,7 +3971,7 @@ const Composer: FC<{
}
composer.setText(draft);
}, [draftScope, draftKey, aui]);
- // Compare's composer or another tab, so whichever is on screen shows the latest draft.
+ // Compare's composer, so whichever is on screen shows the latest draft.
useEffect(() => {
if (draftScope === INCOGNITO_DRAFT_SCOPE) return;
return subscribeDraft(draftSource, (text) => {
@@ -4028,7 +4028,7 @@ const Composer: FC<{
const text = pastedTextOf((attachment as { file?: File }).file);
return text === undefined ? [] : [text];
});
- const missing = readPasteDraft(pasteDraftKey).filter((text) => {
+ const missing = latestPasteDraft().filter((text) => {
const at = shown.indexOf(text);
if (at === -1) return true;
shown.splice(at, 1);
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index b7a61ce0fc2..807a325004e 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -525,6 +525,7 @@ export {
export {
adoptLegacyDraft,
latestDraft,
+ latestPasteDraft,
publishDraft,
subscribeDraft,
composerDraftKey,
diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsx
index 542bffe8420..938ba86e67c 100644
--- a/studio/frontend/src/features/chat/shared-composer.tsx
+++ b/studio/frontend/src/features/chat/shared-composer.tsx
@@ -637,7 +637,7 @@ export function SharedComposer({
useChatRuntimeStore.getState().incognito ? "" : latestDraft(),
);
const [draftSource] = useState(() => Symbol("compare composer"));
- // Text that came from the chat composer or another tab, so it isn't published back.
+ // Text that came from the chat composer, so it isn't published back.
const remoteDraftRef = useRef<string | null>(null);
const unsavedDraftRef = useRef<string | null>(null);
const incognitoBeforeRef = useRef(incognito);
diff --git a/studio/frontend/src/features/chat/utils/composer-draft.ts b/studio/frontend/src/features/chat/utils/composer-draft.ts
index 8a3140cad3b..249f4484bfb 100644
--- a/studio/frontend/src/features/chat/utils/composer-draft.ts
+++ b/studio/frontend/src/features/chat/utils/composer-draft.ts
@@ -36,8 +36,16 @@ export function readPasteDraft(key: string): string[] {
}
}
+// This tab's pastes once it saves any, as `latest` is for its text.
+let latestPastes: string[] | null = null;
+
+export function latestPasteDraft(): string[] {
+ return latestPastes !== null ? [...latestPastes] : readPasteDraft(PASTE_DRAFT_KEY);
+}
+
// A quota error here leaves the text draft untouched, hence separate slots.
export function writePasteDraft(key: string, pastes: readonly string[]): void {
+ if (key === PASTE_DRAFT_KEY) latestPastes = [...pastes];
try {
if (pastes.length > 0) {
window.localStorage.setItem(key, JSON.stringify(pastes));
@@ -89,6 +97,8 @@ export function adoptLegacyDraft(threadId: string | null): boolean {
throw error;
}
adopted = true;
+ seeded = null;
+ latestPastes = null;
}
storage.removeItem(`${LEGACY_DRAFT_PREFIX}${id}`);
storage.removeItem(`${LEGACY_PASTE_DRAFT_PREFIX}${id}`);
@@ -98,16 +108,18 @@ export function adoptLegacyDraft(threadId: string | null): boolean {
}
}
-// The chat composer stays mounted, hidden, under Compare's, and localStorage tells only other tabs
-// about a write. So every composer publishes its text here as it changes, and hears the others and
-// other tabs here too; `latestDraft` is ahead of the debounced save.
+// The chat composer stays mounted, hidden, under Compare's, so every composer publishes its text
+// here and hears the others; `latestDraft` is ahead of the debounced save. Other open tabs are
+// never heard (#15782): the saved slot only seeds a tab once, for a reload or a new tab.
type DraftListener = (text: string, source: symbol) => void;
const draftListeners = new Set<DraftListener>();
let latest: string | null = null;
-const OTHER_TAB = Symbol("other tab");
+let seeded: string | null = null;
export function latestDraft(): string {
- return latest ?? readComposerDraft(DRAFT_KEY) ?? "";
+ if (latest !== null) return latest;
+ seeded ??= readComposerDraft(DRAFT_KEY) ?? "";
+ return seeded;
}
export function publishDraft(text: string, source: symbol): void {
@@ -124,12 +136,6 @@ export function subscribeDraft(source: symbol, listener: (text: string) => void)
return () => draftListeners.delete(wrapped);
}
-if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
- window.addEventListener("storage", (event) => {
- if (event.key === DRAFT_KEY) publishDraft(event.newValue ?? "", OTHER_TAB);
- });
-}
-
// A deleted chat's leftover per-chat draft; never the shared one.
export function clearComposerDraft(threadId: string): void {
try {
diff --git a/studio/frontend/tests/composer-paste-draft.test.ts b/studio/frontend/tests/composer-paste-draft.test.ts
index e53c8edc776..a66c23d5266 100644
--- a/studio/frontend/tests/composer-paste-draft.test.ts
+++ b/studio/frontend/tests/composer-paste-draft.test.ts
@@ -6,8 +6,13 @@ import test from "node:test";
const store = new Map<string, string>();
let failWrites = false;
+const windowListeners = new Map<string, ((event: unknown) => void)[]>();
(globalThis as { window?: unknown }).window = {
+ addEventListener: (type: string, listener: (event: unknown) => void) => {
+ windowListeners.set(type, [...(windowListeners.get(type) ?? []), listener]);
+ },
+ removeEventListener: () => {},
localStorage: {
getItem: (key: string) => store.get(key) ?? null,
setItem: (key: string, value: string) => {
@@ -101,6 +106,60 @@ test("composers hear each other's text, never their own", () => {
assert.equal(latestDraft(), "after stop");
});
+// A fresh module stands in for a newly opened tab or a reload.
+const openTab = () =>
+ import(`../src/features/chat/utils/composer-draft.ts?tab=${Math.random()}`);
+
+test("a new tab or a reload starts from the saved draft", async () => {
+ writeComposerDraft(composerDraftKey(), "saved before the reload");
+ const tab = await openTab();
+ assert.equal(tab.latestDraft(), "saved before the reload");
+});
+
+test("typing or sending in another tab never rewrites this tab's draft", async () => {
+ const tab = await openTab();
+ assert.equal(tab.latestDraft(), "", "nothing saved when the tab opened");
+ const heard: string[] = [];
+ const stop = tab.subscribeDraft(Symbol("chat"), (text: string) => heard.push(text));
+ // The other tab's debounced save, then its send clearing the slot; the browser
+ // tells this tab about each with a storage event.
+ const otherTabWrites = (text: string) => {
+ writeComposerDraft(composerDraftKey(), text);
+ for (const listener of windowListeners.get("storage") ?? []) {
+ listener({ key: composerDraftKey(), newValue: text === "" ? null : text });
+ }
+ };
+ otherTabWrites("typed in the other tab");
+ assert.equal(tab.latestDraft(), "", "a chat switch here does not pick it up");
+ otherTabWrites("");
+ stop();
+ assert.deepEqual(heard, []);
+});
+
+test("this tab keeps its own text after another tab saves", async () => {
+ const tab = await openTab();
+ tab.publishDraft("mine", Symbol("chat"));
+ writeComposerDraft(composerDraftKey(), "theirs");
+ assert.equal(tab.latestDraft(), "mine");
+});
+
+test("a new tab starts from the saved pastes, then keeps its own", async () => {
+ writePasteDraft(composerPasteDraftKey(), ["saved paste"]);
+ const tab = await openTab();
+ assert.deepEqual(tab.latestPasteDraft(), ["saved paste"]);
+ tab.writePasteDraft(tab.composerPasteDraftKey(), []);
+ writePasteDraft(composerPasteDraftKey(), ["pasted in the other tab"]);
+ assert.deepEqual(tab.latestPasteDraft(), [], "a chat switch here does not pick it up");
+});
+
+test("an adopted per-chat paste reaches the restore", async () => {
+ const tab = await openTab();
+ tab.writePasteDraft(tab.composerPasteDraftKey(), []);
+ store.set("chat-draft-pastes:t1", '["old paste"]');
+ assert.equal(tab.adoptLegacyDraft("t1"), true);
+ assert.deepEqual(tab.latestPasteDraft(), ["old paste"]);
+});
+
test("a failed adoption keeps the old draft rather than losing it", () => {
store.set("chat-draft:t1", "unsent");
failWrites = true;