11import * as https from 'https' ;
22import { TextDecoder } from 'util' ;
33import { getChatGPTConfig } from './config' ;
4- import { showChatGPTView } from '../webview' ;
54
65export const createChatCompletion = ( options : {
76 messages : { role : 'system' | 'user' | 'assistant' ; content : string } [ ] ;
8- handleChunk ?: ( data : { text ?: string ; hasMore : boolean } ) => void ;
7+ handleChunk ?: ( data : { text ?: string } ) => void ;
98} ) =>
109 new Promise < string > ( ( resolve ) => {
1110 let combinedResult = '' ;
@@ -41,15 +40,15 @@ export const createChatCompletion = (options: {
4140 if ( element . includes ( 'data: ' ) ) {
4241 if ( element . includes ( '[DONE]' ) ) {
4342 if ( options . handleChunk ) {
44- options . handleChunk ( { hasMore : true , text : '' } ) ;
43+ options . handleChunk ( { text : '' } ) ;
4544 }
4645 return ;
4746 }
4847 // remove 'data: '
4948 const data = JSON . parse ( element . replace ( 'data: ' , '' ) ) ;
5049 if ( data . finish_reason === 'stop' ) {
5150 if ( options . handleChunk ) {
52- options . handleChunk ( { hasMore : true , text : '' } ) ;
51+ options . handleChunk ( { text : '' } ) ;
5352 }
5453 return ;
5554 }
@@ -58,15 +57,13 @@ export const createChatCompletion = (options: {
5857 if ( options . handleChunk ) {
5958 options . handleChunk ( {
6059 text : openaiRes . replaceAll ( '\\n' , '\n' ) ,
61- hasMore : true ,
6260 } ) ;
6361 }
6462 combinedResult += openaiRes ;
6563 }
6664 } else {
6765 if ( options . handleChunk ) {
6866 options . handleChunk ( {
69- hasMore : true ,
7067 text : element ,
7168 } ) ;
7269 }
@@ -84,7 +81,6 @@ export const createChatCompletion = (options: {
8481 res . on ( 'error' , ( e ) => {
8582 if ( options . handleChunk ) {
8683 options . handleChunk ( {
87- hasMore : true ,
8884 text : e . toString ( ) ,
8985 } ) ;
9086 }
@@ -95,7 +91,6 @@ export const createChatCompletion = (options: {
9591 if ( error !== '发生错误:' ) {
9692 if ( options . handleChunk ) {
9793 options . handleChunk ( {
98- hasMore : true ,
9994 text : error ,
10095 } ) ;
10196 }
@@ -111,8 +106,7 @@ export const createChatCompletion = (options: {
111106 max_tokens : config . maxTokens ,
112107 } ;
113108 request . on ( 'error' , ( error ) => {
114- options . handleChunk &&
115- options . handleChunk ( { hasMore : true , text : error . toString ( ) } ) ;
109+ options . handleChunk && options . handleChunk ( { text : error . toString ( ) } ) ;
116110 resolve ( error . toString ( ) ) ;
117111 } ) ;
118112 request . write ( JSON . stringify ( body ) ) ;
0 commit comments