From 6e849121bc0e7811574aa6aaf963e006027a91b7 Mon Sep 17 00:00:00 2001 From: ulleo Date: Mon, 21 Jul 2025 18:46:14 +0800 Subject: [PATCH] feat: disable change & create chat during chat thinking --- frontend/src/api/chat.ts | 15 ++--- frontend/src/utils/request.ts | 3 +- frontend/src/views/chat/ChatListContainer.vue | 57 +++++++++++-------- frontend/src/views/chat/RecommendQuestion.vue | 17 +++++- .../src/views/chat/answer/AnalysisAnswer.vue | 20 +++++-- .../src/views/chat/answer/ChartAnswer.vue | 19 ++++++- .../src/views/chat/answer/PredictAnswer.vue | 18 +++++- frontend/src/views/chat/index.vue | 57 ++++++++++++++++--- 8 files changed, 153 insertions(+), 53 deletions(-) diff --git a/frontend/src/api/chat.ts b/frontend/src/api/chat.ts index 8fd367ed..64d9044a 100644 --- a/frontend/src/api/chat.ts +++ b/frontend/src/api/chat.ts @@ -10,7 +10,8 @@ export const questionApi = { }}).catch(e => reject(e)) }), */ // add: (data: any) => request.post('/chat/question', data), - add: (data: any) => request.fetchStream('/chat/question', data), + add: (data: any, controller?: AbortController) => + request.fetchStream('/chat/question', data, controller), edit: (data: any) => request.put('/chat/question', data), delete: (id: number) => request.delete(`/chat/question/${id}`), query: (id: number) => request.get(`/chat/question/${id}`), @@ -312,13 +313,13 @@ export const chatApi = { deleteChat: (id: number | undefined): Promise => { return request.get(`/chat/delete/${id}`) }, - analysis: (record_id: number | undefined) => { - return request.fetchStream(`/chat/record/${record_id}/analysis`, {}) + analysis: (record_id: number | undefined, controller?: AbortController) => { + return request.fetchStream(`/chat/record/${record_id}/analysis`, {}, controller) }, - predict: (record_id: number | undefined) => { - return request.fetchStream(`/chat/record/${record_id}/predict`, {}) + predict: (record_id: number | undefined, controller?: AbortController) => { + return request.fetchStream(`/chat/record/${record_id}/predict`, {}, controller) }, - recommendQuestions: (record_id: number | undefined) => { - return request.fetchStream(`/chat/recommend_questions/${record_id}`, {}) + recommendQuestions: (record_id: number | undefined, controller?: AbortController) => { + return request.fetchStream(`/chat/recommend_questions/${record_id}`, {}, controller) }, } diff --git a/frontend/src/utils/request.ts b/frontend/src/utils/request.ts index 39000d95..479c948e 100644 --- a/frontend/src/utils/request.ts +++ b/frontend/src/utils/request.ts @@ -228,7 +228,7 @@ class HttpService { return this.request({ ...config, method: 'POST', url, data }) } - public fetchStream(url: string, data?: any): Promise { + public fetchStream(url: string, data?: any, controller?: AbortController): Promise { const token = wsCache.get('user.token') const heads: any = { 'Content-Type': 'application/json', @@ -250,6 +250,7 @@ class HttpService { method: 'POST', headers: heads, body: JSON.stringify(data), + signal: controller?.signal, }) } diff --git a/frontend/src/views/chat/ChatListContainer.vue b/frontend/src/views/chat/ChatListContainer.vue index edda76cf..9678fc6d 100644 --- a/frontend/src/views/chat/ChatListContainer.vue +++ b/frontend/src/views/chat/ChatListContainer.vue @@ -2,7 +2,7 @@ import { Search } from '@element-plus/icons-vue' import ChatList from '@/views/chat/ChatList.vue' import { useI18n } from 'vue-i18n' -import { computed, ref } from 'vue' +import { computed, nextTick, ref } from 'vue' import { Chat, chatApi, ChatInfo } from '@/api/chat.ts' import { filter, includes } from 'lodash-es' import ChatCreator from '@/views/chat/ChatCreator.vue' @@ -110,37 +110,44 @@ function goEmpty() { } const createNewChat = async () => { - goEmpty() - if (isAssistant.value) { - const assistantChat = await assistantStore.setChat() - if (assistantChat) { - onChatCreated(assistantChat) + if (!_loading.value) { + goEmpty() + if (isAssistant.value) { + const assistantChat = await assistantStore.setChat() + if (assistantChat) { + onChatCreated(assistantChat) + } + return + } else { + chatCreatorRef.value?.showDs() } - return - } else { - chatCreatorRef.value?.showDs() } } function onClickHistory(chat: Chat) { - _currentChat.value = new ChatInfo(chat) if (chat !== undefined && chat.id !== undefined && !_loading.value) { - _currentChatId.value = chat.id - _loading.value = true - chatApi - .get(chat.id) - .then((res) => { - const info = chatApi.toChatInfo(res) - if (info) { - _currentChat.value = info + goEmpty() + nextTick(() => { + if (chat !== undefined && chat.id !== undefined) { + _currentChat.value = new ChatInfo(chat) + _currentChatId.value = chat.id + _loading.value = true + chatApi + .get(chat.id) + .then((res) => { + const info = chatApi.toChatInfo(res) + if (info) { + _currentChat.value = info - // scrollToBottom() - emits('onClickHistory', info) - } - }) - .finally(() => { - _loading.value = false - }) + // scrollToBottom() + emits('onClickHistory', info) + } + }) + .finally(() => { + _loading.value = false + }) + } + }) } } diff --git a/frontend/src/views/chat/RecommendQuestion.vue b/frontend/src/views/chat/RecommendQuestion.vue index 6cf0a025..31c01fbb 100644 --- a/frontend/src/views/chat/RecommendQuestion.vue +++ b/frontend/src/views/chat/RecommendQuestion.vue @@ -50,14 +50,24 @@ function clickQuestion(question: string): void { emits('clickQuestion', question) } +const stopFlag = ref(false) + async function getRecommendQuestions() { + stopFlag.value = false loading.value = true try { + const controller: AbortController = new AbortController() const response = await chatApi.recommendQuestions(props.recordId) const reader = response.body.getReader() const decoder = new TextDecoder() while (true) { + if (stopFlag.value) { + controller.abort() + loading.value = false + break + } + const { done, value } = await reader.read() if (done) { break @@ -125,7 +135,12 @@ async function getRecommendQuestions() { } } -defineExpose({ getRecommendQuestions, id: () => props.recordId }) +function stop() { + stopFlag.value = true + loading.value = false +} + +defineExpose({ getRecommendQuestions, id: () => props.recordId, stop })