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.
This commit is contained in:
@@ -80,7 +80,9 @@ export function listModels(type?: string): Promise<ModelConfig[]> {
|
||||
})
|
||||
.catch((error: any) => {
|
||||
console.error('Failed to list models:', error);
|
||||
resolve([]);
|
||||
// 抛出而非吞掉:调用方(含缓存层)才能区分「真失败」与「成功但无模型」,
|
||||
// 避免把一次瞬时失败的空结果缓存下来。各 UI 调用点均已 try/catch 兜底。
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -34,6 +34,10 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
|
||||
// 可能并发触发,缓存尚未写入时不去重会重复打 listKnowledgeBases / listAgents。
|
||||
let kbAllInflight: Promise<any[]> | 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<string, { at: number; data: any[] }>()
|
||||
const agentKbInflight = new Map<string, Promise<any[]>>()
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user