Skip to content

Commit 534fb71

Browse files
Wing900claude
andcommitted
fix: 修复自定义 API 配置时模型列表自动获取逻辑
- 根据 useCustomApi 开关状态动态选择 API 配置源 - 启用自定义时使用用户输入的 Key 和 URL 获取模型 - 关闭自定义时使用环境变量配置获取模型 - 添加 useCustomApi 到依赖数组,确保开关切换时重新获取 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 66fd01d commit 534fb71

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

hooks/useSettings.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)