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.
JavaScript a2aagentic-aiai-agentsdevtoolsmcp-clientn8n-alternativeno-codeopen-sourceorchestrationsmythosvisual-builder
Refactor useChat hook: Enhance user interruption handling and improve comments
Salman-Ahamedc90a3da03002
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.tsindex e6bee93f..1c4c356e 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@@ -399,17 +399,16 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {onError: (streamError) => {// Handle stream errorsif (streamError.isAborted) {- // User stopped generation - finalize current message with generated content+ // 🎯 User stopped generationconst currentContent = currentAIMessageRef.current;- // Update the last message to finalize it (convert loading to system)+ // Step 1: Finalize the AI message (loading → system)setMessages((prev) => {const updated = [...prev];const lastMessageIndex = updated.length - 1;if (lastMessageIndex >= 0) {const lastMsg = updated[lastMessageIndex];- // Finalize the AI message (change from loading to system)if (lastMsg.type === 'loading' || lastMsg.type === 'system') {updated[lastMessageIndex] = {...lastMsg,@@ -420,7 +419,7 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {}}- // Add a separate message for user stopped notification+ // Step 2: Add separate error messageconst stopMessage: IChatMessage = {id: Date.now() + 2,message: USER_STOPPED_MESSAGE,@@ -500,7 +499,7 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {const { message, files } = lastUserMessageRef.current;- // Remove messages: check if last message is error (stop message)+ // Smart removal: check last message typesetMessages((prev) => {const lastMsg = prev[prev.length - 1];@@ -520,21 +519,20 @@ export const useChat = (config: IUseChatConfig): IUseChatReturn => {* The error handler will preserve generated content and append USER_STOPPED_MESSAGE*/const stopGenerating = useCallback(() => {- // Clear throttle timeout to prevent further updates+ // Step 1: Clear throttle timeoutif (updateThrottleRef.current) {clearTimeout(updateThrottleRef.current);updateThrottleRef.current = null;}- // Flush any pending updates before aborting+ // Step 2: Flush any pending updates before abortingif (pendingUpdateRef.current) {const { content: pendingContent } = pendingUpdateRef.current;currentAIMessageRef.current = pendingContent; // Save pending contentpendingUpdateRef.current = null;}- // Abort the stream (this will trigger onError with isAborted=true)- // The error handler will preserve currentAIMessageRef.current and append stop message+ // Step 3: Abort the stream (onError will handle the rest)abortStream();}, [abortStream]);