global sse AbortController

This commit is contained in:
josc146 2023-06-14 20:06:19 +08:00
parent 8976764ee5
commit 4b640f884b
2 changed files with 11 additions and 9 deletions

View File

@ -43,13 +43,14 @@ export type Conversations = {
[uuid: string]: MessageItem
}
let chatSseController: AbortController | null = null;
const ChatPanel: FC = observer(() => {
const { t } = useTranslation();
const [message, setMessage] = useState('');
const bodyRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
const sseControllerRef = useRef<AbortController | null>(null);
let lastMessageId: string;
let generating: boolean = false;
@ -150,7 +151,7 @@ const ChatPanel: FC = observer(() => {
commonStore.setConversationsOrder(commonStore.conversationsOrder);
setTimeout(scrollToBottom);
let answer = '';
sseControllerRef.current = new AbortController();
chatSseController = new AbortController();
fetchEventSource(`http://127.0.0.1:${port}/chat/completions`, // https://api.openai.com/v1/chat/completions || http://127.0.0.1:${port}/chat/completions
{
method: 'POST',
@ -163,7 +164,7 @@ const ChatPanel: FC = observer(() => {
stream: true,
model: 'gpt-3.5-turbo'
}),
signal: sseControllerRef.current?.signal,
signal: chatSseController?.signal,
onmessage(e) {
console.log('sse message', e);
scrollToBottom();
@ -256,7 +257,7 @@ const ChatPanel: FC = observer(() => {
size="large" shape="circular" appearance="subtle"
onClick={(e) => {
if (generating)
sseControllerRef.current?.abort();
chatSseController?.abort();
commonStore.setConversations({});
commonStore.setConversationsOrder([]);
}}
@ -275,7 +276,7 @@ const ChatPanel: FC = observer(() => {
size="large" shape="circular" appearance="subtle"
onClick={(e) => {
if (generating) {
sseControllerRef.current?.abort();
chatSseController?.abort();
if (lastMessageId) {
commonStore.conversations[lastMessageId].type = MessageType.Error;
commonStore.conversations[lastMessageId].done = true;

View File

@ -129,11 +129,12 @@ export const defaultPresets: CompletionPreset[] = [{
}
}];
let completionSseController: AbortController | null = null;
const CompletionPanel: FC = observer(() => {
const { t } = useTranslation();
const inputRef = useRef<HTMLTextAreaElement>(null);
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
const sseControllerRef = useRef<AbortController | null>(null);
const scrollToBottom = () => {
if (inputRef.current)
@ -187,7 +188,7 @@ const CompletionPanel: FC = observer(() => {
prompt += params.injectStart.replaceAll('\\n', '\n');
let answer = '';
sseControllerRef.current = new AbortController();
completionSseController = new AbortController();
fetchEventSource(`http://127.0.0.1:${port}/completions`, // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/completions
{
method: 'POST',
@ -206,7 +207,7 @@ const CompletionPanel: FC = observer(() => {
frequency_penalty: params.frequencyPenalty,
stop: params.stop.replaceAll('\\n', '\n') || undefined
}),
signal: sseControllerRef.current?.signal,
signal: completionSseController?.signal,
onmessage(e) {
console.log('sse message', e);
scrollToBottom();
@ -352,7 +353,7 @@ const CompletionPanel: FC = observer(() => {
}}>{t('Reset')}</Button>
<Button className="grow" appearance="primary" onClick={() => {
if (commonStore.completionGenerating) {
sseControllerRef.current?.abort();
completionSseController?.abort();
commonStore.setCompletionGenerating(false);
} else {
commonStore.setCompletionGenerating(true);