refactor: llm tool parameters (#2449)

This commit is contained in:
yetone
2025-07-15 16:40:25 +08:00
committed by GitHub
parent 0c6a8f5688
commit b8bb0fd969
25 changed files with 627 additions and 381 deletions

View File

@@ -53,21 +53,21 @@ describe("llm_tools", function()
describe("ls", function()
it("should list files in directory", function()
local result, err = ls({ path = ".", max_depth = 1 })
local result, err = ls({ path = ".", max_depth = 1 }, {})
assert.is_nil(err)
assert.falsy(result:find("avante.nvim"))
assert.truthy(result:find("test.txt"))
assert.falsy(result:find("test1.txt"))
end)
it("should list files in directory with depth", function()
local result, err = ls({ path = ".", max_depth = 2 })
local result, err = ls({ path = ".", max_depth = 2 }, {})
assert.is_nil(err)
assert.falsy(result:find("avante.nvim"))
assert.truthy(result:find("test.txt"))
assert.truthy(result:find("test1.txt"))
end)
it("should list files respecting gitignore", function()
local result, err = ls({ path = ".", max_depth = 2 })
local result, err = ls({ path = ".", max_depth = 2 }, {})
assert.is_nil(err)
assert.falsy(result:find("avante.nvim"))
assert.truthy(result:find("test.txt"))
@@ -78,49 +78,61 @@ describe("llm_tools", function()
describe("view", function()
it("should read file content", function()
view({ path = "test.txt" }, nil, function(content, err)
assert.is_nil(err)
assert.equals("test content", vim.json.decode(content).content)
end)
view({ path = "test.txt" }, {
on_complete = function(content, err)
assert.is_nil(err)
assert.equals("test content", vim.json.decode(content).content)
end,
})
end)
it("should return error for non-existent file", function()
view({ path = "non_existent.txt" }, nil, function(content, err)
assert.truthy(err)
assert.equals("", content)
end)
view({ path = "non_existent.txt" }, {
on_complete = function(content, err)
assert.truthy(err)
assert.equals("", content)
end,
})
end)
it("should read directory content", function()
view({ path = test_dir }, nil, function(content, err)
assert.is_nil(err)
assert.truthy(content:find("test.txt"))
assert.truthy(content:find("test content"))
end)
view({ path = test_dir }, {
on_complete = function(content, err)
assert.is_nil(err)
assert.truthy(content:find("test.txt"))
assert.truthy(content:find("test content"))
end,
})
end)
end)
describe("create_dir", function()
it("should create new directory", function()
LlmTools.create_dir({ path = "new_dir" }, nil, function(success, err)
assert.is_nil(err)
assert.is_true(success)
LlmTools.create_dir({ path = "new_dir" }, {
session_ctx = {},
on_complete = function(success, err)
assert.is_nil(err)
assert.is_true(success)
local dir_exists = io.open(test_dir .. "/new_dir", "r") ~= nil
assert.is_true(dir_exists)
end)
local dir_exists = io.open(test_dir .. "/new_dir", "r") ~= nil
assert.is_true(dir_exists)
end,
})
end)
end)
describe("delete_path", function()
it("should delete existing file", function()
LlmTools.delete_path({ path = "test.txt" }, nil, function(success, err)
assert.is_nil(err)
assert.is_true(success)
LlmTools.delete_path({ path = "test.txt" }, {
session_ctx = {},
on_complete = function(success, err)
assert.is_nil(err)
assert.is_true(success)
local file_exists = io.open(test_file, "r") ~= nil
assert.is_false(file_exists)
end)
local file_exists = io.open(test_file, "r") ~= nil
assert.is_false(file_exists)
end,
})
end)
end)
@@ -147,22 +159,22 @@ describe("llm_tools", function()
file:write("this is nothing")
file:close()
local result, err = grep({ path = ".", query = "Searchable", case_sensitive = false })
local result, err = grep({ path = ".", query = "Searchable", case_sensitive = false }, {})
assert.is_nil(err)
assert.truthy(result:find("searchable.txt"))
assert.falsy(result:find("nothing.txt"))
local result2, err2 = grep({ path = ".", query = "searchable", case_sensitive = true })
local result2, err2 = grep({ path = ".", query = "searchable", case_sensitive = true }, {})
assert.is_nil(err2)
assert.truthy(result2:find("searchable.txt"))
assert.falsy(result2:find("nothing.txt"))
local result3, err3 = grep({ path = ".", query = "Searchable", case_sensitive = true })
local result3, err3 = grep({ path = ".", query = "Searchable", case_sensitive = true }, {})
assert.is_nil(err3)
assert.falsy(result3:find("searchable.txt"))
assert.falsy(result3:find("nothing.txt"))
local result4, err4 = grep({ path = ".", query = "searchable", case_sensitive = false })
local result4, err4 = grep({ path = ".", query = "searchable", case_sensitive = false }, {})
assert.is_nil(err4)
assert.truthy(result4:find("searchable.txt"))
assert.falsy(result4:find("nothing.txt"))
@@ -172,7 +184,7 @@ describe("llm_tools", function()
query = "searchable",
case_sensitive = false,
exclude_pattern = "search*",
})
}, {})
assert.is_nil(err5)
assert.falsy(result5:find("searchable.txt"))
assert.falsy(result5:find("nothing.txt"))
@@ -191,7 +203,7 @@ describe("llm_tools", function()
file:write("content for ag test")
file:close()
local result, err = grep({ path = ".", query = "ag test" })
local result, err = grep({ path = ".", query = "ag test" }, {})
assert.is_nil(err)
assert.is_string(result)
assert.truthy(result:find("ag_test.txt"))
@@ -215,22 +227,22 @@ describe("llm_tools", function()
file:write("this is nothing")
file:close()
local result, err = grep({ path = ".", query = "Searchable", case_sensitive = false })
local result, err = grep({ path = ".", query = "Searchable", case_sensitive = false }, {})
assert.is_nil(err)
assert.truthy(result:find("searchable.txt"))
assert.falsy(result:find("nothing.txt"))
local result2, err2 = grep({ path = ".", query = "searchable", case_sensitive = true })
local result2, err2 = grep({ path = ".", query = "searchable", case_sensitive = true }, {})
assert.is_nil(err2)
assert.truthy(result2:find("searchable.txt"))
assert.falsy(result2:find("nothing.txt"))
local result3, err3 = grep({ path = ".", query = "Searchable", case_sensitive = true })
local result3, err3 = grep({ path = ".", query = "Searchable", case_sensitive = true }, {})
assert.is_nil(err3)
assert.falsy(result3:find("searchable.txt"))
assert.falsy(result3:find("nothing.txt"))
local result4, err4 = grep({ path = ".", query = "searchable", case_sensitive = false })
local result4, err4 = grep({ path = ".", query = "searchable", case_sensitive = false }, {})
assert.is_nil(err4)
assert.truthy(result4:find("searchable.txt"))
assert.falsy(result4:find("nothing.txt"))
@@ -240,7 +252,7 @@ describe("llm_tools", function()
query = "searchable",
case_sensitive = false,
exclude_pattern = "search*",
})
}, {})
assert.is_nil(err5)
assert.falsy(result5:find("searchable.txt"))
assert.falsy(result5:find("nothing.txt"))
@@ -250,18 +262,18 @@ describe("llm_tools", function()
-- Mock exepath to return nothing
vim.fn.exepath = function() return "" end
local result, err = grep({ path = ".", query = "test" })
local result, err = grep({ path = ".", query = "test" }, {})
assert.equals("", result)
assert.equals("No search command found", err)
end)
it("should respect path permissions", function()
local result, err = grep({ path = "../outside_project", query = "test" })
local result, err = grep({ path = "../outside_project", query = "test" }, {})
assert.truthy(err:find("No permission to access path"))
end)
it("should handle non-existent paths", function()
local result, err = grep({ path = "non_existent_dir", query = "test" })
local result, err = grep({ path = "non_existent_dir", query = "test" }, {})
assert.equals("", result)
assert.truthy(err)
assert.truthy(err:find("No such file or directory"))
@@ -277,86 +289,84 @@ describe("llm_tools", function()
-- end)
it("should return error when running outside current directory", function()
bash({ path = "../outside_project", command = "echo 'test'" }, nil, function(result, err)
assert.is_false(result)
assert.truthy(err)
assert.truthy(err:find("No permission to access path"))
end)
bash({ path = "../outside_project", command = "echo 'test'" }, {
session_ctx = {},
on_complete = function(result, err)
assert.is_false(result)
assert.truthy(err)
assert.truthy(err:find("No permission to access path"))
end,
})
end)
end)
describe("python", function()
it("should execute Python code and return output", function()
LlmTools.python(
{
path = ".",
code = "print('Hello from Python')",
},
nil,
function(result, err)
LlmTools.python({
path = ".",
code = "print('Hello from Python')",
}, {
session_ctx = {},
on_complete = function(result, err)
assert.is_nil(err)
assert.equals("Hello from Python\n", result)
end
)
end,
})
end)
it("should handle Python errors", function()
LlmTools.python(
{
path = ".",
code = "print(undefined_variable)",
},
nil,
function(result, err)
LlmTools.python({
path = ".",
code = "print(undefined_variable)",
}, {
session_ctx = {},
on_complete = function(result, err)
assert.is_nil(result)
assert.truthy(err)
assert.truthy(err:find("Error"))
end
)
end,
})
end)
it("should respect path permissions", function()
LlmTools.python(
{
path = "../outside_project",
code = "print('test')",
},
nil,
function(result, err)
LlmTools.python({
path = "../outside_project",
code = "print('test')",
}, {
session_ctx = {},
on_complete = function(result, err)
assert.is_nil(result)
assert.truthy(err:find("No permission to access path"))
end
)
end,
})
end)
it("should handle non-existent paths", function()
LlmTools.python(
{
path = "non_existent_dir",
code = "print('test')",
},
nil,
function(result, err)
LlmTools.python({
path = "non_existent_dir",
code = "print('test')",
}, {
session_ctx = {},
on_complete = function(result, err)
assert.is_nil(result)
assert.truthy(err:find("Path not found"))
end
)
end,
})
end)
it("should support custom container image", function()
os.execute("docker image rm python:3.12-slim")
LlmTools.python(
{
path = ".",
code = "print('Hello from custom container')",
container_image = "python:3.12-slim",
},
nil,
function(result, err)
LlmTools.python({
path = ".",
code = "print('Hello from custom container')",
container_image = "python:3.12-slim",
}, {
session_ctx = {},
on_complete = function(result, err)
assert.is_nil(err)
assert.equals("Hello from custom container\n", result)
end
)
end,
})
end)
end)
@@ -370,7 +380,7 @@ describe("llm_tools", function()
os.execute("touch " .. test_dir .. "/nested/file4.lua")
-- Test for lua files in the root
local result, err = glob({ path = ".", pattern = "*.lua" })
local result, err = glob({ path = ".", pattern = "*.lua" }, {})
assert.is_nil(err)
local files = vim.json.decode(result).matches
assert.equals(2, #files)
@@ -380,7 +390,7 @@ describe("llm_tools", function()
assert.falsy(vim.tbl_contains(files, test_dir .. "/nested/file4.lua"))
-- Test with recursive pattern
local result2, err2 = glob({ path = ".", pattern = "**/*.lua" })
local result2, err2 = glob({ path = ".", pattern = "**/*.lua" }, {})
assert.is_nil(err2)
local files2 = vim.json.decode(result2).matches
assert.equals(3, #files2)
@@ -390,13 +400,13 @@ describe("llm_tools", function()
end)
it("should respect path permissions", function()
local result, err = glob({ path = "../outside_project", pattern = "*.txt" })
local result, err = glob({ path = "../outside_project", pattern = "*.txt" }, {})
assert.equals("", result)
assert.truthy(err:find("No permission to access path"))
end)
it("should handle patterns without matches", function()
local result, err = glob({ path = ".", pattern = "*.nonexistent" })
local result, err = glob({ path = ".", pattern = "*.nonexistent" }, {})
assert.is_nil(err)
local files = vim.json.decode(result).matches
assert.equals(0, #files)
@@ -411,7 +421,7 @@ describe("llm_tools", function()
os.execute("touch " .. test_dir .. "/test_dir1/notignored1.lua")
os.execute("touch " .. test_dir .. "/test_dir1/notignored2.lua")
local result, err = glob({ path = ".", pattern = "**/*.lua" })
local result, err = glob({ path = ".", pattern = "**/*.lua" }, {})
assert.is_nil(err)
local files = vim.json.decode(result).matches