fix: gemini (#323)

This commit is contained in:
yetone 2024-08-28 22:17:00 +08:00 committed by GitHub
parent d8c1054e07
commit 9dc00ecc10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 4 deletions

View File

@ -77,6 +77,7 @@ Your task is to modify the provided code according to the user's request. Follow
7. Do not omit any parts of the code, even if they are unchanged. 7. Do not omit any parts of the code, even if they are unchanged.
8. Maintain the SAME indentation in the returned code as in the source code 8. Maintain the SAME indentation in the returned code as in the source code
9. Do NOT include three backticks: ``` 9. Do NOT include three backticks: ```
10. Only return code part, do NOT return the context part!
Remember: Your response should contain nothing but ONLY the modified code, ready to be used as a direct replacement for the original file. Remember: Your response should contain nothing but ONLY the modified code, ready to be used as a direct replacement for the original file.
]] ]]
@ -136,6 +137,8 @@ M.stream = function(question, code_lang, code_content, selected_content_content,
active_job = nil active_job = nil
end end
local completed = false
active_job = curl.post(spec.url, { active_job = curl.post(spec.url, {
headers = spec.headers, headers = spec.headers,
proxy = spec.proxy, proxy = spec.proxy,
@ -143,6 +146,7 @@ M.stream = function(question, code_lang, code_content, selected_content_content,
body = vim.json.encode(spec.body), body = vim.json.encode(spec.body),
stream = function(err, data, _) stream = function(err, data, _)
if err then if err then
completed = true
on_complete(err) on_complete(err)
return return
end end
@ -168,6 +172,7 @@ M.stream = function(question, code_lang, code_content, selected_content_content,
end) end)
end, end,
on_error = function(err) on_error = function(err)
completed = true
on_complete(err) on_complete(err)
end, end,
callback = function(result) callback = function(result)
@ -177,6 +182,19 @@ M.stream = function(question, code_lang, code_content, selected_content_content,
else else
Utils.error("API request failed with status " .. result.status, { once = true, title = "Avante" }) Utils.error("API request failed with status " .. result.status, { once = true, title = "Avante" })
end end
vim.schedule(function()
if not completed then
completed = true
on_complete("API request failed with status " .. result.status .. ". Body: " .. vim.inspect(result.body))
end
end)
else
vim.schedule(function()
if not completed then
completed = true
on_complete(nil)
end
end)
end end
active_job = nil active_job = nil
end, end,

View File

@ -50,14 +50,27 @@ M.parse_message = function(opts)
}, },
} }
end end
M.parse_response = function(data_stream, _, opts) M.parse_response = function(data_stream, _, opts)
local json = vim.json.decode(data_stream) local json = vim.json.decode(data_stream)
if json.candidates and #json.candidates > 0 then
opts.on_chunk(json.candidates[1].content.parts[1].text) opts.on_chunk(json.candidates[1].content.parts[1].text)
end end
end
M.parse_curl_args = function(provider, code_opts) M.parse_curl_args = function(provider, code_opts)
local base, body_opts = P.parse_config(provider) local base, body_opts = P.parse_config(provider)
body_opts = {
generationConfig = {
temperature = body_opts.temperature,
maxOutputTokens = body_opts.max_tokens,
},
}
body_opts.temperature = nil
body_opts.max_tokens = nil
return { return {
url = Utils.trim(base.endpoint, { suffix = "/" }) url = Utils.trim(base.endpoint, { suffix = "/" })
.. "/" .. "/"

View File

@ -364,15 +364,23 @@ function Selection:create_editing_input()
local start_line = self.selection.range.start.line local start_line = self.selection.range.start.line
local finish_line = self.selection.range.finish.line local finish_line = self.selection.range.finish.line
local indentation = Utils.get_indentation(code_lines[self.selection.range.start.line]) local original_first_line_indentation = Utils.get_indentation(code_lines[self.selection.range.start.line])
api.nvim_exec_autocmds("User", { pattern = EDITING_INPUT_START_SPINNER_PATTERN }) api.nvim_exec_autocmds("User", { pattern = EDITING_INPUT_START_SPINNER_PATTERN })
---@type AvanteChunkParser ---@type AvanteChunkParser
local on_chunk = function(chunk) local on_chunk = function(chunk)
full_response = full_response .. chunk full_response = full_response .. chunk
local response_lines = vim.split(full_response, "\n") local response_lines = vim.split(full_response, "\n")
local need_prepend_indentation = false
if #response_lines > 0 then
local first_line = response_lines[1]
local first_line_indentation = Utils.get_indentation(first_line)
need_prepend_indentation = first_line_indentation ~= original_first_line_indentation
end
if need_prepend_indentation then
for i, line in ipairs(response_lines) do for i, line in ipairs(response_lines) do
response_lines[i] = indentation .. line response_lines[i] = original_first_line_indentation .. line
end
end end
api.nvim_buf_set_lines(code_bufnr, start_line - 1, finish_line, true, response_lines) api.nvim_buf_set_lines(code_bufnr, start_line - 1, finish_line, true, response_lines)
finish_line = start_line + #response_lines - 1 finish_line = start_line + #response_lines - 1