新增 RocketChat 渠道扩展,支持在 Yuxi 平台中集成 RocketChat 团队协作平台。 包含以下功能模块: - client: RocketChat API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - websocket: WebSocket 实时连接 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedup: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - gating: 门控管理 - threading: 线程管理 - reactions: 表情反应 - types: 类型定义
78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
ROCKETCHAT_TEXT_LIMIT = 4000
|
|
|
|
|
|
def markdown_to_rocketchat(md_text: str) -> str:
|
|
return md_text.strip()
|
|
|
|
|
|
def strip_markdown_to_plain(md_text: str, max_chars: int = ROCKETCHAT_TEXT_LIMIT) -> str:
|
|
text = md_text.strip()
|
|
if len(text) > max_chars:
|
|
text = text[: max_chars - 3] + "..."
|
|
return text
|
|
|
|
|
|
def safe_split_markdown(text: str, chunk_limit: int = ROCKETCHAT_TEXT_LIMIT) -> list[str]:
|
|
if len(text) <= chunk_limit:
|
|
return [text]
|
|
|
|
chunks: list[str] = []
|
|
remaining = text
|
|
while len(remaining) > chunk_limit:
|
|
split_at = _find_paragraph_break(remaining, chunk_limit)
|
|
chunks.append(remaining[:split_at].strip())
|
|
remaining = remaining[split_at:].strip()
|
|
if remaining:
|
|
chunks.append(remaining)
|
|
return chunks
|
|
|
|
|
|
def _find_paragraph_break(text: str, limit: int) -> int:
|
|
search_text = text[:limit]
|
|
for sep in ["\n\n", "\n"]:
|
|
idx = search_text.rfind(sep)
|
|
if idx > limit * 0.3:
|
|
return idx + len(sep)
|
|
|
|
last_space = search_text.rfind(" ")
|
|
if last_space > limit * 0.7:
|
|
return last_space + 1
|
|
|
|
return limit
|
|
|
|
|
|
def truncate_markdown(text: str, max_chars: int = ROCKETCHAT_TEXT_LIMIT) -> str:
|
|
if len(text) <= max_chars:
|
|
return text
|
|
return text[: max_chars - 3] + "..."
|
|
|
|
|
|
def chunk_text(text: str, chunk_limit: int = ROCKETCHAT_TEXT_LIMIT) -> list[str]:
|
|
return safe_split_markdown(text, chunk_limit)
|
|
|
|
|
|
def normalize_message(text: str, bot_username: str | None = None) -> str:
|
|
result = text.strip()
|
|
if bot_username:
|
|
patterns = [f"@{bot_username}", f"@{bot_username.lower()}"]
|
|
for pattern in patterns:
|
|
result = _strip_mention(result, pattern)
|
|
return result
|
|
|
|
|
|
def _strip_mention(text: str, mention: str) -> str:
|
|
result = text.replace(mention, "").strip()
|
|
for part in mention.split():
|
|
result = result.replace(part, "").strip()
|
|
return result.strip()
|
|
|
|
|
|
def extract_onchar_content(text: str, prefixes: list[str]) -> str | None:
|
|
stripped = text.strip()
|
|
for prefix in prefixes:
|
|
if stripped.startswith(prefix):
|
|
return stripped[len(prefix) :].strip()
|
|
return None
|