-
Notifications
You must be signed in to change notification settings - Fork 91
/
index.ts
126 lines (107 loc) · 2.98 KB
/
index.ts
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
#!/usr/bin/env node
import { execSync } from "child_process";
import enquirer from "enquirer";
import ora from "ora";
import { ChatGPTClient } from "./client.js";
import { loadPromptTemplate } from "./config_storage.js";
const debug = (...args: unknown[]) => {
if (process.env.DEBUG) {
console.debug(...args);
}
};
const CUSTOM_MESSAGE_OPTION = "[write own message]...";
const spinner = ora();
let diff = "";
try {
diff = execSync("git diff --cached").toString();
if (!diff) {
console.log("No changes to commit.");
process.exit(0);
}
} catch (e) {
console.log("Failed to run git diff --cached");
process.exit(1);
}
run(diff)
.then(() => {
process.exit(0);
})
.catch((e: Error) => {
console.log("Error: " + e.message, e.cause ?? "");
process.exit(1);
});
async function run(diff: string) {
// TODO: we should use a good tokenizer here
const diffTokens = diff.split(" ").length;
if (diffTokens > 2000) {
console.log(`Diff is way too bug. Truncating to 700 tokens. It may help`);
diff = diff.split(" ").slice(0, 700).join(" ");
}
const api = new ChatGPTClient();
const prompt = loadPromptTemplate().replace(
"{{diff}}",
["```", diff, "```"].join("\n")
);
while (true) {
debug("prompt: ", prompt);
const choices = await getMessages(api, prompt);
try {
const answer = await enquirer.prompt<{ message: string }>({
type: "select",
name: "message",
message: "Pick a message",
choices,
});
if (answer.message === CUSTOM_MESSAGE_OPTION) {
execSync("git commit", { stdio: "inherit" });
return;
} else {
execSync(`git commit -m '${escapeCommitMessage(answer.message)}'`, {
stdio: "inherit",
});
return;
}
} catch (e) {
console.log("Aborted.");
console.log(e);
process.exit(1);
}
}
}
async function getMessages(api: ChatGPTClient, request: string) {
spinner.start("Asking ChatGPT 🤖 for commit messages...");
// send a message and wait for the response
try {
const response = await api.getAnswer(request);
// find json array of strings in the response
const messages = response
.split("\n")
.map(normalizeMessage)
.filter((l) => l.length > 1);
spinner.stop();
debug("response: ", response);
messages.push(CUSTOM_MESSAGE_OPTION);
return messages;
} catch (e) {
spinner.stop();
if (e.message === "Unauthorized") {
return getMessages(api, request);
} else {
throw e;
}
}
}
function normalizeMessage(line: string) {
return line
.trim()
.replace(/^(\d+\.|-|\*)\s+/, "")
.replace(/^[`"']/, "")
.replace(/[`"']$/, "")
.replace(/[`"']:/, ":") // sometimes it formats messages like this: `feat`: message
.replace(/:[`"']/, ":") // sometimes it formats messages like this: `feat:` message
.replace(/\\n/g, "")
.trim();
}
function escapeCommitMessage(message: string) {
return message.replace(/'/, `''`);
}