refactor(history): change HistoryMessage:new() to accept role and item

Change the constructor to accept role and a single content item instead
of raw AvanteLLMMessage instance. This will help when we will start
changing message.content from being a string or a list of tables to just
a string or a single table.
This commit is contained in:
Dmitry Torokhov
2025-07-18 15:08:19 -07:00
committed by yetone
parent 2d9f8fd252
commit aa606b6147
9 changed files with 171 additions and 252 deletions

View File

@@ -779,17 +779,12 @@ function M._stream(opts)
---@type avante.HistoryMessage[]
local messages = {}
for _, tool_result in ipairs(tool_results) do
messages[#messages + 1] = History.Message:new({
role = "user",
content = {
{
type = "tool_result",
tool_use_id = tool_result.tool_use_id,
content = tool_result.content,
is_error = tool_result.is_error,
is_user_declined = tool_result.is_user_declined,
},
},
messages[#messages + 1] = History.Message:new("user", {
type = "tool_result",
tool_use_id = tool_result.tool_use_id,
content = tool_result.content,
is_error = tool_result.is_error,
is_user_declined = tool_result.is_user_declined,
})
end
if opts.on_messages_add then opts.on_messages_add(messages) end
@@ -822,12 +817,10 @@ function M._stream(opts)
-- Special handling for cancellation signal from tools
if error == LLMToolHelpers.CANCEL_TOKEN then
Utils.debug("Tool execution was cancelled by user")
if opts.on_chunk then opts.on_chunk("\n*[Request cancelled by user during tool execution.]*\n") end
local cancelled_text = "\n*[Request cancelled by user during tool execution.]*\n"
if opts.on_chunk then opts.on_chunk(cancelled_text) end
if opts.on_messages_add then
local message = History.Message:new({
role = "assistant",
content = "\n*[Request cancelled by user during tool execution.]*\n",
}, {
local message = History.Message:new("assistant", cancelled_text, {
just_for_display = true,
})
opts.on_messages_add({ message })
@@ -878,12 +871,10 @@ function M._stream(opts)
if result ~= nil or error ~= nil then return handle_tool_result(result, error) end
end
if stop_opts.reason == "cancelled" then
if opts.on_chunk then opts.on_chunk("\n*[Request cancelled by user.]*\n") end
local cancelled_text = "\n*[Request cancelled by user.]*\n"
if opts.on_chunk then opts.on_chunk(cancelled_text) end
if opts.on_messages_add then
local message = History.Message:new({
role = "assistant",
content = "\n*[Request cancelled by user.]*\n",
}, {
local message = History.Message:new("assistant", cancelled_text, {
just_for_display = true,
})
opts.on_messages_add({ message })
@@ -922,19 +913,21 @@ function M._stream(opts)
Utils.debug("user reminder count", user_reminder_count)
local message
if #unfinished_todos > 0 then
message = History.Message:new({
role = "user",
content = "<user-reminder>You should use tool calls to answer the question, for example, use update_todo_status if the task step is done or cancelled.</user-reminder>",
}, {
visible = false,
})
message = History.Message:new(
"user",
"<user-reminder>You should use tool calls to answer the question, for example, use update_todo_status if the task step is done or cancelled.</user-reminder>",
{
visible = false,
}
)
else
message = History.Message:new({
role = "user",
content = "<user-reminder>You should use tool calls to answer the question, for example, use attempt_completion if the job is done.</user-reminder>",
}, {
visible = false,
})
message = History.Message:new(
"user",
"<user-reminder>You should use tool calls to answer the question, for example, use attempt_completion if the job is done.</user-reminder>",
{
visible = false,
}
)
end
opts.on_messages_add({ message })
local new_opts = vim.tbl_deep_extend("force", opts, {
@@ -958,12 +951,13 @@ function M._stream(opts)
end
if stop_opts.reason == "rate_limit" then
local message = opts.on_messages_add
and History.Message:new({
role = "assistant",
content = "", -- Actual content will be set below
}, {
just_for_display = true,
})
and History.Message:new(
"assistant",
"", -- Actual content will be set below
{
just_for_display = true,
}
)
local timer = vim.loop.new_timer()
if timer then