JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
【回合】fix: 清洗GLM原生工具调用xml格式未剥离干净的标签
d3002969619f2525a7cf6
diff --git a/jiuwenswarm/llm_sse_patch.py b/jiuwenswarm/llm_sse_patch.pyindex 5333a787c6..352b819a23 100644--- a/jiuwenswarm/llm_sse_patch.py+++ b/jiuwenswarm/llm_sse_patch.py@@ -1,25 +1,67 @@# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.-"""Runtime patch: make non-streaming OpenAI invoke() tolerate SSE-only gateways.+"""Runtime patches for OpenAIModelClient.-部分网关(如 celia-claw sse-api)即使在非流式调用下也只返回 ``text/event-stream``-文本,此时 openai SDK 交回给框架的 ``response`` 会是 ``str`` 而非 ``ChatCompletion``,-导致 ``response.choices`` 抛出 ``'str' object has no attribute 'choices'``,进而让-subagent / 心跳等走 ``invoke()`` 的非流式路径全部失败。+1. SSE-only 网关兼容:部分网关(如 celia-claw sse-api)即使在非流式调用下也只返回+ ``text/event-stream`` 文本,此时 openai SDK 交回给框架的 ``response`` 会是 ``str``+ 而非 ``ChatCompletion``,导致 ``response.choices`` 抛出异常。补丁在收到 ``str``+ 响应时,先把 SSE 文本组装成标准的 ``ChatCompletion`` 再交回原解析逻辑。-这里在服务启动时给 ``OpenAIModelClient._parse_response`` 打一个补丁:当收到 ``str``-响应时,先把 SSE 文本组装成标准的 ``ChatCompletion`` 再交回原解析逻辑。+2. GLM XML 标签清洗:流式场景下 GLM 调用原生工具的 XML 标签(如 ``<arg_value>``、+ ``<arg_key>``、``<tool_call>``)未剥离干净,会污染后续 todo_list、LLM 上下文及+ 前端。补丁在解析流式 chunk 和非流式响应时,对 tool_call.arguments 做标签清洗。"""from __future__ import annotationsimport jsonimport logging+import reimport timefrom typing import Any, Optionallogger = logging.getLogger("jiuwenswarm.llm_sse_patch")+_GLM_TOOL_XML_CLOSED_RE = re.compile(+ r"<(arg_value|arg_key|tool_call)[^>]*?>.*?</\1>",+ re.IGNORECASE | re.DOTALL,+)+_GLM_TOOL_XML_TRUNCATED_OPEN_RE = re.compile(+ r"^.*?<(?:arg_value|arg_key|tool_call)[^>]*?>",+ re.IGNORECASE | re.DOTALL,+)+++def _sanitize_glm_tool_xml_tags(raw: str) -> str:+ """Strip GLM native XML tags and their inner content.++ Complete closed tags: remove entire segment including inner content.+ The closing tag name must match the opening tag name, so nested tags+ of different names are handled correctly without leaving stray+ closing tags behind.+ Truncated open tags: delete preceding text + the tag itself,+ preserve content after the tag.+ e.g. 'prefix<tool_call...>suffix' -> 'suffix'+ The ^-anchored regex is applied repeatedly until no truncated+ open tag remains, so multiple truncated tags are all removed.++ The early-exit guard checks for '<arg_' and '<tool_call' in+ lowercased input so that uppercase variants (e.g. <TOOL_CALL>)+ are also caught by the subsequent regex substitutions that use+ re.IGNORECASE.+ """+ if not raw:+ return raw+ lowered = raw.lower()+ if "<arg_" not in lowered and "<tool_call" not in lowered:+ return raw+ result = _GLM_TOOL_XML_CLOSED_RE.sub("", raw)+ prev = None+ while prev != result:+ prev = result+ result = _GLM_TOOL_XML_TRUNCATED_OPEN_RE.sub("", result)+ return result+_PATCH_APPLIED = False@@ -55,7 +97,7 @@ def _build_tool_calls(msg: dict) -> list | None:type="function",function=Function(name=tc["function"]["name"],- arguments=tc["function"]["arguments"],+ arguments=_sanitize_glm_tool_xml_tags(tc["function"]["arguments"]),),)for tc in msg["tool_calls"]@@ -145,10 +187,13 @@ def assemble_openai_response(response: str) -> Any:def apply_openai_sse_invoke_patch() -> None:- """给 ``OpenAIModelClient._parse_response`` 打补丁以兼容 SSE-only 网关。+ """给 ``OpenAIModelClient`` 打补丁:++ 1. SSE-only 网关兼容:非流式调用下返回 str 时,先组装成标准 ChatCompletion。+ 2. GLM XML 标签清洗:流式 chunk 中 tool_call.arguments 的 XML 标签剥离。幂等:重复调用只生效一次。在服务启动早期调用即可覆盖 subagent / 心跳等- 所有走非流式 ``invoke()`` 的 LLM 调用。+ 所有走 ``invoke()`` / ``stream()`` 的 LLM 调用。"""global _PATCH_APPLIEDif _PATCH_APPLIED:@@ -166,8 +211,7 @@ def apply_openai_sse_invoke_patch() -> None:_PATCH_APPLIED = Truereturn- # monkeypatch 必须访问受保护成员 _parse_response 以包一层 SSE 兜底,- # 属于运行期补丁的正常诉求,豁免 G.CLS.11 protected-access。+ # --- Patch 1: _parse_response (SSE-only 网关兼容) ---_orig_parse_response = OpenAIModelClient._parse_response # pylint: disable=protected-accessasync def _parse_response_with_sse_guard(@@ -175,14 +219,31 @@ def apply_openai_sse_invoke_patch() -> None:response: Any,parser: Optional[Any] = None,):- # 非标准 OpenAI 格式:SSE-only 网关在非流式调用下仍返回 str 文本,- # 先组装成标准 ChatCompletion 再走原有解析逻辑。if isinstance(response, str):response = assemble_openai_response(response)return await _orig_parse_response(self, response, parser)- # 同上:运行期替换方法 + 打幂等标记,需写受保护属性。OpenAIModelClient._parse_response = _parse_response_with_sse_guard # pylint: disable=protected-access++ # --- Patch 2: _parse_stream_chunk (GLM XML 标签清洗) ---+ _orig_parse_stream_chunk = OpenAIModelClient._parse_stream_chunk # pylint: disable=protected-access++ def _parse_stream_chunk_with_sanitize(self: Any, chunk: Any):+ result = _orig_parse_stream_chunk(self, chunk)+ if result is not None and getattr(result, "tool_calls", None):+ for tc in result.tool_calls:+ orig_args = getattr(tc, "arguments", None)+ if isinstance(orig_args, str):+ sanitized = _sanitize_glm_tool_xml_tags(orig_args)+ if sanitized != orig_args:+ try:+ tc.arguments = sanitized+ except (AttributeError, TypeError):+ pass+ return result++ OpenAIModelClient._parse_stream_chunk = _parse_stream_chunk_with_sanitize # pylint: disable=protected-access+OpenAIModelClient._sse_invoke_patch_applied = True # pylint: disable=protected-access_PATCH_APPLIED = True- logger.info("[llm_sse_patch] OpenAIModelClient._parse_response SSE 兼容补丁已应用")+ logger.info("[llm_sse_patch] OpenAIModelClient SSE 兼容补丁 + GLM XML 标签清洗补丁已应用")diff --git a/tests/unit_tests/test_sanitize_glm_tool_xml_tags.py b/tests/unit_tests/test_sanitize_glm_tool_xml_tags.pynew file mode 100644index 0000000000..2d7f813c56--- /dev/null+++ b/tests/unit_tests/test_sanitize_glm_tool_xml_tags.py@@ -0,0 +1,162 @@+# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.+"""Unit tests for _sanitize_glm_tool_xml_tags in llm_sse_patch."""++from jiuwenswarm.llm_sse_patch import _sanitize_glm_tool_xml_tags+++class TestSanitizeGlmToolXmlTags:++ @staticmethod+ def test_empty_string():+ assert _sanitize_glm_tool_xml_tags("") == ""++ @staticmethod+ def test_no_tags_unchanged():+ raw = "plain text without any tags"+ assert _sanitize_glm_tool_xml_tags(raw) == raw++ @staticmethod+ def test_early_exit_no_match():+ raw = "text with no arg_ or tool_call markers"+ assert _sanitize_glm_tool_xml_tags(raw) == raw++ @staticmethod+ def test_truncated_tool_call_open_tag():+ raw = "prefix<tool_call>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "suffix"++ @staticmethod+ def test_truncated_arg_value_open_tag():+ raw = "prefix<arg_value>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "suffix"++ @staticmethod+ def test_truncated_arg_key_open_tag():+ raw = "prefix<arg_key>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "suffix"++ @staticmethod+ def test_closed_tool_call_tag_removed():+ raw = "prefix<tool_call>content</tool_call>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "prefixsuffix"++ @staticmethod+ def test_closed_arg_value_tag_removed():+ raw = "prefix<arg_value>inner</arg_value>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "prefixsuffix"++ @staticmethod+ def test_closed_arg_key_tag_removed():+ raw = "prefix<arg_key>inner</arg_key>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "prefixsuffix"++ @staticmethod+ def test_closed_tag_with_attributes():+ raw = "prefix<tool_call name='fn'>inner</tool_call>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "prefixsuffix"++ @staticmethod+ def test_closed_tag_multiline_content():+ raw = "prefix<arg_value>\nline1\nline2\n</arg_value>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "prefixsuffix"++ @staticmethod+ def test_multiple_closed_tags():+ raw = "start<arg_value>v1</arg_value>mid<arg_key>k1</arg_key>end"+ assert _sanitize_glm_tool_xml_tags(raw) == "startmidend"++ @staticmethod+ def test_only_closed_tag_no_surrounding():+ raw = "<arg_value>content</arg_value>"+ assert _sanitize_glm_tool_xml_tags(raw) == ""++ @staticmethod+ def test_self_closing_style_tag_strips_prefix():+ raw = "prefix<arg_value />suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "suffix"++ @staticmethod+ def test_closed_then_truncated_leaves_only_tail():+ raw = "start<arg_value>removed</arg_value>mid<tool_call>tail"+ assert _sanitize_glm_tool_xml_tags(raw) == "tail"++ @staticmethod+ def test_only_truncated_open_tag_no_suffix():+ raw = "prefix<tool_call>"+ assert _sanitize_glm_tool_xml_tags(raw) == ""++ @staticmethod+ def test_uppercase_tool_call_is_stripped():+ raw = "prefix<TOOL_CALL>inner</TOOL_CALL>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "prefixsuffix"++ @staticmethod+ def test_mixed_arg_and_tool_call_tags():+ raw = "a<arg_value>x</arg_value>b<arg_key>y</arg_key>c"+ assert _sanitize_glm_tool_xml_tags(raw) == "abc"++ @staticmethod+ def test_idempotent():+ raw = "prefix<arg_value>v</arg_value>suffix"+ first = _sanitize_glm_tool_xml_tags(raw)+ second = _sanitize_glm_tool_xml_tags(first)+ assert first == second++ @staticmethod+ def test_chinese_text_with_closed_tags():+ raw = "正在分析<arg_value>分析过程</arg_value>分析完成"+ assert _sanitize_glm_tool_xml_tags(raw) == "正在分析分析完成"++ @staticmethod+ def test_real_world_active_form():+ raw = "执行中<arg_key>step</arg_key>已完成"+ assert _sanitize_glm_tool_xml_tags(raw) == "执行中已完成"++ @staticmethod+ def test_real_world_tool_arguments():+ raw = '{"query": "<arg_value>search_term</arg_value>"}'+ expected = '{"query": ""}'+ assert _sanitize_glm_tool_xml_tags(raw) == expected++ @staticmethod+ def test_nested_closed_tags_removed_entirely():+ # Outer tool_call tag with inner arg_value: back-reference ensures+ # closing tag name matches opening tag name, so the entire outer+ # block is removed without leaving a stray closing tag.+ raw = "<tool_call><arg_value>deep</arg_value></tool_call>"+ assert _sanitize_glm_tool_xml_tags(raw) == ""++ @staticmethod+ def test_nested_closed_tags_with_prefix_suffix():+ raw = "prefix<tool_call><arg_value>deep</arg_value></tool_call>suffix"+ assert _sanitize_glm_tool_xml_tags(raw) == "prefixsuffix"++ @staticmethod+ def test_two_truncated_open_tags():+ raw = "prefix<arg_value>middle<tool_call>suffix"+ # Both truncated tags and their preceding text are removed iteratively:+ # Iteration 1: remove prefix + first open tag -> middle + second tag + suffix+ # Iteration 2: remove middle + second open tag -> suffix+ assert _sanitize_glm_tool_xml_tags(raw) == "suffix"++ @staticmethod+ def test_two_truncated_open_tags_no_prefix():+ raw = "<arg_value>text<tool_call>more"+ assert _sanitize_glm_tool_xml_tags(raw) == "more"++ @staticmethod+ def test_three_truncated_open_tags():+ raw = "<arg_value><arg_key><tool_call>final"+ assert _sanitize_glm_tool_xml_tags(raw) == "final"++ @staticmethod+ def test_closed_then_two_truncated_tags():+ raw = "start<arg_value>removed</arg_value>mid<arg_key>tail<tool_call>end"+ assert _sanitize_glm_tool_xml_tags(raw) == "end"++ @staticmethod+ def test_idempotent_with_multiple_truncated_tags():+ raw = "prefix<arg_value>middle<tool_call>suffix"+ first = _sanitize_glm_tool_xml_tags(raw)+ second = _sanitize_glm_tool_xml_tags(first)+ assert first == second