Skip to content

Commit 6874409

Browse files
committed
can stop fetch
1 parent c28d266 commit 6874409

File tree

5 files changed

+285
-231
lines changed

5 files changed

+285
-231
lines changed

components/Chat/Chat.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const Chat = memo(({stopConversationRef}: Props) => {
4141
modelError,
4242
loading,
4343
prompts,
44+
controller
4445
},
4546
handleUpdateConversation,
4647
dispatch: homeDispatch,
@@ -219,7 +220,9 @@ export const Chat = memo(({stopConversationRef}: Props) => {
219220
if (isStream) {
220221
queryDoneRef.current = false;
221222
let lastTime = new Date().getTime();
222-
let response: ReadableStream<Uint8Array> | null = await ChatStream(model, promptToSend, temperatureToUse, api, key, messagesToSend);
223+
const abortController = new AbortController();
224+
homeDispatch({field: 'controller', value: abortController});
225+
let response: ReadableStream<Uint8Array> | null = await ChatStream(model, promptToSend, temperatureToUse, api, key, messagesToSend, abortController);
223226
if (response) {
224227
let notFinishData = "";
225228
const decoder = new TextDecoder();

components/Chat/ChatInput.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {IconArrowDown, IconBolt, IconSend, IconUpload, IconWriting} from '@tabler/icons-react';
1+
import {IconArrowDown, IconBolt, IconSend, IconSendOff, IconUpload, IconWriting} from '@tabler/icons-react';
22
import React, {KeyboardEvent, MutableRefObject, useCallback, useContext, useEffect, useRef, useState,} from 'react';
33

44
import {useTranslation} from 'next-i18next';
@@ -34,7 +34,7 @@ export const ChatInput = ({
3434
const {t} = useTranslation('chat');
3535

3636
const {
37-
state: {selectedConversation, messageIsStreaming, prompts},
37+
state: {selectedConversation, messageIsStreaming, prompts, controller},
3838

3939
dispatch: homeDispatch,
4040
} = useContext(HomeContext);
@@ -75,6 +75,13 @@ export const ChatInput = ({
7575
updatePromptListVisibility(value);
7676
};
7777

78+
const stopSend = () => {
79+
if (messageIsStreaming && controller) {
80+
controller.abort();
81+
homeDispatch({field: 'messageIsStreaming', value: false});
82+
}
83+
}
84+
7885
const handleSend = () => {
7986
let thisList: string[] = imageSrcList
8087
if (urlInputShow) {
@@ -448,11 +455,10 @@ export const ChatInput = ({
448455

449456
<button
450457
className="absolute right-2 top-2 rounded-sm p-1 text-neutral-800 opacity-60 hover:bg-neutral-200 hover:text-neutral-900 dark:bg-opacity-50 dark:text-neutral-100 dark:hover:text-neutral-200"
451-
onClick={handleSend}
458+
onClick={messageIsStreaming ? stopSend : handleSend}
452459
>
453460
{messageIsStreaming ? (
454-
<div
455-
className="h-4 w-4 animate-spin rounded-full border-t-2 border-neutral-800 opacity-60 dark:border-neutral-100"></div>
461+
<IconSendOff size={18}/>
456462
) : (
457463
<IconSend size={18}/>
458464
)}

pages/api/home/home.state.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface HomeInitialState {
2929
defaultModelId: OpenAIModelID | undefined;
3030
serverSideApiKeyIsSet: boolean;
3131
serverSidePluginKeysSet: boolean;
32+
controller: AbortController | null;
3233
}
3334

3435
export const initialState: HomeInitialState = {
@@ -55,4 +56,5 @@ export const initialState: HomeInitialState = {
5556
defaultModelId: undefined,
5657
serverSideApiKeyIsSet: false,
5758
serverSidePluginKeysSet: false,
59+
controller: null
5860
};

utils/server/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export const ChatStream = async (
2121
temperature: number,
2222
api: string,
2323
key: string,
24-
messages: Message[]
24+
messages: Message[],
25+
abortController: AbortController | null
2526
) => {
2627
let finalMessage
2728
let queryUrl = `${api}/v1/chat/completions`;
@@ -47,6 +48,7 @@ export const ChatStream = async (
4748
messages: finalMessage,
4849
stream: true
4950
}),
51+
signal: abortController ? abortController.signal : null
5052
});
5153
return res.body;
5254
}

0 commit comments

Comments
 (0)