It is unwieldy to handle fields in opt table one by one. Luckily there
is vim.bl_extend("force", ...) that can help us here.
Also move annotation for opts from parameter to a separate class to make
it easier to work with.
57 lines
1.8 KiB
Lua
57 lines
1.8 KiB
Lua
local Utils = require("avante.utils")
|
|
|
|
---@class avante.HistoryMessage
|
|
local M = {}
|
|
M.__index = M
|
|
|
|
---@class avante.HistoryMessage.Opts
|
|
---@field uuid? string
|
|
---@field turn_id? string
|
|
---@field state? avante.HistoryMessageState
|
|
---@field displayed_content? string
|
|
---@field original_content? AvanteLLMMessageContent
|
|
---@field selected_code? AvanteSelectedCode
|
|
---@field selected_filepaths? string[]
|
|
---@field is_calling? boolean
|
|
---@field is_dummy? boolean
|
|
---@field is_user_submission? boolean
|
|
---@field just_for_display? boolean
|
|
---@field visible? boolean
|
|
---
|
|
---@param message AvanteLLMMessage
|
|
---@param opts? avante.HistoryMessage.Opts
|
|
---@return avante.HistoryMessage
|
|
function M:new(message, opts)
|
|
local obj = {
|
|
message = message,
|
|
uuid = Utils.uuid(),
|
|
state = "generated",
|
|
timestamp = Utils.get_timestamp(),
|
|
is_user_submission = false,
|
|
visible = true,
|
|
}
|
|
obj = vim.tbl_extend("force", obj, opts or {})
|
|
return setmetatable(obj, M)
|
|
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
|