diff --git a/src/views/dialogue/components/DialogueSession.vue b/src/views/dialogue/components/DialogueSession.vue index a48f4a107e1ec40dcf34bdf9779498893bc6b219..4fd71733238700fd9d2d79eaeaaba85d802b6cf3 100644 --- a/src/views/dialogue/components/DialogueSession.vue +++ b/src/views/dialogue/components/DialogueSession.vue @@ -747,48 +747,38 @@ const handleRootPathRedirect = async () => { conversationId: latestSession.conversationId }; - if (latestSession.appId && latestSession.appId.trim() !== '') { - targetQuery.appId = latestSession.appId; + if (latestSession.appId && latestSession.appId.trim() !== '') { + targetQuery.appId = latestSession.appId; + + // 尝试获取app名称 + const relatedApp = appList.value?.find(app => app.appId === latestSession.appId); + if (relatedApp) { + targetQuery.name = relatedApp.name; + } else { + // 如果在appList中找不到,尝试从历史记录中获取app名称 + console.warn('🔍 [DialogueSession] 在appList中未找到app,appId:', latestSession.appId); + // 可以考虑从其他地方获取app名称,或者使用appId作为fallback + targetQuery.name = latestSession.appId; // fallback + } + } + + // 跳转到最新对话 + router.replace({ + path: '/', + query: targetQuery + }); + + // 设置当前选中的session + currentSelectedSession.value = latestSession.conversationId; - // 尝试获取app名称 - const relatedApp = appList.value?.find(app => app.appId === latestSession.appId); - if (relatedApp) { - targetQuery.name = relatedApp.name; + // 🔑 关键修复:根据对话的appId来设置app状态,而不是保持之前的选择 + if (latestSession.appId && latestSession.appId.trim() !== '') { + user_selected_app.value = latestSession.appId; } else { - // 如果在appList中找不到,尝试从历史记录中获取app名称 - console.warn('🔍 [DialogueSession] 在appList中未找到app,appId:', latestSession.appId); - // 可以考虑从其他地方获取app名称,或者使用appId作为fallback - targetQuery.name = latestSession.appId; // fallback + user_selected_app.value = ''; } } - - // 跳转到最新对话 - router.replace({ - path: '/', - query: targetQuery - }); - - // 设置当前选中的session - currentSelectedSession.value = latestSession.conversationId; - - // 🔑 关键修复:根据对话的appId来设置app状态,而不是保持之前的选择 - if (latestSession.appId && latestSession.appId.trim() !== '') { - console.log('🔍 [DialogueSession] 对话绑定了app,设置app状态:', latestSession.appId); - user_selected_app.value = latestSession.appId; - } else { - console.log('🔍 [DialogueSession] 对话未绑定app,清除app状态'); - user_selected_app.value = ''; - } - - } else { - // 🔑 新增:当前app没有对话记录,通过getHistorySession自动创建 - console.log('🔍 [DialogueSession] 当前app无对话记录,触发自动创建'); - // 不需要手动创建,getHistorySession会检测到没有对话并自动创建 - } - } else { - // 没有历史对话,创建新的无应用对话 - console.log('🔍 [DialogueSession] 无历史对话,创建新的无应用对话'); try { // 清除任何app选择状态 @@ -797,11 +787,8 @@ const handleRootPathRedirect = async () => { // 创建新的无应用对话 await generateSession(false); - // generateSession会自动更新URL,所以这里不需要手动跳转 - console.log('🔍 [DialogueSession] 新对话创建完成'); - } catch (error) { - console.error('🔍 [DialogueSession] 创建新对话失败:', error); + ElMessage.error(`创建新对话失败:${error}`); } } }; @@ -815,30 +802,20 @@ onMounted(() => { // 🔑 修复:从路由参数中恢复conversationId if (route.query?.conversationId && typeof route.query.conversationId === 'string') { - console.log('🔍 [DialogueSession] onMounted: 从路由恢复conversationId', route.query.conversationId); currentSelectedSession.value = route.query.conversationId; } // 🔑 新增:处理根路径访问逻辑 setTimeout(async () => { - console.log('🔍 [DialogueSession] onMounted检查URL同步:', { - currentSelectedSession: currentSelectedSession.value, - currentQuery: route.query || {}, - path: route.path, - user_selected_app: user_selected_app.value, - historySessionCount: historySession.value?.length || 0 - }); - + // 🔑 新增:当路由为/#/时的处理逻辑 if (route.path === '/' && Object.keys(route.query || {}).length === 0) { - console.log('🔍 [DialogueSession] 检测到根路径访问,执行跳转逻辑'); await handleRootPathRedirect(); return; // 根路径处理完成,不执行后续逻辑 } // 🔑 原有逻辑:如果当前有session但URL中没有conversationId,主动更新URL if (currentSelectedSession.value && !(route.query?.conversationId)) { - console.log('🔍 [DialogueSession] onMounted: 发现currentSelectedSession但URL中无conversationId,主动更新URL'); const currentQuery = { ...(route.query || {}) }; currentQuery.conversationId = currentSelectedSession.value; @@ -851,9 +828,7 @@ onMounted(() => { currentQuery.name = selectedApp.name; } } - - console.log('🔍 [DialogueSession] 准备更新URL:', currentQuery); - + router.replace({ path: route.path, query: currentQuery @@ -864,16 +839,13 @@ onMounted(() => { // 🔑 修复:尝试加载变量配置 // 如果user_selected_app已经设置,立即加载 if (user_selected_app.value && conversationVariables.value.length === 0) { - console.log('🔍 [DialogueSession] onMounted: 立即加载变量配置', user_selected_app.value); loadConversationVariables(); } // 🔑 新增:如果user_selected_app还没设置,等待一下再尝试 if (!user_selected_app.value) { - console.log('🔍 [DialogueSession] onMounted: user_selected_app未设置,延迟加载'); setTimeout(() => { if (user_selected_app.value && conversationVariables.value.length === 0) { - console.log('🔍 [DialogueSession] onMounted延迟: 加载变量配置', user_selected_app.value); loadConversationVariables(); } }, 100); @@ -883,7 +855,6 @@ onMounted(() => { setTimeout(() => { // 如果没有选择app且没有当前会话,创建一个无应用对话 if (!user_selected_app.value && !currentSelectedSession.value) { - console.log('🔍 [DialogueSession] 检测到无app无会话状态,创建无应用对话'); generateSession(false); } }, 500); // 等待其他初始化完成 @@ -893,16 +864,8 @@ onMounted(() => { watch( () => route.path + JSON.stringify(route.query), async (newRoute, oldRoute) => { - console.log('🔍 [DialogueSession] 路由变化:', { - newRoute, - oldRoute, - path: route.path, - query: route.query - }); - // 🔑 关键修复:当路由变为根路径时,执行自动跳转逻辑 if (route.path === '/' && Object.keys(route.query || {}).length === 0) { - console.log('🔍 [DialogueSession] 检测到路由变为根路径,执行自动跳转逻辑'); await handleRootPathRedirect(); } } @@ -912,17 +875,9 @@ watch( watch( () => currentSelectedSession.value, (newSessionId, oldSessionId) => { - if (newSessionId && newSessionId !== oldSessionId) { - console.log('🔍 [DialogueSession] currentSelectedSession变化:', { - newSessionId, - oldSessionId, - currentQuery: route.query || {}, - user_selected_app: user_selected_app.value - }); - + if (newSessionId && newSessionId !== oldSessionId) { // 只有当URL中的conversationId与当前session不一致时才更新 if ((route.query?.conversationId) !== newSessionId) { - console.log('🔍 [DialogueSession] currentSelectedSession变化,同步到URL:', newSessionId); const currentQuery = { ...(route.query || {}) }; currentQuery.conversationId = newSessionId; @@ -934,9 +889,7 @@ watch( currentQuery.name = selectedApp.name; } } - - console.log('🔍 [DialogueSession] 更新URL参数:', currentQuery); - + router.replace({ path: route.path, query: currentQuery @@ -1016,15 +969,8 @@ const getappMode = (appId: string) => { watch( () => user_selected_app.value, (newAppId, oldAppId) => { - console.log('🔍 [DialogueSession] user_selected_app变化:', { - newAppId, - oldAppId, - appValueAppId: app.value?.appId - }); - // 🔑 重要修复:app切换时先清空之前的变量和session,避免显示错误的变量 if (newAppId !== oldAppId) { - console.log('🔍 [DialogueSession] App切换,清空变量缓存和session'); conversationVariables.value = []; showVariablePanel.value = false; variablePanelMinimized.value = false; @@ -1041,10 +987,6 @@ watch( if (newAppId && !isCreateApp.value) { getappMode(newAppId); - // 🔑 修复:根据正确的交互逻辑,选择APP只是设置状态,不创建conversation - // 只有点击"新建对话"时才创建conversation - console.log('🔍 [DialogueSession] App选择,加载变量配置但不创建conversation'); - // 加载变量配置以便显示变量面板 const shouldLoadVariables = !oldAppId || oldAppId !== newAppId; if (shouldLoadVariables) { @@ -1052,7 +994,6 @@ watch( } } else if (!newAppId && oldAppId) { // 🔑 修复:切换到无app模式时,确保对话界面显示 - console.log('🔍 [DialogueSession] 切换到无app模式,显示对话界面'); conversationStarted.value = true; }