fix(gateway): stream consumer first message drops thread context

Cherry-picked from PR #13077 commits:
- 5500c7d8 fix(gateway): stream consumer first message drops thread context
- e84403b9 test(gateway): add regression tests for stream consumer thread routing

Fixes: Streaming first message drops thread/topic context in Feishu group
topics, Slack threads, Telegram forum topics. Adds initial_reply_to_id
ctor arg to GatewayStreamConsumer, threaded through _send_or_edit and
_send_new_chunk. Also fixes Feishu _send_raw_message fallback path
(reply -> create) to use receive_id_type='thread_id' so the new message
lands in the correct topic instead of the main channel.

Authored by hrygo via PR #13077 (re-attributed from the bot-authored
salvage commit on the original branch).
This commit is contained in:
hrygo 2026-05-10 15:14:38 -07:00 committed by Teknium
parent 6636fecd47
commit ff14666cdc
4 changed files with 221 additions and 15 deletions

View file

@ -92,6 +92,7 @@ class GatewayStreamConsumer:
config: Optional[StreamConsumerConfig] = None,
metadata: Optional[dict] = None,
on_new_message: Optional[callable] = None,
initial_reply_to_id: Optional[str] = None,
):
self.adapter = adapter
self.chat_id = chat_id
@ -105,6 +106,7 @@ class GatewayStreamConsumer:
# the content, not edit the old bubble above it.
# Called with no arguments. Exceptions are swallowed.
self._on_new_message = on_new_message
self._initial_reply_to_id = initial_reply_to_id
self._queue: queue.Queue = queue.Queue()
self._accumulated = ""
self._message_id: Optional[str] = None
@ -1004,10 +1006,12 @@ class GatewayStreamConsumer:
# The final response will be sent by the fallback path.
return False
else:
# First message — send new
# First message — send new, threaded to the original user message
# so it lands in the correct topic/thread.
result = await self.adapter.send(
chat_id=self.chat_id,
content=text,
reply_to=self._initial_reply_to_id,
metadata=self.metadata,
)
if result.success: