fix: do not pick tool use messages when use ReAct (#2333)

This commit is contained in:
yetone
2025-06-25 20:59:35 +08:00
committed by GitHub
parent ad8b24e3bc
commit 5b2a0e6118

View File

@@ -5,7 +5,7 @@ local Split = require("nui.split")
local event = require("nui.utils.autocmd").event local event = require("nui.utils.autocmd").event
local PPath = require("plenary.path") local PPath = require("plenary.path")
local Provider = require("avante.providers") local Providers = require("avante.providers")
local Path = require("avante.path") local Path = require("avante.path")
local Config = require("avante.config") local Config = require("avante.config")
local Diff = require("avante.diff") local Diff = require("avante.diff")
@@ -165,7 +165,7 @@ function Sidebar:open(opts)
end end
if not vim.g.avante_login or vim.g.avante_login == false then if not vim.g.avante_login or vim.g.avante_login == false then
api.nvim_exec_autocmds("User", { pattern = Provider.env.REQUEST_LOGIN_PATTERN }) api.nvim_exec_autocmds("User", { pattern = Providers.env.REQUEST_LOGIN_PATTERN })
vim.g.avante_login = true vim.g.avante_login = true
end end
@@ -2202,6 +2202,8 @@ function Sidebar:get_history_messages_for_api(opts)
if opts.all then return history_messages0 end if opts.all then return history_messages0 end
local use_ReAct_prompt = Providers[Config.provider].use_ReAct_prompt ~= nil
history_messages0 = vim history_messages0 = vim
.iter(history_messages0) .iter(history_messages0)
:filter(function(message) return message.state ~= "generating" end) :filter(function(message) return message.state ~= "generating" end)
@@ -2411,61 +2413,63 @@ function Sidebar:get_history_messages_for_api(opts)
end end
end end
local picked_messages = {} if not use_ReAct_prompt then
local max_tool_use_count = 25 local picked_messages = {}
local tool_use_count = 0 local max_tool_use_count = 25
for idx = #history_messages, 1, -1 do local tool_use_count = 0
local msg = history_messages[idx] for idx = #history_messages, 1, -1 do
if tool_use_count > max_tool_use_count then local msg = history_messages[idx]
if Utils.is_tool_result_message(msg) then if tool_use_count > max_tool_use_count then
local tool_use_message = Utils.get_tool_use_message(msg, history_messages) if Utils.is_tool_result_message(msg) then
if tool_use_message then local tool_use_message = Utils.get_tool_use_message(msg, history_messages)
table.insert( if tool_use_message then
picked_messages, table.insert(
1, picked_messages,
HistoryMessage:new({ 1,
role = "user", HistoryMessage:new({
content = { role = "user",
{ content = {
type = "text", {
text = string.format( type = "text",
"Tool use [%s] is successful: %s", text = string.format(
tool_use_message.message.content[1].name, "Tool use [%s] is successful: %s",
tostring(not msg.message.content[1].is_error) tool_use_message.message.content[1].name,
), tostring(not msg.message.content[1].is_error)
),
},
}, },
}, }, { is_dummy = true })
}, { is_dummy = true }) )
) local msg_content = {}
local msg_content = {} table.insert(msg_content, {
table.insert(msg_content, { type = "text",
type = "text", text = string.format(
text = string.format( "Tool use %s(%s)",
"Tool use %s(%s)", tool_use_message.message.content[1].name,
tool_use_message.message.content[1].name, vim.json.encode(tool_use_message.message.content[1].input)
vim.json.encode(tool_use_message.message.content[1].input) ),
), })
}) table.insert(
table.insert( picked_messages,
picked_messages, 1,
1, HistoryMessage:new({ role = "assistant", content = msg_content }, { is_dummy = true })
HistoryMessage:new({ role = "assistant", content = msg_content }, { is_dummy = true }) )
) end
elseif Utils.is_tool_use_message(msg) then
tool_use_count = tool_use_count + 1
goto continue
else
table.insert(picked_messages, 1, msg)
end end
elseif Utils.is_tool_use_message(msg) then
tool_use_count = tool_use_count + 1
goto continue
else else
if Utils.is_tool_use_message(msg) then tool_use_count = tool_use_count + 1 end
table.insert(picked_messages, 1, msg) table.insert(picked_messages, 1, msg)
end end
else ::continue::
if Utils.is_tool_use_message(msg) then tool_use_count = tool_use_count + 1 end
table.insert(picked_messages, 1, msg)
end end
::continue::
end
history_messages = picked_messages history_messages = picked_messages
end
local final_history_messages = {} local final_history_messages = {}
for _, msg in ipairs(history_messages) do for _, msg in ipairs(history_messages) do