fix(channels): harden message split boundaries
This commit is contained in:
1 parent
8509432dcf
commit
b04aeeac0e
3 files changed
+85
-5
No files matched your search
@@ -633,6 +633,7 @@ def split_message(content: str, max_len: int = 2000) -> list[str]:
|
||||
return [content]
|
||||
if len(content) <= max_len:
|
||||
return [content]
|
||||
original_content = content
|
||||
chunks: list[str] = []
|
||||
while content:
|
||||
if len(content) <= max_len:
|
||||
@@ -642,8 +643,14 @@ def split_message(content: str, max_len: int = 2000) -> list[str]:
|
||||
cut = content[:max_len]
|
||||
# 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])
|
||||
if newline_pos >= 0:
|
||||
# Exclude both bytes of a CRLF boundary from the emitted chunk.
|
||||
line_end = newline_pos
|
||||
if line_end > 0 and content[line_end - 1] == "\r":
|
||||
line_end -= 1
|
||||
chunk = content[:line_end]
|
||||
if chunk.strip():
|
||||
chunks.append(chunk)
|
||||
content = content[newline_pos + 1 :]
|
||||
continue
|
||||
|
||||
@@ -652,11 +659,31 @@ def split_message(content: str, max_len: int = 2000) -> list[str]:
|
||||
space_pos = cut.rfind(" ")
|
||||
if space_pos > 0 and cut[:space_pos].strip():
|
||||
chunks.append(content[:space_pos])
|
||||
content = content[space_pos:].lstrip()
|
||||
content = content[space_pos:].lstrip(" \t")
|
||||
# A space boundary may sit immediately before a line break. Drop
|
||||
# that delimiter too, without stripping the next line's indent.
|
||||
if content.startswith("\r\n"):
|
||||
content = content[2:]
|
||||
elif content.startswith("\n"):
|
||||
content = content[1:]
|
||||
continue
|
||||
|
||||
chunks.append(content[:max_len])
|
||||
# Do not split between the two code points of a CRLF delimiter.
|
||||
if cut.endswith("\r") and content[max_len : max_len + 1] == "\n":
|
||||
chunk = cut[:-1]
|
||||
if chunk.strip():
|
||||
chunks.append(chunk)
|
||||
content = content[max_len + 1 :]
|
||||
continue
|
||||
|
||||
chunk = content[:max_len]
|
||||
if chunk.strip():
|
||||
chunks.append(chunk)
|
||||
content = content[max_len:]
|
||||
if not chunk.strip():
|
||||
# Keep any remaining indentation so the final non-blank chunk can
|
||||
# retain as much of it as the channel limit permits.
|
||||
continue
|
||||
# A delimiter can sit immediately after the hard-break boundary. Keep
|
||||
# ordinary space trimming, but consume only the newline so indentation
|
||||
# on the following line is preserved.
|
||||
@@ -665,7 +692,10 @@ def split_message(content: str, max_len: int = 2000) -> list[str]:
|
||||
content = content[2:]
|
||||
elif content.startswith("\n"):
|
||||
content = content[1:]
|
||||
return chunks
|
||||
# Preserve the historical non-empty-input contract for callers that take
|
||||
# the first chunk directly. This fallback is only reachable for content
|
||||
# made entirely of whitespace.
|
||||
return chunks or [original_content[:max_len]]
|
||||
|
||||
|
||||
def build_assistant_message(
|
||||
|
||||
Reference in new issue
Block a user