From 4f7893950fa124259c363d2d47aa8f58e3daab69 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 3 Jun 2026 15:56:00 +0800 Subject: [PATCH] fix(chat): improve model selection handling in chat component - Updated the logic in `Input-field.vue` to ensure that user-selected chat models are preserved when navigating away and returning, preventing unintended resets to default models. - Enhanced the watch functionality to better manage model ID changes, ensuring consistent behavior when shared agents are involved. - Modified `index.vue` to update conversation models based on the first model ID, improving the initialization process for chat sessions. --- frontend/src/components/Input-field.vue | 28 ++++++++++++++++++------- frontend/src/views/chat/index.vue | 9 +++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index ca31f8a8..8d6dfb55 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -805,15 +805,29 @@ const ensureModelSelection = () => { }; // 智能体身份或其数据到位时,把对话模型同步到智能体配置的 model_id。 -// 修复场景:导航离开再返回时,onMounted 中的 initChatModelSelection 会用 -// localStorage 的 lastPick(用户上一次手动挑选)覆盖 store 中的 selectedChatModelId, -// 把共享智能体绑定的源租户 model_id 冲掉,UI 显示「未配置」。 -// 该 watch 在 loadAgents/sharedAgents 异步返回让 agentModelId 由空变非空时也会触发, -// 与 handleSelectAgent 中的同步逻辑保持一致:智能体「拥有」其模型选择。 +// 修复场景:导航离开再返回时,initChatModelSelection 会用 localStorage 的 lastPick +// 覆盖共享智能体绑定的源租户 model_id,UI 显示「未配置」——此时需要拉回 agent 模型。 +// 但若用户在本页手动改过模型(lastPick 与 agent 默认不同且当前选中即为 lastPick), +// 则保留用户选择,避免 creatChat → chat 跳转后把模型 B 冲回智能体默认 A。 watch( [selectedAgentId, () => settingsStore.selectedAgentSourceTenantId, agentModelId], - ([, , newModelId]) => { - if (newModelId && newModelId.trim() !== '' && newModelId !== selectedModelId.value) { + ([, sourceTenantId, newModelId]) => { + if (!newModelId || newModelId.trim() === '') return; + + const lastPick = readLastChatModelID(); + const isSharedAgent = !!sourceTenantId; + const agentModelInList = availableModels.value.some(m => m.id === newModelId); + + if ( + lastPick && + selectedModelId.value === lastPick && + lastPick !== newModelId && + (!isSharedAgent || agentModelInList) + ) { + return; + } + + if (newModelId !== selectedModelId.value) { selectedModelId.value = newModelId; } }, diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index 5118b309..cb902d31 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -1407,7 +1407,14 @@ onMounted(async () => { if (firstQuery.value) { scrollLock.value = true; historyLoading.value = false; - sendMsg(firstQuery.value, firstModelId.value || '', firstMentionedItems.value || [], firstImageFiles.value || [], firstAttachmentFiles.value || []); + if (firstModelId.value) { + useSettingsStoreInstance.updateConversationModels({ + summaryModelId: firstModelId.value, + selectedChatModelId: firstModelId.value, + rerankModelId: '', + }); + } + sendMsg(firstQuery.value, firstModelId.value || '', firstMentionedItems.value || [], firstImageFiles.value || [], firstAttachmentFiles.value || []); usemenuStore.changeFirstQuery('', [], '', [], []); } else { scrollLock.value = false;