@@ -9,7 +9,7 @@ export const createChatCompletion = (options: {
99} ) =>
1010 new Promise < string > ( ( resolve ) => {
1111 let combinedResult = '' ;
12- const error = '发生错误:' ;
12+ let error = '发生错误:' ;
1313 const config = getChatGPTConfig ( ) ;
1414 const request = https . request (
1515 {
@@ -27,6 +27,7 @@ export const createChatCompletion = (options: {
2727 res . on ( 'data' , async ( chunk ) => {
2828 const text = new TextDecoder ( 'utf-8' ) . decode ( chunk ) ;
2929 const data = text . split ( '\n\n' ) . filter ( ( s ) => s ) ;
30+ console . log ( data ) ;
3031 for ( let i = 0 ; i < data . length ; i ++ ) {
3132 try {
3233 let element = data [ i ] ;
@@ -43,7 +44,7 @@ export const createChatCompletion = (options: {
4344 // 处理没有 data 开头
4445 element = preDataLast + element ;
4546 }
46- if ( element . includes ( 'data: ' ) ) {
47+ if ( element . startsWith ( 'data: ' ) ) {
4748 if ( element . includes ( '[DONE]' ) ) {
4849 if ( options . handleChunk ) {
4950 options . handleChunk ( { text : '' } ) ;
@@ -69,13 +70,18 @@ export const createChatCompletion = (options: {
6970 }
7071 } else {
7172 console . log ( 'no includes data: ' , element ) ;
73+ if ( options . handleChunk ) {
74+ options . handleChunk ( {
75+ text : element ,
76+ } ) ;
77+ }
7278 }
7379 } catch ( e ) {
7480 console . error ( {
7581 e : ( e as Error ) . toString ( ) ,
7682 element : data [ i ] ,
7783 } ) ;
78- // error = (e as Error).toString();
84+ error = ( e as Error ) . toString ( ) ;
7985 }
8086 }
8187 } ) ;
0 commit comments