Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Task #98

Merged
merged 2 commits into from
Jun 28, 2024
Merged
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
5 changes: 5 additions & 0 deletions lua/fittencode/actions/identify_programming_language.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local API = require('fittencode.api').api
local Base = require('fittencode.base')
local Config = require('fittencode.config')
local Log = require('fittencode.log')
local Status = require('fittencode.status')

local M = {}

Expand All @@ -16,6 +17,10 @@ local IPL_DEBOUNCE_TIME = 1000
local ipl_timer = nil

local function _identify_current_buffer()
local inline = API.get_current_status()
if inline == Status.C.GENERATING then
return
end
local buffer = api.nvim_get_current_buf()
local name = api.nvim_buf_get_name(buffer)
local ext = vim.fn.fnamemodify(name, ':e')
Expand Down
41 changes: 12 additions & 29 deletions lua/fittencode/engines/actions/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local api = vim.api
local fn = vim.fn
local uv = vim.uv or vim.loop

local Base = require('fittencode.base')
local Chat = require('fittencode.views.chat')
Expand All @@ -12,7 +13,6 @@ local PromptProviders = require('fittencode.prompt_providers')
local Sessions = require('fittencode.sessions')
local Status = require('fittencode.status')
local Preprocessing = require('fittencode.preprocessing')
local TaskScheduler = require('fittencode.tasks')
local Unicode = require('fittencode.unicode')

local schedule = Base.schedule
Expand Down Expand Up @@ -62,11 +62,6 @@ local chat = nil
---@type ActionsContent
local content = nil

local TASK_DEFAULT = 1
local TASK_HEADLESS = 2
---@type table<integer, TaskScheduler>
local tasks = {}

-- One by one evaluation
local lock = false

Expand Down Expand Up @@ -110,22 +105,13 @@ local function get_action_type(action)
return ACTION_TYPES[action]
end

local function _create_task(headless)
if headless then
return tasks[TASK_HEADLESS]:create()
else
return tasks[TASK_DEFAULT]:create()
end
end

