This repository was archived by the owner on Jan 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerator.lua
More file actions
147 lines (124 loc) · 4.45 KB
/
Copy pathgenerator.lua
File metadata and controls
147 lines (124 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
local codecompanion_client = require("codecompanion.http")
local codecompanion_config = require("codecompanion.config")
local codecompanion_adapter = require("codecompanion.adapters")
local codecompanion_schema = require("codecompanion.schema")
---@class CodeCompanion.GitCommit.Generator
local Generator = {}
--- @type string?
local _adapter = nil
--- @type string?
local _model = nil
local CONSTANTS = {
STATUS_ERROR = "error",
STATUS_SUCCESS = "success",
}
--- @param adapter string? The adapter to use for generation
--- @param model string? The model of the adapter to use for generation
function Generator.setup(adapter, model)
_adapter = adapter or codecompanion_config.strategies.chat.adapter
_model = model or codecompanion_config.strategies.chat.model
-- Validate adapter
if not codecompanion_adapter.resolve(_adapter) then
error("Invalid adapter specified: " .. tostring(_adapter))
end
end
---Generate commit message using LLM
---
---@param diff string The git diff to analyze
---@param lang? string The language to generate the commit message in (optional)
---@param callback fun(result: string|nil, error: string|nil) Callback function
function Generator.generate_commit_message(diff, lang, callback)
-- Setup adapter
local adapter = codecompanion_adapter.resolve(_adapter)
if not adapter then
return callback(nil, "Failed to resolve adapter")
end
adapter.opts.stream = false
adapter = adapter:map_schema_to_params(codecompanion_schema.get_default(adapter, { model = _model }))
-- Create HTTP client
local new_client = codecompanion_client.new({
adapter = adapter,
})
-- Create prompt for LLM
local prompt = Generator._create_prompt(diff, lang)
local payload = {
messages = adapter:map_roles({
{ role = "user", content = prompt },
}),
}
-- Send request to LLM
new_client:request(payload, {
callback = function(err, data, _adapter)
Generator._handle_response(err, data, _adapter, callback)
end,
}, {
silent = true,
})
end
---Create prompt for commit message generation
---@param diff string The git diff to include in prompt
---@param lang? string The generate language (optional, not used in this implementation)
---@return string prompt The formatted prompt
function Generator._create_prompt(diff, lang)
return string.format(
[[Generate Conventional Commit compliant messages
When to use:
• When analyzing git diffs for commit messages
• When standardizing commit format across projects
• When ensuring consistent commit message patterns
• When generating structured commit documentation
Best practices:
• Must include required type (feat, fix, docs, style, refactor, perf, test, chore)
• Use lowercase for type, optional scope in parentheses
• Start description with imperative verb, keep under 50 characters
• Add body with bullet points for complex changes
• Ensure language matches specification: %s
Format: type(scope): description
Example:
feat(auth): add OAuth2 integration
- implement Google OAuth provider
- update user authentication flow
- add integration tests
Generate commit message for this diff:
```diff
%s
```]],
lang or "English",
diff
)
end
---Handle LLM response
---@param err table|nil Error from request
---@param data table|nil Response data
---@param _adapter table The adapter used
---@param callback fun(result: string|nil, error: string|nil) Callback function
function Generator._handle_response(err, data, _adapter, callback)
-- Handle request errors
if err then
local error_msg = "Error generating commit message: " .. (err.stderr or err.message or "Unknown error")
return callback(nil, error_msg)
end
-- Check for empty or invalid data
if not data then
return callback(nil, "No response received from LLM")
end
-- Process successful response
if data then
local result = _adapter.handlers.chat_output(_adapter, data)
if result and result.status then
if result.status == CONSTANTS.STATUS_SUCCESS then
local content = result.output and result.output.content
if content and vim.trim(content) ~= "" then
return callback(vim.trim(content), nil)
else
return callback(nil, "Generated content is empty")
end
elseif result.status == CONSTANTS.STATUS_ERROR then
local error_msg = result.output or "Unknown error occurred"
return callback(nil, error_msg)
end
end
end
return callback(nil, "No valid response received")
end
return Generator