@@ -17,6 +17,21 @@ const isNonInteractive = process.env.NONINTERACTIVE === 'true';
1717const enquirer = new Enquirer ( ) ;
1818
1919const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
20+
21+ const isValidMongoURI = ( uri ) => {
22+ const atlasPattern = / ^ m o n g o d b \+ s r v : \/ \/ [ ^ : ] + : [ ^ @ ] + @ [ \w - ] + \. m o n g o d b \. n e t \/ ? / ;
23+ return atlasPattern . test ( uri ) ;
24+ } ;
25+
26+ const promptWithValidation = async ( promptConfig ) => {
27+ const response = await enquirer . prompt ( promptConfig ) ;
28+ if ( response [ promptConfig . name ] === '?' ) {
29+ console . log ( chalk . yellow ( `ℹ️ Help: ${ promptConfig . helpMessage } \n` ) ) ;
30+ return promptWithValidation ( promptConfig ) ;
31+ }
32+ return response ;
33+ } ;
34+
2035const CONFIG_PATH = process . env . NODE_ENV === "test"
2136 ? path . join ( process . cwd ( ) , ".mongodb-rag.test.json" )
2237 : path . join ( process . cwd ( ) , ".mongodb-rag.json" ) ;
@@ -153,80 +168,51 @@ program
153168 . action ( async ( ) => {
154169 console . log ( chalk . cyan . bold ( '🔧 Setting up MongoRAG configuration...\n' ) ) ;
155170
156- const responses = await enquirer . prompt ( [
157- {
158- type : 'input' ,
159- name : 'mongoUrl' ,
160- message : 'Enter MongoDB Connection String:'
161- } ,
162- {
163- type : 'input' ,
164- name : 'database' ,
165- message : 'Enter Database Name:'
166- } ,
167- {
168- type : 'input' ,
169- name : 'collection' ,
170- message : 'Enter Collection Name:'
171- } ,
172- {
173- type : 'select' ,
174- name : 'provider' ,
175- message : 'Select an Embedding Provider:' ,
176- choices : [ 'openai' , 'deepseek' , 'ollama' ]
177- } ,
178- {
179- type : 'input' ,
180- name : 'indexName' ,
181- message : 'Enter the name for your Vector Search Index:' ,
182- initial : 'vector_index'
183- }
184- ] ) ;
171+ const responses = { } ;
185172
186- let embeddingConfig = {
187- provider : responses . provider ,
188- dimensions : 1536 ,
189- batchSize : 100
190- } ;
173+ responses . mongoUrl = await promptWithValidation ( {
174+ type : 'input' ,
175+ name : 'mongoUrl' ,
176+ message : 'Enter MongoDB Connection String:' ,
177+ validate : ( input ) => isValidMongoURI ( input ) ? true : 'Invalid MongoDB Atlas connection string.' ,
178+ helpMessage : "Example: mongodb+srv://user:password@cluster.mongodb.net/myDatabase?retryWrites=true&w=majority"
179+ } ) ;
191180
192- if ( responses . provider === 'ollama' ) {
193- const ollamaModels = getOllamaModels ( ) ;
194- if ( ollamaModels . length === 0 ) {
195- console . error ( chalk . red ( '❌ Ollama is not running or no models found.' ) ) ;
196- console . log ( chalk . yellow ( 'ℹ️ Ensure Ollama is installed and running before proceeding.' ) ) ;
197- process . exit ( 1 ) ;
198- }
181+ responses . database = await promptWithValidation ( {
182+ type : 'input' ,
183+ name : 'database' ,
184+ message : 'Enter Database Name:' ,
185+ helpMessage : "Specify the database name where vector search will be performed."
186+ } ) ;
199187
200- const modelResponse = await enquirer . prompt ( [
201- {
202- type : 'select' ,
203- name : 'model' ,
204- message : 'Select an Ollama model:' ,
205- choices : ollamaModels
206- }
207- ] ) ;
188+ responses . collection = await promptWithValidation ( {
189+ type : 'input' ,
190+ name : 'collection' ,
191+ message : 'Enter Collection Name:' ,
192+ helpMessage : "Enter the collection that will store vector embeddings."
193+ } ) ;
208194
209- embeddingConfig . baseUrl = "http://localhost:11434" ;
210- embeddingConfig . model = modelResponse . model ;
211- } else {
212- const apiKeyResponse = await enquirer . prompt ( [
213- {
214- type : 'input' ,
215- name : 'apiKey' ,
216- message : `Enter API Key for ${ responses . provider } :`
217- }
218- ] ) ;
219- embeddingConfig . apiKey = apiKeyResponse . apiKey ;
220- embeddingConfig . model = responses . provider === 'openai' ? 'text-embedding-3-small' : 'deepseek-embedding' ;
221- }
195+ responses . provider = await promptWithValidation ( {
196+ type : 'select' ,
197+ name : 'provider' ,
198+ message : 'Select an Embedding Provider:' ,
199+ choices : [ 'openai' , 'deepseek' , 'ollama' ] ,
200+ helpMessage : "Choose an AI provider for generating vector embeddings."
201+ } ) ;
202+
203+ const indexParams = await getIndexParams ( responses ) ; // Keep the existing index prompt functionality
222204
223205 const newConfig = {
224206 mongoUrl : responses . mongoUrl ,
225207 database : responses . database ,
226208 collection : responses . collection ,
227- embedding : embeddingConfig ,
209+ embedding : {
210+ provider : responses . provider ,
211+ dimensions : 1536 ,
212+ batchSize : 100
213+ } ,
228214 search : { maxResults : 5 , minScore : 0.7 } ,
229- indexName : indexResponse . indexName
215+ indexName : indexParams . indexName // Keep the prompted index name
230216 } ;
231217
232218 fs . writeFileSync ( CONFIG_PATH , JSON . stringify ( newConfig , null , 2 ) ) ;
0 commit comments