Repository navigation
Expand file tree
/
Copy pathfile_write_tool.cpp
More file actions
79 lines (66 loc) · 2.64 KB
/
Copy pathfile_write_tool.cpp
File metadata and controls
79 lines (66 loc) · 2.64 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
69
70
71
72
73
74
75
76
77
78
79
#include "file_write_tool.hpp"
#include <fstream>
#include <filesystem>
#include <fmt/core.h>
namespace fs = std::filesystem;
namespace claw::tools {
std::string FileWriteTool::get_name() const {
return "FileWriteTool";
}
std::string FileWriteTool::get_description() const {
return
"Writes (or overwrites) content to a file at the specified path. "
"Creates any necessary parent directories automatically. "
"Use this to create new source files, modify configurations, "
"write scripts, or save any text content.";
}
nlohmann::json FileWriteTool::get_input_schema() const {
return {
{"type", "object"},
{"properties", {
{"path", {
{"type", "string"},
{"description", "Absolute or relative path to the file to write."}
}},
{"content", {
{"type", "string"},
{"description", "The full text content to write to the file."}
}}
}},
{"required", nlohmann::json::array({"path", "content"})}
};
}
std::string FileWriteTool::execute(const nlohmann::json& input) {
if (!input.contains("path") || !input["path"].is_string()) {
return "[FileWriteTool Error] Input must contain a 'path' string.";
}
if (!input.contains("content") || !input["content"].is_string()) {
return "[FileWriteTool Error] Input must contain a 'content' string.";
}
std::string path = input["path"].get<std::string>();
std::string content = input["content"].get<std::string>();
// Create parent directories if they don't exist
fs::path file_path(path);
if (file_path.has_parent_path()) {
std::error_code ec;
fs::create_directories(file_path.parent_path(), ec);
if (ec) {
return fmt::format(
"[FileWriteTool Error] Cannot create directories for {}: {}",
path, ec.message()
);
}
}
std::ofstream file(path, std::ios::trunc);
if (!file.is_open()) {
return fmt::format("[FileWriteTool Error] Cannot open file for writing: {}", path);
}
file << content;
file.close();
if (file.fail()) {
return fmt::format("[FileWriteTool Error] Write failed for: {}", path);
}
auto written_size = fs::file_size(path);
return fmt::format("Successfully wrote {} bytes to {}", written_size, path);
}
} // namespace claw::tools