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(a2ui): 修复半标记误判导致的流中断与超长 chat.final

KevinLLLove3cb75216964b
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface.py b/jiuwenswarm/server/runtime/agent_adapter/interface.py
index a840b4a55f..8fb4e4d107 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface.py
@@ -66,7 +66,10 @@ from jiuwenswarm.common.utils import (
get_env_file,
reset_free_search_runtime_flags,
)
-from jiuwenswarm.server.runtime.a2ui.integration import finalize_assistant_response_if_a2ui
+from jiuwenswarm.server.runtime.a2ui.integration import (
+ finalize_assistant_response_if_a2ui,
+ is_a2ui_channel,
+)
from jiuwenswarm.server.runtime.a2ui.runtime.finalizer import should_finalize_a2ui_content
from jiuwenswarm.agents.harness.common.auto_memory import (
_execute_auto_memory_extraction,
@@ -320,6 +323,9 @@ _A2UI_STREAM_PARTIAL_MARKERS = (
"dataModelUpdate",
"deleteSurface",
)
+# 过短 token(如 data、begin、<a、delete、surface)在普通 HTML/JS/JSON 中
+# 高频出现,不得命中半标记判定。8 是过滤所有已知误词的最小安全长度。
+_A2UI_PARTIAL_MARKER_MIN_LEN = 8
_A2UI_PENDING_RENDER_DELTA = "<a2ui-json>\n"
@@ -413,7 +419,7 @@ def _looks_like_partial_a2ui_marker(value: Any) -> bool:
if match is None:
continue
token = match.group(0)
- if len(token) < 2:
+ if len(token) < _A2UI_PARTIAL_MARKER_MIN_LEN:
continue
rest = candidate[len(token):].strip()
if rest and not any(marker.startswith(token + rest) for marker in _A2UI_STREAM_PARTIAL_MARKERS):
@@ -468,7 +474,7 @@ def _a2ui_marker_start(value: Any) -> int | None:
if match is None:
continue
token = match.group(0)
- if len(token) < 2:
+ if len(token) < _A2UI_PARTIAL_MARKER_MIN_LEN:
continue
rest = candidate[len(token):].strip()
if rest and not any(marker.startswith(token + rest) for marker in _A2UI_STREAM_PARTIAL_MARKERS):
@@ -3208,6 +3214,7 @@ class JiuWenSwarm:
suppress_a2ui_stream = False
a2ui_pending_render_sent = False
a2ui_stream_probe = ""
+ a2ui_probe_enabled = is_a2ui_channel(cid)
_yielded_from_queue = 0
logger.info(
"[JiuWenSwarm] consumer loop starting: request_id=%s is_team=%s is_first=%s",
@@ -3284,7 +3291,7 @@ class JiuWenSwarm:
payload_content = str(data.payload.get("content", ""))
a2ui_split = None
- if et in {"chat.delta", "chat.final"} and payload_content:
+ if a2ui_probe_enabled and et in {"chat.delta", "chat.final"} and payload_content:
a2ui_split = _split_a2ui_stream_content(a2ui_stream_probe, payload_content)
a2ui_stream_probe = _extend_a2ui_stream_probe(a2ui_stream_probe, payload_content)
if _should_defer_a2ui_processing_status(
@@ -3486,7 +3493,7 @@ class JiuWenSwarm:
payload_content = str(data.get("content", ""))
a2ui_split = None
- if et in {"chat.delta", "chat.final"} and payload_content:
+ if a2ui_probe_enabled and et in {"chat.delta", "chat.final"} and payload_content:
a2ui_split = _split_a2ui_stream_content(a2ui_stream_probe, payload_content)
a2ui_stream_probe = _extend_a2ui_stream_probe(a2ui_stream_probe, payload_content)
if _should_defer_a2ui_processing_status(
diff --git a/tests/unit_tests/a2ui/test_integration_bridge.py b/tests/unit_tests/a2ui/test_integration_bridge.py
index 33726a52ab..2e131381a6 100644
--- a/tests/unit_tests/a2ui/test_integration_bridge.py
+++ b/tests/unit_tests/a2ui/test_integration_bridge.py
@@ -153,11 +153,11 @@ def test_a2ui_stream_probe_detects_split_protocol_marker():
)
probe = ""
- probe = _extend_a2ui_stream_probe(probe, "好的,开始整理。\n\nbeg")
+ probe = _extend_a2ui_stream_probe(probe, "好的,开始整理。\n\nbeginRend")
assert _stream_probe_has_a2ui_marker(probe) is True
- probe = _extend_a2ui_stream_probe(probe, "inRend")
+ probe = _extend_a2ui_stream_probe(probe, "ering")
assert _stream_probe_has_a2ui_marker(probe) is True
@@ -202,21 +202,30 @@ def test_split_a2ui_stream_content_keeps_prefix_streamable():
def test_split_a2ui_stream_content_handles_partial_marker():
- """A partial marker should suppress only the marker line."""
+ """A partial marker long enough to be distinctive should suppress only the marker line."""
from jiuwenswarm.server.runtime.agent_adapter.interface import _split_a2ui_stream_content
- split = _split_a2ui_stream_content("", "现在为你展示结果。\n\nbeg")
+ split = _split_a2ui_stream_content("", "现在为你展示结果。\n\nbeginRend")
- assert split == ("现在为你展示结果。\n\n", "beg")
+ assert split == ("现在为你展示结果。\n\n", "beginRend")
-def test_split_a2ui_stream_content_suppresses_two_character_tag_prefix():
- """A tokenizer chunk containing only '<a' must not leak into visible text."""
+def test_split_a2ui_stream_content_ignores_short_tag_prefix():
+ """A short '<a' fragment must not trigger A2UI suppression (false-positive guard)."""
from jiuwenswarm.server.runtime.agent_adapter.interface import _split_a2ui_stream_content
split = _split_a2ui_stream_content("", "现在为你展示结果。\n\n<a")
- assert split == ("现在为你展示结果。\n\n", "<a")
+ assert split is None
+
+
+def test_split_a2ui_stream_content_suppresses_long_tag_prefix():
+ """A distinctive '<a2ui-js' prefix should trigger suppression."""
+ from jiuwenswarm.server.runtime.agent_adapter.interface import _split_a2ui_stream_content
+
+ split = _split_a2ui_stream_content("", "现在为你展示结果。\n\n<a2ui-js")
+
+ assert split == ("现在为你展示结果。\n\n", "<a2ui-js")
def test_a2ui_pending_render_delta_stays_open():
@@ -378,3 +387,66 @@ async def test_finalize_assistant_response_if_a2ui_bypasses_non_web_channel(monk
)
assert result == content
+
+
+def test_partial_marker_ignores_short_data_token():
+ """'data' alone must not trigger A2UI suppression (ECharts false-positive)."""
+ from jiuwenswarm.server.runtime.agent_adapter.interface import (
+ _looks_like_partial_a2ui_marker,
+ )
+
+ assert _looks_like_partial_a2ui_marker("some content\ndata") is False
+
+
+def test_partial_marker_ignores_data_with_array():
+ """'data: [1,2,3]' must not trigger A2UI suppression."""
+ from jiuwenswarm.server.runtime.agent_adapter.interface import (
+ _looks_like_partial_a2ui_marker,
+ )
+
+ assert _looks_like_partial_a2ui_marker("chart.setOption({\ndata: [1, 2, 3]\n})") is False
+
+
+def test_partial_marker_ignores_aside_tag():
+ """'<aside>' must not trigger A2UI suppression (HTML false-positive)."""
+ from jiuwenswarm.server.runtime.agent_adapter.interface import (
+ _looks_like_partial_a2ui_marker,
+ )
+
+ assert _looks_like_partial_a2ui_marker("content\n<aside>") is False
+
+
+def test_partial_marker_ignores_beginning_word():
+ """'beginning' must not trigger A2UI suppression."""
+ from jiuwenswarm.server.runtime.agent_adapter.interface import (
+ _looks_like_partial_a2ui_marker,
+ )
+
+ assert _looks_like_partial_a2ui_marker("in the beginning") is False
+
+
+def test_partial_marker_ignores_delete_token():
+ """'delete' alone must not trigger A2UI suppression."""
+ from jiuwenswarm.server.runtime.agent_adapter.interface import (
+ _looks_like_partial_a2ui_marker,
+ )
+
+ assert _looks_like_partial_a2ui_marker("code\n\ndelete") is False
+
+
+def test_partial_marker_ignores_surface_token():
+ """'surface' alone must not trigger A2UI suppression."""
+ from jiuwenswarm.server.runtime.agent_adapter.interface import (
+ _looks_like_partial_a2ui_marker,
+ )
+
+ assert _looks_like_partial_a2ui_marker("render\nsurface") is False
+
+
+def test_partial_marker_triggers_on_long_dataModel_prefix():
+ """'dataModel' is long enough to be distinctive and should trigger."""
+ from jiuwenswarm.server.runtime.agent_adapter.interface import (
+ _looks_like_partial_a2ui_marker,
+ )
+
+ assert _looks_like_partial_a2ui_marker("content\ndataModel") is True