@@ -30,10 +30,19 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
3030 } , [ ] ) ;
3131
3232 const _initiateStream = useCallback ( async ( chatId : string , historyForAPI : Message [ ] , personaId : string | null | undefined , titleGenerationMode : 'INITIAL' | 'RECURRING' | null = null ) => {
33- const apiKeys = settings . apiKey && settings . apiKey . length > 0
34- ? settings . apiKey
35- : ( process . env . API_KEY ? [ process . env . API_KEY ] : [ ] ) ;
36-
33+ // 获取 API Key:如果用户启用了自定义,使用用户的配置;否则使用环境变量
34+ let apiKeys : string [ ] = [ ] ;
35+ if ( settings . useCustomApi ) {
36+ // 用户启用了自定义配置,使用用户输入的 API Key
37+ apiKeys = settings . apiKey && settings . apiKey . length > 0 ? settings . apiKey : [ ] ;
38+ } else {
39+ // 用户未启用自定义,使用环境变量
40+ const envKey = settings . llmProvider === 'openai'
41+ ? process . env . OPENAI_API_KEY
42+ : process . env . GEMINI_API_KEY || process . env . API_KEY ;
43+ apiKeys = envKey ? [ envKey ] : [ ] ;
44+ }
45+
3746 if ( apiKeys . length === 0 ) {
3847 const providerName = settings . llmProvider === 'openai' ? 'OpenAI' : 'Gemini' ;
3948 addToast ( `Please set your ${ providerName } API key in Settings.` , 'error' ) ;
@@ -67,7 +76,19 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
6776
6877 try {
6978 const llmService = createLLMService ( settings ) ;
70-
79+
80+ // 获取 API Base URL:如果用户启用了自定义,使用用户的配置;否则使用环境变量
81+ let apiBaseUrl = '' ;
82+ if ( settings . useCustomApi ) {
83+ // 用户启用了自定义配置,使用用户输入的 API Base URL
84+ apiBaseUrl = settings . apiBaseUrl || '' ;
85+ } else {
86+ // 用户未启用自定义,使用环境变量
87+ apiBaseUrl = settings . llmProvider === 'openai'
88+ ? ( process . env . OPENAI_API_BASE_URL || '' )
89+ : ( process . env . API_BASE_URL || '' ) ;
90+ }
91+
7192 const chatRequest : ChatRequest = {
7293 messages : historyForAPI ,
7394 model : chatSession . model ,
@@ -78,7 +99,7 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
7899 contextLength : settings . contextLength ,
79100 } ,
80101 apiKey : apiKeys [ 0 ] , // 服务内部目前只处理单个key
81- apiBaseUrl : settings . apiBaseUrl ,
102+ apiBaseUrl : apiBaseUrl ,
82103 showThoughts : settings . showThoughts ,
83104 enableSearch : settings . enableSearch ,
84105 } ;
0 commit comments