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

Add stop button for streaming responses #64

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 19 additions & 9 deletions components/input/InputBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { InputSettings } from "./InputSettings";

interface InputBarProps {
onSubmit: (content: string) => void;
onStop: () => void;
isLoading: boolean;
}

const MAX_CONTENT_LENGTH = 9000;

export const InputBar: React.FC<InputBarProps> = ({ isLoading, onSubmit }) => {
export const InputBar: React.FC<InputBarProps> = ({ isLoading, onSubmit, onStop }) => {
const [input, setInput] = useState("");
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [isHovering, setIsHovering] = useState(false);
const inputRef = useRef<HTMLTextAreaElement>(null);

const handleInputChange = useCallback(
Expand Down Expand Up @@ -74,14 +76,22 @@ export const InputBar: React.FC<InputBarProps> = ({ isLoading, onSubmit }) => {
{isLoading ? (
<button
type="button"
disabled
className="inline-flex items-center justify-center h-8 w-8 rounded-md bg-black text-white cursor-not-allowed"
aria-label="Loading"
onClick={onStop}
onMouseEnter={() => setIsHovering(true)}
onMouseLeave={() => setIsHovering(false)}
className="inline-flex items-center justify-center h-8 w-8 rounded-md bg-black text-white cursor-pointer"
aria-label={isHovering ? "Stop" : "Loading"}
>
<svg className="animate-spin h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
{isHovering ? (
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 256 256">
<path d="M208,32H48A16,16,0,0,0,32,48V208a16,16,0,0,0,16,16H208a16,16,0,0,0,16-16V48A16,16,0,0,0,208,32Z"></path>
</svg>
) : (
<svg className="animate-spin h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
)}
</button>
) : (
<button
Expand All @@ -103,4 +113,4 @@ export const InputBar: React.FC<InputBarProps> = ({ isLoading, onSubmit }) => {
</form>
</div>
);
};
};
6 changes: 6 additions & 0 deletions hooks/chat/useChatCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ export function useChat({ propsId, onTitleUpdate }: { propsId?: Id<"threads">, o
}
}, [threadId, user, vercelChatProps, threadData, addMessageToThread, sendMessageMutation, model])

const stopGenerating = useCallback(() => {
vercelChatProps.stop()
toast.success('Message generation stopped.')
}, [vercelChatProps])

return {
...vercelChatProps,
messages: debouncedMessages,
Expand All @@ -167,6 +172,7 @@ export function useChat({ propsId, onTitleUpdate }: { propsId?: Id<"threads">, o
user,
setCurrentThreadId,
sendMessage,
stopGenerating,
error,
}
}
5 changes: 3 additions & 2 deletions panes/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const Chat = ({ threadId, className }: ChatProps) => {
messages,
sendMessage,
isLoading,
stopGenerating
} = useChat({ propsId: threadId })
const chatContainerRef = useChatScroll(messages);

Expand All @@ -25,9 +26,9 @@ export const Chat = ({ threadId, className }: ChatProps) => {
</div>
<div className="flex-shrink-0 w-full">
<div className="sticky bottom-0 w-full">
<InputBar onSubmit={sendMessage} isLoading={isLoading} />
<InputBar onSubmit={sendMessage} isLoading={isLoading} onStop={stopGenerating} />
</div>
</div>
</div>
)
}
}
Loading