@@ -24,8 +24,14 @@ export const HandleStreamResponse = async (streamResponse, cb: (streamText: stri
2424 // Check for missing body in the streamResponse
2525 if ( ! reader ) {
2626 // most likely no stream response, so we can just return the result
27- cb ( streamResponse . result )
28- done_cb ?.( "" ) ;
27+ if ( streamResponse . result ) {
28+ cb ( streamResponse . result )
29+ done_cb ?.( streamResponse . result ) ;
30+ } else {
31+ const errorMessage = "Error: Unable to to process your request. Try again!" ;
32+ cb ( errorMessage ) ;
33+ done_cb ?.( errorMessage ) ;
34+ }
2935 return ;
3036 }
3137
@@ -44,7 +50,10 @@ export const HandleStreamResponse = async (streamResponse, cb: (streamText: stri
4450 }
4551 } catch ( error ) {
4652 console . error ( 'Error parsing JSON:' , error ) ;
47- return ; // Just log the error, without unnecessary return value
53+ const errorMessage = "Error: Unable to decode the AI response. Please try again." ;
54+ cb ( errorMessage ) ;
55+ done_cb ?.( errorMessage ) ;
56+ return ;
4857 }
4958 }
5059
@@ -68,8 +77,14 @@ export const HandleOpenAIResponse = async (aiResponse: IAIStreamResponse | any,
6877 const toolCalls : Map < number , any > = new Map ( ) ; // Accumulate tool calls by index
6978
7079 if ( ! reader ) { // normal response, not a stream
71- cb ( streamResponse . result )
72- done_cb ?.( streamResponse . result , streamResponse ?. threadId || "" ) ;
80+ if ( streamResponse . result ) {
81+ cb ( streamResponse . result )
82+ done_cb ?.( streamResponse . result , streamResponse ?. threadId || "" ) ;
83+ } else {
84+ const errorMessage = "Error: Unable to to process your request. Try again!" ;
85+ cb ( errorMessage ) ;
86+ done_cb ?.( errorMessage , streamResponse ?. threadId || "" ) ;
87+ }
7388 return ;
7489 }
7590
@@ -90,6 +105,12 @@ export const HandleOpenAIResponse = async (aiResponse: IAIStreamResponse | any,
90105 done_cb ?.( resultText , threadId ) ;
91106 return ;
92107 }
108+
109+ // Skip empty JSON strings
110+ if ( ! jsonStr || jsonStr . length === 0 ) {
111+ continue ;
112+ }
113+
93114 try {
94115 const json = JSON . parse ( jsonStr ) ;
95116 threadId = json ?. thread_id ;
@@ -158,6 +179,9 @@ export const HandleOpenAIResponse = async (aiResponse: IAIStreamResponse | any,
158179 }
159180 } catch ( e ) {
160181 console . error ( "⚠️ OpenAI Stream parse error:" , e ) ;
182+ console . error ( "Problematic JSON string:" , jsonStr ) ;
183+ // Skip this chunk and continue processing the stream
184+ continue ;
161185 }
162186 }
163187 }
@@ -175,8 +199,14 @@ export const HandleMistralAIResponse = async (aiResponse: IAIStreamResponse | an
175199 let resultText = "" ;
176200
177201 if ( ! reader ) { // normal response, not a stream
178- cb ( streamResponse . result )
179- done_cb ?.( streamResponse . result , streamResponse ?. threadId || "" ) ;
202+ if ( streamResponse . result ) {
203+ cb ( streamResponse . result )
204+ done_cb ?.( streamResponse . result , streamResponse ?. threadId || "" ) ;
205+ } else {
206+ const errorMessage = "Error: Unable to to process your request. Try again!" ;
207+ cb ( errorMessage ) ;
208+ done_cb ?.( errorMessage , streamResponse ?. threadId || "" ) ;
209+ }
180210 return ;
181211 }
182212
@@ -196,6 +226,11 @@ export const HandleMistralAIResponse = async (aiResponse: IAIStreamResponse | an
196226 return ;
197227 }
198228
229+ // Skip empty JSON strings
230+ if ( ! jsonStr || jsonStr . length === 0 ) {
231+ continue ;
232+ }
233+
199234 try {
200235 const json = JSON . parse ( jsonStr ) ;
201236 threadId = json ?. id || threadId ;
@@ -212,6 +247,9 @@ export const HandleMistralAIResponse = async (aiResponse: IAIStreamResponse | an
212247 }
213248 } catch ( e ) {
214249 console . error ( "MistralAI Stream parse error:" , e ) ;
250+ console . error ( "Problematic JSON string:" , jsonStr ) ;
251+ // Skip this chunk and continue processing the stream
252+ continue ;
215253 }
216254 }
217255 }
@@ -230,8 +268,14 @@ export const HandleAnthropicResponse = async (aiResponse: IAIStreamResponse | an
230268 let currentBlockIndex : number = - 1 ;
231269
232270 if ( ! reader ) { // normal response, not a stream
233- cb ( streamResponse . result )
234- done_cb ?.( streamResponse . result , streamResponse ?. threadId || "" ) ;
271+ if ( streamResponse . result ) {
272+ cb ( streamResponse . result )
273+ done_cb ?.( streamResponse . result , streamResponse ?. threadId || "" ) ;
274+ } else {
275+ const errorMessage = "Error: Unable to to process your request. Try again!" ;
276+ cb ( errorMessage ) ;
277+ done_cb ?.( errorMessage , streamResponse ?. threadId || "" ) ;
278+ }
235279 return ;
236280 }
237281
@@ -246,6 +290,12 @@ export const HandleAnthropicResponse = async (aiResponse: IAIStreamResponse | an
246290 for ( const line of lines ) {
247291 if ( line . startsWith ( "data: " ) ) {
248292 const jsonStr = line . replace ( / ^ d a t a : / , "" ) . trim ( ) ;
293+
294+ // Skip empty or invalid JSON strings
295+ if ( ! jsonStr || jsonStr . length === 0 ) {
296+ continue ;
297+ }
298+
249299 try {
250300 const json = JSON . parse ( jsonStr ) ;
251301
@@ -299,6 +349,9 @@ export const HandleAnthropicResponse = async (aiResponse: IAIStreamResponse | an
299349 }
300350 } catch ( e ) {
301351 console . error ( "Anthropic Stream parse error:" , e ) ;
352+ console . error ( "Problematic JSON string:" , jsonStr ) ;
353+ // Skip this chunk and continue processing the stream
354+ continue ;
302355 }
303356 }
304357 }
@@ -315,8 +368,15 @@ export const HandleOllamaResponse = async (aiResponse: IAIStreamResponse | any,
315368 let inThinking = false ;
316369
317370 if ( ! reader ) { // normal response, not a stream
318- cb ( streamResponse . result || streamResponse . response || "" ) ;
319- done_cb ?.( streamResponse . result || streamResponse . response || "" ) ;
371+ const result = streamResponse . result || streamResponse . response ;
372+ if ( result ) {
373+ cb ( result ) ;
374+ done_cb ?.( result ) ;
375+ } else {
376+ const errorMessage = "Error: Unable to to process your request. Try again!" ;
377+ cb ( errorMessage ) ;
378+ done_cb ?.( errorMessage ) ;
379+ }
320380 return ;
321381 }
322382
0 commit comments