feat: carry recently viewed files as context (#1538)

This commit is contained in:
yetone
2025-03-09 15:42:31 +08:00
committed by GitHub
parent 02fb470140
commit a0e0924730
6 changed files with 21 additions and 2 deletions

View File

@@ -118,6 +118,7 @@ function M.generate_prompts(opts)
code_lang = opts.code_lang,
selected_files = opts.selected_files,
selected_code = opts.selected_code,
recently_viewed_files = opts.recently_viewed_files,
project_context = opts.project_context,
diagnostics = opts.diagnostics,
system_info = system_info,

View File

@@ -2400,6 +2400,7 @@ function Sidebar:create_input_container(opts)
ask = opts.ask or true,
project_context = vim.json.encode(project_context),
selected_files = selected_files_contents,
recently_viewed_files = Utils.get_recent_filepaths(),
diagnostics = vim.json.encode(diagnostics),
history_messages = history_messages,
code_lang = filetype,

View File

@@ -23,3 +23,11 @@
</content>
</selected_code>
{%- endif %}
{% if recently_viewed_files -%}
<recently_viewed_files>
{%- for file in recently_viewed_files %}
{{loop.index}}. {{file}}
{%- endfor %}
</recently_viewed_files>
{%- endif %}

View File

@@ -5,6 +5,7 @@ Tools Usage Guide:
- Please DON'T be so aggressive in using tools, as many tasks can be better completed without tools.
- Files will be provided to you as context through <selected_files> tag!
- Before using the `read_file` tool each time, always repeatedly check whether the file is already in the <selected_files> tag. If it is already there, do not use the `read_file` tool, just read the file content directly from the <selected_files> tag.
- If you use the `read_file` tool when file content is already provided in the <selected_files> tag, you will be fired!
- If the `rag_search` tool exists, prioritize using it to do the search!
- If the `rag_search` tool exists, only use tools like `search_keyword` `search_files` `read_file` `list_files` etc when absolutely necessary!
- Keep the `query` parameter of `rag_search` tool as concise as possible! Try to keep it within five English words!

View File

@@ -298,6 +298,7 @@ vim.g.avante_login = vim.g.avante_login
---@class AvanteTemplateOptions
---@field ask boolean
---@field code_lang string
---@field recently_viewed_files string[] | nil
---@field selected_code AvanteSelectedCode | nil
---@field project_context string | nil
---@field selected_files AvanteSelectedFile[] | nil

View File

@@ -642,15 +642,21 @@ end
-- Get recent filepaths in the same project and same file ext
---@param limit? integer
---@param filenames? string[]
---@param same_file_ext? boolean
---@return string[]
function M.get_recent_filepaths(limit, filenames)
function M.get_recent_filepaths(limit, filenames, same_file_ext)
local project_root = M.get_project_root()
local current_ext = fn.expand("%:e")
local oldfiles = vim.v.oldfiles
local recent_files = {}
for _, file in ipairs(oldfiles) do
if vim.startswith(file, project_root) and M.is_same_file_ext(current_ext, file) then
if vim.startswith(file, project_root) then
local has_ext = file:match("%.%w+$")
if not has_ext then goto continue end
if same_file_ext then
if not M.is_same_file_ext(current_ext, file) then goto continue end
end
if filenames and #filenames > 0 then
for _, filename in ipairs(filenames) do
if file:find(filename) then table.insert(recent_files, file) end
@@ -660,6 +666,7 @@ function M.get_recent_filepaths(limit, filenames)
end
if #recent_files >= (limit or 10) then break end
end
::continue::
end
return recent_files