@@ -41,13 +41,18 @@ function setOptions({path, method, headers}) {
4141
4242async function makeRequest ( data , options , customLogFunction ) {
4343 let gptResponse = '' ;
44- let end = false ;
4544 let httpModule = options . protocol === 'http' ? require ( 'http' ) : require ( 'https' ) ;
45+ let timeout ;
4646
4747 return new Promise ( ( resolve , reject ) => {
4848 const req = httpModule . request ( _ . omit ( options , [ 'protocol' ] ) , function ( res ) {
4949 res . on ( 'data' , ( chunk ) => {
5050 try {
51+ clearTimeout ( timeout ) ;
52+ timeout = setTimeout ( ( ) => {
53+ reject ( new Error ( "Request timeout" ) ) ;
54+ } , 30000 ) ;
55+
5156 let stringified = chunk . toString ( ) ;
5257 try {
5358 let json = JSON . parse ( stringified ) ;
@@ -58,11 +63,14 @@ async function makeRequest(data, options, customLogFunction) {
5863 } catch ( e ) { }
5964
6065 gptResponse += stringified ;
66+ if ( gptResponse . indexOf ( 'pythagora_end:' ) > - 1 ) return ;
6167 if ( customLogFunction ) customLogFunction ( gptResponse ) ;
6268 else process . stdout . write ( stringified ) ;
6369 } catch ( e ) { }
6470 } ) ;
6571 res . on ( 'end' , async function ( ) {
72+ clearTimeout ( timeout ) ;
73+
6674 process . stdout . write ( '\n' ) ;
6775 if ( res . statusCode >= 400 ) return reject ( new Error ( `Response status code: ${ res . statusCode } . Error message: ${ gptResponse } ` ) ) ;
6876 if ( gptResponse . error ) return reject ( new Error ( `Error: ${ gptResponse . error . message } . Code: ${ gptResponse . error . code } ` ) ) ;
@@ -73,6 +81,7 @@ async function makeRequest(data, options, customLogFunction) {
7381 } ) ;
7482
7583 req . on ( 'error' , ( e ) => {
84+ clearTimeout ( timeout ) ;
7685 console . error ( "problem with request:" + e . message ) ;
7786 reject ( e ) ;
7887 } ) ;
0 commit comments