|
| 1 | +# AGENTS.md - AI Agent Guidelines for codecompanion-gitcommit.nvim |
| 2 | + |
| 3 | +> Guidelines for AI agents (Claude, GPT, Copilot, etc.) working with this codebase. |
| 4 | +
|
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**codecompanion-gitcommit.nvim** is a Neovim plugin extension for [CodeCompanion](https://github.com/olimorris/codecompanion.nvim) that provides: |
| 8 | +- AI-powered Git commit message generation following Conventional Commits |
| 9 | +- Comprehensive Git workflow tools (`@{git_read}`, `@{git_edit}`, `@{git_bot}`) |
| 10 | +- AI-powered release notes generation |
| 11 | +- Multi-language support for commit messages |
| 12 | +- Smart buffer integration for gitcommit filetype |
| 13 | + |
| 14 | +### Compatibility |
| 15 | +- Supports CodeCompanion **v17.x** and **v18.0+** (compatibility handled automatically) |
| 16 | +- Lua 5.1+ / LuaJIT |
| 17 | +- Neovim 0.9+ |
| 18 | +- Multi-platform (Linux, macOS, WSL, Native Windows) |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Architecture |
| 23 | + |
| 24 | +``` |
| 25 | +codecompanion-gitcommit.nvim/ |
| 26 | +├── lua/codecompanion/_extensions/gitcommit/ |
| 27 | +│ ├── init.lua # Main entry point, extension setup, exports |
| 28 | +│ ├── config.lua # Default configuration options |
| 29 | +│ ├── git.lua # Core Git operations (diff, commit, repository detection) |
| 30 | +│ ├── generator.lua # LLM-based commit message generation |
| 31 | +│ ├── buffer.lua # Gitcommit buffer integration & auto-generation |
| 32 | +│ ├── ui.lua # Floating window UI for commit message display |
| 33 | +│ ├── langs.lua # Multi-language support for commit messages |
| 34 | +│ ├── types.lua # Type definitions (LuaLS annotations) |
| 35 | +│ ├── prompts/ |
| 36 | +│ │ └── release_notes.lua # Prompts for AI release notes generation |
| 37 | +│ └── tools/ |
| 38 | +│ ├── git.lua # GitTool class - low-level git operations |
| 39 | +│ ├── git_read.lua # Read-only git operations tool schema |
| 40 | +│ ├── git_edit.lua # Write-access git operations tool schema |
| 41 | +│ ├── ai_release_notes.lua # AI-powered release notes tool |
| 42 | +│ └── validation.lua # Parameter validation utilities |
| 43 | +├── doc/ |
| 44 | +│ └── codecompanion-gitcommit.txt # Vim help documentation |
| 45 | +├── scripts/ |
| 46 | +│ └── download_codecompanion.ps1 # Development script |
| 47 | +└── .github/workflows/ |
| 48 | + └── stylua-check.yml # CI for code formatting |
| 49 | +``` |
| 50 | + |
| 51 | +### Module Dependency Graph |
| 52 | + |
| 53 | +``` |
| 54 | +init.lua (entry point) |
| 55 | + ├── config.lua |
| 56 | + ├── git.lua ──────────────────┐ |
| 57 | + ├── generator.lua │ |
| 58 | + ├── buffer.lua ───────────────┼── git.lua (core) |
| 59 | + ├── ui.lua │ |
| 60 | + ├── langs.lua │ |
| 61 | + └── tools/ │ |
| 62 | + ├── git.lua ──────────────┘ |
| 63 | + ├── git_read.lua ─────────┬── tools/git.lua |
| 64 | + ├── git_edit.lua ─────────┤ |
| 65 | + ├── ai_release_notes.lua ─┴── prompts/release_notes.lua |
| 66 | + └── validation.lua |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Key Components |
| 72 | + |
| 73 | +### 1. Extension Entry Point (`init.lua`) |
| 74 | + |
| 75 | +The main module that: |
| 76 | +- Sets up the extension with CodeCompanion |
| 77 | +- Registers tools (`git_read`, `git_edit`, `ai_release_notes`) and tool groups (`git_bot`) |
| 78 | +- Creates Vim commands (`:CodeCompanionGitCommit`, `:CCGitCommit`) |
| 79 | +- Adds slash commands (`/gitcommit`) |
| 80 | +- Exposes programmatic API via `exports` |
| 81 | + |
| 82 | +**Version Compatibility Pattern:** |
| 83 | +```lua |
| 84 | +-- v18+ uses interactions, v17.x uses strategies |
| 85 | +if codecompanion_config.interactions and codecompanion_config.interactions.chat then |
| 86 | + return codecompanion_config.interactions.chat |
| 87 | +elseif codecompanion_config.strategies and codecompanion_config.strategies.chat then |
| 88 | + return codecompanion_config.strategies.chat |
| 89 | +end |
| 90 | +``` |
| 91 | + |
| 92 | +### 2. Git Core Module (`git.lua`) |
| 93 | + |
| 94 | +Handles: |
| 95 | +- Repository detection (`is_repository()`) |
| 96 | +- Diff retrieval with file filtering (`get_staged_diff()`, `get_contextual_diff()`) |
| 97 | +- Amend detection (`is_amending()`) |
| 98 | +- Commit history retrieval (`get_commit_history()`) |
| 99 | +- Glob pattern matching for file exclusion |
| 100 | + |
| 101 | +**Important:** This module maintains its own `config` state set via `Git.setup()`. |
| 102 | + |
| 103 | +### 3. Generator (`generator.lua`) |
| 104 | + |
| 105 | +LLM integration for commit message generation: |
| 106 | +- Supports both HTTP and ACP (Anthropic Claude Protocol) adapters |
| 107 | +- Handles streaming responses |
| 108 | +- Cleans markdown code blocks from LLM output |
| 109 | +- Creates structured prompts with commit history context |
| 110 | + |
| 111 | +### 4. Tools System (`tools/`) |
| 112 | + |
| 113 | +CodeCompanion tool implementations following the tool schema pattern: |
| 114 | + |
| 115 | +| Tool | File | Purpose | |
| 116 | +|------|------|---------| |
| 117 | +| `git_read` | `git_read.lua` | 16 read-only operations (status, log, diff, etc.) | |
| 118 | +| `git_edit` | `git_edit.lua` | 17 write operations (stage, commit, push, etc.) | |
| 119 | +| `ai_release_notes` | `ai_release_notes.lua` | AI-powered release notes from commit history | |
| 120 | + |
| 121 | +**Tool Schema Structure:** |
| 122 | +```lua |
| 123 | +Tool.schema = { |
| 124 | + type = "function", |
| 125 | + ["function"] = { |
| 126 | + name = "tool_name", |
| 127 | + parameters = { ... }, |
| 128 | + strict = true, -- Enforce strict parameter validation |
| 129 | + }, |
| 130 | +} |
| 131 | +Tool.system_prompt = [[...]] -- LLM context |
| 132 | +Tool.cmds = { function(self, args) ... end } -- Execution |
| 133 | +Tool.handlers = { setup, on_exit } |
| 134 | +Tool.output = { prompt, success, error, rejected } |
| 135 | +Tool.opts = { require_approval_before, requires_approval } -- v18/v17 compat |
| 136 | +``` |
| 137 | + |
| 138 | +### 5. Validation (`tools/validation.lua`) |
| 139 | + |
| 140 | +Centralized parameter validation with consistent error formatting: |
| 141 | +- `require_string()`, `optional_string()` |
| 142 | +- `require_array()`, `optional_integer()`, `optional_boolean()` |
| 143 | +- `require_enum()`, `first_error()` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Coding Conventions |
| 148 | + |
| 149 | +### Style |
| 150 | +- **Formatter:** StyLua (config in `stylua.toml`) |
| 151 | +- **Line width:** 120 characters |
| 152 | +- **Indentation:** 2 spaces |
| 153 | +- **Quotes:** Double quotes for strings |
| 154 | + |
| 155 | +### Naming |
| 156 | +- Modules: `PascalCase` for classes (`GitTool`, `Generator`) |
| 157 | +- Functions: `snake_case` (`get_staged_diff`, `format_git_response`) |
| 158 | +- Private functions: Prefix with `_` (`Git._filter_diff`, `Buffer._setup_gitcommit_keymap`) |
| 159 | +- Constants: `UPPER_SNAKE_CASE` (`TOOL_NAME`, `VALID_OPERATIONS`) |
| 160 | + |
| 161 | +### Type Annotations |
| 162 | +Use LuaLS (lua-language-server) annotations: |
| 163 | +```lua |
| 164 | +---@class ClassName |
| 165 | +---@field field_name type Description |
| 166 | + |
| 167 | +---@param param_name type Description |
| 168 | +---@return type description |
| 169 | +function Module.function_name(param_name) |
| 170 | +``` |
| 171 | + |
| 172 | +### Error Handling Pattern |
| 173 | +```lua |
| 174 | +local ok, result = pcall(function() |
| 175 | + -- risky operation |
| 176 | +end) |
| 177 | +if not ok then |
| 178 | + return false, "Error: " .. tostring(result) |
| 179 | +end |
| 180 | +return result |
| 181 | +``` |
| 182 | + |
| 183 | +### Git Command Execution Pattern |
| 184 | +```lua |
| 185 | +local success, output = pcall(vim.fn.system, cmd) |
| 186 | +if not success or vim.v.shell_error ~= 0 then |
| 187 | + return false, output or "Command failed" |
| 188 | +end |
| 189 | +return true, output |
| 190 | +``` |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +## Version Compatibility |
| 195 | + |
| 196 | +The codebase maintains compatibility with CodeCompanion v17.x and v18+: |
| 197 | + |
| 198 | +| Feature | v17.x | v18+ | |
| 199 | +|---------|-------|------| |
| 200 | +| Chat config | `strategies.chat` | `interactions.chat` | |
| 201 | +| Approval | `requires_approval` | `require_approval_before` | |
| 202 | + |
| 203 | +**Pattern for dual compatibility:** |
| 204 | +```lua |
| 205 | +Tool.opts = { |
| 206 | + -- v18+ uses require_approval_before |
| 207 | + require_approval_before = function(_self, _agent) return true end, |
| 208 | + -- COMPAT(v17): Remove when dropping v17 support |
| 209 | + requires_approval = function(_self, _agent) return true end, |
| 210 | +} |
| 211 | +``` |
| 212 | + |
| 213 | +--- |
| 214 | + |
| 215 | +## Adding New Features |
| 216 | + |
| 217 | +### Adding a New Git Read Operation |
| 218 | + |
| 219 | +1. Add operation to `VALID_OPERATIONS` in `git_read.lua` |
| 220 | +2. Add to schema `enum` array |
| 221 | +3. Add to `system_prompt` documentation table |
| 222 | +4. Implement handler in `GitTool` (`tools/git.lua`) |
| 223 | +5. Add case in `GitRead.cmds` function |
| 224 | +6. Add to help text |
| 225 | + |
| 226 | +### Adding a New Git Edit Operation |
| 227 | + |
| 228 | +Same pattern as read operations, but in `git_edit.lua`. Remember: |
| 229 | +- Edit operations require approval (`require_approval_before = true`) |
| 230 | +- Add appropriate parameter validation |
| 231 | +- Handle async operations if needed (see `push_async`) |
| 232 | + |
| 233 | +### Adding a New Tool |
| 234 | + |
| 235 | +1. Create `tools/new_tool.lua` following existing pattern |
| 236 | +2. Register in `init.lua` → `setup_tools()` |
| 237 | +3. Add schema, system_prompt, cmds, handlers, output, opts |
| 238 | +4. Add to tool group if needed (`git_bot`) |
| 239 | + |
| 240 | +--- |
| 241 | + |
| 242 | +## Testing & Development |
| 243 | + |
| 244 | +### Running Style Checks |
| 245 | +```bash |
| 246 | +stylua --check . # Check formatting (used in CI) |
| 247 | +stylua . # Auto-fix formatting |
| 248 | +``` |
| 249 | + |
| 250 | +### Available Make Commands |
| 251 | +```bash |
| 252 | +make doc # Download latest CodeCompanion documentation |
| 253 | +``` |
| 254 | + |
| 255 | +### Development Setup |
| 256 | +The extension must be loaded through CodeCompanion: |
| 257 | +```lua |
| 258 | +require("codecompanion").setup({ |
| 259 | + extensions = { |
| 260 | + gitcommit = { |
| 261 | + callback = "codecompanion._extensions.gitcommit", |
| 262 | + opts = { ... } |
| 263 | + } |
| 264 | + } |
| 265 | +}) |
| 266 | +``` |
| 267 | + |
| 268 | +### Debugging Tips |
| 269 | +- Use `vim.notify()` for user-facing messages |
| 270 | +- Use `vim.inspect()` for debugging complex data structures |
| 271 | +- Check `vim.v.shell_error` after shell commands |
| 272 | +- Use `pcall()` to catch Lua errors gracefully |
| 273 | + |
| 274 | +--- |
| 275 | + |
| 276 | +## Common Patterns |
| 277 | + |
| 278 | +### Formatted Tool Response |
| 279 | +```lua |
| 280 | +local function format_git_response(tool_name, success, output, empty_msg) |
| 281 | + local user_msg, llm_msg |
| 282 | + local tag = "git" .. tool_name:gsub("^%l", string.upper) .. "Tool" |
| 283 | + |
| 284 | + if success then |
| 285 | + user_msg = string.format("✓ Git %s:\n```\n%s\n```", tool_name, output) |
| 286 | + llm_msg = string.format("<%s>success:\n%s</%s>", tag, output, tag) |
| 287 | + else |
| 288 | + user_msg = string.format("✗ Git %s failed: %s", tool_name, output) |
| 289 | + llm_msg = string.format("<%s>fail: %s</%s>", tag, output, tag) |
| 290 | + end |
| 291 | + return user_msg, llm_msg |
| 292 | +end |
| 293 | +``` |
| 294 | + |
| 295 | +### Async Git Operations |
| 296 | +```lua |
| 297 | +vim.fn.jobstart(cmd, { |
| 298 | + on_stdout = function(_, data) ... end, |
| 299 | + on_stderr = function(_, data) ... end, |
| 300 | + on_exit = function(_, code) |
| 301 | + if code == 0 then |
| 302 | + callback({ status = "success", data = output }) |
| 303 | + else |
| 304 | + callback({ status = "error", data = error_output }) |
| 305 | + end |
| 306 | + end, |
| 307 | +}) |
| 308 | +``` |
| 309 | + |
| 310 | +### Buffer Content Manipulation |
| 311 | +```lua |
| 312 | +local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 313 | +vim.api.nvim_buf_set_lines(bufnr, start, end_, false, new_lines) |
| 314 | +vim.api.nvim_win_set_cursor(0, { row, col }) |
| 315 | +``` |
| 316 | + |
| 317 | +--- |
| 318 | + |
| 319 | +## Security Considerations |
| 320 | + |
| 321 | +- **Read operations** (`git_read`): No approval required |
| 322 | +- **Write operations** (`git_edit`): Require user approval |
| 323 | +- **Force push**: Extra warning, explicitly dangerous |
| 324 | +- **Input escaping**: Always use `vim.fn.shellescape()` for shell commands |
| 325 | +- **Path validation**: Validate file paths before operations |
| 326 | + |
| 327 | +--- |
| 328 | + |
| 329 | +## API Reference |
| 330 | + |
| 331 | +### Programmatic Exports |
| 332 | + |
| 333 | +```lua |
| 334 | +local gitcommit = require("codecompanion._extensions.gitcommit") |
| 335 | + |
| 336 | +-- Generate commit message |
| 337 | +gitcommit.exports.generate("English", function(result, error) end) |
| 338 | + |
| 339 | +-- Check repository status |
| 340 | +gitcommit.exports.is_git_repo() |
| 341 | + |
| 342 | +-- Git operations |
| 343 | +gitcommit.exports.git_tool.status() |
| 344 | +gitcommit.exports.git_tool.stage({"file.lua"}) |
| 345 | +gitcommit.exports.git_tool.generate_release_notes("v1.0", "v1.1", "markdown") |
| 346 | +``` |
| 347 | + |
| 348 | +--- |
| 349 | + |
| 350 | +## Notes for AI Agents |
| 351 | + |
| 352 | +1. **Always run `stylua --check .`** after making changes to ensure StyLua compliance |
| 353 | +2. **Always run `stylua .`** after modifying code to auto-format |
| 354 | +3. **Maintain v17/v18 compatibility** when modifying tool options |
| 355 | +4. **Use validation.lua** for all parameter validation in tools |
| 356 | +5. **Follow existing patterns** - this codebase is consistent; match the style |
| 357 | +6. **Test with actual CodeCompanion** - the extension requires the parent plugin |
| 358 | +7. **Error messages should be user-friendly** - use icons (✓, ✗, ℹ) for visual feedback |
| 359 | +8 **Document new features** in both code (annotations) and help file |
| 360 | +9. **Update this file (AGENTS.md)** with any new patterns or guidelines you introduce |
0 commit comments