From 6654f0caf854b165cb99d86bbcbdadf370e2d069 Mon Sep 17 00:00:00 2001 From: tt-P607 <68868379+tt-P607@users.noreply.github.com> Date: Fri, 10 Oct 2025 11:15:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(tts):=20=E4=BF=AE=E6=AD=A3=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E5=8A=A0=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 先前通过 `__file__` 相对路径定位配置文件的方案不够健壮,在不同运行环境下可能导致路径解析失败。 本次修改通过从插件文件向上追溯,动态计算项目根目录,从而构建了更可靠的配置文件绝对路径。 此外,移除了实例级别的配置缓存,确保每次都能读取到最新的配置,避免了因缓存导致的问题。同时,恢复了对 `spatial_effects` 配置节的加载支持。 --- .../built_in/tts_voice_plugin/plugin.py | 58 ++++++++----------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/src/plugins/built_in/tts_voice_plugin/plugin.py b/src/plugins/built_in/tts_voice_plugin/plugin.py index 158924fa4..29f092e7c 100644 --- a/src/plugins/built_in/tts_voice_plugin/plugin.py +++ b/src/plugins/built_in/tts_voice_plugin/plugin.py @@ -49,49 +49,41 @@ class TTSVoicePlugin(BasePlugin): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.tts_service = None - # 新增配置缓存 - self._config_cache = None def _get_config_wrapper(self, key: str, default: Any = None) -> Any: """ 配置获取的包装器,用于解决 get_config 无法直接获取动态表(如 tts_styles)和未在 schema 中定义的节的问题。 由于插件系统的 schema 为空时不会加载未定义的键,这里手动读取配置文件以获取所需配置。 """ - # 需要手动加载的顶级配置节(移除未定义的 spatial_effects) - manual_load_keys = ["tts_styles", "tts_advanced", "tts"] + # 需要手动加载的顶级配置节 + manual_load_keys = ["tts_styles", "spatial_effects", "tts_advanced", "tts"] top_key = key.split('.')[0] - # 仅当需要手动加载且缓存为空时加载配置 - if top_key in manual_load_keys and self._config_cache is None: - try: - # 优化:使用标准插件路径 - config_path = Path(__file__).parent / "config" / self.config_file_name - - if not config_path.exists(): - logger.error(f"TTS config file not found at {config_path}") - self._config_cache = {} - else: - # 使用 toml.load 代替 toml.loads 以提高性能 - self._config_cache = toml.load(config_path) - except toml.TomlDecodeError as e: - logger.error(f"Invalid TOML syntax in config: {e}") - self._config_cache = {} - except FileNotFoundError: - logger.error(f"Config file missing: {config_path}") - self._config_cache = {} - except Exception as e: - logger.error(f"Unexpected config error: {e}", exc_info=True) - self._config_cache = {} - - # 从缓存中获取配置 if top_key in manual_load_keys: - value = self._config_cache - for k in key.split('.'): - if isinstance(value, dict): - value = value.get(k) - else: + try: + plugin_file = Path(__file__).resolve() + bot_root = plugin_file.parent.parent.parent.parent.parent + config_file = bot_root / "config" / "plugins" / self.plugin_name / self.config_file_name + + if not config_file.is_file(): + logger.error(f"TTS config file not found at robustly constructed path: {config_file}") return default - return value if value is not None else default + + full_config = toml.loads(config_file.read_text(encoding="utf-8")) + + # 支持点状路径访问 + value = full_config + for k in key.split('.'): + if isinstance(value, dict): + value = value.get(k) + else: + return default + + return value if value is not None else default + + except Exception as e: + logger.error(f"Failed to manually load '{key}' from config: {e}", exc_info=True) + return default return self.get_config(key, default)