Files
avante.nvim/lua/avante/llm_tools/ls.lua
2025-07-02 16:58:15 +08:00

68 lines
1.8 KiB
Lua

local Utils = require("avante.utils")
local Helpers = require("avante.llm_tools.helpers")
local Base = require("avante.llm_tools.base")
---@class AvanteLLMTool
local M = setmetatable({}, Base)
M.name = "ls"
M.description = "List files and directories in a given path in current project scope"
---@type AvanteLLMToolParam
M.param = {
type = "table",
fields = {
{
name = "path",
description = "Relative path to the project directory",
type = "string",
},
{
name = "max_depth",
description = "Maximum depth of the directory",
type = "integer",
},
},
usage = {
path = "Relative path to the project directory",
max_depth = "Maximum depth of the directory",
},
}
---@type AvanteLLMToolReturn[]
M.returns = {
{
name = "entries",
description = "List of file paths and directorie paths in the given directory",
type = "string[]",
},
{
name = "error",
description = "Error message if the directory was not listed successfully",
type = "string",
optional = true,
},
}
---@type AvanteLLMToolFunc<{ path: string, max_depth?: integer }>
function M.func(opts, on_log, on_complete, session_ctx)
local abs_path = Helpers.get_abs_path(opts.path)
if not Helpers.has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end
if on_log then on_log("path: " .. abs_path) end
if on_log then on_log("max depth: " .. tostring(opts.max_depth)) end
local files = Utils.scan_directory({
directory = abs_path,
add_dirs = true,
max_depth = opts.max_depth,
})
local filepaths = {}
for _, file in ipairs(files) do
local uniform_path = Utils.uniform_path(file)
table.insert(filepaths, uniform_path)
end
return vim.json.encode(filepaths), nil
end
return M