11import * as https from 'https' ;
22import { TextDecoder } from 'util' ;
33import { getChatGPTConfig } from './config' ;
4+ import { emitter } from './emitter' ;
5+ import { showChatGPTView } from '../webview' ;
46
57export const createChatCompletion = ( options : {
68 apiKey : string ;
@@ -43,29 +45,46 @@ export const createChatCompletion = (options: {
4345 }
4446 if ( element . includes ( 'data: ' ) ) {
4547 if ( element . includes ( '[DONE]' ) ) {
46- options . handleChunk &&
48+ if ( options . handleChunk ) {
4749 options . handleChunk ( { hasMore : true , text : '' } ) ;
50+ emitter . emit ( 'chatGPTChunck' , { hasMore : true , text : '' } ) ;
51+ }
4852 return ;
4953 }
5054 // remove 'data: '
5155 const data = JSON . parse ( element . replace ( 'data: ' , '' ) ) ;
5256 if ( data . finish_reason === 'stop' ) {
53- options . handleChunk &&
57+ if ( options . handleChunk ) {
5458 options . handleChunk ( { hasMore : true , text : '' } ) ;
59+ emitter . emit ( 'chatGPTChunck' , { hasMore : true , text : '' } ) ;
60+ }
5561 return ;
5662 }
5763 const openaiRes = data . choices [ 0 ] . delta . content ;
5864 if ( openaiRes ) {
59- options . handleChunk &&
65+ if ( options . handleChunk ) {
6066 options . handleChunk ( {
6167 text : openaiRes . replaceAll ( '\\n' , '\n' ) ,
6268 hasMore : true ,
6369 } ) ;
70+ emitter . emit ( 'chatGPTChunck' , {
71+ text : openaiRes . replaceAll ( '\\n' , '\n' ) ,
72+ hasMore : true ,
73+ } ) ;
74+ }
6475 combinedResult += openaiRes ;
6576 }
6677 } else {
67- options . handleChunk &&
68- options . handleChunk ( { hasMore : true , text : element } ) ;
78+ if ( options . handleChunk ) {
79+ options . handleChunk ( {
80+ hasMore : true ,
81+ text : element ,
82+ } ) ;
83+ emitter . emit ( 'chatGPTChunck' , {
84+ hasMore : true ,
85+ text : element ,
86+ } ) ;
87+ }
6988 return ;
7089 }
7190 } catch ( e ) {
@@ -78,16 +97,33 @@ export const createChatCompletion = (options: {
7897 }
7998 } ) ;
8099 res . on ( 'error' , ( e ) => {
81- options . handleChunk &&
82- options . handleChunk ( { hasMore : true , text : e . toString ( ) } ) ;
100+ if ( options . handleChunk ) {
101+ options . handleChunk ( {
102+ hasMore : true ,
103+ text : e . toString ( ) ,
104+ } ) ;
105+ emitter . emit ( 'chatGPTChunck' , {
106+ hasMore : true ,
107+ text : e . toString ( ) ,
108+ } ) ;
109+ }
83110 reject ( e ) ;
84111 } ) ;
85112 res . on ( 'end' , ( ) => {
86113 if ( error !== '发生错误:' ) {
87- options . handleChunk &&
88- options . handleChunk ( { hasMore : true , text : error } ) ;
114+ if ( options . handleChunk ) {
115+ options . handleChunk ( {
116+ hasMore : true ,
117+ text : error ,
118+ } ) ;
119+ emitter . emit ( 'chatGPTChunck' , {
120+ hasMore : true ,
121+ text : error ,
122+ } ) ;
123+ }
89124 }
90125 resolve ( combinedResult || error ) ;
126+ emitter . emit ( 'chatGPTComplete' , combinedResult || error ) ;
91127 } ) ;
92128 } ,
93129 ) ;
@@ -101,6 +137,7 @@ export const createChatCompletion = (options: {
101137 options . handleChunk &&
102138 options . handleChunk ( { hasMore : true , text : error . toString ( ) } ) ;
103139 resolve ( error . toString ( ) ) ;
140+ emitter . emit ( 'chatGPTComplete' , error . toString ( ) ) ;
104141 } ) ;
105142 request . write ( JSON . stringify ( body ) ) ;
106143 request . end ( ) ;
@@ -109,15 +146,37 @@ export const createChatCompletion = (options: {
109146export const createChatCompletionForScript = ( options : {
110147 messages : { role : 'system' | 'user' | 'assistant' ; content : string } [ ] ;
111148 handleChunk ?: ( data : { text ?: string ; hasMore : boolean } ) => void ;
149+ showWebview ?: boolean ;
112150} ) => {
113- const config = getChatGPTConfig ( ) ;
114- return createChatCompletion ( {
115- hostname : config . hostname ,
116- apiPath : config . apiPath ,
117- apiKey : config . apiKey ,
118- model : config . model ,
119- messages : options . messages ,
120- maxTokens : config . maxTokens ,
121- handleChunk : options . handleChunk ,
151+ if ( ! options . showWebview ) {
152+ const config = getChatGPTConfig ( ) ;
153+ return createChatCompletion ( {
154+ hostname : config . hostname ,
155+ apiPath : config . apiPath ,
156+ apiKey : config . apiKey ,
157+ model : config . model ,
158+ messages : options . messages ,
159+ maxTokens : config . maxTokens ,
160+ handleChunk : options . handleChunk ,
161+ } ) ;
162+ }
163+ // 打开 webview,使用 emitter 监听结果,把结果回传给 script
164+ showChatGPTView ( {
165+ task : {
166+ task : 'askChatGPT' ,
167+ data : options . messages . map ( ( m ) => m . content ) . join ( '\n' ) ,
168+ } ,
169+ } ) ;
170+ return new Promise < string > ( ( resolve , reject ) => {
171+ emitter . on ( 'chatGPTChunck' , ( data ) => {
172+ if ( options . handleChunk ) {
173+ options . handleChunk ( data ) ;
174+ }
175+ } ) ;
176+ emitter . on ( 'chatGPTComplete' , ( data ) => {
177+ resolve ( data ) ;
178+ emitter . off ( 'chatGPTChunck' ) ;
179+ emitter . off ( 'chatGPTComplete' ) ;
180+ } ) ;
122181 } ) ;
123182} ;
0 commit comments