aicommits 1.0.5 → 1.0.7

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>
@@ -14,39 +14,40 @@
14
14
 
15
15
  ## Installation and Usage
16
16
 
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.
17
+ 1. Install the CLI:
18
18
 
19
- 1. `npm install -g aicommits`
20
- 2. `export OPENAI_KEY=sk-xxxxxxxxxxxxxxxx`
19
+ ```sh
20
+ npm install -g aicommits
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
+ 2. Retrieve your API key from [OpenAI](https://platform.openai.com/account/api-keys)
23
24
 
24
- After doing the two steps above, generate your commit by running `aicommits`.
25
+ > Note: If you haven't already, you'll have to create an account and set up billing.
25
26
 
26
- > Note: If you get a EACCESS error on mac/linux when running the first command, try running it with `sudo npm install -g aicommits`.
27
+ 3. Set the key so aicommits can use it:
27
28
 
28
- ## How it works
29
+ ```sh
30
+ echo "OPENAI_KEY=<your token>" >> ~/.aicommits
31
+ ```
29
32
 
30
- This CLI tool runs a `git diff` command to grab all the latest changes, sends this to OpenAI's GPT-3, then returns the AI generated commit message. I also want to note that it does cost money since GPT-3 generations aren't free. However, OpenAI gives folks $18 of free credits and commit message generations are cheap so it should be free for a long time.
33
+ 4. You're ready to go!
31
34
 
32
- Video coming soon where I rebuild it from scratch to show you how to easily build your own CLI tools powered by AI.
35
+ Run `aicommits` in any Git repo and it will generate a commit message for you.
33
36
 
34
- ## Limitations
37
+ ## How it works
35
38
 
36
- - Only supports git diffs of up to 200 lines of code for now
37
- - Does not support conventional commits
39
+ This CLI tool runs `git diff` to grab all your latest code changes, sends them to OpenAI's GPT-3, then returns the AI generated commit message.
38
40
 
39
- The next version of the CLI, version 2, will address both of these limitations as well as the tasks below!
41
+ Video coming soon where I rebuild it from scratch to show you how to easily build your own CLI tools powered by AI.
40
42
 
41
43
  ## Future tasks
42
44
 
43
- - Add a debugging flag to troubleshoot OpenAI responses
44
45
  - Add support for conventional commits as a flag that users can enable
45
- - Add support for diffs greater than 200 lines by grabbing the diff per file
46
- - Add support for a flag that can auto-accept
47
- - Add ability to specify a commit message from inside aicommit
48
- - Use gpt-3-tokenizer
49
- - Add automated github releases
50
- - Add opt-in emoji flag
51
- - Add opt-in languages flag
46
+ - Add support for diffs greater than 200 lines by grabbing the diff per file, optional flag
47
+ - Add ability to specify a commit message from inside aicommit if user doesn't like generated one
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). Put behind a flag
49
+ - Use gpt-3-tokenizer instead of hard limit on characters as a more accurate model
50
+ - Add opt-in emoji flag to preface commits with an emoji, use [this](https://gitmoji.dev) as a guide
51
+ - Add opt-in languages flag where it returns the commit in different languages
52
+ - Add automated github releases using [this action](https://github.com/manovotny/github-releases-for-automated-package-publishing-action)
52
53
  - Build landing page for the 2.0 launch
package/dist/cli.mjs ADDED
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env node
2
+ import { execa } from 'execa';
3
+ import chalk from 'chalk';
4
+ import inquirer from 'inquirer';
5
+ import fs from 'fs/promises';
6
+ import path from 'path';
7
+ import os from 'os';
8
+ import ini from 'ini';
9
+ import { OpenAIApi, Configuration } from 'openai';
10
+
11
+ const fileExists = (filePath) => fs.access(filePath).then(() => true, () => false);
12
+ const getConfig = async () => {
13
+ const configPath = path.join(os.homedir(), ".aicommits");
14
+ const configExists = await fileExists(configPath);
15
+ if (!configExists) {
16
+ return {};
17
+ }
18
+ const configString = await fs.readFile(configPath, "utf8");
19
+ return ini.parse(configString);
20
+ };
21
+ const generateCommitMessage = async (apiKey, prompt) => {
22
+ const openai = new OpenAIApi(new Configuration({ apiKey }));
23
+ try {
24
+ const completion = await openai.createCompletion({
25
+ model: "text-davinci-003",
26
+ prompt,
27
+ temperature: 0.7,
28
+ top_p: 1,
29
+ frequency_penalty: 0,
30
+ presence_penalty: 0,
31
+ max_tokens: 200,
32
+ stream: false,
33
+ n: 1
34
+ });
35
+ return completion.data.choices[0].text.trim().replace(/[\n\r]/g, "");
36
+ } catch (error) {
37
+ const errorAsAny = error;
38
+ errorAsAny.message = `OpenAI API Error: ${errorAsAny.message} - ${errorAsAny.response.statusText}`;
39
+ throw errorAsAny;
40
+ }
41
+ };
42
+
43
+ (async () => {
44
+ const config = await getConfig();
45
+ const OPENAI_KEY = process.env.OPENAI_KEY ?? process.env.OPENAI_API_KEY ?? config.OPENAI_KEY;
46
+ console.log(chalk.white("\u25B2 ") + chalk.green("Welcome to AICommits!"));
47
+ if (!OPENAI_KEY) {
48
+ console.error(
49
+ `${chalk.white("\u25B2 ")}Please save your OpenAI API key as an env variable by doing 'export OPENAI_KEY=YOUR_API_KEY'`
50
+ );
51
+ process.exit(1);
52
+ }
53
+ try {
54
+ await execa("git", ["rev-parse", "--is-inside-work-tree"]);
55
+ } catch {
56
+ console.error(`${chalk.white("\u25B2 ")}This is not a git repository`);
57
+ process.exit(1);
58
+ }
59
+ const { stdout: diff } = await execa(
60
+ "git",
61
+ ["diff", "--cached", ".", ":(exclude)package-lock.json", ":(exclude)yarn.lock", ":(exclude)pnpm-lock.yaml"]
62
+ );
63
+ if (!diff) {
64
+ console.log(
65
+ `${chalk.white("\u25B2 ")}No staged changes found. Make sure there are changes and run \`git add .\``
66
+ );
67
+ process.exit(1);
68
+ }
69
+ if (diff.length > 8e3) {
70
+ console.log(
71
+ `${chalk.white("\u25B2 ")}The diff is too large to write a commit message.`
72
+ );
73
+ process.exit(1);
74
+ }
75
+ 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}`;
76
+ console.log(
77
+ chalk.white("\u25B2 ") + chalk.gray("Generating your AI commit message...\n")
78
+ );
79
+ try {
80
+ const aiCommitMessage = await generateCommitMessage(OPENAI_KEY, prompt);
81
+ console.log(
82
+ `${chalk.white("\u25B2 ") + chalk.bold("Commit message: ") + aiCommitMessage}
83
+ `
84
+ );
85
+ const confirmationMessage = await inquirer.prompt([
86
+ {
87
+ name: "useCommitMessage",
88
+ message: "Would you like to use this commit message? (Y / n)",
89
+ choices: ["Y", "y", "n"],
90
+ default: "y"
91
+ }
92
+ ]);
93
+ if (confirmationMessage.useCommitMessage === "n") {
94
+ console.log(`${chalk.white("\u25B2 ")}Commit message has not been commited.`);
95
+ process.exit(1);
96
+ }
97
+ await execa("git", ["commit", "-m", aiCommitMessage], {
98
+ stdio: "inherit"
99
+ });
100
+ } catch (error) {
101
+ console.error(chalk.white("\u25B2 ") + chalk.red(error.message));
102
+ process.exit(1);
103
+ }
104
+ })();
package/package.json CHANGED
@@ -1,29 +1,42 @@
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.7",
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
+ "type": "module",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "bin": "dist/cli.mjs",
18
+ "scripts": {
19
+ "build": "pkgroll",
20
+ "lint": "eslint --cache .",
21
+ "type-check": "tsc"
22
+ },
23
+ "dependencies": {
24
+ "chalk": "^4.1.2",
25
+ "execa": "^7.0.0",
26
+ "ini": "^3.0.1",
27
+ "inquirer": "^8.0.0",
28
+ "openai": "^3.1.0"
29
+ },
30
+ "devDependencies": {
31
+ "@pvtnbr/eslint-config": "^0.33.0",
32
+ "@types/ini": "^1.3.31",
33
+ "@types/inquirer": "^9.0.3",
34
+ "@types/node": "^18.13.0",
35
+ "eslint": "^8.34.0",
36
+ "pkgroll": "^1.9.0",
37
+ "typescript": "^4.9.5"
38
+ },
39
+ "eslintConfig": {
40
+ "extends": "@pvtnbr"
41
+ }
29
42
  }
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
- }))();