@@ -51,10 +51,8 @@ async function handleStreamRequest(req, res) {
5151 res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
5252 res . setHeader ( 'Connection' , 'keep-alive' ) ;
5353
54- let streamTimeout ;
55- let timeoutTriggered = false ;
5654 // Setup the timeout for the stream
57- streamTimeout = setTimeout ( ( ) => {
55+ let streamTimeout = setTimeout ( ( ) => {
5856 console . warn ( "Stream timeout reached" ) ;
5957 res . write ( `data: ${ JSON . stringify ( { error : "Stream timeout" , done : true } ) } \n\n` ) ;
6058 res . end ( ) ;
@@ -93,39 +91,33 @@ async function handleStreamRequest(req, res) {
9391
9492 response . data . on ( 'data' , ( chunk ) => {
9593 try {
96- // Only process if timeout hasn't fired yet
97- if ( ! timeoutTriggered ) {
98- // Clear the timeout on each received chunk
99- clearTimeout ( streamTimeout ) ;
100-
101- // Reset timeout
102- streamTimeout = setTimeout ( ( ) => {
103- if ( ! timeoutTriggered ) {
104- timeoutTriggered = true ;
105- console . warn ( "Stream timeout between chunks" ) ;
106- res . write ( `data: ${ JSON . stringify ( { error : "Stream timeout between responses" , done : true } ) } \n\n` ) ;
107- res . end ( ) ;
108- }
109- } , STREAM_TIMEOUT ) ;
94+ // Clear the timeout on each received chunk
95+ clearTimeout ( streamTimeout ) ;
96+ // Reset timeout
97+ streamTimeout = setTimeout ( ( ) => {
98+ console . warn ( "Stream timeout between chunks" ) ;
99+ res . write ( `data: ${ JSON . stringify ( { error : "Stream timeout between responses" , done : true } ) } \n\n` ) ;
100+ res . end ( ) ;
101+ } , STREAM_TIMEOUT ) ;
110102
111- const data = JSON . parse ( chunk . toString ( ) ) ;
103+ const data = JSON . parse ( chunk . toString ( ) ) ;
112104
113- // Track first token time
114- if ( data . response && tokensGenerated === 0 ) {
105+ // Track first token time
106+ if ( data . response && tokensGenerated === 0 ) {
115107 firstTokenTime = Date . now ( ) ;
116108 console . log ( `First token after ${ firstTokenTime - startTime } ms` ) ;
117- }
109+ }
118110
119- // Count tokens
120- if ( data . response ) {
111+ // Count tokens
112+ if ( data . response ) {
121113 tokensGenerated += 1 ;
122- }
114+ }
123115
124- // Send each chunk as an SSE event
125- res . write ( `data: ${ JSON . stringify ( data ) } \n\n` ) ;
116+ // Send each chunk as an SSE event
117+ res . write ( `data: ${ JSON . stringify ( data ) } \n\n` ) ;
126118
127- // If this is the final response, end the connection and log performance
128- if ( data . done ) {
119+ // If this is the final response, end the connection and log performance
120+ if ( data . done ) {
129121 const endTime = Date . now ( ) ;
130122 const totalTime = endTime - startTime ;
131123 const tokensPerSecond = tokensGenerated / ( totalTime / 1000 ) ;
@@ -135,7 +127,6 @@ async function handleStreamRequest(req, res) {
135127 // Clean up the timeout
136128 clearTimeout ( streamTimeout ) ;
137129 res . end ( ) ;
138- }
139130 }
140131 } catch ( err ) {
141132 console . error ( "Error parsing chunk:" , err ) ;
0 commit comments