ForcePilot/backend/package/yuxi/channels/adapters/nextcloudtalk/chunking.py
Kris b018ad25da feat(backend): add Nextcloud Talk channel adapter implementation
实现了完整的Nextcloud Talk聊天适配器,包含以下核心功能:
1. 基础客户端通信与认证
2. 会话路由与聊天类型解析
3. 消息分块与格式转换
4. 用户配对与权限校验
5. 轮询式消息监听
6. 配置验证与诊断工具
7. 安全策略与去重缓存
8. 指标统计与状态快照

新增配套的配对用户缓存文件与模块导出入口。
2026-05-12 00:47:06 +08:00

61 lines
1.6 KiB
Python

from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
def chunk_text(text: str, limit: int = 4000, mode: str = "markdown") -> list[str]:
if len(text) <= limit:
return [text]
if mode == "markdown":
return _chunk_markdown(text, limit)
elif mode == "newline":
return _chunk_by_newline(text, limit)
return _chunk_by_length(text, limit)
def _chunk_markdown(text: str, limit: int) -> list[str]:
chunks: list[str] = []
remainder = text
while len(remainder) > limit:
cut = remainder.rfind("\n\n", 0, limit)
if cut < limit // 2:
cut = remainder.rfind("\n", 0, limit)
if cut < limit // 4:
cut = remainder.rfind(". ", 0, limit)
if cut < 0:
cut = limit
chunk = remainder[: cut + 1].rstrip()
chunks.append(chunk)
remainder = remainder[cut + 1 :].lstrip()
if remainder.strip():
chunks.append(remainder.strip())
return chunks
def _chunk_by_newline(text: str, limit: int) -> list[str]:
chunks: list[str] = []
current = ""
for line in text.split("\n"):
if len(current) + len(line) + 1 > limit and current:
chunks.append(current.rstrip())
current = line
else:
current = f"{current}\n{line}".strip()
if current:
chunks.append(current)
return chunks
def _chunk_by_length(text: str, limit: int) -> list[str]:
chunks: list[str] = []
for i in range(0, len(text), limit):
chunks.append(text[i : i + limit])
return chunks