fix: missing tool use (#1610)

This commit is contained in:
yetone
2025-03-17 05:00:03 +08:00
committed by GitHub
parent 63605a55a3
commit eb5a2f6cca
2 changed files with 12 additions and 2 deletions

View File

@@ -77,6 +77,7 @@ function M:parse_messages(opts)
end
end
local has_tool_use = false
for idx, message in ipairs(opts.messages) do
local content_items = message.content
local message_content = {}
@@ -102,8 +103,14 @@ function M:parse_messages(opts)
elseif type(item) == "table" and item.type == "image" then
table.insert(message_content, { type = "image", source = item.source })
elseif not provider_conf.disable_tools and type(item) == "table" and item.type == "tool_use" then
has_tool_use = true
table.insert(message_content, { type = "tool_use", name = item.name, id = item.id, input = item.input })
elseif not provider_conf.disable_tools and type(item) == "table" and item.type == "tool_result" then
elseif
not provider_conf.disable_tools
and type(item) == "table"
and item.type == "tool_result"
and has_tool_use
then
table.insert(
message_content,
{ type = "tool_result", tool_use_id = item.tool_use_id, content = item.content, is_error = item.is_error }

View File

@@ -81,6 +81,8 @@ function M:parse_messages(opts)
table.insert(messages, { role = "system", content = opts.system_prompt })
end
local has_tool_use = false
vim.iter(opts.messages):each(function(msg)
if type(msg.content) == "string" then
table.insert(messages, { role = self.role_map[msg.role], content = msg.content })
@@ -101,12 +103,13 @@ function M:parse_messages(opts)
},
})
elseif item.type == "tool_use" then
has_tool_use = true
table.insert(tool_calls, {
id = item.id,
type = "function",
["function"] = { name = item.name, arguments = vim.json.encode(item.input) },
})
elseif item.type == "tool_result" then
elseif item.type == "tool_result" and has_tool_use then
table.insert(
tool_results,
{ tool_call_id = item.tool_use_id, content = item.is_error and "Error: " .. item.content or item.content }