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 a tool call's answer after its result in JSONL chat exports (#12579)

Nilaya8afa696f8eb
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 b2ff298704f..212553b5cad 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
@@ -78,6 +78,7 @@ import {
import { notifyChatHistoryUpdated } from "../api/chat-api";
import { toolResultModelText } from "../api/chat-adapter";
import { toolCallReplayArguments } from "../tool-call-arguments";
+import { codexLocalToolRoundId, startsNewCodexToolRound } from "../codex-reasoning";
import { usePlusMenuPrefsStore } from "../stores/plus-menu-prefs-store";
import type { ThreadRecord, MessageRecord } from "../types";
import {
@@ -336,20 +337,47 @@ function messageToOpenAI(msg: { role: unknown; content: unknown; attachments?: u
];
if (role === "assistant") {
- const textParts: string[] = [];
- const toolCalls: OAIToolCall[] = [];
- const toolResults: OAIMessage[] = [];
+ const out: OAIMessage[] = [];
+ let textParts: string[] = [];
+ let toolCalls: OAIToolCall[] = [];
+ let toolResults: OAIMessage[] = [];
+ let callCount = 0;
+ let roundId: number | null = null;
+
+ const flush = () => {
+ const content = textParts.join("\n\n") || null;
+ out.push(
+ toolCalls.length > 0
+ ? { role: "assistant", content, tool_calls: toolCalls }
+ : { role: "assistant", content: content ?? "" },
+ ...toolResults,
+ );
+ textParts = [];
+ toolCalls = [];
+ toolResults = [];
+ roundId = null;
+ };
+ const pushText = (text: string) => {
+ if (!text.trim()) return;
+ // A call with no result (stopped, provider-native) stays with this text: splitting would leave tool_calls unanswered.
+ if (toolResults.length > 0) flush();
+ textParts.push(text);
+ };
for (const p of allParts) {
if (p.type === "text" && typeof p.text === "string") {
- textParts.push(p.text);
+ pushText(p.text);
} else if (p.type === "reasoning" || p.type === "thinking") {
const t = typeof p.thinking === "string" ? p.thinking : typeof p.text === "string" ? p.text : "";
- if (t) textParts.push(`<thinking>\n${t}\n</thinking>`);
+ if (t) pushText(`<thinking>\n${t}\n</thinking>`);
} else if (p.type === "image" && typeof p.image === "string" && p.image) {
- textParts.push("[image attachment]");
+ pushText("[image attachment]");
} else if (p.type === "tool-call") {
- const id = typeof p.toolCallId === "string" ? p.toolCallId : `call_${toolCalls.length}`;
+ const callRoundId = codexLocalToolRoundId(p.provenance);
+ if (toolCalls.length > 0 && startsNewCodexToolRound(roundId, callRoundId)) flush();
+ if (callRoundId !== null) roundId = callRoundId;
+ const id = typeof p.toolCallId === "string" ? p.toolCallId : `call_${callCount}`;
+ callCount++;
const name = typeof p.toolName === "string" ? p.toolName : "unknown";
const argsStr = toolCallReplayArguments(
typeof p.argsText === "string" ? p.argsText : undefined,
@@ -362,18 +390,13 @@ function messageToOpenAI(msg: { role: unknown; content: unknown; attachments?: u
const resultStr =
typeof modelText === "string" ? modelText : JSON.stringify(modelText);
toolResults.push({ role: "tool", tool_call_id: id, name, content: resultStr });
+ if (callRoundId === null && (p.provenance as { source?: unknown } | undefined)?.source === "local") flush();
}
}
}
- const content = textParts.join("\n\n") || null;
- const assistantMsg: OAIMessage = toolCalls.length > 0
- ? { role: "assistant", content, tool_calls: toolCalls }
- : { role: "assistant", content: content ?? "" };
-
- return toolResults.length > 0
- ? [assistantMsg, ...toolResults]
- : [assistantMsg];
+ if (out.length === 0 || textParts.length > 0 || toolCalls.length > 0) flush();
+ return out;
}
const contentParts: OAIContentPart[] = [];
diff --git a/studio/frontend/tests/message-jsonl-import.test.ts b/studio/frontend/tests/message-jsonl-import.test.ts
index ed308e1bb44..bf99c3f22c3 100644
--- a/studio/frontend/tests/message-jsonl-import.test.ts
+++ b/studio/frontend/tests/message-jsonl-import.test.ts
@@ -6,6 +6,8 @@ import vm from "node:vm";
import { after, before, test } from "node:test";
import ts from "typescript";
import { type ViteDevServer, createServer } from "vite";
+import { toolCallReplayArguments } from "../src/features/chat/tool-call-arguments.ts";
+import { codexLocalToolRoundId, startsNewCodexToolRound } from "../src/features/chat/codex-reasoning.ts";
import type { ParsedConversation } from "../src/features/chat/types.ts";
import { readSrc } from "./helpers/kit.ts";
@@ -36,6 +38,9 @@ function loadMessageToOpenAI(): typeof messageToOpenAI {
const context = {
unwrapPastedTextContent: (text: string) => text,
toolResultModelText: (result: unknown) => result,
+ toolCallReplayArguments,
+ codexLocalToolRoundId,
+ startsNewCodexToolRound,
} as Record<string, unknown>;
vm.runInNewContext(javascript, context);
return context.__messageToOpenAI as typeof messageToOpenAI;
@@ -217,6 +222,153 @@ test("assistant images are represented explicitly in JSONL exports", () => {
);
});
+function toolCallPart(id: string, query: string, result: string) {
+ return {
+ type: "tool-call",
+ toolCallId: id,
+ toolName: "web_search",
+ args: { query },
+ argsText: JSON.stringify({ query }),
+ result,
+ };
+}
+
+function webSearchCall(id: string, query: string) {
+ return {
+ id,
+ type: "function",
+ function: { name: "web_search", arguments: JSON.stringify({ query }) },
+ };
+}
+
+test("JSONL exports put the answer after the tool result it used", () => {
+ const exported = structuredClone(
+ messageToOpenAI({
+ role: "assistant",
+ content: [
+ { type: "reasoning", text: "I should search." },
+ { type: "text", text: "Let me look that up." },
+ toolCallPart("c1", "capital of Australia", "Canberra is the capital."),
+ { type: "text", text: "The capital of Australia is Canberra." },
+ ],
+ }),
+ );
+ assert.deepEqual(exported, [
+ {
+ role: "assistant",
+ content: "<thinking>\nI should search.\n</thinking>\n\nLet me look that up.",
+ tool_calls: [webSearchCall("c1", "capital of Australia")],
+ },
+ { role: "tool", tool_call_id: "c1", name: "web_search", content: "Canberra is the capital." },
+ { role: "assistant", content: "The capital of Australia is Canberra." },
+ ]);
+});
+
+test("JSONL exports keep each tool round as its own assistant turn", () => {
+ const exported = structuredClone(
+ messageToOpenAI({
+ role: "assistant",
+ content: [
+ toolCallPart("a", "first", "A"),
+ { type: "text", text: "Let me check more." },
+ toolCallPart("b", "second", "B"),
+ { type: "text", text: "Final." },
+ ],
+ }),
+ );
+ assert.deepEqual(exported, [
+ { role: "assistant", content: null, tool_calls: [webSearchCall("a", "first")] },
+ { role: "tool", tool_call_id: "a", name: "web_search", content: "A" },
+ { role: "assistant", content: "Let me check more.", tool_calls: [webSearchCall("b", "second")] },
+ { role: "tool", tool_call_id: "b", name: "web_search", content: "B" },
+ { role: "assistant", content: "Final." },
+ ]);
+});
+
+test("JSONL exports split Studio's back-to-back searches into rounds", () => {
+ const local = { provenance: { source: "local" } };
+ const exported = structuredClone(
+ messageToOpenAI({
+ role: "assistant",
+ content: [
+ { ...toolCallPart("a", "first", "No results found."), ...local },
+ { ...toolCallPart("b", "second", "B"), ...local },
+ { type: "text", text: "Final." },
+ ],
+ }),
+ );
+ assert.deepEqual(exported, [
+ { role: "assistant", content: null, tool_calls: [webSearchCall("a", "first")] },
+ { role: "tool", tool_call_id: "a", name: "web_search", content: "No results found." },
+ { role: "assistant", content: null, tool_calls: [webSearchCall("b", "second")] },
+ { role: "tool", tool_call_id: "b", name: "web_search", content: "B" },
+ { role: "assistant", content: "Final." },
+ ]);
+});
+
+test("JSONL exports keep a Studio round's parallel searches in one turn", () => {
+ const round = (round_id: number) => ({ provenance: { source: "local", round_id } });
+ const exported = structuredClone(
+ messageToOpenAI({
+ role: "assistant",
+ content: [
+ { ...toolCallPart("a", "first", "A"), ...round(1) },
+ { ...toolCallPart("b", "second", "B"), ...round(1) },
+ { ...toolCallPart("c", "third", "C"), ...round(2) },
+ { type: "text", text: "Final." },
+ ],
+ }),
+ );
+ assert.deepEqual(exported, [
+ {
+ role: "assistant",
+ content: null,
+ tool_calls: [webSearchCall("a", "first"), webSearchCall("b", "second")],
+ },
+ { role: "tool", tool_call_id: "a", name: "web_search", content: "A" },
+ { role: "tool", tool_call_id: "b", name: "web_search", content: "B" },
+ { role: "assistant", content: null, tool_calls: [webSearchCall("c", "third")] },
+ { role: "tool", tool_call_id: "c", name: "web_search", content: "C" },
+ { role: "assistant", content: "Final." },
+ ]);
+});
+
+test("JSONL exports keep parallel tool calls in one assistant turn", () => {
+ const exported = structuredClone(
+ messageToOpenAI({
+ role: "assistant",
+ content: [
+ toolCallPart("a", "first", "A"),
+ toolCallPart("b", "second", "B"),
+ { type: "text", text: "Both done." },
+ ],
+ }),
+ );
+ assert.deepEqual(exported, [
+ {
+ role: "assistant",
+ content: null,
+ tool_calls: [webSearchCall("a", "first"), webSearchCall("b", "second")],
+ },
+ { role: "tool", tool_call_id: "a", name: "web_search", content: "A" },
+ { role: "tool", tool_call_id: "b", name: "web_search", content: "B" },
+ { role: "assistant", content: "Both done." },
+ ]);
+});
+
+test("JSONL exports keep a call without a result in the turn of the text after it", () => {
+ const { result: _omitted, ...unanswered } = toolCallPart("a", "first", "");
+ const exported = structuredClone(
+ messageToOpenAI({
+ role: "assistant",
+ content: [unanswered, { type: "text", text: "Answer." }],
+ }),
+ );
+ assert.deepEqual(exported, [
+ { role: "assistant", content: "Answer.", tool_calls: [webSearchCall("a", "first")] },
+ ]);
+});
+
test("imports without message send times mark their ordering timestamps as estimated", () => {
for (const [filename, source] of [
["messages.jsonl", '{"messages":[{"role":"user","content":"Hello"}]}'],