Skip to content

Commit

Permalink
✨ feat(version): update project version to 0.0.9
Browse files Browse the repository at this point in the history
- update version in package.json and package-lock.json

♻️ refactor(generator): improve commit message generation

- format function parameters for readability
- enhance error handling and progress reporting
- improve code structure and readability
- update error messages for clarity

💄 style(utils): adjust progress notification title

- capitalize extension name in progress notification
  • Loading branch information
Sitoi committed Nov 27, 2024
1 parent fce0d6d commit 89f7e41
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 72 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ai-commit",
"displayName": "AI Commit",
"description": "Use Azure/OpenAI API to review Git changes, generate conventional commit messages that meet the conventions, simplify the commit process, and keep the commit conventions consistent.",
"version": "0.0.8",
"version": "0.0.9",
"engines": {
"node": ">=16",
"vscode": "^1.77.0"
Expand Down
141 changes: 72 additions & 69 deletions src/generate-commit-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { ProgressHandler } from './utils';
* @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, additionalContext?: string) => {
const generateCommitMessageChatCompletionPrompt = async (
diff: string,
additionalContext?: string
) => {
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt();
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];

Expand Down Expand Up @@ -64,82 +67,82 @@ export async function getRepo(arg) {
* @returns {Promise<void>} - A promise that resolves when the commit message has been generated and set in the SCM input box.
*/
export async function generateCommitMsg(arg) {
return ProgressHandler.withProgress(
'Generating commit message...',
async (progress) => {
try {
const configManager = ConfigurationManager.getInstance();
const repo = await getRepo(arg);
const apiKey = configManager.getConfig<string>(ConfigKeys.OPENAI_API_KEY);
return ProgressHandler.withProgress('', async (progress) => {
try {
const configManager = ConfigurationManager.getInstance();
const repo = await getRepo(arg);
const apiKey = configManager.getConfig<string>(ConfigKeys.OPENAI_API_KEY);

if (!apiKey) {
throw new Error('OpenAI API Key not configured');
}

if (!apiKey) {
throw new Error('OpenAI API Key not configured');
}
progress.report({ message: 'Getting staged changes...' });
const { diff, error } = await getDiffStaged(repo);

progress.report({ message: 'Getting staged changes...' });
const { diff, error } = await getDiffStaged(repo);
if (error) {
throw new Error(`Failed to get staged changes: ${error}`);
}

if (error) {
throw new Error(`Failed to get staged changes: ${error}`);
}
if (!diff || diff === 'No changes staged.') {
throw new Error('No changes staged for commit');
}

if (!diff || diff === 'No changes staged.') {
throw new Error('No changes staged for commit');
}
const scmInputBox = repo.inputBox;
if (!scmInputBox) {
throw new Error('Unable to find the SCM input box');
}

const scmInputBox = repo.inputBox;
if (!scmInputBox) {
throw new Error('Unable to find the SCM input box');
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: additionalContext
? 'Generating commit message with additional context...'
: 'Generating commit message...'
});
try {
const commitMessage = await ChatGPTAPI(
messages as ChatCompletionMessageParam[]
);
if (commitMessage) {
scmInputBox.value = commitMessage;
} else {
throw new Error('Failed to generate commit message');
}

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: additionalContext
? 'Generating commit message with additional context...'
: 'Generating commit message...'
});
try {
const commitMessage = await ChatGPTAPI(
messages as ChatCompletionMessageParam[]
);
if (commitMessage) {
scmInputBox.value = commitMessage;
} else {
throw new Error('Failed to generate commit message');
}
} catch (err) {
let errorMessage = 'An unexpected error occurred';

if (err.response?.status) {
switch (err.response.status) {
case 401:
errorMessage = 'Invalid API key or unauthorized access';
break;
case 429:
errorMessage = 'Rate limit exceeded. Please try again later';
break;
case 500:
errorMessage = 'OpenAI server error. Please try again later';
break;
case 503:
errorMessage = 'OpenAI service is temporarily unavailable';
break;
}
} catch (err) {
let errorMessage = 'An unexpected error occurred';

if (err.response?.status) {
switch (err.response.status) {
case 401:
errorMessage = 'Invalid API key or unauthorized access';
break;
case 429:
errorMessage = 'Rate limit exceeded. Please try again later';
break;
case 500:
errorMessage = 'OpenAI server error. Please try again later';
break;
case 503:
errorMessage = 'OpenAI service is temporarily unavailable';
break;
}

throw new Error(errorMessage);
}
} catch (error) {
throw error;

throw new Error(errorMessage);
}
} catch (error) {
throw error;
}
);
});
}
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ProgressHandler {
return vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: `[ai-commit] ${title}`,
title: `[AI Commit] ${title}`,
cancellable: true
},
task
Expand Down

0 comments on commit 89f7e41

Please sign in to comment.