Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(reasoning): make stripThinkingTags case-insensitive and clean stray closing tags (#1801) (#1802)

Henry Su909ee586b04f
diff --git a/src/helpers/stripThinking.js b/src/helpers/stripThinking.js
index 39d682b10..8ba2bbf58 100644
--- a/src/helpers/stripThinking.js
+++ b/src/helpers/stripThinking.js
@@ -1,14 +1,18 @@
// Strip <think>...</think> blocks (including unterminated ones) emitted by
// reasoning models. Innermost closed pairs are removed first so nested blocks
-// cannot leave a stray </think>, then a trailing unterminated <think> is removed.
+// cannot leave a stray </think>, then a trailing unterminated <think> is removed,
+// and finally any orphan </think> tags are stripped.
export function stripThinkingTags(text) {
if (typeof text !== "string") return text;
- const innermostClosed = /<think>(?:(?!<think>)[\s\S])*?<\/think>/;
+ const innermostClosed = /<think>(?:(?!<think>)[\s\S])*?<\/think>/i;
let out = text;
let next = out.replace(innermostClosed, "");
while (next !== out) {
out = next;
next = out.replace(innermostClosed, "");
}
- return next.replace(/<think>[\s\S]*$/, "").trim();
+ return next
+ .replace(/<think>[\s\S]*$/i, "")
+ .replace(/<\/think>/gi, "")
+ .trim();
}
diff --git a/test/helpers/stripThinking.test.js b/test/helpers/stripThinking.test.js
index 1432b6341..8615107bf 100644
--- a/test/helpers/stripThinking.test.js
+++ b/test/helpers/stripThinking.test.js
@@ -53,3 +53,12 @@ test("still keeps sequential (non-nested) think blocks from leaking", async () =
const { stripThinkingTags } = await load();
assert.equal(stripThinkingTags("<think>one</think>Keep<think>two</think>This"), "KeepThis");
});
+
+test("strips mixed-case think tags and handles stray closing tags", async () => {
+ const { stripThinkingTags } = await load();
+ assert.equal(stripThinkingTags("<Think>reasoning</Think>Meeting Notes"), "Meeting Notes");
+ assert.equal(stripThinkingTags("<THINK>reasoning</THINK>Meeting Notes"), "Meeting Notes");
+ assert.equal(stripThinkingTags("</think>Meeting Notes"), "Meeting Notes");
+ assert.equal(stripThinkingTags("</Think>Meeting Notes"), "Meeting Notes");
+ assert.equal(stripThinkingTags("Title<THINK>unterminated"), "Title");
+});