diff --git a/nonebot_plugin_marshoai/config.py b/nonebot_plugin_marshoai/config.py index 6bf55237..1e0ec207 100644 --- a/nonebot_plugin_marshoai/config.py +++ b/nonebot_plugin_marshoai/config.py @@ -1,4 +1,3 @@ -import shutil from io import StringIO from pathlib import Path @@ -81,15 +80,15 @@ destination_folder = Path("config/marshoai/") destination_file = destination_folder / "config.yaml" -def dump_config_to_yaml(config: ConfigModel): - return yaml_.dump(config.model_dump(), allow_unicode=True, default_flow_style=False) +def dump_config_to_yaml(cfg: ConfigModel): + return yaml_.dump(cfg.model_dump(), allow_unicode=True, default_flow_style=False) -def write_default_config(destination_file): +def write_default_config(dest_file): """ 写入默认配置 """ - with open(destination_file, "w", encoding="utf-8") as f: + with open(dest_file, "w", encoding="utf-8") as f: with StringIO(dump_config_to_yaml(ConfigModel())) as f2: f.write(f2.read()) @@ -110,17 +109,17 @@ def check_yaml_is_changed(): return True -def merge_configs(old_config, new_config): +def merge_configs(existing_cfg, new_cfg): """ 合并配置文件 """ - for key, value in new_config.items(): - if key in old_config: + for key, value in new_cfg.items(): + if key in existing_cfg: continue else: logger.info(f"新增配置项: {key} = {value}") - old_config[key] = value - return old_config + existing_cfg[key] = value + return existing_cfg config: ConfigModel = get_plugin_config(ConfigModel) diff --git a/nonebot_plugin_marshoai/instances.py b/nonebot_plugin_marshoai/instances.py index 3120c6e4..d5aaf709 100644 --- a/nonebot_plugin_marshoai/instances.py +++ b/nonebot_plugin_marshoai/instances.py @@ -1,6 +1,4 @@ # Marsho 的类实例以及全局变量 -from azure.ai.inference.aio import ChatCompletionsClient -from azure.core.credentials import AzureKeyCredential from nonebot import get_driver from openai import AsyncOpenAI diff --git a/nonebot_plugin_marshoai/marsho.py b/nonebot_plugin_marshoai/marsho.py index 33a74c4f..83338457 100644 --- a/nonebot_plugin_marshoai/marsho.py +++ b/nonebot_plugin_marshoai/marsho.py @@ -2,7 +2,6 @@ import contextlib import traceback from typing import Optional -import openai from arclet.alconna import Alconna, AllParam, Args from azure.ai.inference.models import ( AssistantMessage, @@ -18,10 +17,11 @@ from nonebot.adapters import Bot, Event, Message from nonebot.matcher import Matcher from nonebot.params import CommandArg from nonebot.permission import SUPERUSER -from nonebot.rule import Rule, to_me +from nonebot.rule import to_me from nonebot.typing import T_State from nonebot_plugin_alconna import MsgTarget, UniMessage, UniMsg, on_alconna +from .constants import INTRODUCTION, OPENAI_NEW_MODELS, SUPPORT_IMAGE_MODELS from .hooks import * from .instances import * from .metadata import metadata diff --git a/nonebot_plugin_marshoai/models.py b/nonebot_plugin_marshoai/models.py index 85b1c08c..47f02c85 100755 --- a/nonebot_plugin_marshoai/models.py +++ b/nonebot_plugin_marshoai/models.py @@ -9,7 +9,6 @@ import traceback from nonebot import logger from .config import config -from .util import * class MarshoContext: diff --git a/nonebot_plugin_marshoai/util.py b/nonebot_plugin_marshoai/util.py index dca0b16e..bceef64a 100755 --- a/nonebot_plugin_marshoai/util.py +++ b/nonebot_plugin_marshoai/util.py @@ -21,12 +21,12 @@ from zhDateTime import DateTime from ._types import DeveloperMessage from .config import config -from .constants import * +from .constants import CODE_BLOCK_PATTERN, IMG_LATEX_PATTERN, OPENAI_NEW_MODELS from .deal_latex import ConvertLatex nickname_json = None # 记录昵称 praises_json = None # 记录夸赞名单 -loaded_target_list = [] # 记录已恢复备份的上下文的列表 +loaded_target_list: List[str] = [] # 记录已恢复备份的上下文的列表 NOT_GIVEN = NotGiven() @@ -219,7 +219,7 @@ async def get_nicknames(): try: async with aiofiles.open(filename, "r", encoding="utf-8") as f: nickname_json = json.loads(await f.read()) - except Exception: + except (json.JSONDecodeError, FileNotFoundError): nickname_json = {} return nickname_json @@ -249,7 +249,7 @@ async def refresh_nickname_json(): store.get_plugin_data_file("nickname.json"), "r", encoding="utf-8" ) as f: nickname_json = json.loads(await f.read()) - except Exception: + except (json.JSONDecodeError, FileNotFoundError): logger.error("刷新 nickname_json 表错误:无法载入 nickname.json 文件")