From d0daedfeb872d7a36c44bc100b708ea9b4852732 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Sun, 5 Jul 2026 22:52:31 +0800 Subject: [PATCH] =?UTF-8?q?S73=20=E8=81=8A=E5=A4=A9=E5=BF=AB=E6=8D=B7?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E8=8D=89=E7=A8=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapters/chat_commands.py | 56 ++++++++++++++++++++++++++++++++++++ adapters/onebot11_adapter.py | 20 +++++++++++-- adapters/wechat_adapter.py | 20 +++++++++++-- tests/test_chat_adapters.py | 20 +++++++++++++ 4 files changed, 112 insertions(+), 4 deletions(-) diff --git a/adapters/chat_commands.py b/adapters/chat_commands.py index 021bec5..9aa0622 100644 --- a/adapters/chat_commands.py +++ b/adapters/chat_commands.py @@ -3,6 +3,8 @@ from __future__ import annotations import re +from dataclasses import dataclass +from typing import Any WORKBENCH_ALIASES = frozenset( @@ -19,6 +21,27 @@ WORKBENCH_ALIASES = frozenset( } ) +PROFILE_COMMAND_PREFIXES = ( + "profile", + "quick profile", + "imagestudio profile", + "image studio profile", + "is profile", + "创建角色", + "新建角色", + "创建人设", + "新建人设", + "imagestudio 创建角色", + "image studio 创建角色", + "is 创建角色", +) + + +@dataclass(frozen=True) +class ChatCommandAction: + action: str + payload: dict[str, Any] + def normalize_command_text(text: str) -> str: normalized = str(text or "").replace("/", "/").strip().lower() @@ -31,3 +54,36 @@ def command_key(text: str) -> str: def is_workbench_command(text: str) -> bool: return command_key(text) in WORKBENCH_ALIASES + + +def parse_chat_command(text: str) -> ChatCommandAction | None: + if is_workbench_command(text): + return ChatCommandAction("open_workbench", {}) + + profile_name = parse_quick_profile_name(text) + if profile_name is None: + return None + return ChatCommandAction( + "quick_profile", + { + "action_type": "quick_profile", + "draft_only": True, + "questionnaire": {"name": profile_name}, + }, + ) + + +def parse_quick_profile_name(text: str) -> str | None: + raw = re.sub(r"\s+", " ", str(text or "").replace("/", "/").strip()).lstrip("/") + normalized = raw.lower() + for prefix in sorted(PROFILE_COMMAND_PREFIXES, key=len, reverse=True): + if normalized == prefix: + return None + if normalized.startswith(f"{prefix} "): + return _clean_profile_name(raw[len(prefix) + 1 :]) + return None + + +def _clean_profile_name(text: str) -> str | None: + name = text.strip(" \t\r\n::") + return name or None diff --git a/adapters/onebot11_adapter.py b/adapters/onebot11_adapter.py index e5fdfe6..2bbedb6 100644 --- a/adapters/onebot11_adapter.py +++ b/adapters/onebot11_adapter.py @@ -5,7 +5,7 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any -from adapters.chat_commands import is_workbench_command +from adapters.chat_commands import is_workbench_command, parse_chat_command from backend.services.ui_service import API_BASE @@ -52,8 +52,17 @@ def workbench_reply(api_base: str = API_BASE) -> dict[str, Any]: def handle_message_event(event: dict[str, Any], *, api_base: str = API_BASE) -> dict[str, Any] | None: message = normalize_message_event(event) - if not match_workbench_command(message): + command = parse_chat_command(message.text) + if command is None: return None + if command.action == "quick_profile": + return { + "platform": "onebot11", + "action": command.action, + "payload": command.payload, + "source": message.to_dict(), + "reply": quick_profile_reply(command.payload["questionnaire"]["name"]), + } return { "platform": "onebot11", "action": "open_workbench", @@ -62,6 +71,13 @@ def handle_message_event(event: dict[str, Any], *, api_base: str = API_BASE) -> } +def quick_profile_reply(profile_name: str) -> dict[str, Any]: + return { + "reply_type": "text", + "message": f"已准备创建角色草稿:{profile_name}", + } + + def _message_text(value: Any) -> str: if isinstance(value, str): return value.strip() diff --git a/adapters/wechat_adapter.py b/adapters/wechat_adapter.py index 6233f11..1eacd23 100644 --- a/adapters/wechat_adapter.py +++ b/adapters/wechat_adapter.py @@ -5,7 +5,7 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any -from adapters.chat_commands import is_workbench_command +from adapters.chat_commands import is_workbench_command, parse_chat_command from backend.services.ui_service import API_BASE @@ -48,8 +48,17 @@ def workbench_reply(api_base: str = API_BASE) -> dict[str, Any]: def handle_message_event(event: dict[str, Any], *, api_base: str = API_BASE) -> dict[str, Any] | None: message = normalize_message_event(event) - if not match_workbench_command(message): + command = parse_chat_command(message.text) + if command is None: return None + if command.action == "quick_profile": + return { + "platform": "wechat", + "action": command.action, + "payload": command.payload, + "source": message.to_dict(), + "reply": quick_profile_reply(command.payload["questionnaire"]["name"]), + } return { "platform": "wechat", "action": "open_workbench", @@ -58,6 +67,13 @@ def handle_message_event(event: dict[str, Any], *, api_base: str = API_BASE) -> } +def quick_profile_reply(profile_name: str) -> dict[str, Any]: + return { + "reply_type": "text", + "text": f"已准备创建角色草稿:{profile_name}", + } + + def _optional_text(value: Any) -> str | None: text = str(value or "").strip() return text or None diff --git a/tests/test_chat_adapters.py b/tests/test_chat_adapters.py index 6ae51ce..35c5a11 100644 --- a/tests/test_chat_adapters.py +++ b/tests/test_chat_adapters.py @@ -44,6 +44,16 @@ class OneBotAdapterTests(unittest.TestCase): def test_ignores_non_commands(self): self.assertIsNone(handle_message_event({"user_id": "u1", "raw_message": "hello"})) + def test_quick_profile_command_returns_action_payload(self): + result = handle_message_event({"user_id": "u1", "raw_message": "/imagestudio profile Nova"}) + + self.assertIsNotNone(result) + self.assertEqual(result["action"], "quick_profile") + self.assertEqual(result["payload"]["action_type"], "quick_profile") + self.assertTrue(result["payload"]["draft_only"]) + self.assertEqual(result["payload"]["questionnaire"], {"name": "Nova"}) + self.assertNotIn("data", result) + class WeChatAdapterTests(unittest.TestCase): def test_normalizes_wechat_event_aliases(self): @@ -76,6 +86,16 @@ class WeChatAdapterTests(unittest.TestCase): def test_ignores_non_commands(self): self.assertIsNone(handle_wechat_message_event({"from_user": "wx_user", "text": "hello"})) + def test_quick_profile_command_returns_action_payload(self): + result = handle_wechat_message_event({"from_user": "wx_user", "text": "创建角色 Nova"}) + + self.assertIsNotNone(result) + self.assertEqual(result["action"], "quick_profile") + self.assertEqual(result["payload"]["action_type"], "quick_profile") + self.assertTrue(result["payload"]["draft_only"]) + self.assertEqual(result["payload"]["questionnaire"], {"name": "Nova"}) + self.assertNotIn("data", result) + class AdapterExportTests(unittest.TestCase): def test_chat_adapter_entrypoints_are_exported(self):