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.
This commit is contained in:
@@ -97,78 +97,42 @@ M.update_history_messages = function(messages, using_ReAct_prompt, add_diagnosti
|
||||
view_tool_input = { command = "view", path = path }
|
||||
end
|
||||
history_messages = vim.list_extend(history_messages, {
|
||||
Message:new({
|
||||
role = "assistant",
|
||||
content = string.format("Viewing file %s to get the latest content", path),
|
||||
}, {
|
||||
is_dummy = true,
|
||||
Message:new_assistant_synthetic(string.format("Viewing file %s to get the latest content", path)),
|
||||
Message:new_assistant_synthetic({
|
||||
type = "tool_use",
|
||||
id = view_tool_use_id,
|
||||
name = view_tool_name,
|
||||
input = view_tool_input,
|
||||
}),
|
||||
Message:new({
|
||||
role = "assistant",
|
||||
content = {
|
||||
{
|
||||
type = "tool_use",
|
||||
id = view_tool_use_id,
|
||||
name = view_tool_name,
|
||||
input = view_tool_input,
|
||||
},
|
||||
},
|
||||
}, {
|
||||
is_dummy = true,
|
||||
}),
|
||||
Message:new({
|
||||
role = "user",
|
||||
content = {
|
||||
{
|
||||
type = "tool_result",
|
||||
tool_use_id = view_tool_use_id,
|
||||
content = view_result,
|
||||
is_error = view_error ~= nil,
|
||||
is_user_declined = false,
|
||||
},
|
||||
},
|
||||
}, {
|
||||
is_dummy = true,
|
||||
Message:new_user_synthetic({
|
||||
type = "tool_result",
|
||||
tool_use_id = view_tool_use_id,
|
||||
content = view_result,
|
||||
is_error = view_error ~= nil,
|
||||
is_user_declined = false,
|
||||
}),
|
||||
})
|
||||
if last_modified_files[uniformed_path] == idx and add_diagnostic then
|
||||
local diagnostics = Utils.lsp.get_diagnostics_from_filepath(path)
|
||||
history_messages = vim.list_extend(history_messages, {
|
||||
Message:new({
|
||||
role = "assistant",
|
||||
content = string.format(
|
||||
Message:new_assistant_synthetic(
|
||||
string.format(
|
||||
"The file %s has been modified, let me check if there are any errors in the changes.",
|
||||
path
|
||||
),
|
||||
}, {
|
||||
is_dummy = true,
|
||||
)
|
||||
),
|
||||
Message:new_assistant_synthetic({
|
||||
type = "tool_use",
|
||||
id = get_diagnostics_tool_use_id,
|
||||
name = "get_diagnostics",
|
||||
input = { path = path },
|
||||
}),
|
||||
Message:new({
|
||||
role = "assistant",
|
||||
content = {
|
||||
{
|
||||
type = "tool_use",
|
||||
id = get_diagnostics_tool_use_id,
|
||||
name = "get_diagnostics",
|
||||
input = { path = path },
|
||||
},
|
||||
},
|
||||
}, {
|
||||
is_dummy = true,
|
||||
}),
|
||||
Message:new({
|
||||
role = "user",
|
||||
content = {
|
||||
{
|
||||
type = "tool_result",
|
||||
tool_use_id = get_diagnostics_tool_use_id,
|
||||
content = vim.json.encode(diagnostics),
|
||||
is_error = false,
|
||||
is_user_declined = false,
|
||||
},
|
||||
},
|
||||
}, {
|
||||
is_dummy = true,
|
||||
Message:new_user_synthetic({
|
||||
type = "tool_result",
|
||||
tool_use_id = get_diagnostics_tool_use_id,
|
||||
content = vim.json.encode(diagnostics),
|
||||
is_error = false,
|
||||
is_user_declined = false,
|
||||
}),
|
||||
})
|
||||
end
|
||||
@@ -239,33 +203,26 @@ M.update_history_messages = function(messages, using_ReAct_prompt, add_diagnosti
|
||||
table.insert(
|
||||
picked_messages,
|
||||
1,
|
||||
Message:new({
|
||||
role = "user",
|
||||
content = {
|
||||
{
|
||||
type = "text",
|
||||
text = string.format(
|
||||
"Tool use [%s] is successful: %s",
|
||||
tool_use_message.message.content[1].name,
|
||||
tostring(not msg.message.content[1].is_error)
|
||||
),
|
||||
},
|
||||
},
|
||||
}, { is_dummy = true })
|
||||
Message:new_user_synthetic({
|
||||
type = "text",
|
||||
text = string.format(
|
||||
"Tool use [%s] is successful: %s",
|
||||
tool_use_message.message.content[1].name,
|
||||
tostring(not msg.message.content[1].is_error)
|
||||
),
|
||||
})
|
||||
)
|
||||
local msg_content = {}
|
||||
table.insert(msg_content, {
|
||||
type = "text",
|
||||
text = string.format(
|
||||
"Tool use %s(%s)",
|
||||
tool_use_message.message.content[1].name,
|
||||
vim.json.encode(tool_use_message.message.content[1].input)
|
||||
),
|
||||
})
|
||||
table.insert(
|
||||
picked_messages,
|
||||
1,
|
||||
Message:new({ role = "assistant", content = msg_content }, { is_dummy = true })
|
||||
Message:new_assistant_synthetic({
|
||||
type = "text",
|
||||
text = string.format(
|
||||
"Tool use %s(%s)",
|
||||
tool_use_message.message.content[1].name,
|
||||
vim.json.encode(tool_use_message.message.content[1].input)
|
||||
),
|
||||
})
|
||||
)
|
||||
end
|
||||
elseif Helpers.is_tool_use_message(msg) then
|
||||
|
||||
@@ -28,4 +28,23 @@ function M:new(message, opts)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user