11import * as https from 'https' ;
22import { TextDecoder } from 'util' ;
33import { getChatGPTConfig } from './config' ;
4- import { emitter } from './emitter' ;
54import { showChatGPTView } from '../webview' ;
65
76export const createChatCompletion = ( options : {
8- apiKey : string ;
9- model : string ;
10- maxTokens : number ;
11- hostname ?: string ;
12- apiPath ?: string ;
137 messages : { role : 'system' | 'user' | 'assistant' ; content : string } [ ] ;
148 handleChunk ?: ( data : { text ?: string ; hasMore : boolean } ) => void ;
159} ) =>
16- new Promise < string > ( ( resolve , reject ) => {
10+ new Promise < string > ( ( resolve ) => {
1711 let combinedResult = '' ;
1812 let error = '发生错误:' ;
13+ const config = getChatGPTConfig ( ) ;
1914 const request = https . request (
2015 {
21- hostname : options . hostname || 'api.openai.com' ,
16+ hostname : config . hostname || 'api.openai.com' ,
2217 port : 443 ,
23- path : options . apiPath || '/v1/chat/completions' ,
18+ path : config . apiPath || '/v1/chat/completions' ,
2419 method : 'POST' ,
2520 headers : {
2621 'Content-Type' : 'application/json' ,
27- Authorization : `Bearer ${ options . apiKey } ` ,
22+ Authorization : `Bearer ${ config . apiKey } ` ,
2823 } ,
2924 } ,
3025 ( res ) => {
@@ -47,7 +42,6 @@ export const createChatCompletion = (options: {
4742 if ( element . includes ( '[DONE]' ) ) {
4843 if ( options . handleChunk ) {
4944 options . handleChunk ( { hasMore : true , text : '' } ) ;
50- emitter . emit ( 'chatGPTChunck' , { hasMore : true , text : '' } ) ;
5145 }
5246 return ;
5347 }
@@ -56,7 +50,6 @@ export const createChatCompletion = (options: {
5650 if ( data . finish_reason === 'stop' ) {
5751 if ( options . handleChunk ) {
5852 options . handleChunk ( { hasMore : true , text : '' } ) ;
59- emitter . emit ( 'chatGPTChunck' , { hasMore : true , text : '' } ) ;
6053 }
6154 return ;
6255 }
@@ -67,10 +60,6 @@ export const createChatCompletion = (options: {
6760 text : openaiRes . replaceAll ( '\\n' , '\n' ) ,
6861 hasMore : true ,
6962 } ) ;
70- emitter . emit ( 'chatGPTChunck' , {
71- text : openaiRes . replaceAll ( '\\n' , '\n' ) ,
72- hasMore : true ,
73- } ) ;
7463 }
7564 combinedResult += openaiRes ;
7665 }
@@ -80,10 +69,6 @@ export const createChatCompletion = (options: {
8069 hasMore : true ,
8170 text : element ,
8271 } ) ;
83- emitter . emit ( 'chatGPTChunck' , {
84- hasMore : true ,
85- text : element ,
86- } ) ;
8772 }
8873 return ;
8974 }
@@ -102,12 +87,9 @@ export const createChatCompletion = (options: {
10287 hasMore : true ,
10388 text : e . toString ( ) ,
10489 } ) ;
105- emitter . emit ( 'chatGPTChunck' , {
106- hasMore : true ,
107- text : e . toString ( ) ,
108- } ) ;
10990 }
110- reject ( e ) ;
91+ // reject(e);
92+ resolve ( e . toString ( ) ) ;
11193 } ) ;
11294 res . on ( 'end' , ( ) => {
11395 if ( error !== '发生错误:' ) {
@@ -116,67 +98,23 @@ export const createChatCompletion = (options: {
11698 hasMore : true ,
11799 text : error ,
118100 } ) ;
119- emitter . emit ( 'chatGPTChunck' , {
120- hasMore : true ,
121- text : error ,
122- } ) ;
123101 }
124102 }
125103 resolve ( combinedResult || error ) ;
126- emitter . emit ( 'chatGPTComplete' , combinedResult || error ) ;
127104 } ) ;
128105 } ,
129106 ) ;
130107 const body = {
131- model : options . model ,
108+ model : config . model ,
132109 messages : options . messages ,
133110 stream : true ,
134- max_tokens : options . maxTokens ,
111+ max_tokens : config . maxTokens ,
135112 } ;
136113 request . on ( 'error' , ( error ) => {
137114 options . handleChunk &&
138115 options . handleChunk ( { hasMore : true , text : error . toString ( ) } ) ;
139116 resolve ( error . toString ( ) ) ;
140- emitter . emit ( 'chatGPTComplete' , error . toString ( ) ) ;
141117 } ) ;
142118 request . write ( JSON . stringify ( body ) ) ;
143119 request . end ( ) ;
144120 } ) ;
145-
146- export const createChatCompletionForScript = ( options : {
147- messages : { role : 'system' | 'user' | 'assistant' ; content : string } [ ] ;
148- handleChunk ?: ( data : { text ?: string ; hasMore : boolean } ) => void ;
149- showWebview ?: boolean ;
150- } ) => {
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- } ) ;
181- } ) ;
182- } ;
0 commit comments