Fix abort issue
This commit is contained in:
parent
ad1b3150e5
commit
fdd46fdd06
|
@ -57,7 +57,7 @@
|
|||
export let params = { chatId: '' }
|
||||
const chatId: number = parseInt(params.chatId)
|
||||
|
||||
const controller = new AbortController()
|
||||
let controller:AbortController = new AbortController()
|
||||
|
||||
let updating: boolean|number = false
|
||||
let updatingMessage: string = ''
|
||||
|
@ -380,16 +380,18 @@
|
|||
signal
|
||||
}
|
||||
|
||||
if (opts.streaming) {
|
||||
const abortListener = (e:Event) => {
|
||||
chatResponse.updateFromError('User aborted request.')
|
||||
}
|
||||
// fetchEventSource doesn't seem to throw on abort, so...
|
||||
const abortListener = (e:Event) => {
|
||||
controller = new AbortController()
|
||||
chatResponse.updateFromError('User aborted request.')
|
||||
signal.removeEventListener('abort', abortListener)
|
||||
}
|
||||
signal.addEventListener('abort', abortListener)
|
||||
|
||||
if (opts.streaming) {
|
||||
chatResponse.onFinish(() => {
|
||||
updating = false
|
||||
updatingMessage = ''
|
||||
signal.removeEventListener('abort', abortListener)
|
||||
scrollToBottom()
|
||||
})
|
||||
fetchEventSource(apiBase + '/v1/chat/completions', {
|
||||
|
|
|
@ -67,7 +67,7 @@ export class ChatCompletionResponse {
|
|||
} as Usage
|
||||
message.model = response.model
|
||||
message.finish_reason = choice.finish_reason
|
||||
message.streaming = choice.finish_reason === null
|
||||
message.streaming = choice.finish_reason === null && !this.finished
|
||||
this.messages[i] = message
|
||||
if (this.opts.autoAddMessages) addMessage(this.chat.id, message)
|
||||
})
|
||||
|
@ -85,7 +85,7 @@ export class ChatCompletionResponse {
|
|||
}
|
||||
|
||||
updateFromError (errorMessage: string): void {
|
||||
if (this.finished) return
|
||||
if (this.finished || this.error) return
|
||||
this.error = errorMessage
|
||||
if (this.opts.autoAddMessages) {
|
||||
addMessage(this.chat.id, {
|
||||
|
@ -95,11 +95,11 @@ export class ChatCompletionResponse {
|
|||
} as Message)
|
||||
}
|
||||
this.notifyMessageChange()
|
||||
this.finish()
|
||||
setTimeout(() => this.finish(), 250) // give others a chance to signal the finish first
|
||||
}
|
||||
|
||||
updateFromClose (): void {
|
||||
setTimeout(() => this.finish(), 100) // give others a chance to signal the finish first
|
||||
setTimeout(() => this.finish(), 250) // give others a chance to signal the finish first
|
||||
}
|
||||
|
||||
onMessageChange = (listener: (m: Message[]) => void): number =>
|
||||
|
|
Loading…
Reference in New Issue