From 85b67a69a019f4b5d39621396ca44ce3759256fc Mon Sep 17 00:00:00 2001 From: juwpegatron Date: Tue, 24 Nov 2026 13:47:47 +0700 Subject: [PATCH] Lingua: strip reasoning blocks also when the model closes them as (MiniMax-M3 on the company gateway showed its thinking in the Text desk); sw cache v7 --- apps/lingua/engine/maas.py | 5 +++++ apps/lingua/engine/text_desk.py | 6 ++++-- apps/lingua/tests/test_maas.py | 4 ++++ apps/lingua/tests/test_text_desk.py | 1 + apps/lingua/web/assets/lingua-core.js | 2 ++ apps/lingua/web/sw.js | 2 +- 6 files changed, 17 insertions(+), 3 deletions(-) diff --git a/apps/lingua/engine/maas.py b/apps/lingua/engine/maas.py index 4fe290b7..9399773c 100644 --- a/apps/lingua/engine/maas.py +++ b/apps/lingua/engine/maas.py @@ -17,6 +17,7 @@ import json import time import os +import re import requests @@ -121,6 +122,9 @@ def reset_model_health() -> None: "हिन्दी": "Hindi (हिन्दी)", } +#: Reasoning blocks a model may put before its answer, also namespaced ones (...). +_THINK_RE = re.compile(r"<(?:[\w-]+:)?think>.*?\s*", re.DOTALL) + #: Pairs recognised when stripping cosmetic wrapping quotes off model output. _QUOTE_PAIRS = {'"': '"', "'": "'", "\u201c": "\u201d", "\u2018": "\u2019"} @@ -169,6 +173,7 @@ def _clean(text: str) -> str: """Strip leading blank lines and cosmetic wrapping quotes from model output.""" if not isinstance(text, str): return "" + text = _THINK_RE.sub("", text) lines = text.split("\n") while lines and not lines[0].strip(): lines.pop(0) diff --git a/apps/lingua/engine/text_desk.py b/apps/lingua/engine/text_desk.py index 021c0495..f798373e 100644 --- a/apps/lingua/engine/text_desk.py +++ b/apps/lingua/engine/text_desk.py @@ -44,7 +44,9 @@ URL_RE = re.compile(r"https?://[^\s<>]*[^\s<>.,;:!?)\]}'\"\u3002\uff0c\u3001]") NUMBER_RE = re.compile(r"\d+(?:[.,:]\d+)*") -THINK_RE = re.compile(r".*?", re.DOTALL) +# Reasoning blocks, also namespaced or mismatched ones such as ... (MiniMax-M3 on the gateway). +THINK_RE = re.compile(r"<(?:[\w-]+:)?think>.*?\s*", re.DOTALL) +THINK_TAG_RE = re.compile(r"") _converters: dict = {} @@ -180,7 +182,7 @@ def review_warnings(job: TextJob, translated: str) -> list[str]: if (entry["source"].casefold() in job.text.casefold() and entry["target"].casefold() not in translated.casefold()): warnings.append("Check glossary: preferred term '{}' was not used.".format(entry["target"])) - if "" in translated or "" in translated: + if THINK_TAG_RE.search(translated): warnings.append("The model emitted reasoning tags; prefer qwen3:8b.") return warnings diff --git a/apps/lingua/tests/test_maas.py b/apps/lingua/tests/test_maas.py index c60fa8d1..3e20cc65 100644 --- a/apps/lingua/tests/test_maas.py +++ b/apps/lingua/tests/test_maas.py @@ -110,6 +110,10 @@ def test_translate_strips_leading_blank_lines(self): result, _ = self._call("Hello", "English", content="\n\nBonjour\n") self.assertEqual(result, "Bonjour") + def test_translate_strips_reasoning_blocks(self): + result, _ = self._call("Hello", "French", content="the user wants French\nBonjour") + self.assertEqual(result, "Bonjour") + class ErrorHandlingTests(unittest.TestCase): def test_http_error_raises_maas_error(self): diff --git a/apps/lingua/tests/test_text_desk.py b/apps/lingua/tests/test_text_desk.py index 04424bbf..c504e5f1 100644 --- a/apps/lingua/tests/test_text_desk.py +++ b/apps/lingua/tests/test_text_desk.py @@ -48,6 +48,7 @@ def test_prompt_treats_text_as_data(self): def test_finish_and_script(self): self.assertEqual(text_desk.finish("hmm\n\"会议室\"", TW), "會議室") + self.assertEqual(text_desk.finish("let me see\n- meeting room\n会议室", TW), "會議室") self.assertEqual(text_desk.finish("會議室 https://a.b/會", "Simplified Chinese (简体中文)"), "会议室 https://a.b/會") self.assertEqual(text_desk.finish("Hello", "English"), "Hello") diff --git a/apps/lingua/web/assets/lingua-core.js b/apps/lingua/web/assets/lingua-core.js index 4572c6f3..2b7fb8bb 100644 --- a/apps/lingua/web/assets/lingua-core.js +++ b/apps/lingua/web/assets/lingua-core.js @@ -345,6 +345,8 @@ function isMatchingQuotePair(open, close) { // Strip leading blank lines and cosmetic wrapping quotes from model output. export function clean(text) { if (typeof text !== "string") return ""; + // Reasoning blocks a model may put before its answer, also namespaced ones (...). + text = text.replace(/<(?:[\w-]+:)?think>[\s\S]*?<\/(?:[\w-]+:)?think>\s*/g, ""); let lines = text.split("\n"); while (lines.length > 0 && lines[0].trim() === "") lines.shift(); let out = lines.join("\n").trim(); diff --git a/apps/lingua/web/sw.js b/apps/lingua/web/sw.js index e27ba1b3..87dee044 100644 --- a/apps/lingua/web/sw.js +++ b/apps/lingua/web/sw.js @@ -4,7 +4,7 @@ * or intercepted — every call hits the network so the key in localStorage * stays usable and any model change on the gateway is picked up immediately. */ -const CACHE = 'lingua-cache-v6'; +const CACHE = 'lingua-cache-v7'; const PRECACHE = [ './',