ForcePilot/backend/package/yuxi/channel/extensions/line/session.py
Kris 7fd658af57 feat(channel): 添加 LINE 渠道扩展
新增 LINE 渠道扩展,支持在 Yuxi 平台中集成 LINE 即时通讯渠道。

包含以下功能模块:
- bot: LINE Bot 客户端封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- signature: 请求签名验证
- token_manager: Token 管理
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- flex_templates: Flex 模板消息
- card_command: 卡片指令处理
- template_messages: 模板消息
- rich_menu: 富菜单管理
- actions: 动作处理
- directives: 指令处理
- delivery: 消息送达确认
- loading: 加载动画
- media: 媒体资源处理
- types: 类型定义
2026-05-21 11:16:16 +08:00

45 lines
1.4 KiB
Python

from __future__ import annotations
import re
from yuxi.channel.extensions.line.types import LineChatType
LINE_ID_PATTERN = re.compile(r"^[UCR][a-f0-9]{32}$", re.IGNORECASE)
LINE_PREFIX_PATTERN = re.compile(r"^line:", re.IGNORECASE)
class LineSessionAdapter:
def build_dm_session_key(self, agent_id: str, account_id: str, sender_id: str) -> str:
return f"agent:{agent_id}:line:direct:{sender_id}"
def build_group_session_key(self, agent_id: str, account_id: str, group_id: str) -> str:
return f"agent:{agent_id}:line:group:{group_id}"
def build_session_key(
self,
agent_id: str,
account_id: str,
chat_type: LineChatType | str,
target_id: str,
) -> str:
chat = "direct" if chat_type in (LineChatType.DIRECT, "direct") else "group"
return f"agent:{agent_id}:line:{chat}:{target_id}"
@staticmethod
def is_valid_line_id(target_id: str) -> bool:
stripped = target_id.strip()
if LINE_PREFIX_PATTERN.match(stripped):
return True
return bool(LINE_ID_PATTERN.match(stripped))
@staticmethod
def normalize_target_id(target_id: str) -> str:
stripped = target_id.strip()
if stripped.startswith("line:"):
return stripped[5:]
if stripped.startswith("group:"):
return stripped[6:]
if stripped.startswith("room:"):
return stripped[5:]
return stripped