1
- import { ChatCompletionRequestMessage , Configuration , OpenAIApi } from "openai" ;
1
+ import OpenAI from "openai" ;
2
2
import { exec } from "child_process" ;
3
3
import minimist = require( "minimist" ) ;
4
4
@@ -13,6 +13,7 @@ const { GITMSG_OPENAI_API_KEY, GITMSG_COMMIT_PROMPT, GITMSG_PR_PROMPT } = proces
13
13
* @param {string } [stdin] - Optional input to be passed to the command via stdin.
14
14
* @returns {Promise<string> } - A Promise that resolves with the command's stdout output.
15
15
*/
16
+
16
17
async function execHelper ( command : string , stdin ?: string ) : Promise < string > {
17
18
return new Promise ( ( resolve , reject ) => {
18
19
const child = exec ( command ) ;
@@ -22,22 +23,27 @@ async function execHelper(command: string, stdin?: string): Promise<string> {
22
23
child . stdin ?. end ( ) ;
23
24
}
24
25
let stdout = "" ;
26
+ // Listen for data events on the child process's stdout stream
25
27
child . stdout ?. on ( "data" , ( data ) => {
28
+ // Append the data to the stdout buffer
26
29
stdout += data ;
27
30
} ) ;
31
+ // Listen for the close event on the child process
28
32
child . on ( "close" , ( code ) => {
33
+ // If the child process exited with a non-zero code, reject the promise
29
34
if ( code !== 0 ) {
30
35
reject ( ) ;
31
36
} else {
37
+ // Otherwise, resolve the promise with the stdout buffer
32
38
resolve ( stdout ) ;
33
39
}
34
40
} ) ;
35
41
} ) ;
36
42
}
37
43
38
44
async function getChatCompletion ( diff : string , prompt : string ) {
39
- const openai = new OpenAIApi ( new Configuration ( { apiKey : GITMSG_OPENAI_API_KEY } ) ) ;
40
- const messages : ChatCompletionRequestMessage [ ] = [
45
+ const openai = new OpenAI ( { apiKey : GITMSG_OPENAI_API_KEY } ) ;
46
+ const messages : OpenAI . Chat . CreateChatCompletionRequestMessage [ ] = [
41
47
{
42
48
role : "user" ,
43
49
content : diff ,
@@ -47,12 +53,9 @@ async function getChatCompletion(diff: string, prompt: string) {
47
53
content : prompt ,
48
54
} ,
49
55
] ;
50
- const {
51
- data : {
52
- choices : [ result ] ,
53
- ...rest
54
- } ,
55
- } = await openai . createChatCompletion ( {
56
+ const {
57
+ choices : [ result ] , ...rest
58
+ } = await openai . chat . completions . create ( {
56
59
model : "gpt-4" ,
57
60
messages,
58
61
} ) ;
@@ -73,6 +76,7 @@ async function GitDiffStagedCommand() {
73
76
74
77
async function handleGitDiff ( Command : string ) {
75
78
const diff = await execHelper ( Command ) ;
79
+ console . log ( `diff: ${ diff } ` ) ;
76
80
displayDiff ( diff ) ;
77
81
return diff ;
78
82
}
0 commit comments