@elsikora/commitizen-plugin-commitlint-ai
Version:
AI-powered Commitizen adapter with Commitlint integration
50 lines (46 loc) • 2.13 kB
JavaScript
var numeric_constant = require('../../domain/constant/numeric.constant.js');
/**
* Use case for generating commit messages
*/
class GenerateCommitMessageUseCase {
LLM_SERVICES;
constructor(llmServices) {
this.LLM_SERVICES = llmServices;
}
/**
* Execute the commit message generation
* @param {ILlmPromptContext} context - The context for generating the commit message
* @param {LLMConfiguration} configuration - The LLM configuration
* @param {(attempt: number, maxRetries: number, error: Error) => void} onRetry - Callback function called on retry attempts
* @returns {Promise<CommitMessage>} Promise resolving to the generated commit message
*/
async execute(context, configuration, onRetry) {
const service = this.LLM_SERVICES.find((s) => s.supports(configuration));
if (!service) {
throw new Error(`No LLM service found for provider: ${configuration.getProvider()}`);
}
const maxRetries = configuration.getMaxRetries();
// Try to generate with retries
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await service.generateCommitMessage(context, configuration);
}
catch (error) {
if (attempt === maxRetries) {
throw new Error(`Failed to generate commit message after ${maxRetries} attempts: ${error instanceof Error ? error.message : String(error)}`);
}
// Notify about retry
if (onRetry) {
onRetry(attempt, maxRetries, error);
}
// Wait before retrying
await new Promise((resolve) => setTimeout(resolve, numeric_constant.RETRY_DELAY_MS));
}
}
// This should never be reached due to the throw in the loop
throw new Error(`Failed to generate commit message after ${maxRetries} attempts`);
}
}
exports.GenerateCommitMessageUseCase = GenerateCommitMessageUseCase;
//# sourceMappingURL=generate-commit-message.use-case.js.map
;