feat:优化回答消息的返回方式与交互样式调整
This commit is contained in:
@@ -477,31 +477,62 @@ const handleWebSocketMessage = (data) => {
|
||||
}
|
||||
|
||||
let aiMsgIndex = -1;
|
||||
if (currentSessionMessageId && pendingMap.has(currentSessionMessageId)) {
|
||||
aiMsgIndex = pendingMap.get(currentSessionMessageId);
|
||||
if (aiMsgIndex >= 0 && aiMsgIndex < chatMsgList.value.length) {
|
||||
const item = chatMsgList.value[aiMsgIndex];
|
||||
if (item && item.msgType === MessageRole.AI &&
|
||||
item.replyMessageId.length > 0 && data.replyMessageId &&
|
||||
item.replyMessageId !== data.replyMessageId) {
|
||||
// 已经存在对应的AI消息项,继续使用
|
||||
const aiMsg = {
|
||||
msgId: `msg_${chatMsgList.value.length}`,
|
||||
msgType: MessageRole.AI,
|
||||
msg: "",
|
||||
isLoading: false,
|
||||
messageId: currentSessionMessageId,
|
||||
replyMessageId: '',
|
||||
componentName: "",
|
||||
title: "",
|
||||
finish: false,
|
||||
};
|
||||
chatMsgList.value.push(aiMsg);
|
||||
aiMsgIndex = chatMsgList.value.length - 1;
|
||||
}
|
||||
// Prefer matching by replyMessageId if provided
|
||||
if (data.replyMessageId) {
|
||||
// 1) Try to find an existing AI message that already has the same replyMessageId
|
||||
for (let i = chatMsgList.value.length - 1; i >= 0; i--) {
|
||||
const it = chatMsgList.value[i];
|
||||
if (it && it.msgType === MessageRole.AI && it.replyMessageId === data.replyMessageId) {
|
||||
aiMsgIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 2) If not found, check pendingMap for currentSessionMessageId
|
||||
if (aiMsgIndex === -1 && currentSessionMessageId && pendingMap.has(currentSessionMessageId)) {
|
||||
const idx = pendingMap.get(currentSessionMessageId);
|
||||
if (idx >= 0 && idx < chatMsgList.value.length) {
|
||||
const item = chatMsgList.value[idx];
|
||||
// If the pending item already has a different non-empty replyMessageId, create a new AI entry
|
||||
if (item && item.msgType === MessageRole.AI && item.replyMessageId && item.replyMessageId !== data.replyMessageId) {
|
||||
const aiMsg = {
|
||||
msgId: `msg_${chatMsgList.value.length}`,
|
||||
msgType: MessageRole.AI,
|
||||
msg: "",
|
||||
isLoading: false,
|
||||
messageId: currentSessionMessageId,
|
||||
replyMessageId: data.replyMessageId || '',
|
||||
componentName: "",
|
||||
title: "",
|
||||
finish: false,
|
||||
};
|
||||
chatMsgList.value.push(aiMsg);
|
||||
aiMsgIndex = chatMsgList.value.length - 1;
|
||||
} else {
|
||||
// Reuse the pending item
|
||||
aiMsgIndex = idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3) If still not found, create a new AI message for this replyMessageId
|
||||
if (aiMsgIndex === -1) {
|
||||
const aiMsg = {
|
||||
msgId: `msg_${chatMsgList.value.length}`,
|
||||
msgType: MessageRole.AI,
|
||||
msg: "",
|
||||
isLoading: false,
|
||||
messageId: currentSessionMessageId,
|
||||
replyMessageId: data.replyMessageId || '',
|
||||
componentName: "",
|
||||
title: "",
|
||||
finish: false,
|
||||
};
|
||||
chatMsgList.value.push(aiMsg);
|
||||
aiMsgIndex = chatMsgList.value.length - 1;
|
||||
}
|
||||
} else {
|
||||
// 向后搜索最近的 AI 消息(回退逻辑)
|
||||
// No replyMessageId: fall back to most recent AI message
|
||||
for (let i = chatMsgList.value.length - 1; i >= 0; i--) {
|
||||
if (chatMsgList.value[i] && chatMsgList.value[i].msgType === MessageRole.AI) {
|
||||
aiMsgIndex = i;
|
||||
@@ -572,7 +603,7 @@ const handleWebSocketMessage = (data) => {
|
||||
}
|
||||
|
||||
// 清理 pendingMap / timeout
|
||||
const ownedMessageId = chatMsgList.value[aiMsgIndex].messageId || msgId;
|
||||
const ownedMessageId = chatMsgList.value[aiMsgIndex].messageId || null;
|
||||
if (ownedMessageId) {
|
||||
if (pendingTimeouts.has(ownedMessageId)) {
|
||||
clearTimeout(pendingTimeouts.get(ownedMessageId));
|
||||
|
||||
Reference in New Issue
Block a user