From 7054de3b45b448a959f54924d357a06bc4e6e8ed Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Sun, 5 Jul 2026 22:48:40 +0800 Subject: [PATCH] =?UTF-8?q?S72=20=E5=B9=B3=E5=8F=B0=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=88=AB=E5=90=8D=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapters/chat_commands.py | 33 +++++++++++++++++++++++++++++++++ adapters/onebot11_adapter.py | 7 ++----- adapters/wechat_adapter.py | 6 ++---- tests/test_chat_adapters.py | 14 ++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 adapters/chat_commands.py diff --git a/adapters/chat_commands.py b/adapters/chat_commands.py new file mode 100644 index 0000000..021bec5 --- /dev/null +++ b/adapters/chat_commands.py @@ -0,0 +1,33 @@ +"""Shared chat command parsing helpers.""" + +from __future__ import annotations + +import re + + +WORKBENCH_ALIASES = frozenset( + { + "imagestudio", + "image studio", + "is", + "图像工作台", + "打开图像工作台", + "工作台", + "打开工作台", + "生图工作台", + "打开生图工作台", + } +) + + +def normalize_command_text(text: str) -> str: + normalized = str(text or "").replace("/", "/").strip().lower() + return re.sub(r"\s+", " ", normalized) + + +def command_key(text: str) -> str: + return normalize_command_text(text).lstrip("/") + + +def is_workbench_command(text: str) -> bool: + return command_key(text) in WORKBENCH_ALIASES diff --git a/adapters/onebot11_adapter.py b/adapters/onebot11_adapter.py index 58b2182..e5fdfe6 100644 --- a/adapters/onebot11_adapter.py +++ b/adapters/onebot11_adapter.py @@ -5,12 +5,10 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any +from adapters.chat_commands import is_workbench_command from backend.services.ui_service import API_BASE -WORKBENCH_COMMANDS = {"imagestudio", "/imagestudio", "图像工作台", "打开图像工作台"} - - @dataclass(frozen=True) class OneBotMessage: user_id: str @@ -40,8 +38,7 @@ def normalize_message_event(event: dict[str, Any]) -> OneBotMessage: def match_workbench_command(message: OneBotMessage) -> bool: - text = message.text.strip().lower() - return text in WORKBENCH_COMMANDS + return is_workbench_command(message.text) def workbench_reply(api_base: str = API_BASE) -> dict[str, Any]: diff --git a/adapters/wechat_adapter.py b/adapters/wechat_adapter.py index c41b741..6233f11 100644 --- a/adapters/wechat_adapter.py +++ b/adapters/wechat_adapter.py @@ -5,12 +5,10 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any +from adapters.chat_commands import is_workbench_command from backend.services.ui_service import API_BASE -WORKBENCH_COMMANDS = {"imagestudio", "/imagestudio", "图像工作台", "打开图像工作台"} - - @dataclass(frozen=True) class WeChatMessage: user_id: str @@ -36,7 +34,7 @@ def normalize_message_event(event: dict[str, Any]) -> WeChatMessage: def match_workbench_command(message: WeChatMessage) -> bool: - return message.text.strip().lower() in WORKBENCH_COMMANDS + return is_workbench_command(message.text) def workbench_reply(api_base: str = API_BASE) -> dict[str, Any]: diff --git a/tests/test_chat_adapters.py b/tests/test_chat_adapters.py index f584e8a..6ae51ce 100644 --- a/tests/test_chat_adapters.py +++ b/tests/test_chat_adapters.py @@ -34,6 +34,13 @@ class OneBotAdapterTests(unittest.TestCase): self.assertEqual(result["action"], "open_workbench") self.assertEqual(result["reply"]["url"], "/api/plugin/web") + def test_workbench_aliases_are_shared(self): + for command in ["/is", "image studio", "工作台", "生图工作台"]: + with self.subTest(command=command): + result = handle_message_event({"user_id": "u1", "raw_message": command}, api_base="/api/plugin") + self.assertIsNotNone(result) + self.assertEqual(result["action"], "open_workbench") + def test_ignores_non_commands(self): self.assertIsNone(handle_message_event({"user_id": "u1", "raw_message": "hello"})) @@ -59,6 +66,13 @@ class WeChatAdapterTests(unittest.TestCase): self.assertEqual(result["action"], "open_workbench") self.assertEqual(result["reply"]["url"], "/api/plugin/web") + def test_workbench_aliases_are_shared(self): + for command in ["/is", "image studio", "工作台", "生图工作台"]: + with self.subTest(command=command): + result = handle_wechat_message_event({"from_user": "wx_user", "text": command}, api_base="/api/plugin") + self.assertIsNotNone(result) + self.assertEqual(result["action"], "open_workbench") + def test_ignores_non_commands(self): self.assertIsNone(handle_wechat_message_event({"from_user": "wx_user", "text": "hello"}))