mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-04 08:28:36 +00:00
fix(utils): avoid hang in split_message when max_len <= 0
When max_len is 0 or negative the cut pointer never advances, so the loop hangs. Return the content unsplit, matching truncate_text_to_tokens for non-positive budgets.
This commit is contained in:
parent
fe0e65928d
commit
7ac9a46978
@ -526,6 +526,9 @@ def split_message(content: str, max_len: int = 2000) -> list[str]:
|
|||||||
"""
|
"""
|
||||||
if not content:
|
if not content:
|
||||||
return []
|
return []
|
||||||
|
# Non-positive max_len cannot advance the cut pointer; return unsplit.
|
||||||
|
if max_len <= 0:
|
||||||
|
return [content]
|
||||||
if len(content) <= max_len:
|
if len(content) <= max_len:
|
||||||
return [content]
|
return [content]
|
||||||
chunks: list[str] = []
|
chunks: list[str] = []
|
||||||
|
|||||||
@ -12,6 +12,13 @@ def test_split_message_no_code_blocks_unchanged():
|
|||||||
assert split_message(content, max_len=12) == ["alpha beta", "gamma delta"]
|
assert split_message(content, max_len=12) == ["alpha beta", "gamma delta"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_split_message_nonpositive_maxlen_returns_unsplit():
|
||||||
|
content = "alpha beta gamma delta"
|
||||||
|
|
||||||
|
assert split_message(content, max_len=0) == [content]
|
||||||
|
assert split_message(content, max_len=-1) == [content]
|
||||||
|
|
||||||
|
|
||||||
def test_truncate_text_to_tokens_keeps_text_within_budget():
|
def test_truncate_text_to_tokens_keeps_text_within_budget():
|
||||||
text = "hello world " * 100
|
text = "hello world " * 100
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user