fix(ai_auto_response): avoid forced split of single short replies

This commit is contained in:
liuwei
2026-04-10 16:51:11 +08:00
parent 5e80287530
commit 0c90c5d74f
7 changed files with 82 additions and 5 deletions

View File

@@ -12,11 +12,11 @@ def finalize_reply(response: str, reply_mode: str) -> List[str]:
text = text.replace("\n", " ").strip()
if reply_mode == "social_short":
return split_reply_chunks(text, sentence_limit=2, char_limit=24, chunk_limit=2)
return split_reply_chunks(text, sentence_limit=2, char_limit=24, chunk_limit=2, allow_clip_split=False)
if reply_mode == "qa_fast":
return split_reply_chunks(text, sentence_limit=2, char_limit=32, chunk_limit=2)
return split_reply_chunks(text, sentence_limit=2, char_limit=32, chunk_limit=2, allow_clip_split=False)
if reply_mode == "qa_with_context":
return split_reply_chunks(text, sentence_limit=2, char_limit=40, chunk_limit=2)
return split_reply_chunks(text, sentence_limit=2, char_limit=40, chunk_limit=2, allow_clip_split=False)
return [take_first_sentence(text, 28).strip()]
@@ -45,14 +45,22 @@ def take_first_sentence(text: str, limit: int) -> str:
return smart_clip(first, limit)
def split_reply_chunks(text: str, sentence_limit: int, char_limit: int, chunk_limit: int) -> List[str]:
def split_reply_chunks(
text: str,
sentence_limit: int,
char_limit: int,
chunk_limit: int,
allow_clip_split: bool = True,
) -> List[str]:
parts = [item.strip() for item in re.split(r"(?<=[。!?!?;])", text) if item.strip()]
if not parts:
short = text.strip()
clipped = smart_clip(short, char_limit)
remainder = short[len(clipped):].strip(",、;;: ")
if not short:
return []
if not allow_clip_split:
return [clipped] if clipped else []
remainder = short[len(clipped):].strip(",、;;: ")
return [item for item in [clipped, smart_clip(remainder, char_limit)] if item][:chunk_limit]
chunks: List[str] = []
@@ -67,6 +75,8 @@ def split_reply_chunks(text: str, sentence_limit: int, char_limit: int, chunk_li
clipped = current[:char_limit].rstrip(",、;;: ").strip()
if clipped:
chunks.append(clipped)
if not allow_clip_split:
break
current = current[len(clipped):].strip(",、;;: ")
return chunks[:chunk_limit] or [smart_clip(text, char_limit)]