Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Enhance message filtering in useChat hook

Salman-Ahamed22d27063ddb7
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-chat.ts b/packages/app/src/react/features/ai-chat/hooks/use-chat.ts
index 48bf86e8..c1476139 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-chat.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-chat.ts
@@ -308,8 +308,22 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {
setIsProcessing(true);
clearError();
- // Remove any previous error messages before sending new message
- setMessages((prev) => prev.filter((msg) => msg.type !== 'error'));
+ // Remove any previous error messages and incomplete AI messages before sending new message
+ setMessages((prev) => {
+ // Check if there are any error messages (indicates stopped generation)
+ const hasErrorMessages = prev.some((msg) => msg.type === 'error');
+
+ // Remove error messages
+ const filtered = prev.filter((msg) => msg.type !== 'error');
+
+ // If there were error messages, also remove the loading message
+ if (hasErrorMessages && filtered.length > 0) {
+ const lastMessage = filtered[filtered.length - 1];
+ if (lastMessage.type === 'loading') return filtered.slice(0, -1);
+ }
+
+ return filtered;
+ });
// Upload files if present
let attachments: IFileAttachment[] = [];