diff --git a/src/views/dialogue/components/DialogueSession.vue b/src/views/dialogue/components/DialogueSession.vue index 657844e3b204b8641e5e6a6b8ea6cd4209fe3e37..86a5e5ac2e4cc9e5890709b88eed7a96be121398 100644 --- a/src/views/dialogue/components/DialogueSession.vue +++ b/src/views/dialogue/components/DialogueSession.vue @@ -995,7 +995,7 @@ const getFullModelInfo = async (llmId: string): Promise => { // 🔑 优先级2:如果 llmOptions 为空或未找到,才调用 API 查询 try { - const [_, res] = await api.getLLMList({ type: 'chat' }); + const [_, res] = await api.getLLMList('chat'); if (!_ && res && res.code === 200) { const allModels = res.result || []; const chatModels = allModels.filter(model => { @@ -1028,10 +1028,28 @@ const getFullModelInfo = async (llmId: string): Promise => { return null; }; +// 🔑 新增:根据 conversationId 直接获取会话的 llm 信息 +const getCurrentSessionLLM = async (conversationId: string): Promise<{ icon: string; modelName: string; llmId: string } | null> => { + const [err, res] = await api.getSessionRecord(); + if (!err && res && res.code === 200) { + const conversations = res.result.conversations || []; + const currentConv = conversations.find((conv: any) => conv.conversationId === conversationId); + + if (currentConv && currentConv.llm && currentConv.llm.llmId) { + return { + icon: currentConv.llm.icon || '', + modelName: currentConv.llm.modelName || '', + llmId: currentConv.llm.llmId + }; + } + } + return null; +}; + // 🔑 修复:添加 forceReselect 参数,确保每次页面进入都能重新选择模型 const getProviderLLM = async (forceReselect: boolean = false) => { // 🔑 修复:使用查询参数type限制只查询chat类型的模型 - const [_, res] = await api.getLLMList({ type: 'chat' }); + const [_, res] = await api.getLLMList('chat'); if (!_ && res && res.code === 200) { const allModels = res.result || []; // 🔑 修复:适配后端type从字符串改为字符串数组的变更 @@ -1050,61 +1068,34 @@ const getProviderLLM = async (forceReselect: boolean = false) => { }); llmOptions.value = chatModels; - // 🔑 模型选择优先级逻辑 - // 1. 如果 forceReselect 为 true,强制重新选择 - // 2. 如果已经有选中的模型且不是强制重选,则跳过 + // 🔑 简化逻辑:后端已处理用户偏好,前端只需根据 conversation.llm 查找完整信息 const currentLlmId = selectedLLM.value?.llmId || selectedLLM.value?.id; if (llmOptions.value.length > 0 && (!currentLlmId || forceReselect)) { - // 🔑 优先级1:检查当前会话的历史记录是否有模型信息 - // 只有当会话有对话记录时才使用历史模型(表示这是已存在的对话) - const currentSession = historySession.value.find(session => - session.conversationId === currentSelectedSession.value - ); - - // 🔑 关键修复:只有当会话有对话记录时,才使用历史模型 - // 新创建的空会话应该使用用户偏好模型 - if (currentSession && - currentSession.llm && - currentSession.llm.llmId && - conversationList.value.length > 0) { - // 🔑 优化方案:优先从 llmOptions 查找,避免重复 API 请求 - const fullModel = await getFullModelInfo(currentSession.llm.llmId); + // 🔑 优化:直接从 API 获取当前会话的 llm 信息 + // 不依赖 historySession,避免因为 filter 过滤掉当前会话而找不到 + if (currentSelectedSession.value) { + const sessionLLM = await getCurrentSessionLLM(currentSelectedSession.value); - if (fullModel) { - selectedLLM.value = { - llmId: fullModel.llmId || fullModel.id, - id: fullModel.id || fullModel.llmId, - modelName: fullModel.modelName, - icon: fullModel.icon || '', - supports_thinking: fullModel.supportsThinking || false, - can_toggle_thinking: fullModel.canToggleThinking || false - }; - return; // 找到历史模型,直接返回 + if (sessionLLM && sessionLLM.llmId) { + const fullModel = await getFullModelInfo(sessionLLM.llmId); + + if (fullModel) { + selectedLLM.value = { + llmId: fullModel.llmId || fullModel.id, + id: fullModel.id || fullModel.llmId, + modelName: fullModel.modelName, + icon: fullModel.icon || '', + supports_thinking: fullModel.supportsThinking || false, + can_toggle_thinking: fullModel.canToggleThinking || false + }; + } } } - - // 🔑 优先级2:使用用户偏好设置中的chat模型(新对话或无历史模型) - applyUserPreferredModel(); } } }; -// 🔑 新增:应用用户偏好模型的独立函数 -const applyUserPreferredModel = () => { - const preferredModel = getPreferredReasoningModel(llmOptions.value); - if (preferredModel) { - selectedLLM.value = { - llmId: preferredModel.llmId || preferredModel.id, - id: preferredModel.id || preferredModel.llmId, - modelName: preferredModel.modelName, - icon: preferredModel.icon || '', - supports_thinking: preferredModel.supportsThinking || false, - can_toggle_thinking: preferredModel.canToggleThinking || false - }; - } -}; - const autoExecuteChange = async (value) => { autoExecuteRef.value = value; await nextTick(); @@ -1202,20 +1193,22 @@ onMounted(async () => { if (!inputRef.value) return; inputRef.value.focus(); - getProviderLLM(); + // 🔑 修复:不在这里调用 getProviderLLM,等待 currentSelectedSession 设置后由 watch 触发 + // 这样能确保在有会话信息时才获取模型,避免时序问题 // 🔑 修复:从路由参数中恢复conversationId并同步左侧对话记录栏选中状态 if (route.query?.conversationId && typeof route.query.conversationId === 'string') { const conversationId = route.query.conversationId; + + // 🔑 简化:不再需要预加载 historySession,直接设置 currentSelectedSession + // getProviderLLM 内部会通过 getCurrentSessionLLM 直接从 API 获取会话信息 + currentSelectedSession.value = conversationId; - // 🔑 优化:只在DialogueAside没有加载历史记录时才调用getHistorySession - // 避免与DialogueAside的onMounted重复调用 + // 检查该conversationId是否存在于历史记录中 if (historySession.value.length === 0) { await getHistorySession(); } - - // 检查该conversationId是否存在于历史记录中 const sessionExists = historySession.value.some(session => session.conversationId === conversationId); if (sessionExists) { // 如果存在,调用changeSession来同步状态(不重复设置loading状态) @@ -1354,19 +1347,15 @@ watch( }, ); -// 🔑 恢复:监听 selectLLM 变化,但添加条件判断 -// 只有当会话有对话记录时才应用历史模型,新对话使用用户偏好模型 +// 🔑 简化:监听 selectLLM 变化,直接使用 conversation 的 llm 信息 +// 后端已处理用户偏好,前端只需查找完整的模型能力信息 watch(selectLLM, async (newValue) => { - // 🔑 条件判断: - // 1. newValue 必须有值且有ID - // 2. 当前没有选中模型(避免覆盖用户手动选择) - // 3. 会话有对话记录(表示是已存在的对话) + // 只在当前没有选中模型时才应用(避免覆盖用户手动选择) if (newValue && (newValue.llmId || newValue.id) && - !selectedLLM.value && - conversationList.value.length > 0) { + !selectedLLM.value) { - // 🔑 优化方案:优先从 llmOptions 查找完整信息,避免重复 API 请求 + // 🔑 优化:从 llmOptions 查找完整信息,包含模型能力字段 const llmId = newValue.llmId || newValue.id; const fullModel = await getFullModelInfo(llmId); @@ -1433,14 +1422,11 @@ watch( conversationList.value = []; } - // 🔑 新增:如果有可用的LLM选项,立即重新应用模型选择逻辑 - if (llmOptions.value.length > 0) { - // 延迟执行,确保会话切换完成后再选择模型 - // 🔑 修复:传入 true 强制重新选择,确保按优先级选择正确的模型 - nextTick(() => { - getProviderLLM(true); - }); - } + // 🔑 修复:无论 llmOptions 是否已加载,都调用 getProviderLLM + // getProviderLLM 内部会自己获取模型列表 + nextTick(() => { + getProviderLLM(true); + }); } }, {