From f66deb36ddabca7df54d09bd8d1575fd76728f2b Mon Sep 17 00:00:00 2001 From: Supra4E8C Date: Fri, 27 Mar 2026 21:49:22 +0800 Subject: [PATCH] fix(models): avoid cross-session stale model cache --- src/stores/useAuthStore.ts | 3 +++ src/stores/useModelsStore.ts | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/stores/useAuthStore.ts b/src/stores/useAuthStore.ts index 64d920f..1c39b8b 100644 --- a/src/stores/useAuthStore.ts +++ b/src/stores/useAuthStore.ts @@ -11,6 +11,7 @@ import { secureStorage } from '@/services/storage/secureStorage'; import { apiClient } from '@/services/api/client'; import { useConfigStore } from './useConfigStore'; import { useUsageStatsStore } from './useUsageStatsStore'; +import { useModelsStore } from './useModelsStore'; import { detectApiBaseFromLocation, normalizeApiBase } from '@/utils/connection'; interface AuthStoreState extends AuthState { @@ -94,6 +95,7 @@ export const useAuthStore = create()( try { set({ connectionStatus: 'connecting' }); + useModelsStore.getState().clearCache(); // 配置 API 客户端 apiClient.setConfig({ @@ -138,6 +140,7 @@ export const useAuthStore = create()( restoreSessionPromise = null; useConfigStore.getState().clearCache(); useUsageStatsStore.getState().clearUsageStats(); + useModelsStore.getState().clearCache(); set({ isAuthenticated: false, apiBase: '', diff --git a/src/stores/useModelsStore.ts b/src/stores/useModelsStore.ts index 2c5a08d..3489b33 100644 --- a/src/stores/useModelsStore.ts +++ b/src/stores/useModelsStore.ts @@ -11,6 +11,7 @@ interface ModelsCache { data: ModelInfo[]; timestamp: number; apiBase: string; + apiKey: string; } interface ModelsState { @@ -21,7 +22,7 @@ interface ModelsState { fetchModels: (apiBase: string, apiKey?: string, forceRefresh?: boolean) => Promise; clearCache: () => void; - isCacheValid: (apiBase: string) => boolean; + isCacheValid: (apiBase: string, apiKey?: string) => boolean; } export const useModelsStore = create((set, get) => ({ @@ -32,9 +33,10 @@ export const useModelsStore = create((set, get) => ({ fetchModels: async (apiBase, apiKey, forceRefresh = false) => { const { cache, isCacheValid } = get(); + const apiKeyScope = apiKey?.trim() || ''; // 检查缓存 - if (!forceRefresh && isCacheValid(apiBase) && cache) { + if (!forceRefresh && isCacheValid(apiBase, apiKeyScope) && cache) { set({ models: cache.data, error: null }); return cache.data; } @@ -42,13 +44,13 @@ export const useModelsStore = create((set, get) => ({ set({ loading: true, error: null }); try { - const list = await modelsApi.fetchModels(apiBase, apiKey); + const list = await modelsApi.fetchModels(apiBase, apiKeyScope || undefined); const now = Date.now(); set({ models: list, loading: false, - cache: { data: list, timestamp: now, apiBase } + cache: { data: list, timestamp: now, apiBase, apiKey: apiKeyScope } }); return list; @@ -68,10 +70,12 @@ export const useModelsStore = create((set, get) => ({ set({ cache: null, models: [] }); }, - isCacheValid: (apiBase) => { + isCacheValid: (apiBase, apiKey) => { const { cache } = get(); if (!cache) return false; if (cache.apiBase !== apiBase) return false; + const apiKeyScope = apiKey?.trim() || ''; + if ((cache.apiKey || '') !== apiKeyScope) return false; return Date.now() - cache.timestamp < CACHE_EXPIRY_MS; } }));