11import { defineStore } from 'pinia'
2- import { ref , computed } from 'vue'
2+ import { ref , computed , watch } from 'vue'
33import { listKnowledgeBases , getKnowledgeBaseById } from '@/api/knowledge-base'
44import { listAgents , type CustomAgent } from '@/api/agent'
55import { listModels , type ModelConfig } from '@/api/model'
66import { listWebSearchProviders , type WebSearchProviderEntity } from '@/api/web-search-provider'
77import { isNamedSandboxBackend , listSandboxConfigs , type SandboxConfigRecord } from '@/api/system'
88import { useOrganizationStore } from '@/stores/organization'
9+ import { getCurrentLanguage } from '@/utils/request'
10+ import {
11+ isLocalizedCacheFresh ,
12+ shouldCommitLocalizedGeneration ,
13+ shouldReuseLocalizedInflight ,
14+ } from './localizedResourceCache'
915
1016/** 空间级资源缓存 TTL */
1117const CACHE_TTL_MS = 60_000
@@ -49,11 +55,37 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
4955 const validKnowledgeBases = computed ( ( ) => rawKnowledgeBases . value . filter ( isKbModelReady ) )
5056 const chatModels = computed ( ( ) => allModels . value . filter ( ( m ) => m . type === 'KnowledgeQA' ) )
5157
58+ // 内置智能体名称/描述由后端按 Accept-Language 本地化返回;切换 UI 语言后
59+ // 旧缓存必须立即失效,否则要等 TTL 过期或强刷才能看到正确语言。
60+ // agentsLoadedLocale 必须是「请求发起时」的语言,不能在 await 之后再读当前语言。
61+ let agentsLoadedLocale = ''
62+ let agentsAllInflightLocale = ''
63+
5264 function isFresh ( key : ResourceKey ) : boolean {
53- const at = loadedAt . value [ key ]
54- return ! ! at && Date . now ( ) - at < CACHE_TTL_MS
65+ const at = loadedAt . value [ key ] ?? 0
66+ if ( key === 'agents' ) {
67+ return isLocalizedCacheFresh ( at , agentsLoadedLocale , getCurrentLanguage ( ) , CACHE_TTL_MS )
68+ }
69+ return at > 0 && Date . now ( ) - at < CACHE_TTL_MS
5570 }
5671
72+ function bumpAgentsGeneration ( ) {
73+ agentsAllGen ++
74+ agentsAllInflight = null
75+ agentsAllInflightLocale = ''
76+ }
77+
78+ watch (
79+ ( ) => getCurrentLanguage ( ) ,
80+ ( locale ) => {
81+ if ( agentsLoadedLocale && agentsLoadedLocale !== locale ) {
82+ delete loadedAt . value . agents
83+ agentsLoadedLocale = ''
84+ bumpAgentsGeneration ( )
85+ }
86+ } ,
87+ )
88+
5789 async function runOnce ( key : ResourceKey , force : boolean , loader : ( ) => Promise < void > ) : Promise < void > {
5890 if ( ! force && isFresh ( key ) ) return
5991 const existing = inflight . get ( key )
@@ -125,12 +157,20 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
125157 return { data : res . data || [ ] , disabled_own_agent_ids : res . disabled_own_agent_ids || [ ] }
126158 }
127159
160+ const locale = getCurrentLanguage ( )
128161 if ( ! force && isFresh ( 'agents' ) ) {
129162 return { data : agents . value , disabled_own_agent_ids : disabledOwnAgentIds . value }
130163 }
131- if ( ! force && agentsAllInflight ) return agentsAllInflight
164+ if (
165+ ! force &&
166+ shouldReuseLocalizedInflight ( ! ! agentsAllInflight , agentsAllInflightLocale , locale )
167+ ) {
168+ return agentsAllInflight as Promise < { data : CustomAgent [ ] ; disabled_own_agent_ids : string [ ] } >
169+ }
132170
133171 const gen = ++ agentsAllGen
172+ const requestLocale = locale
173+ agentsAllInflightLocale = requestLocale
134174 agentsAllInflight = ( async ( ) => {
135175 try {
136176 const [ agentsRes ] = await Promise . all ( [
@@ -139,12 +179,19 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
139179 ] )
140180 const res = agentsRes as { data ?: CustomAgent [ ] ; disabled_own_agent_ids ?: string [ ] }
141181 const data = res . data || [ ]
142- agents . value = data
143- disabledOwnAgentIds . value = res . disabled_own_agent_ids || [ ]
144- loadedAt . value . agents = Date . now ( )
145- return { data, disabled_own_agent_ids : res . disabled_own_agent_ids || [ ] }
182+ const disabled = res . disabled_own_agent_ids || [ ]
183+ if ( shouldCommitLocalizedGeneration ( gen , agentsAllGen ) ) {
184+ agents . value = data
185+ disabledOwnAgentIds . value = disabled
186+ loadedAt . value . agents = Date . now ( )
187+ agentsLoadedLocale = requestLocale
188+ }
189+ return { data, disabled_own_agent_ids : disabled }
146190 } finally {
147- if ( agentsAllGen === gen ) agentsAllInflight = null
191+ if ( shouldCommitLocalizedGeneration ( gen , agentsAllGen ) ) {
192+ agentsAllInflight = null
193+ agentsAllInflightLocale = ''
194+ }
148195 }
149196 } ) ( )
150197 return agentsAllInflight
@@ -290,7 +337,8 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
290337 inflight . clear ( )
291338 agentKbInflight . clear ( )
292339 kbAllInflight = null
293- agentsAllInflight = null
340+ agentsLoadedLocale = ''
341+ bumpAgentsGeneration ( )
294342 invalidateKnowledgeBaseDetail ( )
295343 return
296344 }
@@ -305,7 +353,8 @@ export const useChatResourcesStore = defineStore('chatResources', () => {
305353 invalidateKnowledgeBaseDetail ( )
306354 }
307355 if ( keys . includes ( 'agents' ) ) {
308- agentsAllInflight = null
356+ agentsLoadedLocale = ''
357+ bumpAgentsGeneration ( )
309358 }
310359 }
311360
0 commit comments