"""Pure helpers for the AstrBot chat command surface.""" from __future__ import annotations from dataclasses import dataclass from typing import Any from core.chat_access_policy import chat_action_allowed @dataclass(frozen=True) class ChatCommandContext: user_id: str group_id: str | None message_origin: str platform: str message_id: str def command_context_from_event(event: Any) -> ChatCommandContext: return ChatCommandContext( user_id=_event_value(event, "get_sender_id", "sender_id", "user_id"), group_id=_optional_event_value(event, "get_group_id", "group_id"), message_origin=_event_value(event, "unified_msg_origin", "message_origin"), platform=_event_value(event, "get_platform_name", "platform_name", fallback="unknown"), message_id=_event_value(event, "message_id", "get_message_id"), ) def command_allowed(config: dict[str, Any], action: str, context: ChatCommandContext) -> bool: return chat_action_allowed( config, action, group_id=context.group_id, user_id=context.user_id, ) def message_text_from_event(event: Any) -> str: for name in ("message_str", "get_message_str", "raw_message"): value = getattr(event, name, None) if callable(value): try: value = value() except (TypeError, AttributeError): continue text = str(value or "").strip() if text: return text return "" def help_text() -> str: return """🎨 ImageStudio 命令 /影棚 帮助 或 /yp 帮助 /影棚 状态 或 /yp 状态 /影棚 快速角色 <角色描述> /影棚 工作流 <角色名或角色 ID> /影棚 单图 <提示词> /影棚 轮次 [4|5] [提示词] /影棚 候选 /影棚 反馈 <自然语言反馈> /影棚 下一轮 [4|5] /影棚 定稿 <候选编号> /影棚 审查 <候选编号> [审核|跳过] /影棚 素材 [material_id,...] /影棚 分支 /影棚 档案卡 [人设文档] 权限规则: • 快速角色、角色工作流:群聊和用户白名单必须同时命中 • 单图:群聊或用户任一白名单命中即可 详细工作流使用稳定候选编号;只有“定稿/审查通过”会正式采用并推进节点。""" def access_denied_text(action: str, context: ChatCommandContext) -> str: scope = f"群聊 {context.group_id}" if context.group_id else "私聊" return f"⛔ 当前{scope}或用户 {context.user_id or '未知'}不满足「{action}」的白名单规则。" def accepted_text(action: str, argument: str, context: ChatCommandContext) -> str: value = argument.strip() or "未提供" return ( f"✅ ImageStudio 已接收「{action}」命令框架请求\n\n" f"输入:{value}\n" f"平台:{context.platform}\n" f"用户:{context.user_id or '未知'}\n" f"群聊:{context.group_id or '私聊'}\n\n" "当前阶段已完成命令注册、参数接收与白名单校验;业务执行器将在下一阶段接入。" ) def _event_value(event: Any, *names: str, fallback: str = "") -> str: for name in names: value = getattr(event, name, None) if callable(value): try: value = value() except (TypeError, AttributeError): continue text = str(value or "").strip() if text: return text return fallback def _optional_event_value(event: Any, *names: str) -> str | None: value = _event_value(event, *names) return value or None