ForcePilot/backend/package/yuxi/channels/adapters/urbit/session.py
Kris 1f78c44b03 refactor: 整理并清理项目中的冗余代码与格式问题
这是一个批量整理提交,包含以下主要改动:
1.  删除多处冗余的空行和未使用的导入
2.  修复文件末尾缺少换行符的问题
3.  调整部分模块的导入顺序与代码排版
4.  修复部分配置默认值与策略逻辑
5.  新增多个功能模块与辅助工具
6.  完善异常处理与日志记录
7.  修复速率限制、消息缓存、权限校验等逻辑bug
8.  废弃部分旧有API与配置项并添加警告提示
2026-05-12 14:51:53 +08:00

79 lines
2.3 KiB
Python

from __future__ import annotations
import time
from collections import defaultdict
from yuxi.channels.models import ChannelMessage
from yuxi.utils.logging_config import logger
def resolve_thread_key(msg: ChannelMessage, default_agent_id: str = "ChatbotAgent") -> str:
chat_type = msg.metadata.get("chat_type", "group")
chat_id = msg.identity.channel_chat_id
if chat_type == "direct":
return f"agent:{default_agent_id}:urbit:direct:{chat_id}"
group_name = msg.metadata.get("group_name", chat_id)
ch_type = msg.metadata.get("urbit_resource_type", "chat")
return f"agent:{default_agent_id}:urbit:group:{group_name}:{ch_type}"
def resolve_session_route(msg: ChannelMessage, default_agent_id: str = "ChatbotAgent") -> str:
return resolve_thread_key(msg, default_agent_id)
_unsafe_sessions: defaultdict[str, set[str]] = defaultdict(set)
_session_last_access: dict[str, float] = {}
_SESSION_TTL_S = 3600
def detect_unsafe_session(msg: ChannelMessage) -> list[str]:
chat_type = msg.metadata.get("chat_type", "group")
if chat_type != "direct":
return []
chat_id = msg.identity.channel_chat_id
participant_key = f"dm:{chat_id}"
sender_ship = msg.metadata.get("urbit_ship", "")
if not sender_ship:
return []
_unsafe_sessions[participant_key].add(sender_ship)
_session_last_access[participant_key] = time.monotonic()
participants = _unsafe_sessions[participant_key]
if len(participants) > 2:
logger.warning(
f"[Urbit] Unsafe DM session detected: "
f"{participant_key} has {len(participants)} "
f"participants: {participants}"
)
return list(participants)
return []
def get_unsafe_sessions() -> dict[str, set[str]]:
return dict(_unsafe_sessions)
def clear_unsafe_sessions() -> None:
_unsafe_sessions.clear()
_session_last_access.clear()
def cleanup_expired_sessions() -> int:
now = time.monotonic()
expired = [
key for key, ts in _session_last_access.items()
if now - ts > _SESSION_TTL_S
]
for key in expired:
_unsafe_sessions.pop(key, None)
_session_last_access.pop(key, None)
if expired:
logger.debug(f"[Urbit] Cleaned up {len(expired)} expired unsafe session records")
return len(expired)