优化新建会话逻辑

This commit is contained in:
2026-08-05 17:31:48 +08:00
parent 2fff814c79
commit 941070a2ee
3 changed files with 54 additions and 50 deletions

View File

@@ -1610,9 +1610,6 @@ if (uni.restoreGlobal) {
if (res.statusCode === 200) {
const respond = res.data;
if ((respond == null ? void 0 : respond.success) && (respond == null ? void 0 : respond.conversation_id)) {
if (respond.workspace_id) {
uni.setStorageSync("workspace_id", respond.workspace_id);
}
resolve({
conversation_id: respond.conversation_id,
workspace_id: respond.workspace_id,
@@ -6096,9 +6093,11 @@ This will fail in production.`);
return;
}
closeNewChatModal();
takeUserConversations();
await takeUserConversations();
currentSessionId.value = conversationId;
uni.setStorageSync("currentSessionId", conversationId);
} catch (error) {
formatAppLog("error", "at pages/Chat/Chat.vue:732", "新建普通会话失败:", error);
formatAppLog("error", "at pages/Chat/Chat.vue:735", "新建普通会话失败:", error);
uni.showToast({
title: "创建会话失败,请重试",
icon: "none"
@@ -6125,7 +6124,7 @@ This will fail in production.`);
uni.showToast({ title: "获取智能体列表失败", icon: "none" });
}
} catch (error) {
formatAppLog("error", "at pages/Chat/Chat.vue:764", "获取智能体列表失败:", error);
formatAppLog("error", "at pages/Chat/Chat.vue:767", "获取智能体列表失败:", error);
uni.showToast({ title: "获取智能体列表失败,请重试", icon: "none" });
} finally {
agentLoading.value = false;
@@ -6147,11 +6146,12 @@ This will fail in production.`);
}
await takeUserConversations();
currentSessionId.value = result.conversation_id;
uni.setStorageSync("currentSessionId", result.conversation_id);
uni.hideLoading();
uni.showToast({ title: `已创建「${result.title || agent.title}」会话`, icon: "success" });
} catch (error) {
uni.hideLoading();
formatAppLog("error", "at pages/Chat/Chat.vue:794", "创建智能体会话失败:", error);
formatAppLog("error", "at pages/Chat/Chat.vue:798", "创建智能体会话失败:", error);
uni.showToast({ title: typeof error === "string" ? error : "创建失败,请重试", icon: "none" });
}
};
@@ -6240,7 +6240,7 @@ This will fail in production.`);
};
const previewFileArray = vue.ref([]);
const uploadPhoto = () => {
formatAppLog("log", "at pages/Chat/Chat.vue:911", "点击了拍照上传");
formatAppLog("log", "at pages/Chat/Chat.vue:915", "点击了拍照上传");
uni.chooseImage({
count: 1,
sourceType: ["camera", "album"],
@@ -6263,7 +6263,7 @@ This will fail in production.`);
});
},
fail: (err) => {
formatAppLog("error", "at pages/Chat/Chat.vue:929", "选择图片失败", err);
formatAppLog("error", "at pages/Chat/Chat.vue:933", "选择图片失败", err);
}
});
};
@@ -6272,12 +6272,12 @@ This will fail in production.`);
};
const fileList = vue.ref([]);
const uploadFile = () => {
formatAppLog("log", "at pages/Chat/Chat.vue:940", "点击了上传文件");
formatAppLog("log", "at pages/Chat/Chat.vue:944", "点击了上传文件");
chooseFile({
count: 5,
type: "all",
success: (res) => {
formatAppLog("log", "at pages/Chat/Chat.vue:945", "成功了");
formatAppLog("log", "at pages/Chat/Chat.vue:949", "成功了");
fileList.value = res.tempFiles;
previewFileArray.value.push(...res.tempFiles);
uni.showToast({
@@ -6286,7 +6286,7 @@ This will fail in production.`);
});
},
fail: (err) => {
formatAppLog("error", "at pages/Chat/Chat.vue:956", "选择失败:", err);
formatAppLog("error", "at pages/Chat/Chat.vue:960", "选择失败:", err);
uni.showToast({
title: "选择失败",
icon: "error"
@@ -6525,7 +6525,7 @@ This will fail in production.`);
previewFileArray.value = [];
} catch (err) {
uni.hideLoading();
formatAppLog("error", "at pages/Chat/Chat.vue:1231", "文件上传失败:", err);
formatAppLog("error", "at pages/Chat/Chat.vue:1235", "文件上传失败:", err);
uni.showToast({
title: "文件上传失败,请重试",
icon: "error"
@@ -6659,15 +6659,15 @@ This will fail in production.`);
}
};
const stopConversation = async () => {
formatAppLog("log", "at pages/Chat/Chat.vue:1401", "中断对话 - 当前会话ID:", currentSessionId.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1405", "中断对话 - 当前会话ID:", currentSessionId.value);
try {
await socketStore.send({
type: "stop",
conversation_id: currentSessionId.value
});
formatAppLog("log", "at pages/Chat/Chat.vue:1407", "中断指令已发送成功");
formatAppLog("log", "at pages/Chat/Chat.vue:1411", "中断指令已发送成功");
} catch (err) {
formatAppLog("error", "at pages/Chat/Chat.vue:1409", "中断指令发送失败:", err);
formatAppLog("error", "at pages/Chat/Chat.vue:1413", "中断指令发送失败:", err);
}
if (streamingMessageId.value) {
allmessages.value = allmessages.value.filter((m) => m._id !== streamingMessageId.value);
@@ -6685,9 +6685,9 @@ This will fail in production.`);
});
};
vue.watch(() => socketStore.isThinking, async (newVal, oldVal) => {
formatAppLog("log", "at pages/Chat/Chat.vue:1433", "isThinking 变化:", oldVal, "→", newVal, "messageString:", socketStore.messageString);
formatAppLog("log", "at pages/Chat/Chat.vue:1437", "isThinking 变化:", oldVal, "→", newVal, "messageString:", socketStore.messageString);
if (!oldVal && newVal) {
formatAppLog("log", "at pages/Chat/Chat.vue:1436", "AI开始回复跨设备同步锁定输入框");
formatAppLog("log", "at pages/Chat/Chat.vue:1440", "AI开始回复跨设备同步锁定输入框");
isThinking.value = true;
if (!isSelfSent.value) {
await takeConversationMessages();
@@ -6704,7 +6704,7 @@ This will fail in production.`);
return;
}
if (oldVal && !newVal) {
formatAppLog("log", "at pages/Chat/Chat.vue:1455", "AI回复结束正常/中断),解锁并刷新消息列表");
formatAppLog("log", "at pages/Chat/Chat.vue:1459", "AI回复结束正常/中断),解锁并刷新消息列表");
await takeConversationMessages();
if (streamingMessageId.value) {
allmessages.value = allmessages.value.filter((m) => m._id !== streamingMessageId.value);
@@ -6775,15 +6775,15 @@ This will fail in production.`);
UserData.value = await getUserInfo(userToken.value);
UserId.value = UserData.value._id;
UserAvatar.value = UserData.value.avatar || "";
formatAppLog("log", "at pages/Chat/Chat.vue:1542", "用户信息已加载:", UserId.value, UserAvatar.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1546", "用户信息已加载:", UserId.value, UserAvatar.value);
} catch (error) {
formatAppLog("error", "at pages/Chat/Chat.vue:1544", "获取用户信息失败:", error);
formatAppLog("error", "at pages/Chat/Chat.vue:1548", "获取用户信息失败:", error);
}
};
const takeUserConversations = async () => {
try {
userToken.value = getToken();
formatAppLog("log", "at pages/Chat/Chat.vue:1552", "token:", userToken.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1556", "token:", userToken.value);
UserConversations.value = await getUserConversations(userToken.value) || [];
const savedSessionId = getCurrentSessionId();
if (savedSessionId && UserConversations.value.some((c) => c._id === savedSessionId)) {
@@ -6793,7 +6793,7 @@ This will fail in production.`);
} else {
currentSessionId.value = "";
}
formatAppLog("log", "at pages/Chat/Chat.vue:1564", "保存会话id", currentSessionId.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1568", "保存会话id", currentSessionId.value);
uni.setStorageSync("currentSessionId", currentSessionId.value);
} catch (error) {
uni.showToast({
@@ -6805,17 +6805,17 @@ This will fail in production.`);
const FriendInfoList = vue.ref([]);
const takeFriendList = async () => {
try {
formatAppLog("log", "at pages/Chat/Chat.vue:1579", "开始获取好友列表");
formatAppLog("log", "at pages/Chat/Chat.vue:1583", "开始获取好友列表");
const friendList = await getChatFriend(UserId.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1582", "friendList:", friendList);
formatAppLog("log", "at pages/Chat/Chat.vue:1586", "friendList:", friendList);
if (friendList && friendList.length) {
FriendInfoList.value = await takeUserAvatar(friendList);
} else {
FriendInfoList.value = [];
formatAppLog("log", "at pages/Chat/Chat.vue:1587", "好友列表为空");
formatAppLog("log", "at pages/Chat/Chat.vue:1591", "好友列表为空");
}
} catch (error) {
formatAppLog("error", "at pages/Chat/Chat.vue:1590", "获取好友列表失败:", error);
formatAppLog("error", "at pages/Chat/Chat.vue:1594", "获取好友列表失败:", error);
FriendInfoList.value = [];
} finally {
UserConversations.value = FriendInfoList.value;
@@ -6842,17 +6842,17 @@ This will fail in production.`);
};
});
} catch (err) {
formatAppLog("error", "at pages/Chat/Chat.vue:1620", "获取好友头像失败", err);
formatAppLog("error", "at pages/Chat/Chat.vue:1624", "获取好友头像失败", err);
return friendList;
}
};
const GroupList = vue.ref([]);
const takeGroupList = async () => {
try {
formatAppLog("log", "at pages/Chat/Chat.vue:1630", "开始获取群聊列表");
formatAppLog("log", "at pages/Chat/Chat.vue:1634", "开始获取群聊列表");
GroupList.value = await getGroup(UserId.value);
} catch (error) {
formatAppLog("error", "at pages/Chat/Chat.vue:1635", "获取群聊列表失败:", error);
formatAppLog("error", "at pages/Chat/Chat.vue:1639", "获取群聊列表失败:", error);
GroupList.value = [];
} finally {
UserConversations.value = GroupList.value;
@@ -6864,22 +6864,22 @@ This will fail in production.`);
return [];
try {
const memberIds = memberList.map((item) => item.groupContactId);
formatAppLog("log", "at pages/Chat/Chat.vue:1649", "请求头像的ID列表:", memberIds);
formatAppLog("log", "at pages/Chat/Chat.vue:1653", "请求头像的ID列表:", memberIds);
const memberAvatarList = await getUserAvatar(userToken.value, memberIds);
formatAppLog("log", "at pages/Chat/Chat.vue:1651", "头像接口返回数据:", memberAvatarList);
formatAppLog("log", "at pages/Chat/Chat.vue:1655", "头像接口返回数据:", memberAvatarList);
const userMap = new Map(memberAvatarList.map((user) => [user.user_id, user]) || []);
formatAppLog("log", "at pages/Chat/Chat.vue:1654", "userMap的keys:", Array.from(userMap.keys()));
formatAppLog("log", "at pages/Chat/Chat.vue:1658", "userMap的keys:", Array.from(userMap.keys()));
return memberList.map((member) => {
const memberId = member.groupContactId;
const userInfo = userMap.get(memberId);
formatAppLog("log", "at pages/Chat/Chat.vue:1659", `查找 ${memberId} 的头像:`, userInfo);
formatAppLog("log", "at pages/Chat/Chat.vue:1663", `查找 ${memberId} 的头像:`, userInfo);
return {
...member,
avatar: (userInfo == null ? void 0 : userInfo.avatar) || null
};
});
} catch (err) {
formatAppLog("error", "at pages/Chat/Chat.vue:1666", "获取群成员头像失败", err);
formatAppLog("error", "at pages/Chat/Chat.vue:1670", "获取群成员头像失败", err);
return memberList;
}
};
@@ -6944,12 +6944,12 @@ This will fail in production.`);
hasMoreGroupMessages.value = true;
const rawMessages = await getGroupMessages(currentSessionId.value, pageSize$1, 0) || [];
allmessages.value = rawMessages;
formatAppLog("log", "at pages/Chat/Chat.vue:1755", "群聊消息:", allmessages.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1759", "群聊消息:", allmessages.value);
const memberList = await getGroupMemberList(currentSessionId.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1758", "获取到的群成员列表:", memberList);
formatAppLog("log", "at pages/Chat/Chat.vue:1762", "获取到的群成员列表:", memberList);
if (memberList && memberList.length) {
groupMemberList.value = await takeGroupMemberAvatar(memberList);
formatAppLog("log", "at pages/Chat/Chat.vue:1761", "群成员列表(带头像):", groupMemberList.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1765", "群成员列表(带头像):", groupMemberList.value);
} else {
groupMemberList.value = [];
}
@@ -6988,7 +6988,7 @@ This will fail in production.`);
hasMoreAIMessages.value = false;
}
} catch (e2) {
formatAppLog("error", "at pages/Chat/Chat.vue:1823", "加载更多消息失败:", e2);
formatAppLog("error", "at pages/Chat/Chat.vue:1827", "加载更多消息失败:", e2);
uni.showToast({ title: "加载更多失败", icon: "none" });
} finally {
isAILoadingMore.value = false;
@@ -7010,7 +7010,7 @@ This will fail in production.`);
hasMoreFriendMessages.value = false;
}
} catch (e2) {
formatAppLog("error", "at pages/Chat/Chat.vue:1848", "加载更多好友消息失败:", e2);
formatAppLog("error", "at pages/Chat/Chat.vue:1852", "加载更多好友消息失败:", e2);
uni.showToast({ title: "加载更多失败", icon: "none" });
} finally {
isFriendLoadingMore.value = false;
@@ -7032,7 +7032,7 @@ This will fail in production.`);
hasMoreGroupMessages.value = false;
}
} catch (e2) {
formatAppLog("error", "at pages/Chat/Chat.vue:1873", "加载更多群聊消息失败:", e2);
formatAppLog("error", "at pages/Chat/Chat.vue:1877", "加载更多群聊消息失败:", e2);
uni.showToast({ title: "加载更多失败", icon: "none" });
} finally {
isGroupLoadingMore.value = false;
@@ -7048,9 +7048,9 @@ This will fail in production.`);
}
};
vue.watch(() => friendSocketStore.MessageReceived, (newId) => {
formatAppLog("log", "at pages/Chat/Chat.vue:1894", "收到了好友消息");
formatAppLog("log", "at pages/Chat/Chat.vue:1898", "收到了好友消息");
if (newId) {
formatAppLog("log", "at pages/Chat/Chat.vue:1896", "ChatType:", ChatType.value);
formatAppLog("log", "at pages/Chat/Chat.vue:1900", "ChatType:", ChatType.value);
switch (ChatType.value) {
case 0:
takeConversationMessages();
@@ -7065,7 +7065,7 @@ This will fail in production.`);
friendSocketStore.MessageReceived = false;
break;
default:
formatAppLog("log", "at pages/Chat/Chat.vue:1912", "default 分支");
formatAppLog("log", "at pages/Chat/Chat.vue:1916", "default 分支");
friendSocketStore.MessageReceived = false;
break;
}