ForcePilot/backend/package/yuxi/channel/extensions/kakaotalk/format.py
Kris 8b66218d9b feat(channel): 添加 KakaoTalk 渠道扩展
新增 KakaoTalk 渠道扩展,支持在 Yuxi 平台中集成 KakaoTalk 即时通讯渠道。

包含以下功能模块:
- bot: Bot 客户端封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- card_builder: KakaoTalk 卡片消息构建
- quick_reply: 快捷回复处理
- types: 类型定义
2026-05-21 11:12:31 +08:00

38 lines
1.3 KiB
Python

from __future__ import annotations
import re
def chunk_text(text: str, limit: int = 1000) -> list[str]:
if len(text) <= limit:
return [text]
chunks = []
remaining = text
while remaining:
if len(remaining) <= limit:
chunks.append(remaining)
break
split_at = remaining.rfind("\n", 0, limit + 1)
if split_at == -1 or split_at < limit // 2:
split_at = remaining.rfind(". ", 0, limit + 1)
if split_at == -1 or split_at < limit // 2:
split_at = remaining.rfind(" ", 0, limit + 1)
if split_at == -1 or split_at < limit // 2:
split_at = limit
chunks.append(remaining[:split_at].strip())
remaining = remaining[split_at:].strip()
return chunks
def strip_markdown(text: str) -> str:
text = re.sub(r"\*\*(.+?)\*\*", r"\1", text)
text = re.sub(r"\*(.+?)\*", r"\1", text)
text = re.sub(r"`(.+?)`", r"\1", text)
text = re.sub(r"\[(.+?)\]\((.+?)\)", r"\1 (\2)", text)
text = re.sub(r"^#{1,6}\s+", "", text, flags=re.MULTILINE)
text = re.sub(r"^>\s+", "", text, flags=re.MULTILINE)
text = re.sub(r"^[\-\*\+]\s+", "", text, flags=re.MULTILINE)
text = re.sub(r"^(\d+)\.\s+", r"\1. ", text, flags=re.MULTILINE)
text = re.sub(r"~~(.+?)~~", r"\1", text)
return text.strip()