aicommits 1.0.5 → 1.0.6

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <div align="center">
2
2
  <div>
3
- <img src="https://raw.githubusercontent.com/Nutlope/aicommits/main/screenshot.png" alt="AI Commits"/>
3
+ <img src=".github/screenshot.png" alt="AI Commits"/>
4
4
  <h1 align="center">AI Commits</h1>
5
5
  </div>
6
6
  <p>A CLI that writes your git commit messages for you with AI. Never write a commit message again.</p>
@@ -45,6 +45,7 @@ The next version of the CLI, version 2, will address both of these limitations a
45
45
  - Add support for diffs greater than 200 lines by grabbing the diff per file
46
46
  - Add support for a flag that can auto-accept
47
47
  - Add ability to specify a commit message from inside aicommit
48
+ - Solve latency issue (use a githook to asynchronously run gpt3 call on every git add, store the result in a temp file (or in the .git folder)
48
49
  - Use gpt-3-tokenizer
49
50
  - Add automated github releases
50
51
  - Add opt-in emoji flag
package/dist/cli.js ADDED
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env node
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
+ };
12
+ var __importDefault = (this && this.__importDefault) || function (mod) {
13
+ return (mod && mod.__esModule) ? mod : { "default": mod };
14
+ };
15
+ var _a;
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ const child_process_1 = require("child_process");
18
+ const chalk_1 = __importDefault(require("chalk"));
19
+ const inquirer_1 = __importDefault(require("inquirer"));
20
+ const openai_1 = require("openai");
21
+ const OPENAI_KEY = (_a = process.env.OPENAI_KEY) !== null && _a !== void 0 ? _a : process.env.OPENAI_API_KEY;
22
+ const generateCommitMessage = (apiKey, prompt) => __awaiter(void 0, void 0, void 0, function* () {
23
+ const openai = new openai_1.OpenAIApi(new openai_1.Configuration({ apiKey }));
24
+ try {
25
+ const completion = yield openai.createCompletion({
26
+ model: 'text-davinci-003',
27
+ prompt,
28
+ temperature: 0.7,
29
+ top_p: 1,
30
+ frequency_penalty: 0,
31
+ presence_penalty: 0,
32
+ max_tokens: 200,
33
+ stream: false,
34
+ n: 1,
35
+ });
36
+ return completion.data.choices[0].text.trim().replace(/[\n\r]/g, '');
37
+ }
38
+ catch (error) {
39
+ const errorAsAny = error;
40
+ errorAsAny.message = `OpenAI API Error: ${errorAsAny.message} - ${errorAsAny.response.statusText}`;
41
+ throw errorAsAny;
42
+ }
43
+ });
44
+ (() => __awaiter(void 0, void 0, void 0, function* () {
45
+ console.log(chalk_1.default.white('▲ ') + chalk_1.default.green('Welcome to AICommits!'));
46
+ if (!OPENAI_KEY) {
47
+ console.error(`${chalk_1.default.white('▲ ')}Please save your OpenAI API key as an env variable by doing 'export OPENAI_KEY=YOUR_API_KEY'`);
48
+ process.exit(1);
49
+ }
50
+ try {
51
+ (0, child_process_1.execSync)('git rev-parse --is-inside-work-tree', {
52
+ encoding: 'utf8',
53
+ stdio: 'ignore',
54
+ });
55
+ }
56
+ catch (_b) {
57
+ console.error(`${chalk_1.default.white('▲ ')}This is not a git repository`);
58
+ process.exit(1);
59
+ }
60
+ const diff = (0, child_process_1.execSync)('git diff --cached . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"', {
61
+ encoding: 'utf8',
62
+ });
63
+ if (!diff) {
64
+ console.log(`${chalk_1.default.white('▲ ')}No staged changes found. Make sure there are changes and run \`git add .\``);
65
+ process.exit(1);
66
+ }
67
+ // Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
68
+ if (diff.length > 8000) {
69
+ console.log(`${chalk_1.default.white('▲ ')}The diff is too large to write a commit message.`);
70
+ process.exit(1);
71
+ }
72
+ const 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, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`;
73
+ console.log(chalk_1.default.white('▲ ') + chalk_1.default.gray('Generating your AI commit message...\n'));
74
+ try {
75
+ const aiCommitMessage = yield generateCommitMessage(OPENAI_KEY, prompt);
76
+ console.log(`${chalk_1.default.white('▲ ') + chalk_1.default.bold('Commit message: ') + aiCommitMessage}\n`);
77
+ const confirmationMessage = yield inquirer_1.default.prompt([
78
+ {
79
+ name: 'useCommitMessage',
80
+ message: 'Would you like to use this commit message? (Y / n)',
81
+ choices: ['Y', 'y', 'n'],
82
+ default: 'y',
83
+ },
84
+ ]);
85
+ if (confirmationMessage.useCommitMessage === 'n') {
86
+ console.log(`${chalk_1.default.white('▲ ')}Commit message has not been commited.`);
87
+ process.exit(1);
88
+ }
89
+ (0, child_process_1.execSync)(`git commit -m "${aiCommitMessage}"`, {
90
+ stdio: 'inherit',
91
+ encoding: 'utf8',
92
+ });
93
+ }
94
+ catch (error) {
95
+ console.error(chalk_1.default.white('▲ ') + chalk_1.default.red(error.message));
96
+ process.exit(1);
97
+ }
98
+ }))();
package/package.json CHANGED
@@ -1,29 +1,35 @@
1
1
  {
2
- "name": "aicommits",
3
- "version": "1.0.5",
4
- "description": "Writes your git commit messages for you with AI",
5
- "files": [
6
- "bin"
7
- ],
8
- "bin": "bin/index.js",
9
- "repository": "https://github.com/Nutlope/aicommits",
10
- "author": "Hassan El Mghari (@nutlope)",
11
- "license": "MIT",
12
- "devDependencies": {
13
- "@types/node": "^18.13.0",
14
- "typescript": "^4.9.5"
15
- },
16
- "scripts": {
17
- "build": "tsc"
18
- },
19
- "dependencies": {
20
- "chalk": "^4.1.2",
21
- "inquirer": "^8.0.0",
22
- "node-fetch": "^2.6.9"
23
- },
24
- "keywords": [
25
- "ai",
26
- "git",
27
- "commit"
28
- ]
2
+ "name": "aicommits",
3
+ "version": "1.0.6",
4
+ "description": "Writes your git commit messages for you with AI",
5
+ "keywords": [
6
+ "ai",
7
+ "git",
8
+ "commit"
9
+ ],
10
+ "license": "MIT",
11
+ "repository": "https://github.com/Nutlope/aicommits",
12
+ "author": "Hassan El Mghari (@nutlope)",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "bin": "dist/cli.js",
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "lint": "eslint --cache ."
20
+ },
21
+ "dependencies": {
22
+ "chalk": "^4.1.2",
23
+ "inquirer": "^8.0.0",
24
+ "openai": "^3.1.0"
25
+ },
26
+ "devDependencies": {
27
+ "@pvtnbr/eslint-config": "^0.33.0",
28
+ "@types/node": "^18.13.0",
29
+ "eslint": "^8.34.0",
30
+ "typescript": "^4.9.5"
31
+ },
32
+ "eslintConfig": {
33
+ "extends": "@pvtnbr"
34
+ }
29
35
  }
package/bin/aicommits.js DELETED
@@ -1,114 +0,0 @@
1
- #!/usr/bin/env node
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
- };
12
- var __importDefault = (this && this.__importDefault) || function (mod) {
13
- return (mod && mod.__esModule) ? mod : { "default": mod };
14
- };
15
- var _a;
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.main = void 0;
18
- const chalk_1 = __importDefault(require("chalk"));
19
- const child_process_1 = require("child_process");
20
- const inquirer_1 = __importDefault(require("inquirer"));
21
- const node_fetch_1 = __importDefault(require("node-fetch"));
22
- let OPENAI_KEY = (_a = process.env.OPENAI_KEY) !== null && _a !== void 0 ? _a : process.env.OPENAI_API_KEY;
23
- function main() {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- console.log(chalk_1.default.white("▲ ") + chalk_1.default.green("Welcome to AICommits!"));
26
- if (!OPENAI_KEY) {
27
- console.error(chalk_1.default.white("▲ ") +
28
- "Please save your OpenAI API key as an env variable by doing 'export OPENAI_KEY=YOUR_API_KEY'");
29
- process.exit(1);
30
- }
31
- try {
32
- (0, child_process_1.execSync)("git rev-parse --is-inside-work-tree", {
33
- encoding: "utf8",
34
- stdio: "ignore",
35
- });
36
- }
37
- catch (e) {
38
- console.error(chalk_1.default.white("▲ ") + "This is not a git repository");
39
- process.exit(1);
40
- }
41
- const diff = (0, child_process_1.execSync)(`git diff --cached . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"`, {
42
- encoding: "utf8",
43
- });
44
- if (!diff) {
45
- console.log(chalk_1.default.white("▲ ") +
46
- "No staged changes found. Make sure there are changes and run `git add .`");
47
- process.exit(1);
48
- }
49
- // Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
50
- if (diff.length > 8000) {
51
- console.log(chalk_1.default.white("▲ ") + "The diff is too large to write a commit message.");
52
- process.exit(1);
53
- }
54
- 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, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`;
55
- console.log(chalk_1.default.white("▲ ") + chalk_1.default.gray("Generating your AI commit message...\n"));
56
- try {
57
- const aiCommitMessage = yield generateCommitMessage(prompt);
58
- console.log(chalk_1.default.white("▲ ") + chalk_1.default.bold("Commit message: ") + aiCommitMessage +
59
- "\n");
60
- const confirmationMessage = yield inquirer_1.default.prompt([
61
- {
62
- name: "useCommitMessage",
63
- message: "Would you like to use this commit message? (Y / n)",
64
- choices: ["Y", "y", "n"],
65
- default: "y",
66
- },
67
- ]);
68
- if (confirmationMessage.useCommitMessage === "n") {
69
- console.log(chalk_1.default.white("▲ ") + "Commit message has not been commited.");
70
- process.exit(1);
71
- }
72
- (0, child_process_1.execSync)(`git commit -m "${aiCommitMessage}"`, {
73
- stdio: "inherit",
74
- encoding: "utf8",
75
- });
76
- }
77
- catch (e) {
78
- console.error(chalk_1.default.white("▲ ") + chalk_1.default.red(e.message));
79
- process.exit(1);
80
- }
81
- });
82
- }
83
- exports.main = main;
84
- function generateCommitMessage(prompt) {
85
- var _a;
86
- return __awaiter(this, void 0, void 0, function* () {
87
- const payload = {
88
- model: "text-davinci-003",
89
- prompt,
90
- temperature: 0.7,
91
- top_p: 1,
92
- frequency_penalty: 0,
93
- presence_penalty: 0,
94
- max_tokens: 200,
95
- stream: false,
96
- n: 1,
97
- };
98
- const response = yield (0, node_fetch_1.default)("https://api.openai.com/v1/completions", {
99
- headers: {
100
- "Content-Type": "application/json",
101
- Authorization: `Bearer ${OPENAI_KEY !== null && OPENAI_KEY !== void 0 ? OPENAI_KEY : ""}`,
102
- },
103
- method: "POST",
104
- body: JSON.stringify(payload),
105
- });
106
- if (response.status !== 200) {
107
- const errorJson = yield response.json();
108
- throw new Error(`OpenAI API failed while processing the request '${(_a = errorJson === null || errorJson === void 0 ? void 0 : errorJson.error) === null || _a === void 0 ? void 0 : _a.message}'`);
109
- }
110
- const json = yield response.json();
111
- const aiCommit = json.choices[0].text;
112
- return aiCommit.replace(/(\r\n|\n|\r)/gm, "");
113
- });
114
- }
package/bin/index.js DELETED
@@ -1,16 +0,0 @@
1
- #! /usr/bin/env node
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
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- const aicommits_1 = require("./aicommits");
14
- (() => __awaiter(void 0, void 0, void 0, function* () {
15
- yield (0, aicommits_1.main)();
16
- }))();