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: 修复企业微信流输出问题

xiaowenzihwlb6da8e380fbf
diff --git a/jiuwenclaw/agentserver/interface.py b/jiuwenclaw/agentserver/interface.py
index 39cf4f6c6c..1ca49e7b8f 100644
--- a/jiuwenclaw/agentserver/interface.py
+++ b/jiuwenclaw/agentserver/interface.py
@@ -1169,7 +1169,11 @@ class JiuWenClaw:
)
if not content:
return None
- return {"event_type": "chat.delta", "content": content}
+ return {
+ "event_type": "chat.delta",
+ "content": content,
+ "source_chunk_type": chunk_type,
+ }
if chunk_type == "answer":
if isinstance(payload, dict):
@@ -1198,8 +1202,16 @@ class JiuWenClaw:
# For chunked answers, return as delta (will be accumulated)
# For non-chunked, return as final
if is_chunked:
- return {"event_type": "chat.delta", "content": content}
- return {"event_type": "chat.final", "content": content}
+ return {
+ "event_type": "chat.delta",
+ "content": content,
+ "source_chunk_type": chunk_type,
+ }
+ return {
+ "event_type": "chat.final",
+ "content": content,
+ "source_chunk_type": chunk_type,
+ }
if chunk_type == "tool_call":
tool_info = (
@@ -1286,7 +1298,11 @@ class JiuWenClaw:
return None
else:
content = str(payload)
- return {"event_type": "chat.delta", "content": content}
+ return {
+ "event_type": "chat.delta",
+ "content": content,
+ "source_chunk_type": chunk_type,
+ }
# 普通 dict
if isinstance(chunk, dict):
@@ -1299,7 +1315,11 @@ class JiuWenClaw:
}
output = chunk.get("output", "")
if output:
- return {"event_type": "chat.delta", "content": str(output)}
+ return {
+ "event_type": "chat.delta",
+ "content": str(output),
+ "source_chunk_type": "dict_output",
+ }
return None
except Exception:
diff --git a/jiuwenclaw/channel/wecom_channel.py b/jiuwenclaw/channel/wecom_channel.py
index c4424036b2..b149c14aa0 100644
--- a/jiuwenclaw/channel/wecom_channel.py
+++ b/jiuwenclaw/channel/wecom_channel.py
@@ -261,7 +261,8 @@ class WecomChannel(BaseChannel):
@staticmethod
def _is_thinking_only_content(text: str) -> bool:
"""判断内容是否为空或仅为占位符(纯省略号等),不应视为有效回复。
- 思考过程(llm_reasoning/thinking)已在 interface 转为 chat.processing_status,不会以 content 到达。"""
+ 思考过程(thinking)会在 interface 转为 chat.processing_status;
+ llm_reasoning 若以 content 到达,则在 WecomChannel 内基于 source_chunk_type 再次过滤。"""
if not text or not isinstance(text, str):
return True
t = text.strip()
@@ -282,6 +283,15 @@ class WecomChannel(BaseChannel):
return None
return str(content)
+ @staticmethod
+ def _is_reasoning_chunk(msg: Message) -> bool:
+ """判断当前消息是否为不应展示给企业微信用户的 reasoning chunk。"""
+ payload = getattr(msg, "payload", None) or {}
+ if not isinstance(payload, dict):
+ return False
+ source_chunk_type = str(payload.get("source_chunk_type") or "").strip().lower()
+ return source_chunk_type == "llm_reasoning"
+
async def _send_stream_placeholder(self, req_id: str) -> None:
"""发送流式首帧占位。PHP SDK 用 <think></think> 显示加载动画,企业微信 Markdown 可能支持。"""
entry = self._pending_streams.get(req_id)
@@ -338,6 +348,9 @@ class WecomChannel(BaseChannel):
if req_id and self.config.enable_streaming:
entry = self._pending_streams.get(req_id)
if entry:
+ if self._is_reasoning_chunk(msg):
+ logger.debug("WecomChannel 跳过 reasoning chunk: req_id=%s", req_id)
+ return
content = self._extract_content_from_payload(msg)
if content is not None:
entry["accumulated"] = (entry.get("accumulated") or "") + content
@@ -395,6 +408,9 @@ class WecomChannel(BaseChannel):
if not chatid:
logger.warning("WecomChannel 无法确定回发目标 chatid")
return
+ if self._is_reasoning_chunk(msg):
+ logger.debug("WecomChannel 跳过 final reasoning chunk")
+ return
content = self._strip_think_tags(self._extract_content(msg)).strip()
if not content or self._is_thinking_only_content(content):
logger.debug("WecomChannel 消息内容为空或仅为思考占位,跳过发送")