@@ -3,9 +3,15 @@ import { askQuestion } from '../api/api.js';
33import { selectFile } from './selectFile.js' ;
44import chalk from 'chalk' ;
55
6- export async function handleQuestionLoop ( filePath = null , enableFileSelection = false ) {
6+ export async function handleQuestionLoop ( filePath = null , enableFileSelection = false , apiKey = null ) {
77 let continueAsking = true ;
88
9+ // Verify API key is provided
10+ if ( ! apiKey ) {
11+ console . error ( chalk . red ( '❌ No API key provided to handleQuestionLoop. Please restart the application.' ) ) ;
12+ return ;
13+ }
14+
915 while ( continueAsking ) {
1016 let fileReference = '' ;
1117
@@ -17,15 +23,38 @@ export async function handleQuestionLoop(filePath = null, enableFileSelection =
1723 } ) ;
1824
1925 if ( useFile ) {
20- fileReference = await selectFile ( ) ;
21- if ( ! fileReference ) {
22- // Retry file selection if it fails
23- continueAsking = await confirm ( {
24- message : 'Do you want to try again?' ,
26+ try {
27+ fileReference = await selectFile ( ) ;
28+ if ( ! fileReference ) {
29+ // Retry file selection if it fails
30+ continueAsking = await confirm ( {
31+ message : 'Do you want to try again?' ,
32+ default : true ,
33+ } ) ;
34+ if ( ! continueAsking ) break ;
35+ continue ;
36+ }
37+ } catch ( error ) {
38+ if ( error . message && error . message . includes ( 'not initialized' ) ) {
39+ console . error ( chalk . red ( '❌ Services are not properly initialized. Please restart the application.' ) ) ;
40+ break ;
41+ }
42+ console . error ( chalk . red ( 'Error during file selection:' ) , error ) ;
43+
44+ // Ask if user wants to continue without file
45+ const continueWithoutFile = await confirm ( {
46+ message : 'File selection failed. Do you want to continue without a file?' ,
2547 default : true ,
2648 } ) ;
27- if ( ! continueAsking ) break ;
28- continue ;
49+
50+ if ( ! continueWithoutFile ) {
51+ continueAsking = await confirm ( {
52+ message : 'Do you want to try again?' ,
53+ default : true ,
54+ } ) ;
55+ if ( ! continueAsking ) break ;
56+ continue ;
57+ }
2958 }
3059 }
3160 }
@@ -35,16 +64,42 @@ export async function handleQuestionLoop(filePath = null, enableFileSelection =
3564 message : 'Ask your question: (Save and close editor to submit)' ,
3665 } ) ;
3766
38- if ( ! question ) {
39- console . log ( chalk . red ( 'No question provided. Exiting...' ) ) ;
40- break ;
67+ if ( ! question || question . trim ( ) === '' ) {
68+ console . log ( chalk . yellow ( '⚠️ No question provided.' ) ) ;
69+
70+ // Ask if user wants to try again
71+ continueAsking = await confirm ( {
72+ message : 'Do you want to try asking another question?' ,
73+ default : true ,
74+ } ) ;
75+ continue ;
4176 }
4277
4378 // Process the Question
4479 try {
4580 await askQuestion ( question , fileReference , filePath ) ;
4681 } catch ( error ) {
47- console . error ( chalk . red ( 'Error processing question:' ) , error ) ;
82+ if ( error . message && error . message . includes ( 'not initialized' ) ) {
83+ console . error ( chalk . red ( '❌ AI services are not properly initialized. Please restart the application.' ) ) ;
84+ break ;
85+ }
86+
87+ console . error ( chalk . red ( 'Error processing question:' ) , error . message || error ) ;
88+
89+ // Ask if user wants to try again with the same question
90+ const retryQuestion = await confirm ( {
91+ message : 'There was an error processing your question. Do you want to try again?' ,
92+ default : true ,
93+ } ) ;
94+
95+ if ( retryQuestion ) {
96+ // Retry the same question
97+ try {
98+ await askQuestion ( question , fileReference , filePath ) ;
99+ } catch ( retryError ) {
100+ console . error ( chalk . red ( 'Error on retry:' ) , retryError . message || retryError ) ;
101+ }
102+ }
48103 }
49104
50105 // Ask if User Wants to Continue
@@ -54,5 +109,5 @@ export async function handleQuestionLoop(filePath = null, enableFileSelection =
54109 } ) ;
55110 }
56111
57- console . log ( chalk . green ( 'Goodbye!' ) ) ;
58- }
112+ console . log ( chalk . green ( '👋 Goodbye!' ) ) ;
113+ }
0 commit comments