aegiscode 6.8.3 → 6.8.5
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 +2 -2
- package/bin/aegiscode.js +35 -5
- package/package.json +1 -6
- package/scripts/predist.mjs +99 -0
- package/src/agents.js +1 -1
- package/src/app.js +41 -9
- package/src/caps.js +1 -1
- package/src/chatflow.js +182 -34
- package/src/checkpoint.js +1 -1
- package/src/clipboard.js +1 -1
- package/src/commands.js +158 -21
- package/src/config.js +2 -2
- package/src/deps.js +2 -1
- package/src/devrun.js +1 -1
- package/src/diff.js +1 -1
- package/src/events.js +38 -15
- package/src/export.js +1 -1
- package/src/fuzzy.js +1 -1
- package/src/history.js +3 -3
- package/src/init.js +1 -1
- package/src/input.js +1 -1
- package/src/keys.js +1 -1
- package/src/markdown.js +2 -2
- package/src/mcp.js +683 -0
- package/src/overlays.js +2 -2
- package/src/panels.js +52 -19
- package/src/permissions.js +2 -2
- package/src/render.js +2 -2
- package/src/screen.js +133 -8
- package/src/screens.js +1 -1
- package/src/summarize.js +1 -1
- package/src/system.js +2 -2
- package/src/theme.js +6 -6
- package/src/title.js +217 -0
- package/src/tokens.js +75 -96
- package/src/update.js +75 -1
- package/vendor/desktop/lib/local/agents.js +1 -1
- package/vendor/desktop/lib/local/autonomous.js +1 -1
- package/vendor/desktop/lib/local/engine.js +32 -8
- package/vendor/desktop/lib/local/git-scope.js +1 -1
- package/vendor/desktop/lib/local/prompt.js +3 -3
- package/vendor/desktop/lib/local/queue.js +2 -2
- package/vendor/desktop/lib/local/shell.js +1 -1
- package/vendor/desktop/lib/local/tools.js +2 -2
- package/vendor/desktop/lib/local/turn-guard.js +1 -1
- package/vendor/desktop/lib/local/worktree-lock.js +1 -1
- package/vendor/desktop/lib/settings.js +67 -0
- package/vendor/desktop/renderer/usage.js +377 -46
- package/vendor/hosts/install.js +638 -0
- package/vendor/hosts/openai-shim.js +419 -0
- package/vendor/hosts/spec.js +164 -0
- package/vendor/hosts/targets.js +346 -0
- package/vendor/mcp/server.js +186 -0
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The installer — reads a host's config, merges exactly one entry into it, and
|
|
5
|
+
* writes it back without disturbing anything else in the file.
|
|
6
|
+
*
|
|
7
|
+
* The invariants this file exists to hold:
|
|
8
|
+
*
|
|
9
|
+
* 1. **Never destroy a user's config.** Every write is a merge of one key
|
|
10
|
+
* into a parsed document, preceded by a timestamped backup whenever the
|
|
11
|
+
* file exists and its bytes change. A config the user hand-tuned for four
|
|
12
|
+
* other MCP servers must come out the other side with those four servers
|
|
13
|
+
* intact — `test/editor-hosts.test.mjs` asserts exactly that.
|
|
14
|
+
*
|
|
15
|
+
* 2. **Idempotent.** Running `aegiscode mcp install vscode` twice leaves the
|
|
16
|
+
* file byte-identical the second time (`changed: false`), so the command
|
|
17
|
+
* is safe in a dotfiles script or a provisioning step.
|
|
18
|
+
*
|
|
19
|
+
* 3. **Never guess.** Unparseable JSON is refused, not overwritten. A TOML
|
|
20
|
+
* file whose `[mcp_servers.aegis]` section sits under a construct this
|
|
21
|
+
* minimal writer does not model is refused with the line number. Silent
|
|
22
|
+
* data loss in someone's editor config is not an acceptable failure mode
|
|
23
|
+
* for a convenience command.
|
|
24
|
+
*
|
|
25
|
+
* 4. **Dry-run is the default in the CLI, not here.** `plan()` computes the
|
|
26
|
+
* exact bytes it would write; `apply()` is the only function that touches
|
|
27
|
+
* the disk.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
const fs = require('node:fs');
|
|
31
|
+
const path = require('node:path');
|
|
32
|
+
|
|
33
|
+
const { TARGETS, getTarget, appConfig } = require('./targets.js');
|
|
34
|
+
const { buildSpec, resolveServer } = require('./spec.js');
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Context
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The environment a target resolves paths against. Tests pass an explicit ctx
|
|
42
|
+
* so nothing here ever writes into the developer's real `~/.config`.
|
|
43
|
+
*/
|
|
44
|
+
function hostCtx(overrides = {}) {
|
|
45
|
+
return {
|
|
46
|
+
env: process.env,
|
|
47
|
+
platform: process.platform,
|
|
48
|
+
home: null,
|
|
49
|
+
cwd: process.cwd(),
|
|
50
|
+
...overrides,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// Document I/O — JSON and the one TOML target
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
function readText(file) {
|
|
59
|
+
try {
|
|
60
|
+
return { text: fs.readFileSync(file, 'utf8'), exists: true };
|
|
61
|
+
} catch (err) {
|
|
62
|
+
if (err.code === 'ENOENT') return { text: null, exists: false };
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A path that is a real file — the only thing `node <path>` can execute. */
|
|
68
|
+
function isFile(file) {
|
|
69
|
+
try {
|
|
70
|
+
return fs.statSync(file).isFile();
|
|
71
|
+
} catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function parseJson(text, file) {
|
|
77
|
+
if (text === null || text.trim() === '') return {};
|
|
78
|
+
try {
|
|
79
|
+
return JSON.parse(text);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
throw new Error(`${file} is not valid JSON (${err.message}) — refusing to overwrite it`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Set `a.b.c` without dropping siblings. */
|
|
86
|
+
function setPath(doc, dotted, value) {
|
|
87
|
+
const parts = dotted.split('.');
|
|
88
|
+
let node = doc;
|
|
89
|
+
for (const part of parts.slice(0, -1)) {
|
|
90
|
+
if (node[part] === null || typeof node[part] !== 'object' || Array.isArray(node[part])) {
|
|
91
|
+
node[part] = {};
|
|
92
|
+
}
|
|
93
|
+
node = node[part];
|
|
94
|
+
}
|
|
95
|
+
node[parts[parts.length - 1]] = value;
|
|
96
|
+
return doc;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function getPath(doc, dotted) {
|
|
100
|
+
let node = doc;
|
|
101
|
+
for (const part of dotted.split('.')) {
|
|
102
|
+
if (node === null || typeof node !== 'object') return undefined;
|
|
103
|
+
node = node[part];
|
|
104
|
+
}
|
|
105
|
+
return node;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Delete a leaf from a dotted path.
|
|
110
|
+
*
|
|
111
|
+
* @param {object} doc
|
|
112
|
+
* @param {string} dotted
|
|
113
|
+
* @param {boolean} prune Remove container objects left empty by the delete.
|
|
114
|
+
* This is OFF by default, and the default is the safe one: an empty
|
|
115
|
+
* `"mcpServers": {}` that the user wrote is a key in their file, and a
|
|
116
|
+
* removal that never installed anything must not touch it. Callers pass
|
|
117
|
+
* `true` only when they know the container was holding our own entry.
|
|
118
|
+
*/
|
|
119
|
+
function delPath(doc, dotted, prune = false) {
|
|
120
|
+
const parts = dotted.split('.');
|
|
121
|
+
const parents = [];
|
|
122
|
+
let node = doc;
|
|
123
|
+
for (const part of parts.slice(0, -1)) {
|
|
124
|
+
if (node === null || typeof node !== 'object') return doc;
|
|
125
|
+
parents.push([node, part]);
|
|
126
|
+
node = node[part];
|
|
127
|
+
}
|
|
128
|
+
if (node && typeof node === 'object') delete node[parts[parts.length - 1]];
|
|
129
|
+
if (!prune) return doc;
|
|
130
|
+
// If deleting our key emptied a container, nothing else of the user's was in
|
|
131
|
+
// it, so dropping it is safe. (The one residue: a container the user had
|
|
132
|
+
// written as an empty object is also removed — a no-op in every editor, and
|
|
133
|
+
// the price of not leaving a stray `{}` behind.)
|
|
134
|
+
for (let i = parents.length - 1; i >= 0; i--) {
|
|
135
|
+
const [parent, key] = parents[i];
|
|
136
|
+
const child = parent[key];
|
|
137
|
+
if (child && typeof child === 'object' && Object.keys(child).length === 0) delete parent[key];
|
|
138
|
+
else break;
|
|
139
|
+
}
|
|
140
|
+
return doc;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const TOML_BARE = /^[A-Za-z0-9_-]+$/;
|
|
144
|
+
|
|
145
|
+
function tomlKey(key) {
|
|
146
|
+
return TOML_BARE.test(key) ? key : JSON.stringify(key);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function tomlString(value) {
|
|
150
|
+
return JSON.stringify(String(value));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function tomlArray(values) {
|
|
154
|
+
return `[${values.map(tomlString).join(', ')}]`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Replace (or append) one `[a.b]` section, leaving every other line of the file
|
|
159
|
+
* alone — including comments inside the section being replaced, which is why
|
|
160
|
+
* the block is regenerated from the spec rather than patched key by key. A
|
|
161
|
+
* generated section carries a header comment saying so.
|
|
162
|
+
*/
|
|
163
|
+
/**
|
|
164
|
+
* Is this line a TOML header for `sectionPath`, or for a subtable of it?
|
|
165
|
+
*
|
|
166
|
+
* `renderEntry` emits `[...env]` as a subtable, so "our section" is a SPAN of
|
|
167
|
+
* the file — the header plus any `[section.…]` children — and not just one
|
|
168
|
+
* header line. Comparing bare-line equality (as this used to) both missed the
|
|
169
|
+
* subtable and mistook a foreign section for ours.
|
|
170
|
+
*/
|
|
171
|
+
function tomlHeaderName(line) {
|
|
172
|
+
const t = line.trim();
|
|
173
|
+
if (!/^\[/.test(t)) return null;
|
|
174
|
+
return t.replace(/^\[+\s*/, '').replace(/\s*\]+$/, '');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function ownedTomlHeader(line, sectionPath) {
|
|
178
|
+
const name = tomlHeaderName(line);
|
|
179
|
+
if (name === null) return false;
|
|
180
|
+
return name === sectionPath || name.startsWith(`${sectionPath}.`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Replace (or append) a whole TOML section, header included.
|
|
185
|
+
*
|
|
186
|
+
* Idempotency is the requirement that shapes this: the second run must produce
|
|
187
|
+
* the same bytes as the first, so the replacement has to consume exactly the
|
|
188
|
+
* span the previous run wrote — the header, the body, and any owned subtable —
|
|
189
|
+
* and leave every other byte of someone else's config alone.
|
|
190
|
+
*
|
|
191
|
+
* @param {string} text
|
|
192
|
+
* @param {string} sectionPath e.g. `mcp_servers.aegis` (no brackets)
|
|
193
|
+
* @param {string[]} bodyLines lines AFTER the header; may include own subtables
|
|
194
|
+
*/
|
|
195
|
+
function upsertTomlSection(text, sectionPath, bodyLines) {
|
|
196
|
+
const header = `[${sectionPath}]`;
|
|
197
|
+
const lines = (text || '').split('\n');
|
|
198
|
+
const out = [];
|
|
199
|
+
let replaced = false;
|
|
200
|
+
let i = 0;
|
|
201
|
+
while (i < lines.length) {
|
|
202
|
+
if (lines[i].trim() === header) {
|
|
203
|
+
out.push(header, ...bodyLines, '');
|
|
204
|
+
replaced = true;
|
|
205
|
+
i++;
|
|
206
|
+
// Consume the old body, then each subtable we own (the `.env` block),
|
|
207
|
+
// stopping at the first header that is somebody else's.
|
|
208
|
+
while (i < lines.length && !/^\s*\[/.test(lines[i])) i++;
|
|
209
|
+
while (i < lines.length && ownedTomlHeader(lines[i], sectionPath)) {
|
|
210
|
+
i++;
|
|
211
|
+
while (i < lines.length && !/^\s*\[/.test(lines[i])) i++;
|
|
212
|
+
}
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
out.push(lines[i]);
|
|
216
|
+
i++;
|
|
217
|
+
}
|
|
218
|
+
if (!replaced) {
|
|
219
|
+
while (out.length && out[out.length - 1].trim() === '') out.pop();
|
|
220
|
+
if (out.length) out.push('');
|
|
221
|
+
out.push(header, ...bodyLines, '');
|
|
222
|
+
}
|
|
223
|
+
return out.join('\n');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function removeTomlSection(text, sectionPath) {
|
|
227
|
+
const header = `[${sectionPath}]`;
|
|
228
|
+
const lines = (text || '').split('\n');
|
|
229
|
+
const out = [];
|
|
230
|
+
let i = 0;
|
|
231
|
+
while (i < lines.length) {
|
|
232
|
+
if (lines[i].trim() === header) {
|
|
233
|
+
i++;
|
|
234
|
+
while (i < lines.length && !/^\s*\[/.test(lines[i])) i++;
|
|
235
|
+
// The `.env` subtable is part of our section, not a section of its own:
|
|
236
|
+
// leaving it behind would orphan a block of env keys under no header.
|
|
237
|
+
while (i < lines.length && ownedTomlHeader(lines[i], sectionPath)) {
|
|
238
|
+
i++;
|
|
239
|
+
while (i < lines.length && !/^\s*\[/.test(lines[i])) i++;
|
|
240
|
+
}
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
out.push(lines[i]);
|
|
244
|
+
i++;
|
|
245
|
+
}
|
|
246
|
+
return out.join('\n');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
// Rendering one target
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
|
|
253
|
+
function renderEntry(target, spec) {
|
|
254
|
+
if (target.kind === 'mcp-toml') {
|
|
255
|
+
const lines = [
|
|
256
|
+
`# Added by \`aegiscode mcp install ${target.id}\`. Key resolution lives in`,
|
|
257
|
+
'# ~/.aegiscode/.env and the 0600 store `aegiscode login` writes.',
|
|
258
|
+
'command = ' + tomlString(spec.command),
|
|
259
|
+
'args = ' + tomlArray(spec.args),
|
|
260
|
+
];
|
|
261
|
+
if (spec.env && Object.keys(spec.env).length) {
|
|
262
|
+
lines.push('', `[${target.container}.${spec.name}.env]`);
|
|
263
|
+
for (const [k, v] of Object.entries(spec.env)) lines.push(`${tomlKey(k)} = ${tomlString(v)}`);
|
|
264
|
+
}
|
|
265
|
+
return lines;
|
|
266
|
+
}
|
|
267
|
+
return target.entry(spec);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* @returns {{target: object, scope: string, file: string, exists: boolean,
|
|
272
|
+
* changed: boolean, before: string, after: string, error: Error|null,
|
|
273
|
+
* backupOf: string|null}}
|
|
274
|
+
*/
|
|
275
|
+
function planTarget(target, scope, opts = {}) {
|
|
276
|
+
const ctx = opts.ctx || hostCtx();
|
|
277
|
+
const spec = opts.spec || buildSpec(opts.specOpts || {});
|
|
278
|
+
const file = target.path(scope, ctx);
|
|
279
|
+
const result = {
|
|
280
|
+
target, scope, file, exists: false, changed: false,
|
|
281
|
+
before: null, after: null, error: null, backupOf: null,
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
let text = null;
|
|
285
|
+
try {
|
|
286
|
+
const read = readText(file);
|
|
287
|
+
text = read.text;
|
|
288
|
+
result.exists = read.exists;
|
|
289
|
+
} catch (err) {
|
|
290
|
+
result.error = err;
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
293
|
+
if (result.exists) {
|
|
294
|
+
try { result.mode = fs.statSync(file).mode & 0o777; } catch { /* keep default */ }
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (target.kind === 'mcp-toml') {
|
|
298
|
+
const section = `${target.container}.${spec.name}`;
|
|
299
|
+
const body = renderEntry(target, spec);
|
|
300
|
+
result.before = text;
|
|
301
|
+
result.after = upsertTomlSection(text, section, body);
|
|
302
|
+
result.changed = result.after !== (text || '');
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
let doc;
|
|
307
|
+
try {
|
|
308
|
+
doc = parseJson(text, file);
|
|
309
|
+
} catch (err) {
|
|
310
|
+
result.error = err;
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
if (doc === null || typeof doc !== 'object' || Array.isArray(doc)) {
|
|
314
|
+
result.error = new Error(`${file} does not contain a JSON object — refusing to overwrite it`);
|
|
315
|
+
return result;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
setPath(doc, `${target.container}.${spec.name}`, renderEntry(target, spec));
|
|
319
|
+
if (typeof target.extras === 'function') {
|
|
320
|
+
const extra = target.extras(spec);
|
|
321
|
+
if (extra && extra.inputs) {
|
|
322
|
+
const existing = Array.isArray(doc.inputs) ? doc.inputs : [];
|
|
323
|
+
for (const input of extra.inputs) {
|
|
324
|
+
const at = existing.findIndex((e) => e && e.id === input.id);
|
|
325
|
+
if (at === -1) existing.push(input);
|
|
326
|
+
else existing[at] = input;
|
|
327
|
+
}
|
|
328
|
+
doc.inputs = existing;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
result.before = text;
|
|
333
|
+
result.after = JSON.stringify(doc, null, 2) + '\n';
|
|
334
|
+
result.changed = result.after !== (text || '');
|
|
335
|
+
return result;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function planRemove(target, scope, opts = {}) {
|
|
339
|
+
const ctx = opts.ctx || hostCtx();
|
|
340
|
+
const spec = opts.spec || buildSpec(opts.specOpts || {});
|
|
341
|
+
const file = target.path(scope, ctx);
|
|
342
|
+
const result = {
|
|
343
|
+
target, scope, file, exists: false, changed: false,
|
|
344
|
+
before: null, after: null, error: null, backupOf: null,
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
let text = null;
|
|
348
|
+
try {
|
|
349
|
+
const read = readText(file);
|
|
350
|
+
text = read.text;
|
|
351
|
+
result.exists = read.exists;
|
|
352
|
+
} catch (err) {
|
|
353
|
+
result.error = err;
|
|
354
|
+
return result;
|
|
355
|
+
}
|
|
356
|
+
if (!result.exists) return result;
|
|
357
|
+
try { result.mode = fs.statSync(file).mode & 0o777; } catch { /* keep default */ }
|
|
358
|
+
|
|
359
|
+
if (target.kind === 'mcp-toml') {
|
|
360
|
+
result.before = text;
|
|
361
|
+
result.after = removeTomlSection(text, `${target.container}.${spec.name}`);
|
|
362
|
+
// The env subtable header is removed by the same sweep (it is a section
|
|
363
|
+
// header directly following ours).
|
|
364
|
+
result.changed = result.after !== text;
|
|
365
|
+
return result;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
let doc;
|
|
369
|
+
try {
|
|
370
|
+
doc = parseJson(text, file);
|
|
371
|
+
} catch (err) {
|
|
372
|
+
result.error = err;
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
// Is there anything of ours to remove? Asking first is what makes removal
|
|
376
|
+
// idempotent AND non-destructive: re-serialising a document we changed
|
|
377
|
+
// nothing in would rewrite the user's whole file — reformatted, key order
|
|
378
|
+
// possibly altered — and report it as a change.
|
|
379
|
+
const entryPath = `${target.container}.${spec.name}`;
|
|
380
|
+
const hadEntry = getPath(doc, entryPath) !== undefined;
|
|
381
|
+
const hadInput = Array.isArray(doc.inputs) && doc.inputs.some((e) => e && e.id === 'aegis-api-key');
|
|
382
|
+
if (!hadEntry && !hadInput) {
|
|
383
|
+
result.before = text;
|
|
384
|
+
result.after = text;
|
|
385
|
+
result.changed = false;
|
|
386
|
+
return result;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
delPath(doc, entryPath, true);
|
|
390
|
+
const inputs = Array.isArray(doc.inputs) ? doc.inputs.filter((e) => !(e && e.id === 'aegis-api-key')) : null;
|
|
391
|
+
if (inputs) {
|
|
392
|
+
if (inputs.length) doc.inputs = inputs;
|
|
393
|
+
else delete doc.inputs;
|
|
394
|
+
}
|
|
395
|
+
result.before = text;
|
|
396
|
+
result.after = JSON.stringify(doc, null, 2) + '\n';
|
|
397
|
+
result.changed = result.after !== text;
|
|
398
|
+
return result;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// ---------------------------------------------------------------------------
|
|
402
|
+
// Writing
|
|
403
|
+
// ---------------------------------------------------------------------------
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* The mode the written file gets.
|
|
407
|
+
*
|
|
408
|
+
* A file that may hold an API key (`--with-key`) is 0600 and nothing else. A
|
|
409
|
+
* file that provably does not keeps whatever mode it already had — so
|
|
410
|
+
* installing into an existing 0644 config leaves it readable by the user's own
|
|
411
|
+
* editor, which is what it was. A newly created config gets 0644, the ordinary
|
|
412
|
+
* case for a file that carries no secret by construction.
|
|
413
|
+
*/
|
|
414
|
+
function modeFor(plan) {
|
|
415
|
+
if (plan.withKey) return 0o600;
|
|
416
|
+
return plan.mode || 0o644;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function apply(plan, opts = {}) {
|
|
420
|
+
if (plan.error) return { ...plan, applied: false };
|
|
421
|
+
if (!plan.changed) return { ...plan, applied: false };
|
|
422
|
+
if (opts.dryRun) return { ...plan, applied: false, dryRun: true };
|
|
423
|
+
|
|
424
|
+
plan.withKey = Boolean(opts.withKey);
|
|
425
|
+
fs.mkdirSync(path.dirname(plan.file), { recursive: true });
|
|
426
|
+
if (plan.exists && plan.before !== null) {
|
|
427
|
+
const backup = `${plan.file}.aegis-backup-${Date.now()}`;
|
|
428
|
+
fs.writeFileSync(backup, plan.before, { mode: 0o600 });
|
|
429
|
+
plan.backupOf = backup;
|
|
430
|
+
}
|
|
431
|
+
const tmp = `${plan.file}.aegis-tmp-${process.pid}`;
|
|
432
|
+
try {
|
|
433
|
+
fs.writeFileSync(tmp, plan.after, { mode: 0o600 });
|
|
434
|
+
fs.renameSync(tmp, plan.file);
|
|
435
|
+
fs.chmodSync(plan.file, modeFor(plan));
|
|
436
|
+
} finally {
|
|
437
|
+
try { fs.unlinkSync(tmp); } catch { /* already renamed */ }
|
|
438
|
+
}
|
|
439
|
+
return { ...plan, applied: true };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// ---------------------------------------------------------------------------
|
|
443
|
+
// Status
|
|
444
|
+
// ---------------------------------------------------------------------------
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Read back the entry this module would have written, from a TOML section.
|
|
448
|
+
*
|
|
449
|
+
* Only the keys `renderEntry` emits are modelled, and a section that is absent
|
|
450
|
+
* returns null. This is deliberately not a TOML parser: the file belongs to
|
|
451
|
+
* someone else's editor and the only question asked of it is "is our entry
|
|
452
|
+
* here, and what does it point at".
|
|
453
|
+
*/
|
|
454
|
+
function parseTomlEntry(text, section) {
|
|
455
|
+
if (!text) return null;
|
|
456
|
+
const escaped = section.replace(/[.[\]*+?^$(){}|]/g, '\\$&');
|
|
457
|
+
const at = new RegExp('^\\s*\\[' + escaped + '\\]\\s*$', 'm').exec(text);
|
|
458
|
+
if (!at) return null;
|
|
459
|
+
// The section is a SPAN — the header, its body, and its own subtables — so
|
|
460
|
+
// the slice has to run to the first header that is NOT ours. Stopping at the
|
|
461
|
+
// next header of any kind (as this used to) truncated the body before
|
|
462
|
+
// `[<section>.env]`, which is where `renderEntry` puts every env var: the
|
|
463
|
+
// parse succeeded and reported an empty env, which is worse than failing.
|
|
464
|
+
const rest = text.slice(at.index + at[0].length);
|
|
465
|
+
const lines = rest.split('\n');
|
|
466
|
+
const body = [];
|
|
467
|
+
let i = 0;
|
|
468
|
+
while (i < lines.length) {
|
|
469
|
+
if (/^\s*\[/.test(lines[i]) && !ownedTomlHeader(lines[i], section)) break;
|
|
470
|
+
body.push(lines[i]);
|
|
471
|
+
i++;
|
|
472
|
+
}
|
|
473
|
+
const entry = { command: null, args: [], env: {} };
|
|
474
|
+
const cmd = /^\s*command\s*=\s*"((?:[^"\\]|\\.)*)"/m.exec(body.join('\n'));
|
|
475
|
+
if (cmd) entry.command = JSON.parse('"' + cmd[1] + '"');
|
|
476
|
+
const args = /^\s*args\s*=\s*\[([\s\S]*?)\]/m.exec(body.join('\n'));
|
|
477
|
+
if (args) {
|
|
478
|
+
entry.args = [...args[1].matchAll(/"((?:[^"\\]|\\.)*)"/g)].map((m) => JSON.parse('"' + m[1] + '"'));
|
|
479
|
+
}
|
|
480
|
+
for (const m of body.join('\n').matchAll(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*"((?:[^"\\]|\\.)*)"\s*$/gm)) {
|
|
481
|
+
if (m[1] !== 'command' && m[1] !== 'args') entry.env[m[1]] = JSON.parse('"' + m[2] + '"');
|
|
482
|
+
}
|
|
483
|
+
return entry;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* The script an entry names, if any.
|
|
488
|
+
*
|
|
489
|
+
* `spec.js` always writes `command: node` plus an absolute path to
|
|
490
|
+
* `mcp/server.js`, so the script is the first argument that looks like one. A
|
|
491
|
+
* user who hand-edited their config to `npx aegiscode-mcp` has no such
|
|
492
|
+
* argument and gets `null` — "cannot tell", which is honest, not "broken".
|
|
493
|
+
*/
|
|
494
|
+
function entryServerPath(entry) {
|
|
495
|
+
if (!entry) return null;
|
|
496
|
+
const args = Array.isArray(entry.args) ? entry.args : [];
|
|
497
|
+
return args.find((a) => typeof a === 'string' && /\.(js|mjs|cjs)$/.test(a)) || null;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Host-expanded placeholders — `${CLAUDE_PLUGIN_ROOT}`, `$HOME`,
|
|
502
|
+
* `%APPDATA%`.
|
|
503
|
+
*
|
|
504
|
+
* The EDITOR substitutes these at load time, not us. A plugin `.mcp.json`
|
|
505
|
+
* ships `${CLAUDE_PLUGIN_ROOT}/mcp/server.js` precisely so one file works on
|
|
506
|
+
* every machine. Existence is therefore not knowable from here, and crying
|
|
507
|
+
* "the server is gone" would push a user to overwrite a deliberately portable
|
|
508
|
+
* config with a machine-absolute path — the exact portability the placeholder
|
|
509
|
+
* exists to preserve.
|
|
510
|
+
*/
|
|
511
|
+
const HOST_VAR_RE = /\$\{?[A-Za-z_][A-Za-z0-9_]*\}?|%[A-Za-z_][A-Za-z0-9_]*%/;
|
|
512
|
+
|
|
513
|
+
function hasHostVar(value) {
|
|
514
|
+
return typeof value === 'string' && HOST_VAR_RE.test(value);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Per target+scope: does the config exist, is our entry in it, and does the
|
|
519
|
+
* command it names still exist on disk? The last question is the one that
|
|
520
|
+
* catches a moved checkout — the silent failure this whole module is written
|
|
521
|
+
* against.
|
|
522
|
+
*/
|
|
523
|
+
function status(ids, opts = {}) {
|
|
524
|
+
const ctx = opts.ctx || hostCtx();
|
|
525
|
+
const spec = opts.spec || buildSpec(opts.specOpts || {});
|
|
526
|
+
const targets = ids && ids.length ? ids.map((id) => getTarget(id)).filter(Boolean) : TARGETS;
|
|
527
|
+
const rows = [];
|
|
528
|
+
|
|
529
|
+
for (const target of targets) {
|
|
530
|
+
for (const scope of target.scopes || ['user']) {
|
|
531
|
+
const file = target.path(scope, ctx);
|
|
532
|
+
const row = { id: target.id, label: target.label, scope, file, exists: false, installed: false, entry: null, error: null, docs: target.docs, verified: target.verified };
|
|
533
|
+
let read;
|
|
534
|
+
try {
|
|
535
|
+
read = readText(file);
|
|
536
|
+
} catch (err) {
|
|
537
|
+
row.error = err.message;
|
|
538
|
+
rows.push(row);
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
row.exists = read.exists;
|
|
542
|
+
if (read.exists && read.text !== null) {
|
|
543
|
+
if (target.kind === 'mcp-toml') {
|
|
544
|
+
row.entry = parseTomlEntry(read.text, `${target.container}.${spec.name}`);
|
|
545
|
+
row.installed = Boolean(row.entry);
|
|
546
|
+
} else {
|
|
547
|
+
try {
|
|
548
|
+
const doc = parseJson(read.text, file);
|
|
549
|
+
row.entry = getPath(doc, `${target.container}.${spec.name}`) || null;
|
|
550
|
+
row.installed = Boolean(row.entry);
|
|
551
|
+
} catch (err) {
|
|
552
|
+
row.error = err.message;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
// The question the editor itself never asks: the config still names a
|
|
557
|
+
// file, but does that file still exist? A checkout that moved, an npm
|
|
558
|
+
// prefix that changed, or a `git clean` all leave a config that loads
|
|
559
|
+
// fine and fails silently at tool-call time. Surfaced here so `status`
|
|
560
|
+
// can say "installed, but pointing at nothing".
|
|
561
|
+
if (row.installed) {
|
|
562
|
+
row.serverPath = entryServerPath(row.entry);
|
|
563
|
+
row.serverUnverifiable = hasHostVar(row.serverPath);
|
|
564
|
+
row.serverExists =
|
|
565
|
+
row.serverPath && !row.serverUnverifiable ? isFile(row.serverPath) : null;
|
|
566
|
+
}
|
|
567
|
+
rows.push(row);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return rows;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Which hosts are actually present on this machine — the input to "offer
|
|
575
|
+
* something sensible" so a user is not shown thirteen hosts they do not have.
|
|
576
|
+
*
|
|
577
|
+
* Two signals count as present: a config file we already know how to write, or
|
|
578
|
+
* the editor's own config DIRECTORY existing. The second matters most — the
|
|
579
|
+
* whole point of this command is offering AEGIS to an editor that has never
|
|
580
|
+
* heard of MCP, and that editor has no `mcp.json` yet.
|
|
581
|
+
*/
|
|
582
|
+
function detected(opts = {}) {
|
|
583
|
+
const ctx = opts.ctx || hostCtx();
|
|
584
|
+
const found = new Set();
|
|
585
|
+
for (const target of TARGETS) {
|
|
586
|
+
for (const scope of target.scopes || ['user']) {
|
|
587
|
+
try {
|
|
588
|
+
fs.accessSync(target.path(scope, ctx));
|
|
589
|
+
found.add(target.id);
|
|
590
|
+
} catch { /* not installed */ }
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
for (const [variant, id] of [
|
|
594
|
+
['Code', 'vscode'],
|
|
595
|
+
['Code - Insiders', 'vscode-insiders'],
|
|
596
|
+
['VSCodium', 'vscodium'],
|
|
597
|
+
['Cursor', 'cursor'],
|
|
598
|
+
]) {
|
|
599
|
+
try {
|
|
600
|
+
fs.accessSync(path.join(appConfig(ctx), variant));
|
|
601
|
+
found.add(id);
|
|
602
|
+
} catch { /* not installed */ }
|
|
603
|
+
}
|
|
604
|
+
for (const [rel, id] of [
|
|
605
|
+
[['.codeium'], 'windsurf'],
|
|
606
|
+
[['.gemini'], 'gemini'],
|
|
607
|
+
[['.codex'], 'codex'],
|
|
608
|
+
[['.continue'], 'continue'],
|
|
609
|
+
[['.junie'], 'junie'],
|
|
610
|
+
[['.cursor'], 'cursor'],
|
|
611
|
+
]) {
|
|
612
|
+
try {
|
|
613
|
+
fs.accessSync(path.join(ctx.home || ctx.env.HOME || ctx.env.USERPROFILE, ...rel));
|
|
614
|
+
found.add(id);
|
|
615
|
+
} catch { /* not installed */ }
|
|
616
|
+
}
|
|
617
|
+
return found;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
module.exports = {
|
|
621
|
+
hostCtx,
|
|
622
|
+
planTarget,
|
|
623
|
+
planRemove,
|
|
624
|
+
apply,
|
|
625
|
+
status,
|
|
626
|
+
detected,
|
|
627
|
+
renderEntry,
|
|
628
|
+
entryServerPath,
|
|
629
|
+
hasHostVar,
|
|
630
|
+
parseTomlEntry,
|
|
631
|
+
upsertTomlSection,
|
|
632
|
+
removeTomlSection,
|
|
633
|
+
setPath,
|
|
634
|
+
getPath,
|
|
635
|
+
delPath,
|
|
636
|
+
readText,
|
|
637
|
+
resolveServer,
|
|
638
|
+
};
|