JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q84208F
  • License MIT

AI-powered pre-commit code review tool

Package Exports

  • ai-commit-guard
  • ai-commit-guard/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ai-commit-guard) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

AI Commit Guard

๐Ÿค– AI-powered pre-commit code review tool that automatically checks your code changes before commit using OpenAI GPT-4 or Anthropic Claude.

npm version License: MIT Node.js Version

โœจ Features

  • ๐Ÿ” Smart Detection - Only reviews staged .js, .ts, .jsx, .tsx, .vue, .py files
  • ๐Ÿš€ Lightning Fast - Cached results for unchanged code
  • ๐ŸŽฏ Custom Rules - Define your own coding standards
  • ๐Ÿ”„ Multi-Provider - Works with OpenAI GPT-4 or Anthropic Claude
  • ๐Ÿ›ก๏ธ Security First - Automatically ignores sensitive files and masks secrets
  • ๐Ÿ›ก๏ธ Zero Config - Works out of the box with sensible defaults
  • โฑ๏ธ Timeout Protection - Handles network issues gracefully with commit message flags
  • ๐Ÿ“ Detailed Feedback - Get specific line-by-line suggestions

๐Ÿš€ Quick Start

1. Install Globally

npm install -g ai-commit-guard

2. Setup in Your Project

# Install husky (if not already installed)
npm install --save-dev husky
npx husky init

# Add AI Guard to pre-commit hook
echo "npx ai-commit-guard" > .husky/pre-commit
chmod +x .husky/pre-commit

3. Set Your AI API Key

Option A: OpenAI (Recommended)

export OPENAI_API_KEY="sk-your-openai-key-here"

Option B: Anthropic Claude

export CLAUDE_API_KEY="sk-ant-your-claude-key-here"
export AI_PROVIDER="claude"

4. That's It! ๐ŸŽ‰

Now AI Guard will automatically review your code on every commit:

git add .
git commit -m "feat: add user authentication"
# โ†’ AI Guard automatically reviews your changes

๐Ÿ“‹ Custom Rules & Security (Optional)

Custom Coding Rules

Create a .code-rules.md file in your project root to define custom coding standards:

# My Code Review Rules

## Magic Strings
- No string literals in code, use constants
- Event properties should use EVENT_FIELDS constant

## Functions
- Functions should not exceed 20 lines
- Use descriptive function names
- Follow single responsibility principle

## Variables
- Use camelCase for variables
- Boolean variables should start with is/has/can/should
- No single letter variables except for loops

## Error Handling
- Always use try-catch for async operations
- Log errors with appropriate context

## Examples

โŒ **Bad:**
```javascript
event['status'] = 'active';
function getData() { /* 30 lines of mixed responsibilities */ }
let x = true;

โœ… Good:

const EVENT_FIELDS = { STATUS: 'status' };
event[EVENT_FIELDS.STATUS] = 'active';

function fetchUserData() { /* single responsibility */ }
function processUserData() { /* separate concern */ }

let isUserActive = true;

### Security & Ignore Patterns
Create a `.ai-guard-ignore` file to exclude sensitive files from AI review:

```bash
# Sensitive files (automatically ignored)
*.env*
*.key
*.pem
*password*
*secret*
*token*
*api-key*

# Custom patterns
config/database.js
secrets/
.env.*
*-secret.json
api-keys.txt

