Skip to content

Commit 016ea0a

Browse files
authored
Merge pull request #11 from alismx/alis/pnpm
chore: upgrade typescript to ^5.7.3 and openai to ^4.80.0
2 parents 3e45d57 + 8ceeda7 commit 016ea0a

File tree

3 files changed

+134
-37
lines changed

3 files changed

+134
-37
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"@types/node": "^18.16.3",
99
"minimist": "^1.2.8",
1010
"prettier": "^2.8.8",
11-
"typescript": "^5.0.4"
11+
"typescript": "^5.7.3"
1212
},
1313
"dependencies": {
14-
"openai": "^3.2.1"
14+
"openai": "^4.80.0"
1515
},
1616
"scripts": {
1717
"build": "npx tsc",

pnpm-lock.yaml

Lines changed: 119 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChatCompletionRequestMessage, Configuration, OpenAIApi } from "openai";
1+
import OpenAI from "openai";
22
import { exec } from "child_process";
33
import minimist = require("minimist");
44

@@ -13,6 +13,7 @@ const { GITMSG_OPENAI_API_KEY, GITMSG_COMMIT_PROMPT, GITMSG_PR_PROMPT } = proces
1313
* @param {string} [stdin] - Optional input to be passed to the command via stdin.
1414
* @returns {Promise<string>} - A Promise that resolves with the command's stdout output.
1515
*/
16+
1617
async function execHelper(command: string, stdin?: string): Promise<string> {
1718
return new Promise((resolve, reject) => {
1819
const child = exec(command);
@@ -22,22 +23,27 @@ async function execHelper(command: string, stdin?: string): Promise<string> {
2223
child.stdin?.end();
2324
}
2425
let stdout = "";
26+
// Listen for data events on the child process's stdout stream
2527
child.stdout?.on("data", (data) => {
28+
// Append the data to the stdout buffer
2629
stdout += data;
2730
});
31+
// Listen for the close event on the child process
2832
child.on("close", (code) => {
33+
// If the child process exited with a non-zero code, reject the promise
2934
if (code !== 0) {
3035
reject();
3136
} else {
37+
// Otherwise, resolve the promise with the stdout buffer
3238
resolve(stdout);
3339
}
3440
});
3541
});
3642
}
3743

3844
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[] = [
4147
{
4248
role: "user",
4349
content: diff,
@@ -47,12 +53,9 @@ async function getChatCompletion(diff: string, prompt: string) {
4753
content: prompt,
4854
},
4955
];
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({
5659
model: "gpt-4",
5760
messages,
5861
});
@@ -73,6 +76,7 @@ async function GitDiffStagedCommand() {
7376

7477
async function handleGitDiff(Command: string) {
7578
const diff = await execHelper(Command);
79+
console.log(`diff: ${diff}`);
7680
displayDiff(diff);
7781
return diff;
7882
}

0 commit comments

Comments
 (0)