Skip to content

The agent that grows with you

refactor(gateway): stream_consumer transport/fallback mixins — merge seed-frame helpers, fold multi-line calls and conditions

Tekniumd6268539d4c1
diff --git a/gateway/stream_consumer_fallback.py b/gateway/stream_consumer_fallback.py
index a6068a8475c..e0a2e4d4e9f 100644
--- a/gateway/stream_consumer_fallback.py
+++ b/gateway/stream_consumer_fallback.py
@@ -17,22 +17,15 @@ logger = logging.getLogger("gateway.stream_consumer")
class StreamFallbackMixin:
"""Non-streaming delivery paths used once progressive edits fail or the turn ends oddly."""
- async def _send_new_chunk(
- self,
- text: str,
- reply_to_id: Optional[str],
- *,
- final: bool = False,
- ) -> Optional[str]:
+ async def _send_new_chunk(self, text: str, reply_to_id: Optional[str], *,
+ final: bool = False) -> Optional[str]:
"""Send a new chunk threaded to ``reply_to_id``; returns the new message_id."""
text = self._clean_for_display(text)
if not text.strip():
return reply_to_id
try:
result = await self.adapter.send(
- chat_id=self.chat_id,
- content=text,
- reply_to=reply_to_id,
+ chat_id=self.chat_id, content=text, reply_to=reply_to_id,
metadata=self._metadata_for_send(final=final, expect_edits=not final),
)
if not (result.success and result.message_id):
@@ -63,19 +56,15 @@ class StreamFallbackMixin:
return final_text
@staticmethod
- def _split_text_chunks(
- text: str, limit: int, len_fn: "Callable[[str], int]" = len,
- ) -> list[str]:
+ def _split_text_chunks(text: str, limit: int, len_fn: "Callable[[str], int]" = len,
+ ) -> list[str]:
"""Split text for fallback sends: newline-preferred, fence-balanced across chunks."""
from gateway.platforms.helpers import split_text_fence_aware
+ return split_text_fence_aware(text, limit, len_fn, prefer_paragraphs=False,
+ balance_fences=True)
- return split_text_fence_aware(
- text, limit, len_fn, prefer_paragraphs=False, balance_fences=True,
- )
-
- def _truncate_for_stream(
- self, text: str, limit: int, len_fn: "Callable[[str], int]",
- ) -> list[str]:
+ def _truncate_for_stream(self, text: str, limit: int, len_fn: "Callable[[str], int]",
+ ) -> list[str]:
"""Split via the adapter's canonical truncate_message (platform-specific rules);
non-base test doubles / legacy adapters keep the two-argument call shape."""
truncate = getattr(self.adapter, "truncate_message", None)
@@ -111,8 +100,7 @@ class StreamFallbackMixin:
sent_any_chunk = False
for chunk in chunks:
result = await self._send_with_flood_retry(
- content=chunk, retry_log="Flood control on fallback send, retrying in %.1fs",
- )
+ content=chunk, retry_log="Flood control on fallback send, retrying in %.1fs")
if not result or not result.success:
# Partial continuation landed: do NOT set _final_response_sent (the
# gateway must still deliver the full answer); _already_sent only
@@ -131,15 +119,10 @@ class StreamFallbackMixin:
# Best-effort delete of the frozen partial — ONLY when the FULL final was
# re-sent. If only the missing tail went out, the partial IS the head of
# the answer ("sent only the second half" symptom).
- if (
- stale_message_id
- and stale_message_id != last_message_id
- and not self._fallback_preserve_partial_messages
- and continuation == final_text
- ):
- await self._delete_previews(
- [stale_message_id], label="Fallback partial", skip_sentinel=False,
- )
+ if (stale_message_id and stale_message_id != last_message_id
+ and not self._fallback_preserve_partial_messages and continuation == final_text):
+ await self._delete_previews([stale_message_id], label="Fallback partial",
+ skip_sentinel=False)
self._message_id = last_message_id
self._already_sent = True
@@ -156,11 +139,8 @@ class StreamFallbackMixin:
visible = self._visible_prefix()
# Telegram clients can lose (part of) a streamed preview after a failed
# final edit, so opt-in adapters commit a fresh final send.
- if (
- final_text.strip()
- and final_text == visible
- and getattr(self.adapter, "RESEND_FINAL_ON_EMPTY_STREAM_FALLBACK", False) is True
- ):
+ if (final_text.strip() and final_text == visible
+ and getattr(self.adapter, "RESEND_FINAL_ON_EMPTY_STREAM_FALLBACK", False) is True):
delivery = await self._send_empty_fallback_final(final_text)
if delivery == "delivered":
return None
@@ -183,12 +163,8 @@ class StreamFallbackMixin:
if final_text.strip() and final_text != visible:
return final_text
# Best-effort strip of a cursor left stuck by the edit failure.
- if (
- self._message_id
- and self._last_sent_text
- and self.cfg.cursor
- and self._last_sent_text.endswith(self.cfg.cursor)
- ):
+ if (self._message_id and self._last_sent_text and self.cfg.cursor
+ and self._last_sent_text.endswith(self.cfg.cursor)):
clean_text = self._last_sent_text[:-len(self.cfg.cursor)]
with contextlib.suppress(Exception):
result = await self._edit_message(message_id=self._message_id, content=clean_text)
@@ -215,9 +191,8 @@ class StreamFallbackMixin:
async def _send_with_flood_retry(self, *, content: str, retry_log: str, reply_to=None):
"""adapter.send(final metadata) with ONE bounded flood retry; returns the last
SendResult. Exceptions propagate (callers decide whether a raise is "ambiguous")."""
- kwargs = dict(
- chat_id=self.chat_id, content=content, metadata=self._metadata_for_send(final=True),
- )
+ kwargs = dict(chat_id=self.chat_id, content=content,
+ metadata=self._metadata_for_send(final=True))
if reply_to is not None:
kwargs["reply_to"] = reply_to
result = None
@@ -240,8 +215,7 @@ class StreamFallbackMixin:
stale_ids = self._stale_preview_ids(segment_only=True)
try:
result = await self._send_with_flood_retry(
- content=final_text,
- reply_to=self._initial_reply_to_id,
+ content=final_text, reply_to=self._initial_reply_to_id,
retry_log="Flood control on empty fallback final send; retrying in %.1fs",
)
except Exception as exc:
@@ -255,9 +229,8 @@ class StreamFallbackMixin:
new_message_id = getattr(result, "message_id", None)
# Telegram reports delete failure by returning False; the flood window that
# broke the finalize can reject this too — one bounded retry.
- await self._delete_previews(
- stale_ids, skip=new_message_id, label="Empty fallback", retry_on_false=True,
- )
+ await self._delete_previews(stale_ids, skip=new_message_id, label="Empty fallback",
+ retry_on_false=True)
self._segment_preview_message_ids = set()
self._message_id = new_message_id or "__no_edit__"
self._already_sent = True
@@ -290,10 +263,8 @@ class StreamFallbackMixin:
except (TypeError, ValueError):
delay = 3.0
if delay > self._max_fallback_flood_retry_seconds:
- logger.debug(
- "Flood control requests %.1fs; leaving final delivery to the gateway",
- delay,
- )
+ logger.debug("Flood control requests %.1fs; leaving final delivery to the gateway",
+ delay)
return None
return max(0.0, delay)
@@ -351,10 +322,8 @@ class StreamFallbackMixin:
_platform_name = str(_plat or getattr(self.adapter, "name", "")).lower()
_needs_reply_anchor = _platform_name in ("buzz", "slack", "mattermost", "feishu")
result = await self.adapter.send(
- chat_id=self.chat_id,
- content=text,
- reply_to=self._initial_reply_to_id if _needs_reply_anchor else None,
- metadata=_md,
+ chat_id=self.chat_id, content=text,
+ reply_to=self._initial_reply_to_id if _needs_reply_anchor else None, metadata=_md,
)
# Do NOT set _already_sent: commentary is interim, and the flag would
# suppress the real final after multiple tool calls.
diff --git a/gateway/stream_consumer_transport.py b/gateway/stream_consumer_transport.py
index 3727c11aabe..70121c75ed2 100644
--- a/gateway/stream_consumer_transport.py
+++ b/gateway/stream_consumer_transport.py
@@ -25,12 +25,8 @@ class StreamTransportMixin:
async def _edit_message(self, *, message_id: str, content: str, finalize: bool = False):
"""Edit via the adapter, passing routing metadata when supported."""
# Contract: adapters must accept finalize= even when False (test-guarded).
- kwargs = {
- "chat_id": self.chat_id,
- "message_id": message_id,
- "content": content,
- "finalize": finalize,
- }
+ kwargs = dict(chat_id=self.chat_id, message_id=message_id, content=content,
+ finalize=finalize)
if self.metadata:
try:
params = inspect.signature(self.adapter.edit_message).parameters
@@ -42,16 +38,14 @@ class StreamTransportMixin:
pass
return await self.adapter.edit_message(**kwargs)
- async def _send_seed_frame(self):
- """Open a native stream with an empty seed frame (typing indicator before any token)."""
- return await self.adapter.send_stream_frame(
+ async def _try_seed_frame(self, fail_log: str, *, exc_info: bool = False) -> bool:
+ """Open a native stream with an empty seed frame (typing indicator before any token) as a
+ bool; a raise logs ``fail_log`` at DEBUG (error formatted in, or the traceback when
+ ``exc_info``) and reads as False."""
+ seed = self.adapter.send_stream_frame(
"", chat_id=self.chat_id, reply_to=self._initial_reply_to_id, turn_id=self._turn_id,
)
-
- async def _try_seed_frame(self, fail_log: str, *, exc_info: bool = False) -> bool:
- """_send_seed_frame() as a bool; a raise logs ``fail_log`` at DEBUG (with the error
- formatted in, or the traceback when ``exc_info``) and reads as False."""
- return await self._try_frame(self._send_seed_frame(), fail_log, exc_info=exc_info)
+ return await self._try_frame(seed, fail_log, exc_info=exc_info)
@staticmethod
async def _try_frame(coro, fail_log: str, *, exc_info: bool = False) -> bool:
@@ -68,10 +62,7 @@ class StreamTransportMixin:
async def _send_frame(self, text: str, *, finalize: bool):
"""One native-stream frame; every frame carries the same chat/reply/turn routing."""
return await self.adapter.send_stream_frame(
- text,
- finalize=finalize,
- chat_id=self.chat_id,
- reply_to=self._initial_reply_to_id,
+ text, finalize=finalize, chat_id=self.chat_id, reply_to=self._initial_reply_to_id,
turn_id=self._turn_id,
)
@@ -103,17 +94,14 @@ class StreamTransportMixin:
def _stale_preview_ids(self, *, segment_only: bool = False) -> set:
"""Preview ids a fresh final replaces; ``segment_only`` spares finalized preambles."""
- stale_ids = set(
- self._segment_preview_message_ids if segment_only else self._preview_message_ids
- )
+ stale_ids = set(self._segment_preview_message_ids if segment_only
+ else self._preview_message_ids)
if self._message_id and self._message_id != "__no_edit__":
stale_ids.add(str(self._message_id) if segment_only else self._message_id)
return stale_ids
- async def _delete_previews(
- self, stale_ids, *, skip=None, label: str, retry_on_false: bool = False,
- skip_sentinel: bool = True,
- ) -> None:
+ async def _delete_previews(self, stale_ids, *, skip=None, label: str,
+ retry_on_false: bool = False, skip_sentinel: bool = True) -> None:
"""Best-effort delete of stale previews; never the message just sent (``skip``)."""
delete_fn = getattr(self.adapter, "delete_message", None)
if delete_fn is None:
@@ -133,39 +121,31 @@ class StreamTransportMixin:
"""cfg.transport "draft"/"auto" → the adapter's supports_draft_streaming probe
("draft" logs the downgrade); "edit"/"off" → False."""
transport = (self.cfg.transport or "edit").lower()
- if transport in ("edit", "off"):
- return False
# MagicMock test adapters default to edit.
- if not isinstance(self.adapter, _BasePlatformAdapter):
+ if transport in ("edit", "off") or not isinstance(self.adapter, _BasePlatformAdapter):
return False
probe_kwargs = dict(chat_type=self.cfg.chat_type or None, metadata=self.metadata)
try:
try:
# Per-chat probe (relay adapters resolve through the CHAT's
# descriptor); older adapters without the kwarg keep the legacy probe.
- supported = self.adapter.supports_draft_streaming(
- chat_id=self.chat_id, **probe_kwargs,
- )
+ supported = self.adapter.supports_draft_streaming(chat_id=self.chat_id,
+ **probe_kwargs)
except TypeError:
supported = self.adapter.supports_draft_streaming(**probe_kwargs)
except Exception:
logger.debug("supports_draft_streaming probe raised", exc_info=True)
supported = False
if not supported and transport == "draft":
- logger.debug(
- "Draft streaming requested but unsupported (chat=%s, type=%r) — "
- "falling back to edit",
- self.chat_id, self.cfg.chat_type,
- )
+ logger.debug("Draft streaming requested but unsupported (chat=%s, type=%r) — "
+ "falling back to edit", self.chat_id, self.cfg.chat_type)
return bool(supported)
def _resolve_native_streaming(self) -> bool:
"""Native streaming (send_stream_frame for ALL frames): a BasePlatformAdapter with
class-level SUPPORTS_NATIVE_STREAMING and a truthy supports_native_streaming probe."""
- if not (
- isinstance(self.adapter, _BasePlatformAdapter)
- and getattr(type(self.adapter), "SUPPORTS_NATIVE_STREAMING", False)
- ):
+ if not (isinstance(self.adapter, _BasePlatformAdapter)
+ and getattr(type(self.adapter), "SUPPORTS_NATIVE_STREAMING", False)):
return False
probe = getattr(self.adapter, "supports_native_streaming", None)
if probe is None:
@@ -185,9 +165,7 @@ class StreamTransportMixin:
return False
try:
result = await self.adapter.send_draft(
- chat_id=self.chat_id,
- draft_id=self._draft_id,
- content=text,
+ chat_id=self.chat_id, draft_id=self._draft_id, content=text,
metadata=self._draft_metadata(),
)
except Exception as e:
@@ -196,10 +174,8 @@ class StreamTransportMixin:
if getattr(result, "success", False):
self._last_sent_text = text # parity with the edit-based no-op skip
return True
- logger.debug(
- "send_draft returned success=False, disabling draft transport: %s",
- getattr(result, "error", "unknown"),
- )
+ logger.debug("send_draft returned success=False, disabling draft transport: %s",
+ getattr(result, "error", "unknown"))
self._draft_failures += 1
self._use_draft_streaming = False
return False
@@ -214,8 +190,7 @@ class StreamTransportMixin:
return
try:
await self.adapter.abandon_open_draft(
- self.chat_id,
- self._last_sent_text or self._clean_for_display(self._accumulated),
+ self.chat_id, self._last_sent_text or self._clean_for_display(self._accumulated),
metadata=self._draft_metadata(),
)
except Exception as e:
@@ -243,11 +218,8 @@ class StreamTransportMixin:
"""Record the primary id plus any continuation ids from an oversized split."""
raw = getattr(result, "raw_response", None) or {}
raw_ids = raw.get("message_ids") if isinstance(raw, dict) else None
- for mid in (
- getattr(result, "message_id", None),
- *(getattr(result, "continuation_message_ids", None) or ()),
- *(raw_ids or ()),
- ):
+ for mid in (getattr(result, "message_id", None),
+ *(getattr(result, "continuation_message_ids", None) or ()), *(raw_ids or ())):
self._track_preview_id(mid)
def _adapter_prefers_fresh_final(self, text: str) -> bool:
@@ -328,22 +300,15 @@ class StreamTransportMixin:
if not visible_stripped:
# Native streams MUST still get a finalize frame (placeholder) to close
# the thinking bubble, e.g. for a MEDIA-only response.
- if (
- finalize and self._use_native_streaming and self._native_stream_opened
- and await self._try_frame(
- self._send_frame("✅", finalize=True), "Finalize empty stream failed: %s",
- )
- ):
+ if (finalize and self._use_native_streaming and self._native_stream_opened
+ and await self._try_frame(self._send_frame("✅", finalize=True),
+ "Finalize empty stream failed: %s")):
self._mark_final_delivered()
return True # cursor-only / whitespace-only update
# Don't open a new message for 1-2 tokens + cursor (rapid tool-calling): if
# the cursor-strip edit is then rate-limited, "X ▉" stays forever.
- if (
- self._message_id is None
- and self.cfg.cursor
- and self.cfg.cursor in text
- and len(visible_stripped) < self._MIN_NEW_MSG_CHARS
- ):
+ if (self._message_id is None and self.cfg.cursor and self.cfg.cursor in text
+ and len(visible_stripped) < self._MIN_NEW_MSG_CHARS):
return True # too short for a standalone message — accumulate more
# A failed native/draft transport disables itself and falls through so the
@@ -353,9 +318,8 @@ class StreamTransportMixin:
if ok is not None:
return ok
if self._use_draft_streaming and self._message_id is None:
- ok = await self._draft_push(
- text, pre_fence_text, finalize=finalize, is_turn_final=is_turn_final,
- )
+ ok = await self._draft_push(text, pre_fence_text, finalize=finalize,
+ is_turn_final=is_turn_final)
if ok is not None:
return ok
self._last_edit_overflowed = False
@@ -369,9 +333,8 @@ class StreamTransportMixin:
logger.error("Stream send/edit error: %s", e)
return False
- async def _native_push(
- self, text: str, *, finalize: bool, is_turn_final: bool,
- ) -> Optional[bool]:
+ async def _native_push(self, text: str, *, finalize: bool, is_turn_final: bool,
+ ) -> Optional[bool]:
"""Native streaming: every frame goes through send_stream_frame(); lazy re-seed after
a boundary. None when native was disabled (seed/frame failure) → caller falls through."""
if not self._native_stream_opened and text:
@@ -381,11 +344,8 @@ class StreamTransportMixin:
self._native_stream_opened = True
self._awaiting_reopen_after_boundary = False
# Paired with the boundary-finalize INFO: typing-reappear latency.
- logger.info(
- "[latency] Re-opened native stream after boundary "
- "(turn=%s, waited for first delta)",
- self._turn_id,
- )
+ logger.info("[latency] Re-opened native stream after boundary "
+ "(turn=%s, waited for first delta)", self._turn_id)
# WeCom renders each finalize as a separate bubble: only the turn-final and
# boundaries close the stream, not segment breaks.
@@ -399,10 +359,8 @@ class StreamTransportMixin:
# stream-final-ack-timeout-duplicate.md). A definitive failure rolls it back.
if finalize:
self._mark_final_delivered(record=text) # recorded: stale frame can't suppress
- if await self._try_frame(
- self._send_frame(text, finalize=finalize),
- "send_stream_frame raised, disabling native streaming: %s",
- ):
+ if await self._try_frame(self._send_frame(text, finalize=finalize),
+ "send_stream_frame raised, disabling native streaming: %s"):
self._already_sent = True
self._last_sent_text = text
self._native_last_pushed_len = len(text)
@@ -430,9 +388,8 @@ class StreamTransportMixin:
logger.debug("Native fallback: failed to finalize stream: %s", e)
return None
- async def _draft_push(
- self, text: str, pre_fence_text: str, *, finalize: bool, is_turn_final: bool,
- ) -> Optional[bool]:
+ async def _draft_push(self, text: str, pre_fence_text: str, *, finalize: bool,
+ is_turn_final: bool) -> Optional[bool]:
"""Draft frame while no message_id exists; None = not applicable / drafts just failed.
Skipped when finalizing (the real send clears the draft), EXCEPT stream-is-the-message
adapters keep ONE stream per turn: a segment-break finalize must not become a real
@@ -455,9 +412,7 @@ class StreamTransportMixin:
async def _first_send(self, text: str, *, finalize: bool) -> bool:
"""First send, threaded to the user's message (correct topic/thread)."""
result = await self.adapter.send(
- chat_id=self.chat_id,
- content=text,
- reply_to=self._initial_reply_to_id,
+ chat_id=self.chat_id, content=text, reply_to=self._initial_reply_to_id,
metadata=self._metadata_for_send(final=finalize, expect_edits=not finalize),
)
if not result.success:
@@ -495,22 +450,18 @@ class StreamTransportMixin:
prefers_fresh or (not has_prefers_hook and self._should_send_fresh_final())
) and await self._try_fresh_final(text, is_turn_final=is_turn_final):
return True
- result = await self._edit_message(
- message_id=self._message_id, content=text, finalize=finalize,
- )
+ result = await self._edit_message(message_id=self._message_id, content=text,
+ finalize=finalize)
if not result.success:
- return await self._on_edit_failure(
- result, text, finalize=finalize, is_turn_final=is_turn_final,
- )
+ return await self._on_edit_failure(result, text, finalize=finalize,
+ is_turn_final=is_turn_final)
self._already_sent = True
self._track_preview_ids_from_result(result)
# Oversized edit split across continuations: message_id is now the LAST
# continuation, which holds only the final chunk — retarget edits and reset
# skip-if-same. getattr keeps SimpleNamespace test mocks working.
- if (
- (getattr(result, "continuation_message_ids", ()) or ())
- and result.message_id and result.message_id != self._message_id
- ):
+ if ((getattr(result, "continuation_message_ids", ()) or ())
+ and result.message_id and result.message_id != self._message_id):
self._last_edit_overflowed = True
self._turn_split_delivery = True
self._adopt_message_id(str(result.message_id))
@@ -528,18 +479,13 @@ class StreamTransportMixin:
self._edit_supported = False
self._already_sent = True
- async def _on_edit_failure(
- self, result, text: str, *, finalize: bool, is_turn_final: bool,
- ) -> bool:
+ async def _on_edit_failure(self, result, text: str, *, finalize: bool, is_turn_final: bool,
+ ) -> bool:
"""Classify a failed edit: partial overflow, flood backoff, or fallback mode. Always
False; the caller's finalize path may still deliver the tail."""
turn_final = finalize and is_turn_final
- if (
- turn_final
- and self.cfg.cursor
- and self._last_sent_text.endswith(self.cfg.cursor)
- and self._visible_prefix() == text
- ):
+ if (turn_final and self.cfg.cursor and self._last_sent_text.endswith(self.cfg.cursor)
+ and self._visible_prefix() == text):
# Cosmetic final edit was rate-limited but the full answer is already on
# screen (cursor stuck): mark delivered so the gateway doesn't send it
# twice, and record the on-screen payload.
@@ -549,9 +495,8 @@ class StreamTransportMixin:
if isinstance(raw_response, dict) and raw_response.get("partial_overflow"):
# Some overflow chunks landed but not the whole response: preserve the
# visible prefix so got_done sends the missing tail.
- self._message_id = str(
- raw_response.get("last_message_id") or result.message_id or self._message_id
- )
+ self._message_id = str(raw_response.get("last_message_id") or result.message_id
+ or self._message_id)
delivered_prefix = raw_response.get("delivered_prefix")
if isinstance(delivered_prefix, str) and delivered_prefix:
self._last_sent_text = delivered_prefix
@@ -570,10 +515,8 @@ class StreamTransportMixin:
if self._is_flood_error(result):
self._flood_strikes += 1
self._current_edit_interval = min(self._current_edit_interval * 2, 10.0)
- logger.debug(
- "Flood control on edit (strike %d/%d), backoff interval → %.1fs",
- self._flood_strikes, self._MAX_FLOOD_STRIKES, self._current_edit_interval,
- )
+ logger.debug("Flood control on edit (strike %d/%d), backoff interval → %.1fs",
+ self._flood_strikes, self._MAX_FLOOD_STRIKES, self._current_edit_interval)
immediate_final_fallback = (
turn_final and getattr(self.adapter, "FALLBACK_ON_FINAL_EDIT_FLOOD", False) is True
)