feat: accept all (#1764)

This commit is contained in:
yetone
2025-03-30 12:51:15 +08:00
committed by GitHub
parent edf02a7d1f
commit 76fd19812e
10 changed files with 182 additions and 79 deletions

View File

@@ -219,7 +219,7 @@ M.returns = {
}
---@type AvanteLLMToolFunc<{ rel_path: string, command: string }>
function M.func(opts, on_log, on_complete)
function M.func(opts, on_log, on_complete, session_ctx)
local abs_path = Helpers.get_abs_path(opts.rel_path)
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
if not Path:new(abs_path):exists() then return false, "Path not found: " .. abs_path end
@@ -248,7 +248,9 @@ function M.func(opts, on_log, on_complete)
local result, err = handle_result(output, exit_code)
on_complete(result, err)
end, abs_path)
end
end,
{ focus = true },
session_ctx
)
end

View File

@@ -45,7 +45,7 @@ M.returns = {
}
---@type AvanteLLMToolFunc<{ path: string, file_text: string }>
function M.func(opts, on_log, on_complete)
function M.func(opts, on_log, on_complete, session_ctx)
if not on_complete then return false, "on_complete not provided" end
if on_log then on_log("path: " .. opts.path) end
if Helpers.already_in_context(opts.path) then
@@ -60,11 +60,11 @@ function M.func(opts, on_log, on_complete)
local bufnr, err = Helpers.get_bufnr(abs_path)
if err then return false, err end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
Helpers.confirm("Are you sure you want to create this file?", function(ok)
Helpers.confirm("Are you sure you want to create this file?", function(ok, reason)
if not ok then
-- close the buffer
vim.api.nvim_buf_delete(bufnr, { force = true })
on_complete(false, "User canceled")
on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return
end
-- save the file
@@ -75,7 +75,7 @@ function M.func(opts, on_log, on_complete)
vim.cmd("write")
vim.api.nvim_set_current_win(current_winid)
on_complete(true, nil)
end)
end, { focus = true }, session_ctx)
end
return M

View File

@@ -21,10 +21,15 @@ function M.get_abs_path(rel_path)
end
---@param message string
---@param callback fun(yes: boolean)
---@param opts? { focus?: boolean }
---@param callback fun(yes: boolean, reason?: string)
---@param confirm_opts? { focus?: boolean }
---@param session_ctx? table
---@return avante.ui.Confirm | nil
function M.confirm(message, callback, opts)
function M.confirm(message, callback, confirm_opts, session_ctx)
if session_ctx and session_ctx.always_yes then
callback(true)
return
end
local Confirm = require("avante.ui.confirm")
local sidebar = require("avante").get()
if not sidebar or not sidebar.input_container or not sidebar.input_container.winid then
@@ -32,8 +37,22 @@ function M.confirm(message, callback, opts)
callback(false)
return
end
local confirm_opts = vim.tbl_deep_extend("force", { container_winid = sidebar.input_container.winid }, opts or {})
M.confirm_popup = Confirm:new(message, callback, confirm_opts)
confirm_opts = vim.tbl_deep_extend("force", { container_winid = sidebar.input_container.winid }, confirm_opts or {})
M.confirm_popup = Confirm:new(message, function(type, reason)
if type == "yes" then
callback(true)
return
end
if type == "all" then
if session_ctx then session_ctx.always_yes = true end
callback(true)
return
end
if type == "no" then
callback(false, reason)
return
end
end, confirm_opts)
M.confirm_popup:open()
return M.confirm_popup
end

View File

@@ -42,11 +42,17 @@ function M.str_replace_editor(opts, on_log, on_complete, session_ctx)
return view(opts_, on_log, on_complete, session_ctx)
end
if opts.command == "str_replace" then
return require("avante.llm_tools.str_replace").func(opts, on_log, on_complete)
return require("avante.llm_tools.str_replace").func(opts, on_log, on_complete, session_ctx)
end
if opts.command == "create" then
return require("avante.llm_tools.create").func(opts, on_log, on_complete, session_ctx)
end
if opts.command == "insert" then
return require("avante.llm_tools.insert").func(opts, on_log, on_complete, session_ctx)
end
if opts.command == "undo_edit" then
return require("avante.llm_tools.undo_edit").func(opts, on_log, on_complete, session_ctx)
end
if opts.command == "create" then return require("avante.llm_tools.create").func(opts, on_log, on_complete) end
if opts.command == "insert" then return require("avante.llm_tools.insert").func(opts, on_log, on_complete) end
if opts.command == "undo_edit" then return require("avante.llm_tools.undo_edit").func(opts, on_log, on_complete) end
return false, "Unknown command: " .. opts.command
end

