Skip to content

Commit

Permalink
feat: pass preset table instead of preset name
Browse files Browse the repository at this point in the history
  • Loading branch information
S1M0N38 committed Jan 30, 2024
1 parent 00c1a71 commit d13e173
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 4 additions & 7 deletions lua/dante/assistant.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local config = require("dante.config")
local assistant = {}

local function encode(preset, lines)
preset = config.options.presets[preset]
local messages = {
{
role = "system",
Expand All @@ -22,20 +20,19 @@ local function encode(preset, lines)
return json
end

local function command(req)
local function command(preset, req)
local args = {
"--silent",
"--no-buffer",
'--header "Authorization: Bearer $OPENAI_API_KEY"',
'--header "content-type: application/json"',
"--url " .. config.options.base_url .. "/chat/completions",
"--url " .. preset.base_url .. "/chat/completions",
"--data " .. vim.fn.shellescape(req),
}
return "curl " .. table.concat(args, " ")
end

local function decode(preset, res)
preset = config.options.presets[preset]
if #(res or "") < 20 then
return ""
end
Expand Down Expand Up @@ -70,9 +67,9 @@ function assistant.query(preset, query, res_buf, callback)
callback()
end

local job = vim.fn.jobstart(command(encode(preset, query)), {
local job = vim.fn.jobstart(command(preset, encode(preset, query)), {
clear_env = true,
env = { OPENAI_API_KEY = os.getenv(config.options.openai_api_key) or "" },
env = { OPENAI_API_KEY = os.getenv(preset.env_var) or "" },
on_stdout = on_stdout,
on_exit = on_exit,
})
Expand Down
3 changes: 2 additions & 1 deletion lua/dante/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ function dante.main(preset, start_line, end_line)

-- Set options for diff mode
local config = require("dante.config")
vim.cmd("set diffopt=" .. table.concat(config.options.diffopt, ","))
preset = config.options.presets[preset] -- this store all the config for the query
vim.cmd("set diffopt=" .. table.concat(preset.diffopt, ","))

-- Response
vim.cmd("vsplit")
Expand Down

0 comments on commit d13e173

Please sign in to comment.