From 78617ec7ce8bf0a4b9a720a2840041c3e7cfecfd Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Sat, 24 Jan 2026 14:25:41 +0800 Subject: [PATCH] fix: enhance provider selection error handling and logging (#4654) --- .../method/agent_sub_stages/internal.py | 9 +++++++-- astrbot/core/pipeline/waking_check/stage.py | 1 - astrbot/core/star/context.py | 17 +++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py b/astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py index 1cce2eb87..41e142819 100644 --- a/astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py +++ b/astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py @@ -116,8 +116,12 @@ class InternalAgentSubStage(Stage): if not provider: logger.error(f"未找到指定的提供商: {sel_provider}。") return provider - - return _ctx.get_using_provider(umo=event.unified_msg_origin) + try: + prov = _ctx.get_using_provider(umo=event.unified_msg_origin) + except ValueError as e: + logger.error(f"Error occurred while selecting provider: {e}") + return None + return prov async def _get_session_conv(self, event: AstrMessageEvent) -> Conversation: umo = event.unified_msg_origin @@ -496,6 +500,7 @@ class InternalAgentSubStage(Stage): try: provider = self._select_provider(event) if provider is None: + logger.info("未找到任何对话模型(提供商),跳过 LLM 请求处理。") return if not isinstance(provider, Provider): logger.error( diff --git a/astrbot/core/pipeline/waking_check/stage.py b/astrbot/core/pipeline/waking_check/stage.py index 50599e818..2dcb840e9 100644 --- a/astrbot/core/pipeline/waking_check/stage.py +++ b/astrbot/core/pipeline/waking_check/stage.py @@ -165,7 +165,6 @@ class WakingCheckStage(Stage): and handler.handler_module_path == "astrbot.builtin_stars.builtin_commands.main" ): - logger.debug("skipping builtin command") continue # filter 需满足 AND 逻辑关系 diff --git a/astrbot/core/star/context.py b/astrbot/core/star/context.py index 9c47ba3a7..dda2d1d0b 100644 --- a/astrbot/core/star/context.py +++ b/astrbot/core/star/context.py @@ -328,28 +328,29 @@ class Context: """获取所有用于 Embedding 任务的 Provider。""" return self.provider_manager.embedding_provider_insts - def get_using_provider(self, umo: str | None = None) -> Provider: + def get_using_provider(self, umo: str | None = None) -> Provider | None: """获取当前使用的用于文本生成任务的 LLM Provider(Chat_Completion 类型)。 Args: umo: unified_message_origin 值,如果传入并且用户启用了提供商会话隔离, - 则使用该会话偏好的提供商。 + 则使用该会话偏好的对话模型(提供商)。 Returns: - 当前使用的文本生成提供者。 + 当前使用的对话模型(提供商),如果未设置则返回 None。 Raises: - ValueError: 返回的提供者不是 Provider 类型。 - - Note: - 通过 /provider 指令可以切换提供者。 + ValueError: 该会话来源配置的的对话模型(提供商)的类型不正确。 """ prov = self.provider_manager.get_using_provider( provider_type=ProviderType.CHAT_COMPLETION, umo=umo, ) + if prov is None: + return None if not isinstance(prov, Provider): - raise ValueError("返回的 Provider 不是 Provider 类型") + raise ValueError( + f"该会话来源的对话模型(提供商)的类型不正确: {type(prov)}" + ) return prov def get_using_tts_provider(self, umo: str | None = None) -> TTSProvider | None: