界面隐显后继续流式输出;优化中断逻辑

This commit is contained in:
2026-08-06 08:52:06 +08:00
parent b4e69802c0
commit add3802d62
2 changed files with 61 additions and 35 deletions

View File

@@ -1418,13 +1418,14 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
streamingMessageId.value = null streamingMessageId.value = null
} }
// 立即关闭弹窗,清除超时 // 立即关闭弹窗,清除超时
// clearTimeout(aiResponseTimer)
isThinking.value = false isThinking.value = false
socketStore.isThinking = false socketStore.isThinking = false
isSelfSent.value = false isSelfSent.value = false
textMessage.value = '' textMessage.value = ''
uploadedFiles.value = [] uploadedFiles.value = []
// takeConversationMessages 由后端中断确认驱动(见 handleBusinessMessage // 中断后刷新消息列表,获取服务端最终状态
await takeConversationMessages()
// await takeUserConversations()
uni.showToast({ uni.showToast({
title: '已中断', title: '已中断',
icon: 'none', icon: 'none',
@@ -1982,11 +1983,24 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
if (chatType === 0) { if (chatType === 0) {
await takeUserConversations(); await takeUserConversations();
// 只有在会话ID存在时才获取消息 // 如果正在流式输出,跳过重新加载消息列表,避免打断流式输出
if (currentSessionId.value) { if (currentSessionId.value) {
if (socketStore.isThinking) {
// 流式输出中:不刷新消息,但确保消息列表中包含流式气泡
// 如果流式气泡因某些原因丢失,重新创建
if (streamingMessageId.value && !allmessages.value.some(m => m._id === streamingMessageId.value)) {
allmessages.value = [...allmessages.value, {
role: 'assistant',
content: socketStore.messageString || '',
_streaming: true,
_id: streamingMessageId.value
}]
}
} else {
isLoadingMessages.value = true isLoadingMessages.value = true
takeConversationMessages(); takeConversationMessages();
} }
}
// AI 对话页面:主动建立 socket 连接,确保消息实时推送 // AI 对话页面:主动建立 socket 连接,确保消息实时推送
if (userToken.value && currentSessionId.value && !socketStore.isConnected) { if (userToken.value && currentSessionId.value && !socketStore.isConnected) {
handleConnect() handleConnect()

View File

@@ -6680,6 +6680,7 @@ This will fail in production.`);
isSelfSent.value = false; isSelfSent.value = false;
textMessage.value = ""; textMessage.value = "";
uploadedFiles.value = []; uploadedFiles.value = [];
await takeConversationMessages();
uni.showToast({ uni.showToast({
title: "已中断", title: "已中断",
icon: "none", icon: "none",
@@ -6687,9 +6688,9 @@ This will fail in production.`);
}); });
}; };
vue.watch(() => socketStore.isThinking, async (newVal, oldVal) => { vue.watch(() => socketStore.isThinking, async (newVal, oldVal) => {
formatAppLog("log", "at pages/Chat/Chat.vue:1437", "isThinking 变化:", oldVal, "→", newVal, "messageString:", socketStore.messageString); formatAppLog("log", "at pages/Chat/Chat.vue:1438", "isThinking 变化:", oldVal, "→", newVal, "messageString:", socketStore.messageString);
if (!oldVal && newVal) { if (!oldVal && newVal) {
formatAppLog("log", "at pages/Chat/Chat.vue:1440", "AI开始回复跨设备同步锁定输入框"); formatAppLog("log", "at pages/Chat/Chat.vue:1441", "AI开始回复跨设备同步锁定输入框");
isThinking.value = true; isThinking.value = true;
if (!isSelfSent.value) { if (!isSelfSent.value) {
await takeConversationMessages(); await takeConversationMessages();
@@ -6706,7 +6707,7 @@ This will fail in production.`);
return; return;
} }
if (oldVal && !newVal) { if (oldVal && !newVal) {
formatAppLog("log", "at pages/Chat/Chat.vue:1459", "AI回复结束正常/中断),解锁并刷新消息列表"); formatAppLog("log", "at pages/Chat/Chat.vue:1460", "AI回复结束正常/中断),解锁并刷新消息列表");
await takeConversationMessages(); await takeConversationMessages();
if (streamingMessageId.value) { if (streamingMessageId.value) {
allmessages.value = allmessages.value.filter((m) => m._id !== streamingMessageId.value); allmessages.value = allmessages.value.filter((m) => m._id !== streamingMessageId.value);
@@ -6777,15 +6778,15 @@ This will fail in production.`);
UserData.value = await getUserInfo(userToken.value); UserData.value = await getUserInfo(userToken.value);
UserId.value = UserData.value._id; UserId.value = UserData.value._id;
UserAvatar.value = UserData.value.avatar || ""; UserAvatar.value = UserData.value.avatar || "";
formatAppLog("log", "at pages/Chat/Chat.vue:1546", "用户信息已加载:", UserId.value, UserAvatar.value); formatAppLog("log", "at pages/Chat/Chat.vue:1547", "用户信息已加载:", UserId.value, UserAvatar.value);
} catch (error) { } catch (error) {
formatAppLog("error", "at pages/Chat/Chat.vue:1548", "获取用户信息失败:", error); formatAppLog("error", "at pages/Chat/Chat.vue:1549", "获取用户信息失败:", error);
} }
}; };
const takeUserConversations = async () => { const takeUserConversations = async () => {
try { try {
userToken.value = getToken(); userToken.value = getToken();
formatAppLog("log", "at pages/Chat/Chat.vue:1556", "token:", userToken.value); formatAppLog("log", "at pages/Chat/Chat.vue:1557", "token:", userToken.value);
UserConversations.value = await getUserConversations(userToken.value) || []; UserConversations.value = await getUserConversations(userToken.value) || [];
const savedSessionId = getCurrentSessionId(); const savedSessionId = getCurrentSessionId();
if (savedSessionId && UserConversations.value.some((c) => c._id === savedSessionId)) { if (savedSessionId && UserConversations.value.some((c) => c._id === savedSessionId)) {
@@ -6795,7 +6796,7 @@ This will fail in production.`);
} else { } else {
currentSessionId.value = ""; currentSessionId.value = "";
} }
formatAppLog("log", "at pages/Chat/Chat.vue:1568", "保存会话id", currentSessionId.value); formatAppLog("log", "at pages/Chat/Chat.vue:1569", "保存会话id", currentSessionId.value);
uni.setStorageSync("currentSessionId", currentSessionId.value); uni.setStorageSync("currentSessionId", currentSessionId.value);
} catch (error) { } catch (error) {
uni.showToast({ uni.showToast({
@@ -6807,17 +6808,17 @@ This will fail in production.`);
const FriendInfoList = vue.ref([]); const FriendInfoList = vue.ref([]);
const takeFriendList = async () => { const takeFriendList = async () => {
try { try {
formatAppLog("log", "at pages/Chat/Chat.vue:1583", "开始获取好友列表"); formatAppLog("log", "at pages/Chat/Chat.vue:1584", "开始获取好友列表");
const friendList = await getChatFriend(UserId.value); const friendList = await getChatFriend(UserId.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1586", "friendList:", friendList); formatAppLog("log", "at pages/Chat/Chat.vue:1587", "friendList:", friendList);
if (friendList && friendList.length) { if (friendList && friendList.length) {
FriendInfoList.value = await takeUserAvatar(friendList); FriendInfoList.value = await takeUserAvatar(friendList);
} else { } else {
FriendInfoList.value = []; FriendInfoList.value = [];
formatAppLog("log", "at pages/Chat/Chat.vue:1591", "好友列表为空"); formatAppLog("log", "at pages/Chat/Chat.vue:1592", "好友列表为空");
} }
} catch (error) { } catch (error) {
formatAppLog("error", "at pages/Chat/Chat.vue:1594", "获取好友列表失败:", error); formatAppLog("error", "at pages/Chat/Chat.vue:1595", "获取好友列表失败:", error);
FriendInfoList.value = []; FriendInfoList.value = [];
} finally { } finally {
UserConversations.value = FriendInfoList.value; UserConversations.value = FriendInfoList.value;
@@ -6844,17 +6845,17 @@ This will fail in production.`);
}; };
}); });
} catch (err) { } catch (err) {
formatAppLog("error", "at pages/Chat/Chat.vue:1624", "获取好友头像失败", err); formatAppLog("error", "at pages/Chat/Chat.vue:1625", "获取好友头像失败", err);
return friendList; return friendList;
} }
}; };
const GroupList = vue.ref([]); const GroupList = vue.ref([]);
const takeGroupList = async () => { const takeGroupList = async () => {
try { try {
formatAppLog("log", "at pages/Chat/Chat.vue:1634", "开始获取群聊列表"); formatAppLog("log", "at pages/Chat/Chat.vue:1635", "开始获取群聊列表");
GroupList.value = await getGroup(UserId.value); GroupList.value = await getGroup(UserId.value);
} catch (error) { } catch (error) {
formatAppLog("error", "at pages/Chat/Chat.vue:1639", "获取群聊列表失败:", error); formatAppLog("error", "at pages/Chat/Chat.vue:1640", "获取群聊列表失败:", error);
GroupList.value = []; GroupList.value = [];
} finally { } finally {
UserConversations.value = GroupList.value; UserConversations.value = GroupList.value;
@@ -6866,22 +6867,22 @@ This will fail in production.`);
return []; return [];
try { try {
const memberIds = memberList.map((item) => item.groupContactId); const memberIds = memberList.map((item) => item.groupContactId);
formatAppLog("log", "at pages/Chat/Chat.vue:1653", "请求头像的ID列表:", memberIds); formatAppLog("log", "at pages/Chat/Chat.vue:1654", "请求头像的ID列表:", memberIds);
const memberAvatarList = await getUserAvatar(userToken.value, memberIds); const memberAvatarList = await getUserAvatar(userToken.value, memberIds);
formatAppLog("log", "at pages/Chat/Chat.vue:1655", "头像接口返回数据:", memberAvatarList); formatAppLog("log", "at pages/Chat/Chat.vue:1656", "头像接口返回数据:", memberAvatarList);
const userMap = new Map(memberAvatarList.map((user) => [user.user_id, user]) || []); const userMap = new Map(memberAvatarList.map((user) => [user.user_id, user]) || []);
formatAppLog("log", "at pages/Chat/Chat.vue:1658", "userMap的keys:", Array.from(userMap.keys())); formatAppLog("log", "at pages/Chat/Chat.vue:1659", "userMap的keys:", Array.from(userMap.keys()));
return memberList.map((member) => { return memberList.map((member) => {
const memberId = member.groupContactId; const memberId = member.groupContactId;
const userInfo = userMap.get(memberId); const userInfo = userMap.get(memberId);
formatAppLog("log", "at pages/Chat/Chat.vue:1663", `查找 ${memberId} 的头像:`, userInfo); formatAppLog("log", "at pages/Chat/Chat.vue:1664", `查找 ${memberId} 的头像:`, userInfo);
return { return {
...member, ...member,
avatar: (userInfo == null ? void 0 : userInfo.avatar) || null avatar: (userInfo == null ? void 0 : userInfo.avatar) || null
}; };
}); });
} catch (err) { } catch (err) {
formatAppLog("error", "at pages/Chat/Chat.vue:1670", "获取群成员头像失败", err); formatAppLog("error", "at pages/Chat/Chat.vue:1671", "获取群成员头像失败", err);
return memberList; return memberList;
} }
}; };
@@ -6946,12 +6947,12 @@ This will fail in production.`);
hasMoreGroupMessages.value = true; hasMoreGroupMessages.value = true;
const rawMessages = await getGroupMessages(currentSessionId.value, pageSize$1, 0) || []; const rawMessages = await getGroupMessages(currentSessionId.value, pageSize$1, 0) || [];
allmessages.value = rawMessages; allmessages.value = rawMessages;
formatAppLog("log", "at pages/Chat/Chat.vue:1759", "群聊消息:", allmessages.value); formatAppLog("log", "at pages/Chat/Chat.vue:1760", "群聊消息:", allmessages.value);
const memberList = await getGroupMemberList(currentSessionId.value); const memberList = await getGroupMemberList(currentSessionId.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1762", "获取到的群成员列表:", memberList); formatAppLog("log", "at pages/Chat/Chat.vue:1763", "获取到的群成员列表:", memberList);
if (memberList && memberList.length) { if (memberList && memberList.length) {
groupMemberList.value = await takeGroupMemberAvatar(memberList); groupMemberList.value = await takeGroupMemberAvatar(memberList);
formatAppLog("log", "at pages/Chat/Chat.vue:1765", "群成员列表(带头像):", groupMemberList.value); formatAppLog("log", "at pages/Chat/Chat.vue:1766", "群成员列表(带头像):", groupMemberList.value);
} else { } else {
groupMemberList.value = []; groupMemberList.value = [];
} }
@@ -6990,7 +6991,7 @@ This will fail in production.`);
hasMoreAIMessages.value = false; hasMoreAIMessages.value = false;
} }
} catch (e2) { } catch (e2) {
formatAppLog("error", "at pages/Chat/Chat.vue:1827", "加载更多消息失败:", e2); formatAppLog("error", "at pages/Chat/Chat.vue:1828", "加载更多消息失败:", e2);
uni.showToast({ title: "加载更多失败", icon: "none" }); uni.showToast({ title: "加载更多失败", icon: "none" });
} finally { } finally {
isAILoadingMore.value = false; isAILoadingMore.value = false;
@@ -7012,7 +7013,7 @@ This will fail in production.`);
hasMoreFriendMessages.value = false; hasMoreFriendMessages.value = false;
} }
} catch (e2) { } catch (e2) {
formatAppLog("error", "at pages/Chat/Chat.vue:1852", "加载更多好友消息失败:", e2); formatAppLog("error", "at pages/Chat/Chat.vue:1853", "加载更多好友消息失败:", e2);
uni.showToast({ title: "加载更多失败", icon: "none" }); uni.showToast({ title: "加载更多失败", icon: "none" });
} finally { } finally {
isFriendLoadingMore.value = false; isFriendLoadingMore.value = false;
@@ -7034,7 +7035,7 @@ This will fail in production.`);
hasMoreGroupMessages.value = false; hasMoreGroupMessages.value = false;
} }
} catch (e2) { } catch (e2) {
formatAppLog("error", "at pages/Chat/Chat.vue:1877", "加载更多群聊消息失败:", e2); formatAppLog("error", "at pages/Chat/Chat.vue:1878", "加载更多群聊消息失败:", e2);
uni.showToast({ title: "加载更多失败", icon: "none" }); uni.showToast({ title: "加载更多失败", icon: "none" });
} finally { } finally {
isGroupLoadingMore.value = false; isGroupLoadingMore.value = false;
@@ -7050,9 +7051,9 @@ This will fail in production.`);
} }
}; };
vue.watch(() => friendSocketStore.MessageReceived, (newId) => { vue.watch(() => friendSocketStore.MessageReceived, (newId) => {
formatAppLog("log", "at pages/Chat/Chat.vue:1898", "收到了好友消息"); formatAppLog("log", "at pages/Chat/Chat.vue:1899", "收到了好友消息");
if (newId) { if (newId) {
formatAppLog("log", "at pages/Chat/Chat.vue:1900", "ChatType:", ChatType.value); formatAppLog("log", "at pages/Chat/Chat.vue:1901", "ChatType:", ChatType.value);
switch (ChatType.value) { switch (ChatType.value) {
case 0: case 0:
takeConversationMessages(); takeConversationMessages();
@@ -7067,7 +7068,7 @@ This will fail in production.`);
friendSocketStore.MessageReceived = false; friendSocketStore.MessageReceived = false;
break; break;
default: default:
formatAppLog("log", "at pages/Chat/Chat.vue:1916", "default 分支"); formatAppLog("log", "at pages/Chat/Chat.vue:1917", "default 分支");
friendSocketStore.MessageReceived = false; friendSocketStore.MessageReceived = false;
break; break;
} }
@@ -7125,9 +7126,20 @@ This will fail in production.`);
if (chatType === 0) { if (chatType === 0) {
await takeUserConversations(); await takeUserConversations();
if (currentSessionId.value) { if (currentSessionId.value) {
if (socketStore.isThinking) {
if (streamingMessageId.value && !allmessages.value.some((m) => m._id === streamingMessageId.value)) {
allmessages.value = [...allmessages.value, {
role: "assistant",
content: socketStore.messageString || "",
_streaming: true,
_id: streamingMessageId.value
}];
}
} else {
isLoadingMessages.value = true; isLoadingMessages.value = true;
takeConversationMessages(); takeConversationMessages();
} }
}
if (userToken.value && currentSessionId.value && !socketStore.isConnected) { if (userToken.value && currentSessionId.value && !socketStore.isConnected) {
handleConnect(); handleConnect();
} }