@@ -8,6 +8,7 @@ import gradient from "gradient-string";
88
99const configPath = path . join ( os . homedir ( ) , ".commandai" , "config.json" ) ;
1010const defaultChatGPTModel = "gpt-4o" ;
11+ const defaultChatGPTApiUrl = "https://api.openai.com/v1" ;
1112const defaultOllamaUrl = "http://127.0.0.1:11434" ;
1213
1314async function checkOllamaActive ( url = defaultOllamaUrl ) {
@@ -46,9 +47,9 @@ async function saveConfig(config) {
4647 console . log ( ) ;
4748}
4849
49- async function validateApiKey ( apiKey ) {
50+ async function validateApiKey ( apiKey , chatgptApiUrl ) {
5051 try {
51- const openai = new OpenAI ( { apiKey : apiKey } ) ;
52+ const openai = new OpenAI ( { baseURL : chatgptApiUrl , apiKey : apiKey } ) ;
5253 await openai . models . list ( ) ;
5354 return true ;
5455 } catch ( error ) {
@@ -57,9 +58,9 @@ async function validateApiKey(apiKey) {
5758 }
5859}
5960
60- async function fetchChatGPTModels ( apiKey ) {
61+ async function fetchChatGPTModels ( apiKey , chatgptApiUrl ) {
6162 try {
62- const openai = new OpenAI ( { apiKey : apiKey } ) ;
63+ const openai = new OpenAI ( { baseURL : chatgptApiUrl , apiKey : apiKey } ) ;
6364 const response = await openai . models . list ( ) ;
6465 return response . data . map ( ( model ) => model . id ) ;
6566 } catch ( error ) {
@@ -161,7 +162,7 @@ async function promptOllamaConfig(defaultUrl, defaultModel) {
161162 } ;
162163}
163164
164- async function promptChatGPTAPIKey ( defaultApiKey ) {
165+ async function promptChatGPTAPIKey ( defaultApiKey , chatgptApiUrl ) {
165166 const chatgptConfig = await inquirer . prompt ( [
166167 {
167168 type : "input" ,
@@ -171,7 +172,7 @@ async function promptChatGPTAPIKey(defaultApiKey) {
171172 } ,
172173 ] ) ;
173174
174- const isValidApiKey = await validateApiKey ( chatgptConfig . chatgptApiKey ) ;
175+ const isValidApiKey = await validateApiKey ( chatgptConfig . chatgptApiKey , chatgptApiUrl ) ;
175176
176177 if ( ! isValidApiKey ) {
177178 return null ;
@@ -180,8 +181,10 @@ async function promptChatGPTAPIKey(defaultApiKey) {
180181 return chatgptConfig ;
181182}
182183
183- async function promptChatGPTModels ( defaultModel , defaultApiKey ) {
184- const models = await fetchChatGPTModels ( defaultApiKey ) ;
184+
185+ async function promptChatGPTModels ( defaultModel , chatgptApiUrl ) {
186+ const models = await fetchChatGPTModels ( chatgptApiUrl ) ;
187+
185188 if ( models . length === 0 ) {
186189 console . error (
187190 "No models found for the provided API key. Please try again." ,
@@ -202,26 +205,55 @@ async function promptChatGPTModels(defaultModel, defaultApiKey) {
202205 return chatgptModel ;
203206}
204207
205- async function promptChatGPTConfig ( defaultApiKey , defaultModel ) {
206- const chatgptConfig = await promptChatGPTAPIKey ( defaultApiKey ) ;
208+ async function promptChatGPTURL ( defaultUrl ) {
209+ const chatgptApiUrl = await inquirer . prompt ( [
210+ {
211+ type : "input" ,
212+ name : "chatgptApiUrl" ,
213+ message : "Enter the URL for the Ollama server:" ,
214+ default : defaultUrl ,
215+ } ,
216+ ] ) ;
217+
218+ return chatgptApiUrl ;
219+ }
220+
221+ async function promptChatGPTConfig ( config ) {
222+ let chatgptApiUrl = { chatgptApiUrl : config . chatgptApiUrl || defaultChatGPTApiUrl } ;
223+
224+ if ( config . aiService === "OpenAI" ) {
225+ chatgptApiUrl = await promptChatGPTURL ( config . chatgptApiUrl ) ;
226+ if ( ! chatgptApiUrl ) return null ;
227+ }
228+
229+ const chatgptConfig = await promptChatGPTAPIKey ( config . chatgptApiKey , chatgptApiUrl . chatgptApiUrl ) ;
207230
208231 if ( ! chatgptConfig ) return null ;
209232
210- const chatgptModel = await promptChatGPTModels ( defaultModel , defaultApiKey ) ;
233+ const chatgptModel = await promptChatGPTModels ( config . chatgptModel , chatgptApiUrl . chatgptApiUrl ) ;
211234
212235 if ( ! chatgptModel ) return null ;
213236
214- return {
215- chatgptApiKey : chatgptConfig . chatgptApiKey ,
216- chatgptModel : chatgptModel . chatgptModel ,
217- } ;
237+ if ( chatgptApiUrl ) {
238+ return {
239+ chatgptApiKey : chatgptConfig . chatgptApiKey ,
240+ chatgptModel : chatgptModel . chatgptModel ,
241+ chatgptApiUrl : chatgptApiUrl . chatgptApiUrl ,
242+ } ;
243+ } else {
244+ return {
245+ chatgptApiKey : chatgptConfig . chatgptApiKey ,
246+ chatgptModel : chatgptModel . chatgptModel ,
247+ } ;
248+ }
218249}
219250
220251async function getAIServiceChoices ( ) {
221252 const isOllamaActive = await checkOllamaActive ( ) ;
222253 return [
223254 { name : isOllamaActive ? "Ollama (Running)" : "Ollama" , value : "Ollama" } ,
224255 { name : "ChatGPT" , value : "ChatGPT" } ,
256+ { name : "OpenAI" , value : "OpenAI" } ,
225257 ] ;
226258}
227259
@@ -232,6 +264,7 @@ async function configure(
232264 ollamaModel : "" ,
233265 chatgptApiKey : "" ,
234266 chatgptModel : defaultChatGPTModel ,
267+ chatgptApiUrl : defaultChatGPTApiUrl ,
235268 showExecutionDescription : true ,
236269 showExecutionPlan : true ,
237270 enableLogging : false ,
@@ -279,6 +312,8 @@ async function configureSelectedAIService(finalConfig) {
279312 return await configureOllamaService ( finalConfig ) ;
280313 } else if ( finalConfig . aiService === "ChatGPT" ) {
281314 return await configureChatGPTService ( finalConfig ) ;
315+ } else if ( finalConfig . aiService === "OpenAI" ) {
316+ return await configureChatGPTService ( finalConfig ) ;
282317 }
283318 return null ;
284319}
@@ -297,16 +332,21 @@ async function configureOllamaService(finalConfig) {
297332}
298333
299334async function configureChatGPTService ( finalConfig ) {
300- const chatgptConfig = await promptChatGPTConfig (
301- finalConfig . chatgptApiKey ,
302- finalConfig . chatgptModel ,
303- ) ;
335+ const chatgptConfig = await promptChatGPTConfig ( finalConfig ) ;
304336 if ( ! chatgptConfig ) return null ;
305337
306- return {
307- chatgptApiKey : chatgptConfig . chatgptApiKey ,
308- chatgptModel : chatgptConfig . chatgptModel ,
309- } ;
338+ if ( finalConfig . aiService === "OpenAI" ) {
339+ return {
340+ chatgptApiKey : chatgptConfig . chatgptApiKey ,
341+ chatgptModel : chatgptConfig . chatgptModel ,
342+ chatgptApiUrl : chatgptConfig . chatgptApiUrl ,
343+ } ;
344+ } else {
345+ return {
346+ chatgptApiKey : chatgptConfig . chatgptApiKey ,
347+ chatgptModel : chatgptConfig . chatgptModel ,
348+ } ;
349+ }
310350}
311351
312352export { loadConfig , saveConfig , configure } ;
0 commit comments