feat: tools support copilot (#1183)

This commit is contained in:
yetone 2025-02-05 23:47:52 +08:00 committed by GitHub
parent 0603364ea8
commit 3aaf7dad77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 14 deletions

View File

@ -31,7 +31,7 @@ local Config = require("avante.config")
local Path = require("plenary.path") local Path = require("plenary.path")
local Utils = require("avante.utils") local Utils = require("avante.utils")
local P = require("avante.providers") local P = require("avante.providers")
local O = require("avante.providers").openai local OpenAI = require("avante.providers").openai
local H = {} local H = {}
@ -215,10 +215,31 @@ M.parse_messages = function(opts)
vim vim
.iter(opts.messages) .iter(opts.messages)
:each(function(msg) table.insert(messages, { role = M.role_map[msg.role], content = msg.content }) end) :each(function(msg) table.insert(messages, { role = M.role_map[msg.role], content = msg.content }) end)
if opts.tool_result then
table.insert(messages, {
role = M.role_map["assistant"],
tool_calls = {
{
id = opts.tool_use.id,
type = "function",
["function"] = {
name = opts.tool_use.name,
arguments = opts.tool_use.input_json,
},
},
},
})
local result_content = opts.tool_result.content or ""
table.insert(messages, {
role = "tool",
tool_call_id = opts.tool_result.tool_use_id,
content = opts.tool_result.is_error and "Error: " .. result_content or result_content,
})
end
return messages return messages
end end
M.parse_response = O.parse_response M.parse_response = OpenAI.parse_response
M.parse_curl_args = function(provider, code_opts) M.parse_curl_args = function(provider, code_opts)
-- refresh token synchronously, only if it has expired -- refresh token synchronously, only if it has expired
@ -227,6 +248,13 @@ M.parse_curl_args = function(provider, code_opts)
local base, body_opts = P.parse_config(provider) local base, body_opts = P.parse_config(provider)
local tools = {}
if code_opts.tools then
for _, tool in ipairs(code_opts.tools) do
table.insert(tools, OpenAI.transform_tool(tool))
end
end
return { return {
url = H.chat_completion_url(base.endpoint), url = H.chat_completion_url(base.endpoint),
timeout = base.timeout, timeout = base.timeout,
@ -242,6 +270,7 @@ M.parse_curl_args = function(provider, code_opts)
model = base.model, model = base.model,
messages = M.parse_messages(code_opts), messages = M.parse_messages(code_opts),
stream = true, stream = true,
tools = tools,
}, body_opts), }, body_opts),
} }
end end

View File

@ -60,9 +60,19 @@ local P = require("avante.providers")
---@field type string ---@field type string
---@field description string ---@field description string
---@class AvanteProviderFunctor
local M = {}
M.api_key_name = "OPENAI_API_KEY"
M.role_map = {
user = "user",
assistant = "assistant",
}
---@param tool AvanteLLMTool ---@param tool AvanteLLMTool
---@return AvanteOpenAITool ---@return AvanteOpenAITool
local function transform_tool(tool) function M.transform_tool(tool)
local input_schema_properties = {} local input_schema_properties = {}
local required = {} local required = {}
for _, field in ipairs(tool.param.fields) do for _, field in ipairs(tool.param.fields) do
@ -90,16 +100,6 @@ local function transform_tool(tool)
return res return res
end end
---@class AvanteProviderFunctor
local M = {}
M.api_key_name = "OPENAI_API_KEY"
M.role_map = {
user = "user",
assistant = "assistant",
}
M.is_openrouter = function(url) return url:match("^https://openrouter%.ai/") end M.is_openrouter = function(url) return url:match("^https://openrouter%.ai/") end
---@param opts AvantePromptOptions ---@param opts AvantePromptOptions
@ -301,7 +301,7 @@ M.parse_curl_args = function(provider, code_opts)
local tools = {} local tools = {}
if code_opts.tools then if code_opts.tools then
for _, tool in ipairs(code_opts.tools) do for _, tool in ipairs(code_opts.tools) do
table.insert(tools, transform_tool(tool)) table.insert(tools, M.transform_tool(tool))
end end
end end