Adding file list tool
This commit is contained in:
@@ -15,6 +15,7 @@ add_library(drharness_lib
|
||||
tools/shell_tool.cpp
|
||||
tools/agent_tool.cpp
|
||||
tools/edit_tool.cpp
|
||||
tools/file_list_tool.cpp
|
||||
)
|
||||
target_include_directories(drharness_lib PUBLIC include tools)
|
||||
target_link_libraries(drharness_lib
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "shell_tool.hpp"
|
||||
#include "agent_tool.hpp"
|
||||
#include "edit_tool.hpp"
|
||||
#include "file_list_tool.hpp"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
@@ -26,6 +27,7 @@ static const std::vector<std::string> default_tools =
|
||||
{
|
||||
"run_shell",
|
||||
"edit",
|
||||
"list_files",
|
||||
};
|
||||
|
||||
|
||||
@@ -39,6 +41,7 @@ int main(int argc, char** argv) {
|
||||
ToolRegistry agent_tools;
|
||||
master_registery.register_tool(make_shell_tool());
|
||||
master_registery.register_tool(make_edit_tool());
|
||||
master_registery.register_tool(make_file_list_tool());
|
||||
// Add more base tools here:
|
||||
// registry.register_tool(make_file_read_tool());
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "file_list_tool.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 absolute path to the directory to list."}
|
||||
}},
|
||||
{"recursive", {
|
||||
{"type", "boolean"},
|
||||
{"description", "Whether to list files recursively. Defaults to false."}
|
||||
}}
|
||||
}},
|
||||
{"required", {"path"}}
|
||||
},
|
||||
.execute = [](const nlohmann::json& args) -> std::string {
|
||||
if (!args.contains("path") || !args["path"].is_string())
|
||||
return "Error: missing or invalid required parameter 'path'";
|
||||
|
||||
std::string dir_path = args["path"].get<std::string>();
|
||||
bool recursive = args.value("recursive", false);
|
||||
|
||||
if (!std::filesystem::exists(dir_path))
|
||||
return "Error: path does not exist: " + dir_path;
|
||||
if (!std::filesystem::is_directory(dir_path))
|
||||
return "Error: path is not a directory: " + dir_path;
|
||||
|
||||
std::string output;
|
||||
if (recursive) {
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(dir_path,
|
||||
std::filesystem::directory_options::skip_permission_denied)) {
|
||||
output += entry.path().string() + (entry.is_directory() ? "/" : "\n");
|
||||
}
|
||||
} else {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(dir_path)) {
|
||||
output += entry.path().string() + (entry.is_directory() ? "/" : "\n");
|
||||
}
|
||||
}
|
||||
return output.empty() ? "(empty directory)" : output;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include "tool.hpp"
|
||||
|
||||
Tool make_file_list_tool();
|
||||
Reference in New Issue
Block a user