ForcePilot/backend/package/yuxi/channels/adapters/mattermost/session.py
Kris 002d601d1b feat(mattermost): 实现完整的 Mattermost 适配器模块
新增 Mattermost 渠道完整实现,包含适配器核心、消息处理、交互回调、命令支持、安全校验、多账号管理等功能,支持机器人消息发送、交互按钮、命令注册、投票功能以及配置动态修改等特性。
2026-05-12 00:46:12 +08:00

47 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from yuxi.channels.models import ChatType
def resolve_chat_id(post: dict, channel_data: dict) -> str:
if post.get("root_id"):
return build_thread_id(post["channel_id"], post["root_id"])
ch_type = channel_data.get("type", "")
if ch_type == "D":
user_id = post.get("user_id", "") or "unknown"
return f"dm_{user_id}"
return f"channel_{post['channel_id']}"
def resolve_chat_type(post: dict, channel_data: dict) -> ChatType:
if post.get("root_id"):
return ChatType.THREAD
ch_type = channel_data.get("type", "")
if ch_type == "D":
return ChatType.DIRECT
return ChatType.GROUP
def build_thread_id(
chat_type_or_channel_id, root_id_or_user_id=None, *, user_id: str = "", channel_id: str = "", agent_id: str = "main"
) -> str:
"""构建线程 ID 用于会话隔离。
兼容两种调用方式:
1. 新式build_thread_id(channel_id, root_id) — 两个位置参数
2. 旧式build_thread_id(ChatType, user_id=..., channel_id=..., agent_id=...)
"""
if isinstance(chat_type_or_channel_id, ChatType):
chat_type = chat_type_or_channel_id
if chat_type == ChatType.DIRECT:
return f"agent:{agent_id}:mattermost:dm:{user_id}"
if chat_type in (ChatType.GROUP, ChatType.THREAD):
return f"agent:{agent_id}:mattermost:channel:{channel_id}"
return f"agent:{agent_id}:mattermost:channel:{channel_id}"
channel_id_val = chat_type_or_channel_id
root_id_val = root_id_or_user_id
if root_id_val:
return f"channel_{channel_id_val}:thread_{root_id_val}"
return f"channel_{channel_id_val}"