aegisone 1.0.22 → 1.0.23

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.
Files changed (226) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +1 -1
  3. package/SOURCE-OFFER.txt +9 -0
  4. package/bin/aegis.js +1 -322
  5. package/package.json +4 -2
  6. package/scripts/bump.mjs +1 -211
  7. package/scripts/bundle.js +1 -129
  8. package/scripts/predist.mjs +1 -939
  9. package/src/child.js +1 -285
  10. package/src/dispatch.js +1 -269
  11. package/src/doctor.js +1 -639
  12. package/src/host.js +1 -111
  13. package/src/permissions.js +1 -369
  14. package/src/probes.js +1 -159
  15. package/src/release.js +1 -409
  16. package/vendor/cli/bin/aegiscode.js +2 -914
  17. package/vendor/cli/package.json +3 -2
  18. package/vendor/cli/scripts/check.mjs +1 -52
  19. package/vendor/cli/scripts/demo.mjs +1 -94
  20. package/vendor/cli/scripts/predist.mjs +1 -75
  21. package/vendor/cli/scripts/runtests.mjs +1 -56
  22. package/vendor/cli/scripts/seal-gate.mjs +1 -74
  23. package/vendor/cli/src/agents.js +1 -139
  24. package/vendor/cli/src/app.js +1 -2932
  25. package/vendor/cli/src/art.js +1 -289
  26. package/vendor/cli/src/autonomous.js +1 -515
  27. package/vendor/cli/src/caps.js +1 -879
  28. package/vendor/cli/src/chatflow.js +1 -3020
  29. package/vendor/cli/src/checkpoint.js +1 -90
  30. package/vendor/cli/src/clipboard.js +1 -424
  31. package/vendor/cli/src/cloudsync.js +1 -528
  32. package/vendor/cli/src/commands.js +1 -4003
  33. package/vendor/cli/src/config.js +1 -581
  34. package/vendor/cli/src/connect.js +1 -349
  35. package/vendor/cli/src/credentials.js +1 -33
  36. package/vendor/cli/src/deps.js +1 -232
  37. package/vendor/cli/src/devrun.js +1 -187
  38. package/vendor/cli/src/diff.js +1 -333
  39. package/vendor/cli/src/diffview.js +1 -296
  40. package/vendor/cli/src/engine.js +1 -297
  41. package/vendor/cli/src/entitlement.js +1 -355
  42. package/vendor/cli/src/events.js +1 -707
  43. package/vendor/cli/src/export.js +1 -67
  44. package/vendor/cli/src/fingerprint.js +1 -592
  45. package/vendor/cli/src/format.js +1 -259
  46. package/vendor/cli/src/fuzzy.js +1 -119
  47. package/vendor/cli/src/gate.js +1 -55
  48. package/vendor/cli/src/history.js +1 -401
  49. package/vendor/cli/src/init.js +1 -175
  50. package/vendor/cli/src/input.js +1 -139
  51. package/vendor/cli/src/keys.js +1 -183
  52. package/vendor/cli/src/limits.js +1 -190
  53. package/vendor/cli/src/markdown.js +1 -306
  54. package/vendor/cli/src/mcp.js +1 -694
  55. package/vendor/cli/src/models.js +1 -487
  56. package/vendor/cli/src/naming.js +1 -229
  57. package/vendor/cli/src/overlays.js +1 -461
  58. package/vendor/cli/src/panels.js +1 -1418
  59. package/vendor/cli/src/permissions.js +1 -290
  60. package/vendor/cli/src/ping.js +1 -226
  61. package/vendor/cli/src/presence.js +1 -238
  62. package/vendor/cli/src/provenance.js +2 -182
  63. package/vendor/cli/src/render.js +1 -515
  64. package/vendor/cli/src/screen.js +1 -518
  65. package/vendor/cli/src/screens.js +1 -896
  66. package/vendor/cli/src/secret.js +1 -59
  67. package/vendor/cli/src/securefs.js +1 -359
  68. package/vendor/cli/src/settings.js +1 -155
  69. package/vendor/cli/src/shared.js +1 -52
  70. package/vendor/cli/src/sharedpaths.js +1 -53
  71. package/vendor/cli/src/signup.js +1 -156
  72. package/vendor/cli/src/summarize.js +1 -95
  73. package/vendor/cli/src/sysdiag.js +1 -585
  74. package/vendor/cli/src/system.js +1 -40
  75. package/vendor/cli/src/theme.js +1 -570
  76. package/vendor/cli/src/title.js +1 -220
  77. package/vendor/cli/src/todos.js +1 -115
  78. package/vendor/cli/src/tokens.js +1 -263
  79. package/vendor/cli/src/topup.js +1 -210
  80. package/vendor/cli/src/update.js +1 -181
  81. package/vendor/cli/vendor/client/aegis.js +1 -2179
  82. package/vendor/cli/vendor/client/attest.js +1 -442
  83. package/vendor/cli/vendor/client/authorship-key.js +1 -410
  84. package/vendor/cli/vendor/client/auto-provision.js +1 -134
  85. package/vendor/cli/vendor/client/brain-catalog.js +1 -268
  86. package/vendor/cli/vendor/client/byok-catalog.js +1 -230
  87. package/vendor/cli/vendor/client/credentials.js +1 -494
  88. package/vendor/cli/vendor/client/device-fingerprint.js +1 -141
  89. package/vendor/cli/vendor/client/drive-connect.js +1 -110
  90. package/vendor/cli/vendor/client/drive-upload.js +1 -174
  91. package/vendor/cli/vendor/client/env-file.js +1 -1273
  92. package/vendor/cli/vendor/client/fingerprint.js +1 -1064
  93. package/vendor/cli/vendor/client/foreign-memory.js +1 -669
  94. package/vendor/cli/vendor/client/google-signin.js +1 -133
  95. package/vendor/cli/vendor/client/hooks.js +1 -430
  96. package/vendor/cli/vendor/client/model-discovery.js +1 -373
  97. package/vendor/cli/vendor/client/platform.js +1 -937
  98. package/vendor/cli/vendor/client/prefs.js +1 -351
  99. package/vendor/cli/vendor/client/presence-constants.js +1 -107
  100. package/vendor/cli/vendor/client/presence.js +1 -438
  101. package/vendor/cli/vendor/client/record-vault.js +3 -1055
  102. package/vendor/cli/vendor/client/session-store.js +1 -548
  103. package/vendor/cli/vendor/client/topup.js +1 -297
  104. package/vendor/cli/vendor/client/touch.js +1 -458
  105. package/vendor/cli/vendor/client/update.js +1 -704
  106. package/vendor/cli/vendor/client/user-models.js +1 -381
  107. package/vendor/cli/vendor/desktop/lib/local/agents.js +1 -105
  108. package/vendor/cli/vendor/desktop/lib/local/autonomous.js +1 -1045
  109. package/vendor/cli/vendor/desktop/lib/local/claude-pro.js +1 -993
  110. package/vendor/cli/vendor/desktop/lib/local/context.js +1 -95
  111. package/vendor/cli/vendor/desktop/lib/local/engine.js +1 -3301
  112. package/vendor/cli/vendor/desktop/lib/local/entitlement.js +1 -660
  113. package/vendor/cli/vendor/desktop/lib/local/git-scope.js +1 -228
  114. package/vendor/cli/vendor/desktop/lib/local/local.js +1 -2135
  115. package/vendor/cli/vendor/desktop/lib/local/prompt.js +1 -275
  116. package/vendor/cli/vendor/desktop/lib/local/queue.js +1 -560
  117. package/vendor/cli/vendor/desktop/lib/local/session-rounds.js +1 -208
  118. package/vendor/cli/vendor/desktop/lib/local/shell.js +1 -680
  119. package/vendor/cli/vendor/desktop/lib/local/tools.js +1 -1238
  120. package/vendor/cli/vendor/desktop/lib/local/turn-guard.js +1 -300
  121. package/vendor/cli/vendor/desktop/lib/local/worktree-lock.js +1 -235
  122. package/vendor/cli/vendor/desktop/lib/settings.js +1 -890
  123. package/vendor/cli/vendor/desktop/renderer/usage.js +1 -1239
  124. package/vendor/cli/vendor/hosts/install.js +1 -638
  125. package/vendor/cli/vendor/hosts/openai-shim.js +1 -515
  126. package/vendor/cli/vendor/hosts/spec.js +1 -164
  127. package/vendor/cli/vendor/hosts/targets.js +1 -346
  128. package/vendor/cli/vendor/mcp/server.js +1 -221
  129. package/vendor/cli/vendor/mcp/tools.js +1 -408
  130. package/vendor/cli/vendor/one/src/child.js +1 -285
  131. package/vendor/cli/vendor/one/src/permissions.js +1 -369
  132. package/vendor/desktop/bin/aegis.js +1 -32
  133. package/vendor/desktop/lib/account-key.js +1 -56
  134. package/vendor/desktop/lib/auto.js +1 -430
  135. package/vendor/desktop/lib/avatar/companion.js +1 -698
  136. package/vendor/desktop/lib/avatar/cosmetics.js +1 -532
  137. package/vendor/desktop/lib/avatar/events.js +1 -344
  138. package/vendor/desktop/lib/avatar/holder-turn.js +1 -350
  139. package/vendor/desktop/lib/avatar/holders.js +1 -612
  140. package/vendor/desktop/lib/avatar/identity.js +1 -641
  141. package/vendor/desktop/lib/avatar/level.js +1 -368
  142. package/vendor/desktop/lib/avatar/mirror.js +1 -524
  143. package/vendor/desktop/lib/avatar/persona.js +1 -388
  144. package/vendor/desktop/lib/avatar/profile.js +1 -977
  145. package/vendor/desktop/lib/avatar/register.js +1 -307
  146. package/vendor/desktop/lib/avatar/store.js +1 -472
  147. package/vendor/desktop/lib/avatar/tiers.js +1 -38
  148. package/vendor/desktop/lib/avatar/transfer.js +1 -415
  149. package/vendor/desktop/lib/avatar/turn.js +1 -216
  150. package/vendor/desktop/lib/avatar/voice.js +1 -328
  151. package/vendor/desktop/lib/avatar/xp.js +1 -404
  152. package/vendor/desktop/lib/deep-link.js +1 -120
  153. package/vendor/desktop/lib/fingerprint.js +2 -347
  154. package/vendor/desktop/lib/google-connect.js +1 -227
  155. package/vendor/desktop/lib/local/agents.js +1 -105
  156. package/vendor/desktop/lib/local/autonomous.js +1 -1045
  157. package/vendor/desktop/lib/local/claude-pro.js +1 -993
  158. package/vendor/desktop/lib/local/context.js +1 -95
  159. package/vendor/desktop/lib/local/engine.js +1 -3301
  160. package/vendor/desktop/lib/local/entitlement.js +1 -660
  161. package/vendor/desktop/lib/local/git-scope.js +1 -228
  162. package/vendor/desktop/lib/local/local.js +1 -2135
  163. package/vendor/desktop/lib/local/prompt.js +1 -275
  164. package/vendor/desktop/lib/local/queue.js +1 -560
  165. package/vendor/desktop/lib/local/session-rounds.js +1 -208
  166. package/vendor/desktop/lib/local/shell.js +1 -680
  167. package/vendor/desktop/lib/local/telemetry.js +1 -470
  168. package/vendor/desktop/lib/local/tools.js +1 -1238
  169. package/vendor/desktop/lib/local/turn-guard.js +1 -300
  170. package/vendor/desktop/lib/local/worktree-lock.js +1 -235
  171. package/vendor/desktop/lib/provider-key-mirror.js +1 -252
  172. package/vendor/desktop/lib/quick-launcher.js +1 -84
  173. package/vendor/desktop/lib/settings.js +1 -890
  174. package/vendor/desktop/lib/sync/memory-queue.js +1 -60
  175. package/vendor/desktop/lib/sync/persist-gate.js +1 -148
  176. package/vendor/desktop/lib/sync/sessions.js +1 -106
  177. package/vendor/desktop/lib/window-state.js +1 -66
  178. package/vendor/desktop/main.js +1 -4529
  179. package/vendor/desktop/package.json +3 -2
  180. package/vendor/desktop/preload.js +1 -636
  181. package/vendor/desktop/renderer/app.js +1 -6660
  182. package/vendor/desktop/renderer/avatar/assemble.js +1 -335
  183. package/vendor/desktop/renderer/avatar/avatar.js +1 -380
  184. package/vendor/desktop/renderer/avatar/cards.js +1 -141
  185. package/vendor/desktop/renderer/avatar/hud.js +1 -206
  186. package/vendor/desktop/renderer/avatar/machine.js +1 -213
  187. package/vendor/desktop/renderer/avatar/pane.js +1 -439
  188. package/vendor/desktop/renderer/avatar/parts.js +1 -437
  189. package/vendor/desktop/renderer/budget.js +1 -168
  190. package/vendor/desktop/renderer/companion.js +1 -223
  191. package/vendor/desktop/renderer/diff.js +1 -359
  192. package/vendor/desktop/renderer/diffview.js +1 -336
  193. package/vendor/desktop/renderer/fingerprint.js +3 -736
  194. package/vendor/desktop/renderer/markdown.js +1 -168
  195. package/vendor/desktop/renderer/provider-toolbar.js +1 -134
  196. package/vendor/desktop/renderer/providers.js +1 -259
  197. package/vendor/desktop/renderer/quick.js +1 -191
  198. package/vendor/desktop/renderer/stream-policy.js +1 -306
  199. package/vendor/desktop/renderer/transcript-view.js +1 -323
  200. package/vendor/desktop/renderer/usage.js +1 -1239
  201. package/vendor/desktop/vendor/aegis.js +1 -2179
  202. package/vendor/desktop/vendor/attest.js +1 -442
  203. package/vendor/desktop/vendor/authorship-key.js +1 -410
  204. package/vendor/desktop/vendor/auto-provision.js +1 -134
  205. package/vendor/desktop/vendor/brain-catalog.js +1 -268
  206. package/vendor/desktop/vendor/byok-catalog.js +1 -230
  207. package/vendor/desktop/vendor/credentials.js +1 -494
  208. package/vendor/desktop/vendor/device-fingerprint.js +1 -141
  209. package/vendor/desktop/vendor/drive-connect.js +1 -110
  210. package/vendor/desktop/vendor/drive-upload.js +1 -174
  211. package/vendor/desktop/vendor/env-file.js +1 -1273
  212. package/vendor/desktop/vendor/fingerprint.js +1 -1064
  213. package/vendor/desktop/vendor/foreign-memory.js +1 -669
  214. package/vendor/desktop/vendor/google-signin.js +1 -133
  215. package/vendor/desktop/vendor/hooks.js +1 -430
  216. package/vendor/desktop/vendor/model-discovery.js +1 -373
  217. package/vendor/desktop/vendor/platform.js +1 -937
  218. package/vendor/desktop/vendor/prefs.js +1 -351
  219. package/vendor/desktop/vendor/presence-constants.js +1 -107
  220. package/vendor/desktop/vendor/presence.js +1 -438
  221. package/vendor/desktop/vendor/record-vault.js +3 -1055
  222. package/vendor/desktop/vendor/session-store.js +1 -548
  223. package/vendor/desktop/vendor/topup.js +1 -297
  224. package/vendor/desktop/vendor/touch.js +1 -458
  225. package/vendor/desktop/vendor/update.js +1 -704
  226. package/vendor/desktop/vendor/user-models.js +1 -381
