Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add stop generation button during AI responses #109

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
glowingjade marked this conversation as resolved.
Show resolved Hide resolved

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
Loading