Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved OpenAI query. #294

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
quote_type = single

[*.yml]
indent_style = space
Expand Down
1 change: 0 additions & 1 deletion .github/FUNDING.yml

This file was deleted.

33 changes: 16 additions & 17 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ jobs:
contents: write

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 7
run_install: true

- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 7
run_install: true

- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: pnpm dlx semantic-release
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: pnpm dlx semantic-release
65 changes: 31 additions & 34 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,34 @@ jobs:
timeout-minutes: 10

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'

- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 7
run_install: true

- name: Type check
run: pnpm type-check

- name: Lint
run: pnpm lint

- name: Build
run: pnpm build

- name: Install tinyproxy
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get install tinyproxy
tinyproxy
- name: Test
env:
OPENAI_KEY: ${{ secrets.OPENAI_KEY }}
run: |
pnpm test
pnpm --use-node-version=14.21.3 test
- name: Checkout
uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'

- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 8
run_install: true

- name: Type check
run: pnpm type-check

- name: Build
run: pnpm build

- name: Install tinyproxy
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get install tinyproxy
tinyproxy
- name: Test
env:
OPENAI_KEY: ${{ secrets.OPENAI_KEY }}
run: |
pnpm test
pnpm --use-node-version=14.21.3 test
72 changes: 53 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@

> The minimum supported version of Node.js is the latest v14. Check your Node.js version with `node --version`.


1. Install _aicommits_:

```sh
npm install -g aicommits
```
```sh
npm install -g aicommits
```

