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: 透传 stream_source_id 修复 skill_turbo 并发节点流式输出交错

white2025cafb0c9a35b1
diff --git a/jiuwenswarm/common/e2a/gateway_normalize.py b/jiuwenswarm/common/e2a/gateway_normalize.py
index b0c1a2f430..ccf669e7ab 100644
--- a/jiuwenswarm/common/e2a/gateway_normalize.py
+++ b/jiuwenswarm/common/e2a/gateway_normalize.py
@@ -441,6 +441,18 @@ def e2a_response_from_agent_chunk(
_val = pl.get(_key)
if _val is not None:
body_chunk[_key] = _val
+ # 并发节点(skill_turbo p6_1_page_worker 等)用它标识 source,前端据其路由到
+ # subagent 行。chat.delta 走白名单构造,须显式透传(chat.reasoning 走 delta: pl
+ # 整包所以不受影响)。
+ _stream_source_id = pl.get("stream_source_id")
+ if _stream_source_id is not None:
+ body_chunk["stream_source_id"] = _stream_source_id
+ # relayclaw e2aToLegacyFrame 对 chat.delta 仅从 body.role 提取
+ # stream_source_id(delta 是字符串时 Object.assign 不执行),故
+ # 同时设 role 作兼容回退。role 仅 team 模式用于 leader/teammate 归属,
+ # 非 team 不影响 teamExpertId 解析(teamCtx 为 None)。
+ if pl.get("role") is None:
+ body_chunk["role"] = _stream_source_id
else:
body_chunk = {
"delta_kind": "custom",
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
index 54634474f2..ab972bdf36 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
@@ -133,6 +133,27 @@ _ERROR_EVENT = getattr(InteractionEventType, "EXECUTION_ERROR", None)
if _ERROR_EVENT is None:
_ERROR_EVENT = getattr(InteractionEventType, "RUNTIME_ERROR")
ERROR_EVENT_TYPE = _ERROR_EVENT.value
+# 与 skill_turbo/executor.STREAM_SOURCE_ID_FIELD 一致:并发节点用它标识 source,
+# 前端据其路由到 subagent 行。adapter 转发 llm_reasoning / llm_output 时须原样透传。
+STREAM_SOURCE_ID_FIELD = "stream_source_id"
+
+
+def _propagate_stream_source_id(
+ src_payload: Any, result: dict[str, Any] | None
+) -> dict[str, Any] | None:
+ """将上游 payload 中的 stream_source_id 透传到输出帧(原地写入,无则原样返回)。
+
+ skill_turbo 并发节点(如 p6_1_page_worker)用它标识 source,
+ 前端据其路由到 subagent 行,避免并发思考/正文混流。
+ """
+ if result is None or not isinstance(src_payload, dict):
+ return result
+ source_id = src_payload.get(STREAM_SOURCE_ID_FIELD)
+ if source_id:
+ result[STREAM_SOURCE_ID_FIELD] = source_id
+ return result
+
+
_DEEPRESEARCH_REWRITE_REPLAY_STATE_KEY = "deepresearch_rewrite_fast_path_replays"
_DEEPRESEARCH_REWRITE_REPLAY_SCHEMA_VERSION = 1
_DEEPRESEARCH_REWRITE_REPLAY_MAX_ENTRIES = 32
@@ -14372,6 +14393,10 @@ class JiuWenSwarmDeepAdapter:
# chat.invocation_paused 终结帧(而非普通完成帧),避免前端把"等待用户
# 输入"误判为"任务完成"。镜像 vendor(clowder-ai) 的 _detect_hitl_pause。
hitl_pending_stream = False
+ # HITL 检测到 ask_user 后,跳过后续所有 chunk(chat.final / chat.delta /
+ # chat.reasoning 等),由循环后的 chat.invocation_paused 终结帧收尾。
+ # 镜像 enterprise_dev 的 suppress_stream_after_hitl 机制。
+ suppress_stream_after_hitl = False
# Start of the wait for the runner's first chunk; every branch above
# has either handed the message over or attached to a running round.
runner_stream_started_at = time.monotonic()
@@ -14387,6 +14412,8 @@ class JiuWenSwarmDeepAdapter:
and not goal_stream_request
)
async for chunk in interaction_stream:
+ if suppress_stream_after_hitl:
+ continue
# [DIAG] HITL resume 调试:对照 forwarder([DeepAgent][fwd]) 转发的 chunk,
# 确认主循环实际从 interaction_stream 消费到什么类型。
_stream_ct = getattr(chunk, "type", None) or type(chunk).__name__
@@ -14510,6 +14537,7 @@ class JiuWenSwarmDeepAdapter:
)
if reasoning_payload is None:
continue
+ _propagate_stream_source_id(chunk.payload, reasoning_payload)
boundary = self._begin_visible_chat_content(stream_is_user_originated)
if boundary is not None:
yield AgentResponseChunk(
@@ -14536,6 +14564,7 @@ class JiuWenSwarmDeepAdapter:
delta_payload = self._stream_text_payload("chat.delta", content)
if delta_payload is None:
continue
+ _propagate_stream_source_id(chunk.payload, delta_payload)
has_streamed_content = True
if accumulated_reasoning:
yield AgentResponseChunk(
@@ -15105,9 +15134,10 @@ class JiuWenSwarmDeepAdapter:
content = (
payload.get("content", "") if isinstance(payload, dict) else str(payload)
)
- return JiuWenSwarmDeepAdapter._stream_text_payload(
+ delta_payload = JiuWenSwarmDeepAdapter._stream_text_payload(
"chat.delta", content
)
+ return _propagate_stream_source_id(payload, delta_payload)
if chunk_type == "llm_reasoning":
content = (
@@ -15115,9 +15145,10 @@ class JiuWenSwarmDeepAdapter:
if isinstance(payload, dict)
else str(payload)
)
- return JiuWenSwarmDeepAdapter._stream_text_payload(
+ reasoning_payload = JiuWenSwarmDeepAdapter._stream_text_payload(
"chat.reasoning", content
)
+ return _propagate_stream_source_id(payload, reasoning_payload)
if chunk_type == "content_chunk":
content = (
diff --git a/jiuwenswarm/server/utils/stream_utils.py b/jiuwenswarm/server/utils/stream_utils.py
index 198fd0c799..5136a6b4e6 100644
--- a/jiuwenswarm/server/utils/stream_utils.py
+++ b/jiuwenswarm/server/utils/stream_utils.py
@@ -10,6 +10,21 @@ from typing import Any
logger = logging.getLogger(__name__)
+def _propagate_stream_source_id(src_payload: Any) -> dict[str, Any]:
+ """从上游 payload 提取 stream_source_id(skill_turbo 并发节点用它标识 source)。
+
+ 并发节点(如 p6_1_page_worker)在 llm_reasoning / llm_output 的 payload 里注入
+ stream_source_id,前端据其把内容路由到 subagent 行。解析分支重建 payload 时
+ 必须原样透传,否则并发思考/正文无法分桶、逐字交错。无 source_id 返回空 dict,
+ 行为与不透传完全一致。
+ """
+ if isinstance(src_payload, dict):
+ source_id = src_payload.get("stream_source_id")
+ if source_id:
+ return {"stream_source_id": source_id}
+ return {}
+
+
def parse_stream_chunk(chunk: Any, *, _has_streamed_content: bool = False) -> dict[str, Any] | None:
"""Parse agent output chunk to frontend-consumable payload dict.
@@ -246,7 +261,11 @@ def _parse_typed_chunk(chunk: Any, _has_streamed_content: bool) -> dict[str, Any
)
if not content or not content.strip():
return None
- return {"event_type": "chat.delta", "content": content}
+ return {
+ "event_type": "chat.delta",
+ "content": content,
+ **_propagate_stream_source_id(payload),
+ }
if chunk_type == "llm_reasoning":
content = (
@@ -256,7 +275,11 @@ def _parse_typed_chunk(chunk: Any, _has_streamed_content: bool) -> dict[str, Any
)
if not content or not content.strip():
return None
- return {"event_type": "chat.reasoning", "content": content}
+ return {
+ "event_type": "chat.reasoning",
+ "content": content,
+ **_propagate_stream_source_id(payload),
+ }
if chunk_type == "content_chunk":
content = (
@@ -266,7 +289,11 @@ def _parse_typed_chunk(chunk: Any, _has_streamed_content: bool) -> dict[str, Any
)
if not content or not content.strip():
return None
- return {"event_type": "chat.delta", "content": content}
+ return {
+ "event_type": "chat.delta",
+ "content": content,
+ **_propagate_stream_source_id(payload),
+ }
if chunk_type == "answer":
if isinstance(payload, dict):