Spaces:
Sleeping
Sleeping
| import { create } from 'zustand'; | |
| // Define API environment configuration interface | |
| interface EnvConfig { | |
| apiKey: boolean; | |
| baseUrl: boolean; | |
| model: boolean; | |
| } | |
| // Define model state interface | |
| interface ModelState { | |
| currentModel: string; | |
| envConfig: EnvConfig; | |
| isLoading: boolean; | |
| isLoaded: boolean; | |
| // Action methods | |
| setCurrentModel: (model: string) => void; | |
| setEnvConfig: (config: EnvConfig) => void; | |
| fetchModelInfo: () => Promise<void>; | |
| } | |
| // Create model state store | |
| export const useModelStore = create<ModelState>((set, get) => ({ | |
| currentModel: '', | |
| envConfig: { | |
| apiKey: false, | |
| baseUrl: false, | |
| model: false | |
| }, | |
| isLoading: false, | |
| isLoaded: false, | |
| setCurrentModel: (model: string) => set({ currentModel: model }), | |
| setEnvConfig: (config: EnvConfig) => set({ envConfig: config }), | |
| fetchModelInfo: async () => { | |
| // If already loaded or currently loading, do not request again | |
| if (get().isLoaded || get().isLoading) return; | |
| set({ isLoading: true }); | |
| try { | |
| const response = await fetch('/api/check-env'); | |
| const data = await response.json(); | |
| if (data.ok) { | |
| set({ | |
| currentModel: data.model || '', | |
| envConfig: data.env || { | |
| apiKey: false, | |
| baseUrl: false, | |
| model: false | |
| }, | |
| isLoaded: true | |
| }); | |
| } | |
| } catch (error) { | |
| console.error("Failed to fetch model info:", error); | |
| } finally { | |
| set({ isLoading: false }); | |
| } | |
| } | |
| })); |