@@ -102,6 +102,12 @@ async function getCodeFromActiveTab(): Promise<string | null> {
102102 } ) ;
103103}
104104
105+ function timeout ( ms : number ) : Promise < never > {
106+ return new Promise ( ( _ , reject ) =>
107+ setTimeout ( ( ) => reject ( new Error ( `Operation timed out after ${ ms } ms` ) ) , ms )
108+ ) ;
109+ }
110+
105111function processCode (
106112 chatGPTProvider : ChatGPTProvider ,
107113 codeText : string ,
@@ -116,10 +122,10 @@ function processCode(
116122 if ( action === 'analyze' ) {
117123 prompt = `
118124 As an experienced software engineer, please analyze the code complexity of the Leetcode
119- problem titled ${ problemTitle } and the accompanying code below. Provide both the time and
120- space complexity in big O notation. Space complexity should not include
121- the output (return value) of the function. Your analysis should be direct and concise.
122- The code is provided below .` ;
125+ problem titled ${ problemTitle } and the accompanying code below. Return only the time
126+ and space complexity of the function in big O notation. The space complexity should not
127+ include the output (return value) of the function. Your analysis should be direct and concise.
128+ You can provide a one sentence explanation of the time and space complexity .` ;
123129 if ( infoMessage ) infoMessage . textContent = 'Analyzing code complexity ...' ;
124130 if ( analyzeCodeResponse ) analyzeCodeResponse . classList . remove ( 'hidden' ) ;
125131 if ( fixCodeContainer ) fixCodeContainer . classList . add ( 'hidden' ) ;
@@ -133,35 +139,43 @@ function processCode(
133139 it from being accepted. Please identify and fix any potential issues in the code.
134140 If the provided code is already correct and optimized, please return it as is.
135141 Only the function definition and code should be returned in plain text format with
136- no usage of code blocks. Anything other than the code text is not permitted.` ;
142+ no usage of code blocks. Do not return any code comments. Anything other than the
143+ code text is not permitted.` ;
137144 if ( infoMessage ) infoMessage . textContent = 'Creating the solution ...' ;
138145 analyzeCodeResponse && analyzeCodeResponse . classList . add ( 'hidden' ) ;
139146 fixCodeContainer && fixCodeContainer . classList . remove ( 'hidden' ) ;
140147 }
141- prompt += '\n' + codeText ;
148+ prompt += 'The code is provided below \n' + codeText ;
142149
143150 let response = '' ;
144- chatGPTProvider . generateAnswer ( {
145- prompt : prompt ,
146- onEvent : ( event : { type : string ; data ?: { text : string } } ) => {
147- if ( event . type === 'answer' && event . data ) {
148- response += event . data . text ;
149- if ( action === 'fix' && fixCodeResponse ) {
150- fixCodeResponse . textContent = response ;
151+ Promise . race ( [
152+ chatGPTProvider . generateAnswer ( {
153+ prompt : prompt ,
154+ onEvent : ( event : { type : string ; data ?: { text : string } } ) => {
155+ if ( event . type === 'answer' && event . data ) {
156+ response += event . data . text ;
157+ if ( action === 'fix' && fixCodeResponse ) {
158+ fixCodeResponse . textContent = response ;
159+ }
160+ else if ( action === 'analyze' && analyzeCodeResponse ) {
161+ analyzeCodeResponse . textContent = response ;
162+ }
151163 }
152- else if ( action === 'analyze' && analyzeCodeResponse ) {
153- analyzeCodeResponse . textContent = response ;
164+ if ( event . type === 'done' ) {
165+ analyzeCodeResponse && chrome . storage . local . set ( { 'analyzeCodeResponse' : analyzeCodeResponse . textContent } ) ;
166+ fixCodeResponse && chrome . storage . local . set ( { 'fixCodeResponse' : fixCodeResponse . textContent } ) ;
167+ chrome . storage . local . set ( { 'lastAction' : action } ) ;
168+ infoMessage && ( infoMessage . textContent = problemTitle ) ;
169+ disableAllButtons ( false ) ;
170+ ( window as any ) . Prism . highlightAll ( ) ;
154171 }
155- }
156- if ( event . type === 'done' ) {
157- analyzeCodeResponse && chrome . storage . local . set ( { 'analyzeCodeResponse' : analyzeCodeResponse . textContent } ) ;
158- fixCodeResponse && chrome . storage . local . set ( { 'fixCodeResponse' : fixCodeResponse . textContent } ) ;
159- chrome . storage . local . set ( { 'lastAction' : action } ) ;
160- infoMessage && ( infoMessage . textContent = problemTitle ) ;
161- disableAllButtons ( false ) ;
162- ( window as any ) . Prism . highlightAll ( ) ;
163- }
164- } ,
172+ } ,
173+ } ) ,
174+ timeout ( 12000 )
175+ ] ) . catch ( ( error ) => {
176+ infoMessage && ( infoMessage . textContent = 'The request timed out. Please try again.' ) ;
177+ console . error ( error ) ;
178+ disableAllButtons ( false ) ;
165179 } ) ;
166180}
167181
0 commit comments