Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(chat): clear stale KB scope on session restore

bigdu332bdad5eea2b46
diff --git a/frontend/src/stores/browserSource.test.ts b/frontend/src/stores/browserSource.test.ts
index a374c1240..4774dfc85 100644
--- a/frontend/src/stores/browserSource.test.ts
+++ b/frontend/src/stores/browserSource.test.ts
@@ -82,6 +82,27 @@ test('session restore follows saved browser choice and clears it for older sessi
assert.equal(store.settings.webSearchEnabled, true)
})
+test('session restore clears a stale knowledge base when the saved scope is absent', () => {
+ items.clear()
+ const store = useSettingsStore(createPinia())
+ store.selectKnowledgeBases(['kb-default'])
+
+ store.hydrateSessionInputState({ knowledge_base_ids: ['kb-session-a'] })
+ assert.deepEqual(store.settings.selectedKnowledgeBases, ['kb-session-a'])
+
+ // Switching sessions first restores the browser defaults, which may still
+ // contain a KB. A last-request state with no KB field must clear that value:
+ // the backend omits empty slices when it persists a request with no scope.
+ store.restoreDefaultsIfSnapshotted()
+ assert.deepEqual(store.settings.selectedKnowledgeBases, ['kb-default'])
+ store.hydrateSessionInputState({ agent_enabled: true })
+ assert.deepEqual(store.settings.selectedKnowledgeBases, [])
+
+ // Leaving the session still restores the user's browser-level default.
+ store.restoreDefaultsIfSnapshotted()
+ assert.deepEqual(store.settings.selectedKnowledgeBases, ['kb-default'])
+})
+
test('new-session hydration preserves the createChat draft after the first query is consumed', async () => {
items.clear()
const store = useSettingsStore(createPinia())
diff --git a/frontend/src/stores/settings.ts b/frontend/src/stores/settings.ts
index 41aa07513..bb0d87206 100644
--- a/frontend/src/stores/settings.ts
+++ b/frontend/src/stores/settings.ts
@@ -526,7 +526,7 @@ export const useSettingsStore = defineStore("settings", {
// 根据 session.last_request_state 覆盖输入栏相关字段。
// 只触碰本次记录的字段,**不**清空 store 中其它无关字段(如模型列表)。
- // 任何字段缺失则保留 store 现值,做"尽力恢复"。
+ // 一般字段缺失时做"尽力恢复";知识库范围例外,缺失代表本会话未选中知识库。
applyLastRequestState(state: SessionLastRequestStatePayload | null | undefined) {
if (!state) return;
this._isApplyingSessionState = true;
@@ -545,9 +545,11 @@ export const useSettingsStore = defineStore("settings", {
const current = this.settings.conversationModels || defaultSettings.conversationModels;
this.settings.conversationModels = { ...current, selectedChatModelId: state.model_id || "" };
}
- if (Array.isArray(state.knowledge_base_ids)) {
- this.settings.selectedKnowledgeBases = [...state.knowledge_base_ids];
- }
+ // 后端会用 omitempty 省略空列表,所以缺失值表示“本会话没有 KB 范围”,
+ // 而不是“保留上一个会话的选择”。后者会把过期的 @KB 继续发给服务端。
+ this.settings.selectedKnowledgeBases = Array.isArray(state.knowledge_base_ids)
+ ? [...state.knowledge_base_ids]
+ : [];
if (Array.isArray(state.knowledge_ids)) {
this.settings.selectedFiles = [...state.knowledge_ids];
// selectedFileKbMap 此时无法重建(state 里没存 KB 归属),交给前端按