ForcePilot/backend/package/yuxi/channel/extensions/mattermost/session.py

60 lines
1.5 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
class MattermostSessionManager:
def build_session_key(
self,
account_id: str,
channel_type: str,
channel_id: str,
root_id: str | None = None,
) -> str:
parts = [account_id, channel_type, channel_id]
if root_id:
parts.append(f"thread:{root_id}")
return ":".join(parts)
def build_thread_session_key(
self,
account_id: str,
channel_id: str,
thread_root_id: str,
) -> str:
return f"{account_id}:thread:{channel_id}:thread:{thread_root_id}"
def extract_from_session_key(self, session_key: str) -> dict:
parts = session_key.split(":")
if len(parts) < 3:
return {"account_id": "", "channel_type": "", "channel_id": ""}
result = {
"account_id": parts[0],
"channel_type": parts[1],
"channel_id": parts[2],
"thread_id": None,
}
if len(parts) >= 5 and parts[3] == "thread":
result["thread_id"] = parts[4]
return result
def resolve_parent_session_key(
self,
session_key: str,
chat_type: str,
) -> str | None:
if chat_type == "direct":
return None
parts = session_key.split(":")
if len(parts) >= 5:
non_thread = ":".join(parts[:3])
return non_thread
return None