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:
1 parent
59c44e94ca
commit
353db7ba10
2 files changed
+21
-6
No files matched your search
@@ -501,12 +501,21 @@ class WecomAibotChannel:
|
|||||||
with self._stream_lock:
|
with self._stream_lock:
|
||||||
self._req_ids.pop(chat_id, None)
|
self._req_ids.pop(chat_id, None)
|
||||||
return
|
return
|
||||||
# no live callback req_id: fall back to a proactive message
|
# No live callback req_id (stale or absent): proactive aibot_send_msg
|
||||||
self.reply(chat_id, text) if text else None
|
# territory. Intermediate deltas are DROPPED — replying each would spam
|
||||||
if finished:
|
# 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
|
st["sent"] = True
|
||||||
with self._stream_lock:
|
with self._stream_lock:
|
||||||
self._req_ids.pop(chat_id, None)
|
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}")
|
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:
|
def _send(self, obj: dict) -> None:
|
||||||
|
|||||||
@@ -178,8 +178,14 @@ def test_wecom_stream_falls_back_when_callback_req_stale():
|
|||||||
ch, ws = _make_wecom()
|
ch, ws = _make_wecom()
|
||||||
ch._ws = ws
|
ch._ws = ws
|
||||||
ch._req_ids["c1"] = ("req-old", __import__("time").time() - 600) # beyond TTL
|
ch._req_ids["c1"] = ("req-old", __import__("time").time() - 600) # beyond TTL
|
||||||
ch.push_event("c1", "final", {"text": "late answer"})
|
# intermediate deltas: only ONE proactive notice, no full-snapshot spam
|
||||||
respond = [m for m in ws.sent if m["cmd"] == "aibot_respond_msg"]
|
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"]
|
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 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)
|
||||||
Reference in new issue
Block a user