@@ -140,10 +140,26 @@ export const useSettings = () => {
140140 } , [ settings , isStorageLoaded , setLanguage ] ) ;
141141
142142 useEffect ( ( ) => {
143- const apiKeys = settings . apiKey || ( process . env . API_KEY ? [ process . env . API_KEY ] : [ ] ) ;
144- if ( isStorageLoaded && apiKeys . length > 0 ) {
143+ if ( ! isStorageLoaded ) return ;
144+
145+ // 获取实际使用的 API Key 和 Base URL
146+ let actualApiKey = '' ;
147+ let actualApiBaseUrl = '' ;
148+
149+ if ( settings . useCustomApi ) {
150+ // 用户启用了自定义配置
151+ const apiKeys = settings . apiKey || [ ] ;
152+ actualApiKey = apiKeys . length > 0 ? apiKeys [ 0 ] : '' ;
153+ actualApiBaseUrl = settings . apiBaseUrl || '' ;
154+ } else if ( envConfig ) {
155+ // 用户未启用自定义,使用环境变量
156+ actualApiKey = envConfig . apiKey ;
157+ actualApiBaseUrl = envConfig . apiBaseUrl ;
158+ }
159+
160+ if ( actualApiKey ) {
145161 const llmService = createLLMService ( settings ) ;
146- llmService . getAvailableModels ( apiKeys [ 0 ] , settings . apiBaseUrl ) . then ( models => {
162+ llmService . getAvailableModels ( actualApiKey , actualApiBaseUrl ) . then ( models => {
147163 if ( ! models || models . length === 0 ) return ;
148164 setAvailableModels ( models ) ;
149165 setSettings ( current => {
@@ -164,7 +180,7 @@ export const useSettings = () => {
164180 } ) ;
165181 } ) ;
166182 }
167- } , [ isStorageLoaded , settings . apiKey , settings . apiBaseUrl , settings . llmProvider ] ) ;
183+ } , [ isStorageLoaded , settings . apiKey , settings . apiBaseUrl , settings . llmProvider , settings . useCustomApi ] ) ;
168184
169185 return { settings, setSettings, availableModels, isStorageLoaded } ;
170186} ;
0 commit comments