refactor: 所有的 json 加载统一到启动阶段中

This commit is contained in:
RockChinQ
2024-03-16 15:41:59 +08:00
parent 98bebfddaa
commit 302d86056d
7 changed files with 29 additions and 19 deletions
+12
View File
@@ -33,6 +33,8 @@ class Application:
tool_mgr: llm_tool_mgr.ToolManager = None tool_mgr: llm_tool_mgr.ToolManager = None
# ======= 配置管理器 =======
command_cfg: config_mgr.ConfigManager = None command_cfg: config_mgr.ConfigManager = None
pipeline_cfg: config_mgr.ConfigManager = None pipeline_cfg: config_mgr.ConfigManager = None
@@ -43,6 +45,16 @@ class Application:
system_cfg: config_mgr.ConfigManager = None system_cfg: config_mgr.ConfigManager = None
# ======= 元数据配置管理器 =======
sensitive_meta: config_mgr.ConfigManager = None
adapter_qq_botpy_meta: config_mgr.ConfigManager = None
plugin_setting_meta: config_mgr.ConfigManager = None
# =========================
ctr_mgr: center_mgr.V2CenterAPI = None ctr_mgr: center_mgr.V2CenterAPI = None
plugin_mgr: plugin_mgr.PluginManager = None plugin_mgr: plugin_mgr.PluginManager = None
-1
View File
@@ -13,7 +13,6 @@ required_files = {
"data/config/platform.json": "templates/platform.json", "data/config/platform.json": "templates/platform.json",
"data/config/provider.json": "templates/provider.json", "data/config/provider.json": "templates/provider.json",
"data/config/system.json": "templates/system.json", "data/config/system.json": "templates/system.json",
"data/config/sensitive-words.json": "templates/sensitive-words.json",
"data/scenario/default.json": "templates/scenario-template.json", "data/scenario/default.json": "templates/scenario-template.json",
} }
+10
View File
@@ -17,3 +17,13 @@ class LoadConfigStage(stage.BootingStage):
ap.platform_cfg = await config.load_json_config("data/config/platform.json", "templates/platform.json") ap.platform_cfg = await config.load_json_config("data/config/platform.json", "templates/platform.json")
ap.provider_cfg = await config.load_json_config("data/config/provider.json", "templates/provider.json") ap.provider_cfg = await config.load_json_config("data/config/provider.json", "templates/provider.json")
ap.system_cfg = await config.load_json_config("data/config/system.json", "templates/system.json") ap.system_cfg = await config.load_json_config("data/config/system.json", "templates/system.json")
ap.plugin_setting_meta = await config.load_json_config("plugins/plugins.json", "templates/plugin-settings.json")
await ap.plugin_setting_meta.dump_config()
ap.sensitive_meta = await config.load_json_config("data/metadata/sensitive-words.json", "templates/metadata/sensitive-words.json")
await ap.sensitive_meta.dump_config()
ap.adapter_qq_botpy_meta = await config.load_json_config("data/metadata/adapter-qq-botpy.json", "templates/metadata/adapter-qq-botpy.json")
await ap.adapter_qq_botpy_meta.dump_config()
+5 -10
View File
@@ -10,31 +10,26 @@ from ....config import manager as cfg_mgr
class BanWordFilter(filter_model.ContentFilter): class BanWordFilter(filter_model.ContentFilter):
"""根据内容禁言""" """根据内容禁言"""
sensitive: cfg_mgr.ConfigManager
async def initialize(self): async def initialize(self):
self.sensitive = await cfg_mgr.load_json_config( pass
"data/config/sensitive-words.json",
"templates/sensitive-words.json"
)
async def process(self, message: str) -> entities.FilterResult: async def process(self, message: str) -> entities.FilterResult:
found = False found = False
for word in self.sensitive.data['words']: for word in self.ap.sensitive_meta.data['words']:
match = re.findall(word, message) match = re.findall(word, message)
if len(match) > 0: if len(match) > 0:
found = True found = True
for i in range(len(match)): for i in range(len(match)):
if self.sensitive.data['mask_word'] == "": if self.ap.sensitive_meta.data['mask_word'] == "":
message = message.replace( message = message.replace(
match[i], self.sensitive.data['mask'] * len(match[i]) match[i], self.ap.sensitive_meta.data['mask'] * len(match[i])
) )
else: else:
message = message.replace( message = message.replace(
match[i], self.sensitive.data['mask_word'] match[i], self.ap.sensitive_meta.data['mask_word']
) )
return entities.FilterResult( return entities.FilterResult(
+1 -4
View File
@@ -506,10 +506,7 @@ class OfficialAdapter(adapter_model.MessageSourceAdapter):
async def run_async(self): async def run_async(self):
self.metadata = await cfg_mgr.load_json_config( self.metadata = self.ap.adapter_qq_botpy_meta
"data/metadata/adapter-qq-botpy.json",
"templates/metadata/adapter-qq-botpy.json",
)
self.member_openid_mapping = OpenIDMapping( self.member_openid_mapping = OpenIDMapping(
map=self.metadata.data["mapping"]["members"], map=self.metadata.data["mapping"]["members"],
+1 -4
View File
@@ -16,10 +16,7 @@ class SettingManager:
self.ap = ap self.ap = ap
async def initialize(self): async def initialize(self):
self.settings = await cfg_mgr.load_json_config( self.settings = self.ap.plugin_setting_meta
'plugins/plugins.json',
'templates/plugin-settings.json'
)
async def sync_setting( async def sync_setting(
self, self,