fix(telegram): hard-cut fence splits when the closer cannot fit

Adaptive HTML limits can shrink max_len to the fence prefix size. Treat
budget <= min_code_pos as a hard cut so the splitter still advances.
This commit is contained in:
santhreal authored and chengyongru committed 2026-07-23 23:56:18 +08:00
1 parent 017a4946e2
commit 98d661775e
2 files changed
+36 -10

No files matched your search

+10 -10
View File
@@ -93,7 +93,7 @@ def _split_telegram_markdown(content: str, max_len: int) -> list[str]:
# When the only break in range is the opening fence newline,
# cutting there re-emits the same fence and never advances.
if pos < min_code_pos:
if min_code_pos + len(closing) > max_len:
if min_code_pos + len(closing) >= max_len:
chunks.append(content[:max_len])
content = content[max_len:].lstrip()
continue
@@ -105,15 +105,15 @@ def _split_telegram_markdown(content: str, max_len: int) -> list[str]:
pos = adjusted if adjusted >= min_code_pos else budget
elif pos + len(closing) > max_len:
budget = max_len - len(closing)
if budget > 0:
recut = content[:budget]
adjusted = recut.rfind("\n", min_code_pos)
if adjusted < min_code_pos:
adjusted = recut.rfind(" ", min_code_pos)
pos = adjusted if adjusted >= min_code_pos else budget
else:
closing = "```"
pos = max_len - len(closing)
if budget <= min_code_pos:
chunks.append(content[:max_len])
content = content[max_len:].lstrip()
continue
recut = content[:budget]
adjusted = recut.rfind("\n", min_code_pos)
if adjusted < min_code_pos:
adjusted = recut.rfind(" ", min_code_pos)
pos = adjusted if adjusted >= min_code_pos else budget
chunks.append(content[:pos] + closing)
remainder = content[pos:]
if remainder.startswith("\n"):