From b8e60eadf841f0c826bff7093dd717dae7e2b811 Mon Sep 17 00:00:00 2001 From: ulleo Date: Tue, 22 Jul 2025 11:51:23 +0800 Subject: [PATCH] fix: click history while chat thinking --- frontend/src/views/chat/ChatListContainer.vue | 77 ++++++++++--------- frontend/src/views/chat/RecommendQuestion.vue | 11 ++- .../src/views/chat/answer/AnalysisAnswer.vue | 8 +- .../src/views/chat/answer/ChartAnswer.vue | 12 ++- .../src/views/chat/answer/PredictAnswer.vue | 12 ++- frontend/src/views/chat/index.vue | 23 +++++- 6 files changed, 95 insertions(+), 48 deletions(-) diff --git a/frontend/src/views/chat/ChatListContainer.vue b/frontend/src/views/chat/ChatListContainer.vue index 9678fc6d..5231c6dc 100644 --- a/frontend/src/views/chat/ChatListContainer.vue +++ b/frontend/src/views/chat/ChatListContainer.vue @@ -103,54 +103,61 @@ function onChatCreated(chat: ChatInfo) { const chatCreatorRef = ref() -function goEmpty() { +function goEmpty(func?: (...p: any[]) => void, ...params: any[]) { _currentChat.value = new ChatInfo() _currentChatId.value = undefined - emits('goEmpty') + emits('goEmpty', func, ...params) } const createNewChat = async () => { - if (!_loading.value) { - goEmpty() - if (isAssistant.value) { - const assistantChat = await assistantStore.setChat() - if (assistantChat) { - onChatCreated(assistantChat) - } - return - } else { - chatCreatorRef.value?.showDs() + goEmpty(doCreateNewChat) +} + +async function doCreateNewChat() { + if (isAssistant.value) { + const assistantChat = await assistantStore.setChat() + if (assistantChat) { + onChatCreated(assistantChat) } + return + } else { + chatCreatorRef.value?.showDs() } } function onClickHistory(chat: Chat) { - if (chat !== undefined && chat.id !== undefined && !_loading.value) { - 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 - }) - } - }) + if (chat !== undefined && chat.id !== undefined) { + if (_currentChatId.value === chat.id) { + return + } + goEmpty(goHistory, chat) } } +function goHistory(chat: Chat) { + 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 && info.id === _currentChatId.value) { + _currentChat.value = info + + // scrollToBottom() + emits('onClickHistory', info) + } + }) + .finally(() => { + _loading.value = false + }) + } + }) +} + function onChatDeleted(id: number) { for (let i = 0; i < _chatList.value.length; i++) { if (_chatList.value[i].id === id) { diff --git a/frontend/src/views/chat/RecommendQuestion.vue b/frontend/src/views/chat/RecommendQuestion.vue index 31c01fbb..d239ee32 100644 --- a/frontend/src/views/chat/RecommendQuestion.vue +++ b/frontend/src/views/chat/RecommendQuestion.vue @@ -1,5 +1,5 @@ diff --git a/frontend/src/views/chat/answer/AnalysisAnswer.vue b/frontend/src/views/chat/answer/AnalysisAnswer.vue index feaf51f5..0757e9f5 100644 --- a/frontend/src/views/chat/answer/AnalysisAnswer.vue +++ b/frontend/src/views/chat/answer/AnalysisAnswer.vue @@ -1,7 +1,7 @@ diff --git a/frontend/src/views/chat/answer/ChartAnswer.vue b/frontend/src/views/chat/answer/ChartAnswer.vue index 3d9f3743..fd478e9d 100644 --- a/frontend/src/views/chat/answer/ChartAnswer.vue +++ b/frontend/src/views/chat/answer/ChartAnswer.vue @@ -2,7 +2,7 @@ import BaseAnswer from './BaseAnswer.vue' import { Chat, chatApi, ChatInfo, type ChatMessage, ChatRecord, questionApi } from '@/api/chat.ts' import { useAssistantStore } from '@/stores/assistant' -import { computed, nextTick, ref } from 'vue' +import { computed, nextTick, onBeforeUnmount, ref } from 'vue' const assistantStore = useAssistantStore() const props = withDefaults( defineProps<{ @@ -25,6 +25,7 @@ const props = withDefaults( const emits = defineEmits([ 'finish', 'error', + 'stop', 'update:loading', 'update:chatList', 'update:currentChat', @@ -102,11 +103,10 @@ const sendMessage = async () => { question: currentRecord.question, chat_id: _currentChatId.value, assistant_certificate: assistantStore.getCertificate, - controller, } console.log(assistantStore.getCertificate) console.log(param) - const response = await questionApi.add(param) + const response = await questionApi.add(param, controller) const reader = response.body.getReader() const decoder = new TextDecoder() @@ -235,7 +235,13 @@ function getChatData(recordId?: number) { function stop() { stopFlag.value = true _loading.value = false + emits('stop') } + +onBeforeUnmount(() => { + stop() +}) + defineExpose({ sendMessage, index: () => index.value, stop }) diff --git a/frontend/src/views/chat/answer/PredictAnswer.vue b/frontend/src/views/chat/answer/PredictAnswer.vue index 7aee5005..992f18c2 100644 --- a/frontend/src/views/chat/answer/PredictAnswer.vue +++ b/frontend/src/views/chat/answer/PredictAnswer.vue @@ -1,7 +1,7 @@ diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index c98681d8..bbf51f25 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -99,6 +99,7 @@ :questions="message.recommended_question" :first-chat="message.first_chat" @click-question="quickAsk" + @stop="onChatStop" /> @@ -206,6 +209,7 @@ :message="message" @finish="onAnalysisAnswerFinish" @error="onAnalysisAnswerError" + @stop="onChatStop" >
>(() => { return messages }) -const goEmpty = () => { +const goEmpty = (func?: (...p: any[]) => void, ...param: any[]) => { inputMessage.value = '' - stop() + stop(func, ...param) } const createNewChatSimple = async () => { @@ -418,8 +423,8 @@ function getChatList() { } function onClickHistory(chat: Chat) { - console.log('click history', chat) scrollToBottom() + console.debug('click history', chat) } function toAssistantHistory(chat: Chat) { @@ -510,6 +515,13 @@ async function onChartAnswerFinish(id: number) { function onChartAnswerError() { loading.value = false isTyping.value = false + console.debug('onChartAnswerError') +} + +function onChatStop() { + loading.value = false + isTyping.value = false + console.debug('onChatStop') } const sendMessage = async () => { @@ -698,7 +710,7 @@ function clickInput() { inputRef.value?.focus() } -function stop() { +function stop(func?: (...p: any[]) => void, ...param: any[]) { if (recommendQuestionRef.value) { if (recommendQuestionRef.value instanceof Array) { for (let i = 0; i < recommendQuestionRef.value.length; i++) { @@ -735,6 +747,9 @@ function stop() { predictAnswerRef.value.stop() } } + if (func && typeof func === 'function') { + func(...param) + } } onMounted(() => {