ForcePilot/backend/package/yuxi/channel/extensions/mattermost/format.py
Kris ebab14660a feat(channel): 添加 Mattermost 渠道扩展
新增 Mattermost 渠道扩展,支持在 Yuxi 平台中集成 Mattermost 团队协作平台。

包含以下功能模块:
- client: Mattermost API 客户端封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- websocket: WebSocket 实时连接
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- dedup: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- interactions: 交互处理
- slash_commands: 斜杠指令
- actions: 动作处理
- approval: 审批流程
- delivery: 消息送达确认
- directory: 目录管理
- threading: 线程管理
- gating: 门控管理
- reconnect: 重连机制
- reactions: 表情反应
- media: 媒体资源处理
- model_picker: 模型选择
- types: 类型定义
2026-05-21 11:22:43 +08:00

80 lines
2.2 KiB
Python

from __future__ import annotations
MATTERMOST_TEXT_LIMIT = 4000
def markdown_to_mattermost(md_text: str) -> str:
return md_text.strip()
def strip_markdown_to_plain(md_text: str, max_chars: int = MATTERMOST_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 = MATTERMOST_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 = MATTERMOST_TEXT_LIMIT) -> str:
if len(text) <= max_chars:
return text
truncated = text[: max_chars - 3] + "..."
return truncated
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
def sanitize_action_id(action_id: str) -> str:
return action_id.replace("-", "").replace("_", "")