fix(wecom): proactive fallback must not spam one message per delta

The 59c44e9 fallback replied every coalesced delta (~0.8s) as a separate
proactive message once the callback req_id went stale - a wall of
repeating snapshots. Now: intermediate deltas are dropped after a single
'long reply, result will follow' notice; only the final text goes out.
This commit is contained in:
openbot committed 2026-10-24 10:12:19 +08:00
1 parent 59c44e94ca
commit 353db7ba10
2 files changed
+25 -10

No files matched your search

+16 -7
View File
@@ -501,13 +501,22 @@ class WecomAibotChannel:
with self._stream_lock:
self._req_ids.pop(chat_id, None)
return
# no live callback req_id: fall back to a proactive message
self.reply(chat_id, text) if text else None
if finished:
st["sent"] = True
with self._stream_lock:
self._req_ids.pop(chat_id, None)
self._logger.info(f"[wecom] stream finished via proactive message (callback req_id stale or absent) chat={chat_id}")
# No live callback req_id (stale or absent): proactive aibot_send_msg
# territory. Intermediate deltas are DROPPED — replying each would spam
# one full-snapshot message every 0.8s. Instead: a single notice the
# first time we notice staleness, then silence until the final text,
# which always goes out (possibly in overflow chunks).
if not finished:
if not st.get("proactive_noticed"):
st["proactive_noticed"] = True
self.reply(chat_id, "⏳ 回复时间较长,处理完成后将直接发送结果…")
return
st["sent"] = True
with self._stream_lock:
self._req_ids.pop(chat_id, None)
if text:
self.reply(chat_id, text)
self._logger.info(f"[wecom] stream finished via proactive message (callback req_id stale or absent) chat={chat_id}")
def _send(self, obj: dict) -> None:
raw = json.dumps(obj, ensure_ascii=False)
+9 -3
View File
@@ -178,8 +178,14 @@ def test_wecom_stream_falls_back_when_callback_req_stale():
ch, ws = _make_wecom()
ch._ws = ws
ch._req_ids["c1"] = ("req-old", __import__("time").time() - 600) # beyond TTL
ch.push_event("c1", "final", {"text": "late answer"})
respond = [m for m in ws.sent if m["cmd"] == "aibot_respond_msg"]
# intermediate deltas: only ONE proactive notice, no full-snapshot spam
for _ in range(5):
ch.push_event("c1", "delta", {"text": "more words "})
send = [m for m in ws.sent if m["cmd"] == "aibot_send_msg"]
assert len(send) == 1 and "处理完成后" in json.dumps(send[0], ensure_ascii=False)
respond = [m for m in ws.sent if m["cmd"] == "aibot_respond_msg"]
assert respond == [] # stale req_id must NOT be reused
assert send and "late answer" in json.dumps(send[-1])
# final: exactly one proactive message with the full text
ch.push_event("c1", "final", {"text": "late answer"})
send = [m for m in ws.sent if m["cmd"] == "aibot_send_msg"]
assert len(send) == 2 and "late answer" in json.dumps(send[-1], ensure_ascii=False)