Skip to content

Commit

Permalink
✨ global completion-session config
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Dec 8, 2024
1 parent a0e5ec0 commit 24c00dd
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 148 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async def _():

- ALCONNA_AUTO_SEND_OUTPUT : 是否全局启用输出信息自动发送
- ALCONNA_USE_COMMAND_START : 是否将 COMMAND_START 作为全局命令前缀
- ALCONNA_AUTO_COMPLETION: 是否全局启用补全会话功能
- ALCONNA_GLOBAL_COMPLETION: 全局的补全会话配置 (不代表全局启用补全会话)
- ALCONNA_USE_ORIGIN: 是否全局使用原始消息 (即未经过 to_me 等处理的)
- ALCONNA_USE_PARAM: 是否使用特制的 Param 提供更好的依赖注入
- ALCONNA_USE_CMD_SEP: 是否将 COMMAND_SEP 作为全局命令分隔符
Expand Down
5 changes: 5 additions & 0 deletions example/.env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ HOST=127.0.0.1
PORT=9555
ALCONNA_AUTO_SEND_OUTPUT=true
ALCONNA_USE_COMMAND_START=true
ALCONNA_GLOBAL_COMPLETION='
{
"tab": "true"
}
'
NBP_ALC_PAGE_SIZE=6
LOG_LEVEL=DEBUG
COMMAND_START=["."]
Expand Down
2 changes: 1 addition & 1 deletion example/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
driver.register_adapter(SatoriAdapter)

# nonebot.require("nonebot_plugin_alconna")
nonebot.load_plugin("plugins.demo")
nonebot.load_plugins("plugins")


