aicommits 1.0.4 → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Hassan El Mghari
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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>
@@ -17,7 +17,7 @@
17
17
  Install the CLI then grab your [OpenAI key](https://openai.com/api/) and add it as an env variable with the two commands below.
18
18
 
19
19
  1. `npm install -g aicommits`
20
- 2. `export OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx`
20
+ 2. `export OPENAI_KEY=sk-xxxxxxxxxxxxxxxx`
21
21
 
22
22
  It's recommended to add the line in #2 to your `.zshrc` or `.bashrc` so it persists instead of having to define it in each terminal session.
23
23
 
@@ -45,6 +45,9 @@ 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)
49
+ - Use gpt-3-tokenizer
50
+ - Add automated github releases
48
51
  - Add opt-in emoji flag
49
52
  - Add opt-in languages flag
50
53
  - Build landing page for the 2.0 launch
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.4",
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,101 +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
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.main = void 0;
17
- const chalk_1 = __importDefault(require("chalk"));
18
- const child_process_1 = require("child_process");
19
- const inquirer_1 = __importDefault(require("inquirer"));
20
- const node_fetch_1 = __importDefault(require("node-fetch"));
21
- let OPENAI_API_KEY = process.env.OPENAI_API_KEY;
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 AICommits!"));
25
- if (!OPENAI_API_KEY) {
26
- console.error(chalk_1.default.white("▲ ") +
27
- "Please save your OpenAI API key as an env variable by doing 'export OPENAI_API_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 . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"`, {
41
- encoding: "utf8",
42
- });
43
- if (!diff) {
44
- console.log(chalk_1.default.white("▲ ") +
45
- "No staged changes found. Make sure there are changes and run `git add .`");
46
- process.exit(1);
47
- }
48
- // Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
49
- if (diff.length > 8000) {
50
- console.log(chalk_1.default.white("▲ ") + "The diff is too large to write a commit message.");
51
- process.exit(1);
52
- }
53
- 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}`;
54
- console.log(chalk_1.default.white("▲ ") + chalk_1.default.gray("Generating your AI commit message...\n"));
55
- const aiCommitMessage = yield generateCommitMessage(prompt);
56
- console.log(chalk_1.default.white("▲ ") + chalk_1.default.bold("Commit message: ") + aiCommitMessage + "\n");
57
- const confirmationMessage = yield inquirer_1.default.prompt([
58
- {
59
- name: "useCommitMessage",
60
- message: "Would you like to use this commit message? (Y / n)",
61
- choices: ["Y", "y", "n"],
62
- default: "y",
63
- },
64
- ]);
65
- if (confirmationMessage.useCommitMessage === "n") {
66
- console.log(chalk_1.default.white("▲ ") + "Commit message has not been commited.");
67
- process.exit(1);
68
- }
69
- (0, child_process_1.execSync)(`git commit -m "${aiCommitMessage}"`, {
70
- stdio: "inherit",
71
- encoding: "utf8",
72
- });
73
- });
74
- }
75
- exports.main = main;
76
- function generateCommitMessage(prompt) {
77
- return __awaiter(this, void 0, void 0, function* () {
78
- const payload = {
79
- model: "text-davinci-003",
80
- prompt,
81
- temperature: 0.7,
82
- top_p: 1,
83
- frequency_penalty: 0,
84
- presence_penalty: 0,
85
- max_tokens: 200,
86
- stream: false,
87
- n: 1,
88
- };
89
- const response = yield (0, node_fetch_1.default)("https://api.openai.com/v1/completions", {
90
- headers: {
91
- "Content-Type": "application/json",
92
- Authorization: `Bearer ${OPENAI_API_KEY !== null && OPENAI_API_KEY !== void 0 ? OPENAI_API_KEY : ""}`,
93
- },
94
- method: "POST",
95
- body: JSON.stringify(payload),
96
- });
97
- const json = yield response.json();
98
- const aiCommit = json.choices[0].text;
99
- return aiCommit.replace(/(\r\n|\n|\r)/gm, "");
100
- });
101
- }
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
- }))();