fix(wechat_kf): stop dropping rapid-fire messages in batch dedup

_dedup_image_text_pair previously fell back to returning only the last message whenever the batch was not exactly an image+text pair, which silently dropped multiple texts/images sent in quick succession.

Cursor freshness is already guaranteed by sync_msg, so no extra stale-history protection is needed. Now we return all messages by default and only collapse a batch when it is exactly a 2-message image+text pair within a 5s window (order-insensitive, normalized to [image, text]).
This commit is contained in:
6vision
2026-05-28 14:23:04 +08:00
parent 5c163cc0fe
commit 877b848370

View File

@@ -479,22 +479,24 @@ class WechatKfChannel(ChatChannel):
def _dedup_image_text_pair(messages: list) -> list: def _dedup_image_text_pair(messages: list) -> list:
""" """
A WeChat user often sends an image immediately followed by a text A WeChat user often sends an image immediately followed by a text
question (e.g. "what's in this picture?"). When that happens we question (e.g. "what's in this picture?"). Only when the batch is
treat them as one combined turn: image first, then text. exactly that 2-message image+text pair within a 5s window do we
Otherwise (single message or unrelated batch) we return only the collapse it into a single [image, text] turn. Otherwise return
last message to avoid replying to stale history. every message so rapid-fire texts/images are all processed —
cursor freshness is already guaranteed by sync_msg.
""" """
if not messages: if not messages:
return [] return []
text_messages = [m for m in messages if m["msgtype"] == "text"] if len(messages) == 2:
image_messages = [m for m in messages if m["msgtype"] == "image"] a, b = messages
if text_messages and image_messages: types = {a["msgtype"], b["msgtype"]}
last_text_time = text_messages[-1]["send_time"] if types == {"image", "text"} and abs(a["send_time"] - b["send_time"]) <= 5:
first_image_time = image_messages[0]["send_time"] img = a if a["msgtype"] == "image" else b
if abs(last_text_time - first_image_time) <= 5: txt = b if a["msgtype"] == "image" else a
return [image_messages[0], text_messages[-1]] return [img, txt]
return [messages[-1]]
return messages
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------