41 lines
1.4 KiB
C++
41 lines
1.4 KiB
C++
#include "file_read_tool.hpp"
|
|
#include "json_types.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 path to the file to read (relative or absolute)."}
|
|
}}
|
|
}},
|
|
{"required", {"path"}}
|
|
},
|
|
.execute = create_tool_execute<ReadFileToolParams>([](const ReadFileToolParams& params) -> std::string {
|
|
|
|
if (!std::filesystem::exists(params.path))
|
|
return "Error: file does not exist: " + params.path;
|
|
if (!std::filesystem::is_regular_file(params.path))
|
|
return "Error: path is not a regular file: " + params.path;
|
|
|
|
std::ifstream file(params.path);
|
|
if (!file.is_open())
|
|
return "Error: could not open file: " + params.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;
|
|
})
|
|
};
|
|
}
|