新增 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: 类型定义
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from yuxi.channel.extensions.mattermost.format import (
|
|
safe_split_markdown,
|
|
truncate_markdown,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class MattermostDeliveryAdapter:
|
|
def __init__(self, chunk_limit: int = 4000, chunk_mode: str = "length"):
|
|
self.chunk_limit = chunk_limit
|
|
self.chunk_mode = chunk_mode
|
|
|
|
def split_for_delivery(self, content: str) -> list[str]:
|
|
if len(content) <= self.chunk_limit:
|
|
return [content]
|
|
|
|
if self.chunk_mode == "newline":
|
|
return self._split_by_newline(content)
|
|
|
|
return safe_split_markdown(content, self.chunk_limit)
|
|
|
|
def _split_by_newline(self, content: str) -> list[str]:
|
|
chunks: list[str] = []
|
|
lines = content.split("\n")
|
|
current = ""
|
|
for line in lines:
|
|
if len(current) + len(line) + 1 > self.chunk_limit:
|
|
if current:
|
|
chunks.append(current.rstrip())
|
|
current = line
|
|
else:
|
|
if current:
|
|
current += "\n"
|
|
current += line
|
|
if current:
|
|
chunks.append(current)
|
|
return chunks
|
|
|
|
def format_response(self, content: str, prefix: str | None = None) -> str:
|
|
if prefix:
|
|
content = f"{prefix}\n\n{content}"
|
|
return truncate_markdown(content, self.chunk_limit)
|