feat: delete tool use messages tool (#2158)

This commit is contained in:
yetone
2025-06-05 02:51:46 +08:00
committed by GitHub
parent af8d373f22
commit 801adc4692
8 changed files with 313 additions and 222 deletions

View File

@@ -58,6 +58,7 @@ M.returns = {
function M.on_render(opts)
local lines = {}
table.insert(lines, Line:new({ { "✓ Task Completed", Highlights.AVANTE_TASK_COMPLETED } }))
table.insert(lines, Line:new({ { "" } }))
local result = opts.result or ""
local text_lines = vim.split(result, "\n")
for _, text_line in ipairs(text_lines) do

View File

@@ -0,0 +1,62 @@
local Base = require("avante.llm_tools.base")
local Utils = require("avante.utils")
---@class AvanteLLMTool
local M = setmetatable({}, Base)
M.name = "delete_tool_use_messages"
M.description =
"Since many tool use messages are useless for completing subsequent tasks and may cause excessive token consumption or even prevent task completion, you need to decide whether to invoke this tool to delete the useless tool use messages."
---@type AvanteLLMToolParam
M.param = {
type = "table",
fields = {
{
name = "tool_use_id",
description = "The tool use id",
type = "string",
},
},
usage = {
tool_use_id = "The tool use id",
},
}
---@type AvanteLLMToolReturn[]
M.returns = {
{
name = "success",
description = "True if the deletion was successful, false otherwise",
type = "boolean",
},
{
name = "error",
description = "Error message",
type = "string",
optional = true,
},
}
---@type AvanteLLMToolFunc<{ tool_use_id: string }>
function M.func(opts)
local sidebar = require("avante").get()
if not sidebar then return false, "Avante sidebar not found" end
local history_messages = Utils.get_history_messages(sidebar.chat_history)
local the_deleted_message_uuids = {}
for _, msg in ipairs(history_messages) do
if Utils.is_tool_use_message(msg) then
local content = msg.message.content
if type(content) == "table" then
for _, item in ipairs(content) do
if item.id == opts.tool_use_id then table.insert(the_deleted_message_uuids, msg.uuid) end
end
end
end
end
sidebar:delete_history_messages(the_deleted_message_uuids)
return true, nil
end
return M

View File

@@ -107,7 +107,6 @@ When you're done, provide a clear and concise summary of what you found.]]):gsub
local stream_options = {
ask = true,
disable_compact_history_messages = true,
memory = memory_content,
code_lang = "unknown",
provider = Providers[Config.provider],

View File

@@ -761,6 +761,7 @@ M._tools = {
},
require("avante.llm_tools.ls"),
require("avante.llm_tools.grep"),
require("avante.llm_tools.delete_tool_use_messages"),
{
name = "read_file_toplevel_symbols",
description = "Read the top-level symbols of a file in current project scope",