fix: empty tool array reports an error for some LLM models (#2639)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
Yinzuo Jiang
2025-08-26 17:19:29 +08:00
committed by GitHub
parent fe57497123
commit f643b262cc
2 changed files with 42 additions and 0 deletions

View File

@@ -464,6 +464,10 @@ function M.generate_prompts(opts)
if opts.tools then tools = vim.list_extend(tools, opts.tools) end
if opts.prompt_opts and opts.prompt_opts.tools then tools = vim.list_extend(tools, opts.prompt_opts.tools) end
-- Set tools to nil if empty to avoid sending empty arrays to APIs that require
-- tools to be either non-existent or have at least one item
if #tools == 0 then tools = nil end
local agents_rules = Prompts.get_agents_rules_prompt()
if agents_rules then system_prompt = system_prompt .. "\n\n" .. agents_rules end
local cursor_rules = Prompts.get_cursor_rules_prompt(selected_files)

View File

@@ -112,4 +112,42 @@ describe("generate_prompts", function()
llm.generate_prompts(opts)
assert.are.same(opts.instructions, nil)
end)
it("should set tools to nil when no tools are provided", function()
local opts = {}
local result = llm.generate_prompts(opts)
assert.are.same(result.tools, nil)
end)
it("should set tools to nil when empty tools array is provided", function()
local opts = {
tools = {},
}
local result = llm.generate_prompts(opts)
assert.are.same(result.tools, nil)
end)
it("should set tools to nil when empty prompt_opts.tools array is provided", function()
local opts = {
prompt_opts = {
tools = {},
},
}
local result = llm.generate_prompts(opts)
assert.are.same(result.tools, nil)
end)
it("should include tools when non-empty tools are provided", function()
local mock_tool = {
name = "test_tool",
description = "A test tool",
func = function() end,
}
local opts = {
tools = { mock_tool },
}
local result = llm.generate_prompts(opts)
assert.are.same(#result.tools, 1)
assert.are.same(result.tools[1].name, "test_tool")
end)
end)