fix: send file attachments before final stream reply in WeCom WebSocket channel#2729
Open
highland0971 wants to merge 1 commit intobytedance:mainfrom
Open
fix: send file attachments before final stream reply in WeCom WebSocket channel#2729highland0971 wants to merge 1 commit intobytedance:mainfrom
highland0971 wants to merge 1 commit intobytedance:mainfrom
Conversation
…et channel # PR 3: Fix WeCom File Attachment Timing — Send Before Final Stream Reply ## Problem On the WeCom (WeChat Work / 企业微信) channel, file attachments sent using `send_file` after a final stream reply (`reply_stream(is_final=True)`) are **silently ignored** by WeCom. The WeCom WebSocket protocol closes the stream context after the final reply, so any subsequent `send_file` calls fail without clear error feedback. This means users receive the AI's text response but **never receive the associated file artifacts** generated by the agent. ## Root Cause The `WeComChannel.on_send()` method was structured as: 1. Call `self.send(msg)` — which sends the text reply (including `is_final=True`) 2. Iterate over `msg.attachments` and call `self.send_file()` 3. Clear WebSocket context When step 1 sends `is_final=True`, WeCom WebSocket closes the message context, and step 2's file uploads are ignored. Additionally, the `send_file` method itself also had an early return for `is_final=False` messages, which blocked non-final attachments from being sent. ## Fix ### wecom.py — Reorder send sequence - **File attachments are now sent BEFORE the final stream reply** when `msg.is_final=True`. - For non-final messages, attachments continue to be sent after the reply (since the stream context remains open). - The WebSocket context clearing (`_clear_ws_context`) remains at the end. New send order for final messages: 1. Send file attachments (`send_file`) 2. Send text reply (`send` / `reply_stream(is_final=True)`) 3. Clear WebSocket context ## Testing - Verified in production: file artifacts now arrive alongside the AI's final response on WeCom. - No regression on non-WeCom channels (the fix is WeCom-specific). ## Files Changed - `backend/app/channels/wecom.py`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR 3: Fix WeCom File Attachment Timing — Send Before Final Stream Reply
Problem
On the WeCom (WeChat Work / 企业微信) channel, file attachments sent using
send_fileafter a final stream reply (reply_stream(is_final=True)) aresilently ignored by WeCom. The WeCom WebSocket protocol closes the stream
context after the final reply, so any subsequent
send_filecalls fail withoutclear error feedback.
This means users receive the AI's text response but never receive the
associated file artifacts generated by the agent.
Root Cause
The
WeComChannel.on_send()method was structured as:self.send(msg)— which sends the text reply (includingis_final=True)msg.attachmentsand callself.send_file()When step 1 sends
is_final=True, WeCom WebSocket closes the message context,and step 2's file uploads are ignored.
Additionally, the
send_filemethod itself also had an early return foris_final=Falsemessages, which blocked non-final attachments from being sent.Fix
wecom.py — Reorder send sequence
msg.is_final=True.(since the stream context remains open).
_clear_ws_context) remains at the end.New send order for final messages:
send_file)send/reply_stream(is_final=True))Testing
final response on WeCom.
Files Changed
backend/app/channels/wecom.py