Skip to content

Commit fce0d6d

Browse files
authored
Merge pull request #14 from adihanifsdr/add-additional-context-from-input-box
✨ feat(commit-msg): enhance commit message generation with additional context support
2 parents a469c2c + 8b3337c commit fce0d6d

4 files changed

Lines changed: 32 additions & 8 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ Use Azure/OpenAI API to review Git changes, generate conventional commit message
4646
1. Ensure that you have installed and enabled the "AI Commit" extension.
4747
2. In VSCode settings, locate the "ai-commit" configuration options and configure them as needed.
4848
3. Make changes in your project and add the changes to the staging area (git add).
49-
4. Next to the commit message input box in the "Source Control" panel, click the "AI Commit" icon button. After clicking, the extension will generate a commit message and populate it in the input box.
50-
5. Review the generated commit message, and if you are satisfied, proceed to commit your changes.
49+
4. (Optional) If you want to provide additional context for the commit message, type it in the Source Control panel's message input box before clicking the AI Commit button.
50+
5. Next to the commit message input box in the "Source Control" panel, click the "AI Commit" icon button. After clicking, the extension will generate a commit message (considering any additional context if provided) and populate it in the input box.
51+
6. Review the generated commit message, and if you are satisfied, proceed to commit your changes.
5152

5253
> **Note**\
5354
> If the code exceeds the maximum token length, consider adding it to the staging area in batches.

README.zh_CN.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
1. 确保您已经安装并启用了 `AI Commit` 扩展。
4747
2.`VSCode` 设置中,找到 "ai-commit" 配置项,并根据需要进行配置:
4848
3. 在项目中进行更改并将更改添加到暂存区 (git add)。
49-
4.`Source Control` 面板的提交消息输入框旁边,单击 `AI Commit` 图标按钮。点击后,扩展将生成 Commit 信息并填充到输入框中。
50-
5. 审核生成的 Commit 信息,如果满意,请提交更改。
49+
4. (可选) 如果您想为提交消息提供额外的上下文,请在点击 AI Commit 按钮之前,在源代码管理面板的消息输入框中输入上下文。
50+
5.`Source Control` 面板的提交消息输入框旁边,单击 `AI Commit` 图标按钮。点击后,扩展将生成 Commit 信息(如果提供了额外上下文,将会考虑在内)并填充到输入框中。
51+
6. 审核生成的 Commit 信息,如果满意,请提交更改。
5152

5253
> **Note**\
5354
> 如果超过最大 token 长度请分批将代码添加到暂存区。

src/generate-commit-msg.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ import { ProgressHandler } from './utils';
1111
* Generates a chat completion prompt for the commit message based on the provided diff.
1212
*
1313
* @param {string} diff - The diff string representing changes to be committed.
14+
* @param {string} additionalContext - Additional context for the changes.
1415
* @returns {Promise<Array<{ role: string, content: string }>>} - A promise that resolves to an array of messages for the chat completion.
1516
*/
16-
const generateCommitMessageChatCompletionPrompt = async (diff: string) => {
17+
const generateCommitMessageChatCompletionPrompt = async (diff: string, additionalContext?: string) => {
1718
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt();
1819
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];
1920

21+
if (additionalContext) {
22+
chatContextAsCompletionRequest.push({
23+
role: 'user',
24+
content: `Additional context for the changes:\n${additionalContext}`
25+
});
26+
}
27+
2028
chatContextAsCompletionRequest.push({
2129
role: 'user',
2230
content: diff
@@ -84,10 +92,20 @@ export async function generateCommitMsg(arg) {
8492
throw new Error('Unable to find the SCM input box');
8593
}
8694

87-
progress.report({ message: 'Analyzing changes...' });
88-
const messages = await generateCommitMessageChatCompletionPrompt(diff);
95+
const additionalContext = scmInputBox.value.trim();
96+
97+
progress.report({
98+
message: additionalContext
99+
? 'Analyzing changes with additional context...'
100+
: 'Analyzing changes...'
101+
});
102+
const messages = await generateCommitMessageChatCompletionPrompt(diff, additionalContext);
89103

90-
progress.report({ message: 'Generating commit message...' });
104+
progress.report({
105+
message: additionalContext
106+
? 'Generating commit message with additional context...'
107+
: 'Generating commit message...'
108+
});
91109
try {
92110
const commitMessage = await ChatGPTAPI(
93111
messages as ChatCompletionMessageParam[]

src/prompts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ You will act as a git commit message generator. When receiving a git diff, you w
7979
4. NO questions or comments
8080
5. NO formatting instructions or metadata
8181
82+
## Additional Context
83+
84+
If provided, consider any additional context about the changes when generating the commit message. This context will be provided before the diff and should influence the final commit message while maintaining all other formatting rules.
85+
8286
## Examples
8387
8488
INPUT:

0 commit comments

Comments
 (0)