README
ΒΆ
kai
kai is a command-line interface (CLI) tool that leverages Artificial Intelligence to automatically generate Git commit messages, pull request content, and even reorganize messy commit histories. It aims to streamline your development workflow by providing concise, relevant, and optionally Conventional Commit-formatted messages, allowing you to focus more on coding and less on crafting perfect commit messages or untangling commit history.
β¨ Features
- AI-Powered Commit Message Generation (
gen): Automatically creates commit messages by analyzing the diff of your staged Git changes. - AI-Powered Pull Request Content Generation (
prgen): Generates titles and descriptions for pull requests by analyzing commits and diffs between branches. - AI-Powered Commit History Reorganization (
prprepare): Analyzes your branch's full diff and uses AI to suggest and apply a clean, logical sequence of atomic commits, making code reviews easier. - Intelligent Fixup (
absorb): Automatically createsfixup!commits for staged changes, targeting the most appropriate original commits. - Conventional Commits Support: Generates commit messages adhering to the Conventional Commits specification (e.g.,
feat(scope): add new feature). - Interactive Workflow: Provides a selection of generated messages and allows interactive editing before committing. You can also quickly select an option by typing its corresponding number.
- Intelligent Provider Selection: Automatically detects and prioritizes available LLM providers based on configured API keys, falling back to others if a preferred one isn't configured.
- Automatic Staging: If no files are staged,
kaican automatically stage all changes in tracked files before generating a message. - Contextual Commit History: Can include previous commit messages for similar files (including those in parent directories) in the prompt, helping the AI generate more consistent and contextually relevant messages.
- Multiple LLM Providers: Supports various Large Language Model providers for flexibility:
- Go-powered: Built with Go, offering a single, fast binary.
π Installation
kai is a Go application. You'll need Go 1.23+ installed on your system.
Using go install
The easiest way to install kai is using go install:
go install github.com/zbiljic/kai@latest
Ensure that your GOBIN is in your system's PATH (e.g., export PATH=$PATH:$(go env GOBIN)) to run kai directly from your terminal.
Building from source
Alternatively, you can clone the repository and build kai yourself. This method requires make.
git clone https://github.com/zbiljic/kai.git
cd kai
make install
The make install command will compile the kai executable and place it in your GOBIN directory.
π‘ Usage
kai is designed to be used within a Git repository.
Generate Commit Message (gen)
This command generates Git commit messages based on your staged changes.
-
Stage your changes (or let
kaido it): Before runningkai, you usually stage your changes:git add .However, if you forget to stage,
kaiwill automatically stage all changes in tracked files by default (similar togit add .) before attempting to generate a message.To explicitly stage all changes and then generate a message, use the
--allor-aflag:kai gen --all # or kai gen -a -
Generate a commit message: Simply run
kaiorkai genin your repository:kai # or kai genkaiwill analyze your staged changes, automatically detect an available LLM provider, generate potential commit messages, and present them in an interactive prompt:β Pick a commit message to use: (Ctrl+c to exit) β [1] feat: add new feature (e to edit) β [2] fix: resolve bug β [3] chore: update dependenciesYou can navigate through the suggestions using arrow keys. Press
Enterto select a message. It is also possible to quickly pick a message by typing the corresponding number (e.g.,1,2,3) instead of using arrow keys.Press
eto edit the currently selected message interactively. If you choose to edit,kaiwill guide you through modifying the type, scope, and message body, especially useful for adhering to Conventional Commits. -
Commit: Once you select or confirm a message,
kaiwill automatically commit your staged changes with the chosen message.
gen Options
-
Specify LLM Provider: Use the
--provideror-pflag to explicitly choose your desired LLM provider, overriding the automatic detection.kai gen --provider openai kai gen -p googleaiAvailable providers:
phind(default fallback),openai,claude,googleai,openrouter,groq,deepseek. -
Specify Model: Use the
--modelor-mflag to explicitly choose a specific model for the selected provider.kai gen --provider openai --model gpt-4 kai gen -p googleai -m gemini-2.5-pro -
Specify Commit Message Type: Use the
--typeor-tflag to set the desired commit message format.kai gen --type simple kai gen -t conventionalAvailable types:
conventional(default),simple.conventional: Generates messages adhering to the Conventional Commits specification (e.g.,type(scope): message).simple: Generates plain messages likemessage.
-
Include Previous Commit History: By default,
kaiincludes previous commit messages for relevant files (and their parent directories if no direct file history exists) to provide context to the AI. To disable this, use the--history=falseflag:kai gen --history=false -
Number of Suggestions: Use the
--countor-nflag to specify how many commit message suggestions to generate (default is 2).kai gen --count 5 -
Non-interactive Mode: Use the
--yesor-yflag to automatically use the first generated commit message without an interactive prompt.kai gen --yes
Generate Pull Request Content (prgen)
The prgen command helps you automatically generate a title and description for your pull request (PR) or merge request (MR) by analyzing the commits and changes between your current branch and a specified base branch.
kai prgen [options]
# or
kai pr [options]
To use it:
- Switch to your feature branch: Ensure you are on the branch for which you want to create a PR.
git checkout feature/my-new-feature - Run
kai prgen:kai prgenkaiwill then:- Compare your current branch with the default base branch (
main). - Optionally ask for additional context about your changes.
- Attempt to find and use a PR template in your repository.
- Generate a PR title and description based on the commits and diff.
- Display the generated content directly in your terminal.
- Compare your current branch with the default base branch (
prgen Options
-
Specify LLM Provider: Use the
--provideror-pflag to explicitly choose your desired LLM provider, overriding the automatic detection.kai prgen --provider openai kai prgen -p googleaiAvailable providers:
phind(default fallback),openai,claude,googleai,openrouter,groq,deepseek. -
Specify Model: Use the
--modelor-mflag to explicitly choose a specific model for the selected provider.kai prgen --provider openai --model gpt-4-turbo kai prgen -p googleai -m gemini-pro -
Specify Base Branch: Use the
--baseor-bflag to compare against a branch other thanmain.kai prgen --base develop -
Maximum Diff Size: Use
--max-diffto set a limit (in characters) on the size of the code diff sent to the LLM. Larger diffs consume more tokens and might be truncated by some models.kai prgen --max-diff 5000The default is 10000 characters.
-
No Additional Context Prompt: Use
--no-contextto skip the interactive prompt for additional business or feature context. The AI will rely solely on the commit messages and code diff.kai prgen --no-context
Reorganize Commit History (prprepare)
The prprepare command uses AI to analyze your current branch's entire diff against a base branch and suggest a reorganized, cleaner commit history. It helps you transform messy, large, or poorly structured commits into logical, atomic units, which significantly improves code review readability and maintainability.
kai prprepare [options]
# or
kai prp [options]
To use it:
-
Switch to your feature branch: Ensure you are on the branch whose history you want to reorganize.
git checkout feature/my-complex-feature -
Run
kai prprepare:kai prpreparekaiwill then:- Analyze all changes between your current branch and the default base branch (
main). - Parse the diff into individual "hunks" of changes.
- Use an LLM to generate a proposed commit plan, detailing new commit messages and which specific hunks belong to each new commit.
- Display the proposed plan for your review.
- If confirmed, it will reset your branch to the base branch and then apply the new commits sequentially, rebuilding your history.
Important: This command rewrites your branch's history. It's recommended to have a backup or ensure your work is pushed before running it, especially without
--dry-run.kaiwill create a temporary backup branch by default before applying changes if--debugor--auto-applyis not used. - Analyze all changes between your current branch and the default base branch (
prprepare Options
-
Specify LLM Provider: Use the
--provideror-pflag to explicitly choose your desired LLM provider, overriding the automatic detection.kai prprepare --provider claude kai prprepare -p deepseekAvailable providers:
phind(default fallback),openai,claude,googleai,openrouter,groq,deepseek. -
Specify Model: Use the
--modelor-mflag to explicitly choose a specific model for the selected provider.kai prprepare --provider claude --model claude-3-opus-20240229 kai prprepare -p groq -m mixtral-8x7b-32768 -
Specify Base Branch: Use the
--baseor-bflag to compare against a branch other thanmain.kai prprepare --base feature/my-base -
Maximum Diff Size: Use
--max-diffto set a limit (in characters) on the total size of the code diff sent to the LLM for analysis. This helps manage token usage for very large diffs.kai prprepare --max-diff 20000The default is 10000 characters.
-
Automatically Apply: Use
--auto-applyto skip the confirmation prompt and immediately apply the generated commit reorganization plan. Use with caution!kai prprepare --auto-apply -
Dry Run: Use
--dry-runor-nto simulate the reorganization without making any actual changes to your repository. It will show the proposed plan and whatgitcommands would be executed.kai prprepare --dry-run -
Debug Mode: Use
--debugto enable detailed logging and write each proposed commit's patch file to.kai/prprepare/in your repository. This implies--dry-runif used alone. Useful for inspecting the AI's hunk grouping and patch creation.kai prprepare --debugWhen debug is enabled, you can then manually apply the patches (e.g.,
git apply --check --cached .kai/prprepare/001.patch).
Absorb Staged Changes (absorb)
The absorb command helps you automatically create fixup! commits for staged changes, targeting the original commits that introduced those changes. This is useful for splitting out and organizing your work and for making small corrections to previous commits before a final rebase.
kai absorb [options]
After running kai absorb, you can execute git rebase -i --autosquash to automatically squash the fixup! commits into their respective targets.
- Automatically Rebase: Use
--and-rebaseor-rto automatically rungit rebase --autosquashafter creating fixups.kai absorb --and-rebase - Dry Run: Use
--dry-runor-nto see what changesabsorbwould make without actually performing them.kai absorb --dry-run - Backup Branch: When using
--and-rebase, use--backupor-bto create a backup branch (e.g.,backup/your-branch-HH-MM-SS) before the rebase operation. This helps in recovery if the rebase fails or does not produce expected results.kaiwill check if an up-to-date backup exists and reuse it if possible.kai absorb --and-rebase --backup - Stage All Changes: Use
--allor-ato automatically stage all changes in tracked files before analyzing for fixups.kai absorb --all - Maximum History Lookback: Use
--max-historyto specify how many commits backabsorbshould look when trying to find the original commit for a modified line (default is 20).kai absorb --max-history 50
βοΈ Configuration
kai relies on environment variables for API keys to access LLM providers.
Automatic Provider Selection: kai will automatically detect and prioritize LLM providers based on the presence of their respective API keys in your environment variables. The preferred order of detection (most preferred first) is:
- Google AI: Requires
GEMINI_API_KEY - Groq: Requires
GROQ_API_KEY - OpenRouter: Requires
OPENROUTER_API_KEY - OpenAI: Requires
OPENAI_API_KEY - Anthropic Claude: Requires
ANTHROPIC_API_KEY - DeepSeek: Requires
DEEPSEEK_API_KEY - Phind: Does not require an API key (used as a last resort if others aren't configured).
To configure a provider, set the corresponding environment variable:
-
Google AI:
export GEMINI_API_KEY="your_google_ai_api_key" -
Groq:
export GROQ_API_KEY="your_groq_api_key" -
OpenRouter:
export OPENROUTER_API_KEY="your_openrouter_api_key"With OpenRouter, it is also possible to set these for custom attribution in their API logs:
export OPENROUTER_HTTP_REFERER="https://github.com/zbiljic/kai" export OPENROUTER_X_TITLE="kai" -
OpenAI:
export OPENAI_API_KEY="your_openai_api_key" -
Anthropic Claude:
export ANTHROPIC_API_KEY="your_anthropic_api_key" -
DeepSeek:
export DEEPSEEK_API_KEY="your_deepseek_api_key"
π€ Contributing
Contributions are welcome! If you find a bug, have a feature request, or want to improve the codebase, please feel free to open an issue or submit a pull request.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgements
- Inspired by other AI-powered commit tools and the need for a simple Go solution that allows easy message modification and robust LLM integration.
- Uses go-clack for interactive prompts.
Similar Projects
Here are some other similar projects that you might find useful:
Documentation
ΒΆ
There is no documentation for this package.