From 4de3c3efbd37a007f6353def287155c689dacc52 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 9 Jun 2026 18:03:28 +0800 Subject: [PATCH] fix(frontend): harden chat resources cache invalidation and dedup - Invalidate the single-KB detail cache when editing a KB from the list page, matching the detail page so the sidebar/detail no longer show a stale name/config for up to the cache TTL. - Reject (instead of swallowing) listModels network failures so the cache layer can tell a transient failure apart from a genuinely empty model list and avoid caching the empty result for the whole TTL. All UI callers already wrap listModels in try/catch. - Guard the kb/agent in-flight handles with a generation counter so a concurrent force refresh cannot null out a still-running request, and clear all in-flight maps on invalidate() so stale requests cannot repopulate the cache after invalidation. --- frontend/src/api/model/index.ts | 4 +++- frontend/src/stores/chatResources.ts | 15 +++++++++++++-- .../src/views/knowledge/KnowledgeBaseList.vue | 2 ++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/frontend/src/api/model/index.ts b/frontend/src/api/model/index.ts index 9f68291d..bdddc5cb 100644 --- a/frontend/src/api/model/index.ts +++ b/frontend/src/api/model/index.ts @@ -80,7 +80,9 @@ export function listModels(type?: string): Promise { }) .catch((error: any) => { console.error('Failed to list models:', error); - resolve([]); + // 抛出而非吞掉:调用方(含缓存层)才能区分「真失败」与「成功但无模型」, + // 避免把一次瞬时失败的空结果缓存下来。各 UI 调用点均已 try/catch 兜底。 + reject(error); }); }); } diff --git a/frontend/src/stores/chatResources.ts b/frontend/src/stores/chatResources.ts index 1d58030c..5881d484 100644 --- a/frontend/src/stores/chatResources.ts +++ b/frontend/src/stores/chatResources.ts @@ -34,6 +34,10 @@ export const useChatResourcesStore = defineStore('chatResources', () => { // 可能并发触发,缓存尚未写入时不去重会重复打 listKnowledgeBases / listAgents。 let kbAllInflight: Promise | null = null let agentsAllInflight: Promise<{ data: CustomAgent[]; disabled_own_agent_ids: string[] }> | null = null + // 代际计数:force 与非 force 并发时句柄会被后来者覆盖,旧请求结束时凭此判断 + // 自己是否仍是最新的那次,避免误清正在飞行的句柄。 + let kbAllGen = 0 + let agentsAllGen = 0 const agentKbCache = new Map() const agentKbInflight = new Map>() @@ -78,6 +82,7 @@ export const useChatResourcesStore = defineStore('chatResources', () => { } if (!force && kbAllInflight) return kbAllInflight + const gen = ++kbAllGen kbAllInflight = (async () => { try { const res: any = await listKnowledgeBases() @@ -88,7 +93,7 @@ export const useChatResourcesStore = defineStore('chatResources', () => { await orgStore.fetchSharedKnowledgeBases({ force }) return data } finally { - kbAllInflight = null + if (kbAllGen === gen) kbAllInflight = null } })() return kbAllInflight @@ -123,6 +128,7 @@ export const useChatResourcesStore = defineStore('chatResources', () => { } if (!force && agentsAllInflight) return agentsAllInflight + const gen = ++agentsAllGen agentsAllInflight = (async () => { try { const [agentsRes] = await Promise.all([ @@ -136,7 +142,7 @@ export const useChatResourcesStore = defineStore('chatResources', () => { loadedAt.value.agents = Date.now() return { data, disabled_own_agent_ids: res.disabled_own_agent_ids || [] } } finally { - agentsAllInflight = null + if (agentsAllGen === gen) agentsAllInflight = null } })() return agentsAllInflight @@ -249,6 +255,9 @@ export const useChatResourcesStore = defineStore('chatResources', () => { allModels.value = [] webSearchProviders.value = [] agentKbCache.clear() + // 同时丢弃所有 inflight 句柄,否则失效后仍在飞行的请求会把旧数据写回缓存。 + inflight.clear() + agentKbInflight.clear() kbAllInflight = null agentsAllInflight = null invalidateKnowledgeBaseDetail() @@ -256,9 +265,11 @@ export const useChatResourcesStore = defineStore('chatResources', () => { } keys.forEach((k) => { delete loadedAt.value[k] + inflight.delete(k) }) if (keys.includes('knowledgeBases')) { agentKbCache.clear() + agentKbInflight.clear() kbAllInflight = null invalidateKnowledgeBaseDetail() } diff --git a/frontend/src/views/knowledge/KnowledgeBaseList.vue b/frontend/src/views/knowledge/KnowledgeBaseList.vue index 241ff858..f7771021 100644 --- a/frontend/src/views/knowledge/KnowledgeBaseList.vue +++ b/frontend/src/views/knowledge/KnowledgeBaseList.vue @@ -1653,6 +1653,8 @@ const handleCreateKnowledgeBase = () => { const handleKBEditorSuccess = (kbId: string) => { console.log('[KnowledgeBaseList] knowledge operation success:', kbId) const shouldOpenDetailForUploadGuide = !isContextualGuideDone('kbDetail') + // 列表页编辑同样要让单 KB 详情缓存失效,否则侧栏 / 详情页 60s 内仍显示旧信息 + chatResources.invalidateKnowledgeBaseDetail(kbId) fetchList(true).then(() => { if (shouldOpenDetailForUploadGuide && kbId) { goDetail(kbId)