优雅 debuglog

This commit is contained in:
zihanjian
2025-09-26 13:04:02 +08:00
parent adf378c063
commit 0e06f6556b
3 changed files with 48 additions and 21 deletions

View File

@@ -110,10 +110,10 @@ class AIRouter:
返回: (是否处理成功, AI决策结果)
"""
print(f"[AI路由器] route方法被调用")
self.logger.debug(f"[AI路由器] route方法被调用")
if not ctx.text:
print("[AI路由器] ctx.text为空返回False")
self.logger.debug("[AI路由器] ctx.text为空返回False")
return False, None
# 获取AI模型
@@ -122,20 +122,19 @@ class AIRouter:
chat_model = getattr(ctx.robot, 'chat', None) if ctx.robot else None
if not chat_model:
print("[AI路由器] 无可用的AI模型")
self.logger.error("AI路由器无可用的AI模型")
self.logger.error("[AI路由器] 无可用的AI模型")
return False, None
print(f"[AI路由器] 找到AI模型: {type(chat_model)}")
self.logger.debug(f"[AI路由器] 找到AI模型: {type(chat_model)}")
try:
# 构建系统提示词
system_prompt = self._build_ai_prompt()
print(f"[AI路由器] 已构建系统提示词,长度: {len(system_prompt)}")
self.logger.debug(f"[AI路由器] 已构建系统提示词,长度: {len(system_prompt)}")
# 让AI分析用户意图
user_input = f"用户输入:{ctx.text}"
print(f"[AI路由器] 准备调用AI分析意图: {user_input}")
self.logger.debug(f"[AI路由器] 准备调用AI分析意图: {user_input}")
ai_response = chat_model.get_answer(
user_input,
@@ -143,7 +142,7 @@ class AIRouter:
system_prompt_override=system_prompt
)
print(f"[AI路由器] AI响应: {ai_response}")
self.logger.debug(f"[AI路由器] AI响应: {ai_response}")
# 解析AI返回的JSON
json_match = re.search(r'\{.*\}', ai_response, re.DOTALL)
@@ -210,19 +209,19 @@ class AIRouter:
返回: 是否成功处理
"""
print(f"[AI路由器] dispatch被调用消息内容: {ctx.text}")
self.logger.debug(f"[AI路由器] dispatch被调用消息内容: {ctx.text}")
# 检查权限
if not self._check_permission(ctx):
print("[AI路由器] 权限检查失败返回False")
self.logger.info("[AI路由器] 权限检查失败返回False")
return False
# 获取AI路由决策
success, decision = self.route(ctx)
print(f"[AI路由器] route返回 - success: {success}, decision: {decision}")
self.logger.debug(f"[AI路由器] route返回 - success: {success}, decision: {decision}")
if not success or not decision:
print("[AI路由器] route失败或无决策返回False")
self.logger.info("[AI路由器] route失败或无决策返回False")
return False
action_type = decision.get("action_type")

View File

@@ -38,7 +38,7 @@ class MessageContext:
if self.logger:
self.logger.error(f"获取群 {self.msg.roomid} 成员失败: {e}")
else:
print(f"获取群 {self.msg.roomid} 成员失败: {e}")
self.logger.error(f"获取群 {self.msg.roomid} 成员失败: {e}")
self._room_members = {} # 出错时返回空字典
return self._room_members
@@ -54,7 +54,7 @@ class MessageContext:
if self.logger:
self.logger.error(f"获取群 {self.msg.roomid} 成员 {self.msg.sender} 昵称失败: {e}")
else:
print(f"获取群 {self.msg.roomid} 成员 {self.msg.sender} 昵称失败: {e}")
self.logger.error(f"获取群 {self.msg.roomid} 成员 {self.msg.sender} 昵称失败: {e}")
# 群昵称获取失败或私聊,返回通讯录昵称
return self.all_contacts.get(self.msg.sender, self.msg.sender) # 兜底返回 wxid
@@ -80,11 +80,11 @@ class MessageContext:
if self.logger:
self.logger.error(f"发送消息失败: {e}")
else:
print(f"发送消息失败: {e}")
self.logger.error(f"发送消息失败: {e}")
return False
else:
if self.logger:
self.logger.error("Robot实例不存在或没有sendTextMsg方法")
else:
print("Robot实例不存在或没有sendTextMsg方法")
self.logger.error("Robot实例不存在或没有sendTextMsg方法")
return False

View File

@@ -276,7 +276,8 @@ class Robot(Job):
reasoning_chat = self._get_reasoning_chat_model()
if reasoning_chat:
ctx.chat = reasoning_chat
self.LOG.debug(f"使用推理模型 {reasoning_chat} 处理消息")
model_label = self._describe_chat_model(reasoning_chat, reasoning=True)
self.LOG.debug(f"使用推理模型 {model_label} 处理消息")
else:
self.LOG.warning("当前模型未配置推理模型,使用默认模型处理深度思考请求")
@@ -295,15 +296,15 @@ class Robot(Job):
# 5. 优先尝试使用AI路由器处理消息仅限私聊或@机器人)
if (msg.from_group() and msg.is_at(self.wxid)) or not msg.from_group():
print(f"[AI路由调试] 准备调用AI路由器处理消息: {msg.content}")
self.LOG.debug(f"[AI路由调试] 准备调用AI路由器处理消息: {msg.content}")
handled = ai_router.dispatch(ctx)
print(f"[AI路由调试] AI路由器处理结果: {handled}")
self.LOG.debug(f"[AI路由调试] AI路由器处理结果: {handled}")
if handled:
self.LOG.info("消息已由AI路由器处理")
print("[AI路由调试] 消息已成功由AI路由器处理")
self.LOG.debug("[AI路由调试] 消息已成功由AI路由器处理")
return
else:
print("[AI路由调试] AI路由器未处理该消息")
self.LOG.warning("[AI路由调试] AI路由器未处理该消息")
# 6. 如果AI路由器未处理则进行特殊逻辑处理
if not handled:
@@ -547,6 +548,33 @@ class Robot(Job):
return None
return self.reasoning_chat_models.get(model_id)
def _describe_chat_model(self, chat_model, reasoning: bool = False) -> str:
"""根据配置返回模型名称,默认回退到实例类名"""
model_id = getattr(self, 'current_model_id', None)
config_entry = self._get_model_config(model_id) if model_id is not None else None
if config_entry:
if reasoning:
label = config_entry.get("model_reasoning")
if label:
return label
label = config_entry.get("model_flash")
if label:
return label
if chat_model and isinstance(getattr(chat_model, '__class__', None), type):
return chat_model.__class__.__name__
return "未知模型"
def _get_model_config(self, model_id: int):
mapping = {
ChatType.CHATGPT.value: getattr(self.config, 'CHATGPT', None),
ChatType.DEEPSEEK.value: getattr(self.config, 'DEEPSEEK', None),
ChatType.GEMINI.value: getattr(self.config, 'GEMINI', None),
ChatType.PERPLEXITY.value: getattr(self.config, 'PERPLEXITY', None),
}
return mapping.get(model_id)
def _select_model_for_message(self, msg: WxMsg) -> None:
"""根据消息来源选择对应的AI模型