Skip to content

Commit 26faf18

Browse files
committed
refactor(gitcommit): improve streaming request handling and error management
- Refactor send_http_request function with improved code structure - Enhance error handling and chunk processing in streaming requests - Optimize client request configuration and callback management
1 parent 78a72b4 commit 26faf18

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

lua/codecompanion/_extensions/gitcommit/generator.lua

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,39 @@ local function send_http_request(client, adapter, payload, callback)
6767
}
6868

6969
-- Use async send to properly handle streaming responses
70-
client:send(payload, vim.tbl_extend("force", request_opts, {
71-
stream = true,
72-
on_chunk = function(chunk)
73-
if chunk and chunk ~= "" then
74-
-- Use adapter's chat_output handler to process the chunk
75-
local result = adapter.handlers.chat_output(adapter, chunk)
76-
if result and result.status == CONSTANTS.STATUS_SUCCESS then
77-
local content = result.output and result.output.content
78-
if content and content ~= "" then
79-
accumulated = accumulated .. content
70+
client:send(
71+
payload,
72+
vim.tbl_extend("force", request_opts, {
73+
stream = true,
74+
on_chunk = function(chunk)
75+
if chunk and chunk ~= "" then
76+
-- Use adapter's chat_output handler to process the chunk
77+
local result = adapter.handlers.chat_output(adapter, chunk)
78+
if result and result.status == CONSTANTS.STATUS_SUCCESS then
79+
local content = result.output and result.output.content
80+
if content and content ~= "" then
81+
accumulated = accumulated .. content
82+
end
8083
end
8184
end
82-
end
83-
end,
84-
on_done = function()
85-
if not has_error then
86-
if accumulated ~= "" then
87-
local cleaned = Generator._clean_commit_message(accumulated)
88-
callback(cleaned, nil)
89-
else
90-
callback(nil, "Generated content is empty")
85+
end,
86+
on_done = function()
87+
if not has_error then
88+
if accumulated ~= "" then
89+
local cleaned = Generator._clean_commit_message(accumulated)
90+
callback(cleaned, nil)
91+
else
92+
callback(nil, "Generated content is empty")
93+
end
9194
end
92-
end
93-
end,
94-
on_error = function(err)
95-
has_error = true
96-
local error_msg = "HTTP request failed: " .. (err.message or vim.inspect(err))
97-
callback(nil, error_msg)
98-
end,
99-
}))
95+
end,
96+
on_error = function(err)
97+
has_error = true
98+
local error_msg = "HTTP request failed: " .. (err.message or vim.inspect(err))
99+
callback(nil, error_msg)
100+
end,
101+
})
102+
)
100103
end
101104

102105
---Send request using ACP client
@@ -202,16 +205,16 @@ function Generator.generate_commit_message(diff, lang, commit_history, callback)
202205
schema_opts.model = _model_name
203206
end
204207
adapter = adapter:map_schema_to_params(codecompanion_schema.get_default(adapter, schema_opts))
205-
end
208+
end
206209

207-
-- 5. Create client (after potential schema mapping for HTTP)
208-
local client, err = create_client(adapter)
209-
if not client then
210-
return callback(nil, err)
211-
end
210+
-- 5. Create client (after potential schema mapping for HTTP)
211+
local client, err = create_client(adapter)
212+
if not client then
213+
return callback(nil, err)
214+
end
212215

213-
-- 6. Send request based on adapter type
214-
if adapter.type == "http" then
216+
-- 6. Send request based on adapter type
217+
if adapter.type == "http" then
215218
-- Prepare HTTP payload
216219
local payload = {
217220
messages = adapter:map_roles(messages),

0 commit comments

Comments
 (0)