-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathskill_view_tool.cpp
More file actions
188 lines (167 loc) · 7.01 KB
/
Copy pathskill_view_tool.cpp
File metadata and controls
188 lines (167 loc) · 7.01 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "skill_view_tool.hpp"
#include "../config/config.hpp"
#include "../skills/skill_init.hpp"
#include "../skills/skill_registry.hpp"
#include "tool_icons.hpp"
#include "../utils/logger.hpp"
#include <nlohmann/json.hpp>
#include <filesystem>
#include <fstream>
#include <memory>
#include <sstream>
namespace fs = std::filesystem;
namespace acecode {
namespace {
constexpr size_t MAX_FILE_SIZE = 2 * 1024 * 1024; // 2MB cap for SKILL files
nlohmann::json available_skills_list(const SkillRegistry& registry) {
auto all = registry.list();
nlohmann::json arr = nlohmann::json::array();
const size_t limit = 20;
for (size_t i = 0; i < all.size() && i < limit; ++i) {
arr.push_back(all[i].name);
}
return arr;
}
ToolSummary make_loaded_summary(const std::string& name,
const std::string& file_path = std::string()) {
ToolSummary summary;
summary.icon = tool_icon("skill_view");
summary.verb = "skill_view";
summary.object = file_path.empty()
? name + " loaded"
: name + " " + file_path + " loaded";
return summary;
}
std::unique_ptr<SkillRegistry> workspace_registry_for_context(
const AppConfig* config,
const ToolContext& ctx) {
if (!config || ctx.cwd.empty()) return nullptr;
auto scoped = std::make_unique<SkillRegistry>();
initialize_skill_registry(*scoped, *config, ctx.cwd);
return scoped;
}
} // namespace
ToolImpl create_skill_view_tool(SkillRegistry& registry,
const AppConfig* config) {
ToolDef def;
def.name = "skill_view";
def.description = "Load the full content of a skill. Without file_path, returns the SKILL.md body. "
"With file_path, returns the content of a supporting file inside the skill's "
"references/, templates/, scripts/, or assets/ directory. "
"Path traversal ('..') is rejected.";
def.parameters = nlohmann::json({
{"type", "object"},
{"properties", {
{"name", {
{"type", "string"},
{"description", "Skill name (as returned by skills_list)."}
}},
{"file_path", {
{"type", "string"},
{"description", "Optional path relative to the skill directory (e.g. 'references/api.md')."}
}}
}},
{"required", nlohmann::json::array({"name"})}
});
auto execute = [®istry, config](const std::string& arguments_json,
const ToolContext& ctx) -> ToolResult {
std::string name;
std::string file_path;
try {
auto args = nlohmann::json::parse(arguments_json);
name = args.value("name", "");
file_path = args.value("file_path", "");
} catch (...) {
return ToolResult{"[Error] Failed to parse tool arguments.", false};
}
if (name.empty()) {
nlohmann::json err;
err["success"] = false;
err["error"] = "Missing required argument 'name'.";
return ToolResult{err.dump(), false};
}
auto scoped_registry = ctx.skill_registry
? nullptr : workspace_registry_for_context(config, ctx);
const SkillRegistry& active_registry = ctx.skill_registry
? *ctx.skill_registry
: (scoped_registry ? *scoped_registry : registry);
auto meta = active_registry.find(name);
if (!meta) {
nlohmann::json err;
err["success"] = false;
err["error"] = "Skill '" + name + "' not found.";
err["available_skills"] = available_skills_list(active_registry);
err["hint"] = "Call skills_list to see all installed skills.";
return ToolResult{err.dump(), false};
}
if (!file_path.empty()) {
if (file_path.find("..") != std::string::npos) {
nlohmann::json err;
err["success"] = false;
err["error"] = "Path traversal ('..') is not allowed.";
return ToolResult{err.dump(), false};
}
auto resolved = active_registry.resolve_skill_file(name, file_path);
if (!resolved) {
nlohmann::json err;
err["success"] = false;
err["error"] = "File '" + file_path + "' not found or outside skill directory.";
err["supporting_files"] = active_registry.list_supporting_files(name);
return ToolResult{err.dump(), false};
}
std::error_code ec;
if (!fs::exists(*resolved, ec) || !fs::is_regular_file(*resolved, ec)) {
nlohmann::json err;
err["success"] = false;
err["error"] = "File '" + file_path + "' does not exist.";
err["supporting_files"] = active_registry.list_supporting_files(name);
return ToolResult{err.dump(), false};
}
auto size = fs::file_size(*resolved, ec);
if (!ec && size > MAX_FILE_SIZE) {
nlohmann::json err;
err["success"] = false;
err["error"] = "File too large to return inline.";
return ToolResult{err.dump(), false};
}
std::ifstream ifs(*resolved, std::ios::binary);
std::ostringstream oss;
oss << ifs.rdbuf();
nlohmann::json out;
out["success"] = true;
out["name"] = meta->name;
out["file_path"] = file_path;
out["content"] = oss.str();
LOG_DEBUG("[skill_view] loaded supporting file " + file_path + " for skill " + meta->name);
ToolResult result{out.dump(), true};
result.summary = make_loaded_summary(meta->name, file_path);
return result;
}
// Main SKILL.md body path.
std::string body = active_registry.read_skill_body(meta->name);
if (body.empty()) {
std::ifstream ifs(meta->skill_md_path, std::ios::binary);
std::ostringstream oss;
oss << ifs.rdbuf();
body = oss.str();
}
auto supporting = active_registry.list_supporting_files(meta->name);
nlohmann::json out;
out["success"] = true;
out["name"] = meta->name;
out["description"] = meta->description;
if (!meta->category.empty()) out["category"] = meta->category;
out["content"] = body;
nlohmann::json files_arr = nlohmann::json::array();
for (const auto& f : supporting) files_arr.push_back(f);
out["linked_files"] = files_arr;
LOG_DEBUG("[skill_view] loaded SKILL.md for " + meta->name + " (" +
std::to_string(body.size()) + " bytes, " +
std::to_string(supporting.size()) + " supporting files)");
ToolResult result{out.dump(), true};
result.summary = make_loaded_summary(meta->name);
return result;
};
return ToolImpl{def, execute, /*is_read_only=*/true};
}
} // namespace acecode