fix: tool result message must be adjacent to the tool use message (#2096)
This commit is contained in:
@@ -401,59 +401,16 @@ function M.generate_prompts(opts)
|
|||||||
|
|
||||||
local final_history_messages = {}
|
local final_history_messages = {}
|
||||||
if cleaned_history_messages then
|
if cleaned_history_messages then
|
||||||
if opts.disable_compact_history_messages then
|
for _, msg in ipairs(cleaned_history_messages) do
|
||||||
for i, msg in ipairs(cleaned_history_messages) do
|
local tool_result_message
|
||||||
if Utils.is_tool_use_message(msg) then
|
if Utils.is_tool_use_message(msg) then
|
||||||
local next_msg = cleaned_history_messages[i + 1]
|
tool_result_message = Utils.get_tool_result_message(msg, cleaned_history_messages)
|
||||||
if not next_msg or not Utils.is_tool_result_message(next_msg) then goto continue end
|
if not tool_result_message then goto continue end
|
||||||
if next_msg.message.content[1].tool_use_id ~= msg.message.content[1].id then goto continue end
|
|
||||||
end
|
|
||||||
if Utils.is_tool_result_message(msg) and not Utils.get_tool_use_message(msg, cleaned_history_messages) then
|
|
||||||
goto continue
|
|
||||||
end
|
|
||||||
table.insert(final_history_messages, msg)
|
|
||||||
::continue::
|
|
||||||
end
|
end
|
||||||
else
|
if Utils.is_tool_result_message(msg) then goto continue end
|
||||||
if Config.history.max_tokens > 0 then
|
table.insert(final_history_messages, msg)
|
||||||
remaining_tokens = math.min(Config.history.max_tokens, remaining_tokens)
|
if tool_result_message then table.insert(final_history_messages, tool_result_message) end
|
||||||
end
|
::continue::
|
||||||
|
|
||||||
-- Traverse the history in reverse, keeping only the latest history until the remaining tokens are exhausted and the first message role is "user"
|
|
||||||
local retained_history_messages = {}
|
|
||||||
for i = #cleaned_history_messages, 1, -1 do
|
|
||||||
local message = cleaned_history_messages[i]
|
|
||||||
local tokens = Utils.tokens.calculate_tokens(message.message.content)
|
|
||||||
remaining_tokens = remaining_tokens - tokens
|
|
||||||
if remaining_tokens > 0 then
|
|
||||||
table.insert(retained_history_messages, 1, message)
|
|
||||||
else
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if #retained_history_messages == 0 then
|
|
||||||
retained_history_messages =
|
|
||||||
vim.list_slice(cleaned_history_messages, #cleaned_history_messages - 1, #cleaned_history_messages)
|
|
||||||
end
|
|
||||||
|
|
||||||
pending_compaction_history_messages =
|
|
||||||
vim.list_slice(cleaned_history_messages, 1, #cleaned_history_messages - #retained_history_messages)
|
|
||||||
|
|
||||||
pending_compaction_history_messages = vim
|
|
||||||
.iter(pending_compaction_history_messages)
|
|
||||||
:filter(function(msg) return msg.is_dummy ~= true end)
|
|
||||||
:totable()
|
|
||||||
|
|
||||||
vim.iter(retained_history_messages):each(function(msg)
|
|
||||||
if Utils.is_tool_use_message(msg) and not Utils.get_tool_result_message(msg, retained_history_messages) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if Utils.is_tool_result_message(msg) and not Utils.get_tool_use_message(msg, retained_history_messages) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
table.insert(final_history_messages, msg)
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ function M.func(opts, on_log, on_complete, session_ctx)
|
|||||||
just_for_display = true,
|
just_for_display = true,
|
||||||
})
|
})
|
||||||
sidebar:add_history_messages({ message })
|
sidebar:add_history_messages({ message })
|
||||||
if opts.command then
|
if opts.command and opts.command ~= "" and opts.command ~= vim.NIL then
|
||||||
require("avante.llm_tools.bash").func({ command = opts.command }, on_log, on_complete, session_ctx)
|
require("avante.llm_tools.bash").func({ command = opts.command }, on_log, on_complete, session_ctx)
|
||||||
else
|
else
|
||||||
on_complete(true, nil)
|
on_complete(true, nil)
|
||||||
|
|||||||
@@ -1411,19 +1411,13 @@ function M.get_tool_use_message(message, messages)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not tool_id then return nil end
|
if not tool_id then return nil end
|
||||||
local idx = nil
|
for idx_ = #messages, 1, -1 do
|
||||||
for idx_, message_ in ipairs(messages) do
|
|
||||||
if message_.uuid == message.uuid then
|
|
||||||
idx = idx_
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if not idx then return nil end
|
|
||||||
for idx_ = idx - 1, 1, -1 do
|
|
||||||
local message_ = messages[idx_]
|
local message_ = messages[idx_]
|
||||||
local content_ = message_.message.content
|
local content_ = message_.message.content
|
||||||
if type(content_) == "table" and content_[1].type == "tool_use" and content_[1].id == tool_id then
|
if type(content_) == "table" then
|
||||||
return message_
|
for _, item in ipairs(content_) do
|
||||||
|
if item.type == "tool_use" and item.id == tool_id then return message_ end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1445,18 +1439,13 @@ function M.get_tool_result_message(message, messages)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not tool_id then return nil end
|
if not tool_id then return nil end
|
||||||
local idx = nil
|
for idx_ = #messages, 1, -1 do
|
||||||
for idx_, message_ in ipairs(messages) do
|
local message_ = messages[idx_]
|
||||||
if message_.uuid == message.uuid then
|
|
||||||
idx = idx_
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if not idx then return nil end
|
|
||||||
for _, message_ in ipairs(vim.list_slice(messages, idx + 1, #messages)) do
|
|
||||||
local content_ = message_.message.content
|
local content_ = message_.message.content
|
||||||
if type(content_) == "table" and content_[1].type == "tool_result" and content_[1].tool_use_id == tool_id then
|
if type(content_) == "table" then
|
||||||
return message_
|
for _, item in ipairs(content_) do
|
||||||
|
if item.type == "tool_result" and item.tool_use_id == tool_id then return message_ end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user