@mwillbanks/ast-mcp 0.1.0
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/CONTRIBUTING.md +60 -0
- package/LICENSE +21 -0
- package/README.md +109 -0
- package/dist/ast-mcp-hook.js +7 -0
- package/dist/ast-mcp-hook.js.map +11 -0
- package/dist/ast-mcp-install.js +22 -0
- package/dist/ast-mcp-install.js.map +12 -0
- package/dist/ast-mcp.js +115 -0
- package/dist/ast-mcp.js.map +89 -0
- package/dprint.json +37 -0
- package/logo.svg +1 -0
- package/package.json +89 -0
- package/src/ast-bro/capability.ts +21 -0
- package/src/ast-bro/client.ts +47 -0
- package/src/ast-bro/result.ts +42 -0
- package/src/ast-bro/rewrite-capability.ts +9 -0
- package/src/ast-bro/tools.json +603 -0
- package/src/hook.ts +79 -0
- package/src/http-entry.ts +3 -0
- package/src/http.ts +138 -0
- package/src/index.ts +8 -0
- package/src/installer.ts +501 -0
- package/src/lifecycle.ts +10 -0
- package/src/managed-hook.ts +4 -0
- package/src/patch/aider.ts +224 -0
- package/src/patch/engine.ts +446 -0
- package/src/patch/languages.ts +36 -0
- package/src/runtime/atomic.ts +26 -0
- package/src/runtime/attributes.ts +70 -0
- package/src/runtime/file-delete.ts +96 -0
- package/src/runtime/file-read.ts +178 -0
- package/src/runtime/format.ts +56 -0
- package/src/runtime/hash.ts +11 -0
- package/src/runtime/locks.ts +87 -0
- package/src/runtime/paths.ts +73 -0
- package/src/runtime/process-input.ts +25 -0
- package/src/runtime/signals.ts +47 -0
- package/src/server.ts +13 -0
- package/src/stdio.ts +154 -0
- package/src/tools/ast-bro.ts +172 -0
- package/src/tools/files.ts +173 -0
- package/src/tools/index.ts +11 -0
- package/src/tools/lifecycle.ts +124 -0
- package/templates/AGENTS.md +56 -0
- package/templates/skills/ast-mcp/SKILL.md +96 -0
- package/templates/skills/ast-mcp/agents/openai.yaml +4 -0
- package/templates/skills/ast-mcp/evals/batch.evals.json +274 -0
- package/templates/skills/ast-mcp/evals/evals.json +1209 -0
- package/templates/skills/ast-mcp/evals/file-hash.evals.json +50 -0
- package/templates/skills/ast-mcp/evals/measure.ts +18 -0
- package/templates/skills/ast-mcp/evals/results.md +17 -0
- package/templates/skills/ast-mcp/evals/score.ts +417 -0
- package/templates/skills/ast-mcp/evals/semantic.ts +332 -0
- package/templates/skills/ast-mcp/evals/trigger-queries.json +22 -0
- package/templates/skills/ast-mcp/evals/validate.ts +99 -0
- package/templates/skills/ast-mcp/references/installation.md +40 -0
- package/templates/skills/ast-mcp/references/managed-hook.ts +3 -0
- package/templates/skills/ast-mcp/references/patch-state-machine.md +39 -0
- package/templates/skills/ast-mcp/references/tool-catalog.md +46 -0
- package/templates/skills/ast-mcp/scripts/check-install.ts +269 -0
- package/templates/skills/ast-mcp/scripts/managed-hook.ts +22 -0
package/src/http.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
|
+
|
|
3
|
+
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/server";
|
|
4
|
+
import { serve } from "bun";
|
|
5
|
+
|
|
6
|
+
import { createServer, installProcessSignalHandlers } from "./lifecycle";
|
|
7
|
+
|
|
8
|
+
const { PORT = "3000" } = process.env;
|
|
9
|
+
|
|
10
|
+
function httpBinding() {
|
|
11
|
+
return { hostname: process.env.AST_MCP_HTTP_HOST ?? "127.0.0.1" };
|
|
12
|
+
}
|
|
13
|
+
function durationFromEnv(name: string, fallback: number) {
|
|
14
|
+
const value = Number(process.env[name]);
|
|
15
|
+
return Number.isSafeInteger(value) && value > 0 ? value : fallback;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const SESSION_TIMEOUT_MS = durationFromEnv(
|
|
19
|
+
"AST_MCP_SESSION_TIMEOUT_MS",
|
|
20
|
+
30 * 60 * 1000,
|
|
21
|
+
);
|
|
22
|
+
const SESSION_SWEEP_INTERVAL_MS = durationFromEnv(
|
|
23
|
+
"AST_MCP_SESSION_SWEEP_INTERVAL_MS",
|
|
24
|
+
60 * 1000,
|
|
25
|
+
); // 30 minutes
|
|
26
|
+
|
|
27
|
+
const sessions = new Map<
|
|
28
|
+
string,
|
|
29
|
+
{
|
|
30
|
+
transport: WebStandardStreamableHTTPServerTransport;
|
|
31
|
+
server: McpServer;
|
|
32
|
+
lastActivity: number;
|
|
33
|
+
}
|
|
34
|
+
>();
|
|
35
|
+
|
|
36
|
+
// Clean up idle sessions periodically
|
|
37
|
+
const sessionSweep = setInterval(() => {
|
|
38
|
+
const now = Date.now();
|
|
39
|
+
for (const [id, session] of sessions) {
|
|
40
|
+
if (now - session.lastActivity > SESSION_TIMEOUT_MS) {
|
|
41
|
+
void session.server.close();
|
|
42
|
+
sessions.delete(id);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}, SESSION_SWEEP_INTERVAL_MS);
|
|
46
|
+
|
|
47
|
+
export function startHttpServer() {
|
|
48
|
+
const httpServer = serve({
|
|
49
|
+
async fetch(request) {
|
|
50
|
+
const url = new URL(request.url);
|
|
51
|
+
|
|
52
|
+
if (url.pathname !== "/mcp") {
|
|
53
|
+
return new Response("Not found", { status: 404 });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const sessionId = request.headers.get("mcp-session-id");
|
|
57
|
+
|
|
58
|
+
if (request.method === "GET" || request.method === "DELETE") {
|
|
59
|
+
if (!sessionId) {
|
|
60
|
+
return new Response("Missing session ID", { status: 400 });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const session = sessions.get(sessionId);
|
|
64
|
+
if (!session) {
|
|
65
|
+
return new Response("Session not found", { status: 404 });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
session.lastActivity = Date.now();
|
|
69
|
+
const response = await session.transport.handleRequest(request);
|
|
70
|
+
|
|
71
|
+
if (request.method === "DELETE") {
|
|
72
|
+
await session.server.close();
|
|
73
|
+
sessions.delete(sessionId);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return response;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (request.method === "POST") {
|
|
80
|
+
if (sessionId) {
|
|
81
|
+
const session = sessions.get(sessionId);
|
|
82
|
+
if (!session) {
|
|
83
|
+
return new Response("Session not found", { status: 404 });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
session.lastActivity = Date.now();
|
|
87
|
+
return session.transport.handleRequest(request);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const server = createServer();
|
|
91
|
+
|
|
92
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
93
|
+
onsessionclosed: (closedSessionId) => {
|
|
94
|
+
const session = sessions.get(closedSessionId);
|
|
95
|
+
if (session) {
|
|
96
|
+
void session.server.close();
|
|
97
|
+
sessions.delete(closedSessionId);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
onsessioninitialized: (newSessionId) => {
|
|
101
|
+
sessions.set(newSessionId, {
|
|
102
|
+
lastActivity: Date.now(),
|
|
103
|
+
server,
|
|
104
|
+
transport,
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
sessionIdGenerator: () => crypto.randomUUID(),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
await server.connect(transport);
|
|
111
|
+
|
|
112
|
+
return transport.handleRequest(request);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return new Response("Method not allowed", { status: 405 });
|
|
116
|
+
},
|
|
117
|
+
port: Number(PORT),
|
|
118
|
+
...httpBinding(),
|
|
119
|
+
});
|
|
120
|
+
let shutdown: Promise<void> | undefined;
|
|
121
|
+
installProcessSignalHandlers(() => {
|
|
122
|
+
shutdown ??= (async () => {
|
|
123
|
+
httpServer.stop(false);
|
|
124
|
+
clearInterval(sessionSweep);
|
|
125
|
+
const results = await Promise.allSettled(
|
|
126
|
+
[...sessions.values()].map((session) => session.server.close()),
|
|
127
|
+
);
|
|
128
|
+
sessions.clear();
|
|
129
|
+
httpServer.stop(true);
|
|
130
|
+
const failed = results.find((result) => result.status === "rejected");
|
|
131
|
+
if (failed?.status === "rejected") throw failed.reason;
|
|
132
|
+
})();
|
|
133
|
+
return shutdown;
|
|
134
|
+
});
|
|
135
|
+
return httpServer;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (import.meta.main) startHttpServer();
|
package/src/index.ts
ADDED
package/src/installer.ts
ADDED
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cp,
|
|
3
|
+
lstat,
|
|
4
|
+
mkdir,
|
|
5
|
+
readdir,
|
|
6
|
+
readFile,
|
|
7
|
+
rm,
|
|
8
|
+
writeFile,
|
|
9
|
+
} from "node:fs/promises";
|
|
10
|
+
import os from "node:os";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { isManagedHook } from "./managed-hook";
|
|
13
|
+
|
|
14
|
+
const packageRoot = path.resolve(import.meta.dir, "..");
|
|
15
|
+
const serverEntry = path.join(packageRoot, "src/index.ts");
|
|
16
|
+
const hookEntry = path.join(packageRoot, "src/hook.ts");
|
|
17
|
+
const targets = ["codex", "claude", "copilot"] as const;
|
|
18
|
+
type Target = (typeof targets)[number];
|
|
19
|
+
|
|
20
|
+
// biome-ignore lint/suspicious/noExplicitAny: Host configuration JSON is intentionally dynamic.
|
|
21
|
+
async function json(file: string): Promise<Record<string, any>> {
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(await readFile(file, "utf8"));
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function save(file: string, value: unknown) {
|
|
30
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
31
|
+
await writeFile(file, `${JSON.stringify(value, null, 2)}\n`);
|
|
32
|
+
}
|
|
33
|
+
function definition(root?: string) {
|
|
34
|
+
return {
|
|
35
|
+
args: ["--hot", serverEntry],
|
|
36
|
+
command: "bun",
|
|
37
|
+
env: root
|
|
38
|
+
? {
|
|
39
|
+
AST_MCP_ALLOW_EXTERNAL_ROOTS: "1",
|
|
40
|
+
AST_MCP_ROOTS: root,
|
|
41
|
+
}
|
|
42
|
+
: {},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async function codexMcp(file: string, root?: string) {
|
|
46
|
+
const old = await readFile(file, "utf8").catch(() => "");
|
|
47
|
+
const clean = old
|
|
48
|
+
.replace(/# ast-mcp:begin[\s\S]*?# ast-mcp:end\n?/g, "")
|
|
49
|
+
.trimEnd();
|
|
50
|
+
const environment = root
|
|
51
|
+
? `env = { AST_MCP_ALLOW_EXTERNAL_ROOTS = "1", AST_MCP_ROOTS = ${JSON.stringify(root)} }\n`
|
|
52
|
+
: "";
|
|
53
|
+
const block = `# ast-mcp:begin\n[mcp_servers.ast-mcp]\ncommand = "bun"\nargs = ["--hot", ${JSON.stringify(serverEntry)}]\n${environment}# ast-mcp:end`;
|
|
54
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
55
|
+
await writeFile(file, `${clean ? `${clean}\n\n` : ""}${block}\n`);
|
|
56
|
+
}
|
|
57
|
+
async function jsonMcp(file: string, root?: string, copilot = false) {
|
|
58
|
+
const value = await json(file);
|
|
59
|
+
value.mcpServers = {
|
|
60
|
+
...(value.mcpServers ?? {}),
|
|
61
|
+
"ast-mcp": copilot
|
|
62
|
+
? { type: "local", ...definition(root), tools: ["*"] }
|
|
63
|
+
: definition(root),
|
|
64
|
+
};
|
|
65
|
+
await save(file, value);
|
|
66
|
+
}
|
|
67
|
+
async function vscodeMcp(file: string, root: string) {
|
|
68
|
+
const value = await json(file);
|
|
69
|
+
value.servers = {
|
|
70
|
+
...(value.servers ?? {}),
|
|
71
|
+
"ast-mcp": { type: "stdio", ...definition(root) },
|
|
72
|
+
};
|
|
73
|
+
await save(file, value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function hook(
|
|
77
|
+
file: string,
|
|
78
|
+
event: "PreToolUse" | "preToolUse",
|
|
79
|
+
scriptFile: string,
|
|
80
|
+
commandPath = scriptFile,
|
|
81
|
+
) {
|
|
82
|
+
await mkdir(path.dirname(scriptFile), { recursive: true });
|
|
83
|
+
await cp(hookEntry, scriptFile, { force: true });
|
|
84
|
+
const value = await json(file);
|
|
85
|
+
if (event === "preToolUse") value.version ??= 1;
|
|
86
|
+
value.hooks ??= {};
|
|
87
|
+
const prior = Array.isArray(value.hooks[event]) ? value.hooks[event] : [];
|
|
88
|
+
const command = `bun ${JSON.stringify(commandPath)}`;
|
|
89
|
+
const kept = prior.filter(
|
|
90
|
+
(item: unknown) => !isManagedHook(item, event, command),
|
|
91
|
+
);
|
|
92
|
+
const item =
|
|
93
|
+
event === "preToolUse"
|
|
94
|
+
? {
|
|
95
|
+
command,
|
|
96
|
+
matcher: "bash|powershell|edit|create",
|
|
97
|
+
timeoutSec: 10,
|
|
98
|
+
type: "command",
|
|
99
|
+
}
|
|
100
|
+
: {
|
|
101
|
+
hooks: [
|
|
102
|
+
{
|
|
103
|
+
command,
|
|
104
|
+
statusMessage: "Enforcing ast-mcp writes",
|
|
105
|
+
timeout: 10,
|
|
106
|
+
type: "command",
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
matcher: ".*",
|
|
110
|
+
};
|
|
111
|
+
value.hooks[event] = [...kept, item];
|
|
112
|
+
await save(file, value);
|
|
113
|
+
}
|
|
114
|
+
async function skills(folder: string) {
|
|
115
|
+
const destination = path.join(folder, "ast-mcp");
|
|
116
|
+
await rm(destination, { force: true, recursive: true });
|
|
117
|
+
await mkdir(folder, { recursive: true });
|
|
118
|
+
await cp(path.join(packageRoot, "templates/skills", "ast-mcp"), destination, {
|
|
119
|
+
filter: (source) => path.basename(source) !== ".ast-bro",
|
|
120
|
+
force: true,
|
|
121
|
+
recursive: true,
|
|
122
|
+
});
|
|
123
|
+
await cp(
|
|
124
|
+
path.join(packageRoot, "templates/AGENTS.md"),
|
|
125
|
+
path.join(destination, "references/agents-guidance.md"),
|
|
126
|
+
{ force: true },
|
|
127
|
+
);
|
|
128
|
+
await cp(hookEntry, path.join(destination, "references/hook.ts"), {
|
|
129
|
+
force: true,
|
|
130
|
+
});
|
|
131
|
+
await cp(
|
|
132
|
+
path.join(destination, "SKILL.md"),
|
|
133
|
+
path.join(destination, "references/skill-template.md"),
|
|
134
|
+
{ force: true },
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
const instructionsBegin = "<!-- ast-mcp:begin -->";
|
|
138
|
+
const instructionsEnd = "<!-- ast-mcp:end -->";
|
|
139
|
+
const instructionsPattern =
|
|
140
|
+
/<!-- ast-mcp:begin -->[\s\S]*?<!-- ast-mcp:end -->\n?/g;
|
|
141
|
+
|
|
142
|
+
async function writeTextOrRemove(file: string, content: string) {
|
|
143
|
+
const normalized = content.trimEnd();
|
|
144
|
+
if (!normalized) {
|
|
145
|
+
await rm(file, { force: true });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
149
|
+
await writeFile(file, `${normalized}\n`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function instructions(file: string) {
|
|
153
|
+
const block = (
|
|
154
|
+
await readFile(path.join(packageRoot, "templates/AGENTS.md"), "utf8")
|
|
155
|
+
).trim();
|
|
156
|
+
const old = await readFile(file, "utf8").catch(() => "");
|
|
157
|
+
const clean = old.replace(instructionsPattern, "").trimEnd();
|
|
158
|
+
await writeTextOrRemove(
|
|
159
|
+
file,
|
|
160
|
+
`${clean ? `${clean}\n\n` : ""}${instructionsBegin}\n\n${block}\n\n${instructionsEnd}`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function removeInstructions(file: string) {
|
|
165
|
+
const old = await readFile(file, "utf8").catch(() => "");
|
|
166
|
+
await writeTextOrRemove(file, old.replace(instructionsPattern, ""));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// biome-ignore lint/suspicious/noExplicitAny: Host configuration JSON is intentionally dynamic.
|
|
170
|
+
async function saveJsonOrRemove(file: string, value: Record<string, any>) {
|
|
171
|
+
if (Object.keys(value).length === 0) {
|
|
172
|
+
await rm(file, { force: true });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
await save(file, value);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function removeJsonMcp(file: string, section = "mcpServers") {
|
|
179
|
+
const value = await json(file);
|
|
180
|
+
const entries = value[section];
|
|
181
|
+
if (!entries || typeof entries !== "object" || !("ast-mcp" in entries))
|
|
182
|
+
return;
|
|
183
|
+
delete entries["ast-mcp"];
|
|
184
|
+
if (Object.keys(entries).length === 0) delete value[section];
|
|
185
|
+
await saveJsonOrRemove(file, value);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function removeCodexMcp(file: string) {
|
|
189
|
+
const old = await readFile(file, "utf8").catch(() => "");
|
|
190
|
+
await writeTextOrRemove(
|
|
191
|
+
file,
|
|
192
|
+
old.replace(/# ast-mcp:begin[\s\S]*?# ast-mcp:end\n?/g, ""),
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function removeHook(
|
|
197
|
+
file: string,
|
|
198
|
+
event: "PreToolUse" | "preToolUse",
|
|
199
|
+
commandPath: string,
|
|
200
|
+
) {
|
|
201
|
+
const value = await json(file);
|
|
202
|
+
const hooks = value.hooks;
|
|
203
|
+
if (!hooks || !Array.isArray(hooks[event])) return;
|
|
204
|
+
const command = `bun ${JSON.stringify(commandPath)}`;
|
|
205
|
+
hooks[event] = hooks[event].filter(
|
|
206
|
+
(item: unknown) => !isManagedHook(item, event, command),
|
|
207
|
+
);
|
|
208
|
+
if (hooks[event].length === 0) delete hooks[event];
|
|
209
|
+
if (Object.keys(hooks).length === 0) delete value.hooks;
|
|
210
|
+
await saveJsonOrRemove(file, value);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async function hasLocalInstallation(root: string) {
|
|
214
|
+
const codex = await readFile(
|
|
215
|
+
path.join(root, ".codex/config.toml"),
|
|
216
|
+
"utf8",
|
|
217
|
+
).catch(() => "");
|
|
218
|
+
const claude = await json(path.join(root, ".mcp.json"));
|
|
219
|
+
const copilot = await json(path.join(root, ".github/mcp.json"));
|
|
220
|
+
return Boolean(
|
|
221
|
+
codex.includes("# ast-mcp:begin") ||
|
|
222
|
+
claude.mcpServers?.["ast-mcp"] ||
|
|
223
|
+
copilot.mcpServers?.["ast-mcp"],
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export interface InstallOptions {
|
|
228
|
+
home?: string;
|
|
229
|
+
root: string;
|
|
230
|
+
scope: "local" | "global";
|
|
231
|
+
targets: Target[];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function targetPaths(
|
|
235
|
+
target: Target,
|
|
236
|
+
global: boolean,
|
|
237
|
+
root: string,
|
|
238
|
+
home: string,
|
|
239
|
+
) {
|
|
240
|
+
if (target === "codex") {
|
|
241
|
+
const base = global ? path.join(home, ".codex") : path.join(root, ".codex");
|
|
242
|
+
return [
|
|
243
|
+
path.join(base, "config.toml"),
|
|
244
|
+
path.join(base, "hooks.json"),
|
|
245
|
+
path.join(base, "hooks/ast-mcp.ts"),
|
|
246
|
+
path.join(base, "skills/ast-mcp"),
|
|
247
|
+
global ? path.join(base, "AGENTS.md") : path.join(root, "AGENTS.md"),
|
|
248
|
+
];
|
|
249
|
+
}
|
|
250
|
+
if (target === "claude") {
|
|
251
|
+
const base = global
|
|
252
|
+
? path.join(home, ".claude")
|
|
253
|
+
: path.join(root, ".claude");
|
|
254
|
+
return [
|
|
255
|
+
global ? path.join(home, ".claude.json") : path.join(root, ".mcp.json"),
|
|
256
|
+
path.join(base, "settings.json"),
|
|
257
|
+
path.join(base, "hooks/ast-mcp.ts"),
|
|
258
|
+
path.join(base, "skills/ast-mcp"),
|
|
259
|
+
global ? path.join(base, "CLAUDE.md") : path.join(root, "AGENTS.md"),
|
|
260
|
+
];
|
|
261
|
+
}
|
|
262
|
+
const base = global
|
|
263
|
+
? path.join(home, ".copilot")
|
|
264
|
+
: path.join(root, ".github");
|
|
265
|
+
return [
|
|
266
|
+
global
|
|
267
|
+
? path.join(base, "mcp-config.json")
|
|
268
|
+
: path.join(root, ".github/mcp.json"),
|
|
269
|
+
...(global ? [] : [path.join(root, ".vscode/mcp.json")]),
|
|
270
|
+
path.join(base, "hooks/ast-mcp.json"),
|
|
271
|
+
path.join(base, "hooks/ast-mcp.ts"),
|
|
272
|
+
path.join(base, "skills/ast-mcp"),
|
|
273
|
+
global
|
|
274
|
+
? path.join(base, "copilot-instructions.md")
|
|
275
|
+
: path.join(root, "AGENTS.md"),
|
|
276
|
+
];
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async function snapshot(paths: string[]) {
|
|
280
|
+
const files = new Map<string, string>();
|
|
281
|
+
const visit = async (file: string): Promise<void> => {
|
|
282
|
+
if (path.basename(file) === ".ast-bro") return;
|
|
283
|
+
const metadata = await lstat(file).catch(() => undefined);
|
|
284
|
+
if (!metadata) return;
|
|
285
|
+
if (metadata.isDirectory()) {
|
|
286
|
+
for (const entry of await readdir(file))
|
|
287
|
+
await visit(path.join(file, entry));
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (metadata.isFile())
|
|
291
|
+
files.set(file, (await readFile(file)).toString("base64"));
|
|
292
|
+
};
|
|
293
|
+
await Promise.all(paths.map(visit));
|
|
294
|
+
return files;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function changedFiles(before: Map<string, string>, after: Map<string, string>) {
|
|
298
|
+
return [...new Set([...before.keys(), ...after.keys()])]
|
|
299
|
+
.filter((file) => before.get(file) !== after.get(file))
|
|
300
|
+
.sort();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async function reconcile(options: InstallOptions) {
|
|
304
|
+
const root = path.resolve(options.root);
|
|
305
|
+
const home = options.home ?? os.homedir();
|
|
306
|
+
const global = options.scope === "global";
|
|
307
|
+
const paths = options.targets.flatMap((target) =>
|
|
308
|
+
targetPaths(target, global, root, home),
|
|
309
|
+
);
|
|
310
|
+
const before = await snapshot(paths);
|
|
311
|
+
for (const target of options.targets) {
|
|
312
|
+
if (target === "codex") {
|
|
313
|
+
const base = global
|
|
314
|
+
? path.join(home, ".codex")
|
|
315
|
+
: path.join(root, ".codex");
|
|
316
|
+
await codexMcp(path.join(base, "config.toml"), global ? undefined : root);
|
|
317
|
+
await hook(
|
|
318
|
+
path.join(base, "hooks.json"),
|
|
319
|
+
"PreToolUse",
|
|
320
|
+
path.join(base, "hooks/ast-mcp.ts"),
|
|
321
|
+
global
|
|
322
|
+
? path.join(base, "hooks/ast-mcp.ts")
|
|
323
|
+
: ".codex/hooks/ast-mcp.ts",
|
|
324
|
+
);
|
|
325
|
+
await skills(path.join(base, "skills"));
|
|
326
|
+
await instructions(
|
|
327
|
+
global ? path.join(base, "AGENTS.md") : path.join(root, "AGENTS.md"),
|
|
328
|
+
);
|
|
329
|
+
} else if (target === "claude") {
|
|
330
|
+
const base = global
|
|
331
|
+
? path.join(home, ".claude")
|
|
332
|
+
: path.join(root, ".claude");
|
|
333
|
+
await jsonMcp(
|
|
334
|
+
global ? path.join(home, ".claude.json") : path.join(root, ".mcp.json"),
|
|
335
|
+
global ? undefined : root,
|
|
336
|
+
);
|
|
337
|
+
await hook(
|
|
338
|
+
path.join(base, "settings.json"),
|
|
339
|
+
"PreToolUse",
|
|
340
|
+
path.join(base, "hooks/ast-mcp.ts"),
|
|
341
|
+
global
|
|
342
|
+
? path.join(base, "hooks/ast-mcp.ts")
|
|
343
|
+
: `\${CLAUDE_PROJECT_DIR}/.claude/hooks/ast-mcp.ts`,
|
|
344
|
+
);
|
|
345
|
+
await skills(path.join(base, "skills"));
|
|
346
|
+
await instructions(
|
|
347
|
+
global ? path.join(base, "CLAUDE.md") : path.join(root, "AGENTS.md"),
|
|
348
|
+
);
|
|
349
|
+
} else {
|
|
350
|
+
const base = global
|
|
351
|
+
? path.join(home, ".copilot")
|
|
352
|
+
: path.join(root, ".github");
|
|
353
|
+
if (global)
|
|
354
|
+
await jsonMcp(path.join(base, "mcp-config.json"), undefined, true);
|
|
355
|
+
else {
|
|
356
|
+
await jsonMcp(path.join(root, ".github/mcp.json"), root, true);
|
|
357
|
+
await vscodeMcp(path.join(root, ".vscode/mcp.json"), root);
|
|
358
|
+
}
|
|
359
|
+
await hook(
|
|
360
|
+
path.join(base, "hooks/ast-mcp.json"),
|
|
361
|
+
"preToolUse",
|
|
362
|
+
path.join(base, "hooks/ast-mcp.ts"),
|
|
363
|
+
global
|
|
364
|
+
? path.join(base, "hooks/ast-mcp.ts")
|
|
365
|
+
: ".github/hooks/ast-mcp.ts",
|
|
366
|
+
);
|
|
367
|
+
await skills(path.join(base, "skills"));
|
|
368
|
+
await instructions(
|
|
369
|
+
global
|
|
370
|
+
? path.join(base, "copilot-instructions.md")
|
|
371
|
+
: path.join(root, "AGENTS.md"),
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return changedFiles(before, await snapshot(paths));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export async function install(options: InstallOptions) {
|
|
379
|
+
return reconcile(options);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export async function update(options: InstallOptions) {
|
|
383
|
+
return reconcile(options);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export async function uninstall(options: InstallOptions) {
|
|
387
|
+
const root = path.resolve(options.root);
|
|
388
|
+
const home = options.home ?? os.homedir();
|
|
389
|
+
const global = options.scope === "global";
|
|
390
|
+
const paths = options.targets.flatMap((target) =>
|
|
391
|
+
targetPaths(target, global, root, home),
|
|
392
|
+
);
|
|
393
|
+
const before = await snapshot(paths);
|
|
394
|
+
for (const target of options.targets) {
|
|
395
|
+
if (target === "codex") {
|
|
396
|
+
const base = global
|
|
397
|
+
? path.join(home, ".codex")
|
|
398
|
+
: path.join(root, ".codex");
|
|
399
|
+
await removeCodexMcp(path.join(base, "config.toml"));
|
|
400
|
+
await removeHook(
|
|
401
|
+
path.join(base, "hooks.json"),
|
|
402
|
+
"PreToolUse",
|
|
403
|
+
global
|
|
404
|
+
? path.join(base, "hooks/ast-mcp.ts")
|
|
405
|
+
: ".codex/hooks/ast-mcp.ts",
|
|
406
|
+
);
|
|
407
|
+
await rm(path.join(base, "hooks/ast-mcp.ts"), { force: true });
|
|
408
|
+
await rm(path.join(base, "skills/ast-mcp"), {
|
|
409
|
+
force: true,
|
|
410
|
+
recursive: true,
|
|
411
|
+
});
|
|
412
|
+
if (global) await removeInstructions(path.join(base, "AGENTS.md"));
|
|
413
|
+
} else if (target === "claude") {
|
|
414
|
+
const base = global
|
|
415
|
+
? path.join(home, ".claude")
|
|
416
|
+
: path.join(root, ".claude");
|
|
417
|
+
await removeJsonMcp(
|
|
418
|
+
global ? path.join(home, ".claude.json") : path.join(root, ".mcp.json"),
|
|
419
|
+
);
|
|
420
|
+
await removeHook(
|
|
421
|
+
path.join(base, "settings.json"),
|
|
422
|
+
"PreToolUse",
|
|
423
|
+
global
|
|
424
|
+
? path.join(base, "hooks/ast-mcp.ts")
|
|
425
|
+
: `\${CLAUDE_PROJECT_DIR}/.claude/hooks/ast-mcp.ts`,
|
|
426
|
+
);
|
|
427
|
+
await rm(path.join(base, "hooks/ast-mcp.ts"), { force: true });
|
|
428
|
+
await rm(path.join(base, "skills/ast-mcp"), {
|
|
429
|
+
force: true,
|
|
430
|
+
recursive: true,
|
|
431
|
+
});
|
|
432
|
+
if (global) await removeInstructions(path.join(base, "CLAUDE.md"));
|
|
433
|
+
} else {
|
|
434
|
+
const base = global
|
|
435
|
+
? path.join(home, ".copilot")
|
|
436
|
+
: path.join(root, ".github");
|
|
437
|
+
if (global) await removeJsonMcp(path.join(base, "mcp-config.json"));
|
|
438
|
+
else {
|
|
439
|
+
await removeJsonMcp(path.join(root, ".github/mcp.json"));
|
|
440
|
+
await removeJsonMcp(path.join(root, ".vscode/mcp.json"), "servers");
|
|
441
|
+
}
|
|
442
|
+
await removeHook(
|
|
443
|
+
path.join(base, "hooks/ast-mcp.json"),
|
|
444
|
+
"preToolUse",
|
|
445
|
+
global
|
|
446
|
+
? path.join(base, "hooks/ast-mcp.ts")
|
|
447
|
+
: ".github/hooks/ast-mcp.ts",
|
|
448
|
+
);
|
|
449
|
+
await rm(path.join(base, "hooks/ast-mcp.ts"), { force: true });
|
|
450
|
+
await rm(path.join(base, "skills/ast-mcp"), {
|
|
451
|
+
force: true,
|
|
452
|
+
recursive: true,
|
|
453
|
+
});
|
|
454
|
+
if (global)
|
|
455
|
+
await removeInstructions(path.join(base, "copilot-instructions.md"));
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (options.scope === "local" && !(await hasLocalInstallation(root)))
|
|
459
|
+
await removeInstructions(path.join(root, "AGENTS.md"));
|
|
460
|
+
return changedFiles(before, await snapshot(paths));
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export async function runInstallerCli(
|
|
464
|
+
args = process.argv.slice(2),
|
|
465
|
+
): Promise<void> {
|
|
466
|
+
const tokens = [...args];
|
|
467
|
+
let operation: "install" | "update" | "uninstall" = "install";
|
|
468
|
+
if (["install", "update", "uninstall"].includes(tokens[0] ?? ""))
|
|
469
|
+
operation = tokens.shift() as typeof operation;
|
|
470
|
+
let scope: "local" | "global" = "local";
|
|
471
|
+
let root = process.cwd();
|
|
472
|
+
let selected: Target[] = [...targets];
|
|
473
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
474
|
+
if (tokens[index] === "--scope") scope = tokens[++index] as typeof scope;
|
|
475
|
+
else if (tokens[index] === "--root") root = tokens[++index];
|
|
476
|
+
else if (tokens[index] === "--target") {
|
|
477
|
+
const value = tokens[++index];
|
|
478
|
+
selected = value === "all" ? [...targets] : [value as Target];
|
|
479
|
+
} else throw new Error(`Unknown argument: ${tokens[index]}`);
|
|
480
|
+
}
|
|
481
|
+
if (
|
|
482
|
+
!["local", "global"].includes(scope) ||
|
|
483
|
+
selected.some((item) => !targets.includes(item))
|
|
484
|
+
)
|
|
485
|
+
throw new Error("Invalid install scope or target");
|
|
486
|
+
const options = { root, scope, targets: selected };
|
|
487
|
+
const changed =
|
|
488
|
+
operation === "install"
|
|
489
|
+
? await install(options)
|
|
490
|
+
: operation === "update"
|
|
491
|
+
? await update(options)
|
|
492
|
+
: await uninstall(options);
|
|
493
|
+
const _label =
|
|
494
|
+
operation === "install"
|
|
495
|
+
? "Installed"
|
|
496
|
+
: operation === "update"
|
|
497
|
+
? "Updated"
|
|
498
|
+
: "Uninstalled";
|
|
499
|
+
process.stdout.write(`${JSON.stringify({ changed, operation })}\n`);
|
|
500
|
+
}
|
|
501
|
+
if (import.meta.main) await runInstallerCli();
|
package/src/lifecycle.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { installProcessSignalHandlers } from "./runtime/signals";
|
|
2
|
+
import { createServer } from "./server";
|
|
3
|
+
|
|
4
|
+
export { createServer, installProcessSignalHandlers };
|
|
5
|
+
|
|
6
|
+
export function createProcessServer() {
|
|
7
|
+
const server = createServer();
|
|
8
|
+
installProcessSignalHandlers(server.close.bind(server));
|
|
9
|
+
return server;
|
|
10
|
+
}
|