11import { ChatHistory } from '../prompts/chat' ;
2- import { JsonStreamParser } from '../types/types'
2+ import { JsonStreamParser } from '../types/types' ;
33
4- export const HandleSimpleResponse = async ( response ,
5- cb ?: ( streamText : string ) => void ) => {
6- let resultText = ''
4+ export const HandleSimpleResponse = async ( response , cb ?: ( streamText : string ) => void ) => {
5+ let resultText = '' ;
76 const parser = new JsonStreamParser ( ) ;
87
98 const chunk = parser . safeJsonParse < { generatedText : string ; isGenerating : boolean } > ( response ) ;
109 for ( const parsedData of chunk ) {
11- if ( parsedData . isGenerating ) {
12- resultText += parsedData . generatedText
13- cb ( parsedData . generatedText )
14- } else {
15- resultText += parsedData . generatedText
16- cb ( parsedData . generatedText )
10+ resultText += parsedData . generatedText ;
11+ if ( cb ) {
12+ cb ( parsedData . generatedText ) ;
1713 }
1814 }
19- }
15+ } ;
2016
21- export const HandleStreamResponse = async ( streamResponse ,
22- cb : ( streamText : string ) => void ,
23- done_cb ?: ( result : string ) => void ) => {
17+ export const HandleStreamResponse = async ( streamResponse , cb : ( streamText : string ) => void , done_cb ?: ( result : string ) => void ) => {
2418 try {
25- let resultText = ''
19+ let resultText = '' ;
2620 const parser = new JsonStreamParser ( ) ;
2721 const reader = streamResponse . body ?. getReader ( ) ;
2822 const decoder = new TextDecoder ( ) ;
2923
24+ // Check for missing body in the streamResponse
25+ if ( ! reader ) {
26+ console . error ( 'Stream response body is missing.' ) ;
27+ return ;
28+ }
29+
3030 // eslint-disable-next-line no-constant-condition
3131 while ( true ) {
3232 const { done, value } = await reader . read ( ) ;
@@ -35,30 +35,25 @@ export const HandleStreamResponse = async (streamResponse,
3535 try {
3636 const chunk = parser . safeJsonParse < { generatedText : string ; isGenerating : boolean } > ( decoder . decode ( value , { stream : true } ) ) ;
3737 for ( const parsedData of chunk ) {
38- if ( parsedData . isGenerating ) {
39- resultText += parsedData . generatedText
40- cb ( parsedData . generatedText )
41- } else {
42- resultText += parsedData . generatedText
43- cb ( parsedData . generatedText )
38+ resultText += parsedData . generatedText ;
39+ if ( cb ) {
40+ cb ( parsedData . generatedText ) ;
4441 }
4542 }
46- }
47- catch ( error ) {
43+ } catch ( error ) {
4844 console . error ( 'Error parsing JSON:' , error ) ;
49- return { 'generateText' : 'Try again!' , 'isGenerating' : false }
45+ return ; // Just log the error, without unnecessary return value
5046 }
5147 }
48+
5249 if ( done_cb ) {
53- done_cb ( resultText )
50+ done_cb ( resultText ) ;
5451 }
52+ } catch ( error ) {
53+ console . error ( 'Error processing stream response:' , error ) ;
5554 }
56- catch ( error ) {
57- console . error ( 'Error parsing JSON:' , error ) ;
58- return { 'generateText' : 'Try again!' , 'isGenerating' : false }
59- }
60- }
55+ } ;
6156
6257export const UpdateChatHistory = ( userPrompt : string , AIAnswer : string ) => {
63- ChatHistory . pushHistory ( userPrompt , AIAnswer )
64- }
58+ ChatHistory . pushHistory ( userPrompt , AIAnswer ) ;
59+ } ;
0 commit comments