ForcePilot/backend/package/yuxi/channel/extensions/googlechat/session.py
Kris c4e301a5e0 feat(googlechat): 新增Google Chat渠道完整实现
新增Google Chat插件的所有核心功能模块,包括账户配置、消息收发、权限控制、媒体处理、会话管理等完整能力
2026-05-21 10:49:33 +08:00

48 lines
1.6 KiB
Python

from __future__ import annotations
from yuxi.channel.protocols import SessionResolution
class GoogleChatSessionAdapter:
def resolve_session(self, msg) -> SessionResolution:
from yuxi.channel.routing.models import PeerKind
if hasattr(msg, "sender") and hasattr(msg.sender, "kind"):
if msg.sender.kind == PeerKind.DIRECT:
space_id = _extract_space_id(msg)
return SessionResolution(
kind="direct",
conversation_id=space_id or msg.sender.id,
)
space_id = _extract_space_id(msg)
if space_id:
return SessionResolution(kind="group", conversation_id=space_id)
if msg.group and msg.group.id:
return SessionResolution(kind="group", conversation_id=msg.group.id)
return SessionResolution(kind="group", conversation_id="unknown")
@staticmethod
def build_dm_session_key(agent_id: str, account_id: str, space_id: str) -> str:
return f"agent:{agent_id}:googlechat:direct:{space_id}"
@staticmethod
def build_group_session_key(agent_id: str, account_id: str, space_id: str) -> str:
return f"agent:{agent_id}:googlechat:group:{space_id}"
def _extract_space_id(msg) -> str | None:
metadata = getattr(msg, "metadata", {}) or {}
space_name = metadata.get("space_name", "")
if space_name:
return space_name
raw = getattr(msg, "raw_payload", {}) or {}
event = raw.get("event", raw)
space = event.get("space", {})
if space:
return space.get("name")
return None