Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(sse): <think>…</think> in content becomes a thinking block (item 3)

Jack Rudenkod4cba87ef27e
diff --git a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
index 37b9c818..26631f62 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
@@ -18,6 +18,7 @@ import {
import { isWebSearchToolCall, warnWebSearchUnsupported } from "../web-search-detector.js";
import { type BlockRef, createBlockWriter } from "./block-writer.js";
import { messageStartUsage } from "./message-start-usage.js";
+import { type ThinkSplit, createThinkTagSplitter } from "./think-tag-splitter.js";
/**
* Hard ceiling, in characters, on ONE logged raw SSE payload.
@@ -268,6 +269,56 @@ export function createStreamingResponseHandler(
// through here. It owns the block-index counter, so nothing else
// allocates an index.
const writer = createBlockWriter(send);
+ const thinkSplitter = createThinkTagSplitter();
+
+ /**
+ * Route one splitter result to its blocks.
+ *
+ * Shared by the streaming path and by `finalize()`'s flush, so a fragment
+ * released at the end of the turn takes exactly the same hold-back
+ * decision as one released mid-stream — rather than bypassing it and
+ * emitting a lone fragment of text that the hold-back is withholding the
+ * rest of.
+ */
+ const emitSplitContent = ({ thinking, text }: ThinkSplit): void => {
+ if (thinking) {
+ behavior?.onAssistantText?.(thinking, "reasoning");
+ writer.append(writer.openThinking(), thinking);
+ }
+ if (!text) return;
+
+ // Accumulate text for potential tool call extraction
+ state.accumulatedText += text;
+
+ // Check if text contains STRUCTURED tool call patterns that we should hold back
+ // Only hold back for patterns we can actually parse (XML, JSON), not natural language
+ // Natural language patterns are extracted at finalization, not held back
+ const hasStructuredToolPattern =
+ // Qwen XML-style: <function=ToolName>. Same shape the
+ // extractor accepts, so text held back here is always text
+ // the extractor can act on. A looser test here withheld
+ // text that nothing later emitted.
+ hasExtractableFunctionTag(state.accumulatedText) ||
+ // JSON tool call in text: {"name": "Task", "arguments":
+ /\{\s*"(?:name|tool)"\s*:\s*"(?:Task|Read|Write|Edit|Bash|Grep|Glob)"/i.test(
+ state.accumulatedText
+ ) ||
+ // XML tool_call tags: <tool_call>
+ /<tool_call>/.test(state.accumulatedText);
+
+ // Only hold back if we have a structured pattern AND haven't accumulated too much
+ // (if we've accumulated > 1000 chars without a complete pattern, release the text)
+ const shouldHoldBack = hasStructuredToolPattern && state.accumulatedText.length < 1000;
+
+ if (shouldHoldBack) {
+ log(
+ `[Streaming] Text held back (structured tool pattern): ${state.accumulatedText.length} chars accumulated`
+ );
+ return;
+ }
+
+ writer.append(writer.openText(), text);
+ };
send("message_start", {
type: "message_start",
@@ -323,6 +374,13 @@ export function createStreamingResponseHandler(
state.finalized = true;
try {
+ // Release anything the think-tag splitter is still holding — at most
+ // seven characters of a tag that never completed. Runs FIRST, before
+ // text-based tool-call recovery reads `accumulatedText`, so a held
+ // fragment is part of the text recovery scans rather than arriving
+ // after it.
+ emitSplitContent(thinkSplitter.flush());
+
// Argument fragments whose `function.name` never arrived. A call with
// no name is not a call — there is nothing to dispatch and no schema to
// validate against — so they are discarded. Named in the log because
@@ -581,6 +639,12 @@ export function createStreamingResponseHandler(
// dropped every OpenRouter thinking model's reasoning.
const reasoningText = delta.reasoning_content || delta.reasoning;
if (reasoningText) {
+ // This provider has a reasoning FIELD, so a `<think>` in its
+ // content is a model writing about tags, not speaking in them.
+ // Close-tag handling stays armed: these are exactly the
+ // providers whose chat template opens `<think>` server-side and
+ // leaks the bare close into the content.
+ thinkSplitter.disarmOpen();
behavior?.onAssistantText?.(reasoningText, "reasoning");
state.lastActivity = Date.now();
// Reasoning arriving AFTER text used to open a thinking block
@@ -626,39 +690,12 @@ export function createStreamingResponseHandler(
}
if (res.cleanedText) {
- // Accumulate text for potential tool call extraction
- state.accumulatedText += res.cleanedText;
-
- // Check if text contains STRUCTURED tool call patterns that we should hold back
- // Only hold back for patterns we can actually parse (XML, JSON), not natural language
- // Natural language patterns are extracted at finalization, not held back
- const hasStructuredToolPattern =
- // Qwen XML-style: <function=ToolName>. Same shape the
- // extractor accepts, so text held back here is always text
- // the extractor can act on. A looser test here withheld
- // text that nothing later emitted.
- hasExtractableFunctionTag(state.accumulatedText) ||
- // JSON tool call in text: {"name": "Task", "arguments":
- /\{\s*"(?:name|tool)"\s*:\s*"(?:Task|Read|Write|Edit|Bash|Grep|Glob)"/i.test(
- state.accumulatedText
- ) ||
- // XML tool_call tags: <tool_call>
- /<tool_call>/.test(state.accumulatedText);
-
- // Only hold back if we have a structured pattern AND haven't accumulated too much
- // (if we've accumulated > 1000 chars without a complete pattern, release the text)
- const shouldHoldBack =
- hasStructuredToolPattern && state.accumulatedText.length < 1000;
-
- if (shouldHoldBack) {
- log(
- `[Streaming] Text held back (structured tool pattern): ${state.accumulatedText.length} chars accumulated`
- );
- }
-
- if (!shouldHoldBack) {
- writer.append(writer.openText(), res.cleanedText);
- }
+ // `<think>…</think>` carried in the ORDINARY content field is
+ // reasoning, not the answer. Split it out here, after the
+ // adapter, so the thinking half never enters
+ // `accumulatedText` (tool-call recovery scans that) and never
+ // renders as the assistant's words.
+ emitSplitContent(thinkSplitter.push(res.cleanedText));
}
}
diff --git a/packages/cli/src/handlers/shared/stream-parsers/think-tag-splitter.ts b/packages/cli/src/handlers/shared/stream-parsers/think-tag-splitter.ts
new file mode 100644
index 00000000..1b9be298
--- /dev/null
+++ b/packages/cli/src/handlers/shared/stream-parsers/think-tag-splitter.ts
@@ -0,0 +1,180 @@
+/**
+ * `<think>…</think>` in an OpenAI `delta.content` stream → an Anthropic thinking
+ * block.
+ *
+ * Some models carry their reasoning in the ordinary content field wrapped in
+ * `<think>` tags rather than in `reasoning_content` / `reasoning`. Passed
+ * through untouched, the tags and the reasoning render as the assistant's
+ * ANSWER, and they enter the conversation history as the assistant's words.
+ *
+ * ## Two deliberate narrowings
+ *
+ * The obvious implementation scans the whole stream for the tags. It is wrong in
+ * a way that is invisible until it bites: a model writing *about* `<think>` — in
+ * a code block, in documentation, in a message about this very feature — has its
+ * output silently eaten, and the text never reaches the client at all.
+ *
+ * So:
+ *
+ * 1. **The open tag is recognised at position 0 only.** `<think>` must be the
+ * first non-whitespace content of the turn. A `<think>` anywhere else is
+ * ordinary text.
+ * 2. **The open tag is disarmed once real reasoning has been seen** on
+ * `reasoning_content` / `reasoning`. A provider that has its own reasoning
+ * field is not also speaking in tags, and a `<think>` in its content is a
+ * model talking about tags.
+ *
+ * Disarmed and decided, `push()` is the identity function on its argument, so a
+ * stream with no tags is byte-identical to one that never met this splitter.
+ *
+ * ## The orphan close
+ *
+ * A `</think>` as the first non-whitespace content, with no `<think>` before it,
+ * is real: the provider's chat template opened the tag server-side and only the
+ * close reaches the wire. It is REMOVED from the text. It needs no further
+ * signal — if a thinking block is open (because `reasoning_content` opened one),
+ * the next text emission closes it through the block writer, and if no text
+ * follows, `finalize()` closes it. Position 0 again, for the same reason as the
+ * open tag.
+ *
+ * ## Chunk boundaries
+ *
+ * A tag can be split across chunks — `</thi` then `nk>`. The splitter holds back
+ * the longest suffix of what it has that is a proper prefix of `</think>` (at
+ * most 7 characters) and releases it on the next chunk, or at {@link flush}. A
+ * held fragment is therefore never dropped; the cost is at most 7 characters of
+ * latency on a chunk that ends mid-tag.
+ */
+
+const OPEN = "<think>";
+const CLOSE = "</think>";
+
+export interface ThinkSplit {
+ /** Content belonging in a thinking block. */
+ thinking: string;
+ /** Content belonging in a text block. */
+ text: string;
+}
+
+export interface ThinkTagSplitter {
+ /** Feed one chunk of post-adapter content. */
+ push(chunk: string): ThinkSplit;
+ /**
+ * Release everything still held. Called once from `finalize()`, so a fragment
+ * held back for a tag that never completed still reaches the client.
+ */
+ flush(): ThinkSplit;
+ /**
+ * Real reasoning arrived on its own field: stop looking for an OPEN tag.
+ * Close-tag handling is unaffected — that is what strips a leaked `</think>`
+ * from the head of the content on exactly those providers.
+ */
+ disarmOpen(): void;
+ /** Whether content is currently being routed to a thinking block. */
+ readonly inThinking: boolean;
+}
+
+const EMPTY: ThinkSplit = { thinking: "", text: "" };
+
+export function createThinkTagSplitter(): ThinkTagSplitter {
+ /**
+ * `deciding` — nothing non-whitespace has been classified yet.
+ * `thinking` — inside `<think>`, looking for the close.
+ * `passthrough` — decided; `push` is the identity from here on.
+ */
+ let phase: "deciding" | "thinking" | "passthrough" = "deciding";
+ /** Held-back bytes: the undecided head, or a partial close tag. */
+ let pending = "";
+ let openArmed = true;
+
+ /** Longest suffix of `s` that is a proper prefix of `CLOSE`, at most 7 chars. */
+ const heldSuffixLength = (s: string): number => {
+ for (let n = Math.min(CLOSE.length - 1, s.length); n > 0; n--) {
+ if (CLOSE.startsWith(s.slice(s.length - n))) return n;
+ }
+ return 0;
+ };
+
+ const consumeThinking = (chunk: string): ThinkSplit => {
+ pending += chunk;
+ const at = pending.indexOf(CLOSE);
+ if (at >= 0) {
+ const thinking = pending.slice(0, at);
+ const text = pending.slice(at + CLOSE.length);
+ pending = "";
+ phase = "passthrough";
+ return { thinking, text };
+ }
+ const hold = heldSuffixLength(pending);
+ const thinking = pending.slice(0, pending.length - hold);
+ pending = hold === 0 ? "" : pending.slice(pending.length - hold);
+ return { thinking, text: "" };
+ };
+
+ /**
+ * Classify the head of the turn. `isFlush` means there will be no more input,
+ * so an undecided head can no longer be held — it is ordinary text.
+ */
+ const decide = (isFlush: boolean): ThinkSplit => {
+ const lead = pending.replace(/^\s+/, "");
+
+ const releaseAsText = (): ThinkSplit => {
+ phase = "passthrough";
+ const text = pending;
+ pending = "";
+ return { thinking: "", text };
+ };
+
+ if (lead === "") return isFlush ? releaseAsText() : EMPTY;
+
+ if (openArmed && lead.startsWith(OPEN)) {
+ // Leading whitespace is dropped rather than emitted: emitting it would
+ // open a TEXT block ahead of the thinking block, which is the very
+ // ordering this exists to get right.
+ phase = "thinking";
+ pending = "";
+ return consumeThinking(lead.slice(OPEN.length));
+ }
+
+ if (lead.startsWith(CLOSE)) {
+ phase = "passthrough";
+ pending = "";
+ return { thinking: "", text: lead.slice(CLOSE.length) };
+ }
+
+ // Still a proper prefix of a tag — undecided, hold for the next chunk.
+ if ((openArmed && OPEN.startsWith(lead)) || CLOSE.startsWith(lead)) {
+ return isFlush ? releaseAsText() : EMPTY;
+ }
+
+ return releaseAsText();
+ };
+
+ return {
+ push(chunk) {
+ if (!chunk) return EMPTY;
+ if (phase === "passthrough") return { thinking: "", text: chunk };
+ if (phase === "thinking") return consumeThinking(chunk);
+ pending += chunk;
+ return decide(false);
+ },
+
+ flush() {
+ if (phase === "passthrough") return EMPTY;
+ if (phase === "thinking") {
+ const thinking = pending;
+ pending = "";
+ return { thinking, text: "" };
+ }
+ return decide(true);
+ },
+
+ disarmOpen() {
+ openArmed = false;
+ },
+
+ get inThinking() {
+ return phase === "thinking";
+ },
+ };
+}