fix(channels): preserve indentation across message splits
Signed-off-by: PP1 <74917296+pengpengyi92@users.noreply.github.com>
This commit is contained in:
1 parent
a8cbcc1c81
commit
01760b7385
4 files changed
+46
-17
No files matched your search
@@ -639,14 +639,23 @@ def split_message(content: str, max_len: int = 2000) -> list[str]:
|
||||
chunks.append(content)
|
||||
break
|
||||
cut = content[:max_len]
|
||||
# Try to break at newline first, then space, then hard break
|
||||
pos = cut.rfind("\n")
|
||||
if pos <= 0:
|
||||
pos = cut.rfind(" ")
|
||||
if pos <= 0:
|
||||
pos = max_len
|
||||
chunks.append(content[:pos])
|
||||
content = content[pos:].lstrip()
|
||||
# Consume only the newline itself so indentation starts the next chunk.
|
||||
newline_pos = cut.rfind("\n")
|
||||
if newline_pos > 0:
|
||||
chunks.append(content[:newline_pos])
|
||||
content = content[newline_pos + 1 :]
|
||||
continue
|
||||
|
||||
# Keep the existing word-boundary behavior, but avoid emitting a
|
||||
# whitespace-only chunk when an indented line exceeds max_len.
|
||||
space_pos = cut.rfind(" ")
|
||||
if space_pos > 0 and cut[:space_pos].strip():
|
||||
chunks.append(content[:space_pos])
|
||||
content = content[space_pos:].lstrip()
|
||||
continue
|
||||
|
||||
chunks.append(content[:max_len])
|
||||
content = content[max_len:]
|
||||
return chunks
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user