GeezerVison is showing github.com — fix: render think tags as reasoning blocks + fix MCP MaxListeners overflow by erwinh22 · Pull Request #15382 · anomalyco/opencode
fix: render think tags as reasoning blocks + fix MCP MaxListeners overflow
#15382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.ts
(3)
.tsx
(2)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
fix: render <think> tags as reasoning blocks at display layer
Models that emit <think>/<thinking> tags (e.g. kimi-k2-thinking via
NVIDIA NIM) currently display raw tag content in the UI instead of
rendering it as collapsible reasoning blocks.
The extractReasoningMiddleware was intentionally removed in PR #11270
because stripping tags at the middleware layer prevents them from being
stored in messages, breaking multi-turn LLM context.
This fix takes the rendering-layer approach: tags are preserved in
storage for faithful multi-turn context, but parsed and rendered as
reasoning blocks at display time in all three rendering surfaces:
- TUI: TextPart in session/index.tsx uses splitThinkBlocks() to
separate reasoning from display text, rendering reasoning with the
same dim/italic styling as native ReasoningPart
- Web UI: TextPartDisplay in message-part.tsx renders extracted
reasoning in a reasoning-part div above the text content
- CLI: run.ts strips think tags from text output and shows reasoning
with dim/italic styling when --thinking flag is set
Shared utility functions (stripThinkTags, splitThinkBlocks) are added
to both @opencode-ai/util/think (for web UI) and the core
packages/opencode/src/util/format.ts (for TUI and CLI).
Fixes #15380
Relates to #11439
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 5fb6ea504ac9b7dc500d6a72f42c894740e7dbae
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -27,6 +27,7 @@ import { SkillTool } from "../../tool/skill"
import { BashTool } from "../../tool/bash"
import { TodoWriteTool } from "../../tool/todo"
import { Locale } from "../../util/locale"
import { splitThinkBlocks } from "../../util/format"
type ToolProps<T extends Tool.Info> = {
input: Tool.InferParameters<T>
Expand Down
Expand Up
@@ -490,7 +491,18 @@ export const RunCommand = cmd({
if (part.type === "text" && part.time?.end) {
if (emit("text", { part })) continue
const text = part.text.trim()
const { reasoning, text: displayText } = splitThinkBlocks(part.text)
if (reasoning && args.thinking) {
const line = `Thinking: ${reasoning}`
if (process.stdout.isTTY) {
UI.empty()
UI.println(`${UI.Style.TEXT_DIM}\u001b[3m${line}\u001b[0m${UI.Style.TEXT_NORMAL}`)
UI.empty()
} else {
process.stdout.write(line + EOL)
}
}
const text = displayText.trim()
if (!text) continue
if (!process.stdout.isTTY) {
process.stdout.write(text + EOL)
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -78,6 +78,7 @@ import { PermissionPrompt } from "./permission"
import { QuestionPrompt } from "./question"
import { DialogExportOptions } from "../../ui/dialog-export-options"
import { formatTranscript } from "../../util/transcript"
import { splitThinkBlocks } from "@/util/format"
import { UI } from "@/cli/ui.ts"
import { useTuiConfig } from "../../context/tui-config"
Expand Down
Expand Up
@@ -1425,33 +1426,58 @@ function ReasoningPart(props: { last: boolean; part: ReasoningPart; message: Ass
function TextPart(props: { last: boolean; part: TextPart; message: AssistantMessage }) {
const ctx = use()
const { theme, syntax } = useTheme()
const { theme, syntax, subtleSyntax } = useTheme()
const parsed = createMemo(() => splitThinkBlocks(props.part.text))
const displayText = createMemo(() => parsed().text.trim())
const reasoning = createMemo(() => parsed().reasoning.trim())
return (
<Show when={props.part.text.trim()}>
<box id={"text-" + props.part.id} paddingLeft={3} marginTop={1} flexShrink={0}>
<Switch>
<Match when={Flag.OPENCODE_EXPERIMENTAL_MARKDOWN}>
<markdown
syntaxStyle={syntax()}
streaming={true}
content={props.part.text.trim()}
conceal={ctx.conceal()}
/>
</Match>
<Match when={!Flag.OPENCODE_EXPERIMENTAL_MARKDOWN}>
<code
filetype="markdown"
drawUnstyledText={false}
streaming={true}
syntaxStyle={syntax()}
content={props.part.text.trim()}
conceal={ctx.conceal()}
fg={theme.text}
/>
</Match>
</Switch>
</box>
</Show>
<>
<Show when={reasoning() && ctx.showThinking()}>
<box
paddingLeft={2}
marginTop={1}
flexDirection="column"
border={["left"]}
customBorderChars={SplitBorder.customBorderChars}
borderColor={theme.backgroundElement}
>
<code
filetype="markdown"
drawUnstyledText={false}
streaming={true}
syntaxStyle={subtleSyntax()}
content={"_Thinking:_ " + reasoning()}
conceal={ctx.conceal()}
fg={theme.textMuted}
/>
</box>
</Show>
<Show when={displayText()}>
<box id={"text-" + props.part.id} paddingLeft={3} marginTop={1} flexShrink={0}>
<Switch>
<Match when={Flag.OPENCODE_EXPERIMENTAL_MARKDOWN}>
<markdown
syntaxStyle={syntax()}
streaming={true}
content={displayText()}
conceal={ctx.conceal()}
/>
</Match>
<Match when={!Flag.OPENCODE_EXPERIMENTAL_MARKDOWN}>
<code
filetype="markdown"
drawUnstyledText={false}
streaming={true}
syntaxStyle={syntax()}
content={displayText()}
conceal={ctx.conceal()}
fg={theme.text}
/>
</Match>
</Switch>
</box>
</Show>
</>
)
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -18,3 +18,35 @@ export function formatDuration(secs: number) {
const weeks = Math.floor(secs / 604800)
return weeks === 1 ? "~1 week" : `~${weeks} weeks`
}
/**
* Regex to match <think>...</think> and <thinking>...</thinking> blocks.
* Uses non-greedy matching to handle multiple blocks.
*/
const THINK_TAG_RE = /<(think(?:ing)?)>[\s\S]*?<\/\1>\s*/g
/**
* Strip `<think>`/`<thinking>` tag blocks from text for display purposes.
* The raw tags are preserved in storage for multi-turn LLM context.
*/
export function stripThinkTags(text: string): string {
return text.replace(THINK_TAG_RE, "").trim()
}
/**
* Extract `<think>`/`<thinking>` blocks and remaining text separately.
* Returns the reasoning content and the cleaned display text.
*/
export function splitThinkBlocks(text: string): { reasoning: string; text: string } {
const blocks: string[] = []
const cleaned = text.replace(THINK_TAG_RE, (match) => {
const inner = match.replace(/<\/?(?:think(?:ing)?)>/g, "").trim()
if (inner) blocks.push(inner)
return ""
})
return {
reasoning: blocks.join("\n\n"),
text: cleaned.trim(),
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -44,6 +44,7 @@ import { Markdown } from "./markdown"
import { ImagePreview } from "./image-preview"
import { getDirectory as _getDirectory, getFilename } from "@opencode-ai/util/path"
import { checksum } from "@opencode-ai/util/encode"
import { splitThinkBlocks } from "@opencode-ai/util/think"
import { Tooltip } from "./tooltip"
import { IconButton } from "./icon-button"
import { TextShimmer } from "./text-shimmer"
Expand Down
Expand Up
@@ -1078,7 +1079,9 @@ PART_MAPPING["text"] = function TextPartDisplay(props) {
return items.filter((x) => !!x).join(" \u00B7 ")
})
const displayText = () => (part.text ?? "").trim()
const parsed = createMemo(() => splitThinkBlocks(part.text ?? ""))
const displayText = () => parsed().text.trim()
const thinkReasoning = () => parsed().reasoning.trim()
const throttledText = createThrottledValue(displayText)
const isLastTextPart = createMemo(() => {
const last = (data.store.part?.[props.message.id] ?? [])
Expand All
@@ -1105,6 +1108,11 @@ PART_MAPPING["text"] = function TextPartDisplay(props) {
return (
<Show when={throttledText()}>
<div data-component="text-part">
<Show when={thinkReasoning()}>
<div data-component="reasoning-part">
<Markdown text={thinkReasoning()} cacheKey={part.id + "-reasoning"} />
</div>
</Show>
<div data-slot="text-part-body">
<Markdown text={throttledText()} cacheKey={part.id} />
</div>
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,40 @@
/**
* Utilities for handling `<think>`/`<thinking>` tags in LLM responses.
*
* These tags are kept in storage to preserve multi-turn LLM context,
* but should be stripped or separated at the rendering layer.
*/
/**
* Regex to match `<think>...</think>` and `<thinking>...</thinking>` blocks.
* Uses non-greedy matching to handle multiple blocks.
*/
const THINK_TAG_RE = /<(think(?:ing)?)>[\s\S]*?<\/\1>\s*/g
/**
* Strip `<think>`/`<thinking>` tag blocks from text for display purposes.
* The raw tags are preserved in storage for multi-turn LLM context.
*/
export function stripThinkTags(text: string): string {
return text.replace(THINK_TAG_RE, "").trim()
}
/**
* Extract `<think>`/`<thinking>` blocks and remaining text separately.
* Returns the reasoning content and the cleaned display text.
*/
export function splitThinkBlocks(text: string): {
reasoning: string
text: string
} {
const blocks: string[] = []
const cleaned = text.replace(THINK_TAG_RE, (match) => {
const inner = match.replace(/<\/?(?:think(?:ing)?)>/g, "").trim()
if (inner) blocks.push(inner)
return ""
})
return {
reasoning: blocks.join("\n\n"),
text: cleaned.trim(),
}
}
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.