fix(polish): 过滤模型深度思考输出#45
Merged
Merged
Conversation
Thinking-capable OpenAI-compatible models can return tagged reasoning before the final polished answer. The cleanup layer now strips only explicit think-tag blocks after response parsing so existing provider requests, UI, and fallback behavior stay unchanged. Constraint: Issue Open-Less#25 asks to adapt thinking output rather than disabling model thinking. Rejected: Add a provider-specific thinking toggle | broader UI and settings change than needed for the bug. Rejected: Strip localized heading text | too likely to remove normal user-facing content without a stable provider contract. Confidence: medium Scope-risk: narrow Tested: cargo test polish::tests -- --nocapture Tested: cargo check
Reviewer's GuideAdds conservative stripping of Flow diagram for clean_polish_output with strip_thinking_blocksflowchart TD
A[Start_clean_polish_output_with_model_content] --> B[Call_strip_thinking_blocks]
B --> C{Any_think_blocks_found?}
C -- Yes --> D[Build_output_without_think_blocks]
D --> E[Return_cleaned_text_from_strip_thinking_blocks]
C -- No --> F[Return_original_text_as_borrowed_Cow]
E --> G[Trim_whitespace]
F --> G[Trim_whitespace]
G --> H[Strip_markdown_fence_if_present]
H --> I[Remove_leading_trailing_boilerplate_sentences]
I --> J[Return_final_polish_output_without_reasoning_blocks]
J --> K[End]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
strip_thinking_blocks, consider operating on a mutableStringpassed in (or using a single pass over&strwith indices) to avoid repeatedto_stringallocations and potential O(n²) behavior when many<think>blocks are present. - If you expect models to sometimes include attributes or casing variants on the thinking tag (e.g.
<think reason="true">or<THINK>), you may want to extendstrip_thinking_blocksto handle those forms as well while still keeping the conservative behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `strip_thinking_blocks`, consider operating on a mutable `String` passed in (or using a single pass over `&str` with indices) to avoid repeated `to_string` allocations and potential O(n²) behavior when many `<think>` blocks are present.
- If you expect models to sometimes include attributes or casing variants on the thinking tag (e.g. `<think reason="true">` or `<THINK>`), you may want to extend `strip_thinking_blocks` to handle those forms as well while still keeping the conservative behavior.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Review feedback pointed out that repeated replace_range passes could become quadratic and that provider tags may vary by casing or attributes. The parser now scans the response once, allocates only when a complete think block is removed, and recognizes explicit think tags with optional attributes and ASCII casing variants. Constraint: Keep cleanup conservative and avoid localized heading stripping. Rejected: Regex dependency | unnecessary for one small tag parser. Rejected: Strip broad reasoning headings | can remove valid user-facing prose. Confidence: medium Scope-risk: narrow Tested: cargo test polish::tests -- --nocapture Tested: cargo check Tested: npm run build
Collaborator
Author
|
@sourcery-ai review |
appergb
pushed a commit
that referenced
this pull request
Apr 30, 2026
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
摘要
Fixes #25。
本 PR 解决 MiniMax 等带深度思考输出的模型会把 reasoning / thinking 内容一并插入到最终文本里的问题。
部分 OpenAI-compatible 思考模型会在最终回答前返回显式的
<think>...</think>内容。此前 polish 流程会把这部分内容当作普通文本继续处理,导致用户输入框中被插入很长的思考块,影响实际输入体验。本次改动在 response parsing 之后、最终插入文本之前,增加保守的清洗逻辑:只移除明确的
<think>...</think>块,保留最终润色文本。修复 / 新增 / 改进
在
clean_polish_output中增加 thinking block 清洗步骤。新增
strip_thinking_blocks:<think>...</think>块保持现有 provider 请求逻辑不变。
保持现有 UI 行为不变。
保持现有 fallback 行为不变。
新增测试覆盖:
<think>...</think>内容会被移除兼容
不包含:
对现有用户 / 本地环境 / 构建流程的影响:
<think>...</think>思考块。测试计划
命令:
cargo test polish::tests -- --nocapture结果:通过
证据路径:本地测试输出
命令:
cargo check结果:通过
证据路径:本地检查输出
主要改动文件
openless-all/app/src-tauri/src/polish.rs备注
本 PR 采用保守清洗策略,只移除有稳定标签边界的
<think>...</think>内容。没有选择按“深度思考”“思考过程”等本地化标题文本清洗,避免误删正常用户可见内容。Summary by Sourcery
Filter out model reasoning blocks from polish output so only the final user-facing text is inserted.
Bug Fixes:
<think>...</think>reasoning sections from polish results to prevent deep-thinking model internals from being inserted into the user input field.Tests:
<think>blocks while preserving the final polished text.