mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-04-24 03:46:02 +00:00
Merge PR #2520: fix(telegram): split oversized final streamed replies
fix(telegram): split oversized final streamed replies
This commit is contained in:
commit
9174a85b4e
@ -558,8 +558,10 @@ class TelegramChannel(BaseChannel):
|
|||||||
await self._remove_reaction(chat_id, int(reply_to_message_id))
|
await self._remove_reaction(chat_id, int(reply_to_message_id))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
chunks = split_message(buf.text, TELEGRAM_MAX_MESSAGE_LEN)
|
||||||
|
primary_text = chunks[0] if chunks else buf.text
|
||||||
try:
|
try:
|
||||||
html = _markdown_to_telegram_html(buf.text)
|
html = _markdown_to_telegram_html(primary_text)
|
||||||
await self._call_with_retry(
|
await self._call_with_retry(
|
||||||
self._app.bot.edit_message_text,
|
self._app.bot.edit_message_text,
|
||||||
chat_id=int_chat_id, message_id=buf.message_id,
|
chat_id=int_chat_id, message_id=buf.message_id,
|
||||||
@ -575,15 +577,18 @@ class TelegramChannel(BaseChannel):
|
|||||||
await self._call_with_retry(
|
await self._call_with_retry(
|
||||||
self._app.bot.edit_message_text,
|
self._app.bot.edit_message_text,
|
||||||
chat_id=int_chat_id, message_id=buf.message_id,
|
chat_id=int_chat_id, message_id=buf.message_id,
|
||||||
text=buf.text,
|
text=primary_text,
|
||||||
)
|
)
|
||||||
except Exception as e2:
|
except Exception as e2:
|
||||||
if self._is_not_modified_error(e2):
|
if self._is_not_modified_error(e2):
|
||||||
logger.debug("Final stream plain edit already applied for {}", chat_id)
|
logger.debug("Final stream plain edit already applied for {}", chat_id)
|
||||||
self._stream_bufs.pop(chat_id, None)
|
else:
|
||||||
return
|
logger.warning("Final stream edit failed: {}", e2)
|
||||||
logger.warning("Final stream edit failed: {}", e2)
|
raise # Let ChannelManager handle retry
|
||||||
raise # Let ChannelManager handle retry
|
# If final content exceeds Telegram limit, keep the first chunk in
|
||||||
|
# the edited stream message and send the rest as follow-up messages.
|
||||||
|
for extra_chunk in chunks[1:]:
|
||||||
|
await self._send_text(int_chat_id, extra_chunk)
|
||||||
self._stream_bufs.pop(chat_id, None)
|
self._stream_bufs.pop(chat_id, None)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@ -385,6 +385,32 @@ async def test_send_delta_stream_end_treats_not_modified_as_success() -> None:
|
|||||||
assert "123" not in channel._stream_bufs
|
assert "123" not in channel._stream_bufs
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_delta_stream_end_splits_oversized_reply() -> None:
|
||||||
|
"""Final streamed reply exceeding Telegram limit is split into chunks."""
|
||||||
|
from nanobot.channels.telegram import TELEGRAM_MAX_MESSAGE_LEN
|
||||||
|
|
||||||
|
channel = TelegramChannel(
|
||||||
|
TelegramConfig(enabled=True, token="123:abc", allow_from=["*"]),
|
||||||
|
MessageBus(),
|
||||||
|
)
|
||||||
|
channel._app = _FakeApp(lambda: None)
|
||||||
|
channel._app.bot.edit_message_text = AsyncMock()
|
||||||
|
channel._app.bot.send_message = AsyncMock(return_value=SimpleNamespace(message_id=99))
|
||||||
|
|
||||||
|
oversized = "x" * (TELEGRAM_MAX_MESSAGE_LEN + 500)
|
||||||
|
channel._stream_bufs["123"] = _StreamBuf(text=oversized, message_id=7, last_edit=0.0)
|
||||||
|
|
||||||
|
await channel.send_delta("123", "", {"_stream_end": True})
|
||||||
|
|
||||||
|
channel._app.bot.edit_message_text.assert_called_once()
|
||||||
|
edit_text = channel._app.bot.edit_message_text.call_args.kwargs.get("text", "")
|
||||||
|
assert len(edit_text) <= TELEGRAM_MAX_MESSAGE_LEN
|
||||||
|
|
||||||
|
channel._app.bot.send_message.assert_called_once()
|
||||||
|
assert "123" not in channel._stream_bufs
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_send_delta_new_stream_id_replaces_stale_buffer() -> None:
|
async def test_send_delta_new_stream_id_replaces_stale_buffer() -> None:
|
||||||
channel = TelegramChannel(
|
channel = TelegramChannel(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user