S73 聊天快捷角色草稿
这个提交存在于:
父节点
7054de3b45
当前提交
d0daedfeb8
共有 4 个文件被更改,包括 112 次插入 和 4 次删除
|
|
@ -3,6 +3,8 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
WORKBENCH_ALIASES = frozenset(
|
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:
|
def normalize_command_text(text: str) -> str:
|
||||||
normalized = str(text or "").replace("/", "/").strip().lower()
|
normalized = str(text or "").replace("/", "/").strip().lower()
|
||||||
|
|
@ -31,3 +54,36 @@ def command_key(text: str) -> str:
|
||||||
|
|
||||||
def is_workbench_command(text: str) -> bool:
|
def is_workbench_command(text: str) -> bool:
|
||||||
return command_key(text) in WORKBENCH_ALIASES
|
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
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ from __future__ import annotations
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any
|
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
|
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:
|
def handle_message_event(event: dict[str, Any], *, api_base: str = API_BASE) -> dict[str, Any] | None:
|
||||||
message = normalize_message_event(event)
|
message = normalize_message_event(event)
|
||||||
if not match_workbench_command(message):
|
command = parse_chat_command(message.text)
|
||||||
|
if command is None:
|
||||||
return 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 {
|
return {
|
||||||
"platform": "onebot11",
|
"platform": "onebot11",
|
||||||
"action": "open_workbench",
|
"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:
|
def _message_text(value: Any) -> str:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
return value.strip()
|
return value.strip()
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ from __future__ import annotations
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any
|
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
|
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:
|
def handle_message_event(event: dict[str, Any], *, api_base: str = API_BASE) -> dict[str, Any] | None:
|
||||||
message = normalize_message_event(event)
|
message = normalize_message_event(event)
|
||||||
if not match_workbench_command(message):
|
command = parse_chat_command(message.text)
|
||||||
|
if command is None:
|
||||||
return 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 {
|
return {
|
||||||
"platform": "wechat",
|
"platform": "wechat",
|
||||||
"action": "open_workbench",
|
"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:
|
def _optional_text(value: Any) -> str | None:
|
||||||
text = str(value or "").strip()
|
text = str(value or "").strip()
|
||||||
return text or None
|
return text or None
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,16 @@ class OneBotAdapterTests(unittest.TestCase):
|
||||||
def test_ignores_non_commands(self):
|
def test_ignores_non_commands(self):
|
||||||
self.assertIsNone(handle_message_event({"user_id": "u1", "raw_message": "hello"}))
|
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):
|
class WeChatAdapterTests(unittest.TestCase):
|
||||||
def test_normalizes_wechat_event_aliases(self):
|
def test_normalizes_wechat_event_aliases(self):
|
||||||
|
|
@ -76,6 +86,16 @@ class WeChatAdapterTests(unittest.TestCase):
|
||||||
def test_ignores_non_commands(self):
|
def test_ignores_non_commands(self):
|
||||||
self.assertIsNone(handle_wechat_message_event({"from_user": "wx_user", "text": "hello"}))
|
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):
|
class AdapterExportTests(unittest.TestCase):
|
||||||
def test_chat_adapter_entrypoints_are_exported(self):
|
def test_chat_adapter_entrypoints_are_exported(self):
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用