Skip to content

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(agentserver): make SSE response patch reversible

guozirui18c38ab2dc98d
diff --git a/jiuwenswarm/llm_sse_patch.py b/jiuwenswarm/llm_sse_patch.py
index a6c3840809..7589ee662d 100644
--- a/jiuwenswarm/llm_sse_patch.py
+++ b/jiuwenswarm/llm_sse_patch.py
@@ -436,6 +436,9 @@ def apply_openai_response_assembly_patch() -> None:
return
_orig_parse_response = client_cls._parse_response # pylint: disable=protected-access
+ client_cls._response_assembly_original_parse_response = ( # pylint: disable=protected-access
+ _orig_parse_response
+ )
async def _parse_response_with_sse_guard(
self: Any,
@@ -452,6 +455,36 @@ def apply_openai_response_assembly_patch() -> None:
logger.info("[llm_sse_patch] OpenAIModelClient SSE 响应组装补丁已应用")
+def remove_openai_response_assembly_patch() -> bool:
+ """移除按渠道启用的 SSE 响应组装补丁,并恢复原始解析方法。
+
+ 运行时配置可能删除或禁用 ``xiaoyi_claw``。此时必须撤销进程级补丁,避免
+ 后续其他渠道的普通字符串响应继续经过 SSE 组装逻辑。
+ """
+ global _RESPONSE_ASSEMBLY_PATCH_APPLIED
+
+ client_cls = _import_openai_model_client()
+ if client_cls is None:
+ return False
+
+ original = getattr(
+ client_cls,
+ "_response_assembly_original_parse_response",
+ None,
+ )
+ if original is None:
+ _RESPONSE_ASSEMBLY_PATCH_APPLIED = False
+ client_cls._response_assembly_patch_applied = False # pylint: disable=protected-access
+ return False
+
+ client_cls._parse_response = original # pylint: disable=protected-access
+ delattr(client_cls, "_response_assembly_original_parse_response")
+ client_cls._response_assembly_patch_applied = False # pylint: disable=protected-access
+ _RESPONSE_ASSEMBLY_PATCH_APPLIED = False
+ logger.info("[llm_sse_patch] OpenAIModelClient SSE 响应组装补丁已移除")
+ return True
+
+
def apply_glm_tool_xml_sanitize_patch() -> None:
"""Patch 2: GLM XML 标签清洗 —— 剥离流式 chunk 中 tool_call.arguments 的原生 XML 标签。
diff --git a/jiuwenswarm/server/app_agentserver.py b/jiuwenswarm/server/app_agentserver.py
index 298d57fb54..a650dfcb50 100644
--- a/jiuwenswarm/server/app_agentserver.py
+++ b/jiuwenswarm/server/app_agentserver.py
@@ -153,6 +153,7 @@ from jiuwenswarm.llm_sse_patch import (
apply_huawei_maas_span_id_patch,
apply_openai_auth_header_patch,
apply_openai_response_assembly_patch,
+ remove_openai_response_assembly_patch,
)
apply_openai_auth_header_patch()
@@ -193,12 +194,13 @@ def _should_apply_sse_invoke_patch() -> bool:
def refresh_openai_response_assembly_patch() -> bool:
- """Apply the SSE response assembly patch when the current config needs it.
+ """Synchronize the SSE response assembly patch with the current config.
- The patch is idempotent, so this can be called both at process startup and
- after a runtime config reload that enables ``xiaoyi_claw``.
+ Both operations are idempotent, so this can be called at process startup and
+ after a runtime config reload that enables or disables ``xiaoyi_claw``.
"""
if not _should_apply_sse_invoke_patch():
+ remove_openai_response_assembly_patch()
return False
apply_openai_response_assembly_patch()
return True
diff --git a/tests/unit_tests/test_llm_sse_patch_granularity.py b/tests/unit_tests/test_llm_sse_patch_granularity.py
index 6e5dc899dd..a7f7e1d034 100644
--- a/tests/unit_tests/test_llm_sse_patch_granularity.py
+++ b/tests/unit_tests/test_llm_sse_patch_granularity.py
@@ -47,6 +47,11 @@ def fresh_patch_state(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(patch_mod, name, False, raising=False)
for name in _CLIENT_PATCH_FLAGS:
monkeypatch.setattr(oai_mod.OpenAIModelClient, name, False, raising=False)
+ monkeypatch.delattr(
+ oai_mod.OpenAIModelClient,
+ "_response_assembly_original_parse_response",
+ raising=False,
+ )
monkeypatch.setattr(headers_helper, "_auth_header_patch_applied", False, raising=False)
@@ -186,3 +191,24 @@ def test_runtime_refresh_applies_patch_after_xiaoyi_claw_is_enabled(
config["channels"]["xiaoyi"]["apps"][0]["mode"] = "xiaoyi_claw"
assert app_agentserver.refresh_openai_response_assembly_patch() is True
assert calls == [True]
+
+
+def test_runtime_refresh_removes_patch_after_xiaoyi_claw_is_disabled(
+ monkeypatch: pytest.MonkeyPatch,
+ fresh_patch_state: None,
+) -> None:
+ """运行时禁用 xiaoyi_claw 后应恢复原方法,避免影响其他渠道。"""
+ config = {"channels": {"xiaoyi": {"apps": [{"mode": "xiaoyi_claw"}]}}}
+ _stub_config(monkeypatch, config)
+ original = oai_mod.OpenAIModelClient._parse_response # pylint: disable=protected-access
+
+ assert app_agentserver.refresh_openai_response_assembly_patch() is True
+ assert oai_mod.OpenAIModelClient._parse_response is not original # pylint: disable=protected-access
+
+ config["channels"]["xiaoyi"]["apps"][0]["mode"] = "chat"
+ assert app_agentserver.refresh_openai_response_assembly_patch() is False
+ assert oai_mod.OpenAIModelClient._parse_response is original # pylint: disable=protected-access
+ assert (
+ oai_mod.OpenAIModelClient._response_assembly_patch_applied # pylint: disable=protected-access
+ is False
+ )