27 lines
704 B
Python
27 lines
704 B
Python
TG_LIMIT = 4096
|
|
|
|
|
|
def chunk_text(text: str, max_length: int = TG_LIMIT) -> list[str]:
|
|
"""Split text into ≤max_length chunks, preferring newline then space boundaries."""
|
|
if len(text) <= max_length:
|
|
return [text]
|
|
|
|
chunks: list[str] = []
|
|
while text:
|
|
if len(text) <= max_length:
|
|
chunks.append(text)
|
|
break
|
|
|
|
split_at = text.rfind('\n', 0, max_length)
|
|
if split_at == -1:
|
|
split_at = text.rfind(' ', 0, max_length)
|
|
if split_at == -1:
|
|
split_at = max_length
|
|
|
|
chunk = text[:split_at].strip()
|
|
if chunk:
|
|
chunks.append(chunk)
|
|
text = text[split_at:].strip()
|
|
|
|
return chunks
|