@mwillbanks/ast-mcp 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -20
- package/dist/ast-mcp.js +232 -130
- package/dist/ast-mcp.js.map +107 -27
- package/package.json +2 -1
- package/src/ast-bro/capability.ts +3 -8
- package/src/ast-bro/client.ts +18 -2
- package/src/ast-bro/result.ts +5 -12
- package/src/cli.ts +70 -95
- package/src/config-cli.ts +54 -0
- package/src/config.ts +886 -0
- package/src/helpers/aider.ts +252 -0
- package/src/helpers/ast-bro.ts +64 -0
- package/src/helpers/config.ts +149 -0
- package/src/helpers/hook.ts +60 -0
- package/src/helpers/installer.ts +249 -0
- package/src/helpers/json.ts +19 -0
- package/src/helpers/mcp-schema.ts +31 -0
- package/src/helpers/process.ts +31 -0
- package/src/helpers/string.ts +97 -0
- package/src/hook.ts +135 -40
- package/src/http-entry.ts +1 -1
- package/src/http.ts +118 -103
- package/src/installer-transport.ts +190 -0
- package/src/installer.ts +604 -247
- package/src/mcp-cli.ts +104 -0
- package/src/patch/aider.ts +6 -199
- package/src/patch/engine.ts +271 -197
- package/src/runtime/dependencies.ts +48 -29
- package/src/runtime/file-delete.ts +13 -9
- package/src/runtime/file-read.ts +91 -57
- package/src/runtime/file-rename.ts +158 -0
- package/src/runtime/format.ts +57 -34
- package/src/runtime/paths.ts +30 -16
- package/src/runtime/policy.ts +21 -0
- package/src/runtime/process-input.ts +14 -4
- package/src/service.ts +217 -0
- package/src/shell-policy.ts +331 -0
- package/src/stdio.ts +47 -35
- package/src/tools/ast-bro.ts +12 -6
- package/src/tools/configured.ts +28 -0
- package/src/tools/files.ts +24 -27
- package/src/tools/index.ts +5 -3
- package/src/tools/lifecycle.ts +69 -55
- package/templates/skills/ast-mcp/SKILL.md +7 -6
- package/templates/skills/ast-mcp/evals/batch.evals.json +48 -0
- package/templates/skills/ast-mcp/evals/evals.json +1 -0
- package/templates/skills/ast-mcp/evals/score.test.ts +175 -0
- package/templates/skills/ast-mcp/evals/score.ts +304 -267
- package/templates/skills/ast-mcp/evals/semantic.test.ts +322 -0
- package/templates/skills/ast-mcp/evals/semantic.ts +390 -135
- package/templates/skills/ast-mcp/evals/shell-policy.ts +252 -0
- package/templates/skills/ast-mcp/evals/source.test.ts +46 -0
- package/templates/skills/ast-mcp/evals/source.ts +164 -0
- package/templates/skills/ast-mcp/evals/trigger-queries.json +1 -1
- package/templates/skills/ast-mcp/references/installation.md +9 -8
- package/templates/skills/ast-mcp/references/patch-state-machine.md +5 -5
- package/templates/skills/ast-mcp/references/tool-catalog.md +4 -2
- package/templates/skills/ast-mcp/scripts/check-install.ts +323 -131
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { DiffMatchPatch } from "diff-match-patch-ts";
|
|
2
|
+
import type { AiderReplacement, SearchReplaceBlock } from "../patch/aider";
|
|
3
|
+
import { normalizeNewlines, normalizeWhitespaceLine } from "./string";
|
|
4
|
+
|
|
5
|
+
export function parseAiderBlocksFromOutput(
|
|
6
|
+
output: string,
|
|
7
|
+
): SearchReplaceBlock[] {
|
|
8
|
+
const blocks: SearchReplaceBlock[] = [];
|
|
9
|
+
const lines = output.split(/\r?\n/);
|
|
10
|
+
let filename = "";
|
|
11
|
+
let mode: "outside" | "search" | "replace" = "outside";
|
|
12
|
+
let search: string[] = [];
|
|
13
|
+
let replace: string[] = [];
|
|
14
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
15
|
+
const line = lines[index];
|
|
16
|
+
if (line.startsWith("<<<<<<< SEARCH")) {
|
|
17
|
+
const prior = lines[index - 1]?.trim();
|
|
18
|
+
if (prior && !prior.includes("```"))
|
|
19
|
+
filename = prior.replace(/[:`*]/g, "").trim();
|
|
20
|
+
search = [];
|
|
21
|
+
replace = [];
|
|
22
|
+
mode = "search";
|
|
23
|
+
} else if (line.startsWith("=======")) mode = "replace";
|
|
24
|
+
else if (line.startsWith(">>>>>>>")) {
|
|
25
|
+
if (mode !== "replace")
|
|
26
|
+
throw new Error(
|
|
27
|
+
"Malformed Aider block: replacement terminator arrived out of order",
|
|
28
|
+
);
|
|
29
|
+
blocks.push({
|
|
30
|
+
filename,
|
|
31
|
+
replace: replace.join("\n"),
|
|
32
|
+
search: search.join("\n"),
|
|
33
|
+
});
|
|
34
|
+
mode = "outside";
|
|
35
|
+
} else if (mode === "search") search.push(line);
|
|
36
|
+
else if (mode === "replace") replace.push(line);
|
|
37
|
+
}
|
|
38
|
+
if (mode !== "outside")
|
|
39
|
+
throw new Error("Malformed Aider block: unterminated SEARCH/REPLACE block");
|
|
40
|
+
return blocks;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function replaceLines(
|
|
44
|
+
contentLines: string[],
|
|
45
|
+
start: number,
|
|
46
|
+
count: number,
|
|
47
|
+
replacement: string,
|
|
48
|
+
): string {
|
|
49
|
+
const before = contentLines.slice(0, start).join("\n");
|
|
50
|
+
const after = contentLines.slice(start + count).join("\n");
|
|
51
|
+
return (
|
|
52
|
+
before + (before ? "\n" : "") + replacement + (after ? "\n" : "") + after
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function exactReplacement(
|
|
57
|
+
content: string,
|
|
58
|
+
search: string,
|
|
59
|
+
replace: string,
|
|
60
|
+
): AiderReplacement | undefined {
|
|
61
|
+
const matches = content.split(search).length - 1;
|
|
62
|
+
if (matches > 1) throw new Error("Aider exact match is ambiguous");
|
|
63
|
+
return matches === 1
|
|
64
|
+
? { content: content.replace(search, () => replace), method: "exact" }
|
|
65
|
+
: undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function whitespaceReplacement(
|
|
69
|
+
content: string,
|
|
70
|
+
search: string,
|
|
71
|
+
replace: string,
|
|
72
|
+
): AiderReplacement | undefined {
|
|
73
|
+
const contentLines = content.split("\n");
|
|
74
|
+
const searchLines = search.split("\n");
|
|
75
|
+
const starts = contentLines
|
|
76
|
+
.map((_, start) => start)
|
|
77
|
+
.filter((start) =>
|
|
78
|
+
searchLines.every(
|
|
79
|
+
(line, offset) =>
|
|
80
|
+
normalizeWhitespaceLine(contentLines[start + offset] ?? "") ===
|
|
81
|
+
normalizeWhitespaceLine(line),
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
if (starts.length > 1) throw new Error("Aider whitespace match is ambiguous");
|
|
85
|
+
return starts.length === 1
|
|
86
|
+
? {
|
|
87
|
+
content: replaceLines(
|
|
88
|
+
contentLines,
|
|
89
|
+
starts[0],
|
|
90
|
+
searchLines.length,
|
|
91
|
+
replace,
|
|
92
|
+
),
|
|
93
|
+
method: "whitespace",
|
|
94
|
+
}
|
|
95
|
+
: undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function shiftedSearches(
|
|
99
|
+
contentLines: string[],
|
|
100
|
+
searchLines: string[],
|
|
101
|
+
): string[] {
|
|
102
|
+
const sample = searchLines.find((line) => line.trim());
|
|
103
|
+
if (!sample) return [];
|
|
104
|
+
const searchIndent = sample.match(/^[ \t]*/)?.[0] ?? "";
|
|
105
|
+
return contentLines
|
|
106
|
+
.filter((line) => line.trim() === sample.trim())
|
|
107
|
+
.map((line) => line.match(/^[ \t]*/)?.[0] ?? "")
|
|
108
|
+
.filter((indent) => indent !== searchIndent)
|
|
109
|
+
.map((indent) =>
|
|
110
|
+
searchLines
|
|
111
|
+
.map((line) =>
|
|
112
|
+
line.startsWith(searchIndent)
|
|
113
|
+
? indent + line.slice(searchIndent.length)
|
|
114
|
+
: line,
|
|
115
|
+
)
|
|
116
|
+
.join("\n"),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function relativeIndentationReplacement(
|
|
121
|
+
content: string,
|
|
122
|
+
search: string,
|
|
123
|
+
replace: string,
|
|
124
|
+
): AiderReplacement | undefined {
|
|
125
|
+
const matches = shiftedSearches(content.split("\n"), search.split("\n"))
|
|
126
|
+
.map((shifted) => ({ index: content.indexOf(shifted), shifted }))
|
|
127
|
+
.filter(
|
|
128
|
+
({ index, shifted }) =>
|
|
129
|
+
index >= 0 && content.indexOf(shifted, index + 1) < 0,
|
|
130
|
+
);
|
|
131
|
+
if (matches.length > 1)
|
|
132
|
+
throw new Error("Aider indentation match is ambiguous");
|
|
133
|
+
const match = matches[0];
|
|
134
|
+
return match
|
|
135
|
+
? {
|
|
136
|
+
content:
|
|
137
|
+
content.slice(0, match.index) +
|
|
138
|
+
replace +
|
|
139
|
+
content.slice(match.index + match.shifted.length),
|
|
140
|
+
method: "relative-indentation",
|
|
141
|
+
}
|
|
142
|
+
: undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type FuzzyCandidate = { index: number; length: number; ratio: number };
|
|
146
|
+
|
|
147
|
+
function fuzzyLocations(
|
|
148
|
+
content: string,
|
|
149
|
+
anchor: string,
|
|
150
|
+
matcher: DiffMatchPatch,
|
|
151
|
+
): number[] {
|
|
152
|
+
const locations: number[] = [];
|
|
153
|
+
for (
|
|
154
|
+
let location = content.indexOf(anchor);
|
|
155
|
+
location >= 0;
|
|
156
|
+
location = content.indexOf(anchor, location + 1)
|
|
157
|
+
)
|
|
158
|
+
locations.push(location);
|
|
159
|
+
if (locations.length > 0) return locations;
|
|
160
|
+
const fallback = (
|
|
161
|
+
matcher as unknown as {
|
|
162
|
+
match_main(text: string, pattern: string, location: number): number;
|
|
163
|
+
}
|
|
164
|
+
).match_main(content, anchor, 0);
|
|
165
|
+
return fallback >= 0 ? [fallback] : [];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function bestFuzzyCandidate(
|
|
169
|
+
content: string,
|
|
170
|
+
search: string,
|
|
171
|
+
index: number,
|
|
172
|
+
adjustment: number,
|
|
173
|
+
matcher: DiffMatchPatch,
|
|
174
|
+
): FuzzyCandidate | undefined {
|
|
175
|
+
const minimum = Math.max(1, search.length - adjustment);
|
|
176
|
+
const maximum = Math.min(content.length - index, search.length + adjustment);
|
|
177
|
+
let best: FuzzyCandidate | undefined;
|
|
178
|
+
for (let length = minimum; length <= maximum; length += 1) {
|
|
179
|
+
const candidate = content.slice(index, index + length);
|
|
180
|
+
const edits = matcher.diff_levenshtein(
|
|
181
|
+
matcher.diff_main(search, candidate),
|
|
182
|
+
);
|
|
183
|
+
const ratio = edits / Math.max(search.length, length);
|
|
184
|
+
if (!best || ratio < best.ratio) best = { index, length, ratio };
|
|
185
|
+
else if (
|
|
186
|
+
ratio === best.ratio &&
|
|
187
|
+
Math.abs(length - search.length) < Math.abs(best.length - search.length)
|
|
188
|
+
)
|
|
189
|
+
best = { index, length, ratio };
|
|
190
|
+
}
|
|
191
|
+
return best;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function fuzzyReplacement(
|
|
195
|
+
content: string,
|
|
196
|
+
search: string,
|
|
197
|
+
replace: string,
|
|
198
|
+
): AiderReplacement {
|
|
199
|
+
const matcher = new DiffMatchPatch();
|
|
200
|
+
matcher.Match_Distance = Math.max(1000, content.length);
|
|
201
|
+
matcher.Match_Threshold = 0.5;
|
|
202
|
+
const anchor = search.slice(0, matcher.Match_MaxBits);
|
|
203
|
+
const adjustment = Math.min(64, Math.max(4, Math.ceil(search.length * 0.2)));
|
|
204
|
+
const candidates = fuzzyLocations(content, anchor, matcher)
|
|
205
|
+
.map((index) =>
|
|
206
|
+
bestFuzzyCandidate(content, search, index, adjustment, matcher),
|
|
207
|
+
)
|
|
208
|
+
.filter((candidate): candidate is FuzzyCandidate => Boolean(candidate))
|
|
209
|
+
.sort((left, right) => left.ratio - right.ratio);
|
|
210
|
+
const best = candidates[0];
|
|
211
|
+
if (!best || best.ratio > 0.5)
|
|
212
|
+
throw new Error(
|
|
213
|
+
"Aider cascade match failed: SEARCH block was not found with sufficient confidence",
|
|
214
|
+
);
|
|
215
|
+
if (
|
|
216
|
+
candidates[1] &&
|
|
217
|
+
Math.abs(candidates[1].ratio - best.ratio) < Number.EPSILON
|
|
218
|
+
)
|
|
219
|
+
throw new Error("Aider diff-match-patch match is ambiguous");
|
|
220
|
+
return {
|
|
221
|
+
content:
|
|
222
|
+
content.slice(0, best.index) +
|
|
223
|
+
replace +
|
|
224
|
+
content.slice(best.index + best.length),
|
|
225
|
+
method: "diff-match-patch",
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function applyAiderBlockCascade(
|
|
230
|
+
fileContent: string,
|
|
231
|
+
searchInput: string,
|
|
232
|
+
replaceInput: string,
|
|
233
|
+
): AiderReplacement {
|
|
234
|
+
const content = normalizeNewlines(fileContent);
|
|
235
|
+
const search = normalizeNewlines(searchInput);
|
|
236
|
+
const replace = normalizeNewlines(replaceInput);
|
|
237
|
+
if (!search.trim() && replace.trim())
|
|
238
|
+
return {
|
|
239
|
+
content: content + (content.endsWith("\n") ? "" : "\n") + replace,
|
|
240
|
+
method: "append",
|
|
241
|
+
};
|
|
242
|
+
if (!search)
|
|
243
|
+
throw new Error(
|
|
244
|
+
"Aider SEARCH block must not be empty when replacement is empty",
|
|
245
|
+
);
|
|
246
|
+
return (
|
|
247
|
+
exactReplacement(content, search, replace) ??
|
|
248
|
+
whitespaceReplacement(content, search, replace) ??
|
|
249
|
+
relativeIndentationReplacement(content, search, replace) ??
|
|
250
|
+
fuzzyReplacement(content, search, replace)
|
|
251
|
+
);
|
|
252
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export type IndexedAstBroTool = "search" | "find_related" | "index";
|
|
2
|
+
|
|
3
|
+
function stringArgument(
|
|
4
|
+
args: Record<string, unknown>,
|
|
5
|
+
key: string,
|
|
6
|
+
fallback: string,
|
|
7
|
+
): string {
|
|
8
|
+
return typeof args[key] === "string" ? args[key] : fallback;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function appendSearchArguments(
|
|
12
|
+
command: string[],
|
|
13
|
+
args: Record<string, unknown>,
|
|
14
|
+
root: string,
|
|
15
|
+
): void {
|
|
16
|
+
if (typeof args.query !== "string")
|
|
17
|
+
throw new Error("ast-bro search requires a query");
|
|
18
|
+
command.push(args.query, stringArgument(args, "path", root));
|
|
19
|
+
if (typeof args.alpha === "number")
|
|
20
|
+
command.push("--alpha", String(args.alpha));
|
|
21
|
+
for (const language of Array.isArray(args.languages) ? args.languages : [])
|
|
22
|
+
if (typeof language === "string") command.push("--lang", language);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function appendRelatedArguments(
|
|
26
|
+
command: string[],
|
|
27
|
+
args: Record<string, unknown>,
|
|
28
|
+
root: string,
|
|
29
|
+
): void {
|
|
30
|
+
if (typeof args.path !== "string" || typeof args.line !== "number")
|
|
31
|
+
throw new Error("ast-bro find_related requires path and line");
|
|
32
|
+
command.push(
|
|
33
|
+
"--file",
|
|
34
|
+
args.path,
|
|
35
|
+
"--line",
|
|
36
|
+
String(args.line),
|
|
37
|
+
stringArgument(args, "root", root),
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function appendCommonArguments(
|
|
42
|
+
command: string[],
|
|
43
|
+
args: Record<string, unknown>,
|
|
44
|
+
): void {
|
|
45
|
+
if (typeof args.top_k === "number")
|
|
46
|
+
command.push("--top-k", String(args.top_k));
|
|
47
|
+
if (args.rebuild === true) command.push("--rebuild");
|
|
48
|
+
if (args.stats === true) command.push("--stats");
|
|
49
|
+
if (args.json === true) command.push("--json", "--compact");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function indexedAstBroCommand(
|
|
53
|
+
toolName: IndexedAstBroTool,
|
|
54
|
+
args: Record<string, unknown>,
|
|
55
|
+
root: string,
|
|
56
|
+
): string[] {
|
|
57
|
+
const command = [toolName.replace("_", "-")];
|
|
58
|
+
if (toolName === "search") appendSearchArguments(command, args, root);
|
|
59
|
+
else if (toolName === "find_related")
|
|
60
|
+
appendRelatedArguments(command, args, root);
|
|
61
|
+
else command.push(stringArgument(args, "path", root));
|
|
62
|
+
appendCommonArguments(command, args);
|
|
63
|
+
return command;
|
|
64
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
type ConfigLayer = {
|
|
4
|
+
dependencies?: {
|
|
5
|
+
ast_bro_binary?: string;
|
|
6
|
+
dprint_binary?: string;
|
|
7
|
+
};
|
|
8
|
+
formatting?: {
|
|
9
|
+
dprint_config?: string;
|
|
10
|
+
formatters?: Array<{
|
|
11
|
+
command: string;
|
|
12
|
+
extensions?: string[];
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
}>;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
};
|
|
17
|
+
workspace?: { roots?: string[] };
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function resolvedPath(value: string, base: string): string {
|
|
22
|
+
return path.resolve(base, value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolvedCommand(value: string, base: string): string {
|
|
26
|
+
return path.isAbsolute(value) ||
|
|
27
|
+
(!value.includes("/") && !value.includes("\\"))
|
|
28
|
+
? value
|
|
29
|
+
: path.resolve(base, value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function normalizeDependencies(
|
|
33
|
+
dependencies: ConfigLayer["dependencies"],
|
|
34
|
+
base: string,
|
|
35
|
+
): ConfigLayer["dependencies"] {
|
|
36
|
+
if (!dependencies) return undefined;
|
|
37
|
+
return {
|
|
38
|
+
ast_bro_binary: dependencies.ast_bro_binary
|
|
39
|
+
? resolvedCommand(dependencies.ast_bro_binary, base)
|
|
40
|
+
: undefined,
|
|
41
|
+
dprint_binary: dependencies.dprint_binary
|
|
42
|
+
? resolvedCommand(dependencies.dprint_binary, base)
|
|
43
|
+
: undefined,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function normalizeFormatting(
|
|
48
|
+
formatting: ConfigLayer["formatting"],
|
|
49
|
+
base: string,
|
|
50
|
+
): ConfigLayer["formatting"] {
|
|
51
|
+
if (!formatting) return undefined;
|
|
52
|
+
return {
|
|
53
|
+
...formatting,
|
|
54
|
+
dprint_config: formatting.dprint_config
|
|
55
|
+
? resolvedPath(formatting.dprint_config, base)
|
|
56
|
+
: undefined,
|
|
57
|
+
formatters: formatting.formatters?.map((formatter) => ({
|
|
58
|
+
...formatter,
|
|
59
|
+
command: resolvedCommand(formatter.command, base),
|
|
60
|
+
extensions: formatter.extensions?.map((extension) =>
|
|
61
|
+
extension.toLowerCase(),
|
|
62
|
+
),
|
|
63
|
+
})),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function normalizeConfigLayer<T extends ConfigLayer>(
|
|
68
|
+
value: T,
|
|
69
|
+
filePath: string,
|
|
70
|
+
): T {
|
|
71
|
+
const base = path.dirname(filePath);
|
|
72
|
+
return {
|
|
73
|
+
...value,
|
|
74
|
+
dependencies: normalizeDependencies(value.dependencies, base),
|
|
75
|
+
formatting: normalizeFormatting(value.formatting, base),
|
|
76
|
+
workspace: value.workspace?.roots
|
|
77
|
+
? { roots: value.workspace.roots.map((item) => resolvedPath(item, base)) }
|
|
78
|
+
: value.workspace,
|
|
79
|
+
} as T;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const pathKeys = new Set([
|
|
83
|
+
"destination",
|
|
84
|
+
"file",
|
|
85
|
+
"filePath",
|
|
86
|
+
"filePaths",
|
|
87
|
+
"path",
|
|
88
|
+
"paths",
|
|
89
|
+
"root",
|
|
90
|
+
]);
|
|
91
|
+
const keyedBatchExclusions = new Set([
|
|
92
|
+
"files",
|
|
93
|
+
"filePaths",
|
|
94
|
+
"path",
|
|
95
|
+
"paths",
|
|
96
|
+
"root",
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
export function collectRequestPaths(value: unknown): string[] {
|
|
100
|
+
const paths: string[] = [];
|
|
101
|
+
visitRequestPaths(value, paths);
|
|
102
|
+
return paths;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function visitArrayPaths(values: unknown[], paths: string[], key?: string) {
|
|
106
|
+
for (const value of values) visitRequestPaths(value, paths, key);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function visitObjectPaths(
|
|
110
|
+
value: Record<string, unknown>,
|
|
111
|
+
paths: string[],
|
|
112
|
+
key?: string,
|
|
113
|
+
) {
|
|
114
|
+
for (const [childKey, child] of Object.entries(value)) {
|
|
115
|
+
if (isKeyedBatchPath(key, childKey, child)) paths.push(childKey);
|
|
116
|
+
visitRequestPaths(child, paths, childKey);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function visitRequestPaths(
|
|
121
|
+
value: unknown,
|
|
122
|
+
paths: string[],
|
|
123
|
+
key?: string,
|
|
124
|
+
): void {
|
|
125
|
+
if (typeof value === "string") {
|
|
126
|
+
if (key && pathKeys.has(key)) paths.push(value);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (Array.isArray(value)) {
|
|
130
|
+
visitArrayPaths(value, paths, key);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (value && typeof value === "object")
|
|
134
|
+
visitObjectPaths(value as Record<string, unknown>, paths, key);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isKeyedBatchPath(
|
|
138
|
+
parentKey: string | undefined,
|
|
139
|
+
childKey: string,
|
|
140
|
+
child: unknown,
|
|
141
|
+
): boolean {
|
|
142
|
+
return (
|
|
143
|
+
parentKey === undefined &&
|
|
144
|
+
Boolean(child) &&
|
|
145
|
+
typeof child === "object" &&
|
|
146
|
+
!Array.isArray(child) &&
|
|
147
|
+
!keyedBatchExclusions.has(childKey)
|
|
148
|
+
);
|
|
149
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export interface HookPolicy {
|
|
2
|
+
allowTools: string[];
|
|
3
|
+
blockTools: string[];
|
|
4
|
+
enabled: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface ToolIdentity {
|
|
8
|
+
name: string;
|
|
9
|
+
normalizedName: string;
|
|
10
|
+
shortName: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function toolIdentity(event: Record<string, unknown>): ToolIdentity {
|
|
14
|
+
const name = String(event.tool_name ?? event.toolName ?? event.name ?? "");
|
|
15
|
+
const normalizedName = name.toLowerCase();
|
|
16
|
+
return {
|
|
17
|
+
name,
|
|
18
|
+
normalizedName,
|
|
19
|
+
shortName: normalizedName.split(".").at(-1) ?? normalizedName,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function policyMatches(
|
|
24
|
+
candidates: string[],
|
|
25
|
+
identity: ToolIdentity,
|
|
26
|
+
): boolean {
|
|
27
|
+
return candidates.some((candidate) => {
|
|
28
|
+
const normalized = candidate.toLowerCase();
|
|
29
|
+
return (
|
|
30
|
+
normalized === identity.normalizedName ||
|
|
31
|
+
normalized === identity.shortName
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function eventCommand(event: Record<string, unknown>): {
|
|
37
|
+
command: string;
|
|
38
|
+
raw: unknown;
|
|
39
|
+
} {
|
|
40
|
+
const raw =
|
|
41
|
+
event.tool_input ??
|
|
42
|
+
event.toolInput ??
|
|
43
|
+
event.toolArgs ??
|
|
44
|
+
event.input ??
|
|
45
|
+
event.args;
|
|
46
|
+
if (typeof raw === "string") return { command: raw, raw };
|
|
47
|
+
const input =
|
|
48
|
+
raw && typeof raw === "object" ? (raw as Record<string, unknown>) : {};
|
|
49
|
+
return {
|
|
50
|
+
command: String(
|
|
51
|
+
input.cmd ??
|
|
52
|
+
input.command ??
|
|
53
|
+
input.script ??
|
|
54
|
+
input.source ??
|
|
55
|
+
input.code ??
|
|
56
|
+
"",
|
|
57
|
+
),
|
|
58
|
+
raw,
|
|
59
|
+
};
|
|
60
|
+
}
|