CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-cz-git

A better customizable and git support commitizen adapter

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

ai-integration.mddocs/

AI Integration

OpenAI-powered commit message generation with customizable prompts and model selection.

Capabilities

AI Subject Generation

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 messages

AI Configuration Options

Complete 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'
};

AI Prompt Parameters

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;
}

Custom AI Question Callback

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:
    `;
  }
});

AI Model Options

Supported OpenAI models for commit message generation.

type SupportedAIModels = 
  | 'gpt-3.5-turbo'
  | 'gpt-4'
  | 'gpt-4o'
  | 'gpt-4o-mini'
  | string; // Custom model names supported

Model 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
});

AI Diff Filtering

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'
  ]
});

API Configuration

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"

AI Integration Flow

The AI integration follows this flow:

  1. Diff Analysis: Collects git diff excluding ignored files
  2. Context Building: Combines diff with configuration context
  3. Prompt Generation: Creates prompt using default or custom callback
  4. API Request: Sends request to OpenAI with configured model
  5. Response Processing: Parses and validates AI response
  6. User Selection: If multiple options, presents selection prompt
  7. Confirmation: Shows generated message for user confirmation

Error Handling

AI integration includes comprehensive error handling:

  • Network Errors: Graceful fallback to manual prompts
  • API Errors: User-friendly error messages with suggestions
  • Token Limits: Automatic diff truncation for large changes
  • Invalid Responses: Validation and retry logic for malformed AI output

The AI system is designed to enhance the commit process without breaking the workflow when issues occur.

docs

ai-integration.md

configuration-loading.md

configuration.md

enhanced-prompts.md

index.md

message-generation.md

prompter.md

types.md

tile.json