From 13f31d3fae0117b9307be4087f9a93caa5f2e16f Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 12 Feb 2023 13:15:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?perf:=20=E6=8F=92=E4=BB=B6=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=A4=B1=E8=B4=A5=E6=97=B6=E4=BB=8D=E4=B8=8D=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E5=90=8E=E5=BA=8F=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index 441fe315..e0425098 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -48,11 +48,15 @@ def walk_plugin_path(module, prefix='', path_prefix=''): walk_plugin_path(__import__(module.__name__ + '.' + item.name, fromlist=['']), prefix + item.name + '.', path_prefix + item.name + '/') else: - logging.debug("扫描插件模块: plugins/{}".format(path_prefix + item.name + '.py')) - logging.info('加载模块: plugins/{}'.format(path_prefix + item.name + '.py')) - __current_module_path__ = "plugins/"+path_prefix + item.name + '.py' + try: + logging.debug("扫描插件模块: plugins/{}".format(path_prefix + item.name + '.py')) + __current_module_path__ = "plugins/"+path_prefix + item.name + '.py' - importlib.import_module(module.__name__ + '.' + item.name) + importlib.import_module(module.__name__ + '.' + item.name) + logging.info('加载模块: plugins/{} 成功'.format(path_prefix + item.name + '.py')) + except: + logging.error('加载模块: plugins/{} 失败: {}'.format(path_prefix + item.name + '.py', sys.exc_info())) + traceback.print_exc() def load_plugins(): From c9028227231db6e5ecde7ecb1c562b9679c75302 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 13 Feb 2023 17:21:23 +0800 Subject: [PATCH 2/3] =?UTF-8?q?perf:=20=E5=AE=8C=E5=96=84config.py?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config-template.py b/config-template.py index 0d9a931c..a8b2beb7 100644 --- a/config-template.py +++ b/config-template.py @@ -68,7 +68,7 @@ sensitive_word_filter = True encourage_sponsor_at_start = True # 每次向OpenAI接口发送对话记录上下文的字符数 -# 最大不超过(4096 - max_tokens)个字符,max_tokens为上述completion_api_params中的max_tokens +# 最大不超过(4096 - max_tokens)个字符,max_tokens为下方completion_api_params中的max_tokens # 注意:较大的prompt_submit_length会导致OpenAI账户额度消耗更快 prompt_submit_length = 1024 @@ -77,7 +77,7 @@ prompt_submit_length = 1024 completion_api_params = { "model": "text-davinci-003", "temperature": 0.9, # 数值越低得到的回答越理性,取值范围[0, 1] - "max_tokens": 512, # 每次向OpenAI请求的最大字符数, 不高于4096 + "max_tokens": 512, # 每次获取OpenAI接口响应的文字量上限, 不高于4096 "top_p": 1, # 生成的文本的文本与要求的符合度, 取值范围[0, 1] "frequency_penalty": 0.2, "presence_penalty": 1.0, From 3c08741cb63bfc8ea5eb77cd5c40221d60871ff1 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Tue, 14 Feb 2023 17:57:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=9C=A8settings?= =?UTF-8?q?.json=E4=B8=AD=E8=AE=BE=E7=BD=AE=E6=8F=92=E4=BB=B6=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=20(#133)=20(#109)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 27 +++++++++++++- pkg/plugin/settings.py | 84 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 pkg/plugin/settings.py diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index e0425098..f431aa04 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -9,6 +9,7 @@ import traceback import pkg.utils.context as context import pkg.plugin.switch as switch +import pkg.plugin.settings as settings from mirai import Mirai @@ -35,6 +36,23 @@ __plugins__ = {} } }""" +__plugins_order__ = [] +"""插件顺序""" + + +def generate_plugin_order(): + """ 根据__plugin__生成插件初始顺序,无视是否启用 """ + global __plugins_order__ + __plugins_order__ = [] + for plugin_name in __plugins__: + __plugins_order__.append(plugin_name) + + +def iter_plugins(): + """ 按照顺序迭代插件 """ + for plugin_name in __plugins_order__: + yield __plugins__[plugin_name] + __current_module_path__ = "" @@ -70,11 +88,16 @@ def load_plugins(): # 加载开关数据 switch.load_switch() + # 生成初始顺序 + generate_plugin_order() + # 加载插件顺序 + settings.load_settings() + def initialize_plugins(): """ 初始化插件 """ logging.info("初始化插件") - for plugin in __plugins__.values(): + for plugin in iter_plugins(): if not plugin['enabled']: continue try: @@ -243,7 +266,7 @@ class PluginHost: """ 触发事件 """ event_context = EventContext(event_name) logging.debug("触发事件: {} ({})".format(event_name, event_context.eid)) - for plugin in __plugins__.values(): + for plugin in iter_plugins(): if not plugin['enabled']: continue diff --git a/pkg/plugin/settings.py b/pkg/plugin/settings.py new file mode 100644 index 00000000..474decce --- /dev/null +++ b/pkg/plugin/settings.py @@ -0,0 +1,84 @@ +import json +import os + +import pkg.plugin.host as host +import logging + + +def wrapper_dict_from_runtime_context() -> dict: + """从变量中包装settings.json的数据字典""" + settings = { + "order": [] + } + + for plugin_name in host.__plugins_order__: + settings["order"].append(plugin_name) + + return settings + + +def apply_settings(settings: dict): + """将settings.json数据应用到变量中""" + if "order" in settings: + host.__plugins_order__ = settings["order"] + + +def dump_settings(): + """保存settings.json数据""" + logging.debug("保存plugins/settings.json数据") + + settings = wrapper_dict_from_runtime_context() + + with open("plugins/settings.json", "w", encoding="utf-8") as f: + json.dump(settings, f, indent=4, ensure_ascii=False) + + +def load_settings(): + """加载settings.json数据""" + logging.debug("加载plugins/settings.json数据") + + # 读取plugins/settings.json + settings = { + } + + # 检查文件是否存在 + if not os.path.exists("plugins/settings.json"): + # 不存在则创建 + with open("plugins/settings.json", "w", encoding="utf-8") as f: + json.dump(wrapper_dict_from_runtime_context(), f, indent=4, ensure_ascii=False) + + with open("plugins/settings.json", "r", encoding="utf-8") as f: + settings = json.load(f) + + if settings is None: + settings = { + } + + # 检查每个设置项 + if "order" not in settings: + settings["order"] = [] + + settings_modified = False + + settings_copy = settings.copy() + + # 检查settings中多余的插件项 + + # order + for plugin_name in settings_copy["order"]: + if plugin_name not in host.__plugins_order__: + settings["order"].remove(plugin_name) + settings_modified = True + + # 检查settings中缺少的插件项 + + # order + for plugin_name in host.__plugins_order__: + if plugin_name not in settings_copy["order"]: + settings["order"].append(plugin_name) + settings_modified = True + + if settings_modified: + dump_settings() + + apply_settings(settings)