diff --git a/openless-all/app/src-tauri/src/polish.rs b/openless-all/app/src-tauri/src/polish.rs index 6f01d8f4..2511869f 100644 --- a/openless-all/app/src-tauri/src/polish.rs +++ b/openless-all/app/src-tauri/src/polish.rs @@ -223,7 +223,8 @@ fn extract_assistant_content(body: &str) -> Result { /// an iterative trim — if the model stacks two boilerplate sentences we'll still /// strip both. fn clean_polish_output(content: &str) -> String { - let trimmed = content.trim(); + let without_thinking = strip_thinking_blocks(content); + let trimmed = without_thinking.trim(); let stripped = strip_markdown_fence(trimmed); let mut output = stripped.to_string(); @@ -239,6 +240,26 @@ fn clean_polish_output(content: &str) -> String { output.trim().to_string() } +/// Strip model reasoning blocks so only the final polished text is inserted. +/// +/// Thinking-capable OpenAI-compatible models commonly return their reasoning in +/// `...` before the final answer, so keep this as a +/// conservative cleanup layer after parsing `message.content` instead of +/// provider-specific handling. +fn strip_thinking_blocks(text: &str) -> String { + let mut output = text.to_string(); + + while let Some(start) = output.find("") { + let Some(end_from_start) = output[start..].find("") else { + break; + }; + let end = start + end_from_start + "".len(); + output.replace_range(start..end, ""); + } + + output +} + fn strip_markdown_fence(text: &str) -> &str { if !(text.starts_with("```") && text.ends_with("```")) { return text; @@ -348,3 +369,16 @@ pub mod prompts { ) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn clean_polish_output_strips_think_tag_block() { + let content = + "先分析用户意图。\n这里可能很长。\n\n请明天上午十点提醒我开会。"; + + assert_eq!(clean_polish_output(content), "请明天上午十点提醒我开会。"); + } +}