@@ -49,34 +49,34 @@ async function makeRequest(data, options, customLogFunction) {
49
49
res . on ( 'data' , ( chunk ) => {
50
50
try {
51
51
let stringified = chunk . toString ( ) ;
52
- if ( stringified === 'pythagora_end' ) {
53
- gptResponse = '' ;
54
- end = true ;
55
- return ;
56
- }
57
- if ( end ) return gptResponse = stringified ;
58
52
try {
59
53
let json = JSON . parse ( stringified ) ;
60
- if ( json . error ) gptResponse = json . error ;
54
+ if ( json . error || json . message ) gptResponse = json ;
61
55
return ;
62
56
} catch ( e ) { }
63
- let receivedMessages = extractGPTMessageFromStreamData ( stringified ) ;
64
- receivedMessages . forEach ( rm => {
65
- let content = _ . get ( rm , 'choices.0.delta.content' ) ;
66
- if ( content ) {
67
- gptResponse += content ;
68
- if ( customLogFunction ) customLogFunction ( gptResponse ) ;
69
- else process . stdout . write ( content ) ;
70
- }
71
- } ) ;
72
57
58
+ if ( ! stringified . includes ( 'pythagora_end:' ) ) {
59
+ let receivedMessages = extractGPTMessageFromStreamData ( stringified ) ;
60
+ receivedMessages . forEach ( rm => {
61
+ let content = _ . get ( rm , 'choices.0.delta.content' ) ;
62
+ if ( content ) {
63
+ gptResponse += content ;
64
+ if ( customLogFunction ) customLogFunction ( gptResponse ) ;
65
+ else process . stdout . write ( content ) ;
66
+ }
67
+ } ) ;
68
+ } else {
69
+ gptResponse += stringified ;
70
+ if ( customLogFunction ) customLogFunction ( gptResponse ) ;
71
+ else process . stdout . write ( stringified ) ;
72
+ }
73
73
} catch ( e ) { }
74
74
} ) ;
75
- res . on ( 'end' , async ( a , b , c ) => {
75
+ res . on ( 'end' , async function ( ) {
76
76
process . stdout . write ( '\n' ) ;
77
77
if ( res . statusCode >= 400 ) throw new Error ( `Response status code: ${ res . statusCode } . Error message: ${ gptResponse } ` ) ;
78
78
if ( gptResponse . message ) throw new Error ( `Error: ${ gptResponse . message } . Code: ${ gptResponse . code } ` ) ;
79
- gptResponse = cleanupGPTResponse ( gptResponse ) ;
79
+ gptResponse = cleanupGPTResponse ( gptResponse . split ( 'pythagora_end:' ) . pop ( ) ) ;
80
80
resolve ( gptResponse ) ;
81
81
} ) ;
82
82
} ) ;
0 commit comments