From 987ff1c04adfe9d9e4a10b8b2d917a42e22e9a2a Mon Sep 17 00:00:00 2001 From: pond918 Date: Mon, 29 May 2023 21:03:31 +0800 Subject: [PATCH] refactor: chatdto.done => chatdto.code --- src/bots/chat.dto.ts | 11 ++++++++--- src/bots/huggingface/GradioBot.ts | 10 +++++----- src/storage/chat-history.ts | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/bots/chat.dto.ts b/src/bots/chat.dto.ts index ed573b9..26b926d 100644 --- a/src/bots/chat.dto.ts +++ b/src/bots/chat.dto.ts @@ -3,11 +3,16 @@ import { nanoid } from 'nanoid' export class ChatDto { /** local msg id */ readonly id?: string - readonly done?: boolean + /** status code. empty means ok; positive means still processing; negative means no more processing */ + readonly code?: number + /** + * @param code status code. empty means ok; positive means still processing; negative means no more processing + * @param options + */ constructor( public readonly prompt: string | string[], - done = true, + code = 0, public readonly options: { /** msg type: true: response, false: request */ resp?: boolean @@ -21,6 +26,6 @@ export class ChatDto { stateless?: boolean } & Record = {}, ) { - done && ((this.id = nanoid()), (this.done = true)) + !code && ((this.id = nanoid()), (this.code = 0)) } } diff --git a/src/bots/huggingface/GradioBot.ts b/src/bots/huggingface/GradioBot.ts index 45b0add..2c14958 100644 --- a/src/bots/huggingface/GradioBot.ts +++ b/src/bots/huggingface/GradioBot.ts @@ -53,11 +53,11 @@ export default abstract class GradioBot extends LLMBot { } async _sendPrompt(prompt: ChatDto, streamCallback?: (msg: ChatDto) => void): Promise { - let result: ChatDto = new ChatDto('', false) + let result: ChatDto = new ChatDto('', -1) for (const key in this._fnIndexes) { const fn_index = this._fnIndexes[key] const resp = await this._sendFnIndex(fn_index, prompt, streamCallback) - resp && resp.done && resp.prompt && (result = resp) + resp && !resp.code && resp.prompt && (result = resp) } return result } @@ -102,12 +102,12 @@ export default abstract class GradioBot extends LLMBot { if (event.rank > 0) { // Waiting in queue event.rank_eta = Math.floor(event.rank_eta) - streamCallback && streamCallback(new ChatDto('gradio.waiting', false)) + streamCallback && streamCallback(new ChatDto('gradio.waiting', 1)) } } else if (event.msg === 'process_generating') { // Generating data if (event.success && event.output.data) { - streamCallback && streamCallback(new ChatDto(this.parseData(fn_index, event.output.data), false)) + streamCallback && streamCallback(new ChatDto(this.parseData(fn_index, event.output.data), 1)) } else { reject(new Error(event.output.error)) } @@ -117,7 +117,7 @@ export default abstract class GradioBot extends LLMBot { const prompt = this.parseData(fn_index, event.output.data) const resp = new ChatDto( prompt, - fn_index == this._fnIndexes[this._fnIndexes.length - 1], // Only the last one is done + fn_index == this._fnIndexes[this._fnIndexes.length - 1] ? 0 : -1, // Only the last one is done ) streamCallback && streamCallback(resp) resolve(resp) diff --git a/src/storage/chat-history.ts b/src/storage/chat-history.ts index f8ef4d6..9f3b6cf 100644 --- a/src/storage/chat-history.ts +++ b/src/storage/chat-history.ts @@ -18,7 +18,7 @@ export class ChatHistory { // async append(msg: ChatDto & { prompt: string }) { async append(msg: ChatDto) { if (msg.options.compound) throw new Error('chat.history.append.compound.not.allowed') - if (!msg.done) throw new Error('chat.history.append.undone.not.allowed') + if (msg.code) throw new Error('chat.history.append.undone.not.allowed') let branched = false @@ -68,7 +68,7 @@ export class ChatHistory { if (!(mid = m.options.lastMsgId)) break } } - const retMsg = new ChatDto(ret.reverse(), msg.done, { ...msg.options, compound: 1 }) + const retMsg = new ChatDto(ret.reverse(), msg.code, { ...msg.options, compound: 1 }) _conversationKey && (retMsg.options._conversationKey = _conversationKey) return retMsg }