From a3cf773e75f72a9e330ef62cdcbe65df58306705 Mon Sep 17 00:00:00 2001 From: Ke Sun Date: Thu, 5 Feb 2026 10:19:43 +0800 Subject: [PATCH] fix(agent): add memory config validation and fix config id reference - Add null check for actual_config_id before calling term_memory_save in langchain_agent.py to prevent errors when memory config is unavailable - Add warning log when skipping term_memory_save due to missing memory config - Fix incorrect attribute reference from memory_config.id to memory_config.config_id in memory_agent_service.py - Fix method call from private _get_workspace_default_config to public get_workspace_default_config in memory_config_service.py - Ensures graceful handling of missing memory configurations and prevents runtime errors --- api/app/core/agent/langchain_agent.py | 10 ++++++++-- api/app/services/memory_agent_service.py | 2 +- api/app/services/memory_config_service.py | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/api/app/core/agent/langchain_agent.py b/api/app/core/agent/langchain_agent.py index 019fe4ce..b2dc8416 100644 --- a/api/app/core/agent/langchain_agent.py +++ b/api/app/core/agent/langchain_agent.py @@ -512,7 +512,10 @@ class LangChainAgent: # AI 回复写入(用户消息和 AI 回复配对,一次性写入完整对话) await self.write(storage_type, actual_end_user_id, message_chat, content, user_rag_memory_id, actual_end_user_id, actual_config_id) '''长期''' - await self.term_memory_save(long_term_messages,actual_config_id,end_user_id,"chunk") + if actual_config_id: + await self.term_memory_save(long_term_messages,actual_config_id,end_user_id,"chunk") + else: + logger.warning(f"Skipping term_memory_save: no memory config available for end_user {end_user_id}") response = { "content": content, "model": self.model_name, @@ -698,7 +701,10 @@ class LangChainAgent: # AI 回复写入(用户消息和 AI 回复配对,一次性写入完整对话) long_term_messages = await agent_chat_messages(message_chat, full_content) await self.write(storage_type, end_user_id, message_chat, full_content, user_rag_memory_id, end_user_id, actual_config_id) - await self.term_memory_save(long_term_messages, actual_config_id, end_user_id, "chunk") + if actual_config_id: + await self.term_memory_save(long_term_messages, actual_config_id, end_user_id, "chunk") + else: + logger.warning(f"Skipping term_memory_save: no memory config available for end_user {end_user_id}") except Exception as e: logger.error(f"Agent astream_events 失败: {str(e)}", exc_info=True) diff --git a/api/app/services/memory_agent_service.py b/api/app/services/memory_agent_service.py index b18f9a40..fed5109f 100644 --- a/api/app/services/memory_agent_service.py +++ b/api/app/services/memory_agent_service.py @@ -1194,7 +1194,7 @@ def get_end_user_connected_config(end_user_id: str, db: Session) -> Dict[str, An workspace_id=app.workspace_id ) - memory_config_id = str(memory_config.id) if memory_config else None + memory_config_id = str(memory_config.config_id) if memory_config else None result = { "end_user_id": str(end_user_id), diff --git a/api/app/services/memory_config_service.py b/api/app/services/memory_config_service.py index 8d8c1978..ebb49fa9 100644 --- a/api/app/services/memory_config_service.py +++ b/api/app/services/memory_config_service.py @@ -525,7 +525,7 @@ class MemoryConfigService: "No memory config ID provided, using workspace default", extra={"workspace_id": str(workspace_id)} ) - return self._get_workspace_default_config(workspace_id) + return self.get_workspace_default_config(workspace_id) config = self.db.get(MemoryConfigModel, memory_config_id) @@ -540,7 +540,7 @@ class MemoryConfigService: } ) - return self._get_workspace_default_config(workspace_id) + return self.get_workspace_default_config(workspace_id) def delete_config( self,