From 868c13657442b799a5c161940602f99623a08197 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 9 Mar 2025 14:58:30 +0800 Subject: [PATCH] refactor: remove use_xml_format (#1535) --- crates/avante-templates/src/lib.rs | 15 ++++- lua/avante/config.lua | 1 - lua/avante/llm.lua | 7 +- lua/avante/providers/bedrock.lua | 1 - lua/avante/providers/claude.lua | 1 - lua/avante/providers/init.lua | 2 - lua/avante/selection.lua | 13 +++- lua/avante/selection_result.lua | 8 ++- lua/avante/sidebar.lua | 14 ++-- lua/avante/templates/_context.avanterules | 65 +++++-------------- lua/avante/templates/_diagnostics.avanterules | 14 ---- lua/avante/templates/_project.avanterules | 7 -- .../templates/_tools-guidelines.avanterules | 4 +- lua/avante/templates/base.avanterules | 1 - lua/avante/templates/planning.avanterules | 4 -- lua/avante/types.lua | 13 ++-- lua/avante/utils/init.lua | 7 +- 17 files changed, 75 insertions(+), 102 deletions(-) diff --git a/crates/avante-templates/src/lib.rs b/crates/avante-templates/src/lib.rs index d04fa46..ee62381 100644 --- a/crates/avante-templates/src/lib.rs +++ b/crates/avante-templates/src/lib.rs @@ -15,6 +15,13 @@ impl State<'_> { } } +#[derive(Debug, Serialize, Deserialize)] +struct SelectedCode { + path: String, + content: Option, + file_type: String, +} + #[derive(Debug, Serialize, Deserialize)] struct SelectedFile { path: String, @@ -24,11 +31,12 @@ struct SelectedFile { #[derive(Debug, Serialize, Deserialize)] struct TemplateContext { - use_xml_format: bool, ask: bool, code_lang: String, selected_files: Option>, - selected_code: Option, + selected_code: Option, + recently_viewed_files: Option>, + relevant_files: Option>, project_context: Option, diagnostics: Option, system_info: Option, @@ -50,11 +58,12 @@ fn render(state: &State, template: &str, context: TemplateContext) -> LuaResult< Ok(jinja_template .render(context! { - use_xml_format => context.use_xml_format, ask => context.ask, code_lang => context.code_lang, selected_files => context.selected_files, selected_code => context.selected_code, + recently_viewed_files => context.recently_viewed_files, + relevant_files => context.relevant_files, project_context => context.project_context, diagnostics => context.diagnostics, system_info => context.system_info, diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 6b062c0..283709b 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -553,7 +553,6 @@ M.BASE_PROVIDER_KEYS = { "local", "_shellenv", "tokenizer_id", - "use_xml_format", "role_map", "support_prompt_caching", "__inherited_from", diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index a7ad083..5ebf80a 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -114,7 +114,6 @@ function M.generate_prompts(opts) local system_info = Utils.get_system_info() local template_opts = { - use_xml_format = provider.use_xml_format, ask = opts.ask, -- TODO: add mode without ask instruction code_lang = opts.code_lang, selected_files = opts.selected_files, @@ -156,11 +155,7 @@ function M.generate_prompts(opts) end if instructions then - if opts.use_xml_format then - table.insert(messages, { role = "user", content = string.format("%s", instructions) }) - else - table.insert(messages, { role = "user", content = string.format("QUESTION:\n%s", instructions) }) - end + table.insert(messages, { role = "user", content = string.format("%s", instructions) }) end local remaining_tokens = max_tokens - Utils.tokens.calculate_tokens(system_prompt) diff --git a/lua/avante/providers/bedrock.lua b/lua/avante/providers/bedrock.lua index 1712335..355882f 100644 --- a/lua/avante/providers/bedrock.lua +++ b/lua/avante/providers/bedrock.lua @@ -5,7 +5,6 @@ local P = require("avante.providers") local M = {} M.api_key_name = "BEDROCK_KEYS" -M.use_xml_format = true M = setmetatable(M, { __index = function(_, k) diff --git a/lua/avante/providers/claude.lua b/lua/avante/providers/claude.lua index ce64a83..84a6d81 100644 --- a/lua/avante/providers/claude.lua +++ b/lua/avante/providers/claude.lua @@ -29,7 +29,6 @@ end local M = {} M.api_key_name = "ANTHROPIC_API_KEY" -M.use_xml_format = true M.support_prompt_caching = true M.role_map = { diff --git a/lua/avante/providers/init.lua b/lua/avante/providers/init.lua index d941d56..c2b3a77 100644 --- a/lua/avante/providers/init.lua +++ b/lua/avante/providers/init.lua @@ -224,8 +224,6 @@ M = setmetatable(M, { -- default to gpt-4o as tokenizer if t[k].tokenizer_id == nil then t[k].tokenizer_id = "gpt-4o" end - if t[k].use_xml_format == nil then t[k].use_xml_format = true end - if t[k].is_env_set == nil then t[k].is_env_set = function() return E.parse_envvar(t[k]) ~= nil end end if t[k].setup == nil then diff --git a/lua/avante/selection.lua b/lua/avante/selection.lua index 2036488..6483c67 100644 --- a/lua/avante/selection.lua +++ b/lua/avante/selection.lua @@ -220,13 +220,24 @@ function Selection:create_editing_input() local diagnostics = Utils.get_current_selection_diagnostics(code_bufnr, self.selection) + ---@type AvanteSelectedCode | nil + local selected_code = nil + + if self.selection then + selected_code = { + content = self.selection.content, + file_type = self.selection.filetype, + path = self.selection.filepath, + } + end + Llm.stream({ ask = true, project_context = vim.json.encode(project_context), diagnostics = vim.json.encode(diagnostics), selected_files = { { content = code_content, file_type = filetype, path = "" } }, code_lang = filetype, - selected_code = self.selection.content, + selected_code = selected_code, instructions = input, mode = "editing", on_start = on_start, diff --git a/lua/avante/selection_result.lua b/lua/avante/selection_result.lua index 452b4b8..051670c 100644 --- a/lua/avante/selection_result.lua +++ b/lua/avante/selection_result.lua @@ -1,4 +1,6 @@ ---@class avante.SelectionResult +---@field filepath string Filepath of the selected content +---@field filetype string Filetype of the selected content ---@field content string Selected content ---@field range avante.Range Selection range local SelectionResult = {} @@ -7,8 +9,10 @@ SelectionResult.__index = SelectionResult -- Create a selection content and range ---@param content string Selected content ---@param range avante.Range Selection range -function SelectionResult:new(content, range) - local instance = setmetatable({}, SelectionResult) +function SelectionResult:new(filepath, filetype, content, range) + local instance = setmetatable({}, self) + instance.filepath = filepath + instance.filetype = filetype instance.content = content instance.range = range return instance diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 50fb18c..9333571 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -166,7 +166,6 @@ end function Sidebar:focus_input() if self.input_container and self.input_container.winid and api.nvim_win_is_valid(self.input_container.winid) then api.nvim_set_current_win(self.input_container.winid) - api.nvim_feedkeys("i", "n", false) end end @@ -2357,8 +2356,15 @@ function Sidebar:create_input_container(opts) local function get_generate_prompts_options(request, summarize_memory, cb) local filetype = api.nvim_get_option_value("filetype", { buf = self.code.bufnr }) - local selected_code_content = nil - if self.code.selection ~= nil then selected_code_content = self.code.selection.content end + ---@type AvanteSelectedCode | nil + local selected_code = nil + if self.code.selection ~= nil then + selected_code = { + path = self.code.selection.filepath, + file_type = self.code.selection.filetype, + content = self.code.selection.content, + } + end local mentions = Utils.extract_mentions(request) request = mentions.new_content @@ -2397,7 +2403,7 @@ function Sidebar:create_input_container(opts) diagnostics = vim.json.encode(diagnostics), history_messages = history_messages, code_lang = filetype, - selected_code = selected_code_content, + selected_code = selected_code, instructions = request, mode = Config.behaviour.enable_cursor_planning_mode and "cursor-planning" or "planning", tools = tools, diff --git a/lua/avante/templates/_context.avanterules b/lua/avante/templates/_context.avanterules index 6639719..77b2f81 100644 --- a/lua/avante/templates/_context.avanterules +++ b/lua/avante/templates/_context.avanterules @@ -1,54 +1,25 @@ -{%- if use_xml_format -%} - -{% if selected_code -%} -{% for file in selected_files %} +{% if selected_files -%} + +{%- for file in selected_files %} + {{file.path}} - - + ```{{file.file_type}} {{file.content}} ``` - -{% endfor %} - - -```{{code_lang}} -{{selected_code}} -``` - -{%- else -%} -{% for file in selected_files %} -{{file.path}} - - -```{{file.file_type}} -{{file.content}} -``` - -{% endfor %} + + +{%- endfor %} + {%- endif %} -{% else %} + {% if selected_code -%} -{% for file in selected_files %} -FILEPATH: {{file.path}} - -CONTEXT: -```{{file.file_type}} -{{file.content}} + +{{selected_code.path}} + +```{{selected_code.file_type}} +{{selected_code.content}} ``` -{% endfor %} - -CODE: -```{{code_lang}} -{{selected_code}} -``` -{%- else -%} -{% for file in selected_files %} -FILEPATH: {{file.path}} - -CODE: -```{{file.file_type}} -{{file.content}} -``` -{% endfor %} -{%- endif %}{%- endif %} + + +{%- endif %} diff --git a/lua/avante/templates/_diagnostics.avanterules b/lua/avante/templates/_diagnostics.avanterules index 5d14ea4..4ccc9d9 100644 --- a/lua/avante/templates/_diagnostics.avanterules +++ b/lua/avante/templates/_diagnostics.avanterules @@ -1,4 +1,3 @@ -{%- if use_xml_format -%} {%- if diagnostics -%} content: The diagnostic content @@ -11,16 +10,3 @@ source: The source of the diagnostic {{diagnostics}} {%- endif %} -{%- else -%} -{%- if diagnostics -%} -DIAGNOSTIC_FIELD_DESCRIPTION: -content: The diagnostic content -start_line: The starting line of the diagnostic (1-indexed) -end_line: The final line of the diagnostic (1-indexed) -severity: The severity of the diagnostic -source: The source of the diagnostic - -DIAGNOSTICS: -{{diagnostics}} -{%- endif %} -{%- endif %} diff --git a/lua/avante/templates/_project.avanterules b/lua/avante/templates/_project.avanterules index c92c579..6681f05 100644 --- a/lua/avante/templates/_project.avanterules +++ b/lua/avante/templates/_project.avanterules @@ -1,12 +1,5 @@ -{%- if use_xml_format -%} {%- if project_context -%} {{project_context}} {%- endif %} -{%- else -%} -{%- if project_context -%} -PROJECT CONTEXT: -{{project_context}} -{%- endif %} -{%- endif %} diff --git a/lua/avante/templates/_tools-guidelines.avanterules b/lua/avante/templates/_tools-guidelines.avanterules index dd57951..787bd94 100644 --- a/lua/avante/templates/_tools-guidelines.avanterules +++ b/lua/avante/templates/_tools-guidelines.avanterules @@ -3,8 +3,8 @@ Don't directly search for code context in historical messages. Instead, prioriti Tools Usage Guide: - You have access to tools, but only use them when necessary. If a tool is not required, respond as normal. - 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 and tags! - - If you need to read a file that is already in the context, do not use the `read_file` tool again; directly use the content from the context. + - Files will be provided to you as context through tag! + - Before using the `read_file` tool each time, always repeatedly check whether the file is already in the tag. If it is already there, do not use the `read_file` tool, just read the file content directly from the tag. - 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! diff --git a/lua/avante/templates/base.avanterules b/lua/avante/templates/base.avanterules index 2fa5980..0818510 100644 --- a/lua/avante/templates/base.avanterules +++ b/lua/avante/templates/base.avanterules @@ -1,7 +1,6 @@ {# Uses https://mitsuhiko.github.io/minijinja-playground/ for testing: { "ask": true, - "use_xml_format": true, "question": "Refactor to include tab flow", "code_lang": "lua", "file_content": "local Config = require('avante.config')" diff --git a/lua/avante/templates/planning.avanterules b/lua/avante/templates/planning.avanterules index aca6c69..456f8f5 100644 --- a/lua/avante/templates/planning.avanterules +++ b/lua/avante/templates/planning.avanterules @@ -137,11 +137,7 @@ Keep *SEARCH/REPLACE* blocks concise. Break large *SEARCH/REPLACE* blocks into a series of smaller blocks that each change a small portion of the file. Include just the changing lines, and a few surrounding lines if needed for uniqueness. Do not include long runs of unchanging lines in *SEARCH/REPLACE* blocks. -{% if use_xml_format -%} ONLY change the , DO NOT change the ! -{% else -%} -ONLY change the CODE, DO NOT change the CONTEXT! -{% endif %} Only create *SEARCH/REPLACE* blocks for files that the user has added to the chat! To move code within a file, use 2 *SEARCH/REPLACE* blocks: 1 to delete it from its current location, 1 to insert it in the new location. diff --git a/lua/avante/types.lua b/lua/avante/types.lua index 90d400a..c65e57d 100644 --- a/lua/avante/types.lua +++ b/lua/avante/types.lua @@ -266,7 +266,6 @@ vim.g.avante_login = vim.g.avante_login ---@field is_env_set fun(): boolean ---@field api_key_name string ---@field tokenizer_id string | "gpt-4o" ----@field use_xml_format boolean ---@field model? string ---@field parse_api_key fun(): string | nil ---@field parse_stream_data? AvanteStreamParser @@ -286,18 +285,22 @@ vim.g.avante_login = vim.g.avante_login --- ---@alias AvanteLlmMode "planning" | "editing" | "suggesting" | "cursor-planning" | "cursor-applying" --- ----@class AvanteSelectedFiles +---@class AvanteSelectedCode +---@field path string +---@field content string +---@field file_type string +--- +---@class AvanteSelectedFile ---@field path string ---@field content string ---@field file_type string --- ---@class AvanteTemplateOptions ----@field use_xml_format boolean | nil ---@field ask boolean ---@field code_lang string ----@field selected_code string | nil +---@field selected_code AvanteSelectedCode | nil ---@field project_context string | nil ----@field selected_files AvanteSelectedFiles[] | nil +---@field selected_files AvanteSelectedFile[] | nil ---@field diagnostics string | nil ---@field history_messages AvanteLLMMessage[] | nil ---@field memory string | nil diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index d3aeaf1..03e0d50 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -275,8 +275,10 @@ function M.get_visual_selection_and_range() end end if not content then return nil end + local filepath = fn.expand("%:p") + local filetype = M.get_filetype(filepath) -- Return the selected content and range - return SelectionResult:new(content, range) + return SelectionResult:new(filepath, filetype, content, range) end ---Wrapper around `api.nvim_buf_get_lines` which defaults to the current buffer @@ -637,6 +639,9 @@ function M.is_same_file_ext(target_ext, filepath) end -- Get recent filepaths in the same project and same file ext +---@param limit? integer +---@param filenames? string[] +---@return string[] function M.get_recent_filepaths(limit, filenames) local project_root = M.get_project_root() local current_ext = fn.expand("%:e")