From 8509432dcfb09a43df8303d61fb07d33e233b67e Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Wed, 12 Aug 2026 00:14:56 +0800 Subject: [PATCH] fix(channels): avoid blank boundary chunks Signed-off-by: chengyongru <2755839590@qq.com> --- nanobot/utils/helpers.py | 11 ++++++++++- tests/utils/test_helpers.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/nanobot/utils/helpers.py b/nanobot/utils/helpers.py index 97f3df0b1..1832d0663 100644 --- a/nanobot/utils/helpers.py +++ b/nanobot/utils/helpers.py @@ -636,7 +636,8 @@ def split_message(content: str, max_len: int = 2000) -> list[str]: chunks: list[str] = [] while content: if len(content) <= max_len: - chunks.append(content) + if content.strip(): + chunks.append(content) break cut = content[:max_len] # Consume only the newline itself so indentation starts the next chunk. @@ -656,6 +657,14 @@ def split_message(content: str, max_len: int = 2000) -> list[str]: chunks.append(content[:max_len]) content = content[max_len:] + # A delimiter can sit immediately after the hard-break boundary. Keep + # ordinary space trimming, but consume only the newline so indentation + # on the following line is preserved. + content = content.lstrip(" \t") + if content.startswith("\r\n"): + content = content[2:] + elif content.startswith("\n"): + content = content[1:] return chunks diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py index f813a7170..9d4b2d4fd 100644 --- a/tests/utils/test_helpers.py +++ b/tests/utils/test_helpers.py @@ -29,6 +29,23 @@ def test_split_message_preserves_indentation_across_hard_break(): assert split_message(content, max_len=8) == ["head", " abcd", "efghij"] +def test_split_message_preserves_indentation_when_newline_is_at_hard_break(): + content = "abcdefgh\n code" + + assert split_message(content, max_len=8) == ["abcdefgh", " code"] + assert split_message(content.replace("\n", "\r\n"), max_len=8) == [ + "abcdefgh", + " code", + ] + + +def test_split_message_drops_whitespace_only_tail_after_hard_break(): + prefix = "abcdefgh" + + assert split_message(prefix + "\n", max_len=8) == [prefix] + assert split_message(prefix + " ", max_len=8) == [prefix] + + def test_split_message_nonpositive_maxlen_returns_unsplit(): content = "alpha beta gamma delta"