aicommits 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/aicommits.js +80 -67
- package/bin/index.js +12 -3
- package/package.json +2 -2
- package/tsconfig.json +1 -1
package/bin/aicommits.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
3
12
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
13
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
14
|
};
|
|
@@ -10,77 +19,81 @@ const child_process_1 = require("child_process");
|
|
|
10
19
|
const inquirer_1 = __importDefault(require("inquirer"));
|
|
11
20
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
12
21
|
let OPENAI_API_KEY = process.env.OPENAI_API_KEY;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
function main() {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
console.log(chalk_1.default.white("▲ ") + chalk_1.default.green("Welcome to AICommit!"));
|
|
25
|
+
if (!OPENAI_API_KEY) {
|
|
26
|
+
console.error(chalk_1.default.white("▲ ") +
|
|
27
|
+
"Please specify an OpenAI key using export OPEN_AI_KEY='YOUR_API_KEY'");
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
(0, child_process_1.execSync)("git rev-parse --is-inside-work-tree", {
|
|
32
|
+
encoding: "utf8",
|
|
33
|
+
stdio: "ignore",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
console.error(chalk_1.default.white("▲ ") + "This is not a git repository");
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
const diff = (0, child_process_1.execSync)("git diff --cached", { encoding: "utf8" });
|
|
41
|
+
if (!diff) {
|
|
42
|
+
console.log(chalk_1.default.white("▲ ") +
|
|
43
|
+
"No staged changes found. Make sure there are changes and run `git add .`");
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
// Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
|
|
47
|
+
if (diff.length > 8000) {
|
|
48
|
+
console.log(chalk_1.default.white("▲ ") + "The diff is too large to write a commit message.");
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
let prompt = `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, return a complete sentence, and do not repeat yourself: ${diff}`;
|
|
52
|
+
console.log(chalk_1.default.white("▲ ") + chalk_1.default.gray("Generating your AI commit message...\n"));
|
|
53
|
+
const aiCommitMessage = yield generateCommitMessage(prompt);
|
|
54
|
+
console.log(chalk_1.default.white("▲ ") + chalk_1.default.bold("Commit message: ") + aiCommitMessage + "\n");
|
|
55
|
+
const confirmationMessage = yield inquirer_1.default.prompt([
|
|
56
|
+
{
|
|
57
|
+
name: "useCommitMessage",
|
|
58
|
+
message: "Would you like to use this commit message? (Y / n)",
|
|
59
|
+
choices: ["Y", "y", "n"],
|
|
60
|
+
default: "y",
|
|
61
|
+
},
|
|
62
|
+
]);
|
|
63
|
+
if (confirmationMessage.useCommitMessage === "n") {
|
|
64
|
+
console.log(chalk_1.default.white("▲ ") + "Commit message has not been commited.");
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
(0, child_process_1.execSync)(`git commit -m "${aiCommitMessage}"`, {
|
|
68
|
+
stdio: "inherit",
|
|
22
69
|
encoding: "utf8",
|
|
23
|
-
stdio: "ignore",
|
|
24
70
|
});
|
|
25
|
-
}
|
|
26
|
-
catch (e) {
|
|
27
|
-
console.error(chalk_1.default.white("▲ ") + "This is not a git repository");
|
|
28
|
-
process.exit(1);
|
|
29
|
-
}
|
|
30
|
-
const diff = (0, child_process_1.execSync)("git diff --cached", { encoding: "utf8" });
|
|
31
|
-
if (!diff) {
|
|
32
|
-
console.log(chalk_1.default.white("▲ ") +
|
|
33
|
-
"No staged changes found. Make sure there are changes and run `git add .`");
|
|
34
|
-
process.exit(1);
|
|
35
|
-
}
|
|
36
|
-
// Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
|
|
37
|
-
if (diff.length > 8000) {
|
|
38
|
-
console.log(chalk_1.default.white("▲ ") + "The diff is too large to write a commit message.");
|
|
39
|
-
process.exit(1);
|
|
40
|
-
}
|
|
41
|
-
let prompt = `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, return a complete sentence, and do not repeat yourself: ${diff}`;
|
|
42
|
-
console.log(chalk_1.default.white("▲ ") + chalk_1.default.gray("Generating your AI commit message...\n"));
|
|
43
|
-
const aiCommitMessage = await generateCommitMessage(prompt);
|
|
44
|
-
console.log(chalk_1.default.white("▲ ") + chalk_1.default.bold("Commit message: ") + aiCommitMessage + "\n");
|
|
45
|
-
const confirmationMessage = await inquirer_1.default.prompt([
|
|
46
|
-
{
|
|
47
|
-
name: "useCommitMessage",
|
|
48
|
-
message: "Would you like to use this commit message? (Y / n)",
|
|
49
|
-
choices: ["Y", "y", "n"],
|
|
50
|
-
default: "y",
|
|
51
|
-
},
|
|
52
|
-
]);
|
|
53
|
-
if (confirmationMessage.useCommitMessage === "n") {
|
|
54
|
-
console.log(chalk_1.default.white("▲ ") + "Commit message has not been commited.");
|
|
55
|
-
process.exit(1);
|
|
56
|
-
}
|
|
57
|
-
(0, child_process_1.execSync)(`git commit -m "${aiCommitMessage}"`, {
|
|
58
|
-
stdio: "inherit",
|
|
59
|
-
encoding: "utf8",
|
|
60
71
|
});
|
|
61
72
|
}
|
|
62
73
|
exports.main = main;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
74
|
+
function generateCommitMessage(prompt) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
const payload = {
|
|
77
|
+
model: "text-davinci-003",
|
|
78
|
+
prompt,
|
|
79
|
+
temperature: 0.7,
|
|
80
|
+
top_p: 1,
|
|
81
|
+
frequency_penalty: 0,
|
|
82
|
+
presence_penalty: 0,
|
|
83
|
+
max_tokens: 200,
|
|
84
|
+
stream: false,
|
|
85
|
+
n: 1,
|
|
86
|
+
};
|
|
87
|
+
const response = yield (0, node_fetch_1.default)("https://api.openai.com/v1/completions", {
|
|
88
|
+
headers: {
|
|
89
|
+
"Content-Type": "application/json",
|
|
90
|
+
Authorization: `Bearer ${OPENAI_API_KEY !== null && OPENAI_API_KEY !== void 0 ? OPENAI_API_KEY : ""}`,
|
|
91
|
+
},
|
|
92
|
+
method: "POST",
|
|
93
|
+
body: JSON.stringify(payload),
|
|
94
|
+
});
|
|
95
|
+
const json = yield response.json();
|
|
96
|
+
const aiCommit = json.choices[0].text;
|
|
97
|
+
return aiCommit.replace(/(\r\n|\n|\r)/gm, "");
|
|
82
98
|
});
|
|
83
|
-
const json = await response.json();
|
|
84
|
-
const aiCommit = json.choices[0].text;
|
|
85
|
-
return aiCommit.replace(/(\r\n|\n|\r)/gm, "");
|
|
86
99
|
}
|
package/bin/index.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
3
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
13
|
const aicommits_1 = require("./aicommits");
|
|
5
|
-
(
|
|
6
|
-
|
|
7
|
-
})();
|
|
14
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
yield (0, aicommits_1.main)();
|
|
16
|
+
}))();
|
package/package.json
CHANGED