fix: reuse parse_messages and remove parse_response_data and have a good function name for check env exists (#1386)

This commit is contained in:
yetone
2025-02-25 13:52:13 +08:00
committed by GitHub
parent 901e1caa91
commit e7ffb76499
3 changed files with 10 additions and 52 deletions

View File

@@ -209,39 +209,7 @@ M.role_map = {
assistant = "assistant",
}
function M.parse_messages(opts)
local messages = {
{ role = "system", content = opts.system_prompt },
}
vim
.iter(opts.messages)
:each(function(msg) table.insert(messages, { role = M.role_map[msg.role], content = msg.content }) end)
if opts.tool_histories then
for _, tool_history in ipairs(opts.tool_histories) do
table.insert(messages, {
role = M.role_map["assistant"],
tool_calls = {
{
id = tool_history.tool_use.id,
type = "function",
["function"] = {
name = tool_history.tool_use.name,
arguments = tool_history.tool_use.input_json,
},
},
},
})
local result_content = tool_history.tool_result.content or ""
table.insert(messages, {
role = "tool",
tool_call_id = tool_history.tool_result.tool_use_id,
content = tool_history.tool_result.is_error and "Error: " .. result_content or result_content,
})
end
end
return messages
end
M.parse_messages = OpenAI.parse_messages
M.parse_response = OpenAI.parse_response

View File

@@ -113,7 +113,7 @@ function E.setup(opts)
vim.fn.setenv(var, value)
vim.g.avante_login = true
else
if not opts.provider.has() then
if not opts.provider.is_env_set() then
Utils.warn("Failed to set " .. var .. ". Avante won't work as expected", { once = true })
end
end
@@ -137,7 +137,7 @@ function E.setup(opts)
"noice",
}
if not vim.tbl_contains(exclude_filetypes, vim.bo.filetype) and not opts.provider.has() then
if not vim.tbl_contains(exclude_filetypes, vim.bo.filetype) and not opts.provider.is_env_set() then
DressingState.winid = api.nvim_get_current_win()
vim.ui.input({ default = "", prompt = "Enter " .. var .. ": " }, on_confirm)
for _, winid in ipairs(api.nvim_list_wins()) do
@@ -203,7 +203,9 @@ M = setmetatable(M, {
---@diagnostic disable: undefined-field,no-unknown,inject-field
if Config.vendors[k] ~= nil then
Opts.parse_response = Opts.parse_response_data
if Opts.parse_response_data ~= nil then
Utils.error("parse_response_data is not supported for avante.nvim vendors")
end
if Opts.__inherited_from ~= nil then
local BaseOpts = M.get_config(Opts.__inherited_from)
local ok, module = pcall(require, "avante.providers." .. Opts.__inherited_from)
@@ -223,9 +225,9 @@ M = setmetatable(M, {
-- default to gpt-4o as tokenizer
if t[k].tokenizer_id == nil then t[k].tokenizer_id = "gpt-4o" end
if t[k].use_xml_format == nil then t[k].use_xml_format = false end
if t[k].use_xml_format == nil then t[k].use_xml_format = true end
if t[k].has == nil then t[k].has = function() return E.parse_envvar(t[k]) ~= nil end end
if t[k].is_env_set == nil then t[k].is_env_set = function() return E.parse_envvar(t[k]) ~= nil end end
if t[k].setup == nil then
local provider_conf = M.parse_config(t[k])