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 [uuid: string]: MessageItem
} }
let chatSseController: AbortController | null = null;
const ChatPanel: FC = observer(() => { const ChatPanel: FC = observer(() => {
const { t } = useTranslation(); const { t } = useTranslation();
const [message, setMessage] = useState(''); const [message, setMessage] = useState('');
const bodyRef = useRef<HTMLDivElement>(null); const bodyRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null); const inputRef = useRef<HTMLTextAreaElement>(null);
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort; const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
const sseControllerRef = useRef<AbortController | null>(null);
let lastMessageId: string; let lastMessageId: string;
let generating: boolean = false; let generating: boolean = false;
@ -150,7 +151,7 @@ const ChatPanel: FC = observer(() => {
commonStore.setConversationsOrder(commonStore.conversationsOrder); commonStore.setConversationsOrder(commonStore.conversationsOrder);
setTimeout(scrollToBottom); setTimeout(scrollToBottom);
let answer = ''; 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 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', method: 'POST',
@ -163,7 +164,7 @@ const ChatPanel: FC = observer(() => {
stream: true, stream: true,
model: 'gpt-3.5-turbo' model: 'gpt-3.5-turbo'
}), }),
signal: sseControllerRef.current?.signal, signal: chatSseController?.signal,
onmessage(e) { onmessage(e) {
console.log('sse message', e); console.log('sse message', e);
scrollToBottom(); scrollToBottom();
@ -256,7 +257,7 @@ const ChatPanel: FC = observer(() => {
size="large" shape="circular" appearance="subtle" size="large" shape="circular" appearance="subtle"
onClick={(e) => { onClick={(e) => {
if (generating) if (generating)
sseControllerRef.current?.abort(); chatSseController?.abort();
commonStore.setConversations({}); commonStore.setConversations({});
commonStore.setConversationsOrder([]); commonStore.setConversationsOrder([]);
}} }}
@ -275,7 +276,7 @@ const ChatPanel: FC = observer(() => {
size="large" shape="circular" appearance="subtle" size="large" shape="circular" appearance="subtle"
onClick={(e) => { onClick={(e) => {
if (generating) { if (generating) {
sseControllerRef.current?.abort(); chatSseController?.abort();
if (lastMessageId) { if (lastMessageId) {
commonStore.conversations[lastMessageId].type = MessageType.Error; commonStore.conversations[lastMessageId].type = MessageType.Error;
commonStore.conversations[lastMessageId].done = true; 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 CompletionPanel: FC = observer(() => {
const { t } = useTranslation(); const { t } = useTranslation();
const inputRef = useRef<HTMLTextAreaElement>(null); const inputRef = useRef<HTMLTextAreaElement>(null);
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort; const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
const sseControllerRef = useRef<AbortController | null>(null);
const scrollToBottom = () => { const scrollToBottom = () => {
if (inputRef.current) if (inputRef.current)
@ -187,7 +188,7 @@ const CompletionPanel: FC = observer(() => {
prompt += params.injectStart.replaceAll('\\n', '\n'); prompt += params.injectStart.replaceAll('\\n', '\n');
let answer = ''; 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 fetchEventSource(`http://127.0.0.1:${port}/completions`, // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/completions
{ {
method: 'POST', method: 'POST',
@ -206,7 +207,7 @@ const CompletionPanel: FC = observer(() => {
frequency_penalty: params.frequencyPenalty, frequency_penalty: params.frequencyPenalty,
stop: params.stop.replaceAll('\\n', '\n') || undefined stop: params.stop.replaceAll('\\n', '\n') || undefined
}), }),
signal: sseControllerRef.current?.signal, signal: completionSseController?.signal,
onmessage(e) { onmessage(e) {
console.log('sse message', e); console.log('sse message', e);
scrollToBottom(); scrollToBottom();
@ -352,7 +353,7 @@ const CompletionPanel: FC = observer(() => {
}}>{t('Reset')}</Button> }}>{t('Reset')}</Button>
<Button className="grow" appearance="primary" onClick={() => { <Button className="grow" appearance="primary" onClick={() => {
if (commonStore.completionGenerating) { if (commonStore.completionGenerating) {
sseControllerRef.current?.abort(); completionSseController?.abort();
commonStore.setCompletionGenerating(false); commonStore.setCompletionGenerating(false);
} else { } else {
commonStore.setCompletionGenerating(true); commonStore.setCompletionGenerating(true);