Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
feat(tg): add support for streaming rich messages (#5614)
Arthur K.813aaaea05a9
diff --git a/nanobot/channels/telegram/runtime.py b/nanobot/channels/telegram/runtime.pyindex 5aafb5bf5..687bfc88f 100644--- a/nanobot/channels/telegram/runtime.py+++ b/nanobot/channels/telegram/runtime.py@@ -25,7 +25,7 @@ from telegram import (Update,User,)-from telegram.error import BadRequest, InvalidToken, NetworkError, TimedOut+from telegram.error import BadRequest, InvalidToken, NetworkError, RetryAfter, TimedOutfrom telegram.ext import Application, CallbackQueryHandler, ContextTypes, MessageHandler, filtersfrom telegram.request import BaseRequest, HTTPXRequest@@ -46,7 +46,12 @@ TELEGRAM_MAX_MESSAGE_LEN = 4000 # Telegram message character limit# raw markdown into chunks whose rendered HTML fits Telegram's true 4096-char# boundary so the final rendered message never overflows.TELEGRAM_HTML_MAX_LEN = 4096+# Bot API rich messages accept up to 32,768 UTF-8 characters. Raw Markdown is+# counted conservatively here; Python's str length counts Unicode code points,+# so multibyte text must not be split at an encoded-byte boundary.+TELEGRAM_RICH_MAX_LEN = 32768TELEGRAM_REPLY_CONTEXT_MAX_LEN = TELEGRAM_MAX_MESSAGE_LEN # Max length for reply context in user message+TELEGRAM_RICH_DRAFT_MIN_INTERVAL = 0.75 # 40 draft updates per 30 seconds per chat# python-telegram-bot exposes a six-parameter Application generic. Nanobot# doesn't customize its context/data/job-queue types, so keep that SDK boundary@@ -387,6 +392,7 @@ class _StreamBuf:"""Per-chat streaming accumulator for progressive message editing."""text: str = ""message_id: int | None = None+ draft_id: int | None = Nonelast_edit: float = 0.0stream_id: str | None = None@@ -829,14 +835,31 @@ class TelegramChannel(BaseChannel):@staticmethoddef _is_rich_capability_error(exc: Exception) -> bool:- """True when the error indicates sendRichMessage is unavailable."""+ """True when the Bot API does not support rich-message endpoints."""err = str(exc).lower()return ("method not found" in error "unknown method" in err+ or ("endpoint" in err and "not found" in err)or "bad request: invalid parameter" in err+ or "unsupported" in err+ or "not implemented" in err)+ def _rich_streaming_enabled(self) -> bool:+ return self.config.rich_messages and not self._rich_send_disabled++ @staticmethod+ def _rich_message_payload(content: str) -> dict[str, str]:+ return {"markdown": content}++ def _mark_rich_unavailable(self, exc: Exception, operation: str) -> bool:+ if not self._is_rich_capability_error(exc):+ return False+ self._rich_send_disabled = True+ self.logger.debug("{} unavailable, disabling rich messages: {}", operation, exc)+ return True+async def _try_send_rich(self,chat_id: int,@@ -851,9 +874,7 @@ class TelegramChannel(BaseChannel):payload: dict[str, Any] = {"chat_id": chat_id,- "rich_message": {- "markdown": content,- },+ "rich_message": self._rich_message_payload(content),}if reply_params is not None:# sendRichMessage uses reply_parameters (object), not reply_to_message_id.@@ -881,13 +902,12 @@ class TelegramChannel(BaseChannel):)return Trueexcept BadRequest as exc:- if self._is_rich_capability_error(exc):- self.logger.debug("sendRichMessage not available, disabling")- self._rich_send_disabled = True- else:+ if not self._mark_rich_unavailable(exc, "sendRichMessage"):self.logger.debug("sendRichMessage rejected: {}", exc)return Falseexcept Exception as exc:+ if self._mark_rich_unavailable(exc, "sendRichMessage"):+ return Falseerr_str = str(exc).lower()is_timeout = "timed out" in err_str or isinstance(exc, TimedOut)if is_timeout:@@ -896,67 +916,103 @@ class TelegramChannel(BaseChannel):self.logger.debug("sendRichMessage failed: {}", exc)return False- async def _try_edit_rich(self, chat_id: int, message_id: int, content: str) -> bool:- """Upgrade an existing message to rich in place via editMessageText (Bot API 10.1).-- Editing in place keeps the message identity, so the streaming preview is- upgraded without the delete-and-resend pattern that caused flickering and- dropped line breaks (issue #4470).-- Returns True when the rich edit is in place (including the ambiguous- "message is not modified" retry outcome after a response timeout).- Returns False only when the legacy HTML path should take over:- capability errors (server older than Bot API 10.1, which also trip the- rich latch) and content-shaped BadRequest rejections. Transport,- rate-limit, and unexpected errors propagate so the final-edit retry- contract is preserved — ChannelManager retries the buffered send- instead of an immediate legacy edit doubling connection demand.- """- if not self._app:- return False+ @staticmethod+ def _new_rich_draft_id() -> int:+ """Return a non-zero Bot API Integer suitable for one live draft."""+ return time.time_ns() % 2_147_483_647 or 1+ async def _try_send_rich_draft(+ self,+ chat_id: int,+ draft_id: int,+ content: str,+ thread_kwargs: dict[str, int],+ ) -> bool:+ """Create or update the ephemeral rich preview for a private chat."""+ app = self._require_app()payload: dict[str, Any] = {"chat_id": chat_id,- "message_id": message_id,- "rich_message": {- "markdown": content,- },+ "draft_id": draft_id,+ "rich_message": self._rich_message_payload(content),+ **thread_kwargs,}try:await self._call_with_retry(- self._app.bot.do_api_request,- "editMessageText",+ app.bot.do_api_request,+ "sendRichMessageDraft",api_kwargs=payload,)return Trueexcept BadRequest as exc:- if self._is_not_modified_error(exc):- # Ambiguous success: the rich edit was applied server-side but- # its response timed out, so the retry hit "message is not- # modified". Treat it as done rather than letting the legacy- # edit overwrite the already-successful rich result.- self.logger.debug("Rich stream edit already applied for {}", chat_id)- return True- # Before Bot API 10.1, editMessageText ignores rich_message and- # reports the absent text argument instead.- pre_rich_edit_server = (- bool(content)- and str(exc).strip().lower() == "message text is empty"- )- if self._is_rich_capability_error(exc) or pre_rich_edit_server:- self.logger.debug("editMessageText rich_message not available, disabling")- self._rich_send_disabled = True+ if not self._mark_rich_unavailable(exc, "sendRichMessageDraft"):+ self.logger.debug("Rich draft rejected: {}", exc)+ return False+ except Exception as exc:+ if self._mark_rich_unavailable(exc, "sendRichMessageDraft"):return False- # Content-shaped rejections (invalid markdown, unsupported media in- # the rich payload, …) fall back to the legacy HTML edit.- self.logger.debug("editMessageText rich_message rejected: {}", exc)+ raise++ async def _try_send_stream_rich(+ self,+ chat_id: int,+ content: str,+ thread_kwargs: dict[str, int],+ ) -> bool:+ """Persist one completed rich stream chunk.++ ``False`` means Telegram rejected rich formatting before delivery and the+ caller may use the legacy path. A timeout is treated as an ambiguous+ success: retrying this non-idempotent method could duplicate a message+ that Telegram already stored.+ """+ app = self._require_app()+ payload: dict[str, Any] = {+ "chat_id": chat_id,+ "rich_message": self._rich_message_payload(content),+ **thread_kwargs,+ }+ try:+ await app.bot.do_api_request(+ "sendRichMessage",+ api_kwargs=payload,+ )+ except BadRequest as exc:+ if not self._mark_rich_unavailable(exc, "sendRichMessage"):+ self.logger.debug("Stream rich send rejected: {}", exc)return False- except Exception:- # Transport, rate-limit, and unexpected errors propagate so the- # final-edit retry contract stays intact: ChannelManager retries- # the buffered send instead of this handler doubling connection- # demand with an immediate legacy edit.+ except TimedOut as exc:+ self.logger.warning(+ "Stream rich send timed out; delivery is ambiguous, not retrying: {}", exc+ )+ return True+ except Exception as exc:+ if self._mark_rich_unavailable(exc, "sendRichMessage"):+ return Falseraise+ return True++ async def _start_legacy_stream(+ self,+ chat_id: int,+ buf: "_StreamBuf",+ thread_kwargs: dict[str, int],+ ) -> None:+ """Replace a rejected rich draft with an editable plain-text tail."""+ app = self._require_app()+ chunks = _split_telegram_markdown_html_chunks(buf.text, TELEGRAM_HTML_MAX_LEN)+ for index, (markdown, _) in enumerate(chunks[:-1]):+ await self._send_text(chat_id, markdown, thread_kwargs=thread_kwargs)+ buf.text = "\n".join(markdown for markdown, _ in chunks[index + 1:])+ tail = chunks[-1][0]+ sent = await self._call_with_retry(+ app.bot.send_message,+ chat_id=chat_id,+ text=_strip_md_block(tail),+ **thread_kwargs,+ )+ buf.text = tail+ buf.message_id = sent.message_id+ buf.draft_id = Noneasync def send(self, msg: OutboundMessage) -> None:"""Send a message through Telegram."""@@ -1091,8 +1147,6 @@ class TelegramChannel(BaseChannel):**kwargs: Any,) -> _T:"""Call an async Telegram API function with retry on pool/network timeout and RetryAfter."""- from telegram.error import RetryAfter-for attempt in range(1, _SEND_MAX_RETRIES + 1):try:return await fn(*args, **kwargs)@@ -1171,7 +1225,7 @@ class TelegramChannel(BaseChannel):resuming: bool = False,merge_next: bool = False,) -> None:- """Progressive message editing: send on first delta, edit on subsequent ones."""+ """Stream a rich draft in private chats, then persist the final message."""app = await self._wait_for_app()if app is None:return@@ -1184,7 +1238,7 @@ class TelegramChannel(BaseChannel):stream_end = Falseif stream_end:buf = self._stream_bufs.get(chat_id)- if not buf or not buf.message_id or not buf.text:+ if not buf or not buf.text or (buf.message_id is None and buf.draft_id is None):returnif stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id:return@@ -1197,20 +1251,30 @@ class TelegramChannel(BaseChannel):thread_kwargs["message_thread_id"] = message_thread_idraw_text = buf.text- # Try upgrading the streaming preview to rich in place (Bot API 10.1:- # editMessageText gained a rich_message parameter). Editing in place- # keeps the message identity, so there is no delete-and-resend and- # none of the flickering / dropped line breaks from issue #4470.- # The previous branch here was unreachable: it was guarded by- # ``not buf.message_id`` after an early return had already ensured- # ``buf.message_id`` is set (issue #5516).- if self.config.rich_messages and not getattr(self, "_rich_send_disabled", False):- rich_ok = await self._try_edit_rich(int_chat_id, buf.message_id, raw_text)- if rich_ok:- self._stream_bufs.pop(chat_id, None)- return+ if buf.draft_id is not None:+ rich_chunks = _split_telegram_markdown(raw_text, TELEGRAM_RICH_MAX_LEN)+ for index, rich_chunk in enumerate(rich_chunks):+ if self._rich_streaming_enabled() and await self._try_send_stream_rich(+ int_chat_id, rich_chunk, thread_kwargs,+ ):+ buf.text = "\n".join(rich_chunks[index + 1:])+ continue+ legacy_chunks = [+ chunk+ for remaining in rich_chunks[index:]+ for chunk in _split_telegram_markdown(remaining, TELEGRAM_MAX_MESSAGE_LEN)+ ]+ for legacy_index, legacy_chunk in enumerate(legacy_chunks):+ await self._send_text(+ int_chat_id, legacy_chunk, thread_kwargs=thread_kwargs,+ )+ buf.text = "\n".join(legacy_chunks[legacy_index + 1:])+ break+ self._stream_bufs.pop(chat_id, None)+ return- # Legacy path: edit existing streaming message with HTML+ # Group chats and streams that rejected drafts keep the established+ # HTML/plain finalization path. Native rich drafts are private-only.html_chunks = _split_telegram_markdown_html(raw_text, TELEGRAM_HTML_MAX_LEN)primary_html = html_chunks[0]extra_html_chunks = html_chunks[1:]@@ -1221,16 +1285,16 @@ class TelegramChannel(BaseChannel):text=primary_html, parse_mode="HTML",)except BadRequest as e:- # Only fall back to plain text on actual HTML parse/format errors.- # Network errors (TimedOut, NetworkError) should propagate immediately- # to avoid doubling connection demand during pool exhaustion.if self._is_not_modified_error(e):self.logger.debug("Final stream edit already applied for {}", chat_id)self._stream_bufs.pop(chat_id, None)returnself.logger.debug("Final stream edit failed (HTML), trying plain: {}", e)- # Fall back to raw markdown (not HTML) so users don't see raw tags.- primary_plain = split_message(raw_text, TELEGRAM_MAX_MESSAGE_LEN)[0] if len(raw_text) > TELEGRAM_MAX_MESSAGE_LEN else raw_text+ primary_plain = (+ split_message(raw_text, TELEGRAM_MAX_MESSAGE_LEN)[0]+ if len(raw_text) > TELEGRAM_MAX_MESSAGE_LEN+ else raw_text+ )try:await self._call_with_retry(app.bot.edit_message_text,@@ -1242,7 +1306,7 @@ class TelegramChannel(BaseChannel):self.logger.debug("Final stream plain edit already applied for {}", chat_id)else:self.logger.warning("Final stream edit failed: {}", e2)- raise # Let ChannelManager handle retry+ raisefor extra_html_chunk in extra_html_chunks:try:await self._call_with_retry(@@ -1252,13 +1316,20 @@ class TelegramChannel(BaseChannel):**thread_kwargs,)except Exception:- # Fall back to _send_text which handles HTML→plain gracefully.- await self._send_text(int_chat_id, extra_html_chunk)+ await self._send_text(+ int_chat_id,+ extra_html_chunk,+ thread_kwargs=thread_kwargs,+ )self._stream_bufs.pop(chat_id, None)returnbuf = self._stream_bufs.get(chat_id)- if buf is None or (stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id):+ if buf is None or (+ stream_id is not None+ and buf.stream_id is not None+ and buf.stream_id != stream_id+ ):buf = _StreamBuf(stream_id=stream_id)self._stream_bufs[chat_id] = bufelif buf.stream_id is None:@@ -1272,26 +1343,68 @@ class TelegramChannel(BaseChannel):stream_thread_kwargs: dict[str, int] = {}if message_thread_id := meta.get("message_thread_id"):stream_thread_kwargs["message_thread_id"] = message_thread_id- if buf.message_id is None:- preview = _strip_md_block(buf.text)+ rich_draft_allowed = (+ self._rich_streaming_enabled() and meta.get("is_group") is False+ )+ if buf.message_id is None and buf.draft_id is None:try:- sent = await self._call_with_retry(- app.bot.send_message,- chat_id=int_chat_id, text=preview,- **stream_thread_kwargs,- )- buf.message_id = sent.message_id+ if rich_draft_allowed and len(buf.text) <= TELEGRAM_RICH_MAX_LEN:+ buf.draft_id = self._new_rich_draft_id()+ if not await self._try_send_rich_draft(+ int_chat_id,+ buf.draft_id,+ buf.text,+ stream_thread_kwargs,+ ):+ buf.draft_id = None+ if buf.draft_id is None:+ await self._start_legacy_stream(+ int_chat_id, buf, stream_thread_kwargs,+ )buf.last_edit = nowexcept Exception as e:self.logger.warning("Stream initial send failed: {}", e)- raise # Let ChannelManager handle retry- elif (now - buf.last_edit) >= self.config.stream_edit_interval:- if len(buf.text) > TELEGRAM_MAX_MESSAGE_LEN:- await self._flush_stream_overflow(int_chat_id, buf, stream_thread_kwargs)+ raise+ else:+ edit_interval = (+ max(self.config.stream_edit_interval, TELEGRAM_RICH_DRAFT_MIN_INTERVAL)+ if buf.draft_id is not None+ else self.config.stream_edit_interval+ )+ if (now - buf.last_edit) < edit_interval:+ return+ overflow_limit = TELEGRAM_RICH_MAX_LEN if buf.draft_id is not None else TELEGRAM_MAX_MESSAGE_LEN+ if len(buf.text) > overflow_limit:+ try:+ await self._flush_stream_overflow(+ int_chat_id, buf, stream_thread_kwargs,+ )+ except (NetworkError, RetryAfter) as exc:+ self.logger.warning(+ "Stream overflow flush paused with unsent content: {}", exc+ )+ # Persisted chunks have already been removed from ``buf``.+ # Let the next delta or stream_end resume that tail; manager+ # retry would append this same delta a second time.+ returnbuf.last_edit = nowreturn- preview = _strip_md_block(buf.text)try:+ if buf.draft_id is not None:+ if await self._try_send_rich_draft(+ int_chat_id,+ buf.draft_id,+ buf.text,+ stream_thread_kwargs,+ ):+ buf.last_edit = now+ return+ await self._start_legacy_stream(+ int_chat_id, buf, stream_thread_kwargs,+ )+ buf.last_edit = now+ return+ preview = _strip_md_block(buf.text)await self._call_with_retry(app.bot.edit_message_text,chat_id=int_chat_id, message_id=buf.message_id,@@ -1303,7 +1416,7 @@ class TelegramChannel(BaseChannel):buf.last_edit = nowreturnself.logger.warning("Stream edit failed: {}", e)- raise # Let ChannelManager handle retry+ raiseasync def _flush_stream_overflow(self,@@ -1311,12 +1424,45 @@ class TelegramChannel(BaseChannel):buf: "_StreamBuf",thread_kwargs: dict[str, int],) -> None:- """Split an oversized stream buffer mid-flight.+ """Persist full chunks and continue streaming into a new draft tail.- Edits the current stream message with the first chunk, sends any- intermediate chunks as standalone messages, then opens a new message- for the tail so subsequent deltas continue streaming into it.+ Rich chunks use the 32,768-character limit and preserve raw Markdown.+ If Telegram rejects rich formatting, the same buffer is flushed through+ the established HTML/plain path without losing stream state."""+ if buf.draft_id is not None:+ if not self._rich_streaming_enabled():+ await self._start_legacy_stream(chat_id, buf, thread_kwargs)+ return+ chunks = _split_telegram_markdown(buf.text, TELEGRAM_RICH_MAX_LEN)+ if len(chunks) <= 1:+ return++ for index, markdown in enumerate(chunks[:-1]):+ if await self._try_send_stream_rich(chat_id, markdown, thread_kwargs):+ buf.text = "\n".join(chunks[index + 1:])+ continue+ buf.text = "\n".join(chunks[index:])+ await self._start_legacy_stream(chat_id, buf, thread_kwargs)+ return++ buf.draft_id = self._new_rich_draft_id()+ if await self._try_send_rich_draft(+ chat_id, buf.draft_id, buf.text, thread_kwargs,+ ):+ return+ await self._start_legacy_stream(chat_id, buf, thread_kwargs)+ return++ await self._flush_stream_overflow_legacy(chat_id, buf, thread_kwargs)++ async def _flush_stream_overflow_legacy(+ self,+ chat_id: int,+ buf: "_StreamBuf",+ thread_kwargs: dict[str, int],+ ) -> None:+ """Legacy HTML/plain overflow path used when rich messaging is absent."""chunks = _split_telegram_markdown_html_chunks(buf.text, TELEGRAM_HTML_MAX_LEN)if len(chunks) <= 1:returndiff --git a/nanobot/channels/telegram/tests/test_telegram_channel.py b/nanobot/channels/telegram/tests/test_telegram_channel.pyindex e91a19995..1af3ef4bf 100644--- a/nanobot/channels/telegram/tests/test_telegram_channel.py+++ b/nanobot/channels/telegram/tests/test_telegram_channel.py@@ -18,6 +18,8 @@ from nanobot.bus.queue import MessageBusfrom nanobot.channels.telegram.runtime import (TELEGRAM_MAX_MESSAGE_LEN,TELEGRAM_REPLY_CONTEXT_MAX_LEN,+ TELEGRAM_RICH_DRAFT_MIN_INTERVAL,+ TELEGRAM_RICH_MAX_LEN,TelegramChannel,TelegramConfig,_markdown_to_telegram_html,@@ -906,6 +908,412 @@ async def test_rich_messages_default_skips_send_rich_message() -> None:assert channel._app.bot.sent_messages[0]["text"]+async def test_send_delta_rich_stream_updates_one_draft_then_persists_final() -> None:+ channel = TelegramChannel(+ TelegramConfig(+ enabled=True,+ token="123:abc",+ allow_from=["*"],+ rich_messages=True,+ stream_edit_interval=0.1,+ ),+ MessageBus(),+ )+ _install_ready_app(channel)+ channel._app.bot.do_api_request = AsyncMock(return_value=True)++ await channel.send_delta(+ "123",+ "# head",+ {"is_group": False, "message_thread_id": 42},+ stream_id="s:0",+ )+ draft_id = channel._stream_bufs["123"].draft_id+ assert draft_id is not None and draft_id != 0+ channel._stream_bufs["123"].last_edit = 0.0+ metadata = {"is_group": False, "message_thread_id": 42}+ await channel.send_delta("123", "\n\n**body**", metadata, stream_id="s:0")+ await channel.send_delta("123", "", metadata, stream_id="s:0", stream_end=True)++ calls = channel._app.bot.do_api_request.call_args_list+ assert [call.args[0] for call in calls] == [+ "sendRichMessageDraft",+ "sendRichMessageDraft",+ "sendRichMessage",+ ]+ assert calls[0].kwargs["api_kwargs"] == {+ "chat_id": 123,+ "draft_id": draft_id,+ "message_thread_id": 42,+ "rich_message": {"markdown": "# head"},+ }+ assert calls[1].kwargs["api_kwargs"]["draft_id"] == draft_id+ assert calls[1].kwargs["api_kwargs"]["rich_message"] == {+ "markdown": "# head\n\n**body**",+ }+ assert "draft_id" not in calls[2].kwargs["api_kwargs"]+ assert calls[2].kwargs["api_kwargs"]["message_thread_id"] == 42+ assert calls[2].kwargs["api_kwargs"]["rich_message"] == {+ "markdown": "# head\n\n**body**",+ }+ assert channel._app.bot.sent_messages == []+ assert "123" not in channel._stream_bufs+++async def test_send_delta_rich_initial_rejection_falls_back_to_legacy_preview() -> None:+ from telegram.error import BadRequest++ channel = TelegramChannel(+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),+ MessageBus(),+ )+ _install_ready_app(channel)+ channel._app.bot.do_api_request = AsyncMock(side_effect=BadRequest("can't parse rich message"))++ await channel.send_delta(+ "123", "# head", {"is_group": False}, stream_id="s:0",+ )++ channel._app.bot.do_api_request.assert_awaited_once()+ assert channel._app.bot.do_api_request.call_args.args[0] == "sendRichMessageDraft"+ assert channel._rich_send_disabled is False+ assert channel._app.bot.sent_messages[0]["text"] == "head"+ assert channel._stream_bufs["123"].message_id == 1+ assert channel._stream_bufs["123"].draft_id is None+++async def test_send_delta_rich_draft_rejection_falls_back_to_legacy_preview() -> None:+ from telegram.error import BadRequest++ channel = TelegramChannel(+ TelegramConfig(+ enabled=True,+ token="123:abc",+ allow_from=["*"],+ rich_messages=True,+ stream_edit_interval=0.1,+ ),+ MessageBus(),+ )+ _install_ready_app(channel)+ channel._stream_bufs["123"] = _StreamBuf(+ text="# head", draft_id=17, last_edit=0.0, stream_id="s:0",+ )+ channel._app.bot.do_api_request = AsyncMock(side_effect=BadRequest("can't parse rich message"))++ await channel.send_delta("123", "\nbody", stream_id="s:0")++ assert channel._app.bot.do_api_request.call_args.args[0] == "sendRichMessageDraft"+ rich_kwargs = channel._app.bot.do_api_request.call_args.kwargs["api_kwargs"]+ assert rich_kwargs["draft_id"] == 17+ assert rich_kwargs["rich_message"] == {"markdown": "# head\nbody"}+ assert channel._app.bot.sent_messages[0]["text"] == "head\nbody"+ assert channel._stream_bufs["123"].message_id == 1+ assert channel._stream_bufs["123"].draft_id is None+++async def test_send_delta_rich_stream_end_persists_draft_with_send_rich_message() -> None:+ channel = TelegramChannel(+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),+ MessageBus(),+ )+ _install_ready_app(channel)+ channel._stream_bufs["123"] = _StreamBuf(+ text="# heading\n\n| a | b |\n|---|---|\n| 1 | 2 |",+ draft_id=17,+ last_edit=0.0,+ stream_id="s:0",+ )+ channel._app.bot.do_api_request = AsyncMock(return_value=True)+ channel._app.bot.delete_message = AsyncMock()++ await channel.send_delta("123", "", stream_id="s:0", stream_end=True)++ call = channel._app.bot.do_api_request.call_args+ assert call.args[0] == "sendRichMessage"+ assert "draft_id" not in call.kwargs["api_kwargs"]+ assert call.kwargs["api_kwargs"]["rich_message"]["markdown"].startswith("# heading")+ channel._app.bot.delete_message.assert_not_awaited()+ assert channel._app.bot.sent_messages == []+ assert "123" not in channel._stream_bufs+++async def test_send_delta_rich_stream_end_timeout_is_not_retried() -> None:+ from telegram.error import TimedOut++ channel = TelegramChannel(+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),+ MessageBus(),+ )+ _install_ready_app(channel)+ channel._stream_bufs["123"] = _StreamBuf(+ text="final answer",+ draft_id=17,+ last_edit=0.0,+ stream_id="s:0",+ )+ channel._app.bot.do_api_request = AsyncMock(side_effect=TimedOut("ambiguous timeout"))++ await channel.send_delta("123", "", stream_id="s:0", stream_end=True)++ channel._app.bot.do_api_request.assert_awaited_once()+ assert channel._app.bot.do_api_request.call_args.args[0] == "sendRichMessage"+ assert channel._app.bot.sent_messages == []+ assert "123" not in channel._stream_bufs++[email protected]("rich_error", [None, "can't parse rich message", "method not found"])+async def test_rich_stream_final_retry_keeps_only_unsent_chunks(+ rich_error: str | None,+) -> None:+ from telegram.error import BadRequest, NetworkError++ channel = TelegramChannel(+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),+ MessageBus(),+ )+ _install_ready_app(channel)+ delivered: list[str] = []+ failed = False++ async def deliver(text):+ nonlocal failed+ if text.startswith("B") and not failed:+ failed = True+ raise NetworkError("second chunk connection failed")+ delivered.append(text)+ return SimpleNamespace(message_id=len(delivered))++ async def api(method, *, api_kwargs):+ if method == "sendRichMessage":+ if rich_error:+ raise BadRequest(rich_error)+ return await deliver(api_kwargs["rich_message"]["markdown"])+ return True++ async def send_message(**kwargs):+ return await deliver(kwargs["text"])++ channel._app.bot.do_api_request = AsyncMock(side_effect=api)+ channel._app.bot.send_message = AsyncMock(side_effect=send_message)+ metadata = {"is_group": False}+ await channel.send_delta("123", "A", metadata, stream_id="s:0")+ delta = "A" * (TELEGRAM_RICH_MAX_LEN - 1) + "\n" + "B" * TELEGRAM_RICH_MAX_LEN + "\nC"+ await channel.send_delta("123", delta, metadata, stream_id="s:0")++ with pytest.raises(NetworkError, match="second chunk"):+ await channel.send_delta(+ "123", "", metadata, stream_id="s:0", stream_end=True,+ )+ assert "".join(delivered) == "A" * TELEGRAM_RICH_MAX_LEN+ assert channel._stream_bufs["123"].text.replace("\n", "") == (+ "B" * TELEGRAM_RICH_MAX_LEN + "C"+ )++ await channel.send_delta(+ "123", "", metadata, stream_id="s:0", stream_end=True,+ )++ assert "".join(delivered).replace("\n", "") == (+ "A" * TELEGRAM_RICH_MAX_LEN + "B" * TELEGRAM_RICH_MAX_LEN + "C"+ )+ assert "123" not in channel._stream_bufs+++async def test_rich_stream_overflow_failure_does_not_drop_same_text_next_delta(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from telegram.error import NetworkError++ monkeypatch.setattr("nanobot.channels.telegram.runtime.TELEGRAM_RICH_MAX_LEN", 4)+ channel = TelegramChannel(+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),+ MessageBus(),+ )+ _install_ready_app(channel)+ delivered: list[str] = []+ failed = False++ async def api(method, *, api_kwargs):+ nonlocal failed+ if method == "sendRichMessage":+ text = api_kwargs["rich_message"]["markdown"]+ if text == "BBBB" and not failed:+ failed = True+ raise NetworkError("second chunk connection failed")+ delivered.append(text)+ return True++ channel._app.bot.do_api_request = AsyncMock(side_effect=api)+ metadata = {"is_group": False}+ await channel.send_delta("123", "A", metadata, stream_id="s:0")+ channel._stream_bufs["123"].last_edit = 0.0+ repeated_delta = "AAA\nBBBB\nC"++ await channel.send_delta("123", repeated_delta, metadata, stream_id="s:0")++ assert delivered == ["AAAA"]+ assert channel._stream_bufs["123"].text == "BBBB\nC"++ await channel.send_delta("123", repeated_delta, metadata, stream_id="s:0")+ await channel.send_delta("123", "", metadata, stream_id="s:0", stream_end=True)++ assert "".join(delivered).replace("\n", "") == (+ "A" + repeated_delta + repeated_delta+ ).replace("\n", "")+ assert "123" not in channel._stream_bufs+++async def test_send_delta_rich_does_not_flush_at_legacy_limit() -> None:+ channel = TelegramChannel(+ TelegramConfig(+ enabled=True,+ token="123:abc",+ allow_from=["*"],+ rich_messages=True,+ stream_edit_interval=0.1,+ ),+ MessageBus(),+ )+ _install_ready_app(channel)+ channel._stream_bufs["123"] = _StreamBuf(+ text="x" * TELEGRAM_MAX_MESSAGE_LEN,+ draft_id=17,+ last_edit=0.0,+ stream_id="s:0",+ )+ channel._app.bot.do_api_request = AsyncMock(return_value=True)++ await channel.send_delta("123", "y", stream_id="s:0")++ channel._app.bot.do_api_request.assert_awaited_once()+ call = channel._app.bot.do_api_request.call_args+ assert call.args[0] == "sendRichMessageDraft"+ assert call.kwargs["api_kwargs"]["draft_id"] == 17+ assert len(call.kwargs["api_kwargs"]["rich_message"]["markdown"]) == (+ TELEGRAM_MAX_MESSAGE_LEN + 1+ )+ assert channel._app.bot.sent_messages == []+ assert channel._stream_bufs["123"].text.endswith("y")+++async def test_flush_stream_overflow_uses_rich_limit_and_reanchors_tail() -> None:+ channel = TelegramChannel(+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),+ MessageBus(),+ )+ _install_ready_app(channel)+ text = "x" * TELEGRAM_RICH_MAX_LEN + "\n" + "**tail**"+ channel._stream_bufs["123"] = _StreamBuf(+ text=text,+ draft_id=17,+ last_edit=0.0,+ stream_id="s:0",+ )+ channel._app.bot.do_api_request = AsyncMock(return_value=True)++ await channel._flush_stream_overflow(+ 123, channel._stream_bufs["123"], {"message_thread_id": 42},+ )++ calls = channel._app.bot.do_api_request.call_args_list+ assert [call.args[0] for call in calls] == [+ "sendRichMessage",+ "sendRichMessageDraft",+ ]+ first = calls[0].kwargs["api_kwargs"]["rich_message"]["markdown"]+ tail = calls[1].kwargs["api_kwargs"]["rich_message"]["markdown"]+ assert len(first) <= TELEGRAM_RICH_MAX_LEN+ assert len(tail) <= TELEGRAM_RICH_MAX_LEN+ assert calls[1].kwargs["api_kwargs"]["message_thread_id"] == 42+ assert channel._stream_bufs["123"].message_id is None+ assert channel._stream_bufs["123"].draft_id not in (None, 0, 17)+ assert channel._stream_bufs["123"].text == tail+ assert channel._app.bot.sent_messages == []+++async def test_flush_stream_overflow_rich_limit_counts_unicode_characters() -> None:+ channel = TelegramChannel(+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),+ MessageBus(),+ )+ _install_ready_app(channel)+ text = "测" * TELEGRAM_RICH_MAX_LEN + "\n尾"+ buf = _StreamBuf(text=text, draft_id=17, last_edit=0.0, stream_id="s:0")+ channel._app.bot.do_api_request = AsyncMock(return_value=True)++ await channel._flush_stream_overflow(123, buf, {})++ calls = channel._app.bot.do_api_request.call_args_list+ first = calls[0].kwargs["api_kwargs"]["rich_message"]["markdown"]+ assert len(first) == TELEGRAM_RICH_MAX_LEN+ assert len(first.encode("utf-8")) > TELEGRAM_RICH_MAX_LEN+ assert buf.text == "尾"+++async def test_send_delta_rich_draft_rate_is_limited_to_40_per_30_seconds() -> None:+ channel = TelegramChannel(+ TelegramConfig(+ enabled=True,+ token="123:abc",+ allow_from=["*"],+ rich_messages=True,+ stream_edit_interval=0.1,+ ),+ MessageBus(),+ )+ _install_ready_app(channel)+ channel._app.bot.do_api_request = AsyncMock(return_value=True)++ await channel.send_delta(+ "123", "first", {"is_group": False}, stream_id="s:0",+ )+ first_edit = channel._stream_bufs["123"].last_edit+ await channel.send_delta("123", " second", stream_id="s:0")++ assert TELEGRAM_RICH_DRAFT_MIN_INTERVAL == 30 / 40+ channel._app.bot.do_api_request.assert_awaited_once()+ assert channel._stream_bufs["123"].last_edit == first_edit+++async def test_send_delta_rich_group_uses_legacy_preview_and_finalization() -> None:+ channel = TelegramChannel(+ TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),+ MessageBus(),+ )+ _install_ready_app(channel)+ channel._app.bot.do_api_request = AsyncMock(return_value=True)+ channel._app.bot.edit_message_text = AsyncMock()+ metadata = {"is_group": True}++ await channel.send_delta(+ "-100123", "**hello**", metadata, stream_id="s:0",+ )+ await channel.send_delta("-100123", "", metadata, stream_id="s:0", stream_end=True)++ channel._app.bot.do_api_request.assert_not_awaited()+ assert channel._app.bot.sent_messages[0]["text"] == "hello"+ channel._app.bot.edit_message_text.assert_awaited_once_with(+ chat_id=-100123,+ message_id=1,+ text="<b>hello</b>",+ parse_mode="HTML",+ )+ assert "-100123" not in channel._stream_bufs++@pytest.mark.asyncioasync def test_on_error_logs_network_issues_as_warning(monkeypatch) -> None:from telegram.error import NetworkError@@ -2737,63 +3145,6 @@ def test_markdown_to_html_code_block_same_line_no_newline() -> None:assert stripped == "Use <tag> here"-async def test_send_delta_stream_end_upgrades_preview_to_rich_in_place() -> None:- """Rich messages finally work with streaming: the preview is upgraded via- editMessageText rich_message (in place), not delete-and-resend (issue #5516)."""- from telegram.error import BadRequest-- channel = TelegramChannel(- TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),- MessageBus(),- )- _install_ready_app(channel)- channel._app.bot.do_api_request = AsyncMock()- channel._app.bot.edit_message_text = AsyncMock(side_effect=BadRequest("should not be reached"))- channel._stream_bufs["123"] = _StreamBuf(text="**hello**", message_id=7, last_edit=0.0)-- await channel.send_delta("123", "", stream_end=True)-- # editMessageText with rich_message payload, in place (same message_id)- channel._app.bot.do_api_request.assert_awaited_once()- args, kwargs = channel._app.bot.do_api_request.await_args- assert args[0] == "editMessageText"- assert kwargs["api_kwargs"]["chat_id"] == 123- assert kwargs["api_kwargs"]["message_id"] == 7- assert kwargs["api_kwargs"]["rich_message"] == {"markdown": "**hello**"}- # No delete-and-resend, no legacy HTML edit- channel._app.bot.edit_message_text.assert_not_awaited()- assert "123" not in channel._stream_bufs---async def test_send_delta_stream_end_rich_capability_error_latches_and_falls_back() -> None:- """On a pre-10.1 Bot API server the rich edit fails, the latch trips, and the- legacy HTML edit handles the final output."""- from telegram.error import BadRequest-- channel = TelegramChannel(- TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),- MessageBus(),- )- _install_ready_app(channel)- # Before Bot API 10.1, editMessageText ignores rich_message and requires text.- channel._app.bot.do_api_request = AsyncMock(- side_effect=BadRequest("Message text is empty")- )- channel._app.bot.edit_message_text = AsyncMock()- channel._stream_bufs["123"] = _StreamBuf(text="hello", message_id=7, last_edit=0.0)-- await channel.send_delta("123", "", stream_end=True)-- channel._app.bot.do_api_request.assert_awaited_once()- # Latch tripped: subsequent sends skip the rich path entirely- assert channel._rich_send_disabled is True- # Legacy HTML edit handled the final message- channel._app.bot.edit_message_text.assert_awaited_once()- assert "123" not in channel._stream_bufs--@pytest.mark.asyncioasync def test_send_delta_stream_end_rich_disabled_uses_legacy_html() -> None:"""rich_messages=False (the default) keeps the legacy HTML path untouched."""@@ -2811,54 +3162,3 @@ async def test_send_delta_stream_end_rich_disabled_uses_legacy_html() -> None:channel._app.bot.do_api_request.assert_not_called()channel._app.bot.edit_message_text.assert_awaited_once()assert "123" not in channel._stream_bufs---async def test_send_delta_stream_end_rich_network_error_propagates_for_retry() -> None:- """A transport failure on the rich edit must propagate so ChannelManager- retries the buffered send — not fall through to an immediate legacy edit- that doubles connection demand during pool exhaustion."""- from telegram.error import NetworkError-- channel = TelegramChannel(- TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),- MessageBus(),- )- _install_ready_app(channel)- channel._app.bot.do_api_request = AsyncMock(side_effect=NetworkError("pool exhausted"))- channel._app.bot.edit_message_text = AsyncMock()- channel._stream_bufs["123"] = _StreamBuf(text="hello", message_id=7, last_edit=0.0)-- with pytest.raises(NetworkError):- await channel.send_delta("123", "", stream_end=True)-- # No legacy fallback edit: the buffered state stays for the manager retry.- channel._app.bot.edit_message_text.assert_not_awaited()- assert "123" in channel._stream_bufs---async def test_send_delta_stream_end_rich_not_modified_after_timeout_is_success() -> None:- """Ambiguous success: the rich edit applied server-side but its response- timed out, so the retry hit "message is not modified". That is a completed- rich upgrade — the legacy edit must not overwrite it."""- from telegram.error import BadRequest, TimedOut-- channel = TelegramChannel(- TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=True),- MessageBus(),- )- _install_ready_app(channel)- # First attempt (inside _call_with_retry) times out, retry reports the- # edit as already applied.- channel._app.bot.do_api_request = AsyncMock(- side_effect=[TimedOut(), BadRequest("Message is not modified")]- )- channel._app.bot.edit_message_text = AsyncMock(side_effect=AssertionError("must not overwrite rich result"))- channel._stream_bufs["123"] = _StreamBuf(text="hello", message_id=7, last_edit=0.0)-- await channel.send_delta("123", "", stream_end=True)-- assert channel._app.bot.do_api_request.await_count == 2- channel._app.bot.edit_message_text.assert_not_awaited()- assert "123" not in channel._stream_bufs