46 lines
1.8 KiB
C++
46 lines
1.8 KiB
C++
#include "file_list_tool.hpp"
|
|
#include "json_types.hpp"
|
|
#include <filesystem>
|
|
#include <string>
|
|
|
|
Tool make_file_list_tool() {
|
|
return Tool{
|
|
.name = "list_files",
|
|
.description = "List the files and directories in a given directory path.",
|
|
.parameters_schema = {
|
|
{"type", "object"},
|
|
{"properties", {
|
|
{"path", {
|
|
{"type", "string"},
|
|
{"description", "The path to the directory to list (relative or absolute)."}
|
|
}},
|
|
{"recursive", {
|
|
{"type", "boolean"},
|
|
{"description", "Whether to list files recursively. Defaults to false."}
|
|
}}
|
|
}},
|
|
{"required", {"path"}}
|
|
},
|
|
.execute = create_tool_execute<ListFilesToolParams>([](const ListFilesToolParams& params) -> std::string {
|
|
|
|
if (!std::filesystem::exists(params.path))
|
|
return "Error: path does not exist: " + params.path;
|
|
if (!std::filesystem::is_directory(params.path))
|
|
return "Error: path is not a directory: " + params.path;
|
|
|
|
std::string output;
|
|
if (params.recursive) {
|
|
for (const auto& entry : std::filesystem::recursive_directory_iterator(params.path,
|
|
std::filesystem::directory_options::skip_permission_denied)) {
|
|
output += entry.path().string() + (entry.is_directory() ? "/\n" : "\n");
|
|
}
|
|
} else {
|
|
for (const auto& entry : std::filesystem::directory_iterator(params.path)) {
|
|
output += entry.path().string() + (entry.is_directory() ? "/\n" : "\n");
|
|
}
|
|
}
|
|
return output.empty() ? "(empty directory)" : output;
|
|
})
|
|
};
|
|
}
|