47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
|
|
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}"
|