@@ -1,638 +1 @@
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
- };
1
+ "use strict";const fs=require("node:fs"),path=require("node:path"),{TARGETS:TARGETS,getTarget:getTarget,appConfig:appConfig}=require("./targets.js"),{buildSpec:buildSpec,resolveServer:resolveServer}=require("./spec.js");function hostCtx(e={}){return{env:process.env,platform:process.platform,home:null,cwd:process.cwd(),...e}}function readText(e){try{return{text:fs.readFileSync(e,"utf8"),exists:!0}}catch(e){if("ENOENT"===e.code)return{text:null,exists:!1};throw e}}function isFile(e){try{return fs.statSync(e).isFile()}catch{return!1}}function parseJson(e,t){if(null===e||""===e.trim())return{};try{return JSON.parse(e)}catch(e){throw new Error(`${t} is not valid JSON (${e.message}) — refusing to overwrite it`)}}function setPath(e,t,r){const n=t.split(".");let s=e;for(const e of n.slice(0,-1))(null===s[e]||"object"!=typeof s[e]||Array.isArray(s[e]))&&(s[e]={}),s=s[e];return s[n[n.length-1]]=r,e}function getPath(e,t){let r=e;for(const e of t.split(".")){if(null===r||"object"!=typeof r)return;r=r[e]}return r}function delPath(e,t,r=!1){const n=t.split("."),s=[];let o=e;for(const t of n.slice(0,-1)){if(null===o||"object"!=typeof o)return e;s.push([o,t]),o=o[t]}if(o&&"object"==typeof o&&delete o[n[n.length-1]],!r)return e;for(let e=s.length-1;e>=0;e--){const[t,r]=s[e],n=t[r];if(!n||"object"!=typeof n||0!==Object.keys(n).length)break;delete t[r]}return e}const TOML_BARE=/^[A-Za-z0-9_-]+$/;function tomlKey(e){return TOML_BARE.test(e)?e:JSON.stringify(e)}function tomlString(e){return JSON.stringify(String(e))}function tomlArray(e){return`[${e.map(tomlString).join(", ")}]`}function tomlHeaderName(e){const t=e.trim();return/^\[/.test(t)?t.replace(/^\[+\s*/,"").replace(/\s*\]+$/,""):null}function ownedTomlHeader(e,t){const r=tomlHeaderName(e);return null!==r&&(r===t||r.startsWith(`${t}.`))}function upsertTomlSection(e,t,r){const n=`[${t}]`,s=(e||"").split("\n"),o=[];let i=!1,a=0;for(;a<s.length;)if(s[a].trim()!==n)o.push(s[a]),a++;else{for(o.push(n,...r,""),i=!0,a++;a<s.length&&!/^\s*\[/.test(s[a]);)a++;for(;a<s.length&&ownedTomlHeader(s[a],t);)for(a++;a<s.length&&!/^\s*\[/.test(s[a]);)a++}if(!i){for(;o.length&&""===o[o.length-1].trim();)o.pop();o.length&&o.push(""),o.push(n,...r,"")}return o.join("\n")}function removeTomlSection(e,t){const r=`[${t}]`,n=(e||"").split("\n"),s=[];let o=0;for(;o<n.length;)if(n[o].trim()!==r)s.push(n[o]),o++;else{for(o++;o<n.length&&!/^\s*\[/.test(n[o]);)o++;for(;o<n.length&&ownedTomlHeader(n[o],t);)for(o++;o<n.length&&!/^\s*\[/.test(n[o]);)o++}return s.join("\n")}function renderEntry(e,t){if("mcp-toml"===e.kind){const r=[`# Added by \`aegiscode mcp install ${e.id}\`. Key resolution lives in`,"# ~/.aegiscode/.env and the 0600 store `aegiscode login` writes.","command = "+tomlString(t.command),"args = "+tomlArray(t.args)];if(t.env&&Object.keys(t.env).length){r.push("",`[${e.container}.${t.name}.env]`);for(const[e,n]of Object.entries(t.env))r.push(`${tomlKey(e)} = ${tomlString(n)}`)}return r}return e.entry(t)}function planTarget(e,t,r={}){const n=r.ctx||hostCtx(),s=r.spec||buildSpec(r.specOpts||{}),o=e.path(t,n),i={target:e,scope:t,file:o,exists:!1,changed:!1,before:null,after:null,error:null,backupOf:null};let a,c=null;try{const e=readText(o);c=e.text,i.exists=e.exists}catch(e){return i.error=e,i}if(i.exists)try{i.mode=511&fs.statSync(o).mode}catch{}if("mcp-toml"===e.kind){const t=`${e.container}.${s.name}`,r=renderEntry(e,s);return i.before=c,i.after=upsertTomlSection(c,t,r),i.changed=i.after!==(c||""),i}try{a=parseJson(c,o)}catch(e){return i.error=e,i}if(null===a||"object"!=typeof a||Array.isArray(a))return i.error=new Error(`${o} does not contain a JSON object — refusing to overwrite it`),i;if(setPath(a,`${e.container}.${s.name}`,renderEntry(e,s)),"function"==typeof e.extras){const t=e.extras(s);if(t&&t.inputs){const e=Array.isArray(a.inputs)?a.inputs:[];for(const r of t.inputs){const t=e.findIndex((e=>e&&e.id===r.id));-1===t?e.push(r):e[t]=r}a.inputs=e}}return i.before=c,i.after=JSON.stringify(a,null,2)+"\n",i.changed=i.after!==(c||""),i}function planRemove(e,t,r={}){const n=r.ctx||hostCtx(),s=r.spec||buildSpec(r.specOpts||{}),o=e.path(t,n),i={target:e,scope:t,file:o,exists:!1,changed:!1,before:null,after:null,error:null,backupOf:null};let a,c=null;try{const e=readText(o);c=e.text,i.exists=e.exists}catch(e){return i.error=e,i}if(!i.exists)return i;try{i.mode=511&fs.statSync(o).mode}catch{}if("mcp-toml"===e.kind)return i.before=c,i.after=removeTomlSection(c,`${e.container}.${s.name}`),i.changed=i.after!==c,i;try{a=parseJson(c,o)}catch(e){return i.error=e,i}const l=`${e.container}.${s.name}`,u=void 0!==getPath(a,l),f=Array.isArray(a.inputs)&&a.inputs.some((e=>e&&"aegis-api-key"===e.id));if(!u&&!f)return i.before=c,i.after=c,i.changed=!1,i;delPath(a,l,!0);const p=Array.isArray(a.inputs)?a.inputs.filter((e=>!(e&&"aegis-api-key"===e.id))):null;return p&&(p.length?a.inputs=p:delete a.inputs),i.before=c,i.after=JSON.stringify(a,null,2)+"\n",i.changed=i.after!==c,i}function modeFor(e){return e.withKey?384:e.mode||420}function apply(e,t={}){if(e.error)return{...e,applied:!1};if(!e.changed)return{...e,applied:!1};if(t.dryRun)return{...e,applied:!1,dryRun:!0};if(e.withKey=Boolean(t.withKey),fs.mkdirSync(path.dirname(e.file),{recursive:!0}),e.exists&&null!==e.before){const t=`${e.file}.aegis-backup-${Date.now()}`;fs.writeFileSync(t,e.before,{mode:384}),e.backupOf=t}const r=`${e.file}.aegis-tmp-${process.pid}`;try{fs.writeFileSync(r,e.after,{mode:384}),fs.renameSync(r,e.file),fs.chmodSync(e.file,modeFor(e))}finally{try{fs.unlinkSync(r)}catch{}}return{...e,applied:!0}}function parseTomlEntry(e,t){if(!e)return null;const r=t.replace(/[.[\]*+?^$(){}|]/g,"\\$&"),n=new RegExp("^\\s*\\["+r+"\\]\\s*$","m").exec(e);if(!n)return null;const s=e.slice(n.index+n[0].length).split("\n"),o=[];let i=0;for(;i<s.length&&(!/^\s*\[/.test(s[i])||ownedTomlHeader(s[i],t));)o.push(s[i]),i++;const a={command:null,args:[],env:{}},c=/^\s*command\s*=\s*"((?:[^"\\]|\\.)*)"/m.exec(o.join("\n"));c&&(a.command=JSON.parse('"'+c[1]+'"'));const l=/^\s*args\s*=\s*\[([\s\S]*?)\]/m.exec(o.join("\n"));l&&(a.args=[...l[1].matchAll(/"((?:[^"\\]|\\.)*)"/g)].map((e=>JSON.parse('"'+e[1]+'"'))));for(const e of o.join("\n").matchAll(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*"((?:[^"\\]|\\.)*)"\s*$/gm))"command"!==e[1]&&"args"!==e[1]&&(a.env[e[1]]=JSON.parse('"'+e[2]+'"'));return a}function entryServerPath(e){return e&&(Array.isArray(e.args)?e.args:[]).find((e=>"string"==typeof e&&/\.(js|mjs|cjs)$/.test(e)))||null}const HOST_VAR_RE=/\$\{?[A-Za-z_][A-Za-z0-9_]*\}?|%[A-Za-z_][A-Za-z0-9_]*%/;function hasHostVar(e){return"string"==typeof e&&HOST_VAR_RE.test(e)}function status(e,t={}){const r=t.ctx||hostCtx(),n=t.spec||buildSpec(t.specOpts||{}),s=e&&e.length?e.map((e=>getTarget(e))).filter(Boolean):TARGETS,o=[];for(const t of s)for(const s of t.scopes||["user"]){const i=t.path(s,r),a={id:t.id,label:t.label,scope:s,file:i,exists:!1,installed:!1,entry:null,error:null,docs:t.docs,verified:t.verified};let c;try{c=readText(i)}catch(e){a.error=e.message,o.push(a);continue}if(a.exists=c.exists,c.exists&&null!==c.text)if("mcp-toml"===t.kind)a.entry=parseTomlEntry(c.text,`${t.container}.${n.name}`),a.installed=Boolean(a.entry);else try{const e=parseJson(c.text,i);a.entry=getPath(e,`${t.container}.${n.name}`)||null,a.installed=Boolean(a.entry)}catch(e){a.error=e.message}a.installed&&(a.serverPath=entryServerPath(a.entry),a.serverUnverifiable=hasHostVar(a.serverPath),a.serverExists=a.serverPath&&!a.serverUnverifiable?isFile(a.serverPath):null),o.push(a)}return o}function detected(e={}){const t=e.ctx||hostCtx(),r=new Set;for(const e of TARGETS)for(const n of e.scopes||["user"])try{fs.accessSync(e.path(n,t)),r.add(e.id)}catch{}for(const[e,n]of[["Code","vscode"],["Code - Insiders","vscode-insiders"],["VSCodium","vscodium"],["Cursor","cursor"]])try{fs.accessSync(path.join(appConfig(t),e)),r.add(n)}catch{}for(const[e,n]of[[[".codeium"],"windsurf"],[[".gemini"],"gemini"],[[".codex"],"codex"],[[".continue"],"continue"],[[".junie"],"junie"],[[".cursor"],"cursor"]])try{fs.accessSync(path.join(t.home||t.env.HOME||t.env.USERPROFILE,...e)),r.add(n)}catch{}return r}module.exports={hostCtx:hostCtx,planTarget:planTarget,planRemove:planRemove,apply:apply,status:status,detected:detected,renderEntry:renderEntry,entryServerPath:entryServerPath,hasHostVar:hasHostVar,parseTomlEntry:parseTomlEntry,upsertTomlSection:upsertTomlSection,removeTomlSection:removeTomlSection,setPath:setPath,getPath:getPath,delPath:delPath,readText:readText,resolveServer:resolveServer};