Files
avante.nvim/lua/avante/llm_tools/delete_tool_use_messages.lua
Dmitry Torokhov 34907fc1cd refactor(history): start moving history-related code into avante/history
The utils module has grown too big and contains unrelated functionality.
Start moving code related to managing history messages comprising chat
history into lua/avante/history module to keep the code more manageable.
2025-07-16 12:49:15 +08:00

63 lines
1.7 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
if History.Helpers.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 == input.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