mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-06 17:38:35 +00:00
fix(telegram): hard-cut when fence body cut lands on the prefix
A leading space in the fenced body made the soft cut land at min_code_pos and re-emit the same fence forever. Require progress past the fence line.
This commit is contained in:
parent
98d661775e
commit
7e9426d9bd
@ -102,7 +102,7 @@ def _split_telegram_markdown(content: str, max_len: int) -> list[str]:
|
|||||||
adjusted = recut.rfind("\n", min_code_pos)
|
adjusted = recut.rfind("\n", min_code_pos)
|
||||||
if adjusted < min_code_pos:
|
if adjusted < min_code_pos:
|
||||||
adjusted = recut.rfind(" ", min_code_pos)
|
adjusted = recut.rfind(" ", min_code_pos)
|
||||||
pos = adjusted if adjusted >= min_code_pos else budget
|
pos = adjusted if adjusted > min_code_pos else budget
|
||||||
elif pos + len(closing) > max_len:
|
elif pos + len(closing) > max_len:
|
||||||
budget = max_len - len(closing)
|
budget = max_len - len(closing)
|
||||||
if budget <= min_code_pos:
|
if budget <= min_code_pos:
|
||||||
@ -113,7 +113,11 @@ def _split_telegram_markdown(content: str, max_len: int) -> list[str]:
|
|||||||
adjusted = recut.rfind("\n", min_code_pos)
|
adjusted = recut.rfind("\n", min_code_pos)
|
||||||
if adjusted < min_code_pos:
|
if adjusted < min_code_pos:
|
||||||
adjusted = recut.rfind(" ", min_code_pos)
|
adjusted = recut.rfind(" ", min_code_pos)
|
||||||
pos = adjusted if adjusted >= min_code_pos else budget
|
pos = adjusted if adjusted > min_code_pos else budget
|
||||||
|
if pos <= min_code_pos:
|
||||||
|
chunks.append(content[:max_len])
|
||||||
|
content = content[max_len:].lstrip()
|
||||||
|
continue
|
||||||
chunks.append(content[:pos] + closing)
|
chunks.append(content[:pos] + closing)
|
||||||
remainder = content[pos:]
|
remainder = content[pos:]
|
||||||
if remainder.startswith("\n"):
|
if remainder.startswith("\n"):
|
||||||
|
|||||||
@ -293,6 +293,19 @@ def test_split_telegram_markdown_tiny_limit_with_early_body_newline() -> None:
|
|||||||
assert plain.count("a") >= 100
|
assert plain.count("a") >= 100
|
||||||
|
|
||||||
|
|
||||||
|
def test_split_telegram_markdown_leading_space_in_fence_body() -> None:
|
||||||
|
body = "a" * 4500
|
||||||
|
content = f"```\n {body}"
|
||||||
|
|
||||||
|
chunks = _split_telegram_markdown(content, TELEGRAM_MAX_MESSAGE_LEN)
|
||||||
|
|
||||||
|
assert chunks
|
||||||
|
assert all(len(chunk) <= TELEGRAM_MAX_MESSAGE_LEN for chunk in chunks)
|
||||||
|
plain = "".join(chunks).replace("```", "").replace("\n", "")
|
||||||
|
assert plain.count("a") == 4500
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_start_creates_separate_pools_with_proxy(monkeypatch) -> None:
|
async def test_start_creates_separate_pools_with_proxy(monkeypatch) -> None:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user