Fixed issues
This commit is contained in:
parent
925c22b834
commit
495972f826
@ -46,11 +46,13 @@ local function make_request(prompt)
|
||||
-- Escape the payload for shell
|
||||
payload = vim.fn.shellescape(payload)
|
||||
|
||||
-- Updated API endpoint URL
|
||||
-- Updated API endpoint URL with debug headers
|
||||
local command = string.format(
|
||||
"curl -s -X POST "
|
||||
.. "'https://generativelanguage.googleapis.com/v1/models/%s:generateContent?key=%s' "
|
||||
.. "-H 'Content-Type: application/json' "
|
||||
.. "-H 'Accept: application/json' "
|
||||
.. "-v " -- Add verbose output
|
||||
.. "-d %s",
|
||||
model,
|
||||
api_key,
|
||||
@ -58,6 +60,9 @@ local function make_request(prompt)
|
||||
)
|
||||
|
||||
local result = vim.fn.system(command)
|
||||
|
||||
-- Debug: Print the raw response
|
||||
vim.notify("Raw API response: " .. result, vim.log.levels.DEBUG)
|
||||
|
||||
-- Check for errors during the curl execution
|
||||
if vim.v.shell_error ~= 0 then
|
||||
@ -71,24 +76,37 @@ local function make_request(prompt)
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Debug: Print the decoded response structure
|
||||
vim.notify("Decoded response structure: " .. vim.inspect(decoded_result), vim.log.levels.DEBUG)
|
||||
|
||||
return decoded_result
|
||||
end
|
||||
|
||||
function M.get_response(prompt)
|
||||
local result = make_request(prompt)
|
||||
|
||||
if
|
||||
result
|
||||
and result.candidates
|
||||
and result.candidates[1]
|
||||
and result.candidates[1].content
|
||||
and result.candidates[1].content.parts
|
||||
then
|
||||
return result.candidates[1].content.parts[1].text
|
||||
else
|
||||
vim.notify("No response from Google AI API or malformed response.", vim.log.levels.ERROR)
|
||||
return nil
|
||||
if result then
|
||||
if result.error then
|
||||
vim.notify("API Error: " .. vim.inspect(result.error), vim.log.levels.ERROR)
|
||||
return nil
|
||||
end
|
||||
|
||||
if
|
||||
result.candidates
|
||||
and result.candidates[1]
|
||||
and result.candidates[1].content
|
||||
and result.candidates[1].content.parts
|
||||
and result.candidates[1].content.parts[1]
|
||||
and result.candidates[1].content.parts[1].text
|
||||
then
|
||||
return result.candidates[1].content.parts[1].text
|
||||
end
|
||||
|
||||
vim.notify("Unexpected response structure: " .. vim.inspect(result), vim.log.levels.ERROR)
|
||||
end
|
||||
|
||||
vim.notify("No response from Google AI API or malformed response.", vim.log.levels.ERROR)
|
||||
return nil
|
||||
end
|
||||
|
||||
return M
|
||||
|
Loading…
x
Reference in New Issue
Block a user