Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
serve: tool_call_recovery reads a declared tool's JSON call in a Markdown fence inside <tool_call> (#2037)
Niko1221de603bb8bd43
diff --git a/serve/frontend.py b/serve/frontend.pyindex 27c747ff3..e8cc2335f 100644--- a/serve/frontend.py+++ b/serve/frontend.py@@ -722,9 +722,17 @@ def function_end(text: str) -> int:return -1+_JSON_FENCE = re.compile(r"```[ \t]*(?:json)?[ \t]*\r?\n\s*", re.I)++def json_tool_call(body: str, schemas: dict) -> ToolCall | None:"""`{"name": N, "arguments": {...}}` (or "parameters", or the arguments as a JSON string) of a declared tool -> its- call; anything else -> None."""+ call; anything else -> None. #2037: the object may sit in a Markdown fence (```json ... ```)."""+ body = body.strip()+ m = _JSON_FENCE.match(body)+ if m:+ body = body[m.end():].rstrip()+ body = body[:-3] if body.endswith("```") else bodytry:obj = json.loads(body)except ValueError:@@ -828,6 +836,15 @@ class OutputParser:return "param" if after[len(PARAM_START):gt] in self.schemas else "text"if after.startswith("{"):return "json" if self.schemas else "text"+ if after.startswith("```") or (after and "```".startswith(after)):+ # #2037: the JSON call inside a Markdown fence (```json ... ```) in the explicit wrapper+ m = _JSON_FENCE.match(after)+ if m:+ rest = after[m.end():]+ if rest.startswith("{"):+ return "json" if self.schemas else "text"+ return "wait" if not rest else "text"+ return "wait" if "\n" not in after else "text"if not after or FUNC_START.startswith(after) or PARAM_START.startswith(after):return "wait"return "text"diff --git a/serve/test_frontend.py b/serve/test_frontend.pyindex 801ad8c9c..f4fe3e686 100644--- a/serve/test_frontend.py+++ b/serve/test_frontend.py@@ -232,3 +232,43 @@ class QuotedToolCallCloser(unittest.TestCase):text = "<tool_call>x</tool_call>"ids, _ = self.encode("assistant", text)self.assertEqual(ids, self.tok.encode(text, parse_special=True))+++class FencedJsonCallInWrapper(unittest.TestCase):+ """#2037: with recovery on, a declared tool's JSON call in a Markdown fence inside <tool_call> is the call."""+ TOOLS = [{"name": "write_file", "parameters": {"type": "object", "properties": {"content": {"type": "string"}}}}]+ PAYLOAD = '{"name": "write_file", "arguments": {"content": "hello `world`"}}'++ def run_parser(self, raw, chunk, stream_tools, recover=True, tools=None):+ from serve.frontend import OutputParser+ p = OutputParser(thinking=False, tools=self.TOOLS if tools is None else tools, recover=recover,+ stream_tools=stream_tools)+ events = []+ for i in range(0, len(raw), chunk):+ events += p.feed(raw[i:i + chunk])+ events += p.finish()+ return ([e.call.arguments for e in events if e.kind == "tool_call"],+ "".join(e.text for e in events if e.kind == "content"))++ def test_fenced_and_bare_agree(self):+ for fence in ("```json\n", "```\n", "```JSON\r\n"):+ raw = f"<tool_call>\n{fence}{self.PAYLOAD}\n```\n</tool_call>"+ for chunk in (len(raw), 1, 7):+ for st in (False, True):+ with self.subTest(fence=fence, chunk=chunk, stream_tools=st):+ self.assertEqual(self.run_parser(raw, chunk, st), ([{"content": "hello `world`"}], ""))++ def test_other_fences_and_prose_stay_text(self):+ raw = "<tool_call>\n```python\nx = 1\n```\n</tool_call>"+ for chunk in (len(raw), 1):+ self.assertEqual(self.run_parser(raw, chunk, False), ([], raw))+ raw = f"Here:\n```json\n{self.PAYLOAD}\n```\n" # no wrapper: just text+ self.assertEqual(self.run_parser(raw, len(raw), False), ([], raw))++ def test_off_without_recovery_or_for_an_undeclared_tool(self):+ raw = f"<tool_call>\n```json\n{self.PAYLOAD}\n```\n</tool_call>"+ calls, text = self.run_parser(raw, len(raw), False, recover=False)+ self.assertEqual((calls, text), ([], raw))+ other = [{"name": "other", "parameters": {}}]+ calls, text = self.run_parser(raw, len(raw), False, tools=other)+ self.assertEqual((calls, text), ([], raw))