Skip to content

Commit

Permalink
chore: Code refactoring and fix typos (#33)
Browse files Browse the repository at this point in the history
- Remove unused parameters
  - `value` in `function(key, value)` in run_action -> select_action ->
    cb in ogpt.lua
  - `usage` in `CompletionAction:on_result(answer, usage)`
  - `usage` in `PopupAction:on_result(answer, usage)`
  - (For consistency) `usage` in `BaseAction:on_result(answer, usage)`
  - `processsed` to `processed` and `Sesssions` to `Sessions`
- Add comments to comprehend code better
  - Add what other strategies fall into `else` case in `PopupAction:run`
  • Loading branch information
swoh816 authored Sep 27, 2024
1 parent 4fadda0 commit 068dec6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lua/ogpt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ end
M.run_action = function(opts)
if opts.args == "" then
M.select_action({
cb = function(key, value)
cb = function(key)
local _opts = vim.tbl_extend("force", opts, {
args = key,
fargs = {
Expand Down
2 changes: 1 addition & 1 deletion lua/ogpt/flows/actions/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function BaseAction:run()
self:set_loading(true)
end

function BaseAction:on_result(answer, usage)
function BaseAction:on_result(answer)
self:set_loading(false)
end

Expand Down
4 changes: 2 additions & 2 deletions lua/ogpt/flows/actions/completions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ function CompletionAction:run()
local params = self:get_params()
params.stream = false
Api.completions(params, function(answer, usage)
self:on_result(answer, usage)
self:on_result(answer)
end)
end)
end

function CompletionAction:on_result(answer, usage)
function CompletionAction:on_result(answer)
vim.schedule(function()
self:set_loading(false)

Expand Down
4 changes: 2 additions & 2 deletions lua/ogpt/flows/actions/popup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function PopupAction:run()
end
end,
})
else
else -- Other Popup strategies: REPLACE, APPEND, PREPEND, QUICK_FIX
self:set_loading(true)
self.provider.api:chat_completions(response, {
custom_params = params,
Expand All @@ -107,7 +107,7 @@ function PopupAction:run()
end
end

function PopupAction:on_result(answer, usage)
function PopupAction:on_result(answer)
self:set_loading(false)
local lines = utils.split_string_by_line(answer)
local _, start_row, start_col, end_row, end_col = self:get_visual_selection()
Expand Down
2 changes: 1 addition & 1 deletion lua/ogpt/provider/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function Provider:process_response(response)
return
end

-- given a JSON response from the STREAMING api, processs it
-- given a JSON response from the STREAMING api, process it
if type(json) == "string" then
utils.log("got something weird. " .. json, vim.log.levels.ERROR)
elseif vim.tbl_isempty(json) then
Expand Down
14 changes: 7 additions & 7 deletions lua/ogpt/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function Response:init(provider, events)
self.not_processed = Deque.new()
self.not_processed_raw = Deque.new()
self.raw_chunk_tx, self.raw_chunk_rx = channel.mpsc()
self.processed_raw_tx, self.processsed_raw_rx = channel.mpsc()
self.processed_content_tx, self.processsed_content_rx = channel.mpsc()
self.processed_raw_tx, self.processed_raw_rx = channel.mpsc()
self.processed_content_tx, self.processed_content_rx = channel.mpsc()
self.response_state = nil
self.chunk_regex = ""
self:set_state(self.STATE_INIT)
Expand Down Expand Up @@ -90,7 +90,7 @@ function Response:_process_added_chunk()
chunk = chunk .. queued_chunk
end

-- Run different strategies for processsing responses here
-- Run different strategies for processing responses here
if self.provider.response_params.strategy == self.STRATEGY_CHUNK then
self.processed_raw_tx.send(chunk)
elseif
Expand Down Expand Up @@ -123,7 +123,7 @@ function Response:_process_added_chunk()
end

function Response:pop_content()
local content = self.processsed_content_rx.recv()
local content = self.processed_content_rx.recv()
if content[2] == "END" and (content[1] and content[1] == "") then
content[1] = self:get_processed_text()
end
Expand All @@ -141,14 +141,14 @@ function Response:render()
end

function Response:pop_chunk()
-- -- pop the next chunk and add anything that is not processs
-- -- pop the next chunk and add anything that is not process
-- local _value = self.not_processed
-- self.not_processed = ""
-- local _chunk = self.processsed_raw_rx.recv()
-- local _chunk = self.processed_raw_rx.recv()
-- utils.log("Got chunk... now appending to 'not_processed'", vim.log.levels.TRACE)
-- return _value .. _chunk

local _chunk = self.processsed_raw_rx.recv()
local _chunk = self.processed_raw_rx.recv()
utils.log("pushing processed raw to queue: " .. _chunk, vim.log.levels.TRACE)

-- push on to queue
Expand Down

0 comments on commit 068dec6

Please sign in to comment.