View File

@@ -50,7 +50,7 @@ M.returns = {
}
---@type AvanteLLMToolFunc<{ path: string, insert_line: integer, new_str: string }>
function M.func(opts, on_log, on_complete)
function M.func(opts, on_log, on_complete, session_ctx)
if on_log then on_log("path: " .. opts.path) end
local abs_path = Helpers.get_abs_path(opts.path)
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
@@ -77,15 +77,15 @@ function M.func(opts, on_log, on_complete)
hl_eol = true,
hl_mode = "combine",
})
Helpers.confirm("Are you sure you want to insert these lines?", function(ok)
Helpers.confirm("Are you sure you want to insert these lines?", function(ok, reason)
clear_highlights()
if not ok then
on_complete(false, "User canceled")
on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return
end
vim.api.nvim_buf_set_lines(bufnr, opts.insert_line, opts.insert_line, false, new_lines)
on_complete(true, nil)
end)
end, { focus = true }, session_ctx)
end
return M

View File

@@ -52,7 +52,7 @@ M.returns = {
}
---@type AvanteLLMToolFunc<{ path: string, old_str: string, new_str: string }>
function M.func(opts, on_log, on_complete)
function M.func(opts, on_log, on_complete, session_ctx)
if on_log then on_log("path: " .. opts.path) end
local abs_path = Helpers.get_abs_path(opts.path)
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
@@ -135,21 +135,8 @@ function M.func(opts, on_log, on_complete)
vim.cmd("normal! zz")
vim.api.nvim_set_current_win(current_winid)
local augroup = vim.api.nvim_create_augroup("avante_str_replace_editor", { clear = true })
local confirm = Helpers.confirm("Are you sure you want to apply this modification?", function(ok)
pcall(vim.api.nvim_del_augroup_by_id, augroup)
vim.api.nvim_set_current_win(sidebar.code.winid)
vim.cmd("noautocmd stopinsert")
vim.cmd("noautocmd undo")
if not ok then
vim.api.nvim_set_current_win(current_winid)
on_complete(false, "User canceled")
return
end
vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, new_lines)
vim.api.nvim_set_current_win(current_winid)
on_complete(true, nil)
end, { focus = false })
vim.api.nvim_set_current_win(sidebar.code.winid)
local confirm
vim.api.nvim_create_autocmd({ "TextChangedI", "TextChanged" }, {
group = augroup,
buffer = bufnr,
@@ -167,6 +154,20 @@ function M.func(opts, on_log, on_complete)
on_complete(true, nil)
end,
})
confirm = Helpers.confirm("Are you sure you want to apply this modification?", function(ok, reason)
pcall(vim.api.nvim_del_augroup_by_id, augroup)
vim.api.nvim_set_current_win(sidebar.code.winid)
vim.cmd("noautocmd stopinsert")
vim.cmd("noautocmd undo")
if not ok then
vim.api.nvim_set_current_win(current_winid)
on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return
end
vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, new_lines)
vim.api.nvim_set_current_win(current_winid)
on_complete(true, nil)
end, { focus = false }, session_ctx)
end
return M

View File

@@ -40,7 +40,7 @@ M.returns = {
}
---@type AvanteLLMToolFunc<{ path: string }>
function M.func(opts, on_log, on_complete)
function M.func(opts, on_log, on_complete, session_ctx)
if on_log then on_log("path: " .. opts.path) end
local abs_path = Helpers.get_abs_path(opts.path)
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
@@ -52,9 +52,9 @@ function M.func(opts, on_log, on_complete)
local winid = Utils.get_winid(bufnr)
vim.api.nvim_set_current_win(winid)
vim.api.nvim_set_current_win(current_winid)
Helpers.confirm("Are you sure you want to undo edit this file?", function(ok)
Helpers.confirm("Are you sure you want to undo edit this file?", function(ok, reason)
if not ok then
on_complete(false, "User canceled")
on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return
end
vim.api.nvim_set_current_win(winid)
@@ -62,7 +62,7 @@ function M.func(opts, on_log, on_complete)
vim.cmd("undo")
vim.api.nvim_set_current_win(current_winid)
on_complete(true, nil)
end)
end, { focus = true }, session_ctx)
end
return M