refactor(history): move code collecting pending tools to history

Code dealing with scanning history messages and extracting some data or
state belongs to avante/history/ so move it there. Along with the move
update the implementation to make use of get_tool_use_data() and
get_tool_result_data() helpers to simplify it.

Also rename the function to History.get_pending_tools() to better
reflect its purpose: "uncalled" means something that was done without
request, unsolicited, not something that has not completed yet.
This commit is contained in:
Dmitry Torokhov
2025-07-14 08:54:07 -07:00
committed by yetone
parent b64120749f
commit d7c48075a5
3 changed files with 47 additions and 56 deletions

View File

@@ -257,4 +257,45 @@ M.update_history_messages = function(messages, using_ReAct_prompt, add_diagnosti
return final_history_messages
end
---Scans message history backwards, looking for tool invocations that have not been executed yet
---@param messages avante.HistoryMessage[]
---@return AvantePartialLLMToolUse[]
---@return avante.HistoryMessage[]
function M.get_pending_tools(messages)
local last_turn_id = nil
if #messages > 0 then last_turn_id = messages[#messages].turn_id end
local pending_tool_uses = {} ---@type AvantePartialLLMToolUse[]
local pending_tool_uses_messages = {} ---@type avante.HistoryMessage[]
local tool_result_seen = {}
for idx = #messages, 1, -1 do
local message = messages[idx]
if last_turn_id and message.turn_id ~= last_turn_id then break end
local use = Helpers.get_tool_use_data(message)
if use then
if not tool_result_seen[use.id] then
local partial_tool_use = {
name = use.name,
id = use.id,
input = use.input,
state = message.state,
}
table.insert(pending_tool_uses, 1, partial_tool_use)
table.insert(pending_tool_uses_messages, 1, message)
end
goto continue
end
local result = Helpers.get_tool_result_data(message)
if result then tool_result_seen[result.tool_use_id] = true end
::continue::
end
return pending_tool_uses, pending_tool_uses_messages
end
return M