Skip to content

Commit

Permalink
Merge pull request #11 from alismx/alis/pnpm
Browse files Browse the repository at this point in the history
chore: upgrade typescript to ^5.7.3 and openai to ^4.80.0
  • Loading branch information
alismx authored Jan 31, 2025
2 parents 3e45d57 + 8ceeda7 commit 016ea0a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 37 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"@types/node": "^18.16.3",
"minimist": "^1.2.8",
"prettier": "^2.8.8",
"typescript": "^5.0.4"
"typescript": "^5.7.3"
},
"dependencies": {
"openai": "^3.2.1"
"openai": "^4.80.0"
},
"scripts": {
"build": "npx tsc",
Expand Down
145 changes: 119 additions & 26 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChatCompletionRequestMessage, Configuration, OpenAIApi } from "openai";
import OpenAI from "openai";
import { exec } from "child_process";
import minimist = require("minimist");

Expand All @@ -13,6 +13,7 @@ const { GITMSG_OPENAI_API_KEY, GITMSG_COMMIT_PROMPT, GITMSG_PR_PROMPT } = proces
* @param {string} [stdin] - Optional input to be passed to the command via stdin.
* @returns {Promise<string>} - A Promise that resolves with the command's stdout output.
*/

async function execHelper(command: string, stdin?: string): Promise<string> {
return new Promise((resolve, reject) => {
const child = exec(command);
Expand All @@ -22,22 +23,27 @@ async function execHelper(command: string, stdin?: string): Promise<string> {
child.stdin?.end();
}
let stdout = "";
// Listen for data events on the child process's stdout stream
child.stdout?.on("data", (data) => {
// Append the data to the stdout buffer
stdout += data;
});
// Listen for the close event on the child process
child.on("close", (code) => {
// If the child process exited with a non-zero code, reject the promise
if (code !== 0) {
reject();
} else {
// Otherwise, resolve the promise with the stdout buffer
resolve(stdout);
}
});
});
}

async function getChatCompletion(diff: string, prompt: string) {
const openai = new OpenAIApi(new Configuration({ apiKey: GITMSG_OPENAI_API_KEY }));
const messages: ChatCompletionRequestMessage[] = [
const openai = new OpenAI({ apiKey: GITMSG_OPENAI_API_KEY });
const messages: OpenAI.Chat.CreateChatCompletionRequestMessage[] = [
{
role: "user",
content: diff,
Expand All @@ -47,12 +53,9 @@ async function getChatCompletion(diff: string, prompt: string) {
content: prompt,
},
];
const {
data: {
choices: [result],
...rest
},
} = await openai.createChatCompletion({
const {
choices: [result], ...rest
} = await openai.chat.completions.create({
model: "gpt-4",
messages,
});
Expand All @@ -73,6 +76,7 @@ async function GitDiffStagedCommand() {

async function handleGitDiff(Command: string) {
const diff = await execHelper(Command);
console.log(`diff: ${diff}`);
displayDiff(diff);
return diff;
}
Expand Down

0 comments on commit 016ea0a

Please sign in to comment.