Skip to content

Commit

Permalink
stable
Browse files Browse the repository at this point in the history
  • Loading branch information
huynle committed Feb 11, 2024
1 parent 92f7ff2 commit 0638f8b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
14 changes: 9 additions & 5 deletions lua/ogpt/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ function Api:chat_completions(custom_params, partial_result_fn, should_stop, opt
end
end
end,
function(err, _)
partial_result_fn(err, "ERROR", ctx)
function(_text, _state, _ctx)
partial_result_fn(_text, _state, _ctx)
-- if opts.on_stop then
-- opts.on_stop()
-- end
end,
should_stop,
function()
Expand Down Expand Up @@ -286,7 +289,8 @@ function Api:exec(cmd, args, on_stdout_chunk, on_complete, should_stop, on_stop)
pcall(function()
handle:close()
end)
on_stop()
-- on_stop()
on_complete("", "END")
end
return
end
Expand All @@ -313,13 +317,13 @@ function Api:exec(cmd, args, on_stdout_chunk, on_complete, should_stop, on_stop)

vim.schedule(function()
if code ~= 0 then
on_complete(vim.trim(table.concat(stderr_chunks, "")))
on_complete(vim.trim(table.concat(stderr_chunks, "")), "ERROR")
end
end)
end)

if not handle then
on_complete(cmd .. " could not be started: " .. err)
on_complete(cmd .. " could not be started: " .. err, "ERROR")
else
stdout:read_start(on_stdout_read)
stderr:read_start(on_stderr_read)
Expand Down
32 changes: 19 additions & 13 deletions lua/ogpt/flows/chat/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ end
function Chat:addAnswerPartial(text, state, ctx)
if state == "ERROR" then
self:stopSpinner()
self.is_running = false
return self:addAnswer(text, {})
end

Expand All @@ -234,7 +233,10 @@ function Chat:addAnswerPartial(text, state, ctx)
start_line = prev.end_line + (prev.type == ANSWER and 2 or 1)
end

if state == "END" and text ~= "" then
if state == "END" and text == "" then
-- -- most likely, ended by the using raising the stop flag
-- self:stopSpinner()
elseif state == "END" and text ~= "" then
local usage = {}
local idx = self.session:add_item({
type = ANSWER,
Expand Down Expand Up @@ -266,16 +268,17 @@ function Chat:addAnswerPartial(text, state, ctx)
self.selectedIndex = self.selectedIndex + 1
vim.api.nvim_buf_set_lines(self.chat_window.bufnr, -1, -1, false, { "", "" })
Signs.set_for_lines(self.chat_window.bufnr, start_line, end_line, "chat")
self.is_running = false
end

if state == "START" then
self.is_running = true
self:stopSpinner()
self:set_lines(-2, -1, false, { "" })
vim.api.nvim_buf_set_option(self.chat_window.bufnr, "modifiable", true)
end

if state == "START" or state == "CONTINUE" then
self.is_running = true
vim.api.nvim_buf_set_option(self.chat_window.bufnr, "modifiable", true)
local lines = vim.split(text, "\n", {})
local length = #lines
Expand All @@ -296,6 +299,9 @@ function Chat:addAnswerPartial(text, state, ctx)
end
end
end
-- assume after each partial answer, the API stopped streaming
-- gemini has no stop flag in its response
self.is_running = false
end

function Chat:get_total_tokens()
Expand Down Expand Up @@ -476,6 +482,7 @@ function Chat:renderLastMessage()
end

function Chat:showProgess()
self.is_running = true
self.spinner:start()
end

Expand Down Expand Up @@ -752,8 +759,6 @@ function Chat:open()
return
end

self.is_running = true

-- clear input
vim.api.nvim_buf_set_lines(self.chat_input.bufnr, 0, -1, false, { "" })

Expand All @@ -772,14 +777,14 @@ function Chat:open()
self:addAnswerPartial(answer, state, ctx)
end, function()
-- check the stop flag if it should stop
-- return not self.is_running
return self.stop_flag
end, {
on_stop = function()
self:stopSpinner()
self.is_running = false
-- reset the stop flag
self.stop_flag = false
end,
-- on_stop = function()
-- self:stopSpinner()
-- -- reset the stop flag
-- self.is_running = false
-- end,
})
end
end,
Expand Down Expand Up @@ -856,7 +861,7 @@ function Chat:set_keymaps()
-- stop generating
self:map(Config.options.chat.keymaps.stop_generating, function()
self.stop_flag = true
self.is_running = false
-- self.is_running = false
end, { self.chat_input })

-- close
Expand Down Expand Up @@ -889,7 +894,8 @@ function Chat:set_keymaps()

-- new session
self:map(Config.options.chat.keymaps.new_session, function()
self.stop_flag = true
-- self.stop_flag = true
self.is_running = false
self:new_session()
self.sessions_panel:refresh()
end, { self.parameters_panel, self.chat_input, self.chat_window })
Expand Down

0 comments on commit 0638f8b

Please sign in to comment.