fix: send the latest file content each time (#1879)

This commit is contained in:
yetone
2025-04-15 20:16:13 +08:00
committed by GitHub
parent fdcfda7437
commit 1fdb1a7b84
9 changed files with 78 additions and 35 deletions

View File

@@ -184,10 +184,26 @@ function M.generate_prompts(opts)
local system_info = Utils.get_system_info() local system_info = Utils.get_system_info()
local selected_files = opts.selected_files or {}
if opts.selected_filepaths then
for _, filepath in ipairs(opts.selected_filepaths) do
local lines, error = Utils.read_file_from_buf_or_disk(filepath)
lines = lines or {}
local filetype = Utils.get_filetype(filepath)
if error ~= nil then
Utils.error("error reading file: " .. error)
else
local content = table.concat(lines, "\n")
table.insert(selected_files, { path = filepath, content = content, file_type = filetype })
end
end
end
local template_opts = { local template_opts = {
ask = opts.ask, -- TODO: add mode without ask instruction ask = opts.ask, -- TODO: add mode without ask instruction
code_lang = opts.code_lang, code_lang = opts.code_lang,
selected_files = opts.selected_files, selected_files = selected_files,
selected_code = opts.selected_code, selected_code = opts.selected_code,
recently_viewed_files = opts.recently_viewed_files, recently_viewed_files = opts.recently_viewed_files,
project_context = opts.project_context, project_context = opts.project_context,

View File

@@ -91,6 +91,32 @@ function M.already_in_context(path)
return false return false
end end
---@param path string
---@param session_ctx table
---@return boolean
function M.already_viewed(path, session_ctx)
local view_history = session_ctx.view_history or {}
local uniform_path = Utils.uniform_path(path)
if view_history[uniform_path] then return true end
return false
end
---@param path string
---@param session_ctx table
function M.mark_as_viewed(path, session_ctx)
local view_history = session_ctx.view_history or {}
local uniform_path = Utils.uniform_path(path)
view_history[uniform_path] = true
session_ctx.view_history = view_history
end
function M.mark_as_not_viewed(path, session_ctx)
local view_history = session_ctx.view_history or {}
local uniform_path = Utils.uniform_path(path)
view_history[uniform_path] = nil
session_ctx.view_history = view_history
end
---@param abs_path string ---@param abs_path string
---@return integer bufnr ---@return integer bufnr
---@return string | nil error ---@return string | nil error

View File

@@ -104,14 +104,17 @@ function M.rename_file(opts, on_log, on_complete)
end end
if Path:new(new_abs_path):exists() then return false, "File already exists: " .. new_abs_path end if Path:new(new_abs_path):exists() then return false, "File already exists: " .. new_abs_path end
if not on_complete then return false, "on_complete not provided" end if not on_complete then return false, "on_complete not provided" end
Helpers.confirm("Are you sure you want to rename the file: " .. abs_path .. " to: " .. new_abs_path, function(ok) Helpers.confirm(
if not ok then "Are you sure you want to rename the file: " .. abs_path .. " to: " .. new_abs_path,
on_complete(false, "User canceled") function(ok, reason)
return if not ok then
on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return
end
os.rename(abs_path, new_abs_path)
on_complete(true, nil)
end end
os.rename(abs_path, new_abs_path) )
on_complete(true, nil)
end)
end end
---@type AvanteLLMToolFunc<{ rel_path: string, new_rel_path: string }> ---@type AvanteLLMToolFunc<{ rel_path: string, new_rel_path: string }>
@@ -137,9 +140,9 @@ function M.delete_file(opts, on_log, on_complete)
if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end
if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end
if not on_complete then return false, "on_complete not provided" end if not on_complete then return false, "on_complete not provided" end
Helpers.confirm("Are you sure you want to delete the file: " .. abs_path, function(ok) Helpers.confirm("Are you sure you want to delete the file: " .. abs_path, function(ok, reason)
if not ok then if not ok then
on_complete(false, "User canceled") on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return return
end end
if on_log then on_log("Deleting file: " .. abs_path) end if on_log then on_log("Deleting file: " .. abs_path) end
@@ -154,9 +157,9 @@ function M.create_dir(opts, on_log, on_complete)
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
if Path:new(abs_path):exists() then return false, "Directory already exists: " .. abs_path end if Path:new(abs_path):exists() then return false, "Directory already exists: " .. abs_path end
if not on_complete then return false, "on_complete not provided" end if not on_complete then return false, "on_complete not provided" end
Helpers.confirm("Are you sure you want to create the directory: " .. abs_path, function(ok) Helpers.confirm("Are you sure you want to create the directory: " .. abs_path, function(ok, reason)
if not ok then if not ok then
on_complete(false, "User canceled") on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return return
end end
if on_log then on_log("Creating directory: " .. abs_path) end if on_log then on_log("Creating directory: " .. abs_path) end
@@ -179,9 +182,9 @@ function M.rename_dir(opts, on_log, on_complete)
if not on_complete then return false, "on_complete not provided" end if not on_complete then return false, "on_complete not provided" end
Helpers.confirm( Helpers.confirm(
"Are you sure you want to rename directory " .. abs_path .. " to " .. new_abs_path .. "?", "Are you sure you want to rename directory " .. abs_path .. " to " .. new_abs_path .. "?",
function(ok) function(ok, reason)
if not ok then if not ok then
on_complete(false, "User canceled") on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return return
end end
if on_log then on_log("Renaming directory: " .. abs_path .. " to " .. new_abs_path) end if on_log then on_log("Renaming directory: " .. abs_path .. " to " .. new_abs_path) end
@@ -198,9 +201,9 @@ function M.delete_dir(opts, on_log, on_complete)
if not Path:new(abs_path):exists() then return false, "Directory not found: " .. abs_path end if not Path:new(abs_path):exists() then return false, "Directory not found: " .. abs_path end
if not Path:new(abs_path):is_dir() then return false, "Path is not a directory: " .. abs_path end if not Path:new(abs_path):is_dir() then return false, "Path is not a directory: " .. abs_path end
if not on_complete then return false, "on_complete not provided" end if not on_complete then return false, "on_complete not provided" end
Helpers.confirm("Are you sure you want to delete the directory: " .. abs_path, function(ok) Helpers.confirm("Are you sure you want to delete the directory: " .. abs_path, function(ok, reason)
if not ok then if not ok then
on_complete(false, "User canceled") on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return return
end end
if on_log then on_log("Deleting directory: " .. abs_path) end if on_log then on_log("Deleting directory: " .. abs_path) end
@@ -462,9 +465,9 @@ function M.git_commit(opts, on_log, on_complete)
if not on_complete then return false, "on_complete not provided" end if not on_complete then return false, "on_complete not provided" end
-- Confirm with user -- Confirm with user
Helpers.confirm("Are you sure you want to commit with message:\n" .. full_commit_msg, function(ok) Helpers.confirm("Are you sure you want to commit with message:\n" .. full_commit_msg, function(ok, reason)
if not ok then if not ok then
on_complete(false, "User canceled") on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return return
end end
-- Stage changes if scope is provided -- Stage changes if scope is provided
@@ -530,9 +533,9 @@ function M.python(opts, on_log, on_complete)
.. abs_path .. abs_path
.. "`?\n" .. "`?\n"
.. opts.code, .. opts.code,
function(ok) function(ok, reason)
if not ok then if not ok then
on_complete(nil, "User canceled") on_complete(nil, "User declined, reason: " .. (reason or "unknown"))
return return
end end
if vim.fn.executable("docker") == 0 then if vim.fn.executable("docker") == 0 then

View File

@@ -84,6 +84,8 @@ function M.func(opts, on_log, on_complete, session_ctx)
return return
end end
vim.api.nvim_buf_set_lines(bufnr, opts.insert_line, opts.insert_line, false, new_lines) vim.api.nvim_buf_set_lines(bufnr, opts.insert_line, opts.insert_line, false, new_lines)
vim.api.nvim_buf_call(bufnr, function() vim.cmd("noautocmd write") end)
if session_ctx then Helpers.mark_as_not_viewed(opts.path, session_ctx) end
on_complete(true, nil) on_complete(true, nil)
end, { focus = true }, session_ctx) end, { focus = true }, session_ctx)
end end

View File

@@ -148,6 +148,7 @@ function M.func(opts, on_log, on_complete, session_ctx)
on_complete(false, "User canceled") on_complete(false, "User canceled")
return return
end end
if session_ctx then Helpers.mark_as_not_viewed(opts.path, session_ctx) end
on_complete(true, nil) on_complete(true, nil)
end, end,
}) })
@@ -163,6 +164,7 @@ function M.func(opts, on_log, on_complete, session_ctx)
end end
vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, new_lines) vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, new_lines)
vim.api.nvim_buf_call(bufnr, function() vim.cmd("noautocmd write") end) vim.api.nvim_buf_call(bufnr, function() vim.cmd("noautocmd write") end)
if session_ctx then Helpers.mark_as_not_viewed(opts.path, session_ctx) end
on_complete(true, nil) on_complete(true, nil)
end, { focus = not Config.behaviour.auto_focus_on_diff_view }, session_ctx) end, { focus = not Config.behaviour.auto_focus_on_diff_view }, session_ctx)
end end

View File

@@ -48,19 +48,15 @@ function M.func(opts, on_log, on_complete, session_ctx)
if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end
local bufnr, err = Helpers.get_bufnr(abs_path) local bufnr, err = Helpers.get_bufnr(abs_path)
if err then return false, err end if err then return false, err end
local current_winid = vim.api.nvim_get_current_win()
local winid = Utils.get_winid(bufnr) 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, reason) Helpers.confirm("Are you sure you want to undo edit this file?", function(ok, reason)
if not ok then if not ok then
on_complete(false, "User declined, reason: " .. (reason or "unknown")) on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return return
end end
vim.api.nvim_set_current_win(winid) vim.api.nvim_win_call(winid, function() vim.cmd("noautocmd undo") end)
-- run undo vim.api.nvim_buf_call(bufnr, function() vim.cmd("noautocmd write") end)
vim.cmd("undo") if session_ctx then Helpers.mark_as_not_viewed(opts.path, session_ctx) end
vim.api.nvim_set_current_win(current_winid)
on_complete(true, nil) on_complete(true, nil)
end, { focus = true }, session_ctx) end, { focus = true }, session_ctx)
end end

View File

@@ -81,14 +81,11 @@ function M.func(opts, on_log, on_complete, session_ctx)
return return
end end
if session_ctx then if session_ctx then
local view_history = session_ctx.view_history or {} if Helpers.already_viewed(opts.path, session_ctx) then
local uniform_path = Utils.uniform_path(opts.path)
if view_history[uniform_path] then
on_complete(nil, "Ooooops! You have already viewed this file! Why you are trying to read it again?") on_complete(nil, "Ooooops! You have already viewed this file! Why you are trying to read it again?")
return return
end end
view_history[uniform_path] = true Helpers.mark_as_viewed(opts.path, session_ctx)
session_ctx.view_history = view_history
end end
local abs_path = Helpers.get_abs_path(opts.path) 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 if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end

View File

@@ -2472,8 +2472,6 @@ function Sidebar:create_input_container(opts)
local project_context = mentions.enable_project_context and file_ext and RepoMap.get_repo_map(file_ext) or nil local project_context = mentions.enable_project_context and file_ext and RepoMap.get_repo_map(file_ext) or nil
local selected_files_contents = self.file_selector:get_selected_files_contents() or {}
local diagnostics = nil local diagnostics = nil
if mentions.enable_diagnostics then if mentions.enable_diagnostics then
if self.code ~= nil and self.code.bufnr ~= nil and self.code.selection ~= nil then if self.code ~= nil and self.code.bufnr ~= nil and self.code.selection ~= nil then
@@ -2530,11 +2528,13 @@ function Sidebar:create_input_container(opts)
if Config.behaviour.enable_claude_text_editor_tool_mode then mode = "claude-text-editor-tool" end if Config.behaviour.enable_claude_text_editor_tool_mode then mode = "claude-text-editor-tool" end
local selected_filepaths = self.file_selector.selected_filepaths or {}
---@type AvanteGeneratePromptsOptions ---@type AvanteGeneratePromptsOptions
local prompts_opts = { local prompts_opts = {
ask = opts.ask or true, ask = opts.ask or true,
project_context = vim.json.encode(project_context), project_context = vim.json.encode(project_context),
selected_files = selected_files_contents, selected_filepaths = selected_filepaths,
recently_viewed_files = Utils.get_recent_filepaths(), recently_viewed_files = Utils.get_recent_filepaths(),
diagnostics = vim.json.encode(diagnostics), diagnostics = vim.json.encode(diagnostics),
history_messages = history_messages, history_messages = history_messages,

View File

@@ -314,6 +314,7 @@ vim.g.avante_login = vim.g.avante_login
---@field selected_code AvanteSelectedCode | nil ---@field selected_code AvanteSelectedCode | nil
---@field project_context string | nil ---@field project_context string | nil
---@field selected_files AvanteSelectedFile[] | nil ---@field selected_files AvanteSelectedFile[] | nil
---@field selected_filepaths string[] | nil
---@field diagnostics string | nil ---@field diagnostics string | nil
---@field history_messages AvanteLLMMessage[] | nil ---@field history_messages AvanteLLMMessage[] | nil
---@field memory string | nil ---@field memory string | nil