OpenAI-powered commit message generation with customizable prompts and model selection.
Function for generating commit subject suggestions from git diff using OpenAI.
/**
* Generate AI-powered commit subject suggestions
* @param answers - Current prompt answers including type selection
* @param options - Configuration including AI settings and model
* @returns Promise resolving to array of suggested commit subjects
*/
function generateAISubjects(
answers: Answers,
options: CommitizenGitOptions
): Promise<string[]>;Usage Examples:
import { prompter } from "cz-git";
// AI integration is configured through options, not called directly
const options = {
useAI: true,
aiModel: 'gpt-4',
aiNumber: 3,
openAIToken: 'sk-...',
defaultType: 'feat',
maxSubjectLength: 50
};
// AI prompts are triggered automatically when useAI is enabled
prompter(cz, commit, './config-with-ai.js');
// AI will analyze git diff and suggest appropriate commit messagesComplete set of AI-related configuration options.
interface AIConfigurationOptions {
/** Enable OpenAI integration */
useAI?: boolean;
/** OpenAI model to use */
aiModel?: string;
/** Number of suggestions to generate (>1 enables selection mode) */
aiNumber?: number;
/** Files to ignore in diff analysis */
aiDiffIgnore?: string[];
/** OpenAI API token */
openAIToken?: string;
/** OpenAI API endpoint */
apiEndpoint?: string;
/** HTTP proxy for API requests */
apiProxy?: string;
/** CLI-set API model override */
apiModel?: string;
/** Custom AI question callback */
aiQuestionCB?: (aiParam: GenerateAIPromptType) => string;
}Default Values:
const aiDefaults = {
useAI: false,
aiModel: 'gpt-4o-mini',
aiNumber: 1,
aiDiffIgnore: ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'],
apiEndpoint: 'https://api.openai.com/v1',
openAIToken: '',
apiProxy: '',
apiModel: 'gpt-4o-mini'
};Interface for parameters passed to AI question generation.
interface GenerateAIPromptType {
/** Pre-selected commit type */
type?: string;
/** Default scope(s) for the change */
defaultScope?: string | string[];
/** Maximum length constraint for subject */
maxSubjectLength?: number;
/** Subject case preference */
upperCaseSubject?: boolean | null;
/** Git diff content for analysis */
diff?: string;
}Function interface for customizing AI prompts sent to OpenAI.
/**
* Custom callback for generating AI prompts
* @param aiParam - Parameters containing context and constraints
* @returns Custom prompt string to send to OpenAI
*/
type AIQuestionCallback = (aiParam: GenerateAIPromptType) => string;Usage Examples:
import { definePrompt } from "cz-git";
export default definePrompt({
useAI: true,
aiQuestionCB: ({ type, defaultScope, maxSubjectLength, diff }) => {
return `
Analyze the following git diff and generate a commit message:
Type: ${type || 'auto-detect'}
Scope: ${defaultScope || 'auto-detect'}
Max subject length: ${maxSubjectLength || 50}
Rules:
- Follow Conventional Commits specification
- Use imperative mood
- Be concise and descriptive
- Focus on what changed and why
Git diff:
${diff}
Generate a commit subject line:
`;
}
});Supported OpenAI models for commit message generation.
type SupportedAIModels =
| 'gpt-3.5-turbo'
| 'gpt-4'
| 'gpt-4o'
| 'gpt-4o-mini'
| string; // Custom model names supportedModel Selection Examples:
// Use GPT-4 for better quality
export default definePrompt({
useAI: true,
aiModel: 'gpt-4',
aiNumber: 1
});
// Generate multiple options with GPT-4o-mini for speed
export default definePrompt({
useAI: true,
aiModel: 'gpt-4o-mini',
aiNumber: 5 // User can select from 5 AI-generated options
});Configuration for excluding files from AI analysis to improve relevance and reduce token usage.
interface AIDiffOptions {
/** Files/patterns to ignore in AI diff analysis */
aiDiffIgnore?: string[];
}Usage Examples:
export default definePrompt({
useAI: true,
aiDiffIgnore: [
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
'*.map',
'dist/**',
'build/**',
'node_modules/**',
'*.log'
]
});Configuration for OpenAI API access including endpoints, proxies, and authentication.
interface APIConfiguration {
/** OpenAI API token (can be set via CLI or config) */
openAIToken?: string;
/** Custom API endpoint (default: https://api.openai.com/v1) */
apiEndpoint?: string;
/** HTTP/SOCKS proxy for API requests */
apiProxy?: string;
/** CLI override for AI model */
apiModel?: string;
}CLI Configuration Examples:
# Set API key globally
npx czg --api-key=sk-xxxxx
# Use custom model
npx czg --api-model=gpt-4
# Configure proxy
npx czg --api-proxy="http://127.0.0.1:1080"
# Custom endpoint for Azure OpenAI
npx czg --api-endpoint="https://your-resource.openai.azure.com/v1"The AI integration follows this flow:
AI integration includes comprehensive error handling:
The AI system is designed to enhance the commit process without breaking the workflow when issues occur.