git-aicommit 4.0.1 → 5.1.0
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/.github/workflows/npm-publish.yml +1 -1
- package/README.md +1 -3
- package/autocommit.js +84 -12
- package/config.js +4 -0
- package/package.json +5 -5
package/README.md
CHANGED
package/autocommit.js
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { execSync, spawn } from "child_process";
|
|
4
4
|
import rc from 'rc';
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
ChatPromptTemplate,
|
|
7
|
+
HumanMessagePromptTemplate,
|
|
8
|
+
PromptTemplate,
|
|
9
|
+
SystemMessagePromptTemplate
|
|
10
|
+
} from "langchain/prompts";
|
|
7
11
|
import defaultConfig from './config.js';
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
import {RecursiveCharacterTextSplitter} from "langchain/text_splitter";
|
|
13
|
+
import {loadSummarizationChain} from "langchain/chains";
|
|
14
|
+
import {ChatOpenAI} from "langchain/chat_models/openai";
|
|
15
|
+
import {OpenAI} from "langchain/llms/openai";
|
|
16
|
+
import fs from "fs";
|
|
11
17
|
|
|
12
18
|
const config = rc(
|
|
13
19
|
'git-aicommit',
|
|
14
20
|
{
|
|
15
21
|
...defaultConfig,
|
|
16
22
|
openAiKey: process.env.OPENAI_API_KEY,
|
|
23
|
+
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY
|
|
17
24
|
},
|
|
18
25
|
);
|
|
19
26
|
|
|
@@ -27,21 +34,29 @@ try {
|
|
|
27
34
|
process.exit(1);
|
|
28
35
|
}
|
|
29
36
|
|
|
30
|
-
if (!config.openAiKey) {
|
|
31
|
-
console.error("Please set OPENAI_API_KEY");
|
|
37
|
+
if (!config.openAiKey && !config.azureOpenAiKey) {
|
|
38
|
+
console.error("Please set OPENAI_API_KEY or AZURE_OPENAI_API_KEY");
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// if any settings related to AZURE are set, if there are items that are not set, will error.
|
|
43
|
+
if (!(config.azureOpenAiKey && config.azureOpenAiInstanceName
|
|
44
|
+
&& config.azureOpenAiDeploymentName && config.azureOpenAiVersion)){
|
|
45
|
+
console.error("Please set AZURE_OPENAI_API_KEY, AZURE_OPENAI_API_INSTANCE_NAME, AZURE_OPENAI_API_DEPLOYMENT_NAME, AZURE_OPENAI_API_VERSION when Azure OpenAI Service.");
|
|
32
46
|
process.exit(1);
|
|
33
47
|
}
|
|
34
48
|
|
|
35
49
|
const excludeFromDiff = config.excludeFromDiff || [];
|
|
36
50
|
const diffFilter = config.diffFilter || 'ACMRTUXB';
|
|
37
51
|
const diffCommand = `git diff --staged \
|
|
52
|
+
--no-ext-diff \
|
|
38
53
|
--diff-filter=${diffFilter} \
|
|
39
54
|
-- "${excludeFromDiff.map(
|
|
40
55
|
(pattern) => `:(exclude)${pattern}`
|
|
41
56
|
).join(' ')}"
|
|
42
57
|
`;
|
|
43
58
|
|
|
44
|
-
|
|
59
|
+
let diff = execSync(diffCommand, {encoding: 'utf8'});
|
|
45
60
|
|
|
46
61
|
if (!diff) {
|
|
47
62
|
console.error("Diff seems empty. Please commit manually.");
|
|
@@ -51,17 +66,70 @@ if (!diff) {
|
|
|
51
66
|
const openai = new ChatOpenAI({
|
|
52
67
|
modelName: config.modelName,
|
|
53
68
|
openAIApiKey: config.openAiKey,
|
|
69
|
+
azureOpenAIApiKey: config.azureOpenAiKey,
|
|
70
|
+
azureOpenAIApiInstanceName: config.azureOpenAiInstanceName,
|
|
71
|
+
azureOpenAIApiDeploymentName: config.azureOpenAiDeploymentName,
|
|
72
|
+
azureOpenAIApiVersion: config.azureOpenAiVersion,
|
|
54
73
|
temperature: config.temperature,
|
|
55
74
|
maxTokens: config.maxTokens,
|
|
56
75
|
});
|
|
57
76
|
|
|
58
|
-
const systemMessagePromptTemplate = SystemMessagePromptTemplate.fromTemplate(
|
|
59
|
-
|
|
77
|
+
const systemMessagePromptTemplate = SystemMessagePromptTemplate.fromTemplate(
|
|
78
|
+
config.systemMessagePromptTemplate
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const humanPromptTemplate = HumanMessagePromptTemplate.fromTemplate(
|
|
82
|
+
config.humanPromptTemplate
|
|
83
|
+
);
|
|
60
84
|
|
|
61
85
|
const chatPrompt = ChatPromptTemplate.fromPromptMessages([
|
|
62
86
|
systemMessagePromptTemplate,
|
|
63
87
|
humanPromptTemplate,
|
|
64
|
-
])
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
if (diff.length > 2000) {
|
|
91
|
+
const filenameRegex = /^a\/(.+?)\s+b\/(.+?)/;
|
|
92
|
+
const diffByFiles = diff
|
|
93
|
+
.split('diff ' + '--git ') // Wierd string concat in order to avoid splitting on this line when using autocommit in this repo :)
|
|
94
|
+
.filter((fileDiff) => fileDiff.length > 0)
|
|
95
|
+
.map((fileDiff) => {
|
|
96
|
+
const match = fileDiff.match(filenameRegex);
|
|
97
|
+
const filename = match ? match[1] : 'Unknown file';
|
|
98
|
+
|
|
99
|
+
const content = fileDiff
|
|
100
|
+
.replaceAll(filename, '')
|
|
101
|
+
.replaceAll('a/ b/\n', '')
|
|
102
|
+
|
|
103
|
+
return chatPrompt
|
|
104
|
+
.formatMessages({
|
|
105
|
+
diff: content,
|
|
106
|
+
language: config.language,
|
|
107
|
+
})
|
|
108
|
+
.then((prompt) => {
|
|
109
|
+
return openai.call(prompt)
|
|
110
|
+
.then((res) => {
|
|
111
|
+
return {
|
|
112
|
+
filename: filename,
|
|
113
|
+
changes: res.text.trim(),
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
.catch((e) => {
|
|
117
|
+
console.error(`Error during OpenAI request: ${e.message}`);
|
|
118
|
+
process.exit(1);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// wait for all promises to resolve
|
|
124
|
+
const mergeChanges = await Promise.all(diffByFiles);
|
|
125
|
+
|
|
126
|
+
diff = mergeChanges
|
|
127
|
+
.map((fileDiff) => {
|
|
128
|
+
return `diff --git ${fileDiff.filename}\n${fileDiff.changes}`
|
|
129
|
+
|
|
130
|
+
})
|
|
131
|
+
.join('\n\n')
|
|
132
|
+
}
|
|
65
133
|
|
|
66
134
|
const prompt = await chatPrompt.formatMessages({
|
|
67
135
|
diff: diff,
|
|
@@ -69,6 +137,10 @@ const prompt = await chatPrompt.formatMessages({
|
|
|
69
137
|
})
|
|
70
138
|
|
|
71
139
|
const res = await openai.call(prompt)
|
|
140
|
+
.catch((e) => {
|
|
141
|
+
console.error(`Error during OpenAI request: ${e.message}`);
|
|
142
|
+
process.exit(1);
|
|
143
|
+
});
|
|
72
144
|
|
|
73
145
|
const commitMessage = res.text.trim();
|
|
74
146
|
|
package/config.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
openAiKey: process.env.OPENAI_API_KEY,
|
|
3
|
+
azureOpenAiKey: process.env.AZURE_OPENAI_API_KEY,
|
|
4
|
+
azureOpenAiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
|
|
5
|
+
azureOpenAiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
|
|
6
|
+
azureOpenAiVersion: process.env.AZURE_OPENAI_API_VERSION,
|
|
3
7
|
addAllChangesBeforeCommit: true,
|
|
4
8
|
autocommit: true,
|
|
5
9
|
openCommitTextEditor: false,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "git-aicommit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Generates auto commit messages with OpenAI GPT3 model",
|
|
5
5
|
"main": "autocommit.js",
|
|
6
6
|
"repository": "https://github.com/shanginn/autocommit",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"
|
|
12
|
-
"langchain": "^0.0.
|
|
13
|
-
"openai": "^3.
|
|
14
|
-
"rc": "1.2.8"
|
|
11
|
+
"@dqbd/tiktoken": "^1.0.7",
|
|
12
|
+
"langchain": "^0.0.75",
|
|
13
|
+
"openai": "^3.2.1",
|
|
14
|
+
"rc": "^1.2.8"
|
|
15
15
|
},
|
|
16
16
|
"preferGlobal": true,
|
|
17
17
|
"bin": {
|