Skip to content

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

refactor(frontend): enhance chat resources store for knowledge bases and agents

wizardchene78a9482d45e
diff --git a/frontend/src/stores/chatResources.ts b/frontend/src/stores/chatResources.ts
index 4b15e7cad..1d58030c6 100644
--- a/frontend/src/stores/chatResources.ts
+++ b/frontend/src/stores/chatResources.ts
@@ -30,6 +30,10 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
const loadedAt = ref<Partial<Record<ResourceKey, number>>>({})
const inflight = new Map<ResourceKey, Promise<void>>()
+ // creator==='all' 的列表请求单独去重:首屏 platform 预取与对话页 onMounted
+ // 可能并发触发,缓存尚未写入时不去重会重复打 listKnowledgeBases / listAgents。
+ let kbAllInflight: Promise<any[]> | null = null
+ let agentsAllInflight: Promise<{ data: CustomAgent[]; disabled_own_agent_ids: string[] }> | null = null
const agentKbCache = new Map<string, { at: number; data: any[] }>()
const agentKbInflight = new Map<string, Promise<any[]>>()
@@ -63,23 +67,31 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
force = false,
): Promise<any[]> {
const creator = params?.creator ?? 'all'
- if (!force && creator === 'all' && isFresh('knowledgeBases')) {
- return rawKnowledgeBases.value
+ // 带 creator 过滤的列表是列表页专用、不进缓存,直接透传请求。
+ if (creator !== 'all') {
+ const res: any = await listKnowledgeBases({ creator })
+ return res?.data && Array.isArray(res.data) ? res.data : []
}
- const res: any = await listKnowledgeBases(
- creator === 'all' ? undefined : { creator },
- )
- const data = res?.data && Array.isArray(res.data) ? res.data : []
-
- if (creator === 'all') {
- rawKnowledgeBases.value = data
- loadedAt.value.knowledgeBases = Date.now()
- const orgStore = useOrganizationStore()
- await orgStore.fetchSharedKnowledgeBases({ force })
+ if (!force && isFresh('knowledgeBases')) {
+ return rawKnowledgeBases.value
}
+ if (!force && kbAllInflight) return kbAllInflight
- return data
+ kbAllInflight = (async () => {
+ try {
+ const res: any = await listKnowledgeBases()
+ const data = res?.data && Array.isArray(res.data) ? res.data : []
+ rawKnowledgeBases.value = data
+ loadedAt.value.knowledgeBases = Date.now()
+ const orgStore = useOrganizationStore()
+ await orgStore.fetchSharedKnowledgeBases({ force })
+ return data
+ } finally {
+ kbAllInflight = null
+ }
+ })()
+ return kbAllInflight
}
async function ensureKnowledgeBases(force = false): Promise<void> {
@@ -94,25 +106,40 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
force = false,
): Promise<{ data: CustomAgent[]; disabled_own_agent_ids: string[] }> {
const creator = params?.creator ?? 'all'
- if (!force && creator === 'all' && isFresh('agents')) {
- return { data: agents.value, disabled_own_agent_ids: disabledOwnAgentIds.value }
- }
-
const orgStore = useOrganizationStore()
- const [agentsRes] = await Promise.all([
- listAgents(creator === 'all' ? undefined : { creator }),
- orgStore.fetchSharedAgents({ force }),
- ])
- const res = agentsRes as { data?: CustomAgent[]; disabled_own_agent_ids?: string[] }
- const data = res.data || []
- if (creator === 'all') {
- agents.value = data
- disabledOwnAgentIds.value = res.disabled_own_agent_ids || []
- loadedAt.value.agents = Date.now()
+ // 带 creator 过滤的列表不进缓存,但仍需刷新共享智能体(与全量路径保持一致)。
+ if (creator !== 'all') {
+ const [agentsRes] = await Promise.all([
+ listAgents({ creator }),
+ orgStore.fetchSharedAgents({ force }),
+ ])
+ const res = agentsRes as { data?: CustomAgent[]; disabled_own_agent_ids?: string[] }
+ return { data: res.data || [], disabled_own_agent_ids: res.disabled_own_agent_ids || [] }
+ }
+
+ if (!force && isFresh('agents')) {
+ return { data: agents.value, disabled_own_agent_ids: disabledOwnAgentIds.value }
}
+ if (!force && agentsAllInflight) return agentsAllInflight
- return { data, disabled_own_agent_ids: res.disabled_own_agent_ids || [] }
+ agentsAllInflight = (async () => {
+ try {
+ const [agentsRes] = await Promise.all([
+ listAgents(),
+ orgStore.fetchSharedAgents({ force }),
+ ])
+ const res = agentsRes as { data?: CustomAgent[]; disabled_own_agent_ids?: string[] }
+ const data = res.data || []
+ agents.value = data
+ disabledOwnAgentIds.value = res.disabled_own_agent_ids || []
+ loadedAt.value.agents = Date.now()
+ return { data, disabled_own_agent_ids: res.disabled_own_agent_ids || [] }
+ } finally {
+ agentsAllInflight = null
+ }
+ })()
+ return agentsAllInflight
}
async function ensureAgents(force = false): Promise<void> {
@@ -222,6 +249,8 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
allModels.value = []
webSearchProviders.value = []
agentKbCache.clear()
+ kbAllInflight = null
+ agentsAllInflight = null
invalidateKnowledgeBaseDetail()
return
}
@@ -230,8 +259,12 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
})
if (keys.includes('knowledgeBases')) {
agentKbCache.clear()
+ kbAllInflight = null
invalidateKnowledgeBaseDetail()
}
+ if (keys.includes('agents')) {
+ agentsAllInflight = null
+ }
}
return {
diff --git a/frontend/src/stores/settings.ts b/frontend/src/stores/settings.ts
index 787b4b573..4a11a80d8 100644
--- a/frontend/src/stores/settings.ts
+++ b/frontend/src/stores/settings.ts
@@ -1,4 +1,5 @@
import { defineStore } from "pinia";
+import { nextTick } from "vue";
import { BUILTIN_QUICK_ANSWER_ID, BUILTIN_SMART_REASONING_ID } from "@/api/agent";
import { getApiBaseUrl } from "@/utils/api-base";
import { updateMyPreferences, type UserPreferences } from "@/api/auth";
@@ -479,7 +480,13 @@ export const useSettingsStore = defineStore("settings", {
this.settings.webSearchEnabled = state.web_search_enabled;
}
} finally {
- this._isApplyingSessionState = false;
+ // 复位必须延后到下一次 flush 之后:监听 selectedAgentId 的 watcher 默认
+ // flush:'pre',是异步执行的;若在此处同步复位,watcher 真正运行时标志早已
+ // 为 false,守卫形同虚设、恢复出来的 KB 仍会被 agent 配置覆盖。放到 nextTick
+ // 可保证本次状态变更触发的 watcher 在标志仍为 true 时执行。
+ nextTick(() => {
+ this._isApplyingSessionState = false;
+ });
}
// 注意:故意不写 localStorage —— 旧会话的状态不应污染"用户默认"。
// 离开会话时 restoreDefaultsIfSnapshotted 会把 localStorage 里那份完整