refactor(sidebar): use helpers when working with history
Do not poke directly into avante.HistoryMessage instances, use appropriate helpers instead.
This commit is contained in:
@@ -35,10 +35,6 @@ function M.get_tool_use_data(message)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param message avante.HistoryMessage
|
|
||||||
---@return boolean
|
|
||||||
function M.is_tool_use_message(message) return M.get_tool_use_data(message) ~= nil end
|
|
||||||
|
|
||||||
---If message is a "tool result" message returns results of the tool invocation.
|
---If message is a "tool result" message returns results of the tool invocation.
|
||||||
---@param message avante.HistoryMessage
|
---@param message avante.HistoryMessage
|
||||||
---@return AvanteLLMToolResult | nil
|
---@return AvanteLLMToolResult | nil
|
||||||
@@ -66,38 +62,43 @@ function M.get_tool_result(id, messages)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Given a tool invocation ID locate corresponding tool use message
|
||||||
|
---@param id string
|
||||||
|
---@param messages avante.HistoryMessage[]
|
||||||
|
---@return avante.HistoryMessage | nil
|
||||||
|
function M.get_tool_use_message(id, messages)
|
||||||
|
for idx = #messages, 1, -1 do
|
||||||
|
local msg = messages[idx]
|
||||||
|
local use = M.get_tool_use_data(msg)
|
||||||
|
if use and use.id == id then return msg end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Given a tool invocation ID locate corresponding tool result message
|
||||||
|
---@param id string
|
||||||
|
---@param messages avante.HistoryMessage[]
|
||||||
|
---@return avante.HistoryMessage | nil
|
||||||
|
function M.get_tool_result_message(id, messages)
|
||||||
|
for idx = #messages, 1, -1 do
|
||||||
|
local msg = messages[idx]
|
||||||
|
local result = M.get_tool_result_data(msg)
|
||||||
|
if result and result.tool_use_id == id then return msg end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param message avante.HistoryMessage
|
||||||
|
---@return boolean
|
||||||
|
function M.is_thinking_message(message)
|
||||||
|
local content = message.message.content
|
||||||
|
return type(content) == "table" and (content[1].type == "thinking" or content[1].type == "redacted_thinking")
|
||||||
|
end
|
||||||
|
|
||||||
---@param message avante.HistoryMessage
|
---@param message avante.HistoryMessage
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.is_tool_result_message(message) return M.get_tool_result_data(message) ~= nil end
|
function M.is_tool_result_message(message) return M.get_tool_result_data(message) ~= nil end
|
||||||
|
|
||||||
---Given a tool result message locate corresponding tool use message
|
|
||||||
---@param message avante.HistoryMessage
|
---@param message avante.HistoryMessage
|
||||||
---@param messages avante.HistoryMessage[]
|
---@return boolean
|
||||||
---@return avante.HistoryMessage | nil
|
function M.is_tool_use_message(message) return M.get_tool_use_data(message) ~= nil end
|
||||||
function M.get_tool_use_message(message, messages)
|
|
||||||
local result = M.get_tool_result_data(message)
|
|
||||||
if result then
|
|
||||||
for idx = #messages, 1, -1 do
|
|
||||||
local msg = messages[idx]
|
|
||||||
local use = M.get_tool_use_data(msg)
|
|
||||||
if use and use.id == result.tool_use_id then return msg end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---Given a tool use message locate corresponding tool result message
|
|
||||||
---@param message avante.HistoryMessage
|
|
||||||
---@param messages avante.HistoryMessage[]
|
|
||||||
---@return avante.HistoryMessage | nil
|
|
||||||
function M.get_tool_result_message(message, messages)
|
|
||||||
local use = M.get_tool_use_data(message)
|
|
||||||
if use then
|
|
||||||
for idx = #messages, 1, -1 do
|
|
||||||
local msg = messages[idx]
|
|
||||||
local result = M.get_tool_result_data(msg)
|
|
||||||
if result and result.tool_use_id == use.id then return msg end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ local function message_content_item_to_lines(item, message, messages)
|
|||||||
elseif item.type == "tool_use" then
|
elseif item.type == "tool_use" then
|
||||||
local ok, llm_tool = pcall(require, "avante.llm_tools." .. item.name)
|
local ok, llm_tool = pcall(require, "avante.llm_tools." .. item.name)
|
||||||
if ok then
|
if ok then
|
||||||
local tool_result_message = Helpers.get_tool_result_message(message, messages)
|
local tool_result_message = Helpers.get_tool_result_message(item.id, messages)
|
||||||
---@cast llm_tool AvanteLLMTool
|
---@cast llm_tool AvanteLLMTool
|
||||||
if llm_tool.on_render then
|
if llm_tool.on_render then
|
||||||
return llm_tool.on_render(item.input, {
|
return llm_tool.on_render(item.input, {
|
||||||
|
|||||||
@@ -1724,8 +1724,7 @@ local function _get_message_lines(message, messages, ctx)
|
|||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
if message.message.role == "assistant" then
|
if message.message.role == "assistant" then
|
||||||
local content = message.message.content
|
if History.Helpers.is_tool_use_message(message) then return lines end
|
||||||
if type(content) == "table" and content[1].type == "tool_use" then return lines end
|
|
||||||
local text = table.concat(vim.tbl_map(function(line) return tostring(line) end, lines), "\n")
|
local text = table.concat(vim.tbl_map(function(line) return tostring(line) end, lines), "\n")
|
||||||
local transformed = transform_result_content(text, ctx.prev_filepath)
|
local transformed = transform_result_content(text, ctx.prev_filepath)
|
||||||
ctx.prev_filepath = transformed.current_filepath
|
ctx.prev_filepath = transformed.current_filepath
|
||||||
@@ -2081,12 +2080,9 @@ function Sidebar:add_history_messages(messages)
|
|||||||
end
|
end
|
||||||
local last_message = messages[#messages]
|
local last_message = messages[#messages]
|
||||||
if last_message then
|
if last_message then
|
||||||
local content = last_message.message.content
|
if History.Helpers.is_tool_use_message(last_message) then
|
||||||
if type(content) == "table" and content[1].type == "tool_use" then
|
|
||||||
self.current_state = "tool calling"
|
self.current_state = "tool calling"
|
||||||
elseif type(content) == "table" and content[1].type == "thinking" then
|
elseif History.Helpers.is_thinking_message(last_message) then
|
||||||
self.current_state = "thinking"
|
|
||||||
elseif type(content) == "table" and content[1].type == "redacted_thinking" then
|
|
||||||
self.current_state = "thinking"
|
self.current_state = "thinking"
|
||||||
else
|
else
|
||||||
self.current_state = "generating"
|
self.current_state = "generating"
|
||||||
@@ -2514,24 +2510,18 @@ function Sidebar:create_input_container()
|
|||||||
---@param state AvanteLLMToolUseState
|
---@param state AvanteLLMToolUseState
|
||||||
local function on_tool_log(tool_id, tool_name, log, state)
|
local function on_tool_log(tool_id, tool_name, log, state)
|
||||||
if state == "generating" then on_state_change("tool calling") end
|
if state == "generating" then on_state_change("tool calling") end
|
||||||
local tool_use_message = nil
|
local tool_use_message = History.Helpers.get_tool_use_message(tool_id, self.chat_history.messages)
|
||||||
for idx = #self.chat_history.messages, 1, -1 do
|
|
||||||
local message = self.chat_history.messages[idx]
|
|
||||||
local content = message.message.content
|
|
||||||
if type(content) == "table" and content[1].type == "tool_use" and content[1].id == tool_id then
|
|
||||||
tool_use_message = message
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if not tool_use_message then
|
if not tool_use_message then
|
||||||
-- Utils.debug("tool_use message not found", tool_id, tool_name)
|
-- Utils.debug("tool_use message not found", tool_id, tool_name)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local tool_use_logs = tool_use_message.tool_use_logs or {}
|
local tool_use_logs = tool_use_message.tool_use_logs or {}
|
||||||
local content = string.format("[%s]: %s", tool_name, log)
|
local content = string.format("[%s]: %s", tool_name, log)
|
||||||
table.insert(tool_use_logs, content)
|
table.insert(tool_use_logs, content)
|
||||||
local orig_is_calling = tool_use_message.is_calling
|
|
||||||
tool_use_message.tool_use_logs = tool_use_logs
|
tool_use_message.tool_use_logs = tool_use_logs
|
||||||
|
|
||||||
|
local orig_is_calling = tool_use_message.is_calling
|
||||||
tool_use_message.is_calling = true
|
tool_use_message.is_calling = true
|
||||||
self:update_content("")
|
self:update_content("")
|
||||||
tool_use_message.is_calling = orig_is_calling
|
tool_use_message.is_calling = orig_is_calling
|
||||||
@@ -2539,21 +2529,14 @@ function Sidebar:create_input_container()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function set_tool_use_store(tool_id, key, value)
|
local function set_tool_use_store(tool_id, key, value)
|
||||||
local tool_use_message = nil
|
local tool_use_message = History.Helpers.get_tool_use_message(tool_id, self.chat_history.messages)
|
||||||
for idx = #self.chat_history.messages, 1, -1 do
|
if tool_use_message then
|
||||||
local message = self.chat_history.messages[idx]
|
|
||||||
local content = message.message.content
|
|
||||||
if type(content) == "table" and content[1].type == "tool_use" and content[1].id == tool_id then
|
|
||||||
tool_use_message = message
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if not tool_use_message then return end
|
|
||||||
local tool_use_store = tool_use_message.tool_use_store or {}
|
local tool_use_store = tool_use_message.tool_use_store or {}
|
||||||
tool_use_store[key] = value
|
tool_use_store[key] = value
|
||||||
tool_use_message.tool_use_store = tool_use_store
|
tool_use_message.tool_use_store = tool_use_store
|
||||||
self:save_history()
|
self:save_history()
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@type AvanteLLMStopCallback
|
---@type AvanteLLMStopCallback
|
||||||
local function on_stop(stop_opts)
|
local function on_stop(stop_opts)
|
||||||
|
|||||||
Reference in New Issue
Block a user