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.
@@ -11,7 +11,7 @@ jobs:
11
11
  - uses: actions/checkout@v3
12
12
  - uses: actions/setup-node@v3
13
13
  with:
14
- node-version: 16
14
+ node-version: 18
15
15
  registry-url: https://registry.npmjs.org/
16
16
  - run: npm ci
17
17
  - run: npm publish
package/README.md CHANGED
@@ -8,10 +8,8 @@ Tired of writing commit messages? Let the computer do it for you!
8
8
 
9
9
  ## Installation
10
10
 
11
- It's recommended to use [bun](https://bun.sh/)
12
-
13
11
  ```bash
14
- bun install -g git-aicommit
12
+ npm install -g git-aicommit
15
13
  ```
16
14
 
17
15
  ## Configuration
package/autocommit.js CHANGED
@@ -1,13 +1,19 @@
1
- #!/usr/bin/env bun
1
+ #!/usr/bin/env node
2
2
 
3
3
  import { execSync, spawn } from "child_process";
4
4
  import rc from 'rc';
5
- import { ChatOpenAI } from "langchain/chat_models";
6
- import {ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate} from "langchain/prompts";
5
+ import {
6
+ ChatPromptTemplate,
7
+ HumanMessagePromptTemplate,
8
+ PromptTemplate,
9
+ SystemMessagePromptTemplate
10
+ } from "langchain/prompts";
7
11
  import defaultConfig from './config.js';
8
- import dotenv from 'dotenv';
9
-
10
- dotenv.config();
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
- const diff = execSync(diffCommand, {encoding: 'utf8'});
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(config.systemMessagePromptTemplate)
59
- const humanPromptTemplate = HumanMessagePromptTemplate.fromTemplate(config.humanPromptTemplate)
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": "4.0.1",
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
- "dotenv": "^16.0.2",
12
- "langchain": "^0.0.47",
13
- "openai": "^3.0.0",
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": {