git-aicommit 4.0.1 → 5.0.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 +70 -10
- package/package.json +5 -5
package/README.md
CHANGED
package/autocommit.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
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',
|
|
@@ -35,13 +41,14 @@ if (!config.openAiKey) {
|
|
|
35
41
|
const excludeFromDiff = config.excludeFromDiff || [];
|
|
36
42
|
const diffFilter = config.diffFilter || 'ACMRTUXB';
|
|
37
43
|
const diffCommand = `git diff --staged \
|
|
44
|
+
--no-ext-diff \
|
|
38
45
|
--diff-filter=${diffFilter} \
|
|
39
46
|
-- "${excludeFromDiff.map(
|
|
40
47
|
(pattern) => `:(exclude)${pattern}`
|
|
41
48
|
).join(' ')}"
|
|
42
49
|
`;
|
|
43
50
|
|
|
44
|
-
|
|
51
|
+
let diff = execSync(diffCommand, {encoding: 'utf8'});
|
|
45
52
|
|
|
46
53
|
if (!diff) {
|
|
47
54
|
console.error("Diff seems empty. Please commit manually.");
|
|
@@ -55,13 +62,62 @@ const openai = new ChatOpenAI({
|
|
|
55
62
|
maxTokens: config.maxTokens,
|
|
56
63
|
});
|
|
57
64
|
|
|
58
|
-
const systemMessagePromptTemplate = SystemMessagePromptTemplate.fromTemplate(
|
|
59
|
-
|
|
65
|
+
const systemMessagePromptTemplate = SystemMessagePromptTemplate.fromTemplate(
|
|
66
|
+
config.systemMessagePromptTemplate
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const humanPromptTemplate = HumanMessagePromptTemplate.fromTemplate(
|
|
70
|
+
config.humanPromptTemplate
|
|
71
|
+
);
|
|
60
72
|
|
|
61
73
|
const chatPrompt = ChatPromptTemplate.fromPromptMessages([
|
|
62
74
|
systemMessagePromptTemplate,
|
|
63
75
|
humanPromptTemplate,
|
|
64
|
-
])
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
if (diff.length > 2000) {
|
|
79
|
+
const filenameRegex = /^a\/(.+?)\s+b\/(.+?)/;
|
|
80
|
+
const diffByFiles = diff
|
|
81
|
+
.split('diff ' + '--git ') // Wierd string concat in order to avoid splitting on this line when using autocommit in this repo :)
|
|
82
|
+
.filter((fileDiff) => fileDiff.length > 0)
|
|
83
|
+
.map((fileDiff) => {
|
|
84
|
+
const match = fileDiff.match(filenameRegex);
|
|
85
|
+
const filename = match ? match[1] : 'Unknown file';
|
|
86
|
+
|
|
87
|
+
const content = fileDiff
|
|
88
|
+
.replaceAll(filename, '')
|
|
89
|
+
.replaceAll('a/ b/\n', '')
|
|
90
|
+
|
|
91
|
+
return chatPrompt
|
|
92
|
+
.formatMessages({
|
|
93
|
+
diff: content,
|
|
94
|
+
language: config.language,
|
|
95
|
+
})
|
|
96
|
+
.then((prompt) => {
|
|
97
|
+
return openai.call(prompt)
|
|
98
|
+
.then((res) => {
|
|
99
|
+
return {
|
|
100
|
+
filename: filename,
|
|
101
|
+
changes: res.text.trim(),
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
.catch((e) => {
|
|
105
|
+
console.error(`Error during OpenAI request: ${e.message}`);
|
|
106
|
+
process.exit(1);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// wait for all promises to resolve
|
|
112
|
+
const mergeChanges = await Promise.all(diffByFiles);
|
|
113
|
+
|
|
114
|
+
diff = mergeChanges
|
|
115
|
+
.map((fileDiff) => {
|
|
116
|
+
return `diff --git ${fileDiff.filename}\n${fileDiff.changes}`
|
|
117
|
+
|
|
118
|
+
})
|
|
119
|
+
.join('\n\n')
|
|
120
|
+
}
|
|
65
121
|
|
|
66
122
|
const prompt = await chatPrompt.formatMessages({
|
|
67
123
|
diff: diff,
|
|
@@ -69,6 +125,10 @@ const prompt = await chatPrompt.formatMessages({
|
|
|
69
125
|
})
|
|
70
126
|
|
|
71
127
|
const res = await openai.call(prompt)
|
|
128
|
+
.catch((e) => {
|
|
129
|
+
console.error(`Error during OpenAI request: ${e.message}`);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
});
|
|
72
132
|
|
|
73
133
|
const commitMessage = res.text.trim();
|
|
74
134
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "git-aicommit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.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.6",
|
|
12
|
+
"langchain": "^0.0.59",
|
|
13
|
+
"openai": "^3.2.1",
|
|
14
|
+
"rc": "^1.2.8"
|
|
15
15
|
},
|
|
16
16
|
"preferGlobal": true,
|
|
17
17
|
"bin": {
|