mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-01-13 07:38:01 -05:00
Compare commits
54 Commits
github-act
...
v2.2.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56e02f2604 | ||
|
|
b926a627a8 | ||
|
|
32f3e176f0 | ||
|
|
cf4212016f | ||
|
|
c491fa4bad | ||
|
|
f10fc37fe7 | ||
|
|
f7b1a6358f | ||
|
|
5f9f29c467 | ||
|
|
0ec5dab80a | ||
|
|
8d47a1bb0f | ||
|
|
fad05e0757 | ||
|
|
12f7e7eaf9 | ||
|
|
6490532818 | ||
|
|
e0e953dab8 | ||
|
|
75fa04efd4 | ||
|
|
93019139fb | ||
|
|
ce47c1fe81 | ||
|
|
a880dd6bd2 | ||
|
|
f193bb1d96 | ||
|
|
4cc34dfa5f | ||
|
|
d034238505 | ||
|
|
25acb1c219 | ||
|
|
44c9d48eae | ||
|
|
702eab9ec0 | ||
|
|
e8be3858ab | ||
|
|
50e7cd3576 | ||
|
|
4f57201e98 | ||
|
|
009462f92c | ||
|
|
34775e9e69 | ||
|
|
3f3043c48e | ||
|
|
820422760a | ||
|
|
b40657c1a7 | ||
|
|
dcd8f52be3 | ||
|
|
0eff198eb9 | ||
|
|
b345eee815 | ||
|
|
cba599337d | ||
|
|
4e25f1460a | ||
|
|
61b145455d | ||
|
|
0b8dc12047 | ||
|
|
1a1134a010 | ||
|
|
d40e2ca4af | ||
|
|
540b4b3bf1 | ||
|
|
0429b92120 | ||
|
|
c6036f0570 | ||
|
|
e2eb13a678 | ||
|
|
c23b0b4806 | ||
|
|
ee540108cd | ||
|
|
a123fbd703 | ||
|
|
aff1d902d2 | ||
|
|
0d8469ee42 | ||
|
|
517734f293 | ||
|
|
6aff5ebef4 | ||
|
|
6afa493726 | ||
|
|
3eb319a919 |
@@ -21,8 +21,8 @@
|
|||||||
"rules": {
|
"rules": {
|
||||||
"prettier/prettier": "error",
|
"prettier/prettier": "error",
|
||||||
"no-console": "error",
|
"no-console": "error",
|
||||||
"sort-imports": "off",
|
|
||||||
"import/order": "off",
|
"import/order": "off",
|
||||||
|
"sort-imports": "off",
|
||||||
"simple-import-sort/imports": "error",
|
"simple-import-sort/imports": "error",
|
||||||
"simple-import-sort/exports": "error",
|
"simple-import-sort/exports": "error",
|
||||||
"import/first": "error",
|
"import/first": "error",
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
/build
|
/build
|
||||||
/dist
|
/dist
|
||||||
|
/out
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"trailingComma": "none",
|
"singleQuote": true,
|
||||||
"singleQuote": true
|
"trailingComma": "none"
|
||||||
}
|
}
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) Dima Sukharev
|
Copyright (c) Dima Sukharev, https://github.com/di-sukharev
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|||||||
49
README.md
49
README.md
@@ -5,7 +5,7 @@
|
|||||||
<h4 align="center">Follow the bird <a href="https://twitter.com/io_Y_oi"><img src="https://img.shields.io/twitter/follow/io_Y_oi?style=flat&label=io_Y_oi&logo=twitter&color=0bf&logoColor=fff" align="center"></a>
|
<h4 align="center">Follow the bird <a href="https://twitter.com/io_Y_oi"><img src="https://img.shields.io/twitter/follow/io_Y_oi?style=flat&label=io_Y_oi&logo=twitter&color=0bf&logoColor=fff" align="center"></a>
|
||||||
</h4>
|
</h4>
|
||||||
</div>
|
</div>
|
||||||
<h2>GPT CLI to auto-generate impressive commits in 1 second</h2>
|
<h2>Auto-generate meaningful commits in 1 second</h2>
|
||||||
<p>Killing lame commits with AI 🤯🔫</p>
|
<p>Killing lame commits with AI 🤯🔫</p>
|
||||||
<a href="https://www.npmjs.com/package/opencommit"><img src="https://img.shields.io/npm/v/opencommit" alt="Current version"></a>
|
<a href="https://www.npmjs.com/package/opencommit"><img src="https://img.shields.io/npm/v/opencommit" alt="Current version"></a>
|
||||||
</div>
|
</div>
|
||||||
@@ -18,22 +18,16 @@
|
|||||||
|
|
||||||
All the commits in this repo are done with OpenCommit — look into [the commits](https://github.com/di-sukharev/opencommit/commit/eae7618d575ee8d2e9fff5de56da79d40c4bc5fc) to see how OpenCommit works. Emoji and long commit description text is configurable.
|
All the commits in this repo are done with OpenCommit — look into [the commits](https://github.com/di-sukharev/opencommit/commit/eae7618d575ee8d2e9fff5de56da79d40c4bc5fc) to see how OpenCommit works. Emoji and long commit description text is configurable.
|
||||||
|
|
||||||
## Setup OpenCommit as a Github Action
|
## Setup OpenCommit as a Github Action 🔥
|
||||||
|
|
||||||
OpenCommit is now available as a GitHub Action which automatically improves all new commits messages when you push to remote!
|
OpenCommit is now available as a GitHub Action which automatically improves all new commits messages when you push to remote!
|
||||||
|
|
||||||
This is great if you want to make sure all of the commits in all of repository branches are meaningful and not lame like `fix1` or `done2`.
|
This is great if you want to make sure all of the commits in all of repository branches are meaningful and not lame like `fix1` or `done2`.
|
||||||
|
|
||||||
### Automatic 1 click setup
|
|
||||||
|
|
||||||
You can simply [setup the action automatically via the GitHub Marketplace](TODO).
|
|
||||||
|
|
||||||
### Manual 3 clicks setup
|
|
||||||
|
|
||||||
Create a file `.github/workflows/opencommit.yml` with contents below:
|
Create a file `.github/workflows/opencommit.yml` with contents below:
|
||||||
|
|
||||||
```yml
|
```yml
|
||||||
name: 'OpenCommit'
|
name: 'OpenCommit Action'
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -43,6 +37,7 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
opencommit:
|
opencommit:
|
||||||
|
timeout-minutes: 10
|
||||||
name: OpenCommit
|
name: OpenCommit
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions: write-all
|
permissions: write-all
|
||||||
@@ -54,7 +49,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- uses: di-sukharev/opencommit@github-action
|
- uses: di-sukharev/opencommit@github-action-v1.0.4
|
||||||
with:
|
with:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
@@ -81,7 +76,7 @@ Interactive rebase (`rebase -i`) changes commit SHA, so commit history in remote
|
|||||||
|
|
||||||
## Setup OpenCommit as a CLI
|
## Setup OpenCommit as a CLI
|
||||||
|
|
||||||
You can use OpenCommit by simply running it via CLI like this `oc`. 2 seconds and your staged changes are committed with a meaningful message.
|
You can use OpenCommit by simply running it via CLI like this `oco`. 2 seconds and your staged changes are committed with a meaningful message.
|
||||||
|
|
||||||
1. Install OpenCommit globally to use in any repository:
|
1. Install OpenCommit globally to use in any repository:
|
||||||
|
|
||||||
@@ -108,7 +103,7 @@ git add <files...>
|
|||||||
opencommit
|
opencommit
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also use the `oc` shortcut:
|
You can also use the `oco` shortcut:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git add <files...>
|
git add <files...>
|
||||||
@@ -138,19 +133,19 @@ Local config still has more priority as Global config, but you may set `OCO_MODE
|
|||||||
Simply run any of the variable above like this:
|
Simply run any of the variable above like this:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
oc config set OCO_OPENAI_API_KEY=gpt-4
|
oco config set OCO_OPENAI_API_KEY=gpt-4
|
||||||
```
|
```
|
||||||
|
|
||||||
Configure [GitMoji](https://gitmoji.dev/) to preface a message.
|
Configure [GitMoji](https://gitmoji.dev/) to preface a message.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
oc config set OCO_EMOJI=true
|
oco config set OCO_EMOJI=true
|
||||||
```
|
```
|
||||||
|
|
||||||
To remove preface emoji:
|
To remove preface emoji:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
oc config set OCO_EMOJI=false
|
oco config set OCO_EMOJI=false
|
||||||
```
|
```
|
||||||
|
|
||||||
### Switch to GPT-4
|
### Switch to GPT-4
|
||||||
@@ -160,7 +155,7 @@ By default OpenCommit uses GPT-3.5-turbo (ChatGPT).
|
|||||||
You may switch to GPT-4 which performs better, but costs ~x15 times more 🤠
|
You may switch to GPT-4 which performs better, but costs ~x15 times more 🤠
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
oc config set OCO_MODEL=gpt-4
|
oco config set OCO_MODEL=gpt-4
|
||||||
```
|
```
|
||||||
|
|
||||||
Make sure you do lowercase `gpt-4` and you have API access to the 4th model. Even if you have ChatGPT+ it doesn't necessarily mean that you have API access to GPT-4.
|
Make sure you do lowercase `gpt-4` and you have API access to the 4th model. Even if you have ChatGPT+ it doesn't necessarily mean that you have API access to GPT-4.
|
||||||
@@ -171,25 +166,25 @@ To globally specify the language used to generate commit messages:
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
# de, German ,Deutsch
|
# de, German ,Deutsch
|
||||||
oc config set OCO_LANGUAGE=de
|
oco config set OCO_LANGUAGE=de
|
||||||
oc config set OCO_LANGUAGE=German
|
oco config set OCO_LANGUAGE=German
|
||||||
oc config set OCO_LANGUAGE=Deutsch
|
oco config set OCO_LANGUAGE=Deutsch
|
||||||
|
|
||||||
# fr, French, française
|
# fr, French, française
|
||||||
oc config set OCO_LANGUAGE=fr
|
oco config set OCO_LANGUAGE=fr
|
||||||
oc config set OCO_LANGUAGE=French
|
oco config set OCO_LANGUAGE=French
|
||||||
oc config set OCO_LANGUAGE=française
|
oco config set OCO_LANGUAGE=française
|
||||||
```
|
```
|
||||||
|
|
||||||
The default language set is **English**
|
The default language set is **English**
|
||||||
All available languages are currently listed in the [i18n](https://github.com/di-sukharev/opencommit/tree/master/src/i18n) folder
|
All available languages are currently listed in the [i18n](https://github.com/di-sukharev/opencommit/tree/master/src/i18n) folder
|
||||||
|
|
||||||
### Git flags
|
### Git flags
|
||||||
|
|
||||||
The `opencommit` or `oc` commands can be used in place of the `git commit -m "${generatedMessage}"` command. This means that any regular flags that are used with the `git commit` command will also be applied when using `opencommit` or `oc`.
|
The `opencommit` or `oco` commands can be used in place of the `git commit -m "${generatedMessage}"` command. This means that any regular flags that are used with the `git commit` command will also be applied when using `opencommit` or `oco`.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
oc --no-verify
|
oco --no-verify
|
||||||
```
|
```
|
||||||
|
|
||||||
is translated to :
|
is translated to :
|
||||||
@@ -218,13 +213,13 @@ You can set OpenCommit as Git [`prepare-commit-msg`](https://git-scm.com/docs/gi
|
|||||||
To set the hook:
|
To set the hook:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
oc hook set
|
oco hook set
|
||||||
```
|
```
|
||||||
|
|
||||||
To unset the hook:
|
To unset the hook:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
oc hook unset
|
oco hook unset
|
||||||
```
|
```
|
||||||
|
|
||||||
To use the hook:
|
To use the hook:
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
name: 'OpenCommit'
|
name: 'OpenCommit — improve commits with AI 🧙'
|
||||||
description: 'Replaces lame commit messages with meaningful AI-generated messages when you push to remote 🤯🔫'
|
description: 'Replaces lame commit messages with meaningful AI-generated messages when you push to remote'
|
||||||
author: 'https://github.com/di-sukharev'
|
author: 'https://github.com/di-sukharev'
|
||||||
repo: 'https://github.com/di-sukharev/opencommit/tree/github-action'
|
repo: 'https://github.com/di-sukharev/opencommit/tree/github-action'
|
||||||
|
branding:
|
||||||
|
icon: 'git-commit'
|
||||||
|
color: 'green'
|
||||||
keywords:
|
keywords:
|
||||||
[
|
[
|
||||||
'git',
|
'git',
|
||||||
|
|||||||
59
out/cli.cjs
59
out/cli.cjs
@@ -16272,7 +16272,7 @@ function G3(t, e2) {
|
|||||||
// package.json
|
// package.json
|
||||||
var package_default = {
|
var package_default = {
|
||||||
name: "opencommit",
|
name: "opencommit",
|
||||||
version: "2.0.15",
|
version: "2.2.6",
|
||||||
description: "Auto-generate impressive commits in 1 second. Killing lame commits with AI \u{1F92F}\u{1F52B}",
|
description: "Auto-generate impressive commits in 1 second. Killing lame commits with AI \u{1F92F}\u{1F52B}",
|
||||||
keywords: [
|
keywords: [
|
||||||
"git",
|
"git",
|
||||||
@@ -16289,7 +16289,6 @@ var package_default = {
|
|||||||
main: "cli.js",
|
main: "cli.js",
|
||||||
bin: {
|
bin: {
|
||||||
opencommit: "./out/cli.cjs",
|
opencommit: "./out/cli.cjs",
|
||||||
oc: "./out/cli.cjs",
|
|
||||||
oco: "./out/cli.cjs"
|
oco: "./out/cli.cjs"
|
||||||
},
|
},
|
||||||
repository: {
|
repository: {
|
||||||
@@ -16314,7 +16313,8 @@ var package_default = {
|
|||||||
start: "node ./out/cli.cjs",
|
start: "node ./out/cli.cjs",
|
||||||
dev: "ts-node ./src/cli.ts",
|
dev: "ts-node ./src/cli.ts",
|
||||||
build: "rimraf out && node esbuild.config.js",
|
build: "rimraf out && node esbuild.config.js",
|
||||||
deploy: "npm run build && npm version patch && npm publish --tag latest",
|
deploy: "npm run build:push && git push --tags && npm publish --tag latest",
|
||||||
|
"build:push": "npm run build && git add . && git commit -m 'build' && git push",
|
||||||
lint: "eslint src --ext ts && tsc --noEmit",
|
lint: "eslint src --ext ts && tsc --noEmit",
|
||||||
format: "prettier --write src"
|
format: "prettier --write src"
|
||||||
},
|
},
|
||||||
@@ -17643,7 +17643,7 @@ var configValidators = {
|
|||||||
}
|
}
|
||||||
validateConfig(
|
validateConfig(
|
||||||
"OCO_OPENAI_MAX_TOKENS" /* OCO_OPENAI_MAX_TOKENS */,
|
"OCO_OPENAI_MAX_TOKENS" /* OCO_OPENAI_MAX_TOKENS */,
|
||||||
typeof value === "number",
|
value ? typeof value === "number" : void 0,
|
||||||
"Must be a number"
|
"Must be a number"
|
||||||
);
|
);
|
||||||
return value;
|
return value;
|
||||||
@@ -17674,8 +17674,8 @@ var configValidators = {
|
|||||||
},
|
},
|
||||||
["OCO_MODEL" /* OCO_MODEL */](value) {
|
["OCO_MODEL" /* OCO_MODEL */](value) {
|
||||||
validateConfig(
|
validateConfig(
|
||||||
"OCO_OPENAI_BASE_PATH" /* OCO_OPENAI_BASE_PATH */,
|
"OCO_MODEL" /* OCO_MODEL */,
|
||||||
value === "gpt-3.5-turbo" || value === "gpt-4",
|
["gpt-3.5-turbo", "gpt-4"].includes(value),
|
||||||
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
|
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
|
||||||
);
|
);
|
||||||
return value;
|
return value;
|
||||||
@@ -17685,12 +17685,12 @@ var configPath = (0, import_path.join)((0, import_os.homedir)(), ".opencommit");
|
|||||||
var getConfig = () => {
|
var getConfig = () => {
|
||||||
const configFromEnv = {
|
const configFromEnv = {
|
||||||
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
|
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
|
||||||
OCO_OPENAI_MAX_TOKENS: Number(process.env.OCO_OPENAI_MAX_TOKENS),
|
OCO_OPENAI_MAX_TOKENS: process.env.OCO_OPENAI_MAX_TOKENS ? Number(process.env.OCO_OPENAI_MAX_TOKENS) : void 0,
|
||||||
OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
|
OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
|
||||||
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === "true" ? true : false,
|
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === "true" ? true : false,
|
||||||
OCO_EMOJI: process.env.OCO_EMOJI === "true" ? true : false,
|
OCO_EMOJI: process.env.OCO_EMOJI === "true" ? true : false,
|
||||||
OCO_MODEL: process.env.OCO_MODEL,
|
OCO_MODEL: process.env.OCO_MODEL || "gpt-3.5-turbo",
|
||||||
OCO_LANGUAGE: process.env.OCO_LANGUAGE
|
OCO_LANGUAGE: process.env.OCO_LANGUAGE || "en"
|
||||||
};
|
};
|
||||||
const configExists = (0, import_fs.existsSync)(configPath);
|
const configExists = (0, import_fs.existsSync)(configPath);
|
||||||
if (!configExists)
|
if (!configExists)
|
||||||
@@ -17698,6 +17698,10 @@ var getConfig = () => {
|
|||||||
const configFile = (0, import_fs.readFileSync)(configPath, "utf8");
|
const configFile = (0, import_fs.readFileSync)(configPath, "utf8");
|
||||||
const config4 = (0, import_ini.parse)(configFile);
|
const config4 = (0, import_ini.parse)(configFile);
|
||||||
for (const configKey of Object.keys(config4)) {
|
for (const configKey of Object.keys(config4)) {
|
||||||
|
if (!config4[configKey] || ["null", "undefined"].includes(config4[configKey])) {
|
||||||
|
config4[configKey] = void 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const validator = configValidators[configKey];
|
const validator = configValidators[configKey];
|
||||||
const validValue = validator(
|
const validValue = validator(
|
||||||
@@ -21681,7 +21685,7 @@ var [command, mode] = process.argv.slice(2);
|
|||||||
if (!apiKey && command !== "config" && mode !== "set" /* set */) {
|
if (!apiKey && command !== "config" && mode !== "set" /* set */) {
|
||||||
ae("opencommit");
|
ae("opencommit");
|
||||||
ce(
|
ce(
|
||||||
"OCO_OPENAI_API_KEY is not set, please run `oc config set OCO_OPENAI_API_KEY=<your token>. Make sure you add payment details, so API works.`"
|
"OCO_OPENAI_API_KEY is not set, please run `oco config set OCO_OPENAI_API_KEY=<your token>. Make sure you add payment details, so API works.`"
|
||||||
);
|
);
|
||||||
ce(
|
ce(
|
||||||
"For help look into README https://github.com/di-sukharev/opencommit#setup"
|
"For help look into README https://github.com/di-sukharev/opencommit#setup"
|
||||||
@@ -21728,6 +21732,17 @@ var OpenAi = class {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
var getOpenCommitLatestVersion = async () => {
|
||||||
|
try {
|
||||||
|
const { data } = await axios_default.get(
|
||||||
|
"https://unpkg.com/opencommit/package.json"
|
||||||
|
);
|
||||||
|
return data.version;
|
||||||
|
} catch (_6) {
|
||||||
|
ce("Error while getting the latest version of opencommit");
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
var api = new OpenAi();
|
var api = new OpenAi();
|
||||||
|
|
||||||
// src/utils/tokenCount.ts
|
// src/utils/tokenCount.ts
|
||||||
@@ -21898,7 +21913,7 @@ var prepareCommitMessageHook = async (isStageAllFlag = false) => {
|
|||||||
if (changedFiles)
|
if (changedFiles)
|
||||||
await gitAdd({ files: changedFiles });
|
await gitAdd({ files: changedFiles });
|
||||||
else {
|
else {
|
||||||
ce("No changes detected, write some code and run `oc` again");
|
ce("No changes detected, write some code and run `oco` again");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -22032,7 +22047,7 @@ async function commit(extraArgs2 = [], isStageAllFlag = false) {
|
|||||||
if (changedFiles2)
|
if (changedFiles2)
|
||||||
await gitAdd({ files: changedFiles2 });
|
await gitAdd({ files: changedFiles2 });
|
||||||
else {
|
else {
|
||||||
ce("No changes detected, write some code and run `oc` again");
|
ce("No changes detected, write some code and run `oco` again");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -22090,6 +22105,25 @@ ${stagedFiles.map((file) => ` ${file}`).join("\n")}`
|
|||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// src/utils/checkIsLatestVersion.ts
|
||||||
|
var checkIsLatestVersion = async () => {
|
||||||
|
const latestVersion = await getOpenCommitLatestVersion();
|
||||||
|
if (latestVersion) {
|
||||||
|
const currentVersion = package_default.version;
|
||||||
|
if (currentVersion !== latestVersion) {
|
||||||
|
console.warn(
|
||||||
|
source_default.yellow(
|
||||||
|
`
|
||||||
|
You are not using the latest stable version of OpenCommit with new features and bug fixes.
|
||||||
|
Current version: ${currentVersion}. Latest version: ${latestVersion}.
|
||||||
|
\u{1F680} To update run: npm i -g opencommit@latest.
|
||||||
|
`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// src/cli.ts
|
// src/cli.ts
|
||||||
var extraArgs = process.argv.slice(2);
|
var extraArgs = process.argv.slice(2);
|
||||||
Z2(
|
Z2(
|
||||||
@@ -22102,6 +22136,7 @@ Z2(
|
|||||||
help: { description: package_default.description }
|
help: { description: package_default.description }
|
||||||
},
|
},
|
||||||
async () => {
|
async () => {
|
||||||
|
await checkIsLatestVersion();
|
||||||
if (await isHookCalled()) {
|
if (await isHookCalled()) {
|
||||||
prepareCommitMessageHook();
|
prepareCommitMessageHook();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -26839,7 +26839,7 @@ var configValidators = {
|
|||||||
}
|
}
|
||||||
validateConfig(
|
validateConfig(
|
||||||
"OCO_OPENAI_MAX_TOKENS" /* OCO_OPENAI_MAX_TOKENS */,
|
"OCO_OPENAI_MAX_TOKENS" /* OCO_OPENAI_MAX_TOKENS */,
|
||||||
typeof value === "number",
|
value ? typeof value === "number" : void 0,
|
||||||
"Must be a number"
|
"Must be a number"
|
||||||
);
|
);
|
||||||
return value;
|
return value;
|
||||||
@@ -26870,8 +26870,8 @@ var configValidators = {
|
|||||||
},
|
},
|
||||||
["OCO_MODEL" /* OCO_MODEL */](value) {
|
["OCO_MODEL" /* OCO_MODEL */](value) {
|
||||||
validateConfig(
|
validateConfig(
|
||||||
"OCO_OPENAI_BASE_PATH" /* OCO_OPENAI_BASE_PATH */,
|
"OCO_MODEL" /* OCO_MODEL */,
|
||||||
value === "gpt-3.5-turbo" || value === "gpt-4",
|
["gpt-3.5-turbo", "gpt-4"].includes(value),
|
||||||
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
|
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
|
||||||
);
|
);
|
||||||
return value;
|
return value;
|
||||||
@@ -26881,12 +26881,12 @@ var configPath = (0, import_path.join)((0, import_os.homedir)(), ".opencommit");
|
|||||||
var getConfig = () => {
|
var getConfig = () => {
|
||||||
const configFromEnv = {
|
const configFromEnv = {
|
||||||
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
|
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
|
||||||
OCO_OPENAI_MAX_TOKENS: Number(process.env.OCO_OPENAI_MAX_TOKENS),
|
OCO_OPENAI_MAX_TOKENS: process.env.OCO_OPENAI_MAX_TOKENS ? Number(process.env.OCO_OPENAI_MAX_TOKENS) : void 0,
|
||||||
OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
|
OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
|
||||||
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === "true" ? true : false,
|
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === "true" ? true : false,
|
||||||
OCO_EMOJI: process.env.OCO_EMOJI === "true" ? true : false,
|
OCO_EMOJI: process.env.OCO_EMOJI === "true" ? true : false,
|
||||||
OCO_MODEL: process.env.OCO_MODEL,
|
OCO_MODEL: process.env.OCO_MODEL || "gpt-3.5-turbo",
|
||||||
OCO_LANGUAGE: process.env.OCO_LANGUAGE
|
OCO_LANGUAGE: process.env.OCO_LANGUAGE || "en"
|
||||||
};
|
};
|
||||||
const configExists = (0, import_fs.existsSync)(configPath);
|
const configExists = (0, import_fs.existsSync)(configPath);
|
||||||
if (!configExists)
|
if (!configExists)
|
||||||
@@ -26894,6 +26894,10 @@ var getConfig = () => {
|
|||||||
const configFile = (0, import_fs.readFileSync)(configPath, "utf8");
|
const configFile = (0, import_fs.readFileSync)(configPath, "utf8");
|
||||||
const config4 = (0, import_ini.parse)(configFile);
|
const config4 = (0, import_ini.parse)(configFile);
|
||||||
for (const configKey of Object.keys(config4)) {
|
for (const configKey of Object.keys(config4)) {
|
||||||
|
if (!config4[configKey] || ["null", "undefined"].includes(config4[configKey])) {
|
||||||
|
config4[configKey] = void 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const validator = configValidators[configKey];
|
const validator = configValidators[configKey];
|
||||||
const validValue = validator(
|
const validValue = validator(
|
||||||
@@ -26969,7 +26973,7 @@ var [command, mode] = process.argv.slice(2);
|
|||||||
if (!apiKey && command !== "config" && mode !== "set" /* set */) {
|
if (!apiKey && command !== "config" && mode !== "set" /* set */) {
|
||||||
ae("opencommit");
|
ae("opencommit");
|
||||||
ce(
|
ce(
|
||||||
"OCO_OPENAI_API_KEY is not set, please run `oc config set OCO_OPENAI_API_KEY=<your token>. Make sure you add payment details, so API works.`"
|
"OCO_OPENAI_API_KEY is not set, please run `oco config set OCO_OPENAI_API_KEY=<your token>. Make sure you add payment details, so API works.`"
|
||||||
);
|
);
|
||||||
ce(
|
ce(
|
||||||
"For help look into README https://github.com/di-sukharev/opencommit#setup"
|
"For help look into README https://github.com/di-sukharev/opencommit#setup"
|
||||||
|
|||||||
5
package-lock.json
generated
5
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "opencommit",
|
"name": "opencommit",
|
||||||
"version": "2.0.15",
|
"version": "2.2.7",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "opencommit",
|
"name": "opencommit",
|
||||||
"version": "2.0.15",
|
"version": "2.2.7",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.10.0",
|
"@actions/core": "^1.10.0",
|
||||||
@@ -26,7 +26,6 @@
|
|||||||
"openai": "^3.2.1"
|
"openai": "^3.2.1"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"oc": "out/cli.cjs",
|
|
||||||
"oco": "out/cli.cjs",
|
"oco": "out/cli.cjs",
|
||||||
"opencommit": "out/cli.cjs"
|
"opencommit": "out/cli.cjs"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "opencommit",
|
"name": "opencommit",
|
||||||
"version": "2.0.15",
|
"version": "2.2.7",
|
||||||
"description": "Auto-generate impressive commits in 1 second. Killing lame commits with AI 🤯🔫",
|
"description": "Auto-generate impressive commits in 1 second. Killing lame commits with AI 🤯🔫",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"git",
|
"git",
|
||||||
@@ -17,7 +17,6 @@
|
|||||||
"main": "cli.js",
|
"main": "cli.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
"opencommit": "./out/cli.cjs",
|
"opencommit": "./out/cli.cjs",
|
||||||
"oc": "./out/cli.cjs",
|
|
||||||
"oco": "./out/cli.cjs"
|
"oco": "./out/cli.cjs"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -42,7 +41,8 @@
|
|||||||
"start": "node ./out/cli.cjs",
|
"start": "node ./out/cli.cjs",
|
||||||
"dev": "ts-node ./src/cli.ts",
|
"dev": "ts-node ./src/cli.ts",
|
||||||
"build": "rimraf out && node esbuild.config.js",
|
"build": "rimraf out && node esbuild.config.js",
|
||||||
"deploy": "npm run build && npm version patch && npm publish --tag latest",
|
"deploy": "npm run build:push && git push --tags && npm publish --tag latest",
|
||||||
|
"build:push": "npm run build && git add . && git commit -m 'build' && git push",
|
||||||
"lint": "eslint src --ext ts && tsc --noEmit",
|
"lint": "eslint src --ext ts && tsc --noEmit",
|
||||||
"format": "prettier --write src"
|
"format": "prettier --write src"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export enum COMMANDS {
|
export enum COMMANDS {
|
||||||
hook = 'hook',
|
config = 'config',
|
||||||
config = 'config'
|
hook = 'hook'
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/api.ts
14
src/api.ts
@@ -7,7 +7,9 @@ import {
|
|||||||
OpenAIApi
|
OpenAIApi
|
||||||
} from 'openai';
|
} from 'openai';
|
||||||
|
|
||||||
import { CONFIG_MODES, getConfig } from './commands/config';
|
import {CONFIG_MODES, DEFAULT_MODEL_TOKEN_LIMIT, getConfig} from './commands/config';
|
||||||
|
import {tokenCount} from './utils/tokenCount';
|
||||||
|
import {GenerateCommitMessageErrorEnum} from './generateCommitMessageFromGitDiff';
|
||||||
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
|
|
||||||
@@ -21,7 +23,7 @@ if (!apiKey && command !== 'config' && mode !== CONFIG_MODES.set) {
|
|||||||
intro('opencommit');
|
intro('opencommit');
|
||||||
|
|
||||||
outro(
|
outro(
|
||||||
'OCO_OPENAI_API_KEY is not set, please run `oc config set OCO_OPENAI_API_KEY=<your token>. Make sure you add payment details, so API works.`'
|
'OCO_OPENAI_API_KEY is not set, please run `oco config set OCO_OPENAI_API_KEY=<your token>. Make sure you add payment details, so API works.`'
|
||||||
);
|
);
|
||||||
outro(
|
outro(
|
||||||
'For help look into README https://github.com/di-sukharev/opencommit#setup'
|
'For help look into README https://github.com/di-sukharev/opencommit#setup'
|
||||||
@@ -56,6 +58,14 @@ class OpenAi {
|
|||||||
max_tokens: maxTokens || 500
|
max_tokens: maxTokens || 500
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
|
const REQUEST_TOKENS = messages.map(
|
||||||
|
(msg) => tokenCount(msg.content) + 4
|
||||||
|
).reduce((a, b) => a + b, 0);
|
||||||
|
|
||||||
|
if (REQUEST_TOKENS > (DEFAULT_MODEL_TOKEN_LIMIT - maxTokens)) {
|
||||||
|
throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens);
|
||||||
|
}
|
||||||
|
|
||||||
const { data } = await this.openAI.createChatCompletion(params);
|
const { data } = await this.openAI.createChatCompletion(params);
|
||||||
|
|
||||||
const message = data.choices[0].message;
|
const message = data.choices[0].message;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { configCommand } from './commands/config';
|
|||||||
import { hookCommand, isHookCalled } from './commands/githook.js';
|
import { hookCommand, isHookCalled } from './commands/githook.js';
|
||||||
import { prepareCommitMessageHook } from './commands/prepare-commit-msg-hook';
|
import { prepareCommitMessageHook } from './commands/prepare-commit-msg-hook';
|
||||||
import { commit } from './commands/commit';
|
import { commit } from './commands/commit';
|
||||||
// import { checkIsLatestVersion } from './utils/checkIsLatestVersion';
|
import { checkIsLatestVersion } from './utils/checkIsLatestVersion';
|
||||||
|
|
||||||
const extraArgs = process.argv.slice(2);
|
const extraArgs = process.argv.slice(2);
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ cli(
|
|||||||
help: { description: packageJSON.description }
|
help: { description: packageJSON.description }
|
||||||
},
|
},
|
||||||
async () => {
|
async () => {
|
||||||
// await checkIsLatestVersion();
|
await checkIsLatestVersion();
|
||||||
|
|
||||||
if (await isHookCalled()) {
|
if (await isHookCalled()) {
|
||||||
prepareCommitMessageHook();
|
prepareCommitMessageHook();
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { execa } from 'execa';
|
import { execa } from 'execa';
|
||||||
import {
|
import {
|
||||||
GenerateCommitMessageErrorEnum,
|
|
||||||
generateCommitMessageByDiff
|
generateCommitMessageByDiff
|
||||||
} from '../generateCommitMessageFromGitDiff';
|
} from '../generateCommitMessageFromGitDiff';
|
||||||
import {
|
import {
|
||||||
@@ -139,7 +138,7 @@ export async function commit(
|
|||||||
|
|
||||||
if (changedFiles) await gitAdd({ files: changedFiles });
|
if (changedFiles) await gitAdd({ files: changedFiles });
|
||||||
else {
|
else {
|
||||||
outro('No changes detected, write some code and run `oc` again');
|
outro('No changes detected, write some code and run `oco` again');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ export enum CONFIG_KEYS {
|
|||||||
OCO_LANGUAGE = 'OCO_LANGUAGE'
|
OCO_LANGUAGE = 'OCO_LANGUAGE'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const DEFAULT_MODEL_TOKEN_LIMIT = 4096;
|
||||||
|
|
||||||
export enum CONFIG_MODES {
|
export enum CONFIG_MODES {
|
||||||
get = 'get',
|
get = 'get',
|
||||||
set = 'set'
|
set = 'set'
|
||||||
@@ -80,7 +82,7 @@ export const configValidators = {
|
|||||||
}
|
}
|
||||||
validateConfig(
|
validateConfig(
|
||||||
CONFIG_KEYS.OCO_OPENAI_MAX_TOKENS,
|
CONFIG_KEYS.OCO_OPENAI_MAX_TOKENS,
|
||||||
typeof value === 'number',
|
value ? typeof value === 'number' : undefined,
|
||||||
'Must be a number'
|
'Must be a number'
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -117,8 +119,8 @@ export const configValidators = {
|
|||||||
|
|
||||||
[CONFIG_KEYS.OCO_MODEL](value: any) {
|
[CONFIG_KEYS.OCO_MODEL](value: any) {
|
||||||
validateConfig(
|
validateConfig(
|
||||||
CONFIG_KEYS.OCO_OPENAI_BASE_PATH,
|
CONFIG_KEYS.OCO_MODEL,
|
||||||
value === 'gpt-3.5-turbo' || value === 'gpt-4',
|
['gpt-3.5-turbo', 'gpt-4'].includes(value),
|
||||||
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
|
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
|
||||||
);
|
);
|
||||||
return value;
|
return value;
|
||||||
@@ -134,12 +136,12 @@ const configPath = pathJoin(homedir(), '.opencommit');
|
|||||||
export const getConfig = (): ConfigType | null => {
|
export const getConfig = (): ConfigType | null => {
|
||||||
const configFromEnv = {
|
const configFromEnv = {
|
||||||
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
|
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
|
||||||
OCO_OPENAI_MAX_TOKENS: Number(process.env.OCO_OPENAI_MAX_TOKENS),
|
OCO_OPENAI_MAX_TOKENS: process.env.OCO_OPENAI_MAX_TOKENS ? Number(process.env.OCO_OPENAI_MAX_TOKENS) : undefined,
|
||||||
OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
|
OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
|
||||||
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false,
|
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false,
|
||||||
OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false,
|
OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false,
|
||||||
OCO_MODEL: process.env.OCO_MODEL,
|
OCO_MODEL: process.env.OCO_MODEL || 'gpt-3.5-turbo',
|
||||||
OCO_LANGUAGE: process.env.OCO_LANGUAGE
|
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en'
|
||||||
};
|
};
|
||||||
|
|
||||||
const configExists = existsSync(configPath);
|
const configExists = existsSync(configPath);
|
||||||
@@ -149,6 +151,10 @@ export const getConfig = (): ConfigType | null => {
|
|||||||
const config = iniParse(configFile);
|
const config = iniParse(configFile);
|
||||||
|
|
||||||
for (const configKey of Object.keys(config)) {
|
for (const configKey of Object.keys(config)) {
|
||||||
|
if (!config[configKey] || ['null', 'undefined'].includes(config[configKey])) {
|
||||||
|
config[configKey] = undefined;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const validator = configValidators[configKey as CONFIG_KEYS];
|
const validator = configValidators[configKey as CONFIG_KEYS];
|
||||||
const validValue = validator(
|
const validValue = validator(
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export const prepareCommitMessageHook = async (
|
|||||||
|
|
||||||
if (changedFiles) await gitAdd({ files: changedFiles });
|
if (changedFiles) await gitAdd({ files: changedFiles });
|
||||||
else {
|
else {
|
||||||
outro('No changes detected, write some code and run `oc` again');
|
outro('No changes detected, write some code and run `oco` again');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,28 @@
|
|||||||
import {
|
import {
|
||||||
ChatCompletionRequestMessage,
|
ChatCompletionRequestMessage,
|
||||||
ChatCompletionRequestMessageRoleEnum
|
ChatCompletionRequestMessageRoleEnum
|
||||||
} from 'openai';
|
} from 'openai';
|
||||||
import { api } from './api';
|
import {api} from './api';
|
||||||
import { getConfig } from './commands/config';
|
import {DEFAULT_MODEL_TOKEN_LIMIT, getConfig} from './commands/config';
|
||||||
import { mergeDiffs } from './utils/mergeDiffs';
|
import {mergeDiffs} from './utils/mergeDiffs';
|
||||||
import { i18n, I18nLocals } from './i18n';
|
import {i18n, I18nLocals} from './i18n';
|
||||||
import { tokenCount } from './utils/tokenCount';
|
import {tokenCount} from './utils/tokenCount';
|
||||||
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
const translation = i18n[(config?.OCO_LANGUAGE as I18nLocals) || 'en'];
|
const translation = i18n[(config?.OCO_LANGUAGE as I18nLocals) || 'en'];
|
||||||
|
|
||||||
const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
|
const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
|
||||||
{
|
{
|
||||||
role: ChatCompletionRequestMessageRoleEnum.System,
|
role: ChatCompletionRequestMessageRoleEnum.System,
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
content: `You are to act as the author of a commit message in git. Your mission is to create clean and comprehensive commit messages in the conventional commit convention and explain WHAT were the changes and WHY the changes were done. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message.
|
content: `You are to act as the author of a commit message in git. Your mission is to create clean and comprehensive commit messages in the conventional commit convention and explain WHAT were the changes and WHY the changes were done. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message.
|
||||||
${config?.OCO_EMOJI ? 'Use GitMoji convention to preface the commit.': 'Do not preface the commit with anything.'}
|
${config?.OCO_EMOJI ? 'Use GitMoji convention to preface the commit.' : 'Do not preface the commit with anything.'}
|
||||||
${config?.OCO_DESCRIPTION ? 'Add a short description of WHY the changes are done after the commit message. Don\'t start it with "This commit", just describe the changes.': "Don't add any descriptions to the commit, only commit message."}
|
${config?.OCO_DESCRIPTION ? 'Add a short description of WHY the changes are done after the commit message. Don\'t start it with "This commit", just describe the changes.' : "Don't add any descriptions to the commit, only commit message."}
|
||||||
Use the present tense. Lines must not be longer than 74 characters. Use ${translation.localLanguage} to answer.`
|
Use the present tense. Lines must not be longer than 74 characters. Use ${translation.localLanguage} to answer.`
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: ChatCompletionRequestMessageRoleEnum.User,
|
role: ChatCompletionRequestMessageRoleEnum.User,
|
||||||
content: `diff --git a/src/server.ts b/src/server.ts
|
content: `diff --git a/src/server.ts b/src/server.ts
|
||||||
index ad4db42..f3b18a9 100644
|
index ad4db42..f3b18a9 100644
|
||||||
--- a/src/server.ts
|
--- a/src/server.ts
|
||||||
+++ b/src/server.ts
|
+++ b/src/server.ts
|
||||||
@@ -46,131 +46,183 @@ app.use((_, res, next) => {
|
|||||||
+app.listen(process.env.PORT || PORT, () => {
|
+app.listen(process.env.PORT || PORT, () => {
|
||||||
+ console.log(\`Server listening on port \${PORT}\`);
|
+ console.log(\`Server listening on port \${PORT}\`);
|
||||||
});`
|
});`
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: ChatCompletionRequestMessageRoleEnum.Assistant,
|
role: ChatCompletionRequestMessageRoleEnum.Assistant,
|
||||||
content: `${config?.OCO_EMOJI ? '🐛 ' : ''}${translation.commitFix}
|
content: `${config?.OCO_EMOJI ? '🐛 ' : ''}${translation.commitFix}
|
||||||
${config?.OCO_EMOJI ? '✨ ' : ''}${translation.commitFeat}
|
${config?.OCO_EMOJI ? '✨ ' : ''}${translation.commitFeat}
|
||||||
${config?.OCO_DESCRIPTION ? translation.commitDescription : ''}`
|
${config?.OCO_DESCRIPTION ? translation.commitDescription : ''}`
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const generateCommitMessageChatCompletionPrompt = (
|
const generateCommitMessageChatCompletionPrompt = (
|
||||||
diff: string
|
diff: string
|
||||||
): Array<ChatCompletionRequestMessage> => {
|
): Array<ChatCompletionRequestMessage> => {
|
||||||
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];
|
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];
|
||||||
|
|
||||||
chatContextAsCompletionRequest.push({
|
chatContextAsCompletionRequest.push({
|
||||||
role: ChatCompletionRequestMessageRoleEnum.User,
|
role: ChatCompletionRequestMessageRoleEnum.User,
|
||||||
content: diff
|
content: diff
|
||||||
});
|
});
|
||||||
|
|
||||||
return chatContextAsCompletionRequest;
|
return chatContextAsCompletionRequest;
|
||||||
};
|
};
|
||||||
|
|
||||||
export enum GenerateCommitMessageErrorEnum {
|
export enum GenerateCommitMessageErrorEnum {
|
||||||
tooMuchTokens = 'TOO_MUCH_TOKENS',
|
tooMuchTokens = 'TOO_MUCH_TOKENS',
|
||||||
internalError = 'INTERNAL_ERROR',
|
internalError = 'INTERNAL_ERROR',
|
||||||
emptyMessage = 'EMPTY_MESSAGE'
|
emptyMessage = 'EMPTY_MESSAGE'
|
||||||
}
|
|
||||||
|
|
||||||
interface GenerateCommitMessageError {
|
|
||||||
error: GenerateCommitMessageErrorEnum;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
|
const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
|
||||||
(msg) => tokenCount(msg.content) + 4
|
(msg) => tokenCount(msg.content) + 4
|
||||||
).reduce((a, b) => a + b, 0);
|
).reduce((a, b) => a + b, 0);
|
||||||
|
|
||||||
const MAX_REQ_TOKENS = 3000 - INIT_MESSAGES_PROMPT_LENGTH;
|
const ADJUSTMENT_FACTOR = 20;
|
||||||
|
|
||||||
export const generateCommitMessageByDiff = async (
|
export const generateCommitMessageByDiff = async (
|
||||||
diff: string
|
diff: string
|
||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
if (tokenCount(diff) >= MAX_REQ_TOKENS) {
|
const MAX_REQUEST_TOKENS = DEFAULT_MODEL_TOKEN_LIMIT
|
||||||
const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs(
|
- ADJUSTMENT_FACTOR
|
||||||
diff,
|
- INIT_MESSAGES_PROMPT_LENGTH
|
||||||
MAX_REQ_TOKENS
|
- config?.OCO_OPENAI_MAX_TOKENS;
|
||||||
);
|
|
||||||
|
|
||||||
const commitMessages = await Promise.all(commitMessagePromises);
|
if (tokenCount(diff) >= MAX_REQUEST_TOKENS) {
|
||||||
|
const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs(
|
||||||
|
diff,
|
||||||
|
MAX_REQUEST_TOKENS
|
||||||
|
);
|
||||||
|
|
||||||
return commitMessages.join('\n\n');
|
const commitMessages = [];
|
||||||
} else {
|
for (const promise of commitMessagePromises) {
|
||||||
const messages = generateCommitMessageChatCompletionPrompt(diff);
|
commitMessages.push(await promise);
|
||||||
|
await delay(2000);
|
||||||
|
}
|
||||||
|
|
||||||
const commitMessage = await api.generateCommitMessage(messages);
|
return commitMessages.join('\n\n');
|
||||||
|
} else {
|
||||||
|
const messages = generateCommitMessageChatCompletionPrompt(diff);
|
||||||
|
|
||||||
if (!commitMessage)
|
const commitMessage = await api.generateCommitMessage(messages);
|
||||||
throw new Error(GenerateCommitMessageErrorEnum.emptyMessage);
|
|
||||||
|
|
||||||
return commitMessage;
|
if (!commitMessage)
|
||||||
|
throw new Error(GenerateCommitMessageErrorEnum.emptyMessage);
|
||||||
|
|
||||||
|
return commitMessage;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function getMessagesPromisesByChangesInFile(
|
function getMessagesPromisesByChangesInFile(
|
||||||
fileDiff: string,
|
fileDiff: string,
|
||||||
separator: string,
|
separator: string,
|
||||||
maxChangeLength: number
|
maxChangeLength: number
|
||||||
) {
|
) {
|
||||||
const hunkHeaderSeparator = '@@ ';
|
const hunkHeaderSeparator = '@@ ';
|
||||||
const [fileHeader, ...fileDiffByLines] = fileDiff.split(hunkHeaderSeparator);
|
const [fileHeader, ...fileDiffByLines] = fileDiff.split(hunkHeaderSeparator);
|
||||||
|
|
||||||
// merge multiple line-diffs into 1 to save tokens
|
// merge multiple line-diffs into 1 to save tokens
|
||||||
const mergedChanges = mergeDiffs(
|
const mergedChanges = mergeDiffs(
|
||||||
fileDiffByLines.map((line) => hunkHeaderSeparator + line),
|
fileDiffByLines.map((line) => hunkHeaderSeparator + line),
|
||||||
maxChangeLength
|
maxChangeLength
|
||||||
);
|
|
||||||
|
|
||||||
const lineDiffsWithHeader = mergedChanges.map(
|
|
||||||
(change) => fileHeader + change
|
|
||||||
);
|
|
||||||
|
|
||||||
const commitMsgsFromFileLineDiffs = lineDiffsWithHeader.map((lineDiff) => {
|
|
||||||
const messages = generateCommitMessageChatCompletionPrompt(
|
|
||||||
separator + lineDiff
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return api.generateCommitMessage(messages);
|
const lineDiffsWithHeader = [];
|
||||||
});
|
for (const change of mergedChanges) {
|
||||||
|
const totalChange = fileHeader + change;
|
||||||
|
if (tokenCount(totalChange) > maxChangeLength) {
|
||||||
|
// If the totalChange is too large, split it into smaller pieces
|
||||||
|
const splitChanges = splitDiff(totalChange, maxChangeLength);
|
||||||
|
lineDiffsWithHeader.push(...splitChanges);
|
||||||
|
} else {
|
||||||
|
lineDiffsWithHeader.push(totalChange);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return commitMsgsFromFileLineDiffs;
|
const commitMsgsFromFileLineDiffs = lineDiffsWithHeader.map((lineDiff) => {
|
||||||
|
const messages = generateCommitMessageChatCompletionPrompt(
|
||||||
|
separator + lineDiff
|
||||||
|
);
|
||||||
|
|
||||||
|
return api.generateCommitMessage(messages);
|
||||||
|
});
|
||||||
|
|
||||||
|
return commitMsgsFromFileLineDiffs;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function splitDiff(diff: string, maxChangeLength: number) {
|
||||||
|
const lines = diff.split('\n');
|
||||||
|
const splitDiffs = [];
|
||||||
|
let currentDiff = '';
|
||||||
|
|
||||||
|
for (let line of lines) {
|
||||||
|
// If a single line exceeds maxChangeLength, split it into multiple lines
|
||||||
|
while (tokenCount(line) > maxChangeLength) {
|
||||||
|
const subLine = line.substring(0, maxChangeLength);
|
||||||
|
line = line.substring(maxChangeLength);
|
||||||
|
splitDiffs.push(subLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the tokenCount of the currentDiff and the line separately
|
||||||
|
if (tokenCount(currentDiff) + tokenCount('\n' + line) > maxChangeLength) {
|
||||||
|
// If adding the next line would exceed the maxChangeLength, start a new diff
|
||||||
|
splitDiffs.push(currentDiff);
|
||||||
|
currentDiff = line;
|
||||||
|
} else {
|
||||||
|
// Otherwise, add the line to the current diff
|
||||||
|
currentDiff += '\n' + line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the last diff
|
||||||
|
if (currentDiff) {
|
||||||
|
splitDiffs.push(currentDiff);
|
||||||
|
}
|
||||||
|
|
||||||
|
return splitDiffs;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCommitMsgsPromisesFromFileDiffs(
|
export function getCommitMsgsPromisesFromFileDiffs(
|
||||||
diff: string,
|
diff: string,
|
||||||
maxDiffLength: number
|
maxDiffLength: number
|
||||||
) {
|
) {
|
||||||
const separator = 'diff --git ';
|
const separator = 'diff --git ';
|
||||||
|
|
||||||
const diffByFiles = diff.split(separator).slice(1);
|
const diffByFiles = diff.split(separator).slice(1);
|
||||||
|
|
||||||
// merge multiple files-diffs into 1 prompt to save tokens
|
// merge multiple files-diffs into 1 prompt to save tokens
|
||||||
const mergedFilesDiffs = mergeDiffs(diffByFiles, maxDiffLength);
|
const mergedFilesDiffs = mergeDiffs(diffByFiles, maxDiffLength);
|
||||||
|
|
||||||
const commitMessagePromises = [];
|
const commitMessagePromises = [];
|
||||||
|
|
||||||
for (const fileDiff of mergedFilesDiffs) {
|
for (const fileDiff of mergedFilesDiffs) {
|
||||||
if (tokenCount(fileDiff) >= maxDiffLength) {
|
if (tokenCount(fileDiff) >= maxDiffLength) {
|
||||||
// if file-diff is bigger than gpt context — split fileDiff into lineDiff
|
// if file-diff is bigger than gpt context — split fileDiff into lineDiff
|
||||||
const messagesPromises = getMessagesPromisesByChangesInFile(
|
const messagesPromises = getMessagesPromisesByChangesInFile(
|
||||||
fileDiff,
|
fileDiff,
|
||||||
separator,
|
separator,
|
||||||
maxDiffLength
|
maxDiffLength
|
||||||
);
|
);
|
||||||
|
|
||||||
commitMessagePromises.push(...messagesPromises);
|
commitMessagePromises.push(...messagesPromises);
|
||||||
} else {
|
} else {
|
||||||
const messages = generateCommitMessageChatCompletionPrompt(
|
const messages = generateCommitMessageChatCompletionPrompt(
|
||||||
separator + fileDiff
|
separator + fileDiff
|
||||||
);
|
);
|
||||||
|
|
||||||
commitMessagePromises.push(api.generateCommitMessage(messages));
|
commitMessagePromises.push(api.generateCommitMessage(messages));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return commitMessagePromises;
|
|
||||||
|
return commitMessagePromises;
|
||||||
|
}
|
||||||
|
|
||||||
|
function delay(ms: number) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
// "rootDir": "./src",
|
// "rootDir": "./src",
|
||||||
"moduleResolution": "node",
|
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user