1
1
import { ChatHistory } from '../prompts/chat' ;
2
- import { JsonStreamParser } from '../types/types'
2
+ import { JsonStreamParser } from '../types/types' ;
3
3
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 = '' ;
7
6
const parser = new JsonStreamParser ( ) ;
8
7
9
8
const chunk = parser . safeJsonParse < { generatedText : string ; isGenerating : boolean } > ( response ) ;
10
9
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 ) ;
17
13
}
18
14
}
19
- }
15
+ } ;
20
16
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 ) => {
24
18
try {
25
- let resultText = ''
19
+ let resultText = '' ;
26
20
const parser = new JsonStreamParser ( ) ;
27
21
const reader = streamResponse . body ?. getReader ( ) ;
28
22
const decoder = new TextDecoder ( ) ;
29
23
24
+ // Check for missing body in the streamResponse
25
+ if ( ! reader ) {
26
+ console . error ( 'Stream response body is missing.' ) ;
27
+ return ;
28
+ }
29
+
30
30
// eslint-disable-next-line no-constant-condition
31
31
while ( true ) {
32
32
const { done, value } = await reader . read ( ) ;
@@ -35,30 +35,25 @@ export const HandleStreamResponse = async (streamResponse,
35
35
try {
36
36
const chunk = parser . safeJsonParse < { generatedText : string ; isGenerating : boolean } > ( decoder . decode ( value , { stream : true } ) ) ;
37
37
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 ) ;
44
41
}
45
42
}
46
- }
47
- catch ( error ) {
43
+ } catch ( error ) {
48
44
console . error ( 'Error parsing JSON:' , error ) ;
49
- return { 'generateText' : 'Try again!' , 'isGenerating' : false }
45
+ return ; // Just log the error, without unnecessary return value
50
46
}
51
47
}
48
+
52
49
if ( done_cb ) {
53
- done_cb ( resultText )
50
+ done_cb ( resultText ) ;
54
51
}
52
+ } catch ( error ) {
53
+ console . error ( 'Error processing stream response:' , error ) ;
55
54
}
56
- catch ( error ) {
57
- console . error ( 'Error parsing JSON:' , error ) ;
58
- return { 'generateText' : 'Try again!' , 'isGenerating' : false }
59
- }
60
- }
55
+ } ;
61
56
62
57
export const UpdateChatHistory = ( userPrompt : string , AIAnswer : string ) => {
63
- ChatHistory . pushHistory ( userPrompt , AIAnswer )
64
- }
58
+ ChatHistory . pushHistory ( userPrompt , AIAnswer ) ;
59
+ } ;
0 commit comments