diff --git a/README.md b/README.md index 250bce5..7768704 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,19 @@ Have AI Commit write your git commit messages for you so you never have to write This project uses a command line interface tool called zx that's developed by google. In the script, it 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. Video coming soon where I rebuild it from scratch to show you how to easily build your own CLI tools powered by AI. Fully privacy friendly as commands only run on your local machine using your OpenAI account. +Uses conventional commits by default. If you don't want this and want to return a full sentence with the commit message, set the conventionalCommit to false at the top of the script. + ## Remaining tasks - [x] Get a working first version with zx -- [ ] pwd trick to get rid of /Users/hassan from the code -- [ ] Add better error handling for missing git add . and commit msgs about a certain amount of chars +- [x] pwd trick to get rid of /Users/hassan from the code +- [x] Add better error handling for missing git add . and commit msgs about a certain amount of chars +- [x] Add support for conventional commits by default +- [ ] Figure out how to fail gracefully instead of exit 1 +- [ ] Go over other CLI repos to see what I can add +- [ ] Test on Windows and Mac + - [ ] Try openai curie And/OR codex - [ ] Try supporting more than 200 lines by grabbing the diff per file -- [ ] Add support for conventional commits - [ ] Rewrite this CLI tool using a Node framework and publish on npm to simplify the process - [ ] Build landing page w/ demo diff --git a/aicommit.mjs b/aicommit.mjs index 6d89ca9..c712a00 100644 --- a/aicommit.mjs +++ b/aicommit.mjs @@ -1,17 +1,39 @@ #!/usr/bin/env zx $.verbose = false; -// Add console.log with "no git diff" +// TODO: Figure out how to fail gracefully instead of exit 1 + +let conventionalCommit = true; console.log(chalk.white("▲ ") + chalk.green("Welcome to AICommit!")); -let { OPENAI_API_KEY } = await fs.readJson( - "/Users/hassan/dev/ai-commit/.env.json" -); -let diff = await quiet($`git diff --cached`); +let pwd = await $`cd ~ && pwd;`; -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, do not repeat yourself, and return a complete sentence: ${diff}`; -// let prompt = `Write one detailed commit message based on the following commit. Do not preface the commit with anything, write it right away: `; +let { OPENAI_API_KEY } = await fs.readJson( + `${pwd.stdout.trim()}/ai-commit/.env.json` +); +let diff = await $`git diff --cached`; + +// Accounting for GPT-3's input req of 4k tokens (approx 8k chars) +if (diff.stdout.length > 8000) { + console.log("The diff is too large to write a commit message."); + await $`exit 1`; +} + +if (diff.stdout.length === 0) { + console.log( + "No staged changes found. Make sure there are changes and run `git add .`" + ); + await $`exit 1`; +} + +// 2371 chars = 1132 tokens = 60 LOC +// limit of 4k tokens = 8k characters +// 200 lines of code + +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${ + conventionalCommit ? " in the style of conventional commits." : "." +} Preface the commit with 'feat:' if it is a feature or 'fix:' if it is a bug, return a complete sentence, and do not repeat yourself: ${diff}`; const payload = { model: "text-davinci-003",