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 Action #62

Merged
merged 5 commits into from
May 18, 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
109 changes: 60 additions & 49 deletions lua/fittencode/engines/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ local function chain_actions(action, solved_prefix, on_error)
else
elapsed_time = elapsed_time + ms
depth = depth + 1
chat:commit(lines)
chat:commit(lines, true)
local new_solved_prefix = prompt.prefix .. table.concat(lines, '\n') .. '\n'
chain_actions(action, new_solved_prefix, on_error)
end
Expand Down Expand Up @@ -245,6 +245,58 @@ local function make_range(buffer)
return range
end

local function make_filetype(buffer, range)
local filetype = api.nvim_get_option_value('filetype', { buf = buffer })
Log.debug('Action option filetype: {}', filetype)
local langs = get_tslangs(buffer, range)
Log.debug('Action langs: {}', langs)
-- Markdown contains blocks of code
-- JS or CSS is embedded in the HTML
if #langs >= 2 then
filetype = vim.tbl_filter(function(lang) return lang ~= filetype end, langs)[1]
end
return filetype
end

local function _start_action(action, prompt_opts)
Promise:new(function(resolve, reject)
local task_id = tasks:create(0, 0)
Sessions.request_generate_one_stage(task_id, prompt_opts, function(_, prompt, suggestions)
-- Log.debug('Suggestions for Actions: {}', suggestions)
local lines, ms = filter_suggestions(task_id, suggestions)
elapsed_time = elapsed_time + ms
if not lines or #lines == 0 then
reject()
else
depth = depth + 1
chat:commit(lines, true)
local solved_prefix = prompt.prefix .. table.concat(lines, '\n') .. '\n'
resolve(solved_prefix)
end
end, function(err)
reject(err)
end)
end):forward(function(solved_prefix)
chain_actions(action, solved_prefix, on_error)
end, function(err)
schedule(on_error, err)
end
)
end

local function chat_commit_inout(action_name, prompt_opts, range)
local prompt_preview = PromptProviders.get_prompt_one(prompt_opts)
if #prompt_preview.filename == 0 then
prompt_preview.filename = 'unnamed'
end
local source_info = ' (' .. prompt_preview.filename .. ' ' .. range.start[1] .. ':' .. range['end'][1] .. ')'
local c_in = '# In`[' .. current_eval .. ']`:= ' .. action_name .. source_info
chat:commit(c_in)
chat:commit(prompt_preview.content)
local c_out = '# Out`[' .. current_eval .. ']`='
chat:commit(c_out)
end

---@param action number
---@param opts? ActionOptions
---@return nil
Expand All @@ -253,10 +305,9 @@ function ActionsEngine.start_action(action, opts)

local action_name = get_action_name(action)
if not action_name then
Log.error('Invalid action: {}', action)
Log.error('Invalid Action: {}', action)
return
end

Log.debug('Start Action({})...', action_name)

if lock then
Expand All @@ -273,21 +324,13 @@ function ActionsEngine.start_action(action, opts)
local window = api.nvim_get_current_win()
local buffer = api.nvim_win_get_buf(window)

local range = make_range(buffer)
Log.debug('Action range: {}', range)

chat:show()
fn.win_gotoid(window)

local filetype = api.nvim_get_option_value('filetype', { buf = buffer })
Log.debug('Action filetype: {}', filetype)
local langs = get_tslangs(buffer, range)
Log.debug('Action langs: {}', langs)
-- Markdown embeded code block
-- HTML embeded js or css
if #langs >= 2 then
filetype = vim.tbl_filter(function(lang) return lang ~= filetype end, langs)[1]
end
local range = make_range(buffer)
Log.debug('Action range: {}', range)

local filetype = make_filetype(buffer, range)
Log.debug('Action real filetype: {}', filetype)

local prompt_opts = {
Expand All @@ -301,41 +344,9 @@ function ActionsEngine.start_action(action, opts)
prompt = opts and opts.prompt,
action_opts = opts,
}
local prompt_preview = PromptProviders.get_prompt_one(prompt_opts)
if #prompt_preview.filename == 0 then
prompt_preview.filename = 'unnamed'
end
local source_info = ' (' .. prompt_preview.filename .. ' ' .. range.start[1] .. ':' .. range['end'][1] .. ')'

local c_in = '# In`[' .. current_eval .. ']`:= ' .. action_name .. source_info
chat:commit(c_in)
chat:commit(prompt_preview.content)
local c_out = '# Out`[' .. current_eval .. ']`='
chat:commit(c_out)

Promise:new(function(resolve, reject)
local task_id = tasks:create(0, 0)
Sessions.request_generate_one_stage(task_id, prompt_opts, function(_, prompt, suggestions)
-- Log.debug('Suggestions for Actions: {}', suggestions)
local lines, ms = filter_suggestions(task_id, suggestions)
elapsed_time = elapsed_time + ms
if not lines or #lines == 0 then
reject()
else
depth = depth + 1
chat:commit(lines, true)
local solved_prefix = prompt.prefix .. table.concat(lines, '\n') .. '\n'
resolve(solved_prefix)
end
end, function(err)
reject(err)
end)
end):forward(function(solved_prefix)
chain_actions(action, solved_prefix, on_error)
end, function(err)
schedule(on_error, err)
end
)
chat_commit_inout(action_name, prompt_opts, range)
_start_action(action, prompt_opts)
end

---@param opts? ActionOptions
Expand Down
2 changes: 1 addition & 1 deletion lua/fittencode/views/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function M:commit(text, linebreak)
return
end
if linebreak and #self.text > 0 and #lines > 0 then
if lines[1] ~= '' and not string.match(lines[1], '^```') and self.text[#self.text] ~= '' and not string.match(self.text[#self.text], '^```') then
if lines[1] ~= '' and self.text[#self.text] ~= '' then
table.insert(lines, 1, '')
end
end
Expand Down