ForcePilot/backend/package/yuxi/channels/adapters/zalo_oa/chunking.py
Kris 30dd9e16f4 feat(zalo-oa): 实现完整的 Zalo OA 渠道适配器模块
新增 Zalo OA 官方账号完整集成能力,包含:
1. 基础通信能力:消息编解码、目标归一化、文本分块
2. 安全与校验:Webhook 签名验证、DM 策略管理、配对流程
3. 辅助工具:重复事件去重、请求限流、异常告警
4. 管理功能:账号多实例管理、配置验证、健康诊断
5. 扩展能力:媒体托管、视觉识别、TTS 语音合成
6. 运维支持:审计日志、状态监控、目录同步
2026-05-12 00:52:47 +08:00

53 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
import re
_PARAGRAPH_BOUNDARY = re.compile(r"\n\s*\n")
_SENTENCE_BOUNDARY = re.compile(r"(?<=[.!?。!?])\s+")
_CHUNK_BOUNDARY = re.compile(r"[\n,;]+")
def chunk_text(text: str, max_chars: int) -> list[str]:
if len(text) <= max_chars:
return [text]
chunks: list[str] = []
remaining = text
while len(remaining) > max_chars:
split_point = _find_split_point(remaining, max_chars)
chunk = remaining[:split_point].strip()
remaining = remaining[split_point:].strip()
if chunk:
chunks.append(chunk)
if remaining:
chunks.append(remaining)
if not chunks:
chunks.append(text[:max_chars])
return chunks
def _find_split_point(text: str, max_chars: int) -> int:
head = text[:max_chars]
para_matches = list(_PARAGRAPH_BOUNDARY.finditer(head))
if para_matches:
return para_matches[-1].start()
sent_matches = list(_SENTENCE_BOUNDARY.finditer(head))
if sent_matches:
return sent_matches[-1].end()
chunk_matches = list(_CHUNK_BOUNDARY.finditer(head))
if chunk_matches:
return chunk_matches[-1].start()
space_match = head.rfind(" ")
if space_match > max_chars // 2:
return space_match
return max_chars