Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 57 additions & 40 deletions lua/groq-nvim/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- groq-nvim.lua
local M = {}
local curl = require('plenary.curl')
local curl = require("plenary.curl")
local json = vim.json

-- Configuration
Expand All @@ -13,7 +13,7 @@ M.original_range = nil

-- Debugging function
local function log(message)
vim.fn.writefile({message}, '/tmp/groq_nvim_debug.log', 'a')
vim.fn.writefile({ message }, "/tmp/groq_nvim_debug.log", "a")
end

-- Set up the plugin
Expand All @@ -23,23 +23,33 @@ function M.setup(opts)
error("Groq API key not set. Please set it in the setup function.")
end
-- Set up commands
vim.api.nvim_create_user_command("GroqGenerate", M.generate_code, {nargs = 1})
vim.api.nvim_create_user_command("GroqGenerateWithContext", M.generate_code_with_context, {nargs = '+', complete = 'file'})
vim.api.nvim_create_user_command("GroqEdit", M.edit_code, {range = true, nargs = '?'})
vim.api.nvim_create_user_command("GroqGenerate", M.generate_code, { nargs = 1 })
vim.api.nvim_create_user_command(
"GroqGenerateWithContext",
M.generate_code_with_context,
{ nargs = "+", complete = "file" }
)
vim.api.nvim_create_user_command("GroqEdit", M.edit_code, { range = true, nargs = "?" })
--vim.api.nvim_create_user_command("GroqApprove", M.approve_changes, {})
end

-- Helper function to make API calls with streaming
local function call_groq_api_stream(messages, callback)
local job_id = vim.fn.jobstart({"curl", "-sS", "-N",
local job_id = vim.fn.jobstart({
"curl",
"-sS",
"-N",
M.config.api_url,
"-H", "Authorization: Bearer " .. M.config.api_key,
"-H", "Content-Type: application/json",
"-d", json.encode({
model = M.config.model,
"-H",
"Authorization: Bearer " .. M.config.api_key,
"-H",
"Content-Type: application/json",
"-d",
json.encode({
messages = messages,
stream = true
})
model = M.config.model,
stream = true,
}),
}, {
on_stdout = function(_, data)
for _, line in ipairs(data) do
Expand All @@ -55,37 +65,40 @@ local function call_groq_api_stream(messages, callback)
end
end,
on_exit = function()
callback(nil) -- Signal end of stream
end
callback(nil) -- Signal end of stream
end,
})
end

-- Function to generate code
function M.generate_code(opts)
local prompt = opts.args
local messages = {
{role = "system", content = "You are a helpful coding assistant. Based on the users prompt, write the code or response. If the user is asking you to write some code, only generate the code they need with no additional formatting or text. The code you generate is written directly to the current file so make sure it is valid code."},
{role = "user", content = prompt}
{
role = "system",
content = "You are a helpful coding assistant. Based on the users prompt, write the code or response. If the user is asking you to write some code, only generate the code they need with no additional formatting or text. The code you generate is written directly to the current file so make sure it is valid code.",
},
{ role = "user", content = prompt },
}

local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local lines = {}

call_groq_api_stream(messages, function(content)
if content then
local new_lines = vim.split(content, "\n", true)
for i, line in ipairs(new_lines) do
if i == 1 then
-- Append to the current line
local current_line = vim.api.nvim_buf_get_lines(0, row-1, row, false)[1]
vim.api.nvim_buf_set_lines(0, row-1, row, false, {current_line .. line})
local current_line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1]
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { current_line .. line })
else
-- Insert new lines
vim.api.nvim_buf_set_lines(0, row, row, false, {line})
vim.api.nvim_buf_set_lines(0, row, row, false, { line })
row = row + 1
end
end
vim.api.nvim_win_set_cursor(0, {row, col})
vim.api.nvim_win_set_cursor(0, { row, col })
else
-- End of stream, do any cleanup if needed
end
Expand All @@ -99,30 +112,33 @@ function M.edit_code(opts)
local selected_text = table.concat(vim.api.nvim_buf_get_lines(0, start_line, end_line, false), "\n")
local prompt = opts.args
local messages = {
{role = "system", content = "You are a helpful coding assistant. Based on the users prompt, and the selected code, rewrite the selection with any necessary edits based on the users prompt. All of the selected code will be deleted so make sure you rewrite it by incorporating both the old code and the new changes. The user is asking you to write some code, only generate the code they need with no additional formatting or text. The code you generate is written directly to the current file so make sure it is valid code."},
{role = "user", content = prompt .. selected_text}
{
role = "system",
content = "You are a helpful coding assistant. Based on the users prompt, and the selected code, rewrite the selection with any necessary edits based on the users prompt. All of the selected code will be deleted so make sure you rewrite it by incorporating both the old code and the new changes. The user is asking you to write some code, only generate the code they need with no additional formatting or text. The code you generate is written directly to the current file so make sure it is valid code.",
},
{ role = "user", content = prompt .. selected_text },
}
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local lines = {}

-- Clear the selected lines
vim.api.nvim_buf_set_lines(0, start_line, end_line, false, {})

call_groq_api_stream(messages, function(content)
if content then
local new_lines = vim.split(content, "\n", true)
for i, line in ipairs(new_lines) do
if i == 1 then
-- Append to the current line
local current_line = vim.api.nvim_buf_get_lines(0, row-1, row, false)[1]
vim.api.nvim_buf_set_lines(0, row-1, row, false, {current_line .. line})
local current_line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1]
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { current_line .. line })
else
-- Insert new lines
vim.api.nvim_buf_set_lines(0, row, row, false, {line})
vim.api.nvim_buf_set_lines(0, row, row, false, { line })
row = row + 1
end
end
vim.api.nvim_win_set_cursor(0, {row, col})
vim.api.nvim_win_set_cursor(0, { row, col })
else
-- End of stream, do any cleanup if needed
end
Expand Down Expand Up @@ -152,34 +168,35 @@ function M.generate_code_with_context(opts)
end

local messages = {
{role = "system", content = "You are a helpful coding assistant. Based on the users prompt and context, write the code or response. If the user is asking you to write some code, only generate the code they need with no additional formatting or text. The code you generate is written directly to the current file so make sure it is valid code."},
{role = "user", content = prompt .. file_content}
{
role = "system",
content = "You are a helpful coding assistant. Based on the users prompt and context, write the code or response. If the user is asking you to write some code, only generate the code they need with no additional formatting or text. The code you generate is written directly to the current file so make sure it is valid code.",
},
{ role = "user", content = prompt .. file_content },
}

local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local lines = {}

call_groq_api_stream(messages, function(content)
if content then
local new_lines = vim.split(content, "\n", true)
for i, line in ipairs(new_lines) do
if i == 1 then
-- Append to the current line
local current_line = vim.api.nvim_buf_get_lines(0, row-1, row, false)[1]
vim.api.nvim_buf_set_lines(0, row-1, row, false, {current_line .. line})
local current_line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1]
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { current_line .. line })
else
-- Insert new lines
vim.api.nvim_buf_set_lines(0, row, row, false, {line})
vim.api.nvim_buf_set_lines(0, row, row, false, { line })
row = row + 1
end
end
vim.api.nvim_win_set_cursor(0, {row, col})
vim.api.nvim_win_set_cursor(0, { row, col })
else
-- End of stream, do any cleanup if needed
end
end)
end



return M
6 changes: 6 additions & 0 deletions stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
call_parentheses = "Always"
column_width = 120
indent_type = "Spaces"
indent_width = 2
line_endings = "Unix"
quote_style = "ForceDouble"