Skip to content

Commit

Permalink
Merge pull request #14 from adihanifsdr/add-additional-context-from-i…
Browse files Browse the repository at this point in the history
…nput-box

✨ feat(commit-msg): enhance commit message generation with additional context support
  • Loading branch information
Sitoi authored Nov 27, 2024
2 parents a469c2c + 8b3337c commit fce0d6d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ Use Azure/OpenAI API to review Git changes, generate conventional commit message
1. Ensure that you have installed and enabled the "AI Commit" extension.
2. In VSCode settings, locate the "ai-commit" configuration options and configure them as needed.
3. Make changes in your project and add the changes to the staging area (git add).
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.
5. Review the generated commit message, and if you are satisfied, proceed to commit your changes.
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.
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.
6. Review the generated commit message, and if you are satisfied, proceed to commit your changes.

> **Note**\
> If the code exceeds the maximum token length, consider adding it to the staging area in batches.
Expand Down
5 changes: 3 additions & 2 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
1. 确保您已经安装并启用了 `AI Commit` 扩展。
2.`VSCode` 设置中,找到 "ai-commit" 配置项,并根据需要进行配置:
3. 在项目中进行更改并将更改添加到暂存区 (git add)。
4.`Source Control` 面板的提交消息输入框旁边,单击 `AI Commit` 图标按钮。点击后,扩展将生成 Commit 信息并填充到输入框中。
5. 审核生成的 Commit 信息,如果满意,请提交更改。
4. (可选) 如果您想为提交消息提供额外的上下文,请在点击 AI Commit 按钮之前,在源代码管理面板的消息输入框中输入上下文。
5.`Source Control` 面板的提交消息输入框旁边,单击 `AI Commit` 图标按钮。点击后,扩展将生成 Commit 信息(如果提供了额外上下文,将会考虑在内)并填充到输入框中。
6. 审核生成的 Commit 信息,如果满意,请提交更改。

> **Note**\
> 如果超过最大 token 长度请分批将代码添加到暂存区。
Expand Down
26 changes: 22 additions & 4 deletions src/generate-commit-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ import { ProgressHandler } from './utils';
* Generates a chat completion prompt for the commit message based on the provided diff.
*
* @param {string} diff - The diff string representing changes to be committed.
* @param {string} additionalContext - Additional context for the changes.
* @returns {Promise<Array<{ role: string, content: string }>>} - A promise that resolves to an array of messages for the chat completion.
*/
const generateCommitMessageChatCompletionPrompt = async (diff: string) => {
const generateCommitMessageChatCompletionPrompt = async (diff: string, additionalContext?: string) => {
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt();
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];

if (additionalContext) {
chatContextAsCompletionRequest.push({
role: 'user',
content: `Additional context for the changes:\n${additionalContext}`
});
}

chatContextAsCompletionRequest.push({
role: 'user',
content: diff
Expand Down Expand Up @@ -84,10 +92,20 @@ export async function generateCommitMsg(arg) {
throw new Error('Unable to find the SCM input box');
}

progress.report({ message: 'Analyzing changes...' });
const messages = await generateCommitMessageChatCompletionPrompt(diff);
const additionalContext = scmInputBox.value.trim();

progress.report({
message: additionalContext
? 'Analyzing changes with additional context...'
: 'Analyzing changes...'
});
const messages = await generateCommitMessageChatCompletionPrompt(diff, additionalContext);

progress.report({ message: 'Generating commit message...' });
progress.report({
message: additionalContext
? 'Generating commit message with additional context...'
: 'Generating commit message...'
});
try {
const commitMessage = await ChatGPTAPI(
messages as ChatCompletionMessageParam[]
Expand Down
4 changes: 4 additions & 0 deletions src/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ You will act as a git commit message generator. When receiving a git diff, you w
4. NO questions or comments
5. NO formatting instructions or metadata
## Additional Context
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.
## Examples
INPUT:
Expand Down

0 comments on commit fce0d6d

Please sign in to comment.