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 replaced replies out of ShareGPT exports (#10941)

Nilay74e732e79273
diff --git a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
index f4ab5a97592..b2b3858cb9b 100644
--- a/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
+++ b/studio/frontend/src/features/chat/prompt-storage/prompt-storage-dialog.tsx
@@ -94,6 +94,7 @@ import {
type ConversationJsonlLayout,
} from "../utils/ndjson";
import { orderByParentChain } from "../utils/message-order";
+import { liveThreadBranch } from "../utils/live-thread-head";
import { unwrapPastedTextContent } from "../utils/pasted-text.ts";
import {
buildConversationMarkdown,
@@ -237,6 +238,8 @@ async function loadConversationMessages(
emptyMessage = "No messages in this conversation to export.",
includeSiblings = true,
} = options;
+ // Read before the storage await: switching chats meanwhile would point the lookup at another thread.
+ const liveBranch = liveThreadBranch(threadId);
const raw = await listStoredChatMessages(threadId);
if (raw.length === 0) {
toast.info(emptyMessage);
@@ -245,7 +248,12 @@ async function loadConversationMessages(
// No parentId = legacy flat thread (already DB createdAt-sorted); walking the chain would invert order.
const hasParentIds = raw.some((m) => (m as { parentId?: unknown }).parentId != null);
if (!hasParentIds) return raw;
- return orderByParentChain(raw, { includeSiblings }) as typeof raw;
+ // Newest saved turn of the branch on screen: a reply still generating is not stored yet, and falling back to the newest leaf would export the reply it replaces.
+ const storedIds = new Set(raw.map((m) => m.id));
+ const headId = liveBranch
+ ? ([...liveBranch].reverse().find((id) => storedIds.has(id)) ?? null)
+ : undefined;
+ return orderByParentChain(raw, { includeSiblings, headId }) as typeof raw;
}
function exportTs(): string {
@@ -390,7 +398,9 @@ function messageToOpenAI(msg: { role: unknown; content: unknown; attachments?: u
// ShareGPT training JSONL (human/system/gpt turns).
export async function exportConversationShareGPT(threadId: string): Promise<void> {
- const messages = await loadConversationMessages(threadId);
+ const messages = await loadConversationMessages(threadId, {
+ includeSiblings: exportFormatIncludesSiblings("sharegpt"),
+ });
if (!messages) return;
const conversations: Array<{ from: string; value: string }> = [];
diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsx
index 89ca5735795..9f2b5a10cd8 100644
--- a/studio/frontend/src/features/chat/runtime-provider.tsx
+++ b/studio/frontend/src/features/chat/runtime-provider.tsx
@@ -129,6 +129,7 @@ import {
} from "./utils/chat-generation-recovery";
import { createGenerationToolRecovery } from "./utils/generation-tool-recovery";
import { mergeContextTruncation } from "./utils/context-truncation";
+import { registerLiveThreadView } from "./utils/live-thread-head";
import {
extractDeltaText,
parseAssistantContent,
@@ -1651,6 +1652,8 @@ function useStudioRuntimeAdapters(
const signalReady = useAppShellReadySignal();
const aui = useAui();
+ useEffect(() => registerLiveThreadView(aui), [aui]);
+
useEffect(() => {
const recoverCurrentThread = () => {
const remoteId = aui.threadListItem().getState().remoteId;
diff --git a/studio/frontend/src/features/chat/utils/live-thread-head.ts b/studio/frontend/src/features/chat/utils/live-thread-head.ts
new file mode 100644
index 00000000000..21171ec899a
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/live-thread-head.ts
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+type LiveThreadView = {
+ threadListItem: () => { getState: () => { remoteId?: string | null } };
+ thread: () => {
+ getState: () => { messages: ReadonlyArray<{ id: string }> };
+ };
+};
+
+const views = new Set<LiveThreadView>();
+
+export function registerLiveThreadView(view: LiveThreadView): () => void {
+ views.add(view);
+ return () => {
+ views.delete(view);
+ };
+}
+
+// The branch picker moves the head only in memory, so storage alone cannot say which branch is on screen.
+export function liveThreadBranch(threadId: string): string[] | null {
+ for (const view of views) {
+ try {
+ if (view.threadListItem().getState().remoteId !== threadId) continue;
+ return view.thread().getState().messages.map((message) => message.id);
+ } catch {
+ // A view torn down mid-switch has no thread to read.
+ }
+ }
+ return null;
+}
diff --git a/studio/frontend/src/features/chat/utils/message-order.ts b/studio/frontend/src/features/chat/utils/message-order.ts
index 46a3dc5f805..ded3c3d2c80 100644
--- a/studio/frontend/src/features/chat/utils/message-order.ts
+++ b/studio/frontend/src/features/chat/utils/message-order.ts
@@ -31,6 +31,7 @@ export function createParentResolver(): (
export function orderBySelectedBranch<T extends ParentLinkedMessage>(
messages: T[],
+ headId?: string | null,
): T[] {
const sorted = messages.slice().sort((a, b) => {
const createdAtDelta = (a.createdAt ?? 0) - (b.createdAt ?? 0);
@@ -55,7 +56,9 @@ export function orderBySelectedBranch<T extends ParentLinkedMessage>(
const chain: T[] = [];
const seen = new Set<string>();
- let currentId: string | null = sorted.at(-1)?.id ?? null;
+ // undefined follows the newest message; null is a live branch with nothing saved yet.
+ let currentId: string | null =
+ headId === undefined ? (sorted.at(-1)?.id ?? null) : headId;
while (currentId != null && !seen.has(currentId)) {
seen.add(currentId);
const message = byId.get(currentId);
@@ -71,11 +74,11 @@ export function orderBySelectedBranch<T extends ParentLinkedMessage>(
// follow the newest parent chain because response slots can predate the next user message.
export function orderByParentChain<T extends ParentLinkedMessage>(
messages: T[],
- options: { includeSiblings?: boolean } = {},
+ options: { includeSiblings?: boolean; headId?: string | null } = {},
): T[] {
- const { includeSiblings = true } = options;
+ const { includeSiblings = true, headId } = options;
if (!includeSiblings) {
- return orderBySelectedBranch(messages);
+ return orderBySelectedBranch(messages, headId);
}
const byId = new Map<string, T>(
messages.map((message) => [message.id, message]),
diff --git a/studio/frontend/src/features/chat/utils/ndjson.ts b/studio/frontend/src/features/chat/utils/ndjson.ts
index 1ee0f7004ab..339ba00bb11 100644
--- a/studio/frontend/src/features/chat/utils/ndjson.ts
+++ b/studio/frontend/src/features/chat/utils/ndjson.ts
@@ -13,7 +13,11 @@ export function canMergeConversationExport(format: string): boolean {
}
export function exportFormatIncludesSiblings(format: string): boolean {
- return format !== "jsonl-raw" && format !== "jsonl-messages";
+ return (
+ format !== "jsonl-raw" &&
+ format !== "jsonl-messages" &&
+ format !== "sharegpt"
+ );
}
const OPENAI_MESSAGE_ROLES = new Set([
diff --git a/studio/frontend/tests/ndjson-body.test.ts b/studio/frontend/tests/ndjson-body.test.ts
index 132e1f65976..af7a50d9905 100644
--- a/studio/frontend/tests/ndjson-body.test.ts
+++ b/studio/frontend/tests/ndjson-body.test.ts
@@ -78,11 +78,11 @@ test("message JSONL cannot merge conversations without losing boundaries", () =>
assert.equal(canMergeConversationExport("jsonl-raw"), true);
});
-test("both JSONL layouts export only the displayed branch", () => {
+test("training formats export only the displayed branch", () => {
assert.equal(exportFormatIncludesSiblings("jsonl-raw"), false);
assert.equal(exportFormatIncludesSiblings("jsonl-messages"), false);
+ assert.equal(exportFormatIncludesSiblings("sharegpt"), false);
assert.equal(exportFormatIncludesSiblings("csv"), true);
- assert.equal(exportFormatIncludesSiblings("sharegpt"), true);
});
test("training order excludes abandoned response branches", () => {
diff --git a/studio/frontend/tests/sharegpt-export-branches.test.ts b/studio/frontend/tests/sharegpt-export-branches.test.ts
new file mode 100644
index 00000000000..737f94d969f
--- /dev/null
+++ b/studio/frontend/tests/sharegpt-export-branches.test.ts
@@ -0,0 +1,238 @@
+// 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 test from "node:test";
+import vm from "node:vm";
+import ts from "typescript";
+
+import * as liveThreadHead from "../src/features/chat/utils/live-thread-head.ts";
+import { orderByParentChain } from "../src/features/chat/utils/message-order.ts";
+import {
+ exportFormatIncludesSiblings,
+ ndjsonBody,
+} from "../src/features/chat/utils/ndjson.ts";
+import { readSrc } from "./helpers/kit.ts";
+
+type StoredMessage = {
+ id: string;
+ parentId: string | null;
+ createdAt: number;
+ role: string;
+ content: string;
+};
+
+type Exporters = {
+ exportConversationShareGPT: (threadId: string) => Promise<void>;
+ buildThreadContent: (
+ threadId: string,
+ format: string,
+ ) => Promise<string | null>;
+};
+
+const SOURCE = readSrc(
+ "features/chat/prompt-storage/prompt-storage-dialog.tsx",
+);
+
+function sliceSource(startMarker: string, endMarker: string): string {
+ const start = SOURCE.indexOf(startMarker);
+ const end = SOURCE.indexOf(endMarker, start);
+ assert.notEqual(start, -1, `${startMarker} must exist`);
+ assert.notEqual(end, -1, `${endMarker} must exist`);
+ return SOURCE.slice(start, end);
+}
+
+function storedMessages(
+ rows: [id: string, parentId: string | null, role: string, content: string][],
+): StoredMessage[] {
+ return rows.map(([id, parentId, role, content], index) => ({
+ id,
+ parentId,
+ createdAt: index + 1,
+ role,
+ content,
+ }));
+}
+
+function loadExporters(
+ stored: StoredMessage[],
+ downloads: string[] = [],
+ onRead: () => void = () => {},
+) {
+ const javascript = ts.transpileModule(
+ [
+ sliceSource(
+ "async function loadConversationMessages(",
+ "function exportTs(",
+ ),
+ sliceSource("// ShareGPT training JSONL", "// OpenAI/ChatML JSONL"),
+ sliceSource("async function buildThreadContent(", "function csvHeader("),
+ "globalThis.__exporters = { exportConversationShareGPT, buildThreadContent };",
+ ].join("\n"),
+ {
+ compilerOptions: {
+ module: ts.ModuleKind.CommonJS,
+ target: ts.ScriptTarget.ES2022,
+ },
+ },
+ ).outputText;
+ const context = {
+ exports: {},
+ toast: { info: () => {} },
+ listStoredChatMessages: async () => {
+ onRead();
+ return stored;
+ },
+ ...liveThreadHead,
+ orderByParentChain,
+ exportFormatIncludesSiblings,
+ ndjsonBody,
+ messageToText: (message: StoredMessage) => message.content,
+ csvEscape: (value: string) => value,
+ exportTs: () => "ts",
+ downloadBlob: async (body: string) => {
+ downloads.push(body);
+ },
+ } as Record<string, unknown>;
+ vm.runInNewContext(javascript, context);
+ return context.__exporters as Exporters;
+}
+
+async function withLiveBranch<T>(
+ ids: string[] | null,
+ run: () => Promise<T>,
+): Promise<T> {
+ if (!ids) return run();
+ const unregister = liveThreadHead.registerLiveThreadView({
+ threadListItem: () => ({ getState: () => ({ remoteId: "thread" }) }),
+ thread: () => ({
+ getState: () => ({ messages: ids.map((id) => ({ id })) }),
+ }),
+ });
+ try {
+ return await run();
+ } finally {
+ unregister();
+ }
+}
+
+async function shareGptExport(
+ stored: StoredMessage[],
+ liveBranch: string[] | null = null,
+) {
+ const downloads: string[] = [];
+ const exporters = loadExporters(stored, downloads);
+ return withLiveBranch(liveBranch, async () => {
+ await exporters.exportConversationShareGPT("thread");
+ const bulk = await exporters.buildThreadContent("thread", "sharegpt");
+ return { downloads, bulk };
+ });
+}
+
+async function shareGptConversations(
+ stored: StoredMessage[],
+ liveBranch: string[] | null = null,
+) {
+ const { downloads, bulk } = await shareGptExport(stored, liveBranch);
+ assert.ok(bulk);
+ assert.deepEqual(downloads, [`${bulk}\n`]);
+ return JSON.parse(bulk).conversations;
+}
+
+const regenerated = storedMessages([
+ ["u1", null, "user", "Name one fruit."],
+ ["a1", "u1", "assistant", "Apples."],
+ ["a1-retry", "u1", "assistant", "Apples!"],
+]);
+
+test("ShareGPT exports only the regenerated reply", async () => {
+ assert.deepEqual(await shareGptConversations(regenerated), [
+ { from: "human", value: "Name one fruit." },
+ { from: "gpt", value: "Apples!" },
+ ]);
+});
+
+test("ShareGPT exports only the edited prompt and its reply", async () => {
+ const edited = storedMessages([
+ ["u1", null, "user", "Name one color."],
+ ["a1", "u1", "assistant", "Blue."],
+ ["u1-edit", null, "user", "Name one animal."],
+ ["a1-edit", "u1-edit", "assistant", "Cat."],
+ ]);
+ assert.deepEqual(await shareGptConversations(edited), [
+ { from: "human", value: "Name one animal." },
+ { from: "gpt", value: "Cat." },
+ ]);
+});
+
+test("ShareGPT exports the branch picked in the branch picker", async () => {
+ assert.deepEqual(await shareGptConversations(regenerated, ["u1", "a1"]), [
+ { from: "human", value: "Name one fruit." },
+ { from: "gpt", value: "Apples." },
+ ]);
+ const edited = storedMessages([
+ ["u1", null, "user", "Name one color."],
+ ["a1", "u1", "assistant", "Blue."],
+ ["u1-edit", null, "user", "Name one animal."],
+ ["a1-edit", "u1-edit", "assistant", "Cat."],
+ ]);
+ assert.deepEqual(await shareGptConversations(edited, ["u1", "a1"]), [
+ { from: "human", value: "Name one color." },
+ { from: "gpt", value: "Blue." },
+ ]);
+});
+
+test("ShareGPT leaves out the replaced reply while its regeneration is unsaved", async () => {
+ const saved = storedMessages([
+ ["u1", null, "user", "Name one fruit."],
+ ["a1", "u1", "assistant", "Apples."],
+ ]);
+ assert.deepEqual(await shareGptConversations(saved, ["u1", "a1-retry"]), [
+ { from: "human", value: "Name one fruit." },
+ ]);
+});
+
+test("ShareGPT exports nothing from the replaced branch while an edited first prompt is unsaved", async () => {
+ const saved = storedMessages([
+ ["u1", null, "user", "Name one color."],
+ ["a1", "u1", "assistant", "Blue."],
+ ]);
+ const { downloads, bulk } = await shareGptExport(saved, [
+ "u1-edit",
+ "a1-edit",
+ ]);
+ assert.deepEqual(downloads, []);
+ assert.equal(bulk, null);
+});
+
+test("ShareGPT keeps the picked branch when the chat is switched during the export", async () => {
+ const downloads: string[] = [];
+ let unregister = () => {};
+ const exporters = loadExporters(regenerated, downloads, () => unregister());
+ unregister = liveThreadHead.registerLiveThreadView({
+ threadListItem: () => ({ getState: () => ({ remoteId: "thread" }) }),
+ thread: () => ({
+ getState: () => ({ messages: [{ id: "u1" }, { id: "a1" }] }),
+ }),
+ });
+ try {
+ await exporters.exportConversationShareGPT("thread");
+ } finally {
+ unregister();
+ }
+ assert.deepEqual(JSON.parse(downloads[0]).conversations, [
+ { from: "human", value: "Name one fruit." },
+ { from: "gpt", value: "Apples." },
+ ]);
+});
+
+test("CSV still exports every branch", async () => {
+ const csv = await loadExporters(regenerated).buildThreadContent(
+ "thread",
+ "csv",
+ );
+ assert.equal(
+ csv,
+ "user,Name one fruit.\nassistant,Apples!\nassistant,Apples.",
+ );
+});