Skip to content

Commit

Permalink
getting simple window to work
Browse files Browse the repository at this point in the history
  • Loading branch information
huynle committed Jan 30, 2024
1 parent 8fa1e0a commit cda0bcd
Show file tree
Hide file tree
Showing 10 changed files with 1,197 additions and 41 deletions.
16 changes: 8 additions & 8 deletions lua/ogpt/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Api:completions(custom_params, cb)
self:make_call(self.COMPLETIONS_URL, params, cb)
end

function Api:chat_completions(custom_params, cb, should_stop, opts)
function Api:chat_completions(custom_params, partial_result_fn, should_stop, opts)
local stream = custom_params.stream or false
local params, _completion_url = Config.expand_model(self, custom_params)

Expand All @@ -31,7 +31,7 @@ function Api:chat_completions(custom_params, cb, should_stop, opts)
local raw_chunks = ""
local state = "START"

cb = vim.schedule_wrap(cb)
partial_result_fn = vim.schedule_wrap(partial_result_fn)

self:exec(
"curl",
Expand Down Expand Up @@ -59,32 +59,32 @@ function Api:chat_completions(custom_params, cb, should_stop, opts)
}
table.insert(error_msg, vim.inspect(params))
-- local error_msg = "OGPT ERROR: " .. (json.error.message or "Something went wrong")
cb(table.concat(error_msg, " "), "ERROR", ctx)
partial_result_fn(table.concat(error_msg, " "), "ERROR", ctx)
return
end
ctx, raw_chunks, state = self.provider.process_line(json, ctx, raw_chunks, state, cb)
ctx, raw_chunks, state = self.provider.process_line(json, ctx, raw_chunks, state, partial_result_fn)
return
end

for line in chunk:gmatch("[^\n]+") do
local raw_json = string.gsub(line, "^data:", "")
local _ok, _json = pcall(vim.json.decode, raw_json)
if _ok then
ctx, raw_chunks, state = self.provider.process_line(_json, ctx, raw_chunks, state, cb)
ctx, raw_chunks, state = self.provider.process_line(_json, ctx, raw_chunks, state, partial_result_fn)
end
end
end,
function(err, _)
cb(err, "ERROR", ctx)
partial_result_fn(err, "ERROR", ctx)
end,
should_stop,
function()
cb(raw_chunks, "END", ctx)
partial_result_fn(raw_chunks, "END", ctx)
end
)
else
params.stream = false
self:make_call(self.provider.envs.CHAT_COMPLETIONS_URL, params, cb)
self:make_call(self.provider.envs.CHAT_COMPLETIONS_URL, params, partial_result_fn)
end
end

Expand Down
92 changes: 92 additions & 0 deletions lua/ogpt/common/layouts.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
local Config = require("ogpt.config")
local Layout = require("nui.layout")

local M = {}

M.edit_with_nui_layout = function(layout, input, instruction, output, parameters, opts)
opts = opts or {}
local _boxes
if opts.show_parameters then
_boxes = Layout.Box({
Layout.Box({
Layout.Box(input, { grow = 1 }),
Layout.Box(instruction, { size = 3 }),
}, { dir = "col", grow = 1 }),
Layout.Box(output, { grow = 1 }),
Layout.Box(parameters, { size = 40 }),
}, { dir = "row" })
else
_boxes = Layout.Box({
Layout.Box({
Layout.Box(input, { grow = 1 }),
Layout.Box(instruction, { size = 3 }),
}, { dir = "col", size = "50%" }),
Layout.Box(output, { size = "50%" }),
}, { dir = "row" })
end

if not layout then
layout = Layout({
relative = "editor",
position = "50%",
size = {
width = Config.options.popup_layout.center.width,
height = Config.options.popup_layout.center.height,
},
}, _boxes)
else
layout:update(_boxes)
end

layout:mount()

if opts.show_parameters then
parameters:show()
parameters:mount()

vim.api.nvim_set_current_win(parameters.winid)
vim.api.nvim_buf_set_option(parameters.bufnr, "modifiable", false)
vim.api.nvim_win_set_option(parameters.winid, "cursorline", true)
else
parameters:hide()
vim.api.nvim_set_current_win(instruction.winid)
end

return layout
end

M.edit_with_no_layout = function(layout, input, instruction, output, parameters, opts)
opts = opts or {}
opts = vim.tbl_extend("force", opts, {
buf = {
vars = {
ogpt = true,
},
},
})

input:mount()
vim.api.nvim_buf_set_var(input.bufnr, "ogpt_input", true)
output:mount()
vim.api.nvim_buf_set_var(output.bufnr, "ogpt_output", true)
instruction:mount()
vim.api.nvim_buf_set_var(instruction.bufnr, "ogpt_instruction", true)

vim.api.nvim_buf_set_var(parameters.bufnr, "ogpt_parameters", true)

if opts.show_parameters then
parameters:show()
parameters:mount()

vim.api.nvim_set_current_win(parameters.winid)
vim.api.nvim_buf_set_option(parameters.bufnr, "modifiable", false)
vim.api.nvim_win_set_option(parameters.winid, "cursorline", true)
else
parameters:hide()
-- vim.api.nvim_set_current_win(instruction.winid)
end

return nil
end

return M
Loading

0 comments on commit cda0bcd

Please sign in to comment.