-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathglob_tool.cpp
More file actions
198 lines (168 loc) · 6.06 KB
/
Copy pathglob_tool.cpp
File metadata and controls
198 lines (168 loc) · 6.06 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
193
194
195
196
197
198
#include "glob_tool.hpp"
#include "ignore_utils.hpp"
#include "utils/utf8_path.hpp"
#include <nlohmann/json.hpp>
#include <filesystem>
#include <sstream>
#include <vector>
#include <algorithm>
namespace acecode {
static constexpr size_t MAX_GLOB_RESULTS = 500;
// 用户点「停止」后工具必须尽快返回:大仓库 + 慢盘上的全量递归遍历可阻塞数十秒,
// 期间 abort 只能干等(2026-07-11 日志复盘,同 grep_tool / McpManager::invoke)。
static constexpr const char* ABORTED_MSG =
"[Aborted] Search abandoned because the user aborted the turn.";
// Simple glob pattern matcher supporting *, **, and ?
static bool glob_match(const std::string& pattern, const std::string& path) {
// Split pattern and path into segments by '/' or '\'
auto split = [](const std::string& s) {
std::vector<std::string> parts;
std::string part;
for (char c : s) {
if (c == '/' || c == '\\') {
if (!part.empty()) {
parts.push_back(part);
part.clear();
}
} else {
part += c;
}
}
if (!part.empty()) parts.push_back(part);
return parts;
};
// Match a single segment pattern (with * and ?) against a string
std::function<bool(const char*, const char*)> match_segment;
match_segment = [&](const char* p, const char* s) -> bool {
while (*p && *s) {
if (*p == '*') {
p++;
// '*' matches any sequence within a segment
while (*s) {
if (match_segment(p, s)) return true;
s++;
}
return match_segment(p, s);
}
if (*p == '?') {
p++;
s++;
continue;
}
if (*p != *s) return false;
p++;
s++;
}
while (*p == '*') p++;
return *p == 0 && *s == 0;
};
auto pat_parts = split(pattern);
auto path_parts = split(path);
// Recursive match with ** support
std::function<bool(size_t, size_t)> match_parts;
match_parts = [&](size_t pi, size_t si) -> bool {
if (pi == pat_parts.size() && si == path_parts.size()) return true;
if (pi == pat_parts.size()) return false;
if (pat_parts[pi] == "**") {
// ** matches zero or more directories
for (size_t i = si; i <= path_parts.size(); i++) {
if (match_parts(pi + 1, i)) return true;
}
return false;
}
if (si == path_parts.size()) return false;
if (match_segment(pat_parts[pi].c_str(), path_parts[si].c_str())) {
return match_parts(pi + 1, si + 1);
}
return false;
};
return match_parts(0, 0);
}
static ToolResult execute_glob(const std::string& arguments_json, const ToolContext& ctx) {
std::string pattern;
std::string search_path;
try {
auto args = nlohmann::json::parse(arguments_json);
pattern = args.value("pattern", "");
search_path = args.value("path", "");
} catch (...) {
return ToolResult{"[Error] Failed to parse tool arguments.", false};
}
if (pattern.empty()) {
return ToolResult{"[Error] No pattern provided.", false};
}
if (search_path.empty()) {
search_path = ctx.cwd.empty() ? current_path_utf8() : ctx.cwd;
}
const auto search_root = path_from_utf8(search_path);
if (!std::filesystem::is_directory(search_root)) {
return ToolResult{"[Error] Path is not a directory: " + search_path, false};
}
std::vector<std::string> matches;
bool truncated = false;
std::error_code ec;
for (auto it = std::filesystem::recursive_directory_iterator(
search_root,
std::filesystem::directory_options::skip_permission_denied,
ec);
it != std::filesystem::recursive_directory_iterator(); ++it)
{
if (ctx.abort_flag && ctx.abort_flag->load()) {
return ToolResult{ABORTED_MSG, false};
}
if (ec) { ec.clear(); continue; }
if (it->is_directory()) {
if (should_ignore_dir(path_to_utf8(it->path().filename()))) {
it.disable_recursion_pending();
}
continue;
}
if (!it->is_regular_file()) continue;
auto rel = std::filesystem::relative(it->path(), search_root, ec);
if (ec) { ec.clear(); continue; }
std::string rel_str = path_to_utf8_generic(rel); // use forward slashes
if (glob_match(pattern, rel_str)) {
matches.push_back(rel_str);
if (matches.size() >= MAX_GLOB_RESULTS) {
truncated = true;
break;
}
}
}
if (matches.empty()) {
return ToolResult{"No files found matching pattern: " + pattern, true};
}
std::sort(matches.begin(), matches.end());
std::ostringstream out;
for (const auto& m : matches) {
out << m << "\n";
}
if (truncated) {
out << "\n[Results truncated at " << MAX_GLOB_RESULTS
<< " files. Narrow your pattern.]";
}
return ToolResult{out.str(), true};
}
ToolImpl create_glob_tool() {
ToolDef def;
def.name = "glob";
def.description = "Find files matching a glob pattern. Supports *, **, and ?. "
"Use to discover files in a project. "
"Skips .git, node_modules, build directories.";
def.parameters = nlohmann::json({
{"type", "object"},
{"properties", {
{"pattern", {
{"type", "string"},
{"description", "Glob pattern (e.g. 'src/**/*.cpp', '*.hpp', '**/*.json')"}
}},
{"path", {
{"type", "string"},
{"description", "Directory to search in (default: CWD). Optional."}
}}
}},
{"required", nlohmann::json::array({"pattern"})}
});
return ToolImpl{def, execute_glob, /*is_read_only=*/true};
}
} // namespace acecode