From aff520e69a27306885923b362d0c3205154367e0 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 15 Feb 2025 18:31:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Dify=20=E4=B8=8B?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E4=B8=BB=E5=8A=A8=E5=9B=9E=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20#494?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/provider/sources/dify_source.py | 2 +- packages/astrbot/main.py | 36 +++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/astrbot/core/provider/sources/dify_source.py b/astrbot/core/provider/sources/dify_source.py index 33c53e218..3b171bda5 100644 --- a/astrbot/core/provider/sources/dify_source.py +++ b/astrbot/core/provider/sources/dify_source.py @@ -124,7 +124,7 @@ class ProviderDify(Provider): return LLMResponse(role="assistant", completion_text=result) async def forget(self, session_id): - self.conversation_ids.pop(session_id, None) + self.conversation_ids[session_id] = "" return True async def get_current_key(self): diff --git a/packages/astrbot/main.py b/packages/astrbot/main.py index 6846fa5b9..c1df24985 100644 --- a/packages/astrbot/main.py +++ b/packages/astrbot/main.py @@ -775,20 +775,30 @@ UID: {user_id} 此 ID 可用于设置管理员。/op 授权管理员, /deo logger.error("未找到任何 LLM 提供商。请先配置。无法主动回复") return try: - session_curr_cid = await self.context.conversation_manager.get_curr_conversation_id(event.unified_msg_origin) - - if not session_curr_cid: - logger.error("当前未处于对话状态,无法主动回复,请确保 平台设置->会话隔离(unique_session) 未开启,并使用 /switch 序号 切换或者 /new 创建一个会话。") - return - - conv = await self.context.conversation_manager.get_conversation( - event.unified_msg_origin, - session_curr_cid - ) + conv = None history = [] - if conv: - history = json.loads(conv.history) - + if provider.meta().type != 'dify': + # Dify 自己有维护对话,不需要 bot 端维护。 + session_curr_cid = await self.context.conversation_manager.get_curr_conversation_id(event.unified_msg_origin) + + if not session_curr_cid: + logger.error("当前未处于对话状态,无法主动回复,请确保 平台设置->会话隔离(unique_session) 未开启,并使用 /switch 序号 切换或者 /new 创建一个会话。") + return + + conv = await self.context.conversation_manager.get_conversation( + event.unified_msg_origin, + session_curr_cid + ) + history = [] + if conv: + history = json.loads(conv.history) + else: + assert isinstance(provider, ProviderDify) + cid = provider.conversation_ids.get(event.unified_msg_origin, None) + if cid is None: + logger.error("[Dify] 当前未处于对话状态,无法主动回复,请确保 平台设置->会话隔离(unique_session) 未开启,并使用 /switch 序号 切换或者 /new 创建一个会话。") + return + prompt = self.ltm.ar_prompt if not prompt: prompt = event.message_str