async def _():
Expand Down
156 changes: 78 additions & 78 deletions pdm.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = [
{name = "RF-Tar-Railt", email = "rf_tar_railt@qq.com"},
]
dependencies = [
"tarina<0.7,>=0.6.3",
"tarina<0.7,>=0.6.8",
"nepattern<1.0,>=0.7.7",
"arclet-alconna<2.0,>=1.8.34",
"arclet-alconna-tools>=0.7.10",
Expand Down
5 changes: 5 additions & 0 deletions src/nonebot_plugin_alconna/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pydantic import Field, BaseModel

from .model import CompConfig


class Config(BaseModel):
"""Plugin Config Here"""
Expand All @@ -12,6 +14,9 @@ class Config(BaseModel):
alconna_use_command_start: bool = False
"""是否将 COMMAND_START 作为全局命令前缀"""

alconna_global_completion: Optional[CompConfig] = Field(None, strict=True)
"""全局的补全会话配置 (不代表全局启用补全会话)"""

alconna_use_origin: bool = False
"""是否全局使用原始消息 (即未经过 to_me 等处理的)"""

Expand Down
3 changes: 1 addition & 2 deletions src/nonebot_plugin_alconna/i18n/.config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"default": "zh-CN",
"frozen": [
"completion.nonebot"
"nbp-alc:completion"
],
"require": [
"completion.nonebot",
"nbp-alc"
],
"name": "nonebot_plugin_alconna"
Expand Down
22 changes: 7 additions & 15 deletions src/nonebot_plugin_alconna/i18n/.lang.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"description": "Schema for lang file",
"type": "object",
"properties": {
"completion": {
"title": "Completion",
"description": "Scope 'completion' of lang item",
"nbp-alc": {
"title": "Nbp-alc",
"description": "Scope 'nbp-alc' of lang item",
"type": "object",
"additionalProperties": false,
"properties": {
"nonebot": {
"title": "Nonebot",
"description": "Scope 'nonebot' of lang item",
"completion": {
"title": "Completion",
"description": "Scope 'completion' of lang item",
"type": "object",
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -46,15 +46,7 @@
"type": "string"
}
}
}
}
},
"nbp-alc": {
"title": "Nbp-alc",
"description": "Scope 'nbp-alc' of lang item",
"type": "object",
"additionalProperties": false,
"properties": {
},
"log": {
"title": "Log",
"description": "Scope 'log' of lang item",
Expand Down
11 changes: 3 additions & 8 deletions src/nonebot_plugin_alconna/i18n/.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"$schema": "./.template.schema.json",
"scopes": [
{
"scope": "completion",
"scope": "nbp-alc",
"types": [
{
"subtype": "nonebot",
"subtype": "completion",
"types": [
"tab",
"enter",
Expand All @@ -15,12 +15,7 @@
"timeout",
"exited"
]
}
]
},
{
"scope": "nbp-alc",
"types": [
},
{
"subtype": "log",
"types": [
Expand Down
8 changes: 3 additions & 5 deletions src/nonebot_plugin_alconna/i18n/en-US.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"$schema": ".lang.schema.json",
"completion": {
"nonebot": {
"nbp-alc": {
"completion": {
"tab": "Send a {cmd} to toggle prompt",
"enter": "Send {cmd} to confirm the prompt as the input",
"exit": "Send {cmd} to exit the completion session",
"other": "Input which not in above will be confirmed as the input",
"timeout": "Completion session timed out. Exited automatically",
"exited": "Completion session exited"
}
},
"nbp-alc": {
},
"log": {
"load_global_extensions": "Loading global extension from {path}",
"got_path": {
Expand Down
20 changes: 8 additions & 12 deletions src/nonebot_plugin_alconna/i18n/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
from tarina.lang.model import LangItem, LangModel


class CompletionNonebot:
tab: LangItem = LangItem("completion", "nonebot.tab")
enter: LangItem = LangItem("completion", "nonebot.enter")
exit: LangItem = LangItem("completion", "nonebot.exit")
other: LangItem = LangItem("completion", "nonebot.other")
timeout: LangItem = LangItem("completion", "nonebot.timeout")
exited: LangItem = LangItem("completion", "nonebot.exited")


class Completion:
nonebot = CompletionNonebot
class NbpAlcCompletion:
tab: LangItem = LangItem("nbp-alc", "completion.tab")
enter: LangItem = LangItem("nbp-alc", "completion.enter")
exit: LangItem = LangItem("nbp-alc", "completion.exit")
other: LangItem = LangItem("nbp-alc", "completion.other")
timeout: LangItem = LangItem("nbp-alc", "completion.timeout")
exited: LangItem = LangItem("nbp-alc", "completion.exited")


class NbpAlcLogGotPath:
Expand Down Expand Up @@ -55,6 +51,7 @@ class NbpAlcTest:


class NbpAlc:
completion = NbpAlcCompletion
log = NbpAlcLog
error = NbpAlcError
test = NbpAlcTest
Expand Down Expand Up @@ -83,6 +80,5 @@ class NbpAlcBuiltin:


class Lang(LangModel):
completion = Completion
nbp_alc = NbpAlc
nbp_alc_builtin = NbpAlcBuiltin
8 changes: 3 additions & 5 deletions src/nonebot_plugin_alconna/i18n/zh-CN.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"$schema": ".lang.schema.json",
"completion": {
"nonebot": {
"nbp-alc": {
"completion": {
"tab": "发送 {cmd} 切换提示",
"enter": "发送 {cmd} 以使用提示内容作为输入",
"exit": "发送 {cmd} 退出补全会话",
"other": "以上选项之外的输入将作为参数输入",
"timeout": "补全会话超时, 已自动退出",
"exited": "补全会话已退出"
}
},
"nbp-alc": {
},
"log": {
"load_global_extensions": "正在从 {path} 加载全局扩展 。。。",
"got_path": {
Expand Down
4 changes: 2 additions & 2 deletions src/nonebot_plugin_alconna/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing_extensions import NotRequired
from typing import Any, Union, Generic, Literal, TypeVar, Optional, TypedDict
from typing_extensions import TypedDict, NotRequired
from typing import Any, Union, Generic, Literal, TypeVar, Optional

from pydantic import Field, BaseModel
from arclet.alconna.action import Action
Expand Down
34 changes: 16 additions & 18 deletions src/nonebot_plugin_alconna/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from arclet.alconna.exceptions import SpecialOptionTriggered
from arclet.alconna import Alconna, Arparma, CompSession, output_manager, command_manager

from .i18n import Lang
from .config import Config
from .adapters import MAPPING
from .uniseg import UniMsg, UniMessage
Expand Down Expand Up @@ -71,7 +72,7 @@ def __init__(
command: Alconna,
skip_for_unmatch: bool = True,
auto_send_output: Optional[bool] = None,
comp_config: Optional[CompConfig] = None,
comp_config: Optional[Union[CompConfig, bool]] = None,
extensions: Optional[list[Union[type[Extension], Extension]]] = None,
exclude_ext: Optional[list[Union[type[Extension], str]]] = None,
use_origin: Optional[bool] = None,
Expand All @@ -80,11 +81,16 @@ def __init__(
response_self: Optional[bool] = None,
_aliases: Optional[Union[set[str], tuple[str, ...]]] = None,
):
self.comp_config = comp_config
if isinstance(comp_config, bool):
self.comp_config = {} if comp_config else None
else:
self.comp_config = comp_config
self.use_origin = use_origin or False
try:
global_config = get_driver().config
config = get_plugin_config(Config)
if config.alconna_global_completion is not None and self.comp_config == {}:
self.comp_config = config.alconna_global_completion
if auto_send_output is None:
self.auto_send = True if config.alconna_auto_send_output is None else config.alconna_auto_send_output
else:
Expand Down Expand Up @@ -152,19 +158,11 @@ def _update(cmd_id: int):
hides = {"tab", "enter", "exit"}
hides |= disables
if len(hides) < 3:
template = f"\n\n{{}}{{}}{{}}{lang.require('completion', 'nonebot.other')}\n"
template = f"\n\n{{}}{{}}{{}}{Lang.nbp_alc.completion.other()}\n"
self._comp_help = template.format(
(f"{lang.require('completion', 'nonebot.tab').format(cmd=_tab)}\n" if "tab" not in hides else ""),
(
f"{lang.require('completion', 'nonebot.enter').format(cmd=_enter)}\n"
if "enter" not in hides
else ""
),
(
f"{lang.require('completion', 'nonebot.exit').format(cmd=_exit)}\n"
if "exit" not in hides
else ""
),
(f"{Lang.nbp_alc.completion.tab(cmd=_tab)}\n" if "tab" not in hides else ""),
(f"{Lang.nbp_alc.completion.enter(cmd=_enter)}\n" if "enter" not in hides else ""),
(f"{Lang.nbp_alc.completion.exit(cmd=_exit)}\n" if "exit" not in hides else ""),
)

async def _waiter_handle(_bot: Bot, _event: Event, _matcher: Matcher, content: UniMsg):
Expand Down Expand Up @@ -261,7 +259,7 @@ def _clear():
try:
await asyncio.wait_for(_futures[session_id], timeout=self.comp_config.get("timeout", 60))
except asyncio.TimeoutError:
await self.send(lang.require("completion", "nonebot.timeout"), bot, event, res)
await self.send(Lang.nbp_alc.completion.timeout(), bot, event, res)
_clear()
return res
finally:
Expand All @@ -270,7 +268,7 @@ def _clear():
ans: Union[UniMessage, bool, None] = _futures[session_id].result()
_futures[session_id] = asyncio.get_running_loop().create_future()
if ans is False:
await self.send(lang.require("completion", "nonebot.exited"), bot, event, res)
await self.send(Lang.nbp_alc.completion.exited(), bot, event, res)
_clear()
return res
elif ans is None:
Expand Down Expand Up @@ -326,14 +324,14 @@ async def __call__(self, event: Event, state: T_State, bot: Bot) -> bool:
if not arp.matched and not may_help_text and self.skip:
log(
"TRACE",
escape_tag(lang.require("nbp-alc", "log.parse").format(msg=msg, cmd=self._path, arp=arp)),
escape_tag(Lang.nbp_alc.log.parse(msg=msg, cmd=self._path, arp=arp)),
)
self.executor.clear()
return False
if arp.head_matched:
log(
"DEBUG",
escape_tag(lang.require("nbp-alc", "log.parse").format(msg=msg, cmd=self._path, arp=arp)),
escape_tag(Lang.nbp_alc.log.parse(msg=msg, cmd=self._path, arp=arp)),
)
if not may_help_text and arp.error_info:
may_help_text = str(arp.error_info)
Expand Down

0 comments on commit 24c00dd

Please sign in to comment.