Skip to content

Commit 6e84912

Browse files
committed
feat: disable change & create chat during chat thinking
1 parent e6148d1 commit 6e84912

File tree

8 files changed

+154
-54
lines changed

8 files changed

+154
-54
lines changed

frontend/src/api/chat.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export const questionApi = {
1010
}}).catch(e => reject(e))
1111
}), */
1212
// add: (data: any) => request.post('/chat/question', data),
13-
add: (data: any) => request.fetchStream('/chat/question', data),
13+
add: (data: any, controller?: AbortController) =>
14+
request.fetchStream('/chat/question', data, controller),
1415
edit: (data: any) => request.put('/chat/question', data),
1516
delete: (id: number) => request.delete(`/chat/question/${id}`),
1617
query: (id: number) => request.get(`/chat/question/${id}`),
@@ -312,13 +313,13 @@ export const chatApi = {
312313
deleteChat: (id: number | undefined): Promise<string> => {
313314
return request.get(`/chat/delete/${id}`)
314315
},
315-
analysis: (record_id: number | undefined) => {
316-
return request.fetchStream(`/chat/record/${record_id}/analysis`, {})
316+
analysis: (record_id: number | undefined, controller?: AbortController) => {
317+
return request.fetchStream(`/chat/record/${record_id}/analysis`, {}, controller)
317318
},
318-
predict: (record_id: number | undefined) => {
319-
return request.fetchStream(`/chat/record/${record_id}/predict`, {})
319+
predict: (record_id: number | undefined, controller?: AbortController) => {
320+
return request.fetchStream(`/chat/record/${record_id}/predict`, {}, controller)
320321
},
321-
recommendQuestions: (record_id: number | undefined) => {
322-
return request.fetchStream(`/chat/recommend_questions/${record_id}`, {})
322+
recommendQuestions: (record_id: number | undefined, controller?: AbortController) => {
323+
return request.fetchStream(`/chat/recommend_questions/${record_id}`, {}, controller)
323324
},
324325
}

frontend/src/utils/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class HttpService {
228228
return this.request({ ...config, method: 'POST', url, data })
229229
}
230230

231-
public fetchStream(url: string, data?: any): Promise<any> {
231+
public fetchStream(url: string, data?: any, controller?: AbortController): Promise<any> {
232232
const token = wsCache.get('user.token')
233233
const heads: any = {
234234
'Content-Type': 'application/json',
@@ -250,6 +250,7 @@ class HttpService {
250250
method: 'POST',
251251
headers: heads,
252252
body: JSON.stringify(data),
253+
signal: controller?.signal,
253254
})
254255
}
255256

frontend/src/views/chat/ChatListContainer.vue

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Search } from '@element-plus/icons-vue'
33
import ChatList from '@/views/chat/ChatList.vue'
44
import { useI18n } from 'vue-i18n'
5-
import { computed, ref } from 'vue'
5+
import { computed, nextTick, ref } from 'vue'
66
import { Chat, chatApi, ChatInfo } from '@/api/chat.ts'
77
import { filter, includes } from 'lodash-es'
88
import ChatCreator from '@/views/chat/ChatCreator.vue'
@@ -110,37 +110,44 @@ function goEmpty() {
110110
}
111111
112112
const createNewChat = async () => {
113-
goEmpty()
114-
if (isAssistant.value) {
115-
const assistantChat = await assistantStore.setChat()
116-
if (assistantChat) {
117-
onChatCreated(assistantChat)
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()
118123
}
119-
return
120-
} else {
121-
chatCreatorRef.value?.showDs()
122124
}
123125
}
124126
125127
function onClickHistory(chat: Chat) {
126-
_currentChat.value = new ChatInfo(chat)
127128
if (chat !== undefined && chat.id !== undefined && !_loading.value) {
128-
_currentChatId.value = chat.id
129-
_loading.value = true
130-
chatApi
131-
.get(chat.id)
132-
.then((res) => {
133-
const info = chatApi.toChatInfo(res)
134-
if (info) {
135-
_currentChat.value = info
136-
137-
// scrollToBottom()
138-
emits('onClickHistory', info)
139-
}
140-
})
141-
.finally(() => {
142-
_loading.value = false
143-
})
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+
})
144151
}
145152
}
146153

frontend/src/views/chat/RecommendQuestion.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,24 @@ function clickQuestion(question: string): void {
5050
emits('clickQuestion', question)
5151
}
5252
53+
const stopFlag = ref(false)
54+
5355
async function getRecommendQuestions() {
56+
stopFlag.value = false
5457
loading.value = true
5558
try {
59+
const controller: AbortController = new AbortController()
5660
const response = await chatApi.recommendQuestions(props.recordId)
5761
const reader = response.body.getReader()
5862
const decoder = new TextDecoder()
5963
6064
while (true) {
65+
if (stopFlag.value) {
66+
controller.abort()
67+
loading.value = false
68+
break
69+
}
70+
6171
const { done, value } = await reader.read()
6272
if (done) {
6373
break
@@ -125,7 +135,12 @@ async function getRecommendQuestions() {
125135
}
126136
}
127137
128-
defineExpose({ getRecommendQuestions, id: () => props.recordId })
138+
function stop() {
139+
stopFlag.value = true
140+
loading.value = false
141+
}
142+
143+
defineExpose({ getRecommendQuestions, id: () => props.recordId, stop })
129144
</script>
130145

131146
<template>

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

Lines changed: 16 additions & 4 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 } from 'vue'
4+
import { computed, nextTick, ref } from 'vue'
55
import MdComponent from '@/views/chat/component/MdComponent.vue'
66
const props = withDefaults(
77
defineProps<{
@@ -75,7 +75,9 @@ const _loading = computed({
7575
},
7676
})
7777
78+
const stopFlag = ref(false)
7879
const sendMessage = async () => {
80+
stopFlag.value = false
7981
_loading.value = true
8082
8183
if (index.value < 0) {
@@ -92,14 +94,21 @@ const sendMessage = async () => {
9294
if (error) return
9395
9496
try {
95-
const response = await chatApi.analysis(currentRecord.analysis_record_id)
97+
const controller: AbortController = new AbortController()
98+
const response = await chatApi.analysis(currentRecord.analysis_record_id, controller)
9699
const reader = response.body.getReader()
97100
const decoder = new TextDecoder()
98101
99102
let analysis_answer = ''
100103
let analysis_answer_thinking = ''
101104
102105
while (true) {
106+
if (stopFlag.value) {
107+
controller.abort()
108+
_loading.value = false
109+
break
110+
}
111+
103112
const { done, value } = await reader.read()
104113
if (done) {
105114
_loading.value = false
@@ -180,8 +189,11 @@ const sendMessage = async () => {
180189
_loading.value = false
181190
}
182191
}
183-
184-
defineExpose({ sendMessage, index: index.value, chatList: _chatList })
192+
function stop() {
193+
stopFlag.value = true
194+
_loading.value = false
195+
}
196+
defineExpose({ sendMessage, index: () => index.value, chatList: () => _chatList.value, stop })
185197
</script>
186198

187199
<template>

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

Lines changed: 16 additions & 3 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 { Chat, chatApi, ChatInfo, type ChatMessage, ChatRecord, questionApi } from '@/api/chat.ts'
4-
import { computed, nextTick } from 'vue'
4+
import { computed, nextTick, ref } from 'vue'
55
const props = withDefaults(
66
defineProps<{
77
chatList?: Array<ChatInfo>
@@ -75,7 +75,10 @@ const _loading = computed({
7575
},
7676
})
7777
78+
const stopFlag = ref(false)
79+
7880
const sendMessage = async () => {
81+
stopFlag.value = false
7982
_loading.value = true
8083
8184
if (index.value < 0) {
@@ -92,9 +95,11 @@ const sendMessage = async () => {
9295
if (error) return
9396
9497
try {
98+
const controller: AbortController = new AbortController()
9599
const response = await questionApi.add({
96100
question: currentRecord.question,
97101
chat_id: _currentChatId.value,
102+
controller,
98103
})
99104
const reader = response.body.getReader()
100105
const decoder = new TextDecoder()
@@ -103,6 +108,11 @@ const sendMessage = async () => {
103108
let chart_answer = ''
104109
105110
while (true) {
111+
if (stopFlag.value) {
112+
controller.abort()
113+
break
114+
}
115+
106116
const { done, value } = await reader.read()
107117
if (done) {
108118
_loading.value = false
@@ -216,8 +226,11 @@ function getChatData(recordId?: number) {
216226
})
217227
})
218228
}
219-
220-
defineExpose({ sendMessage, index: index.value })
229+
function stop() {
230+
stopFlag.value = true
231+
_loading.value = false
232+
}
233+
defineExpose({ sendMessage, index: () => index.value, stop })
221234
</script>
222235

223236
<template>

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

Lines changed: 15 additions & 3 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 } from 'vue'
4+
import { computed, nextTick, ref } from 'vue'
55
import MdComponent from '@/views/chat/component/MdComponent.vue'
66
const props = withDefaults(
77
defineProps<{
@@ -75,7 +75,9 @@ const _loading = computed({
7575
},
7676
})
7777
78+
const stopFlag = ref(false)
7879
const sendMessage = async () => {
80+
stopFlag.value = false
7981
_loading.value = true
8082
8183
if (index.value < 0) {
@@ -92,6 +94,7 @@ const sendMessage = async () => {
9294
if (error) return
9395
9496
try {
97+
const controller: AbortController = new AbortController()
9598
const response = await chatApi.predict(currentRecord.predict_record_id)
9699
const reader = response.body.getReader()
97100
const decoder = new TextDecoder()
@@ -100,6 +103,12 @@ const sendMessage = async () => {
100103
let predict_content = ''
101104
102105
while (true) {
106+
if (stopFlag.value) {
107+
controller.abort()
108+
_loading.value = false
109+
break
110+
}
111+
103112
const { done, value } = await reader.read()
104113
if (done) {
105114
_loading.value = false
@@ -186,8 +195,11 @@ const sendMessage = async () => {
186195
_loading.value = false
187196
}
188197
}
189-
190-
defineExpose({ sendMessage, index: index.value, chatList: _chatList })
198+
function stop() {
199+
stopFlag.value = true
200+
_loading.value = false
201+
}
202+
defineExpose({ sendMessage, index: () => index.value, chatList: () => _chatList, stop })
191203
</script>
192204

193205
<template>

0 commit comments

Comments
 (0)