Skip to content

Commit b8e60ea

Browse files
committed
fix: click history while chat thinking
1 parent e00ca7e commit b8e60ea

File tree

6 files changed

+95
-48
lines changed

6 files changed

+95
-48
lines changed

frontend/src/views/chat/ChatListContainer.vue

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -103,54 +103,61 @@ function onChatCreated(chat: ChatInfo) {
103103
104104
const chatCreatorRef = ref()
105105
106-
function goEmpty() {
106+
function goEmpty(func?: (...p: any[]) => void, ...params: any[]) {
107107
_currentChat.value = new ChatInfo()
108108
_currentChatId.value = undefined
109-
emits('goEmpty')
109+
emits('goEmpty', func, ...params)
110110
}
111111
112112
const createNewChat = async () => {
113-
if (!_loading.value) {
114-
goEmpty()
115-
if (isAssistant.value) {
116-
const assistantChat = await assistantStore.setChat()
117-
if (assistantChat) {
118-
onChatCreated(assistantChat)
119-
}
120-
return
121-
} else {
122-
chatCreatorRef.value?.showDs()
113+
goEmpty(doCreateNewChat)
114+
}
115+
116+
async function doCreateNewChat() {
117+
if (isAssistant.value) {
118+
const assistantChat = await assistantStore.setChat()
119+
if (assistantChat) {
120+
onChatCreated(assistantChat)
123121
}
122+
return
123+
} else {
124+
chatCreatorRef.value?.showDs()
124125
}
125126
}
126127
127128
function onClickHistory(chat: Chat) {
128-
if (chat !== undefined && chat.id !== undefined && !_loading.value) {
129-
goEmpty()
130-
nextTick(() => {
131-
if (chat !== undefined && chat.id !== undefined) {
132-
_currentChat.value = new ChatInfo(chat)
133-
_currentChatId.value = chat.id
134-
_loading.value = true
135-
chatApi
136-
.get(chat.id)
137-
.then((res) => {
138-
const info = chatApi.toChatInfo(res)
139-
if (info) {
140-
_currentChat.value = info
141-
142-
// scrollToBottom()
143-
emits('onClickHistory', info)
144-
}
145-
})
146-
.finally(() => {
147-
_loading.value = false
148-
})
149-
}
150-
})
129+
if (chat !== undefined && chat.id !== undefined) {
130+
if (_currentChatId.value === chat.id) {
131+
return
132+
}
133+
goEmpty(goHistory, chat)
151134
}
152135
}
153136
137+
function goHistory(chat: Chat) {
138+
nextTick(() => {
139+
if (chat !== undefined && chat.id !== undefined) {
140+
_currentChat.value = new ChatInfo(chat)
141+
_currentChatId.value = chat.id
142+
_loading.value = true
143+
chatApi
144+
.get(chat.id)
145+
.then((res) => {
146+
const info = chatApi.toChatInfo(res)
147+
if (info && info.id === _currentChatId.value) {
148+
_currentChat.value = info
149+
150+
// scrollToBottom()
151+
emits('onClickHistory', info)
152+
}
153+
})
154+
.finally(() => {
155+
_loading.value = false
156+
})
157+
}
158+
})
159+
}
160+
154161
function onChatDeleted(id: number) {
155162
for (let i = 0; i < _chatList.value.length; i++) {
156163
if (_chatList.value[i].id === id) {

frontend/src/views/chat/RecommendQuestion.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { computed, nextTick, ref } from 'vue'
2+
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
33
import { endsWith, startsWith } from 'lodash-es'
44
import { useI18n } from 'vue-i18n'
55
import { chatApi, ChatInfo } from '@/api/chat.ts'
@@ -19,7 +19,7 @@ const props = withDefaults(
1919
}
2020
)
2121
22-
const emits = defineEmits(['clickQuestion', 'update:currentChat'])
22+
const emits = defineEmits(['clickQuestion', 'update:currentChat', 'stop'])
2323
2424
const loading = ref(false)
2525
@@ -57,7 +57,7 @@ async function getRecommendQuestions() {
5757
loading.value = true
5858
try {
5959
const controller: AbortController = new AbortController()
60-
const response = await chatApi.recommendQuestions(props.recordId)
60+
const response = await chatApi.recommendQuestions(props.recordId, controller)
6161
const reader = response.body.getReader()
6262
const decoder = new TextDecoder()
6363
@@ -138,8 +138,13 @@ async function getRecommendQuestions() {
138138
function stop() {
139139
stopFlag.value = true
140140
loading.value = false
141+
emits('stop')
141142
}
142143
144+
onBeforeUnmount(() => {
145+
stop()
146+
})
147+
143148
defineExpose({ getRecommendQuestions, id: () => props.recordId, stop })
144149
</script>
145150

frontend/src/views/chat/answer/AnalysisAnswer.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import BaseAnswer from './BaseAnswer.vue'
33
import { chatApi, ChatInfo, type ChatMessage, ChatRecord } from '@/api/chat.ts'
4-
import { computed, nextTick, ref } from 'vue'
4+
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
55
import MdComponent from '@/views/chat/component/MdComponent.vue'
66
const props = withDefaults(
77
defineProps<{
@@ -23,6 +23,7 @@ const props = withDefaults(
2323
const emits = defineEmits([
2424
'finish',
2525
'error',
26+
'stop',
2627
'update:loading',
2728
'update:chatList',
2829
'update:currentChat',
@@ -192,7 +193,12 @@ const sendMessage = async () => {
192193
function stop() {
193194
stopFlag.value = true
194195
_loading.value = false
196+
emits('stop')
195197
}
198+
199+
onBeforeUnmount(() => {
200+
stop()
201+
})
196202
defineExpose({ sendMessage, index: () => index.value, chatList: () => _chatList.value, stop })
197203
</script>
198204

frontend/src/views/chat/answer/ChartAnswer.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import BaseAnswer from './BaseAnswer.vue'
33
import { Chat, chatApi, ChatInfo, type ChatMessage, ChatRecord, questionApi } from '@/api/chat.ts'
44
import { useAssistantStore } from '@/stores/assistant'
5-
import { computed, nextTick, ref } from 'vue'
5+
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
66
const assistantStore = useAssistantStore()
77
const props = withDefaults(
88
defineProps<{
@@ -25,6 +25,7 @@ const props = withDefaults(
2525
const emits = defineEmits([
2626
'finish',
2727
'error',
28+
'stop',
2829
'update:loading',
2930
'update:chatList',
3031
'update:currentChat',
@@ -102,11 +103,10 @@ const sendMessage = async () => {
102103
question: currentRecord.question,
103104
chat_id: _currentChatId.value,
104105
assistant_certificate: assistantStore.getCertificate,
105-
controller,
106106
}
107107
console.log(assistantStore.getCertificate)
108108
console.log(param)
109-
const response = await questionApi.add(param)
109+
const response = await questionApi.add(param, controller)
110110
const reader = response.body.getReader()
111111
const decoder = new TextDecoder()
112112
@@ -235,7 +235,13 @@ function getChatData(recordId?: number) {
235235
function stop() {
236236
stopFlag.value = true
237237
_loading.value = false
238+
emits('stop')
238239
}
240+
241+
onBeforeUnmount(() => {
242+
stop()
243+
})
244+
239245
defineExpose({ sendMessage, index: () => index.value, stop })
240246
</script>
241247

frontend/src/views/chat/answer/PredictAnswer.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import BaseAnswer from './BaseAnswer.vue'
33
import { chatApi, ChatInfo, type ChatMessage, ChatRecord } from '@/api/chat.ts'
4-
import { computed, nextTick, ref } from 'vue'
4+
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
55
import MdComponent from '@/views/chat/component/MdComponent.vue'
66
const props = withDefaults(
77
defineProps<{
@@ -23,6 +23,7 @@ const props = withDefaults(
2323
const emits = defineEmits([
2424
'finish',
2525
'error',
26+
'stop',
2627
'update:loading',
2728
'update:chatList',
2829
'update:currentChat',
@@ -95,7 +96,7 @@ const sendMessage = async () => {
9596
9697
try {
9798
const controller: AbortController = new AbortController()
98-
const response = await chatApi.predict(currentRecord.predict_record_id)
99+
const response = await chatApi.predict(currentRecord.predict_record_id, controller)
99100
const reader = response.body.getReader()
100101
const decoder = new TextDecoder()
101102
@@ -195,10 +196,17 @@ const sendMessage = async () => {
195196
_loading.value = false
196197
}
197198
}
199+
198200
function stop() {
199201
stopFlag.value = true
200202
_loading.value = false
203+
emits('stop')
201204
}
205+
206+
onBeforeUnmount(() => {
207+
stop()
208+
})
209+
202210
defineExpose({ sendMessage, index: () => index.value, chatList: () => _chatList, stop })
203211
</script>
204212

frontend/src/views/chat/index.vue

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
:questions="message.recommended_question"
100100
:first-chat="message.first_chat"
101101
@click-question="quickAsk"
102+
@stop="onChatStop"
102103
/>
103104
<UserChat v-if="message.role === 'user'" :message="message" />
104105
<template v-if="message.role === 'assistant' && !message.first_chat">
@@ -118,6 +119,7 @@
118119
:reasoning-name="['sql_answer', 'chart_answer']"
119120
@finish="onChartAnswerFinish"
120121
@error="onChartAnswerError"
122+
@stop="onChatStop"
121123
>
122124
<ChartBlock style="margin-top: 12px" :message="message" />
123125
<div
@@ -190,6 +192,7 @@
190192
:questions="message.recommended_question"
191193
:first-chat="message.first_chat"
192194
@click-question="quickAsk"
195+
@stop="onChatStop"
193196
/>
194197
</template>
195198
</ChartAnswer>
@@ -206,6 +209,7 @@
206209
:message="message"
207210
@finish="onAnalysisAnswerFinish"
208211
@error="onAnalysisAnswerError"
212+
@stop="onChatStop"
209213
>
210214
<div
211215
v-if="message.record?.error && message.record?.error?.trim().length > 0"
@@ -230,6 +234,7 @@
230234
:message="message"
231235
@finish="onPredictAnswerFinish"
232236
@error="onPredictAnswerError"
237+
@stop="onChatStop"
233238
>
234239
<ChartBlock style="margin-top: 12px" :message="message" is-predict />
235240
<div
@@ -382,9 +387,9 @@ const computedMessages = computed<Array<ChatMessage>>(() => {
382387
return messages
383388
})
384389
385-
const goEmpty = () => {
390+
const goEmpty = (func?: (...p: any[]) => void, ...param: any[]) => {
386391
inputMessage.value = ''
387-
stop()
392+
stop(func, ...param)
388393
}
389394
390395
const createNewChatSimple = async () => {
@@ -418,8 +423,8 @@ function getChatList() {
418423
}
419424
420425
function onClickHistory(chat: Chat) {
421-
console.log('click history', chat)
422426
scrollToBottom()
427+
console.debug('click history', chat)
423428
}
424429
425430
function toAssistantHistory(chat: Chat) {
@@ -510,6 +515,13 @@ async function onChartAnswerFinish(id: number) {
510515
function onChartAnswerError() {
511516
loading.value = false
512517
isTyping.value = false
518+
console.debug('onChartAnswerError')
519+
}
520+
521+
function onChatStop() {
522+
loading.value = false
523+
isTyping.value = false
524+
console.debug('onChatStop')
513525
}
514526
515527
const sendMessage = async () => {
@@ -698,7 +710,7 @@ function clickInput() {
698710
inputRef.value?.focus()
699711
}
700712
701-
function stop() {
713+
function stop(func?: (...p: any[]) => void, ...param: any[]) {
702714
if (recommendQuestionRef.value) {
703715
if (recommendQuestionRef.value instanceof Array) {
704716
for (let i = 0; i < recommendQuestionRef.value.length; i++) {
@@ -735,6 +747,9 @@ function stop() {
735747
predictAnswerRef.value.stop()
736748
}
737749
}
750+
if (func && typeof func === 'function') {
751+
func(...param)
752+
}
738753
}
739754
740755
onMounted(() => {

0 commit comments

Comments
 (0)