Skip to content

Commit

Permalink
Refactor Task
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jun 28, 2024
1 parent 8d57737 commit d002a04
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 174 deletions.
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
2 changes: 1 addition & 1 deletion lua/fittencode/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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.

0 comments on commit d002a04

Please sign in to comment.