From 8b5dfa971380b2eda4f20b41496d7f366bb62c13 Mon Sep 17 00:00:00 2001 From: zihanjian Date: Thu, 1 Jan 2026 15:00:52 +0800 Subject: [PATCH] feat(chat): add force_reasoning flag for chat model selection --- commands/handlers.py | 5 ++++- robot.py | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/commands/handlers.py b/commands/handlers.py index 97ac8db..dccccba 100644 --- a/commands/handlers.py +++ b/commands/handlers.py @@ -175,7 +175,10 @@ def handle_chitchat(ctx: 'MessageContext', match: Optional[Match]) -> bool: tools = None tool_handler = None - if ctx.robot and getattr(ctx.robot, 'message_summary', None): + # 插嘴模式下不使用 function call,减少 token 消耗 + is_auto_random_reply = getattr(ctx, 'auto_random_reply', False) + + if ctx.robot and getattr(ctx.robot, 'message_summary', None) and not is_auto_random_reply: chat_id = ctx.get_receiver() message_summary = ctx.robot.message_summary diff --git a/robot.py b/robot.py index 60d84c1..24628e1 100644 --- a/robot.py +++ b/robot.py @@ -331,6 +331,11 @@ class Robot(Job): setattr(ctx, 'persona', persona_text) group_enabled = ctx.is_group and self._is_group_enabled(msg.roomid) setattr(ctx, 'group_enabled', group_enabled) + + # 检查是否配置了 force_reasoning(闲聊时强制使用推理模型,AI路由仍正常执行) + force_reasoning = getattr(self, '_current_force_reasoning', False) + setattr(ctx, 'force_reasoning', force_reasoning) + trigger_decision = None if getattr(self, "keyword_trigger_processor", None): trigger_decision = self.keyword_trigger_processor.evaluate(ctx) @@ -665,12 +670,17 @@ class Robot(Job): def _handle_chitchat(self, ctx, match=None): """统一处理闲聊,自动切换推理模型""" - reasoning_requested = bool(getattr(ctx, 'reasoning_requested', False)) + # force_reasoning 配置会强制使用推理模型 + force_reasoning = bool(getattr(ctx, 'force_reasoning', False)) + reasoning_requested = bool(getattr(ctx, 'reasoning_requested', False)) or force_reasoning previous_ctx_chat = getattr(ctx, 'chat', None) reasoning_chat = None if reasoning_requested: - self.LOG.info("检测到推理模式请求,将启用深度思考。") + if force_reasoning and not getattr(ctx, 'reasoning_requested', False): + self.LOG.info("群配置了 force_reasoning,闲聊将使用推理模型。") + else: + self.LOG.info("检测到推理模式请求,将启用深度思考。") ctx.send_text("正在深度思考,请稍候...", record_message=False) reasoning_chat = self._get_reasoning_chat_model() if reasoning_chat: @@ -759,12 +769,15 @@ class Robot(Job): """根据消息来源选择对应的AI模型 :param msg: 接收到的消息 """ + # 重置 force_reasoning 标记 + self._current_force_reasoning = False + if not hasattr(self, 'chat_models') or not self.chat_models: return # 没有可用模型,无需切换 - + # 获取消息来源ID source_id = msg.roomid if msg.from_group() else msg.sender - + # 检查配置 if not hasattr(self.config, 'GROUP_MODELS'): # 没有配置,使用默认模型 @@ -772,13 +785,15 @@ class Robot(Job): self.chat = self.chat_models[self.default_model_id] self.current_model_id = self.default_model_id return - + # 群聊消息处理 if msg.from_group(): model_mappings = self.config.GROUP_MODELS.get('mapping', []) for mapping in model_mappings: if mapping.get('room_id') == source_id: model_id = mapping.get('model') + # 读取 force_reasoning 配置 + self._current_force_reasoning = bool(mapping.get('force_reasoning', False)) if model_id in self.chat_models: # 切换到指定模型 if self.chat != self.chat_models[model_id]: