refactor: remove use_xml_format (#1535)

This commit is contained in:
yetone
2025-03-09 14:58:30 +08:00
committed by GitHub
parent 510bf2ff35
commit 868c136574
17 changed files with 75 additions and 102 deletions

View File

@@ -553,7 +553,6 @@ M.BASE_PROVIDER_KEYS = {
"local",
"_shellenv",
"tokenizer_id",
"use_xml_format",
"role_map",
"support_prompt_caching",
"__inherited_from",

View File

@@ -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("<question>%s</question>", instructions) })
else
table.insert(messages, { role = "user", content = string.format("QUESTION:\n%s", instructions) })
end
table.insert(messages, { role = "user", content = string.format("<question>%s</question>", instructions) })
end
local remaining_tokens = max_tokens - Utils.tokens.calculate_tokens(system_prompt)

View File

@@ -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)

View File

@@ -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 = {

View File

@@ -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

View File

@@ -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,

View File

@@ -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

View File

@@ -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,

View File

@@ -1,54 +1,25 @@
{%- if use_xml_format -%}
{% if selected_code -%}
{% for file in selected_files %}
{% if selected_files -%}
<selected_files>
{%- for file in selected_files %}
<file>
<filepath>{{file.path}}</filepath>
<context>
<content>
```{{file.file_type}}
{{file.content}}
```
</context>
{% endfor %}
<code>
```{{code_lang}}
{{selected_code}}
```
</code>
{%- else -%}
{% for file in selected_files %}
<filepath>{{file.path}}</filepath>
<code>
```{{file.file_type}}
{{file.content}}
```
</code>
{% endfor %}
</content>
</file>
{%- endfor %}
</selected_files>
{%- endif %}
{% else %}
{% if selected_code -%}
{% for file in selected_files %}
FILEPATH: {{file.path}}
CONTEXT:
```{{file.file_type}}
{{file.content}}
<selected_code>
<filepath>{{selected_code.path}}</filepath>
<content>
```{{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 %}
</content>
</selected_code>
{%- endif %}

View File

@@ -1,4 +1,3 @@
{%- if use_xml_format -%}
{%- if diagnostics -%}
<diagnostic_field_description>
content: The diagnostic content
@@ -11,16 +10,3 @@ source: The source of the diagnostic
{{diagnostics}}
</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 %}

View File

@@ -1,12 +1,5 @@
{%- if use_xml_format -%}
{%- if project_context -%}
<project_context>
{{project_context}}
</project_context>
{%- endif %}
{%- else -%}
{%- if project_context -%}
PROJECT CONTEXT:
{{project_context}}
{%- endif %}
{%- endif %}

View File

@@ -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 <filepath> and <code> 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 <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 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

@@ -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')"

View File

@@ -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 <code>, DO NOT change the <context>!
{% 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.

View File

@@ -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

View File

@@ -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")