From f7e34202126b183a5dd5ac65558ca7a5636eb5ff Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 28 May 2025 10:10:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=80=E4=B8=8Bdify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/dify/main.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/plugins/dify/main.py b/plugins/dify/main.py index a248c6a..982c039 100644 --- a/plugins/dify/main.py +++ b/plugins/dify/main.py @@ -140,7 +140,9 @@ class DifyPlugin(MessagePluginInterface): try: # 调用Dify API获取回复 - response = self._chat_with_dify((roomid if roomid else sender), sender, query) + success, response = self._chat_with_dify((roomid if roomid else sender), sender, query) + if not success: + return False, response # 去除广告内容 response = remove_trailing_content(response) @@ -184,7 +186,8 @@ class DifyPlugin(MessagePluginInterface): return False, "命令格式错误" # 检查权限 - if roomid and gbm.get_group_permission((roomid if roomid else sender), Feature.AI_CAPABILITY) == PermissionStatus.DISABLED: + if roomid and gbm.get_group_permission((roomid if roomid else sender), + Feature.AI_CAPABILITY) == PermissionStatus.DISABLED: return False, "没有权限" client_msg_id, create_time, new_msg_id = await bot.send_text_message((roomid if roomid else sender), @@ -203,7 +206,9 @@ class DifyPlugin(MessagePluginInterface): try: # 调用Dify API获取回复 - response = self._chat_with_dify(session_id, user_id, query) + success, response = self._chat_with_dify(session_id, user_id, query) + if not success: + return False, response # 去除广告内容 self.LOG.debug(f"_chat_with_dify response:{response}") response = remove_trailing_content(response) @@ -242,7 +247,7 @@ class DifyPlugin(MessagePluginInterface): revoke.add_message_to_revoke((roomid if roomid else sender), client_msg_id, create_time, new_msg_id, 5) return False, f"处理出错: {e}" - def _chat_with_dify(self, session_id: str, user_id: str, query: str) -> Optional[str]: + def _chat_with_dify(self, session_id: str, user_id: str, query: str) -> Tuple[bool, Optional[str]]: """ 与Dify API交互获取回复 @@ -322,7 +327,7 @@ class DifyPlugin(MessagePluginInterface): if response.status_code != 200: self.LOG.error(f"Dify API请求失败: {response.status_code} {response.text}") - return f"请求失败,状态码: {response.status_code}" + return False, f"请求失败,状态码: {response.status_code}" # 解析响应 response_data = response.json() @@ -399,11 +404,11 @@ class DifyPlugin(MessagePluginInterface): self.LOG.info( f"用户 {user_id} 本次消耗 {total_tokens} tokens,累计 {self.token_usage[user_id]} tokens") - return answer + return True, answer except Exception as e: self.LOG.error(f"处理Dify响应时出错: {str(e)}") - return f"处理响应时出错: {str(e)}" + return False, f"处理响应时出错" def _cleanup_expired_conversations(self) -> None: """清理过期的会话"""