Adding read file tool

This commit is contained in:
2026-05-04 13:36:53 -05:00
parent 1732b68dcc
commit 3ce89b690a
4 changed files with 51 additions and 1 deletions
+1
View File
@@ -16,6 +16,7 @@ add_library(drharness_lib
tools/agent_tool.cpp
tools/edit_tool.cpp
tools/file_list_tool.cpp
tools/file_read_tool.cpp
)
target_include_directories(drharness_lib PUBLIC include tools)
target_link_libraries(drharness_lib
+3 -1
View File
@@ -6,6 +6,7 @@
#include "agent_tool.hpp"
#include "edit_tool.hpp"
#include "file_list_tool.hpp"
#include "file_read_tool.hpp"
#include <cstdlib>
#include <iostream>
#include <fstream>
@@ -28,6 +29,7 @@ static const std::vector<std::string> default_tools =
"run_shell",
"edit",
"list_files",
"read_file",
};
@@ -42,8 +44,8 @@ int main(int argc, char** argv) {
master_registery.register_tool(make_shell_tool());
master_registery.register_tool(make_edit_tool());
master_registery.register_tool(make_file_list_tool());
master_registery.register_tool(make_file_read_tool());
// Add more base tools here:
// registry.register_tool(make_file_read_tool());
for (const auto& profile : profiles)
master_registery.register_tool(make_agent_tool(profile, master_registery, cfg.base_url));
+43
View File
@@ -0,0 +1,43 @@
#include "file_read_tool.hpp"
#include <fstream>
#include <stdexcept>
#include <string>
#include <filesystem>
Tool make_file_read_tool() {
return Tool{
.name = "read_file",
.description = "Read the contents of a text file.",
.parameters_schema = {
{"type", "object"},
{"properties", {
{"path", {
{"type", "string"},
{"description", "The absolute path to the file to read."}
}}
}},
{"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 file_path = args["path"].get<std::string>();
if (!std::filesystem::exists(file_path))
return "Error: file does not exist: " + file_path;
if (!std::filesystem::is_regular_file(file_path))
return "Error: path is not a regular file: " + file_path;
std::ifstream file(file_path);
if (!file.is_open())
return "Error: could not open file: " + file_path;
// Use char as template argument for text file
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
return content.empty() ? "(empty file)" : content;
}
};
}
+4
View File
@@ -0,0 +1,4 @@
#pragma once
#include "tool.hpp"
Tool make_file_read_tool();