---@param task_id integer
---@param timestamp integer
---@param suggestions Suggestions
---@return Suggestions?, integer?
local function preprocessing(presug, task_id, headless, preprocess_format, suggestions)
local match = headless and tasks[TASK_HEADLESS]:match_clean(task_id, nil, nil, false) or
tasks[TASK_DEFAULT]:match_clean(task_id, nil, nil)
local ms = match[2]
if not match[1] or not suggestions or #suggestions == 0 then
local function preprocessing(presug, timestamp, preprocess_format, suggestions)
local ms = math.floor((uv.hrtime() - timestamp) / 1000000)
Log.debug('A<{}> Received and time elapsed: {} ms', string.format('%x', timestamp), ms)
if not suggestions or #suggestions == 0 then
return nil, ms
end
local opts = {
Expand Down Expand Up @@ -412,10 +398,11 @@ local function chain_actions(opts)
}
end
Promise:new(function(resolve, reject)
local task_id = _create_task(headless)
Sessions.request_generate_one_stage(task_id, prompt_ctx, function(id, prompt, suggestions)
local lines, ms = preprocessing(presug, id, headless, preprocess_format, suggestions)
Log.debug('ActionsEngine<{}> Preprocessed: {}, Generated: {}', string.format('%x', id), lines, suggestions)
local last_timestamp = uv.hrtime()
Log.debug('A<{}> Send request', string.format('%x', last_timestamp))
Sessions.request_generate_one_stage(last_timestamp, prompt_ctx, function(timestamp, prompt, suggestions)
local lines, ms = preprocessing(presug, timestamp, preprocess_format, suggestions)
Log.debug('A<{}> Preprocessed: {}, Generated: {}', string.format('%x', timestamp), lines, suggestions)
elapsed_time = elapsed_time + ms
if not lines or #lines == 0 then
if extra_newline then
Expand Down Expand Up @@ -815,12 +802,8 @@ local CHAT_MODEL = {
function ActionsEngine.setup()
chat = Chat:new(CHAT_MODEL)
content = Content:new(chat)
tasks[TASK_DEFAULT] = TaskScheduler:new('ActionsEngine/Default')
tasks[TASK_DEFAULT]:setup()
tasks[TASK_HEADLESS] = TaskScheduler:new('ActionsEngine/Headless')
tasks[TASK_HEADLESS]:setup()
status = Status:new({
tag = 'ActionsEngine',
tag = 'A',
ready_idle = true,
})
setup_actions_menu()
Expand Down
44 changes: 20 additions & 24 deletions lua/fittencode/engines/inline/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local api = vim.api
local uv = vim.uv or vim.loop

local Base = require('fittencode.base')
local Config = require('fittencode.config')
Expand All @@ -9,7 +10,6 @@ local Model = require('fittencode.engines.inline.model')
local Sessions = require('fittencode.sessions')
local Status = require('fittencode.status')
local Preprocessing = require('fittencode.preprocessing')
local TaskScheduler = require('fittencode.tasks')
local PromptProviders = require('fittencode.prompt_providers')

local schedule = Base.schedule
Expand All @@ -21,9 +21,6 @@ local M = {}
---@class InlineModel
local model = nil

---@class TaskScheduler
local tasks = nil

---@type Status
local status = nil

Expand All @@ -44,6 +41,8 @@ local CURSORMOVED_INTERVAL = 120
---@type uv_timer_t?
local cursormoved_timer = nil

local last_timestamp = 0

local function testandclear_cursor_ignored(row, col)
local c = ignore_cursor
ignore_cursor = nil
Expand All @@ -66,18 +65,16 @@ function M.update_disabled()
end

---@param ctx PromptContext
---@param task_id integer
---@param timestamp integer
---@param suggestions? Suggestions
---@return Suggestions?
local function preprocessing(ctx, task_id, suggestions)
local row, col = Base.get_cursor(ctx.window)
if not row or not col then
return
end
local match = tasks:match_clean(task_id, row, col)
if not match[1] then
local function preprocessing(ctx, timestamp, suggestions)
if timestamp ~= last_timestamp then
Log.debug('I<{}> Received but outdated', string.format('%x', timestamp))
return
end
local ms = math.floor((uv.hrtime() - timestamp) / 1000000)
Log.debug('I<{}> Received and time elapsed: {} ms', string.format('%x', timestamp), ms)
if not suggestions or #suggestions == 0 then
return
end
Expand Down Expand Up @@ -183,14 +180,15 @@ end
local function _generate_one_stage(row, col, on_success, on_error)
status:update(SC.GENERATING)

local task_id = tasks:create(row, col)
last_timestamp = uv.hrtime()
local ctx = PromptProviders.get_current_prompt_ctx(row, col)
Sessions.request_generate_one_stage(task_id, ctx, function(id, _, suggestions)
local lines = preprocessing(ctx, id, suggestions)
Log.debug('InlineEngine<{}> Preprocessed: {}, Generated: {}', string.format('%x', id), lines, suggestions)
Log.debug('I<{}> Send request at cursor: {}', string.format('%x', last_timestamp), { row, col })
Sessions.request_generate_one_stage(last_timestamp, ctx, function(timestamp, _, suggestions)
local lines = preprocessing(ctx, timestamp, suggestions)
if lines and #lines > 0 then
Log.debug('I<{}> Preprocessed: {}, Generated: {}', string.format('%x', timestamp), lines, suggestions)
status:update(SC.SUGGESTIONS_READY)
apply_new_suggestions(task_id, row, col, lines)
apply_new_suggestions(timestamp, row, col, lines)
schedule(on_success, vim.deepcopy(lines))
else
status:update(SC.NO_MORE_SUGGESTIONS)
Expand All @@ -210,16 +208,16 @@ end
---@param on_success? function
---@param on_error? function
function M.generate_one_stage(row, col, force, delaytime, on_success, on_error)
Log.debug('Start generating one stage')
-- Log.debug('Start generating one stage')

if not force and model:cache_hit(row, col) and M.has_suggestions() then
status:update(SC.SUGGESTIONS_READY)
render_virt_text_segments(model:get_suggestions_segments())
schedule(on_success, model:make_new_trim_commmited_suggestions())
Log.debug('CACHE HIT')
-- Log.debug('CACHE HIT')
return
else
Log.debug('CACHE MISS')
-- Log.debug('CACHE MISS')
end

if inline_suggestions_ready() then
Expand Down Expand Up @@ -422,7 +420,7 @@ function M.reset()
clear_virt_text_all()
end
model:reset()
tasks:clear()
last_timestamp = 0
end

---@return boolean
Expand Down Expand Up @@ -698,9 +696,7 @@ end

function M.setup()
model = Model:new()
tasks = TaskScheduler:new('InlineEngine')
tasks:setup()
status = Status:new({ tag = 'InlineEngine' })
status = Status:new({ tag = 'I' })
setup_keymaps()
setup_keyfilters()
setup_autocmds()
Expand Down
6 changes: 3 additions & 3 deletions lua/fittencode/sessions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ function M.ready_for_generate()
return key_storage:get_key_by_name(current_username) ~= nil
end

---@param task_id integer
---@param timestamp integer
---@param on_success function|nil
---@param on_error function|nil
function M.request_generate_one_stage(task_id, opts, on_success, on_error)
function M.request_generate_one_stage(timestamp, opts, on_success, on_error)
local api_key = key_storage:get_key_by_name(current_username)
if api_key == nil then
-- Log.debug('Key is not found')
Expand All @@ -147,7 +147,7 @@ function M.request_generate_one_stage(task_id, opts, on_success, on_error)
end

client:generate_one_stage(api_key, params, function(generated_text)
schedule(on_success, task_id, prompt, generate_suggestions(generated_text))
schedule(on_success, timestamp, prompt, generate_suggestions(generated_text))
end, on_error)
end

Expand Down
9 changes: 4 additions & 5 deletions lua/fittencode/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ local Log = require('fittencode.log')
---@field get_current function
local M = {}

---@alias StatusCodes table<string, integer>

---@type StatusCodes
---@class StatusCodes
local C = {
DISABLED = 1,
IDLE = 2,
Expand All @@ -25,7 +23,8 @@ local C = {
SUGGESTIONS_READY = 6,
}

M.C = C
---@type StatusCodes
M.C = vim.deepcopy(C)

function M:new(opts)
local obj = {
Expand Down Expand Up @@ -70,7 +69,7 @@ function M:update(status)
-- Force `lualine` to update statusline
-- vim.cmd('redrawstatus')
_force_update_lualine()
Log.debug('{} -> {}', self.tag, name)
Log.debug(self.tag .. ' > ' .. name)
end
self.idle_timer = Base.debounce(self.idle_timer, function()
if vim.tbl_contains(self.filters, self.current) then
Expand Down
117 changes: 0 additions & 117 deletions lua/fittencode/tasks.lua

This file was deleted.