Repository navigation
Expand file tree
/
Copy pathfile_read_tool.cpp
More file actions
68 lines (56 loc) · 2.12 KB
/
Copy pathfile_read_tool.cpp
File metadata and controls
68 lines (56 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "file_read_tool.hpp"
#include <fstream>
#include <sstream>
#include <filesystem>
#include <fmt/core.h>
namespace fs = std::filesystem;
namespace claw::tools {
std::string FileReadTool::get_name() const {
return "FileReadTool";
}
std::string FileReadTool::get_description() const {
return
"Reads the contents of a file at the specified path and returns "
"it as a string. Use this to inspect source code, configuration "
"files, logs, or any text file on the user's system.";
}
nlohmann::json FileReadTool::get_input_schema() const {
return {
{"type", "object"},
{"properties", {
{"path", {
{"type", "string"},
{"description", "Absolute or relative path to the file to read."}
}}
}},
{"required", nlohmann::json::array({"path"})}
};
}
std::string FileReadTool::execute(const nlohmann::json& input) {
if (!input.contains("path") || !input["path"].is_string()) {
return "[FileReadTool Error] Input must contain a 'path' string.";
}
std::string path = input["path"].get<std::string>();
if (!fs::exists(path)) {
return fmt::format("[FileReadTool Error] File not found: {}", path);
}
if (!fs::is_regular_file(path)) {
return fmt::format("[FileReadTool Error] Not a regular file: {}", path);
}
// Check file size — refuse to read files larger than 512 KB
auto file_size = fs::file_size(path);
if (file_size > 512 * 1024) {
return fmt::format(
"[FileReadTool Error] File is too large ({} bytes, max 524288). "
"Consider reading specific lines with BashTool instead.", file_size
);
}
std::ifstream file(path);
if (!file.is_open()) {
return fmt::format("[FileReadTool Error] Cannot open file: {}", path);
}
std::ostringstream ss;
ss << file.rdbuf();
return ss.str();
}
} // namespace claw::tools