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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: keep <think> text visible in the reply when Thinking is off (#11481)
Nilay90557809930a
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex d69949e55ca..d008a60abcd 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -311,6 +311,7 @@ import {generationChunkHasSubstantiveDelta,generationIsSettled,releaseLiveGenerationRun,+ requestParsesThinkTags,} from "../utils/chat-generation-recovery";import {generateAudio,@@ -5226,6 +5227,7 @@ export function createOpenAIStreamAdapter(// Both this stream and a recovery follower must persist the marker, or the next reload// attaches another follower and blocks the composer for a further deadline.let generationStalled = false;+ let parseThink = true;const generationCustom = () =>generationRunId? {@@ -5241,6 +5243,7 @@ export function createOpenAIStreamAdapter(),generationLocallyInterrupted: generationStalled,serverManaged: true,+ parseThinkTags: parseThink,}: {};if (activeModel?.isAudio && !activeModel?.hasAudioInput) {@@ -5367,9 +5370,20 @@ export function createOpenAIStreamAdapter();// The parse of everything streamed so far, extended by each delta. The final merge can rewrite// the prefix it is handed, which an extend-only parse cannot follow, so it reparses.- const segmentedText = createSegmentedAssistantText({- trustAppends: !(continuationPartial && repairContinuation),- });+ const trustAppends = !(continuationPartial && repairContinuation);+ let segmentedText = createSegmentedAssistantText({ trustAppends });+ // Thinking off leaves <think> as reply text, unless reasoning arrives anyway: then the+ // reply is reparsed with the tags that wrap it.+ const setParseThink = (next: boolean): void => {+ if (next === parseThink) {+ return;+ }+ parseThink = next;+ segmentedText = createSegmentedAssistantText({+ trustAppends,+ parseThink,+ });+ };// The single place `cumulativeText` grows, so everything derived from it sees the same// characters in the same order.const appendCumulative = (text: string): void => {@@ -5751,6 +5765,88 @@ export function createOpenAIStreamAdapter(return pinTextThoughtSignature(assembled);};+ const {+ supportsReasoning,+ reasoningEnabled,+ reasoningAlwaysOn,+ reasoningStyle,+ reasoningEffort,+ reasoningEffortLevels,+ } = runtime;+ const externalReasoningCaps: ReturnType<+ typeof getExternalReasoningCapabilities+ > =+ externalSelection && externalProvider+ ? getExternalReasoningCapabilities(+ externalProvider.providerType,+ externalSelection.modelId,+ {+ isReasoningProvider: externalProvider.isReasoningModel === true,+ baseUrl: externalProvider.baseUrl ?? null,+ },+ )+ : {+ supportsReasoning,+ reasoningStyle,+ reasoningAlwaysOn: false,+ supportsReasoningOff: false,+ reasoningEffortLevels: ["low", "medium", "high"] as const,+ };+ const externalReasoningEnabled =+ !externalReasoningCaps.supportsReasoningOff ? true : reasoningEnabled;+ type RequestReasoningEffort = Extract<+ NonNullable<OpenAIChatCompletionsRequest["reasoning_effort"]>,+ "none" | "minimal" | "low" | "medium" | "high" | "max" | "xhigh"+ >;+ type ReasoningRequestFields = Pick<+ OpenAIChatCompletionsRequest,+ "enable_thinking" | "reasoning_effort" | "thinking"+ >;+ const fallbackExternalEffort = (externalReasoningCaps+ .reasoningEffortLevels[0] ?? "low") as RequestReasoningEffort;+ const selectedExternalEffort: RequestReasoningEffort =+ clampReasoningEffortToLevels(+ reasoningEffort,+ externalReasoningCaps.reasoningEffortLevels,+ ) as RequestReasoningEffort;+ // Clamp to the loaded model's advertised levels so a stale value becomes one the backend+ // honors: gpt-oss takes low|medium|high, GLM enable_thinking_effort high|max.+ const localReasoningEffort = clampReasoningEffortToLevels(+ reasoningEffort,+ reasoningEffortLevels,+ );+ const externalReasoningFields: ReasoningRequestFields =+ externalReasoningCaps.supportsReasoning+ ? externalReasoningCaps.reasoningStyle === "reasoning_effort"+ ? externalReasoningEnabled+ ? { reasoning_effort: selectedExternalEffort }+ : externalReasoningCaps.supportsReasoningOff+ ? { reasoning_effort: "none" }+ : { reasoning_effort: fallbackExternalEffort }+ : {+ thinking: {+ type: externalReasoningEnabled ? "enabled" : "disabled",+ },+ }+ : {};+ const localReasoningFields: ReasoningRequestFields = supportsReasoning+ ? reasoningStyle === "enable_thinking_effort"+ ? // GLM-5.2-style gate plus level, e.g. high|max.+ reasoningEnabled+ ? { enable_thinking: true, reasoning_effort: localReasoningEffort }+ : { enable_thinking: false }+ : reasoningStyle === "reasoning_effort"+ ? reasoningEnabled+ ? { reasoning_effort: localReasoningEffort }+ : {}+ : { thinking: { type: reasoningEnabled ? "enabled" : "disabled" } }+ : {};+ // Decided before the continuation yield below, which an abort during load saves as is.+ setParseThink(+ isExternalRequest+ ? requestParsesThinkTags(externalReasoningFields)+ : reasoningAlwaysOn || requestParsesThinkTags(localReasoningFields),+ );// Yielded before the request starts: an abort during load skips the partial-content yield// below, saving an empty message.if (continuation) {@@ -5885,15 +5981,7 @@ export function createOpenAIStreamAdapter(runSignal.addEventListener("abort", onAbortCancel, { once: true });}- const {- supportsReasoning,- reasoningEnabled,- reasoningStyle,- reasoningEffort,- reasoningEffortLevels,- supportsPreserveThinking,- preserveThinking,- } = runtime;+ const { supportsPreserveThinking, preserveThinking } = runtime;const externalBackendProviderType = toExternalBackendProviderType(externalProvider?.providerType,);@@ -5951,46 +6039,6 @@ export function createOpenAIStreamAdapter(const externalCapabilities = getProviderCapabilities(externalProvider?.providerType,);- const externalReasoningCaps: ReturnType<- typeof getExternalReasoningCapabilities- > =- externalSelection && externalProvider- ? getExternalReasoningCapabilities(- externalProvider.providerType,- externalSelection.modelId,- {- isReasoningProvider:- externalProvider.isReasoningModel === true,- baseUrl: externalProvider.baseUrl ?? null,- },- )- : {- supportsReasoning,- reasoningStyle,- reasoningAlwaysOn: false,- supportsReasoningOff: false,- reasoningEffortLevels: ["low", "medium", "high"] as const,- };- type RequestReasoningEffort = Extract<- NonNullable<OpenAIChatCompletionsRequest["reasoning_effort"]>,- "none" | "minimal" | "low" | "medium" | "high" | "max" | "xhigh"- >;- const fallbackExternalEffort = (externalReasoningCaps- .reasoningEffortLevels[0] ?? "low") as RequestReasoningEffort;- const selectedExternalEffort: RequestReasoningEffort =- clampReasoningEffortToLevels(- reasoningEffort,- externalReasoningCaps.reasoningEffortLevels,- ) as RequestReasoningEffort;- // Clamp to the loaded model's advertised levels so a stale value becomes one the backend- // honors: gpt-oss takes low|medium|high, GLM enable_thinking_effort high|max.- // gpt-oss-style reasoning_effort gets low|medium|high, GLM-style enable_thinking_effort high|max.- const localReasoningEffort = clampReasoningEffortToLevels(- reasoningEffort,- reasoningEffortLevels,- );- const externalReasoningEnabled =- !externalReasoningCaps.supportsReasoningOff ? true : reasoningEnabled;const buildRequestPayload = async (forceRefreshPublicKey = false,): Promise<OpenAIChatCompletionsRequest> => {@@ -6308,21 +6356,7 @@ export function createOpenAIStreamAdapter()? { fast_mode: true }: {}),- ...(externalReasoningCaps.supportsReasoning- ? externalReasoningCaps.reasoningStyle === "reasoning_effort"- ? externalReasoningEnabled- ? { reasoning_effort: selectedExternalEffort }- : externalReasoningCaps.supportsReasoningOff- ? { reasoning_effort: "none" }- : {- reasoning_effort: fallbackExternalEffort,- }- : {- thinking: {- type: externalReasoningEnabled ? "enabled" : "disabled",- },- }- : {}),+ ...externalReasoningFields,};}@@ -6359,28 +6393,7 @@ export function createOpenAIStreamAdapter(...(sandboxSessionId ? { session_id: sandboxSessionId } : {}),...(resolvedThreadId ? { thread_id: resolvedThreadId } : {}),...(useAdapter === undefined ? {} : { use_adapter: useAdapter }),- ...(supportsReasoning- ? reasoningStyle === "enable_thinking_effort"- // GLM-5.2-style gate plus level: disabling sends enable_thinking=false, enabling sends- // the chosen level.- // Enabling sends the chosen level, e.g. high|max.- ?- reasoningEnabled- ? {- enable_thinking: true,- reasoning_effort: localReasoningEffort,- }- : { enable_thinking: false }- : reasoningStyle === "reasoning_effort"- ? reasoningEnabled- ? { reasoning_effort: localReasoningEffort }- : {}- : {- thinking: {- type: reasoningEnabled ? "enabled" : "disabled",- },- }- : {}),+ ...localReasoningFields,...(supportsPreserveThinking? { preserve_thinking: preserveThinking }: {}),@@ -7182,8 +7195,11 @@ export function createOpenAIStreamAdapter(}const rawDelta = chunk.choices?.[0]?.delta?.content;// Normalize structured delta.content (mistral magistral).- const { text: delta, structuredReasoningContinues } =- extractDeltaText(rawDelta);+ const {+ text: delta,+ structuredReasoningContinues,+ hasStructuredReasoning,+ } = extractDeltaText(rawDelta);const deltaExtraContent = (chunk.choices?.[0]?.delta as| { extra_content?: unknown }@@ -7666,6 +7682,9 @@ export function createOpenAIStreamAdapter(runtime.setGeneratingStatus(null);}+ if (reasoning || hasStructuredReasoning) {+ setParseThink(true);+ }if (reasoning) {if (!reasoningContentOpen) {reasoningDurationTracker.startGroup();@@ -7685,7 +7704,8 @@ export function createOpenAIStreamAdapter(producedReplyText = true;// The trailing ${...} strip runs once on the finished reply, below the loop; nothing on this// path reads the buffer, so no arrival can flatten it.- const textEndsInsideThink = thinkTags.endsInsideThink();+ const textEndsInsideThink =+ parseThink && thinkTags.endsInsideThink();const assistantContent = liveAssistantContent();// Fallback when no server-side reasoning_summary arrives.diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsxindex 9f2b5a10cd8..18f2a6d81dd 100644--- a/studio/frontend/src/features/chat/runtime-provider.tsx+++ b/studio/frontend/src/features/chat/runtime-provider.tsx@@ -113,6 +113,7 @@ import {generationIsCorroboratedLive,threadHasDurableGenerationRun,generationNeedsRecovery,+ requestParsesThinkTags,restoreCarriedPartsFromRaw,isLiveGenerationRun,generationRawContent,@@ -870,6 +871,7 @@ function scheduleGenerationRecovery(const carried = stored.carried;const toolRecovery = createGenerationToolRecovery(carried, runId, cursor);let { raw, reasoningOpen } = stored;+ let parseThink = metadata.parseThinkTags !== false;let completionTokens: number | undefined;let recoveryUsage:| {@@ -907,6 +909,7 @@ function scheduleGenerationRecovery(restoreCarriedPartsFromRaw(reasoningOpen ? `${raw}</think>` : raw,carried,+ { parseThink },),) as MessageRecord["content"];const toolNames = (content: MessageRecord["content"]): string[] =>@@ -1043,6 +1046,13 @@ function scheduleGenerationRecovery(raw = lastRequestMessage.content;}}+ if (typeof metadata.parseThinkTags !== "boolean") {+ parseThink = requestParsesThinkTags(update.run.requestPayload);+ currentMetadata = {+ ...currentMetadata,+ parseThinkTags: parseThink,+ };+ }identityValidated = true;}// Replay from 0 re-delivers already-saved chunks: apply them, but publish nothing.@@ -1112,7 +1122,13 @@ function scheduleGenerationRecovery(typeof deltaRecord?.reasoning_content === "string"? deltaRecord.reasoning_content: "";- const delta = extractDeltaText(deltaRecord?.content).text;+ const { text: delta, hasStructuredReasoning } = extractDeltaText(+ deltaRecord?.content,+ );+ if (!parseThink && (reasoning || hasStructuredReasoning)) {+ parseThink = true;+ currentMetadata = { ...currentMetadata, parseThinkTags: true };+ }if (reasoning) {if (!reasoningOpen) raw += "<think>";raw += reasoning;diff --git a/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts b/studio/frontend/src/features/chat/utils/chat-generation-recovery.tsindex 8356a95cacb..c2b96d754a6 100644--- a/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts+++ b/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts@@ -330,8 +330,9 @@ function pastThinkTag(raw: string, at: number): number {export function restoreCarriedPartsFromRaw(raw: string,carried: readonly CarriedPart[],+ { parseThink = true }: { parseThink?: boolean } = {},): ReturnType<typeof parseAssistantContent> {- if (carried.length === 0) return parseAssistantContent(raw);+ if (carried.length === 0) return parseAssistantContent(raw, { parseThink });const out: ReturnType<typeof parseAssistantContent> = [];const tracker = createThinkTagTracker();let cursor = 0;@@ -339,7 +340,8 @@ export function restoreCarriedPartsFromRaw(const text = raw.slice(cursor, end);out.push(...parseAssistantContent(- tracker.endsInsideThink() ? `${THINK_OPEN}${text}` : text,+ parseThink && tracker.endsInsideThink() ? `${THINK_OPEN}${text}` : text,+ { parseThink },),);tracker.append(text);@@ -355,6 +357,20 @@ export function restoreCarriedPartsFromRaw(return out;}+// Whether a reply to this request can carry real <think> reasoning. Recovery reads it from the+// stored request because the server-created placeholder has no parseThinkTags until the first save.+export function requestParsesThinkTags(payload: {+ enable_thinking?: boolean | null;+ reasoning_effort?: string | null;+ thinking?: { type?: string } | null;+}): boolean {+ return !(+ payload.thinking?.type === "disabled" ||+ payload.enable_thinking === false ||+ payload.reasoning_effort === "none"+ );+}+function carriedPartKey({ at, part }: CarriedPart): string {const record = part as { type?: string; toolCallId?: string; id?: string };const id = record.toolCallId ?? record.id;diff --git a/studio/frontend/src/features/chat/utils/incremental-assistant-content.ts b/studio/frontend/src/features/chat/utils/incremental-assistant-content.tsindex a07017c4fb8..55cfd9feeb5 100644--- a/studio/frontend/src/features/chat/utils/incremental-assistant-content.ts+++ b/studio/frontend/src/features/chat/utils/incremental-assistant-content.ts@@ -35,11 +35,20 @@ class ParsedRun {readonly parts: ContentPart[] = [];private insideThink = false;private held = "";+ private readonly parseThink: boolean;++ constructor(parseThink: boolean) {+ this.parseThink = parseThink;+ }append(delta: string): void {if (!delta) {return;}+ if (!this.parseThink) {+ appendTextPart(this.parts, delta);+ return;+ }const work = this.held ? this.held + delta : delta;this.held = "";let cursor = 0;@@ -126,8 +135,12 @@ function sameBoundaries(* length or boundary mismatch and rebuilds from `rawText`, at one full parse. */export function createSegmentedAssistantText({trustAppends = true,-}: { trustAppends?: boolean } = {}): SegmentedAssistantText {- let runs: ParsedRun[] = [new ParsedRun()];+ parseThink = true,+}: {+ trustAppends?: boolean;+ parseThink?: boolean;+} = {}): SegmentedAssistantText {+ let runs: ParsedRun[] = [new ParsedRun(parseThink)];let boundaries: number[] = [];let length = 0;@@ -140,12 +153,12 @@ export function createSegmentedAssistantText({length = rawText.length;let from = 0;for (const boundary of boundaries) {- const run = new ParsedRun();+ const run = new ParsedRun(parseThink);run.append(rawText.slice(from, boundary));runs.push(run);from = boundary;}- const last = new ParsedRun();+ const last = new ParsedRun(parseThink);last.append(rawText.slice(from));runs.push(last);};diff --git a/studio/frontend/src/features/chat/utils/parse-assistant-content.ts b/studio/frontend/src/features/chat/utils/parse-assistant-content.tsindex 0577c5fb518..41d8f272888 100644--- a/studio/frontend/src/features/chat/utils/parse-assistant-content.ts+++ b/studio/frontend/src/features/chat/utils/parse-assistant-content.ts@@ -14,6 +14,7 @@ const THINK_CLOSE_TAG = "</think>";export function extractDeltaText(delta: unknown): {text: string;structuredReasoningContinues: boolean;+ hasStructuredReasoning: boolean;} {const extractReasoningText = (payload: unknown): string => {if (typeof payload === "string") return payload;@@ -33,14 +34,23 @@ export function extractDeltaText(delta: unknown): {};if (typeof delta === "string") {- return { text: delta, structuredReasoningContinues: false };+ return {+ text: delta,+ structuredReasoningContinues: false,+ hasStructuredReasoning: false,+ };}if (!Array.isArray(delta)) {- return { text: "", structuredReasoningContinues: false };+ return {+ text: "",+ structuredReasoningContinues: false,+ hasStructuredReasoning: false,+ };}let text = "";let structuredReasoningContinues = false;+ let hasStructuredReasoning = false;for (const part of delta) {if (typeof part === "string") {text += part;@@ -72,10 +82,11 @@ export function extractDeltaText(delta: unknown): {if (thinking) {text += `${THINK_OPEN_TAG}${thinking}${THINK_CLOSE_TAG}`;structuredReasoningContinues = true;+ hasStructuredReasoning = true;}}}- return { text, structuredReasoningContinues };+ return { text, structuredReasoningContinues, hasStructuredReasoning };}// ContentPart from @assistant-ui/react has readonly fields, so `last.text += text` fails@@ -103,11 +114,16 @@ export function appendReasoningPart(parts: ContentPart[], text: string): void {export function parseAssistantContent(raw: string,+ { parseThink = true }: { parseThink?: boolean } = {},): ContentPart[] {const parts: ContentPart[] = [];if (!raw) {return parts;}+ if (!parseThink) {+ appendTextPart(parts, raw);+ return parts;+ }let cursor = 0;while (cursor < raw.length) {diff --git a/studio/frontend/tests/external-thinking-wire.test.ts b/studio/frontend/tests/external-thinking-wire.test.tsindex 9edf3bb1424..f028e484730 100644--- a/studio/frontend/tests/external-thinking-wire.test.ts+++ b/studio/frontend/tests/external-thinking-wire.test.ts@@ -12,17 +12,20 @@ installLocalStorageFake();const { getExternalReasoningCapabilities } = await import("../src/features/chat/provider-capabilities.ts");+const { requestParsesThinkTags } = await import(+ "../src/features/chat/utils/chat-generation-recovery.ts"+);type Caps = ReturnType<typeof getExternalReasoningCapabilities>;// Runs the adapter's own external reasoning expressions, so the test follows the shipped code.-function externalReasoningFields(caps: Caps, reasoningEnabled: boolean): unknown {+function externalReasoningFields(caps: Caps, reasoningEnabled: boolean, effort = "high"): unknown {const adapter = readSrc("features/chat/api/chat-adapter.ts");const enabledAt = adapter.indexOf("const externalReasoningEnabled =");- const fieldsAt = adapter.indexOf("...(externalReasoningCaps.supportsReasoning", enabledAt);+ const fieldsAt = adapter.indexOf("const externalReasoningFields", enabledAt);assert.ok(enabledAt > 0 && fieldsAt > enabledAt, "the adapter's external reasoning fields moved");const enabled = adapter.slice(adapter.indexOf("=", enabledAt) + 1, adapter.indexOf(";", enabledAt));- const fields = adapter.slice(fieldsAt + 3, adapter.indexOf(": {}),", fieldsAt) + ": {})".length);+ const fields = adapter.slice(adapter.indexOf("=", fieldsAt) + 1, adapter.indexOf(";", fieldsAt)).trim();const build = new Function("externalReasoningCaps","reasoningEnabled",@@ -30,7 +33,7 @@ function externalReasoningFields(caps: Caps, reasoningEnabled: boolean): unknown"fallbackExternalEffort",`const externalReasoningEnabled = ${enabled};\nreturn ${fields};`,);- return build(caps, reasoningEnabled, "high", "high");+ return build(caps, reasoningEnabled, effort, "high");}test("an always-on catalog model sends thinking on even when the chat stored it off", () => {@@ -60,3 +63,11 @@ test("a toggleable catalog model still sends the stored choice", () => {assert.deepEqual(externalReasoningFields(qwen, false), { thinking: { type: "disabled" } });assert.deepEqual(externalReasoningFields(qwen, true), { thinking: { type: "enabled" } });});++test("an external effort of none reads as thinking off", () => {+ const gpt = getExternalReasoningCapabilities("openai", "gpt-5.1");+ assert.equal(gpt.reasoningStyle, "reasoning_effort");+ assert.ok(gpt.reasoningEffortLevels.includes("none"));+ assert.equal(requestParsesThinkTags(externalReasoningFields(gpt, true, "none") as object), false);+ assert.equal(requestParsesThinkTags(externalReasoningFields(gpt, true, "high") as object), true);+});diff --git a/studio/frontend/tests/literal-think-when-thinking-off.test.ts b/studio/frontend/tests/literal-think-when-thinking-off.test.tsnew file mode 100644index 00000000000..1262783bc30--- /dev/null+++ b/studio/frontend/tests/literal-think-when-thinking-off.test.ts@@ -0,0 +1,197 @@+// 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 { readSrc, registerBundlerResolver } from "./helpers/kit.ts";++registerBundlerResolver();++const { extractDeltaText, parseAssistantContent } = await import(+ "../src/features/chat/utils/parse-assistant-content.ts"+);+const { createSegmentedAssistantText } = await import(+ "../src/features/chat/utils/incremental-assistant-content.ts"+);+const {+ generationRawContent,+ requestParsesThinkTags,+ restoreCarriedPartsFromRaw,+} = await import(+ "../src/features/chat/utils/chat-generation-recovery.ts"+);++const LITERAL = "Use <think>x</think> tags, or a lone </think> and <think>";++test("thinking off keeps literal think tags as reply text", () => {+ assert.deepEqual(+ parseAssistantContent("Use <think>x</think> tags", { parseThink: false }),+ [{ type: "text", text: "Use <think>x</think> tags" }],+ );+ assert.deepEqual(parseAssistantContent("", { parseThink: false }), []);+});++test("thinking on still splits think tags into reasoning", () => {+ assert.deepEqual(parseAssistantContent("Use <think>x</think> tags"), [+ { type: "text", text: "Use " },+ { type: "reasoning", text: "x" },+ { type: "text", text: " tags" },+ ]);+});++test("the streaming parse matches the full parse with think parsing off", () => {+ const boundaries = [4, 20];+ const segmented = createSegmentedAssistantText({ parseThink: false });+ let raw = "";+ for (const char of LITERAL) {+ raw += char;+ segmented.appendText(char);+ const cuts = boundaries.filter((at) => at <= raw.length);+ const expected = [0, ...cuts].map((from, index) =>+ parseAssistantContent(raw.slice(from, cuts[index] ?? raw.length), {+ parseThink: false,+ }),+ );+ assert.deepEqual(segmented.runs(raw, cuts), expected);+ }+ assert.deepEqual(segmented.runs(LITERAL, boundaries), [+ [{ type: "text", text: LITERAL.slice(0, 4) }],+ [{ type: "text", text: LITERAL.slice(4, 20) }],+ [{ type: "text", text: LITERAL.slice(20) }],+ ]);+});++test("reasoning that arrives on a thinking-off turn still renders as reasoning", () => {+ const raw = "Answer<think>real reasoning</think>Use <think>x</think>";+ const reparsed = createSegmentedAssistantText({ parseThink: true });+ reparsed.appendText("<think>real reasoning");+ assert.deepEqual(reparsed.runs(raw, []), [parseAssistantContent(raw)]);+ assert.equal(+ reparsed.runs(raw, [])[0].some((part) => part.type === "reasoning"),+ true,+ );+ assert.equal(+ extractDeltaText([{ type: "thinking", thinking: "plan" }])+ .hasStructuredReasoning,+ true,+ );+ assert.equal(+ extractDeltaText([{ type: "text", text: "<think>x</think>" }])+ .hasStructuredReasoning,+ false,+ );+});++test("recovery replay keeps literal think text when the turn stamped it", () => {+ const stored = [+ { type: "text", text: "Use <think>x</think> tags " },+ { type: "tool-call", toolCallId: "call_0", toolName: "web_search" },+ { type: "text", text: "then <think>" },+ ];+ const { raw, carried } = generationRawContent(stored);+ assert.deepEqual(+ restoreCarriedPartsFromRaw(raw, carried, { parseThink: false }),+ stored,+ );+ assert.deepEqual(restoreCarriedPartsFromRaw(raw, [], { parseThink: false }), [+ { type: "text", text: raw },+ ]);+ assert.equal(+ restoreCarriedPartsFromRaw(raw, carried).some(+ (part) => part.type === "reasoning",+ ),+ true,+ );+});++test("a recovery before the first client save reads the choice from the request", () => {+ assert.equal(+ requestParsesThinkTags({ thinking: { type: "disabled" } }),+ false,+ );+ assert.equal(requestParsesThinkTags({ enable_thinking: false }), false);+ assert.equal(requestParsesThinkTags({ reasoning_effort: "none" }), false);+ assert.equal(requestParsesThinkTags({ thinking: { type: "enabled" } }), true);+ assert.equal(+ requestParsesThinkTags({ enable_thinking: true, reasoning_effort: "high" }),+ true,+ );+ assert.equal(requestParsesThinkTags({ reasoning_effort: "low" }), true);+ assert.equal(requestParsesThinkTags({}), true);++ const { raw, carried } = generationRawContent([]);+ const replayed = `${raw}Use <think>hi</think> in your prompt.`;+ assert.deepEqual(+ restoreCarriedPartsFromRaw(replayed, carried, {+ parseThink: requestParsesThinkTags({ thinking: { type: "disabled" } }),+ }),+ [{ type: "text", text: "Use <think>hi</think> in your prompt." }],+ );+});++test("a local effort dial set to none parses like thinking off", () => {+ const adapter = readSrc("features/chat/api/chat-adapter.ts");+ const at = adapter.indexOf("const localReasoningFields");+ assert.ok(at !== -1, "the adapter's local reasoning fields moved");+ const fields = new Function(+ "supportsReasoning",+ "reasoningStyle",+ "reasoningEnabled",+ "localReasoningEffort",+ `return ${adapter.slice(adapter.indexOf("=", at) + 1, adapter.indexOf(";", at)).trim()};`,+ );+ const parses = (...args: unknown[]) => requestParsesThinkTags(fields(...args));+ assert.equal(parses(true, "reasoning_effort", true, "none"), false);+ assert.equal(parses(true, "reasoning_effort", true, "high"), true);+ assert.equal(parses(true, "reasoning_effort", false, "none"), true);+ assert.equal(parses(true, "enable_thinking", false, "high"), false);+ assert.equal(parses(true, "enable_thinking_effort", false, "high"), false);+ assert.equal(parses(true, "enable_thinking_effort", true, "high"), true);+ assert.equal(parses(false, "enable_thinking", false, "high"), true);+});++test("the adapter and recovery follow the turn's think parse state", () => {+ const adapter = readSrc("features/chat/api/chat-adapter.ts");+ assert.match(adapter, /parseThinkTags: parseThink,/);+ assert.match(+ adapter,+ /createSegmentedAssistantText\(\{\s*trustAppends,\s*parseThink,\s*\}\)/,+ );+ assert.match(+ adapter,+ /if \(reasoning \|\| hasStructuredReasoning\) \{\s*setParseThink\(true\);\s*\}\s*if \(reasoning\) \{\s*if \(!reasoningContentOpen\) \{/,+ );+ assert.match(adapter, /parseThink && thinkTags\.endsInsideThink\(\)/);+ assert.match(+ adapter,+ /setParseThink\(\s*isExternalRequest\s*\? requestParsesThinkTags\(externalReasoningFields\)\s*: reasoningAlwaysOn \|\| requestParsesThinkTags\(localReasoningFields\),\s*\);/,+ "the live stream and recovery must read thinking off from the same request fields",+ );+ assert.match(adapter, /\.\.\.externalReasoningFields,/);+ assert.match(adapter, /\.\.\.localReasoningFields,/);+ const decided = adapter.search(/setParseThink\(\s*isExternalRequest/);+ const continuationYield = adapter.indexOf(+ "// Yielded before the request starts",+ );+ assert.ok(+ decided !== -1 && continuationYield !== -1 && decided < continuationYield,+ "an abort during load saves the continuation yield, so it must parse with the turn's choice",+ );++ const recovery = readSrc("features/chat/runtime-provider.tsx");+ assert.match(+ recovery,+ /let parseThink = metadata\.parseThinkTags !== false;/,+ );+ assert.match(recovery, /carried,\s*\{ parseThink \},/);+ assert.match(+ recovery,+ /if \(typeof metadata\.parseThinkTags !== "boolean"\) \{\s*parseThink = requestParsesThinkTags\(update\.run\.requestPayload\);\s*currentMetadata = \{\s*\.\.\.currentMetadata,\s*parseThinkTags: parseThink,\s*\};/,+ "the server placeholder has no flag until the first client save",+ );+ assert.match(+ recovery,+ /if \(!parseThink && \(reasoning \|\| hasStructuredReasoning\)\) \{\s*parseThink = true;\s*currentMetadata = \{ \.\.\.currentMetadata, parseThinkTags: true \};/,+ );+});diff --git a/studio/frontend/tests/reasoning-duration.test.ts b/studio/frontend/tests/reasoning-duration.test.tsindex e8c4270241a..45013b59b46 100644--- a/studio/frontend/tests/reasoning-duration.test.ts+++ b/studio/frontend/tests/reasoning-duration.test.ts@@ -126,6 +126,7 @@ test("keeps structured reasoning active only when it is the final content", () ={text: "<think>First</think>",structuredReasoningContinues: true,+ hasStructuredReasoning: true,},);assert.deepEqual(@@ -136,6 +137,7 @@ test("keeps structured reasoning active only when it is the final content", () ={text: "<think>Last thought</think>Answer",structuredReasoningContinues: false,+ hasStructuredReasoning: true,},);assert.deepEqual(@@ -146,6 +148,7 @@ test("keeps structured reasoning active only when it is the final content", () ={text: "Preface<think>First thought</think>",structuredReasoningContinues: true,+ hasStructuredReasoning: true,},);});