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