ForcePilot/backend/package/yuxi/channel/extensions/mattermost/gating.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

73 lines
2.3 KiB
Python

from __future__ import annotations
import logging
import re
logger = logging.getLogger(__name__)
class MattermostGating:
def __init__(self, account: dict):
self.chatmode = account.get("chatmode", "oncall")
self.onchar_prefixes = account.get("onchar_prefixes", [">", "!"])
self.require_mention = account.get("require_mention", True)
def should_respond(
self,
chat_type: str,
message_text: str,
was_mentioned: bool,
) -> tuple[bool, str]:
if chat_type == "direct":
return True, "dm_always"
if self.chatmode == "onmessage":
return True, "onmessage"
if self.chatmode == "onchar":
return self._check_onchar(message_text)
if self.chatmode == "oncall":
if was_mentioned:
return True, "mentioned"
return self._check_onchar_fallback(message_text)
return False, "unknown_chatmode"
def _check_onchar(self, text: str) -> tuple[bool, str]:
stripped = text.strip()
for prefix in self.onchar_prefixes:
if stripped.startswith(prefix):
return True, "onchar_triggered"
return False, "onchar_not_triggered"
def _check_onchar_fallback(self, text: str) -> tuple[bool, str]:
if not self.require_mention:
return True, "mention_not_required"
for prefix in self.onchar_prefixes:
if text.strip().startswith(prefix):
return True, "onchar_fallback"
return False, "missing_mention"
def was_mentioned(self, text: str, bot_user_id: str, bot_username: str | None = None) -> bool:
mentions = self.extract_mentions(text)
if bot_user_id in mentions:
return True
if bot_username:
return bot_username in text or f"@{bot_username}" in text
return False
def extract_mentions(self, text: str) -> list[str]:
user_ids = re.findall(r"@([a-z0-9]{26})", text, re.IGNORECASE)
usernames = re.findall(r"@(\w+)", text)
return user_ids + usernames
def strip_onchar_prefix(self, text: str) -> str:
stripped = text.strip()
for prefix in self.onchar_prefixes:
if stripped.startswith(prefix):
return stripped[len(prefix):].strip()
return text