From 91a9361dde873a2aab462c0764445c614f32f4f7 Mon Sep 17 00:00:00 2001 From: Kwanghyun On Date: Sun, 17 Nov 2024 22:26:22 -0500 Subject: [PATCH 1/2] feat: Add stop generation button during AI responses --- src/components/chat-view/Chat.tsx | 14 ++++++++++++-- styles.css | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/components/chat-view/Chat.tsx b/src/components/chat-view/Chat.tsx index 3c8f07e..4acfb3d 100644 --- a/src/components/chat-view/Chat.tsx +++ b/src/components/chat-view/Chat.tsx @@ -1,5 +1,5 @@ import { useMutation } from '@tanstack/react-query' -import { History, Plus } from 'lucide-react' +import { CircleStop, History, Plus } from 'lucide-react' import { App, Notice } from 'obsidian' import { forwardRef, @@ -118,9 +118,10 @@ const Chat = forwardRef((props, ref) => { const [queryProgress, setQueryProgress] = useState({ type: 'idle', }) + const [isStreaming, setIsStreaming] = useState(false) + const preventAutoScrollRef = useRef(false) const lastProgrammaticScrollRef = useRef(0) - const activeStreamAbortControllersRef = useRef([]) const chatUserInputRefs = useRef>(new Map()) const chatMessagesRef = useRef(null) @@ -171,6 +172,7 @@ const Chat = forwardRef((props, ref) => { abortController.abort() } activeStreamAbortControllersRef.current = [] + setIsStreaming(false) } const handleLoadConversation = async (conversationId: string) => { @@ -241,6 +243,7 @@ const Chat = forwardRef((props, ref) => { try { const abortController = new AbortController() activeStreamAbortControllersRef.current.push(abortController) + setIsStreaming(true) const { requestMessages, compiledMessages } = await promptGenerator.generateRequestMessages({ @@ -280,6 +283,7 @@ const Chat = forwardRef((props, ref) => { handleScrollToBottom() } } + setIsStreaming(false) } catch (error) { if (error.name === 'AbortError') { return @@ -594,6 +598,12 @@ const Chat = forwardRef((props, ref) => { ), )} + {isStreaming && ( + + )} Date: Wed, 20 Nov 2024 14:47:57 +0900 Subject: [PATCH 2/2] fix: use submitMutation.isPending instead of separate streaming state --- src/components/chat-view/Chat.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/components/chat-view/Chat.tsx b/src/components/chat-view/Chat.tsx index 4acfb3d..ea0eb57 100644 --- a/src/components/chat-view/Chat.tsx +++ b/src/components/chat-view/Chat.tsx @@ -118,7 +118,6 @@ const Chat = forwardRef((props, ref) => { const [queryProgress, setQueryProgress] = useState({ type: 'idle', }) - const [isStreaming, setIsStreaming] = useState(false) const preventAutoScrollRef = useRef(false) const lastProgrammaticScrollRef = useRef(0) @@ -172,7 +171,6 @@ const Chat = forwardRef((props, ref) => { abortController.abort() } activeStreamAbortControllersRef.current = [] - setIsStreaming(false) } const handleLoadConversation = async (conversationId: string) => { @@ -243,7 +241,6 @@ const Chat = forwardRef((props, ref) => { try { const abortController = new AbortController() activeStreamAbortControllersRef.current.push(abortController) - setIsStreaming(true) const { requestMessages, compiledMessages } = await promptGenerator.generateRequestMessages({ @@ -283,7 +280,6 @@ const Chat = forwardRef((props, ref) => { handleScrollToBottom() } } - setIsStreaming(false) } catch (error) { if (error.name === 'AbortError') { return @@ -598,7 +594,7 @@ const Chat = forwardRef((props, ref) => { ), )} - {isStreaming && ( + {submitMutation.isPending && (