diff --git a/lua/gemini/api.lua b/lua/gemini/api.lua index b8f0b70..e1bf5e8 100644 --- a/lua/gemini/api.lua +++ b/lua/gemini/api.lua @@ -87,17 +87,13 @@ function M.get_response(prompt, context, callback) return end - store_message("user", prompt) - - local contents = create_contents(prompt, context) - - -- Validate contents before sending - if not contents or #contents == 0 then - vim.schedule(function() - callback(nil, "Failed to create request contents") - end) - return - end + -- Create the request content + local contents = { + { + role = "user", + parts = {{text = context and (context .. "\n\n" .. prompt) or prompt}} + } + } local payload = vim.json.encode({ contents = contents, @@ -118,9 +114,12 @@ function M.get_response(prompt, context, callback) local request = http.Request.new(url, payload) request:execute(function(result, error) if error then - callback(nil, error) + vim.schedule(function() + callback(nil, "HTTP Error: " .. error) + end) return end + handle_response(result, callback) end) end