fix: remove <think> blocks from chat title generation

- Update removeCodeBlocksAndTrim to handle <think> blocks from models like qwen3
- Add comprehensive test coverage for think block removal
- Fixes issue where chat titles showed thinking process instead of content

Fixes #5689
This commit is contained in:
Ayyan Shaikh 2025-10-14 09:38:54 +00:00
parent 4aa138fd0c
commit 535c9bf2bb
2 changed files with 32 additions and 4 deletions

View file

@ -1,9 +1,9 @@
// File generated by Continue
import {
copyOf,
dedent,
dedentAndGetCommonWhitespace,
deduplicateArray,
copyOf,
getMarkdownLanguageTagForFile,
removeCodeBlocksAndTrim,
removeQuotesAndEscapes,
@ -463,4 +463,30 @@ describe("removeCodeBlocksAndTrim", () => {
const output = removeCodeBlocksAndTrim(text);
expect(output).toBe("");
});
it("should remove think blocks from text", () => {
const text =
"<think>Okay, the user wants me to help with...</think>Here is my actual response.";
const output = removeCodeBlocksAndTrim(text);
expect(output).toBe("Here is my actual response.");
});
it("should remove both code blocks and think blocks", () => {
const text =
"<think>Let me plan this...</think>Here's some code:\n```javascript\nconsole.log('hello');\n```\nThat should work!";
const output = removeCodeBlocksAndTrim(text);
expect(output).toBe("Here's some code:\n\nThat should work!");
});
it("should handle multiple think blocks", () => {
const text =
"<think>First thought</think>Some text<think>Second thought</think>More text";
const output = removeCodeBlocksAndTrim(text);
expect(output).toBe("Some textMore text");
});
it("should handle think blocks with newlines", () => {
const text = "<think>\nMultiline\nthinking\n</think>\nActual response";
const output = removeCodeBlocksAndTrim(text);
expect(output).toBe("Actual response");
});
});

View file

@ -197,11 +197,13 @@ export function dedent(strings: TemplateStringsArray, ...values: any[]) {
*/
export function removeCodeBlocksAndTrim(text: string): string {
const codeBlockRegex = /```[\s\S]*?```/g;
const thinkBlockRegex = /<think>[\s\S]*?<\/think>/g;
// Remove code blocks from the message text
const textWithoutCodeBlocks = text.replace(codeBlockRegex, "");
// Remove code blocks and think blocks from the message text
let processedText = text.replace(codeBlockRegex, "");
processedText = processedText.replace(thinkBlockRegex, "");
return textWithoutCodeBlocks.trim();
return processedText.trim();
}
export function splitCamelCaseAndNonAlphaNumeric(value: string) {