# Large files
dist/*
build/*
*.min.js
package-lock.json

๐Ÿ”’ Built-in Security Features:

  • Automatically detects and ignores sensitive files
  • Masks API keys and secrets in code diffs
  • Filters out files containing password, secret, token, key
  • Skips large files (>50KB by default)

๐Ÿ› ๏ธ Usage

AI Guard runs automatically when you commit:

git add src/
git commit -m "refactor: improve error handling"
# ๐Ÿ” Checking staged files...
# ๐Ÿ“ Reviewing 3 files...
# ๐Ÿค– Sending to AI for review...
# โœ… Code review passed!

Manual Mode

Run review without committing:

npx ai-commit-guard

โฑ๏ธ Timeout Protection

๐Ÿ” Checking staged files...
๐Ÿ“ Reviewing 2 files...
๐Ÿค– Sending to AI for review (timeout: 30s)...
โฑ๏ธ AI review timed out after 30 seconds
๐Ÿ“ Added [AI-REVIEW-FAILED: Timeout] to commit message

๐Ÿ”’ Security Protection

๐Ÿ” Checking staged files...
๐Ÿ”’ Ignored 2 sensitive/excluded files
๐Ÿ“ Reviewing 1 files...
โœ… Code review passed!

๐Ÿ“Š Example Output

โœ… Passing Review

๐Ÿ” Checking staged files...
๐Ÿ“ Reviewing 2 files...
๐Ÿค– Sending to AI for review...
โœ… Code review passed!

๐Ÿ’ก Suggestions:
โ€ข Consider adding JSDoc comments for public functions
โ€ข Variable naming looks great, well done!

โŒ Failing Review

๐Ÿ” Checking staged files...
๐Ÿ“ Reviewing 3 files...
๐Ÿค– Sending to AI for review...
โŒ Code review failed!

Issues found:
โ€ข src/utils.js: Magic string 'status' on line 15
  Fix: Use EVENT_FIELDS.STATUS constant

โ€ข src/api.js: Function getUserData() is 25 lines long
  Fix: Split into fetchUser() and processUser() functions

โ€ข src/components/User.tsx: Variable 'x' is not descriptive
  Fix: Rename to 'isUserLoggedIn' or similar

โš™๏ธ Configuration

Environment Variables

Variable Description Default
OPENAI_API_KEY Your OpenAI API key -
CLAUDE_API_KEY Your Claude API key -
AI_PROVIDER AI provider: openai or claude openai
AI_GUARD_TIMEOUT Review timeout in milliseconds 30000 (30s)
AI_GUARD_MAX_FILE_SIZE Max file size for review in bytes 50000 (50KB)

Files

File Purpose
.code-rules.md Your custom coding rules
.ai-guard-ignore Files to exclude from AI review
.ai-guard-cache/ Cache directory (auto-created)

Advanced Configuration

# Custom timeout (45 seconds)
export AI_GUARD_TIMEOUT=45000

# Custom file size limit (100KB)
export AI_GUARD_MAX_FILE_SIZE=100000

# Use Claude instead of OpenAI
export AI_PROVIDER=claude
export CLAUDE_API_KEY="sk-ant-your-key"

๐ŸŽฏ Supported File Types

  • JavaScript: .js
  • TypeScript: .ts
  • React: .jsx, .tsx
  • Vue: .vue
  • Python: .py

๐Ÿ”ง Advanced Usage

Custom Ignore Patterns

# Create project-specific ignore rules
cat > .ai-guard-ignore << 'EOF'
# Database configs
config/database.js
config/secrets.json

# Generated files
dist/*
build/*
*.generated.js

# Third-party
vendor/*
libs/*
EOF

Different Rules Per Project

# Create project-specific rules
echo "# Strict React Rules\n- All components must have PropTypes" > .code-rules.md

Timeout Configuration

# Longer timeout for large projects
export AI_GUARD_TIMEOUT=60000  # 60 seconds

# Commit with custom timeout
AI_GUARD_TIMEOUT=45000 git commit -m "feat: large refactor"

Temporary Disable

# Skip AI review for this commit
git commit -m "docs: update README" --no-verify

Clear Cache

# Remove cached reviews
rm -rf .ai-guard-cache

๐Ÿท๏ธ Commit Message Flags

When AI review fails or times out, flags are automatically added to your commit messages:

Timeout Cases

feat: add user authentication

[AI-REVIEW-FAILED: Timeout]

Error Cases

fix: update validation logic

[AI-REVIEW-SKIPPED: Error]

Flag Meanings:

  • No flag = AI review completed successfully โœ…
  • [AI-REVIEW-FAILED: Timeout] = Review timed out, manual review recommended โฑ๏ธ
  • [AI-REVIEW-SKIPPED: Error] = Error occurred (no API key, network issue, etc.) โŒ

This helps your team track which commits received AI review and which need manual attention.

๐Ÿค” Why AI Commit Guard?

Problem Solution
๐Ÿ˜ฐ Inconsistent code style across team โœ… Enforces rules automatically
๐Ÿ˜ด Bugs slip through manual review โœ… AI catches common mistakes
๐Ÿ˜ฌ Junior developers need guidance โœ… Educational feedback on every commit
๐Ÿ˜ค Code review takes too long โœ… Pre-filter obvious issues
๐Ÿ˜ต Forget to follow team standards โœ… Instant feedback at commit time

๐Ÿ†š Comparison

Feature AI Commit Guard ESLint Manual Review
Custom Rules โœ… Natural language โš ๏ธ Config syntax โœ… Human judgment
Context Aware โœ… Understands logic โŒ Syntax only โœ… Full context
Security Protection โœ… Auto-masks secrets โŒ No security features โš ๏ธ Manual vigilance
Learning โœ… Teaches best practices โŒ Just flags errors โš ๏ธ Inconsistent
Speed โœ… Instant (cached) โœ… Very fast โŒ Slow
Flexibility โœ… Any rule you write โš ๏ธ Predefined rules โœ… Completely flexible
Timeout Handling โœ… Graceful degradation โœ… Always works โœ… Always works

๐Ÿ› Troubleshooting

Common Issues

"No AI API key found"

# Check if key is set
echo $OPENAI_API_KEY

# Set the key
export OPENAI_API_KEY="sk-your-key-here"

# Make it permanent (add to ~/.bashrc or ~/.zshrc)
echo 'export OPENAI_API_KEY="sk-your-key-here"' >> ~/.bashrc

"AI review timed out"

# Increase timeout for large projects
export AI_GUARD_TIMEOUT=60000  # 60 seconds

# Check network connection
curl -I https://api.openai.com

# Try with smaller changesets
git add specific-file.js  # Instead of git add .

"Sensitive files being reviewed"

# Check ignore patterns
cat .ai-guard-ignore

# Add patterns for your project
echo "config/secrets.js" >> .ai-guard-ignore
echo "*.env.*" >> .ai-guard-ignore

"Review is too slow"

# Enable caching (default: enabled)
ls -la .ai-guard-cache/

# Reduce file size limit
export AI_GUARD_MAX_FILE_SIZE=25000  # 25KB

# Stage fewer files at once
git add src/specific-file.js

"Not a git repository"

# Initialize git first
git init
git add .
git commit -m "initial commit"

"No staged files to review"

# Make sure files are staged
git status
git add .

"AI connection failed"

# Check your API key and internet connection
curl -H "Authorization: Bearer $OPENAI_API_KEY" https://api.openai.com/v1/models

Performance Tips

  • ๐Ÿƒโ€โ™‚๏ธ Enable caching: Cache is enabled by default
  • ๐Ÿ“ Stage only what you need: git add specific-file.js
  • ๐ŸŽฏ Use specific rules: More specific rules = faster reviews
  • ๐Ÿงน Clean cache periodically: rm -rf .ai-guard-cache

๐Ÿ“ˆ Roadmap

  • Support for more file types (Go, Rust, C++)
  • Integration with popular IDEs
  • Team dashboards and analytics
  • Custom AI model fine-tuning
  • Slack/Discord notifications
  • CI/CD pipeline integration

๐Ÿค Contributing

We love contributions! Here's how to help:

  1. ๐Ÿด Fork the repository
  2. ๐ŸŒฑ Create your feature branch: git checkout -b my-feature
  3. โœ… Commit your changes: git commit -m 'Add cool feature'
  4. ๐Ÿš€ Push to the branch: git push origin my-feature
  5. ๐Ÿ“ Open a Pull Request

๐Ÿ“„ License

MIT ยฉ Adem Alkan

๐Ÿ’ฌ Support & Community


Made with โค๏ธ for better code quality

Star โญ this repo if it helps you write better code!