-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·124 lines (106 loc) · 3.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
import OpenAI from "openai";
import { promisify } from "util";
import path from "path";
import process from "process";
import { exec as originalExec, execSync } from "child_process";
import prompts from "prompts";
import { program } from "commander";
let openai;
let model = "gpt-4o"; // Default model
export async function getGitSummary() {
try {
const dotenv = await import("dotenv");
const envPath = path.join(process.cwd(), ".env");
dotenv.config({ path: envPath });
openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const exec = promisify(originalExec);
const { stdout } = await exec(
"git diff --cached -- . ':(exclude)*lock.json' ':(exclude)*lock.yaml'"
);
const summary = stdout.trim();
if (summary.length === 0) {
return null;
}
return summary;
} catch (error) {
console.error("Error while summarizing Git changes:", error);
process.exit(1);
}
}
const gptCommit = async () => {
const gitSummary = await getGitSummary();
if (!gitSummary) {
console.log("No changes to commit. Commit canceled.");
process.exit(0);
}
const messages = [
{ role: "system", content: "You are a helpful assistant." },
{
role: "user",
content: `Generate a Git commit message based on the following summary: ${gitSummary}\n\nCommit message: `,
},
];
const parameters = {
model,
messages,
n: 1,
temperature: 0,
max_tokens: 50,
};
const response = await openai.chat.completions.create(parameters);
const message = response.choices[0].message.content
.replace(/[^\w\s.:@<>/-]/gi, "")
.trim();
const confirm = await prompts({
type: "confirm",
name: "value",
message: `${message}.`,
initial: true,
});
if (confirm.value) {
execSync(`git commit -m "${message}"`); // escape double quart
console.log("Committed with the suggested message.");
} else {
console.log("Commit canceled.");
}
};
const gitExtension = (args) => {
// Extract the command and arguments from the command line
const [command, ...rest] = args;
program
.command("commit")
.description(
"Generate a Git commit message based on the summary of changes"
)
.action(async () => {
await gptCommit();
});
program
.command("model")
.description("Select the model to use")
.action(async () => {
const response = await prompts({
type: "select",
name: "value",
message: "Select a model",
choices: [
{ title: "gpt-3.5-turbo-instruct", value: "gpt-3.5-turbo-instruct" },
{ title: "gpt-4-turbo", value: "gpt-4-turbo" },
{ title: "gpt-4", value: "gpt-4" }, // New model added
],
initial: 0,
});
model = response.value;
console.log(`Model set to ${model}`);
});
// Handle invalid commands
program.on("command:*", () => {
console.error("Invalid command: %s\n", program.args.join(" "));
program.help();
process.exit(1);
});
program.parse(process.argv);
};
gitExtension(process.argv.slice(2));
export default gitExtension;