diff --git a/channel/wechatmp/passive_reply.py b/channel/wechatmp/passive_reply.py index d03efc4d..6206e0e7 100644 --- a/channel/wechatmp/passive_reply.py +++ b/channel/wechatmp/passive_reply.py @@ -131,8 +131,22 @@ class Query: # Only one request can access to the cached data try: - (reply_type, reply_content) = channel.cache_dict[from_user].pop(0) - if not channel.cache_dict[from_user]: # If popping the message makes the list empty, delete the user entry from cache + # WeChat passive reply allows only a single reply per request. + # To avoid forcing the user to send an extra message for every + # segment of multi-turn agent output, drain all consecutive + # cached text segments at once and merge them into one reply. + # Media (voice/image) can only be returned one at a time, so it + # stops the merge and is returned on its own. + cached = channel.cache_dict[from_user] + if cached[0][0] == "text": + reply_type = "text" + merged_parts = [] + while cached and cached[0][0] == "text": + merged_parts.append(cached.pop(0)[1]) + reply_content = "\n\n".join(merged_parts) + else: + (reply_type, reply_content) = cached.pop(0) + if not channel.cache_dict[from_user]: # If draining empties the list, delete the user entry from cache del channel.cache_dict[from_user] except IndexError: return "success"