Files
avante.nvim/lua/avante/llm_tools/delete_tool_use_messages.lua
Dmitry Torokhov 47ad90b389 fix: when deleting tool use also delete tool result
Tool results are not useful without their "use" messages, so delete them
when deleting "use" ones.

Makes use of the new get_tool_use_data() and get_tool_result_data()
helpers.
2025-07-16 12:49:15 +08:00

65 lines
1.8 KiB
Lua

local Base = require("avante.llm_tools.base")
local History = require("avante.history")
---@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(input, opts)
local sidebar = require("avante").get()
if not sidebar then return false, "Avante sidebar not found" end
local history_messages = History.get_history_messages(sidebar.chat_history)
local the_deleted_message_uuids = {}
for _, msg in ipairs(history_messages) do
local use = History.Helpers.get_tool_use_data(msg)
if use then
if use.id == input.tool_use_id then table.insert(the_deleted_message_uuids, msg.uuid) end
goto continue
end
local result = History.Helpers.get_tool_result_data(msg)
if result then
if result.tool_use_id == input.tool_use_id then table.insert(the_deleted_message_uuids, msg.uuid) end
end
::continue::
end
sidebar:delete_history_messages(the_deleted_message_uuids)
return true, nil
end
return M