Files
avante.nvim/lua/avante/history/message.lua
Dmitry Torokhov 06cc3b3f21 refactor(history): add helpers to generate synthetic history messages
Add helpers such as HistoryMessage:new_assistant_synthetic() and
HistoryMessage:new_user_syntheric() to make callers more compact and
understandable.
2025-07-16 12:49:15 +08:00

51 lines
2.3 KiB
Lua

local Utils = require("avante.utils")
---@class avante.HistoryMessage
local M = {}
M.__index = M
---@param message AvanteLLMMessage
---@param opts? {is_user_submission?: boolean, visible?: boolean, displayed_content?: string, state?: avante.HistoryMessageState, uuid?: string, selected_filepaths?: string[], selected_code?: AvanteSelectedCode, just_for_display?: boolean, is_dummy?: boolean, turn_id?: string, is_calling?: boolean}
---@return avante.HistoryMessage
function M:new(message, opts)
opts = opts or {}
local obj = setmetatable({}, M)
obj.message = message
obj.uuid = opts.uuid or Utils.uuid()
obj.state = opts.state or "generated"
obj.timestamp = Utils.get_timestamp()
obj.is_user_submission = false
obj.visible = true
if opts.is_user_submission ~= nil then obj.is_user_submission = opts.is_user_submission end
if opts.visible ~= nil then obj.visible = opts.visible end
if opts.displayed_content ~= nil then obj.displayed_content = opts.displayed_content end
if opts.selected_filepaths ~= nil then obj.selected_filepaths = opts.selected_filepaths end
if opts.selected_code ~= nil then obj.selected_code = opts.selected_code end
if opts.just_for_display ~= nil then obj.just_for_display = opts.just_for_display end
if opts.is_dummy ~= nil then obj.is_dummy = opts.is_dummy end
if opts.turn_id ~= nil then obj.turn_id = opts.turn_id end
if opts.is_calling ~= nil then obj.is_calling = opts.is_calling end
return obj
end
---Creates a new instance of synthetic (dummy) history message
---@param role "assistant" | "user"
---@param item AvanteLLMMessageContentItem | string
---@return avante.HistoryMessage
function M:new_synthetic(role, item)
local content = type(item) == "string" and item or { item }
return M:new({ role = role, content = content }, { is_dummy = true })
end
---Creates a new instance of synthetic (dummy) history message attributed to the assistant
---@param item AvanteLLMMessageContentItem | string
---@return avante.HistoryMessage
function M:new_assistant_synthetic(item) return M:new_synthetic("assistant", item) end
---Creates a new instance of synthetic (dummy) history message attributed to the user
---@param item AvanteLLMMessageContentItem | string
---@return avante.HistoryMessage
function M:new_user_synthetic(item) return M:new_synthetic("user", item) end
return M