实现ai对话(电脑同步)

This commit is contained in:
2026-06-06 18:10:29 +08:00
parent dd4975fd2c
commit d284789240
23 changed files with 3768 additions and 1548 deletions

View File

@@ -475,6 +475,10 @@
// 跳转到工作区
const goWorkSpace = () => {
// 保存当前会话id确保从工作区返回时能恢复
if (currentSessionId.value) {
uni.setStorageSync('currentSessionId', currentSessionId.value)
}
uni.navigateTo({
url: '/pages/WorkSpace/WorkSpace'
})
@@ -732,6 +736,9 @@
// })
// }
// }
// AI 回复超时定时器
let aiResponseTimer = null
// 发送AI消息
const sendAIMessage = async () => {
try {
@@ -750,7 +757,16 @@
}
socketStore.send(data)
isThinking.value = true
await waitForAIResponse();
// 3分钟超时
aiResponseTimer = setTimeout(() => {
uni.showToast({
title: 'AI回复超时请重试',
icon: 'none'
})
isThinking.value = false
textMessage.value = ''
uploadedFiles.value = []
}, 180000)
} catch (error) {
isThinking.value = false
uni.showToast({
@@ -760,43 +776,20 @@
}
}
// 等待ai回复信息并保存到数据库
const waitForAIResponse = () => {
return new Promise((resolve, reject) => {
// 设置超时
const timeout = setTimeout(() => {
unwatch()
reject(new Error('等待AI回复超时'));
}, 180000); // 3分钟超时
const finish = async () => {
try {
console.log("ai返回的消息", socketStore.messageString);
console.log('AI回复结束刷新消息列表');
await takeConversationMessages();
resolve();
} catch (error) {
console.error('保存AI回复失败', error);
reject(error);
} finally {
isThinking.value = false
textMessage.value = ''
uploadedFiles.value = []
unwatch();
clearTimeout(timeout);
}
}
// 监听 isThinkingfalse → true 时不做处理true → false 时表示 AI 回复结束
const unwatch = watch(() => socketStore.isThinking, (newVal, oldVal) => {
console.log("isThinking 变化:", oldVal, "→", newVal);
// AI 回复结束:从 true 变为 false且已有回复内容
if (oldVal && !newVal && socketStore.messageString) {
finish()
}
})
})
}
// 全局监听 socketStore.isThinkingtrue → false 时表示 AI 回复结束
watch(() => socketStore.isThinking, (newVal, oldVal) => {
console.log("isThinking 变化:", oldVal, "→", newVal);
// AI 回复结束:从 true 变为 false且已有回复内容
if (oldVal && !newVal && socketStore.messageString) {
clearTimeout(aiResponseTimer)
console.log("ai返回的消息", socketStore.messageString);
console.log('AI回复结束刷新消息列表');
takeConversationMessages()
isThinking.value = false
textMessage.value = ''
uploadedFiles.value = []
}
})
const textMessage = ref('')
@@ -829,11 +822,13 @@
console.log("token:", userToken.value);
UserConversations.value = await getUserConversations(userToken.value) || [];
console.log("UserConversations:", UserConversations.value);
// 获取列表第一个会话
if (UserConversations.value.length > 0) {
currentSessionId.value = UserConversations.value[0]._id; // 假设会话对象有 id 字段
// 优先恢复已保存的会话id避免刷新到列表第一个
const savedSessionId = getCurrentSessionId();
if (savedSessionId && UserConversations.value.some(c => c._id === savedSessionId)) {
currentSessionId.value = savedSessionId;
} else if (UserConversations.value.length > 0) {
currentSessionId.value = UserConversations.value[0]._id;
} else {
// 如果没有会话,可以创建一个默认会话或设为空
currentSessionId.value = '';
}
console.log('保存会话id', currentSessionId.value);