-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·249 lines (210 loc) · 6.35 KB
/
index.ts
File metadata and controls
executable file
·249 lines (210 loc) · 6.35 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env node
import { name, description, version } from "./package.json";
import { Command, Option } from "commander";
import { spawn } from "child_process";
import readline from "readline";
import { z } from "zod";
import { ChatCompletionMessageParam } from "openai/resources";
import { zodResponseFormat } from "openai/helpers/zod";
import { openAIChatCompletion } from "./src/openai";
import { geminiChatCompletion } from "./src/gemini";
const fullName = name
.split("-")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
const defaultExcludedFiles = ["package-lock.json", "yarn.lock"];
const responseObject = z.object({
title: z.string(),
description: z.string(),
});
async function runCommand(
command: [string, string[]],
timeout: number = 10000
): Promise<string> {
const commandCmd = spawn(...command);
let commandOutput = "";
commandCmd.stdout.on("data", (data) => {
commandOutput += data.toString();
});
const output = await Promise.race([
new Promise((resolve) =>
setTimeout(() => {
commandCmd.kill();
resolve("");
}, timeout)
),
new Promise((resolve) =>
commandCmd.stdout.on("end", () => resolve(commandOutput))
),
]);
return output as string;
}
function lineToFilePath(line: string) {
const [status, ...fileParts] = line.split("\t");
if (status.startsWith("R")) {
return fileParts[1];
}
return fileParts[0];
}
const prompt = `You're an experienced programmer known for your precise and effective commit messages. Review the output of git diff --staged and create a commit message. The commit should include a clear and concise title that accurately summarizes the purpose of the changes, followed by a brief description that outlines the key updates or modifications made. Ensure the description highlights any new functionality, bug fixes, or refactoring, and is no longer than 2 sentences. The commit message must follow best practices for clarity and relevance to maintain a well-organized project history.`;
const conventionalCommitsPrompt =
"Format the commit title using the Conventional Commits specification: `type(scope): short imperative description`";
async function main() {
const command = new Command()
.name(name)
.version(version, "-v, --version")
.description(description)
.addOption(
new Option(
"-d, --dry-run",
"print the prompt without sending it to provider"
)
)
.addOption(
new Option(
"-m, --message <message>",
"custom message to include in the prompt"
)
)
.addOption(
new Option(
"-e, --exclude <files>",
"exclude files (comma separated values)"
)
)
.addOption(
new Option(
"-c, --conventional-commits",
"enforce commit message format as Conventional Commits"
)
)
.addOption(
new Option(
"-p, --provider <provider>",
"choose a provider to generate the commit message"
)
.choices(["openai", "gemini"])
.default("gemini")
);
command.parse();
const options = command.opts();
if (!options.dryRun) {
console.info(
`🤖 ${fullName} v${version} is running with ${options.provider} as the AI provider.`
);
}
if (options.provider && !["openai", "gemini"].includes(options.provider)) {
console.error("Invalid provider. Please choose 'openai' or 'gemini'.");
process.exit(1);
}
const excludedFiles = Array.from(
new Set(
(options.exclude ? options.exclude.split(",") : []).concat(
defaultExcludedFiles
)
)
);
const files = await runCommand([
"git",
["diff", "--staged", "--name-status"],
]);
const stagedFiles = files.split("\n").filter(Boolean);
if (!stagedFiles.length) {
throw new Error("No files to diff.");
}
let gitStagedOutput = await runCommand([
"git",
[
"diff",
"--staged",
"--",
...stagedFiles
.map(lineToFilePath)
.filter((file) => !excludedFiles.includes(file))
.filter(Boolean),
],
]);
if (!gitStagedOutput) {
throw new Error("No staged changes found.");
}
gitStagedOutput = "```\n" + gitStagedOutput + "\n```";
if (options.dryRun) {
console.info(
[
prompt,
options.conventionalCommits && conventionalCommitsPrompt,
gitStagedOutput,
options.message,
]
.filter(Boolean)
.join("\n\n")
);
process.exit();
}
const messages: ChatCompletionMessageParam[] = [
{ role: "system", content: prompt },
];
if (options.conventionalCommits) {
messages.push({
role: "system",
content: conventionalCommitsPrompt,
});
}
if (options.message) {
messages.push({
role: "user",
content: options.message,
});
}
messages.push({
role: "user",
content: gitStagedOutput,
});
const providerChatCompletion =
options.provider === "gemini" ? geminiChatCompletion : openAIChatCompletion;
const chatCompletionResponse = await providerChatCompletion(
messages,
zodResponseFormat(responseObject, "commit")
);
const commitMsgJson = JSON.parse(chatCompletionResponse) as z.infer<
typeof responseObject
>;
if (!commitMsgJson) {
throw new Error("Error parsing Chat GPT response.");
}
const commitMsg = commitMsgJson.title + "\n\n" + commitMsgJson.description;
console.info(
`\nProposed Commit:\n------------------------------\n${commitMsg}\n------------------------------`
);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const answer = await new Promise<string>((resolve) =>
rl.question("Do you want to continue? (Y/n)", (_answer) => {
rl.close();
resolve(_answer);
})
);
if (answer.toLowerCase() === "n") {
throw new Error("Commit aborted by user.");
}
console.info("Committing message...");
const psCommit = spawn("git", ["commit", "-m", commitMsg], {
stdio: "inherit",
});
const result = await new Promise<string>((resolve, reject) =>
psCommit.on("close", (code) => {
if (code !== 0) {
reject(new Error("There was an error when creating the commit."));
}
resolve("Commit created successfully.");
})
);
console.info(result);
process.exit();
}
main().catch((error) => {
console.error(error.message);
process.exit(1);
});