diff --git a/web/src/components/ChatComponent.vue b/web/src/components/ChatComponent.vue index 8897f71d..85b3e161 100644 --- a/web/src/components/ChatComponent.vue +++ b/web/src/components/ChatComponent.vue @@ -100,6 +100,10 @@ > 请求错误,请重试。{{ message.message }} +
+ 你停止生成了本次回答 + 重新编辑问题 +
@@ -153,9 +157,19 @@
- - - + + + + +
@@ -188,6 +202,7 @@ import { RobotOutlined, CaretRightOutlined, DeploymentUnitOutlined, + StopOutlined } from '@ant-design/icons-vue' import { onClickOutside } from '@vueuse/core' import { Marked } from 'marked'; @@ -456,6 +471,9 @@ const loadDatabases = () => { // 新函数用于处理 fetch 请求 const fetchChatResponse = (user_input, cur_res_id) => { + const controller = new AbortController(); + const signal = controller.signal; + fetch('/api/chat/', { method: 'POST', body: JSON.stringify({ @@ -466,7 +484,8 @@ const fetchChatResponse = (user_input, cur_res_id) => { }), headers: { 'Content-Type': 'application/json' - } + }, + signal // 添加 signal 用于中断请求 }) .then((response) => { if (!response.body) throw new Error("ReadableStream not supported."); @@ -524,13 +543,24 @@ const fetchChatResponse = (user_input, cur_res_id) => { readChunk(); }) .catch((error) => { - console.error(error); - updateMessage({ - id: cur_res_id, - status: "error", - }); + if (error.name === 'AbortError') { + console.log('Fetch aborted'); + } else { + console.error(error); + updateMessage({ + id: cur_res_id, + status: "error", + }); + } isStreaming.value = false; }); + + // 监听 isStreaming 变化,当为 false 时中断请求 + watch(isStreaming, (newValue) => { + if (!newValue) { + controller.abort(); + } + }); } @@ -607,6 +637,36 @@ watch( }, { deep: true } ); + +// 处理发送或停止 +const handleSendOrStop = () => { + if (isStreaming.value) { + // 停止生成 + isStreaming.value = false; + const lastMessage = conv.value.messages[conv.value.messages.length - 1]; + if (lastMessage) { + lastMessage.isStoppedByUser = true; + lastMessage.status = 'stopped'; + } + } else { + // 发送消息 + sendMessage(); + } +} + +// 重试被停止的消息 +const retryStoppedMessage = (message) => { + // 找到用户的原始问题 + const messageIndex = conv.value.messages.findIndex(msg => msg.id === message.id); + if (messageIndex > 0) { + const userMessage = conv.value.messages[messageIndex - 1]; + if (userMessage && userMessage.role === 'sent') { + conv.value.inputText = userMessage.text; + // 删除被停止的消息 + conv.value.messages = conv.value.messages.slice(0, messageIndex); + } + } +}