Skip to content

Commit

Permalink
feat: Add stop generation button during AI responses
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-on committed Nov 19, 2024
1 parent 82e276e commit 91a9361
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/components/chat-view/Chat.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -118,9 +118,10 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
const [queryProgress, setQueryProgress] = useState<QueryProgressState>({
type: 'idle',
})
const [isStreaming, setIsStreaming] = useState(false)

const preventAutoScrollRef = useRef(false)
const lastProgrammaticScrollRef = useRef<number>(0)

const activeStreamAbortControllersRef = useRef<AbortController[]>([])
const chatUserInputRefs = useRef<Map<string, ChatUserInputRef>>(new Map())
const chatMessagesRef = useRef<HTMLDivElement>(null)
Expand Down Expand Up @@ -171,6 +172,7 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
abortController.abort()
}
activeStreamAbortControllersRef.current = []
setIsStreaming(false)
}

const handleLoadConversation = async (conversationId: string) => {
Expand Down Expand Up @@ -241,6 +243,7 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
try {
const abortController = new AbortController()
activeStreamAbortControllersRef.current.push(abortController)
setIsStreaming(true)

const { requestMessages, compiledMessages } =
await promptGenerator.generateRequestMessages({
Expand Down Expand Up @@ -280,6 +283,7 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
handleScrollToBottom()
}
}
setIsStreaming(false)
} catch (error) {
if (error.name === 'AbortError') {
return
Expand Down Expand Up @@ -594,6 +598,12 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
),
)}
<QueryProgress state={queryProgress} />
{isStreaming && (
<button onClick={abortActiveStreams} className="smtcmp-stop-gen-btn">
<CircleStop size={16} />
<div>Stop Generation</div>
</button>
)}
</div>
<ChatUserInput
key={inputMessage.id} // this is needed to clear the editor when the user submits a new message
Expand Down
12 changes: 12 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,20 @@ button:not(.clickable-icon).smtcmp-chat-list-dropdown {

.smtcmp-chat-container {
display: flex;
position: relative;
flex-direction: column;
height: 100%;

.smtcmp-stop-gen-btn {
z-index: 1000;
position: absolute;
bottom: 160px;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: var(--size-4-1);
}
}

.smtcmp-chat-messages {
Expand Down

0 comments on commit 91a9361

Please sign in to comment.