Don't allow a prompt to be sent while streaming.

This commit is contained in:
Webifi 2023-06-07 08:10:13 -05:00
parent 15272de1d4
commit b21bba3dfa
2 changed files with 20 additions and 12 deletions

View File

@ -57,7 +57,7 @@
export let params = { chatId: '' } export let params = { chatId: '' }
const chatId: number = parseInt(params.chatId) const chatId: number = parseInt(params.chatId)
let updating: boolean = false let updating: boolean|number = false
let updatingMessage: string = '' let updatingMessage: string = ''
let input: HTMLTextAreaElement let input: HTMLTextAreaElement
let recognition: any = null let recognition: any = null
@ -370,11 +370,12 @@
} }
if (opts.streaming) { if (opts.streaming) {
chatResponse.onFinish(() => { updating = false; updatingMessage = '' })
fetchEventSource(apiBase + '/v1/chat/completions', { fetchEventSource(apiBase + '/v1/chat/completions', {
...fetchOptions, ...fetchOptions,
onmessage (ev) { onmessage (ev) {
// Remove updating indicator // Remove updating indicator
updating = false updating = 1 // hide indicator, but still signal we're updating
updatingMessage = '' updatingMessage = ''
if (!chatResponse.hasFinished()) { if (!chatResponse.hasFinished()) {
// console.log('ev.data', ev.data) // console.log('ev.data', ev.data)
@ -403,12 +404,10 @@
chatResponse.updateFromSyncResponse(json) chatResponse.updateFromSyncResponse(json)
} }
} catch (e) { } catch (e) {
chatResponse.updateFromError(e.message)
}
// Hide updating bar
updating = false updating = false
updatingMessage = '' updatingMessage = ''
chatResponse.updateFromError(e.message)
}
return chatResponse return chatResponse
} }
@ -559,7 +558,7 @@
<Messages messages={chat.messages} chatId={chatId} /> <Messages messages={chat.messages} chatId={chatId} />
{#if updating} {#if updating === true}
<article class="message is-success assistant-message"> <article class="message is-success assistant-message">
<div class="message-body content"> <div class="message-body content">
<span class="is-loading" ></span> <span class="is-loading" ></span>
@ -603,7 +602,7 @@
<button title="Queue message, don't send yet" class="button is-ghost" on:click|preventDefault={addNewMessage}><span class="icon"><Fa icon={faArrowUpFromBracket} /></span></button> <button title="Queue message, don't send yet" class="button is-ghost" on:click|preventDefault={addNewMessage}><span class="icon"><Fa icon={faArrowUpFromBracket} /></span></button>
</p> </p>
<p class="control send"> <p class="control send">
<button title="Send" class="button is-info" type="submit"><span class="icon"><Fa icon={faPaperPlane} /></span></button> <button title="Send" class="button is-info" class:is-disabled={updating} type="submit"><span class="icon"><Fa icon={faPaperPlane} /></span></button>
</p> </p>
</form> </form>
<!-- a target to scroll to --> <!-- a target to scroll to -->

View File

@ -20,7 +20,6 @@ export class ChatCompletionResponse {
private messages: Message[] private messages: Message[]
private error: string private error: string
private didFinish: boolean
private finishResolver: (value: Message[]) => void private finishResolver: (value: Message[]) => void
private errorResolver: (error: string) => void private errorResolver: (error: string) => void
@ -32,6 +31,7 @@ export class ChatCompletionResponse {
private promptTokenCount:number private promptTokenCount:number
private finished = false private finished = false
private messageChangeListeners: ((m: Message[]) => void)[] = [] private messageChangeListeners: ((m: Message[]) => void)[] = []
private finishListeners: ((m: Message[]) => void)[] = []
setPromptTokenCount (tokens:number) { setPromptTokenCount (tokens:number) {
this.promptTokenCount = tokens this.promptTokenCount = tokens
@ -100,6 +100,9 @@ export class ChatCompletionResponse {
onMessageChange = (listener: (m: Message[]) => void): number => onMessageChange = (listener: (m: Message[]) => void): number =>
this.messageChangeListeners.push(listener) this.messageChangeListeners.push(listener)
onFinish = (listener: (m: Message[]) => void): number =>
this.finishListeners.push(listener)
promiseToFinish = (): Promise<Message[]> => this.finishPromise promiseToFinish = (): Promise<Message[]> => this.finishPromise
hasFinished = (): boolean => this.finished hasFinished = (): boolean => this.finished
@ -114,14 +117,20 @@ export class ChatCompletionResponse {
}) })
} }
private notifyFinish (): void {
this.finishListeners.forEach((listener) => {
listener(this.messages)
})
}
private finish = (): void => { private finish = (): void => {
if (this.didFinish) return if (this.finished) return
this.didFinish = true this.finished = true
const message = this.messages[0] const message = this.messages[0]
if (message) { if (message) {
updateRunningTotal(this.chat.id, message.usage as any, message.model as any) updateRunningTotal(this.chat.id, message.usage as any, message.model as any)
} }
this.finished = true this.notifyFinish()
if (this.error) { if (this.error) {
this.errorResolver(this.error) this.errorResolver(this.error)
} else { } else {