-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathskill_init.cpp
More file actions
162 lines (134 loc) · 5.99 KB
/
Copy pathskill_init.cpp
File metadata and controls
162 lines (134 loc) · 5.99 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
#include "skill_init.hpp"
#include "skill_registry.hpp"
#include "../config/config.hpp"
#include "../utils/encoding.hpp"
#include "../utils/paths.hpp"
#include "../utils/utf8_path.hpp"
#include <algorithm>
#include <filesystem>
#include <optional>
#include <unordered_set>
#include <vector>
namespace acecode {
namespace {
namespace fs = std::filesystem;
std::string env_or_expanded_path(const char* name, const std::string& fallback) {
std::string value = getenv_utf8(name);
if (!value.empty()) return value;
return expand_path(fallback);
}
void append_opencode_config_skill_roots(std::vector<fs::path>& roots,
const fs::path& base) {
if (base.empty()) return;
roots.emplace_back(base / "skills");
roots.emplace_back(base / "skill");
}
void append_opencode_project_skill_roots(std::vector<fs::path>& roots,
const fs::path& dir) {
append_opencode_config_skill_roots(roots, dir / ".opencode");
roots.emplace_back(dir / ".agents" / "skills");
roots.emplace_back(dir / ".claude" / "skills");
}
void append_opencode_global_skill_roots(std::vector<fs::path>& roots) {
const fs::path xdg_config =
path_from_utf8(env_or_expanded_path("XDG_CONFIG_HOME", "~/.config"));
append_opencode_config_skill_roots(roots, xdg_config / "opencode");
const fs::path home_opencode = path_from_utf8(expand_path("~/.opencode"));
append_opencode_config_skill_roots(roots, home_opencode);
const std::string custom_config = getenv_utf8("OPENCODE_CONFIG_DIR");
if (!custom_config.empty()) {
append_opencode_config_skill_roots(roots, path_from_utf8(custom_config));
}
roots.emplace_back(path_from_utf8(expand_path("~/.agents/skills")));
roots.emplace_back(path_from_utf8(expand_path("~/.claude/skills")));
const fs::path xdg_cache =
path_from_utf8(env_or_expanded_path("XDG_CACHE_HOME", "~/.cache"));
roots.emplace_back(xdg_cache / "opencode" / "skills");
}
} // namespace
std::vector<std::filesystem::path> project_skill_scan_roots(
const AppConfig& config, const std::string& working_dir) {
std::vector<std::filesystem::path> roots;
for (const auto& dir : get_project_dirs_up_to_home(working_dir)) {
roots.emplace_back(path_from_utf8(dir) / ".acecode" / "skills");
}
for (const auto& dir : get_project_dirs_up_to_home(working_dir)) {
roots.emplace_back(path_from_utf8(dir) / ".agent" / "skills");
}
if (config.skills.reuse_opencode) {
for (const auto& dir : get_project_dirs_up_to_home(working_dir)) {
append_opencode_project_skill_roots(roots, path_from_utf8(dir));
}
}
return roots;
}
std::vector<std::filesystem::path> global_skill_scan_roots(const AppConfig& config) {
std::vector<std::filesystem::path> roots;
roots.emplace_back(path_from_utf8(get_acecode_dir()) / "skills");
roots.emplace_back(path_from_utf8(expand_path("~/.agent/skills")));
if (config.skills.reuse_opencode) {
append_opencode_global_skill_roots(roots);
}
for (const auto& raw : config.skills.external_dirs) {
std::string expanded = expand_path(raw);
if (!expanded.empty()) roots.emplace_back(path_from_utf8(expanded));
}
return roots;
}
void initialize_skill_registry(SkillRegistry& skill_registry,
const AppConfig& config,
const std::string& working_dir,
const std::vector<std::filesystem::path>&
prepended_roots,
const std::optional<std::vector<std::string>>&
expert_allowed) {
std::error_code ec;
std::vector<std::filesystem::path> roots = prepended_roots;
auto project_roots = project_skill_scan_roots(config, working_dir);
for (auto& root : project_roots) roots.emplace_back(std::move(root));
// ~/.acecode/skills 是历史上自动创建的全局根,保持行为不变。
std::filesystem::path default_acecode_skills_dir =
path_from_utf8(get_acecode_dir()) / "skills";
if (!std::filesystem::exists(default_acecode_skills_dir, ec)) {
std::filesystem::create_directories(default_acecode_skills_dir, ec);
}
for (auto& root : global_skill_scan_roots(config)) {
roots.emplace_back(std::move(root));
}
std::optional<std::unordered_set<std::string>> effective_allowed;
std::unordered_set<std::string> effective_disabled(
config.skills.disabled.begin(), config.skills.disabled.end());
if (expert_allowed) {
std::unordered_set<std::string> selected(
expert_allowed->begin(), expert_allowed->end());
// Package-local Skills are part of the selected expert package rather
// than the installed/global catalog. Preserve every valid bundled
// Skill even when it was not named in capabilities.skills.
if (!prepended_roots.empty()) {
SkillRegistry packaged;
packaged.set_scan_roots(prepended_roots);
packaged.set_disabled({});
packaged.set_allowed(std::nullopt);
packaged.scan();
for (const auto& skill : packaged.list()) {
selected.insert(skill.name);
}
}
// Explicit expert selections have first priority over daemon-global
// allow/disable defaults. They still pass through ordinary permission,
// sandbox and runtime safety checks when tools execute.
for (const auto& name : selected) {
effective_disabled.erase(name);
}
effective_allowed = std::move(selected);
} else if (config.skills.allowed) {
effective_allowed = std::unordered_set<std::string>(
config.skills.allowed->begin(), config.skills.allowed->end());
}
skill_registry.configure(
std::move(roots),
std::move(effective_disabled),
std::move(effective_allowed));
skill_registry.scan();
}
} // namespace acecode