From cc577e7b502967b7a6de44b6ab9be81dd81595e7 Mon Sep 17 00:00:00 2001 From: Wendong-Fan Date: Thu, 20 Nov 2025 14:16:53 +0800 Subject: [PATCH] minor fix wendong --- src/store/chatStore.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/store/chatStore.ts b/src/store/chatStore.ts index ba612a424..296a15bdf 100644 --- a/src/store/chatStore.ts +++ b/src/store/chatStore.ts @@ -458,7 +458,6 @@ const chatStore = (initial?: Partial) => createStore()( // Create error task to notify user const currentStore = getCurrentChatStore(); - const currentId = getCurrentTaskId(); const newTaskId = currentStore.create(); currentStore.setActiveTaskId(newTaskId); currentStore.setHasWaitComfirm(newTaskId, true); @@ -1223,9 +1222,14 @@ const chatStore = (initial?: Partial) => createStore()( try { console.error('Model error:', agentMessages.data); - // Safely extract error message with optional chaining + // Validate that agentMessages.data exists before processing + if (agentMessages.data === undefined || agentMessages.data === null) { + throw new Error('Invalid error message format: missing data'); + } + + // Safely extract error message with fallback chain const errorMessage = agentMessages.data?.message || - agentMessages.data?.error || + (typeof agentMessages.data === 'string' ? agentMessages.data : null) || 'An error occurred while processing your request'; // Mark all incomplete tasks as failed @@ -1277,18 +1281,21 @@ const chatStore = (initial?: Partial) => createStore()( console.error('Failed to handle model error:', error); console.error('Original agentMessages:', agentMessages); - // Fallback: create error task with generic message + // Fallback: try to create error task with minimal operations try { + const { create, setActiveTaskId, setHasWaitComfirm, addMessages } = get(); const fallbackTaskId = create(); setActiveTaskId(fallbackTaskId); setHasWaitComfirm(fallbackTaskId, true); addMessages(fallbackTaskId, { id: generateUniqueId(), role: "agent", - content: `**Critical Error**: An unexpected error occurred. Please try again or contact support.`, + content: `**Critical Error**: An unexpected error occurred while handling a model error. Please refresh the application or contact support.`, }); } catch (fallbackError) { console.error('Failed to create fallback error task:', fallbackError); + // Last resort: just log the error without creating UI elements + console.error('Original error that could not be displayed:', agentMessages); } } return;