fix(streaming): preserve recovered segments across channels

This commit is contained in:
Xubin Ren
2026-07-27 01:39:46 +08:00
parent e6baecafcd
commit b55b76d755
12 changed files with 208 additions and 0 deletions
+4
View File
@@ -1257,6 +1257,10 @@ class WeixinChannel(BaseChannel):
return
is_end = stream_end or bool(meta.get("_stream_end"))
buffer_key = stream_id or chat_id
if is_end and merge_next:
if delta:
self._stream_buffers.setdefault(buffer_key, []).append(delta)
return
# Accumulate intermediate deltas. The stream_end message's own content
# (present when the manager coalesces deltas into the end message) is
# folded into `full` below instead of appended here, so a send retry
@@ -1824,6 +1824,29 @@ async def test_stream_end_flushes_buffered_answer() -> None:
assert "wx-user" not in channel._stream_buffers
@pytest.mark.asyncio
async def test_stream_end_merge_next_preserves_buffer_until_final_end() -> None:
channel, _bus = _make_channel()
channel._client = object()
channel._token = "token"
channel._context_tokens["wx-user"] = "ctx-1"
channel._context_token_at["wx-user"] = time.time()
channel._send_text = AsyncMock()
await channel.send_delta(
"wx-user",
"first-",
stream_id="s1",
stream_end=True,
merge_next=True,
)
await channel.send_delta("wx-user", "second", stream_id="s1")
await channel.send_delta("wx-user", "", stream_id="s1", stream_end=True)
channel._send_text.assert_awaited_once_with("wx-user", "first-second", "ctx-1")
assert "s1" not in channel._stream_buffers
@pytest.mark.asyncio
async def test_stream_end_send_failure_keeps_buffer_for_retry() -> None:
channel, _bus = _make_channel()