feat: 请求终止的逻辑调整

This commit is contained in:
2025-08-08 09:08:07 +08:00
parent a432c32fb5
commit 1af6fd2e5f
2 changed files with 141 additions and 58 deletions

View File

@@ -103,7 +103,7 @@
import CreateServiceOrder from '@/components/CreateServiceOrder/index.vue'
import { agentChatStream } from '@/request/api/AgentChatStream';
import { agentChatStream, stopAbortTask } from '@/request/api/AgentChatStream';
import { mainPageData } from '@/request/api/MainPageDataApi';
import { conversationMsgList, recentConversation } from '@/request/api/ConversationApi';
@@ -145,8 +145,6 @@
// 会话进行中标志
const isSessionActive = ref(false);
// 请求任务引用
const requestTaskRef = ref(null);
/// 指令
let commonType = ''
@@ -340,7 +338,7 @@
}
}
chatMsgList.value.push(newMsg)
inputMessage.value = '';
inputMessage.value = '';
sendChat(message, isInstruct)
console.log("发送的新消息:",JSON.stringify(newMsg))
}
@@ -357,7 +355,8 @@
conversationId: conversationId.value,
agentId: agentId.value,
messageType: isInstruct ? 1 : 0,
messageContent: isInstruct ? commonType : message
messageContent: isInstruct ? commonType : message,
messageId: 'mid' + new Date().getTime()
}
// 插入AI消息
@@ -390,8 +389,8 @@
}
// 流式接收内容
const { promise, requestTask } = agentChatStream(args, (chunk) => {
console.log('分段内容:', chunk)
const promise = agentChatStream(args, (chunk) => {
// console.log('分段内容:', chunk)
if (chunk && chunk.error) {
chatMsgList.value[aiMsgIndex].msg = '请求错误,请重试';
clearInterval(loadingTimer);
@@ -456,8 +455,6 @@
}
})
// 存储请求任务
requestTaskRef.value = requestTask;
// 可选处理Promise完成/失败, 已经在回调中处理数据,此处无需再处理
promise.then(data => {
@@ -486,31 +483,24 @@
// 停止请求函数
const stopRequest = () => {
if (requestTaskRef.value && requestTaskRef.value.abort) {
// 标记请求已中止,用于过滤后续可能到达的数据
requestTaskRef.value.isAborted = true;
// 中止请求
requestTaskRef.value.abort();
// 重置状态
isSessionActive.value = false;
const msg = chatMsgList.value[currentAIMsgIndex].msg;
if (!msg || msg === '加载中.' || msg.startsWith('加载中')) {
chatMsgList.value[currentAIMsgIndex].msg = '已终止请求,请重试';
}
// 清除计时器
if (loadingTimer) {
clearInterval(loadingTimer);
loadingTimer = null;
}
if (typeWriterTimer) {
clearTimeout(typeWriterTimer);
typeWriterTimer = null;
}
setTimeoutScrollToBottom()
// 清空请求引用
requestTaskRef.value = null;
}
const stopRequest = () => {
stopAbortTask()
// 重置状态
isSessionActive.value = false;
const msg = chatMsgList.value[currentAIMsgIndex].msg;
if (!msg || msg === '加载中.' || msg.startsWith('加载中')) {
chatMsgList.value[currentAIMsgIndex].msg = '已终止请求,请重试';
}
// 清除计时器
if (loadingTimer) {
clearInterval(loadingTimer);
loadingTimer = null;
}
if (typeWriterTimer) {
clearTimeout(typeWriterTimer);
typeWriterTimer = null;
}
setTimeoutScrollToBottom()
}
</script>