From 4bb4b7c2dde7350c0e5f6b07ab772e28cd6c9e1e Mon Sep 17 00:00:00 2001 From: shaohuzhang1 Date: Thu, 30 Nov 2023 17:12:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9=E8=AF=9D=E6=9A=82=E5=81=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/src/api/type/application.ts | 105 +++++++++++++++++- ui/src/components/ai-dialog/index.vue | 62 ++++++++--- ui/src/components/markdown-renderer/index.vue | 36 +++++- 3 files changed, 180 insertions(+), 23 deletions(-) diff --git a/ui/src/api/type/application.ts b/ui/src/api/type/application.ts index d6f3ccfcb..65605fbf5 100644 --- a/ui/src/api/type/application.ts +++ b/ui/src/api/type/application.ts @@ -1,3 +1,4 @@ +import { type Dict } from '@/api/type/common' import { type Ref } from 'vue' interface ApplicationFormType { name?: string @@ -13,20 +14,38 @@ interface chatType { problem_text: string answer_text: string buffer: Array + /** + * 是否写入结束 + */ + write_ed?: boolean + /** + * 是否暂停 + */ + is_stop?: boolean } -export class ChatManage { +export class ChatRecordManage { id?: NodeJS.Timer ms: number chat: chatType is_close?: boolean + write_ed?: boolean + is_stop?: boolean loading?: Ref constructor(chat: chatType, ms?: number, loading?: Ref) { this.ms = ms ? ms : 10 this.chat = chat this.loading = loading + this.is_stop = false + this.is_close = false + this.write_ed = false } write() { + this.chat.is_stop = false + this.is_stop = false + if (this.loading) { + this.loading.value = true + } this.id = setInterval(() => { const s = this.chat.buffer.shift() if (s !== undefined) { @@ -34,6 +53,8 @@ export class ChatManage { } else { if (this.is_close) { clearInterval(this.id) + this.chat.write_ed = true + this.write_ed = true if (this.loading) { this.loading.value = false } @@ -41,6 +62,14 @@ export class ChatManage { } }, this.ms) } + stop() { + clearInterval(this.id) + this.is_stop = true + this.chat.is_stop = true + if (this.loading) { + this.loading.value = false + } + } close() { this.is_close = true } @@ -50,4 +79,78 @@ export class ChatManage { } } } + +export class ChatManagement { + static chatMessageContainer: Dict = {} + + static addChatRecord(chat: chatType, ms: number, loading?: Ref) { + this.chatMessageContainer[chat.id] = new ChatRecordManage(chat, ms, loading) + } + static append(chatRecordId: string, content: string) { + const chatRecord = this.chatMessageContainer[chatRecordId] + if (chatRecord) { + chatRecord.append(content) + } + } + /** + * 持续从缓存区 写出数据 + * @param chatRecordId 对话记录id + */ + static write(chatRecordId: string) { + const chatRecord = this.chatMessageContainer[chatRecordId] + if (chatRecord) { + chatRecord.write() + } + } + /** + * 等待所有数据输出完毕后 才会关闭流 + * @param chatRecordId 对话记录id + * @returns boolean + */ + static close(chatRecordId: string) { + const chatRecord = this.chatMessageContainer[chatRecordId] + if (chatRecord) { + chatRecord.close() + } + } + /** + * 停止输出 立即关闭定时任务输出 + * @param chatRecordId 对话记录id + * @returns boolean + */ + static stop(chatRecordId: string) { + const chatRecord = this.chatMessageContainer[chatRecordId] + if (chatRecord) { + chatRecord.stop() + } + } + /** + * 判断是否输出完成 + * @param chatRecordId 对话记录id + * @returns boolean + */ + static isClose(chatRecordId: string) { + const chatRecord = this.chatMessageContainer[chatRecordId] + return chatRecord ? chatRecord.is_close && chatRecord.write_ed : false + } + /** + * 判断是否停止输出 + * @param chatRecordId 对话记录id + * @returns + */ + static isStop(chatRecordId: string) { + const chatRecord = this.chatMessageContainer[chatRecordId] + return chatRecord ? chatRecord.is_stop : false + } + /** + * 清除无用数据 也就是被close掉的和stop的数据 + */ + static clean() { + for (const key in Object.keys(this.chatMessageContainer)) { + if (this.chatMessageContainer[key].is_close) { + delete this.chatMessageContainer[key] + } + } + } +} export type { ApplicationFormType, chatType } diff --git a/ui/src/components/ai-dialog/index.vue b/ui/src/components/ai-dialog/index.vue index 471331977..f6a12ec30 100644 --- a/ui/src/components/ai-dialog/index.vue +++ b/ui/src/components/ai-dialog/index.vue @@ -59,9 +59,27 @@ 回答中... - + - 停止回答 + 继续 + 停止回答 @@ -90,7 +108,7 @@ +