Skip to content

Commit 793ebe3

Browse files
committed
refactor(gitcommit): improve adapter and configuration handling
- update generator to support table-based adapter configuration - enhance adapter resolution with fallback mechanism - add model override support in schema mapping - update system prompt for Git workflow specialist tool
1 parent 38a139d commit 793ebe3

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Add this extension to your CodeCompanion configuration:
2525
require("codecompanion").setup({
2626
extensions = {
2727
gitcommit = {
28-
callback = "codecompanion._extensions.gitcommit",
28+
callback = "codecompanion.extensions.gitcommit",
2929
opts = {
3030
-- Basic configuration
3131
adapter = "openai", -- LLM adapter

lua/codecompanion/_extensions/gitcommit/generator.lua

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,50 @@ local CONSTANTS = {
1919
--- @param adapter string? The adapter to use for generation
2020
--- @param model string? The model of the adapter to use for generation
2121
function Generator.setup(adapter, model)
22-
_adapter = adapter or codecompanion_config.strategies.chat.adapter
23-
_model = model or codecompanion_config.strategies.chat.model
24-
25-
-- Validate adapter
26-
if not codecompanion_adapter.resolve(_adapter) then
27-
error("Invalid adapter specified: " .. tostring(_adapter))
22+
-- Get the adapter configuration properly
23+
local chat_config = codecompanion_config.strategies.chat
24+
25+
-- Handle adapter as a table (with name and model) or string
26+
if type(chat_config.adapter) == "table" then
27+
_adapter = adapter or chat_config.adapter.name
28+
_model = model or chat_config.adapter.model or chat_config.model
29+
else
30+
_adapter = adapter or chat_config.adapter
31+
_model = model or chat_config.model
2832
end
2933
end
3034
---@param commit_history? string[] Array of recent commit messages for context (optional)
3135
function Generator.generate_commit_message(diff, lang, commit_history, callback)
32-
-- Setup adapter
33-
local adapter = codecompanion_adapter.resolve(_adapter)
36+
-- Setup adapter with proper resolution
37+
local adapter
38+
local success, result = pcall(function()
39+
return codecompanion_adapter.resolve({
40+
name = _adapter,
41+
model = _model,
42+
})
43+
end)
44+
45+
if not success then
46+
-- Fallback to simple resolve
47+
adapter = codecompanion_adapter.resolve(_adapter)
48+
else
49+
adapter = result
50+
end
51+
3452
if not adapter then
35-
return callback(nil, "Failed to resolve adapter")
53+
return callback(nil, "Failed to resolve adapter: " .. tostring(_adapter))
3654
end
3755

56+
-- Configure adapter for non-streaming
57+
adapter.opts = adapter.opts or {}
3858
adapter.opts.stream = false
39-
adapter = adapter:map_schema_to_params(codecompanion_schema.get_default(adapter, { model = _model }))
59+
60+
-- Map schema with model override if specified
61+
local schema_opts = {}
62+
if _model then
63+
schema_opts.model = _model
64+
end
65+
adapter = adapter:map_schema_to_params(codecompanion_schema.get_default(adapter, schema_opts))
4066

4167
-- Create HTTP client
4268
local new_client = codecompanion_client.new({

lua/codecompanion/_extensions/gitcommit/init.lua

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,37 @@ local function setup_tools(opts)
107107
chat_tools.groups = chat_tools.groups or {}
108108
chat_tools.groups["git_bot"] = {
109109
description = "A Git agent that can perform read and write operations.",
110-
system_prompt = [[Provide Git repository assistance and management
111-
112-
When to use:
113-
• When users need comprehensive Git operations
114-
• When combining read and write Git operations
115-
• When providing guided Git workflow assistance
116-
• When troubleshooting Git repository issues
117-
118-
Best practices:
119-
• Use git_read tool for repository analysis first
120-
• Ensure operations are safe before execution
121-
• Avoid destructive operations without user confirmation
122-
• Provide clear explanations for Git commands]],
110+
system_prompt = [[You are a Git workflow specialist with expert-level knowledge of version control best practices and Git operations.
111+
112+
CORE RESPONSIBILITIES:
113+
• Analyze repository state and provide comprehensive Git assistance
114+
• Execute safe and efficient Git operations through available tools
115+
• Guide users through complex Git workflows and troubleshooting
116+
• Maintain repository integrity and prevent data loss
117+
118+
WORKFLOW APPROACH:
119+
1. Always start with git_read to understand the current repository state
120+
2. Analyze the situation before suggesting or executing operations
121+
3. Explain the impact of operations before execution
122+
4. Use git_edit only after confirming the intended changes
123+
124+
SAFETY PROTOCOLS:
125+
• Never execute destructive operations (reset --hard, force push) without explicit confirmation
126+
• Always check current branch and uncommitted changes before operations
127+
• Warn about potential conflicts or issues before they occur
128+
• Preserve user's work by suggesting stash or backup when appropriate
129+
130+
BEST PRACTICES:
131+
• Follow conventional commits and branching strategies
132+
• Provide clear explanations of Git concepts when needed
133+
• Suggest appropriate Git workflows based on project type
134+
• Help maintain clean and meaningful commit history
135+
136+
When responding:
137+
- Be concise but thorough in explanations
138+
- Use git_read first to assess the situation
139+
- Propose operations step-by-step
140+
- Confirm understanding before using git_edit]],
123141
tools = {
124142
"git_read",
125143
"git_edit",

0 commit comments

Comments
 (0)