2. Retrieve your API key from [OpenAI](https://platform.openai.com/account/api-keys)

> Note: If you haven't already, you'll have to create an account and set up billing.
> Note: If you haven't already, you'll have to create an account and set up billing.

3. Set the key so aicommits can use it:

```sh
aicommits config set OPENAI_KEY=<your token>
```

This will create a `.aicommits` file in your home directory.
```sh
aicommits config set OPENAI_KEY=<your token>
```

This will create a `.aicommits` file in your home directory.

### Upgrading

Check the installed version with:

```
aicommits --version
```
Expand All @@ -47,6 +46,7 @@ npm update -g aicommits
```

## Usage

### CLI mode

You can call `aicommits` directly to generate a commit message for your staged changes:
Expand All @@ -59,6 +59,7 @@ aicommits
`aicommits` passes down unknown flags to `git commit`, so you can pass in [`commit` flags](https://git-scm.com/docs/git-commit).

For example, you can stage all changes in tracked files with as you commit:

```sh
aicommits --all # or -a
```
Expand All @@ -68,24 +69,37 @@ aicommits --all # or -a
#### Generate multiple recommendations

Sometimes the recommended commit message isn't the best so you want it to generate a few to pick from. You can generate multiple commit messages at once by passing in the `--generate <i>` flag, where 'i' is the number of generated messages:

```sh
aicommits --generate <i> # or -g <i>
```

> Warning: this uses more tokens, meaning it costs more.

#### Generating Conventional Commits

If you'd like to generate [Conventional Commits](https://conventionalcommits.org/), you can use the `--type` flag followed by `conventional`. This will prompt `aicommits` to format the commit message according to the Conventional Commits specification:

```sh
aicommits --type conventional # or -t conventional
```

This feature can be useful if your project follows the Conventional Commits standard or if you're using tools that rely on this commit format.

### Git hook

You can also integrate _aicommits_ with Git via the [`prepare-commit-msg`](https://git-scm.com/docs/githooks#_prepare_commit_msg) hook. This lets you use Git like you normally would, and edit the commit message before committing.

#### Install

In the Git repository you want to install the hook in:

```sh
aicommits hook install
```

#### Uninstall

In the Git repository you want to uninstall the hook from:

```sh
Expand All @@ -95,12 +109,13 @@ aicommits hook uninstall
#### Usage

1. Stage your files and commit:
```sh
git add <files...>
git commit # Only generates a message when it's not passed in
```

> If you ever want to write your own message instead of generating one, you can simply pass one in: `git commit -m "My message"`
```sh
git add <files...>
git commit # Only generates a message when it's not passed in
```

> If you ever want to write your own message instead of generating one, you can simply pass one in: `git commit -m "My message"`

2. Aicommits will generate the commit message for you and pass it back to Git. Git will open it with the [configured editor](https://docs.github.com/en/get-started/getting-started-with-git/associating-text-editors-with-git) for you to review/edit it.

Expand All @@ -109,13 +124,15 @@ aicommits hook uninstall
## Configuration

### Reading a configuration value

To retrieve a configuration option, use the command:

```sh
aicommits config get <key>
```

For example, to retrieve the API key, you can use:

```sh
aicommits config get OPENAI_KEY
```
Expand Down Expand Up @@ -147,13 +164,15 @@ aicommits config set OPENAI_KEY=<your-api-key> generate=3 locale=en
```

### Options

#### OPENAI_KEY

Required

The OpenAI API key. You can retrieve it from [OpenAI API Keys page](https://platform.openai.com/account/api-keys).

#### locale

Default: `en`

The locale to use for the generated commit messages. Consult the list of codes in: https://wikipedia.org/wiki/List_of_ISO_639-1_codes.
Expand Down Expand Up @@ -184,8 +203,8 @@ The Chat Completions (`/v1/chat/completions`) model to use. Consult the list of

> Tip: If you have access, try upgrading to [`gpt-4`](https://platform.openai.com/docs/models/gpt-4) for next-level code analysis. It can handle double the input size, but comes at a higher cost. Check out OpenAI's website to learn more.


#### timeout

The timeout for network requests to the OpenAI API in milliseconds.

Default: `10000` (10 seconds)
Expand All @@ -195,6 +214,7 @@ aicommits config set timeout=20000 # 20s
```

#### max-length

The maximum character length of the generated commit message.

Default: `50`
Expand All @@ -203,6 +223,22 @@ Default: `50`
aicommits config set max-length=100
```

#### type

Default: `""` (Empty string)

The type of commit message to generate. Set this to "conventional" to generate commit messages that follow the Conventional Commits specification:

```sh
aicommits config set type=conventional
```

You can clear this option by setting it to an empty string:

```sh
aicommits config set type=
```

## How it works

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.
Expand All @@ -213,10 +249,8 @@ Video coming soon where I rebuild it from scratch to show you how to easily buil

- **Hassan El Mghari**: [@Nutlope](https://github.com/Nutlope) [<img src="https://img.shields.io/twitter/follow/nutlope?style=flat&label=nutlope&logo=twitter&color=0bf&logoColor=fff" align="center">](https://twitter.com/nutlope)


- **Hiroki Osame**: [@privatenumber](https://github.com/privatenumber) [<img src="https://img.shields.io/twitter/follow/privatenumbr?style=flat&label=privatenumbr&logo=twitter&color=0bf&logoColor=fff" align="center">](https://twitter.com/privatenumbr)


## Contributing

If you want to help fix a bug or implement a feature in [Issues](https://github.com/Nutlope/aicommits/issues), checkout the [Contribution Guide](CONTRIBUTING.md) to learn how to setup and test the project.
If you want to help fix a bug or implement a feature in [Issues](https://github.com/Nutlope/aicommits/issues), checkout the [Contribution Guide](CONTRIBUTING.md) to learn how to setup and test the project
34 changes: 2 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,36 @@
"aic": "./dist/cli.mjs"
},
"scripts": {
"prepare": "simple-git-hooks",
"build": "pkgroll --minify",
"lint": "eslint --cache .",
"lint": "",
"type-check": "tsc",
"test": "tsx tests",
"prepack": "pnpm build && clean-pkg-json"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
},
"lint-staged": {
"*.ts": "eslint --cache"
},
"dependencies": {
"@dqbd/tiktoken": "^1.0.2"
},
"devDependencies": {
"@clack/prompts": "^0.6.1",
"@pvtnbr/eslint-config": "^0.33.0",
"@clack/prompts": "^0.7.0",
"@types/ini": "^1.3.31",
"@types/inquirer": "^9.0.3",
"@types/node": "^18.14.2",
"clean-pkg-json": "^1.2.0",
"cleye": "^1.3.2",
"eslint": "^8.35.0",
"execa": "^7.0.0",
"fs-fixture": "^1.2.0",
"https-proxy-agent": "^5.0.1",
"ini": "^3.0.1",
"kolorist": "^1.7.0",
"lint-staged": "^13.1.2",
"manten": "^0.7.0",
"openai": "^3.2.1",
"pkgroll": "^1.9.0",
"simple-git-hooks": "^2.8.1",
"tsx": "^3.12.3",
"typescript": "^4.9.5"
},
"eslintConfig": {
"extends": "@pvtnbr",
"rules": {
"unicorn/no-process-exit": "off"
},
"overrides": [
{
"files": "./src/commands/prepare-commit-msg-hook.ts",
"rules": {
"unicorn/prevent-abbreviations": "off"
}
}
]
},
"release": {
"branches": [
"main"
]
},
"pnpm": {
"patchedDependencies": {
"@clack/[email protected]": "patches/@[email protected]"
}
}
}
Loading