-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathskill_loader.cpp
More file actions
192 lines (166 loc) · 6.38 KB
/
Copy pathskill_loader.cpp
File metadata and controls
192 lines (166 loc) · 6.38 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
189
190
191
192
#include "skill_loader.hpp"
#include "frontmatter.hpp"
#include "../utils/encoding.hpp"
#include "../utils/logger.hpp"
#include "../utils/utf8_path.hpp"
#include <algorithm>
#include <cctype>
#include <fstream>
#include <sstream>
namespace fs = std::filesystem;
namespace acecode {
namespace {
constexpr size_t FRONTMATTER_READ_BUDGET = 8 * 1024; // first 8KB is plenty
std::string read_frontmatter_chunk(const fs::path& path) {
std::ifstream ifs(path, std::ios::binary);
if (!ifs.is_open()) return "";
std::string buf(FRONTMATTER_READ_BUDGET, '\0');
ifs.read(buf.data(), static_cast<std::streamsize>(FRONTMATTER_READ_BUDGET));
buf.resize(static_cast<size_t>(ifs.gcount()));
// A SKILL.md larger than the read budget gets sliced at an arbitrary byte,
// often mid-character. Drop the partial trailing UTF-8 sequence so ensure_utf8
// doesn't see "invalid UTF-8" and re-decode the whole (valid UTF-8) chunk as
// the system codepage — which would turn the frontmatter into mojibake.
trim_trailing_partial_utf8(buf);
return ensure_utf8(buf);
}
std::string truncate(const std::string& s, size_t n) {
if (s.size() <= n) return s;
// 字节预算截断必须回退到 UTF-8 序列边界,否则切在多字节字符中间会
// 留下"残缺引导字节 + '.'"的非法序列(如 0xE9 0x94 + "..." 的 0x2E),
// 进入 skills 索引后会被 body.dump() 以 type_error.316 打挂整个请求。
return truncate_utf8_prefix(s, n, "...");
}
std::string first_non_empty_body_line(const std::string& body) {
std::istringstream iss(body);
std::string line;
while (std::getline(iss, line)) {
std::string t;
t.reserve(line.size());
size_t i = 0;
while (i < line.size() && std::isspace(static_cast<unsigned char>(line[i]))) ++i;
while (i < line.size()) t.push_back(line[i++]);
while (!t.empty() && std::isspace(static_cast<unsigned char>(t.back()))) t.pop_back();
if (t.empty()) continue;
if (t.front() == '#') continue;
return t;
}
return "";
}
std::string derive_category(const fs::path& skill_dir, const fs::path& scan_root) {
std::error_code ec;
auto rel = fs::relative(skill_dir, scan_root, ec);
if (ec) return "";
auto it = rel.begin();
if (it == rel.end()) return "";
// rel = "<category>/<name>" (at minimum two components) → first segment
// is the category. A flat "<name>" layout has no category.
auto first = *it;
++it;
if (it == rel.end()) return "";
return path_to_utf8(first);
}
} // namespace
std::string current_platform_identifier() {
#if defined(_WIN32)
return "windows";
#elif defined(__APPLE__)
return "macos";
#elif defined(__linux__)
return "linux";
#else
return "unknown";
#endif
}
bool skill_matches_platform(const std::vector<std::string>& platforms) {
if (platforms.empty()) return true;
std::string current = current_platform_identifier();
for (const auto& p : platforms) {
std::string lower;
lower.reserve(p.size());
for (char c : p) lower.push_back(static_cast<char>(std::tolower(static_cast<unsigned char>(c))));
if (lower == current) return true;
}
return false;
}
std::string normalize_skill_command_key(const std::string& name) {
std::string out;
out.reserve(name.size());
for (char c : name) {
unsigned char uc = static_cast<unsigned char>(c);
if (uc == ' ' || uc == '_') {
out.push_back('-');
} else if (std::isalnum(uc)) {
out.push_back(static_cast<char>(std::tolower(uc)));
} else if (c == '-') {
out.push_back('-');
}
// else: drop silently (matches hermes behaviour)
}
// Collapse multiple hyphens and trim.
std::string collapsed;
collapsed.reserve(out.size());
bool last_hyphen = false;
for (char c : out) {
if (c == '-') {
if (last_hyphen) continue;
last_hyphen = true;
} else {
last_hyphen = false;
}
collapsed.push_back(c);
}
while (!collapsed.empty() && collapsed.front() == '-') collapsed.erase(collapsed.begin());
while (!collapsed.empty() && collapsed.back() == '-') collapsed.pop_back();
return collapsed;
}
std::optional<SkillMetadata> load_skill_from_dir(const fs::path& dir,
const fs::path& scan_root) {
fs::path skill_md = dir / "SKILL.md";
std::error_code ec;
if (!fs::exists(skill_md, ec) || !fs::is_regular_file(skill_md, ec)) {
return std::nullopt;
}
std::string chunk = read_frontmatter_chunk(skill_md);
if (chunk.empty()) {
LOG_WARN("[skills] Empty or unreadable SKILL.md: " + path_to_utf8(skill_md));
return std::nullopt;
}
auto [fm, body] = parse_frontmatter(chunk);
SkillMetadata meta;
meta.skill_md_path = skill_md;
meta.skill_dir = dir;
meta.scan_root = scan_root;
std::string fm_name = get_string(fm, "name");
if (fm_name.empty()) {
meta.name = path_to_utf8(dir.filename());
} else {
meta.name = fm_name;
}
meta.command_key = normalize_skill_command_key(meta.name);
if (meta.command_key.empty()) {
LOG_WARN("[skills] Skill name '" + meta.name + "' has no usable command slug; skipping " + path_to_utf8(skill_md));
return std::nullopt;
}
std::string desc = get_string(fm, "description");
if (desc.empty()) desc = first_non_empty_body_line(body);
meta.description = ensure_utf8(truncate(desc, 1024));
// 可选触发条件:写明"什么时候该用这个 skill"。主键 whenToUse 与
// claude-code 的 frontmatter 约定一致,snake_case 作别名。
std::string when = get_string(fm, "whenToUse");
if (when.empty()) when = get_string(fm, "when_to_use");
meta.when_to_use = ensure_utf8(truncate(when, 1024));
meta.category = derive_category(dir, scan_root);
meta.platforms = get_list(fm, "platforms");
// tags live under metadata.hermes.tags (hermes convention) OR metadata.tags
if (const FrontmatterValue* tv = get_nested(fm, {"metadata", "hermes", "tags"})) {
if (tv->is_list()) meta.tags = tv->list_value;
}
if (meta.tags.empty()) {
if (const FrontmatterValue* tv = get_nested(fm, {"metadata", "tags"})) {
if (tv->is_list()) meta.tags = tv->list_value;
}
}
return meta;
}
} // namespace acecode