Restructure

This commit is contained in:
2025-03-16 14:09:26 +01:00
parent 35cda55e5b
commit dad689ded2
3 changed files with 9 additions and 2 deletions

7
plugin/gemini.lua Normal file
View File

@ -0,0 +1,7 @@
if vim.g.loaded_gemini then
return
end
vim.g.loaded_gemini = true
-- Optional: Auto-setup the plugin
-- require("gemini").setup()

View File

@ -1,88 +0,0 @@
-- plugin/gemini/api.lua
local M = {}
local function get_api_key()
-- Check for environment variable
local api_key = os.getenv("GEMINI_API_KEY")
if api_key then
return api_key
end
-- Check for Neovim global variable
api_key = vim.g.gemini_api_key
if api_key then
return api_key
end
return nil -- API key not found
end
local function make_request(prompt)
local api_key = get_api_key() -- Get API key from environment or global variable
if not api_key then
vim.notify(
"Google AI API key not set. Set GEMINI_API_KEY environment variable or g.gemini_api_key in your init.vim or init.lua.",
vim.log.levels.ERROR
)
return nil
end
local model = "gemini-2.0-flash" -- SPECIFY Gemini 2.0 Flash MODEL
-- Construct the JSON payload
local payload = vim.json.encode({
contents = {
{
parts = {
{
text = prompt,
},
},
},
},
})
local command = string.format(
"curl -s -X POST "
.. "'https://generative-ai.googleapis.com/v1beta/models/%s:generateContent?key=%s' "
.. "-H 'Content-Type: application/json' "
.. "-d '%s'",
model,
api_key,
payload
)
local result = vim.fn.system(command)
-- Check for errors during the curl execution
if vim.v.shell_error ~= 0 then
vim.notify("Error executing curl. Check your command and ensure curl is installed.", vim.log.levels.ERROR)
return nil
end
local decoded_result = vim.json.decode(result)
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
end
end
print("api.lua loaded")
return M

View File

@ -1,58 +0,0 @@
-- plugin/gemini/init.lua
print(vim.o.runtimepath)
local api = require("gemini.plugin.gemini.api")
local function gemini_query(prompt)
local response = api.get_response(prompt)
if response then
-- Display the response in a new buffer
local new_buf = vim.api.nvim_create_buf(false, true) -- Create a scratch buffer
vim.api.nvim_buf_set_lines(new_buf, 0, 0, false, vim.split(response, "\n"))
local new_win = vim.api.nvim_open_win(new_buf, true, {
relative = "editor",
width = 80,
height = 20,
row = 5,
col = vim.o.columns / 2 - 40,
border = "rounded",
title = "Google AI Response",
})
else
vim.notify("Failed to get a response from Gemini API", vim.log.levels.ERROR)
end
end
vim.api.nvim_create_user_command("Gemini", function(opts)
local prompt = opts.args
if prompt == "" then
vim.notify("Please provide a prompt for Gemini.", vim.log.levels.WARN)
return
end
gemini_query(prompt)
end, {
desc = "Query Google AI",
nargs = "+", -- Require at least one argument
complete = "shellcmd", -- Optional: Enable completion
})
-- Keymapping example (optional)
vim.keymap.set("n", "<leader>g", function()
vim.ui.input({ prompt = "Gemini Query: " }, function(input)
if input then
gemini_query(input)
end
end)
end, { desc = "Query Google AI (via Input)" })
-- Add a setup function (even if it's empty)
local function setup()
print("gemini.setup() called") -- For debugging
local api = require("gemini.plugin.gemini.api")
print("api module loaded:", api) -- For debugging
-- Optional: Add any initialization logic here
end
return {
setup = setup,
}