@gramatr/mcp 0.8.10 → 0.8.12

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 (60) hide show
  1. package/dist/bin/gramatr-mcp.js +1 -1
  2. package/dist/bin/setup-config-io.d.ts +26 -0
  3. package/dist/bin/setup-config-io.d.ts.map +1 -0
  4. package/dist/bin/setup-config-io.js +131 -0
  5. package/dist/bin/setup-config-io.js.map +1 -0
  6. package/dist/bin/setup-legacy.d.ts +9 -0
  7. package/dist/bin/setup-legacy.d.ts.map +1 -0
  8. package/dist/bin/setup-legacy.js +289 -0
  9. package/dist/bin/setup-legacy.js.map +1 -0
  10. package/dist/bin/setup-platforms.d.ts +20 -0
  11. package/dist/bin/setup-platforms.d.ts.map +1 -0
  12. package/dist/bin/setup-platforms.js +180 -0
  13. package/dist/bin/setup-platforms.js.map +1 -0
  14. package/dist/bin/setup-shared.d.ts +26 -0
  15. package/dist/bin/setup-shared.d.ts.map +1 -0
  16. package/dist/bin/setup-shared.js +39 -0
  17. package/dist/bin/setup-shared.js.map +1 -0
  18. package/dist/bin/setup.d.ts +14 -68
  19. package/dist/bin/setup.d.ts.map +1 -1
  20. package/dist/bin/setup.js +32 -612
  21. package/dist/bin/setup.js.map +1 -1
  22. package/dist/hooks/generated/schema-constants.d.ts +2 -2
  23. package/dist/hooks/generated/schema-constants.d.ts.map +1 -1
  24. package/dist/hooks/generated/schema-constants.js +2 -2
  25. package/dist/hooks/generated/schema-constants.js.map +1 -1
  26. package/dist/hooks/lib/gramatr-hook-utils.d.ts +1 -1
  27. package/dist/hooks/lib/gramatr-hook-utils.d.ts.map +1 -1
  28. package/dist/hooks/lib/gramatr-hook-utils.js +4 -5
  29. package/dist/hooks/lib/gramatr-hook-utils.js.map +1 -1
  30. package/dist/hooks/lib/hook-state.d.ts +21 -0
  31. package/dist/hooks/lib/hook-state.d.ts.map +1 -1
  32. package/dist/hooks/lib/hook-state.js +46 -0
  33. package/dist/hooks/lib/hook-state.js.map +1 -1
  34. package/dist/hooks/lib/project-file.d.ts +22 -0
  35. package/dist/hooks/lib/project-file.d.ts.map +1 -0
  36. package/dist/hooks/lib/project-file.js +44 -0
  37. package/dist/hooks/lib/project-file.js.map +1 -0
  38. package/dist/hooks/lib/session.d.ts +17 -1
  39. package/dist/hooks/lib/session.d.ts.map +1 -1
  40. package/dist/hooks/lib/session.js +65 -2
  41. package/dist/hooks/lib/session.js.map +1 -1
  42. package/dist/hooks/lib/types.d.ts +4 -0
  43. package/dist/hooks/lib/types.d.ts.map +1 -1
  44. package/dist/hooks/session-end.d.ts.map +1 -1
  45. package/dist/hooks/session-end.js +9 -2
  46. package/dist/hooks/session-end.js.map +1 -1
  47. package/dist/hooks/session-start.d.ts.map +1 -1
  48. package/dist/hooks/session-start.js +12 -2
  49. package/dist/hooks/session-start.js.map +1 -1
  50. package/dist/hooks/user-prompt-submit.d.ts.map +1 -1
  51. package/dist/hooks/user-prompt-submit.js +11 -1
  52. package/dist/hooks/user-prompt-submit.js.map +1 -1
  53. package/dist/setup/generated/instruction-blocks.d.ts +3 -1
  54. package/dist/setup/generated/instruction-blocks.d.ts.map +1 -1
  55. package/dist/setup/generated/instruction-blocks.js +3 -1
  56. package/dist/setup/generated/instruction-blocks.js.map +1 -1
  57. package/dist/setup/instructions.d.ts.map +1 -1
  58. package/dist/setup/instructions.js +5 -1
  59. package/dist/setup/instructions.js.map +1 -1
  60. package/package.json +3 -2
package/dist/bin/setup.js CHANGED
@@ -1,26 +1,36 @@
1
1
  /**
2
- * gramatr setup claude auto-configure Claude Code to use the local MCP server.
2
+ * gramatr setup — Main orchestrator for multi-platform setup.
3
3
  *
4
- * Writes the mcpServers entry into ~/.claude.json (Claude Code's global MCP config).
5
- * Safe: reads existing config, merges in the gramatr server entry, writes back.
6
- * Idempotent: running it twice produces the same result.
4
+ * Delegates to focused modules:
5
+ * - setup-config-io.ts — Config file reading/writing, path resolution
6
+ * - setup-legacy.ts — Legacy artifact cleanup (pre-rebrand remnants)
7
+ * - setup-platforms.ts — Platform-specific setup (Codex, Gemini, OpenCode, web, generic MCP)
8
+ * - setup-shared.ts — Binary resolution / deployment (npx no-ops)
9
+ *
10
+ * This file owns: setupClaude(), setupAutoInstall(), verifySetupInstall(),
11
+ * getAutoDetectedTargets(), and the convenience wrappers for MCP-only targets
12
+ * (Claude Desktop, ChatGPT Desktop, Cursor, Windsurf, VS Code).
7
13
  *
8
14
  * Usage:
9
15
  * gramatr-mcp setup claude Configure Claude Code
10
16
  * gramatr-mcp setup claude --dry Run without writing
11
17
  */
12
- import { readFileSync, writeFileSync, existsSync, mkdirSync, rmSync, readdirSync, statSync, lstatSync, readlinkSync, unlinkSync, } from 'node:fs';
18
+ import { readFileSync, writeFileSync, existsSync, mkdirSync, } from 'node:fs';
13
19
  import { join, dirname } from 'node:path';
14
- import { fileURLToPath } from 'node:url';
15
- import { getGramatrDirFromEnv, getGramatrUrlFromEnv } from '../config-runtime.js';
16
- import { buildClaudeHooksFile, buildCodexHooksFile, buildClaudeMcpServerEntry, ensureCodexHooksFeature, mergeManagedHooks, } from '../setup/integrations.js';
17
- import { CLAUDE_BLOCK_END, CLAUDE_BLOCK_START, CLAUDE_CODE_GUIDANCE, CODEX_BLOCK_END, CODEX_BLOCK_START, CODEX_GUIDANCE, buildInstallPromptSuggestion, } from '../setup/instructions.js';
18
- import { buildGeminiExtensionManifest, buildGeminiHooksFile, buildLocalMcpServerEntry, buildOpenCodeMcpServerEntry, getChatgptDesktopConfigPath, getClaudeDesktopConfigPath, getCursorConfigPath, getGeminiExtensionDir, getGeminiHooksPath, getGeminiManifestPath, getOpenCodeConfigPath, getVscodeConfigPath, getWindsurfConfigPath, mergeMcpServerConfig, } from '../setup/targets.js';
19
- import { buildConnectorInstructions, buildPromptSuggestion, validateServerReachability, } from '../setup/web-connector.js';
20
- const __filename = fileURLToPath(import.meta.url);
21
- const __dirname = dirname(__filename);
22
- // gramatr-allow: C1 — CLI entry point, reads HOME for config path
23
- const HOME = process.env.HOME || process.env.USERPROFILE || '';
20
+ import { getGramatrUrlFromEnv } from '../config-runtime.js';
21
+ import { buildClaudeHooksFile, buildClaudeMcpServerEntry, mergeManagedHooks, } from '../setup/integrations.js';
22
+ import { CLAUDE_BLOCK_END, CLAUDE_BLOCK_START, CLAUDE_CODE_GUIDANCE, CODEX_BLOCK_END, CODEX_BLOCK_START, } from '../setup/instructions.js';
23
+ import { getChatgptDesktopConfigPath, getClaudeDesktopConfigPath, getCursorConfigPath, getGeminiHooksPath, getGeminiManifestPath, getOpenCodeConfigPath, getVscodeConfigPath, getWindsurfConfigPath, } from '../setup/targets.js';
24
+ // ── Re-export everything from sub-modules so consumers keep importing from setup.js ──
25
+ export { getClaudeConfigPath, getClaudeSettingsPath, getCodexHooksPath, getCodexConfigPath, getClaudeMarkdownPath, getCodexAgentsPath, getGramatrSettingsPath, readJsonFile, readClaudeConfig, escapeRegExp, upsertManagedBlock, ensureLocalSettings, parseJson, readManagedBlock, hasHookCommand, } from './setup-config-io.js';
26
+ export { runCleanInstall, } from './setup-legacy.js';
27
+ export { ensureCodexMcpServerConfig, emitInstallPromptSuggestion, setupCodex, setupMcpTarget, setupGemini, setupOpenCode, setupWeb, } from './setup-platforms.js';
28
+ export { resolveBinaryPath, deployPlatformBinary, } from './setup-shared.js';
29
+ // ── Local imports from sub-modules ──
30
+ import { readClaudeConfig, upsertManagedBlock, ensureLocalSettings, getClaudeConfigPath, getClaudeSettingsPath, getClaudeMarkdownPath, getCodexHooksPath as getCodexHooksPathFn, getCodexConfigPath as getCodexConfigPathFn, getCodexAgentsPath, getGramatrSettingsPath as getGramatrSettingsPathFn, parseJson, readManagedBlock, hasHookCommand, HOME, } from './setup-config-io.js';
31
+ import { runCleanInstall } from './setup-legacy.js';
32
+ import { emitInstallPromptSuggestion, setupCodex, setupMcpTarget, setupGemini, setupOpenCode, } from './setup-platforms.js';
33
+ import { deployPlatformBinary } from './setup-shared.js';
24
34
  export const AUTO_TARGET_ORDER = [
25
35
  'claude',
26
36
  'codex',
@@ -32,425 +42,7 @@ export const AUTO_TARGET_ORDER = [
32
42
  'windsurf',
33
43
  'vscode',
34
44
  ];
35
- const LEGACY_HOOK_BASENAMES = [
36
- 'LoadContext.hook.ts',
37
- 'SecurityValidator.hook.ts',
38
- 'RatingCapture.hook.ts',
39
- 'VoiceGate.hook.ts',
40
- 'AutoWorkCreation.hook.ts',
41
- 'WorkCompletionLearning.hook.ts',
42
- 'RelationshipMemory.hook.ts',
43
- 'SessionSummary.hook.ts',
44
- 'UpdateCounts.hook.ts',
45
- 'IntegrityCheck.hook.ts',
46
- ];
47
- const LEGACY_MCP_KEY_PATTERNS = [/^aios/i, /^pai$/i, /^pai[-_]/i, /^fabric/i];
48
- const LEGACY_CLAUDE_ARTIFACT_PATTERNS = [
49
- /^pai[-_]/i,
50
- /^pai$/i,
51
- /^fabric[-_]/i,
52
- /^fabric$/i,
53
- /^aios[-_]/i,
54
- /^aios$/i,
55
- /^extract[-_]?wisdom/i,
56
- /^pattern[-_]/i,
57
- /^official[-_]?pattern/i,
58
- /^becreative$/i,
59
- /^beexpert$/i,
60
- ];
61
- const LEGACY_MANAGED_BLOCK_MARKERS = [
62
- { start: '<!-- AIOS-START -->', end: '<!-- AIOS-END -->' },
63
- { start: '<!-- PAI-START -->', end: '<!-- PAI-END -->' },
64
- { start: '<!-- FABRIC-START -->', end: '<!-- FABRIC-END -->' },
65
- { start: '<!-- GMTR-START -->', end: '<!-- GMTR-END -->' },
66
- ];
67
- /**
68
- * Resolve the path to the gramatr binary.
69
- * Prefers the compiled Bun binary at ~/.gramatr/bin/gramatr (self-contained,
70
- * no Node version dependency). Falls back to npx for first-run / not-yet-installed.
71
- */
72
- export function resolveBinaryPath() {
73
- const bunBin = join(HOME, '.gramatr', 'bin', 'gramatr');
74
- if (existsSync(bunBin)) {
75
- return { command: bunBin, args: [] };
76
- }
77
- // Fallback to npx (first install, before binary is deployed)
78
- return { command: 'npx', args: ['-y', '@gramatr/mcp'] };
79
- }
80
- /**
81
- * Deploy the correct platform-specific pre-compiled binary to ~/.gramatr/bin/.
82
- *
83
- * Checks for platform-specific binaries (e.g. gramatr-linux-x64) in the package
84
- * dist/ directory, then falls back to the generic 'gramatr' binary from
85
- * build-binary.mjs. If neither exists, logs a warning and returns false so the
86
- * caller can fall back to npx.
87
- *
88
- * Uses atomic copy (write to .new, then rename) to avoid partial-binary issues.
89
- */
90
- export function deployPlatformBinary(_dryRun = false) {
91
- // No-op: MCP server and hooks now use npx @gramatr/mcp (no compiled binary).
92
- // Compiled binaries are broken on macOS (Gatekeeper) and Windows (silent stdout).
93
- // Kept as a function so callers don't need updating.
94
- return true;
95
- }
96
- /**
97
- * Get the Claude Code config file path.
98
- * Claude Code stores global MCP config in ~/.claude.json
99
- */
100
- export function getClaudeConfigPath() {
101
- return join(HOME, '.claude.json');
102
- }
103
- export function getClaudeSettingsPath() {
104
- return join(HOME, '.claude', 'settings.json');
105
- }
106
- export function getCodexHooksPath() {
107
- return join(HOME, '.codex', 'hooks.json');
108
- }
109
- export function getCodexConfigPath() {
110
- return join(HOME, '.codex', 'config.toml');
111
- }
112
- export function getClaudeMarkdownPath() {
113
- return join(HOME, '.claude', 'CLAUDE.md');
114
- }
115
- export function getCodexAgentsPath() {
116
- return join(HOME, '.codex', 'AGENTS.md');
117
- }
118
- export function getGramatrSettingsPath() {
119
- const gramatrDir = getGramatrDirFromEnv() || join(HOME, '.gramatr');
120
- return join(gramatrDir, 'settings.json');
121
- }
122
- export function readJsonFile(path, fallback) {
123
- try {
124
- return JSON.parse(readFileSync(path, 'utf8'));
125
- }
126
- catch {
127
- return fallback;
128
- }
129
- }
130
- /**
131
- * Read existing Claude config or return empty.
132
- */
133
- export function readClaudeConfig(configPath) {
134
- try {
135
- const raw = readFileSync(configPath, 'utf8');
136
- return JSON.parse(raw);
137
- }
138
- catch {
139
- return {};
140
- }
141
- }
142
- export function upsertManagedBlock(existing, content, startMarker, endMarker) {
143
- const block = content.trim();
144
- const pattern = new RegExp(`${escapeRegExp(startMarker)}[\\s\\S]*?${escapeRegExp(endMarker)}`, 'm');
145
- if (pattern.test(existing)) {
146
- return existing.replace(pattern, block);
147
- }
148
- const trimmed = existing.trim();
149
- return trimmed ? `${trimmed}\n\n${block}\n` : `${block}\n`;
150
- }
151
- export function escapeRegExp(text) {
152
- return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
153
- }
154
- function toTomlString(value) {
155
- return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
156
- }
157
- function stripTomlSection(content, section) {
158
- const pattern = new RegExp(`^\\[${escapeRegExp(section)}\\]\\n[\\s\\S]*?(?=^\\[[^\\n]+\\]\\n|\\s*$)`, 'm');
159
- return content.replace(pattern, '').replace(/\n{3,}/g, '\n\n').trimEnd();
160
- }
161
- export function ensureCodexMcpServerConfig(configToml) {
162
- const gramatrDir = getGramatrDirFromEnv() || join(HOME, '.gramatr');
163
- const gramatrUrl = getGramatrUrlFromEnv() || 'https://api.gramatr.com/mcp';
164
- const base = stripTomlSection(stripTomlSection(configToml.trimEnd(), 'mcp_servers.gramatr.env'), 'mcp_servers.gramatr');
165
- const block = [
166
- '[mcp_servers.gramatr]',
167
- `command = ${toTomlString('npx')}`,
168
- `args = [${toTomlString('-y')}, ${toTomlString('@gramatr/mcp')}]`,
169
- '',
170
- '[mcp_servers.gramatr.env]',
171
- `GRAMATR_DIR = ${toTomlString(gramatrDir)}`,
172
- `GRAMATR_URL = ${toTomlString(gramatrUrl)}`,
173
- ].join('\n');
174
- return `${base ? `${base}\n\n` : ''}${block}\n`;
175
- }
176
- export function ensureLocalSettings() {
177
- const settingsPath = getGramatrSettingsPath();
178
- const settingsDir = dirname(settingsPath);
179
- const current = existsSync(settingsPath)
180
- ? readClaudeConfig(settingsPath)
181
- : {};
182
- const principalName = process.env.GRAMATR_PRINCIPAL_NAME || current.principal?.name || 'User';
183
- const principalTimezone = process.env.GRAMATR_PRINCIPAL_TIMEZONE || current.principal?.timezone || 'UTC';
184
- const next = {
185
- ...current,
186
- daidentity: current.daidentity || {
187
- name: 'gramatr',
188
- fullName: 'gramatr — Personal AI',
189
- displayName: 'gramatr',
190
- color: '#3B82F6',
191
- },
192
- principal: {
193
- ...(current.principal || {}),
194
- name: principalName,
195
- timezone: principalTimezone,
196
- },
197
- };
198
- mkdirSync(settingsDir, { recursive: true });
199
- writeFileSync(settingsPath, JSON.stringify(next, null, 2) + '\n', 'utf8');
200
- }
201
- function matchesLegacyClaudeArtifact(name) {
202
- const stem = name.replace(/\.md$/i, '');
203
- return LEGACY_CLAUDE_ARTIFACT_PATTERNS.some((pattern) => pattern.test(stem));
204
- }
205
- function sanitizeLegacyMcpServers(raw) {
206
- if (!raw.mcpServers || typeof raw.mcpServers !== 'object')
207
- return raw;
208
- const servers = raw.mcpServers;
209
- const next = {};
210
- for (const [key, value] of Object.entries(servers)) {
211
- if (LEGACY_MCP_KEY_PATTERNS.some((pattern) => pattern.test(key)))
212
- continue;
213
- next[key] = value;
214
- }
215
- return { ...raw, mcpServers: next };
216
- }
217
- function stripManagedBlock(content, startMarker, endMarker) {
218
- const pattern = new RegExp(`${escapeRegExp(startMarker)}[\\s\\S]*?${escapeRegExp(endMarker)}\\n?`, 'gm');
219
- return content.replace(pattern, '').trimEnd() + '\n';
220
- }
221
- function isLegacyHookCommand(command) {
222
- if (command.includes('/.claude/hooks/'))
223
- return true;
224
- if (command.includes('/aios-v2-client/'))
225
- return true;
226
- if (command.includes('/fabric/'))
227
- return true;
228
- if (command.includes('/pai/'))
229
- return true;
230
- return LEGACY_HOOK_BASENAMES.some((basename) => command.includes(`/${basename}`));
231
- }
232
- function sanitizeHookEventArray(value) {
233
- if (!Array.isArray(value))
234
- return value;
235
- return value
236
- .map((entry) => {
237
- if (!entry || typeof entry !== 'object')
238
- return entry;
239
- const commands = entry.hooks;
240
- if (!Array.isArray(commands))
241
- return entry;
242
- const filtered = commands.filter((cmd) => {
243
- const command = typeof cmd?.command === 'string' ? cmd.command : '';
244
- return !command || !isLegacyHookCommand(command);
245
- });
246
- if (filtered.length === 0)
247
- return null;
248
- return { ...entry, hooks: filtered };
249
- })
250
- .filter(Boolean);
251
- }
252
- function sanitizeHookFile(hookFile) {
253
- const hooksRoot = hookFile.hooks;
254
- if (!hooksRoot || typeof hooksRoot !== 'object')
255
- return hookFile;
256
- const nextHooks = { ...hooksRoot };
257
- for (const [eventName, value] of Object.entries(nextHooks)) {
258
- nextHooks[eventName] = sanitizeHookEventArray(value);
259
- }
260
- return { ...hookFile, hooks: nextHooks };
261
- }
262
- function removeDirectoryIfExists(path, dryRun) {
263
- if (!existsSync(path))
264
- return;
265
- if (dryRun) {
266
- process.stderr.write(`[gramatr-mcp] clean-install dry-run: would remove ${path}\n`);
267
- return;
268
- }
269
- rmSync(path, { recursive: true, force: true });
270
- process.stderr.write(`[gramatr-mcp] clean-install: removed ${path}\n`);
271
- }
272
- function removeFileIfExists(path, dryRun) {
273
- if (!existsSync(path))
274
- return;
275
- if (dryRun) {
276
- process.stderr.write(`[gramatr-mcp] clean-install dry-run: would remove ${path}\n`);
277
- return;
278
- }
279
- rmSync(path, { force: true });
280
- process.stderr.write(`[gramatr-mcp] clean-install: removed ${path}\n`);
281
- }
282
- function cleanLegacyClaudeArtifacts(dryRun) {
283
- process.stderr.write('[gramatr-mcp] clean-install: removing legacy Claude + PAI/Fabric/AIOS artifacts\n');
284
- const legacyDirs = [
285
- join(HOME, 'aios-v2-client'),
286
- join(HOME, '.claude', 'hooks'),
287
- ];
288
- for (const dir of legacyDirs)
289
- removeDirectoryIfExists(dir, dryRun);
290
- const commandsDir = join(HOME, '.claude', 'commands');
291
- if (existsSync(commandsDir)) {
292
- try {
293
- for (const entry of readdirSync(commandsDir)) {
294
- if (!entry.endsWith('.md'))
295
- continue;
296
- if (!matchesLegacyClaudeArtifact(entry))
297
- continue;
298
- removeFileIfExists(join(commandsDir, entry), dryRun);
299
- }
300
- }
301
- catch {
302
- // non-critical
303
- }
304
- }
305
- const skillsDir = join(HOME, '.claude', 'skills');
306
- if (existsSync(skillsDir)) {
307
- try {
308
- for (const entry of readdirSync(skillsDir)) {
309
- const path = join(skillsDir, entry);
310
- if (!statSync(path).isDirectory())
311
- continue;
312
- if (!matchesLegacyClaudeArtifact(entry))
313
- continue;
314
- removeDirectoryIfExists(path, dryRun);
315
- }
316
- }
317
- catch {
318
- // non-critical
319
- }
320
- }
321
- const clientHooksDir = join(HOME, '.gramatr', 'hooks');
322
- if (existsSync(clientHooksDir)) {
323
- for (const basename of LEGACY_HOOK_BASENAMES) {
324
- removeFileIfExists(join(clientHooksDir, basename), dryRun);
325
- }
326
- try {
327
- for (const entry of readdirSync(clientHooksDir)) {
328
- if (!entry.endsWith('.sh'))
329
- continue;
330
- removeFileIfExists(join(clientHooksDir, entry), dryRun);
331
- }
332
- }
333
- catch {
334
- // non-critical
335
- }
336
- }
337
- }
338
- function sanitizeLegacyMcpServersInFile(path, dryRun) {
339
- const current = readJsonFile(path, {});
340
- const next = sanitizeLegacyMcpServers(current);
341
- if (JSON.stringify(next) === JSON.stringify(current))
342
- return;
343
- if (dryRun) {
344
- process.stderr.write(`[gramatr-mcp] clean-install dry-run: would sanitize legacy mcpServers in ${path}\n`);
345
- return;
346
- }
347
- mkdirSync(dirname(path), { recursive: true });
348
- writeFileSync(path, JSON.stringify(next, null, 2) + '\n', 'utf8');
349
- process.stderr.write(`[gramatr-mcp] clean-install: sanitized legacy mcpServers in ${path}\n`);
350
- }
351
- function sanitizeHookFileAtPath(path, dryRun, label) {
352
- const current = readJsonFile(path, {});
353
- const next = sanitizeHookFile(current);
354
- if (JSON.stringify(next) === JSON.stringify(current))
355
- return;
356
- if (dryRun) {
357
- process.stderr.write(`[gramatr-mcp] clean-install dry-run: would sanitize legacy hooks in ${path}\n`);
358
- return;
359
- }
360
- mkdirSync(dirname(path), { recursive: true });
361
- writeFileSync(path, JSON.stringify(next, null, 2) + '\n', 'utf8');
362
- process.stderr.write(`[gramatr-mcp] clean-install: sanitized legacy hooks (${label}) in ${path}\n`);
363
- }
364
- function stripLegacyManagedBlocks(path, dryRun) {
365
- if (!existsSync(path))
366
- return;
367
- const current = readFileSync(path, 'utf8');
368
- let next = current;
369
- for (const marker of LEGACY_MANAGED_BLOCK_MARKERS) {
370
- next = stripManagedBlock(next, marker.start, marker.end);
371
- }
372
- if (next === current)
373
- return;
374
- if (dryRun) {
375
- process.stderr.write(`[gramatr-mcp] clean-install dry-run: would strip legacy managed blocks in ${path}\n`);
376
- return;
377
- }
378
- writeFileSync(path, next, 'utf8');
379
- process.stderr.write(`[gramatr-mcp] clean-install: stripped legacy managed blocks in ${path}\n`);
380
- }
381
- function cleanAllInstallTargets(dryRun) {
382
- sanitizeLegacyMcpServersInFile(getClaudeConfigPath(), dryRun);
383
- sanitizeLegacyMcpServersInFile(join(HOME, '.claude', 'mcp.json'), dryRun);
384
- sanitizeLegacyMcpServersInFile(getClaudeDesktopConfigPath(HOME), dryRun);
385
- sanitizeLegacyMcpServersInFile(getChatgptDesktopConfigPath(HOME), dryRun);
386
- sanitizeLegacyMcpServersInFile(getCursorConfigPath(HOME), dryRun);
387
- sanitizeLegacyMcpServersInFile(getWindsurfConfigPath(HOME), dryRun);
388
- sanitizeLegacyMcpServersInFile(getVscodeConfigPath(HOME), dryRun);
389
- sanitizeLegacyMcpServersInFile(getGeminiManifestPath(HOME), dryRun);
390
- sanitizeHookFileAtPath(getClaudeSettingsPath(), dryRun, 'claude');
391
- sanitizeHookFileAtPath(getCodexHooksPath(), dryRun, 'codex');
392
- sanitizeHookFileAtPath(getGeminiHooksPath(HOME), dryRun, 'gemini');
393
- stripLegacyManagedBlocks(getClaudeMarkdownPath(), dryRun);
394
- stripLegacyManagedBlocks(getCodexAgentsPath(), dryRun);
395
- }
396
- function cleanLegacyHomeNodeShims(dryRun) {
397
- const candidates = new Set();
398
- candidates.add(join(HOME, '.local', 'bin', 'gramatr-mcp'));
399
- candidates.add(join(HOME, '.local', 'bin', 'gramatr'));
400
- candidates.add(join(HOME, 'bin', 'gramatr-mcp'));
401
- candidates.add(join(HOME, 'bin', 'gramatr'));
402
- candidates.add(join(HOME, 'bin', 'gramatr-mac'));
403
- for (const shimPath of candidates) {
404
- if (!existsSync(shimPath))
405
- continue;
406
- try {
407
- let stale = false;
408
- if (lstatSync(shimPath).isSymbolicLink()) {
409
- const target = readlinkSync(shimPath);
410
- if (target.includes('aios-v2-client')
411
- || target.includes('/packages/client/bin/gramatr')
412
- || target.includes('/fabric/')
413
- || target.includes('/pai/')) {
414
- stale = true;
415
- }
416
- }
417
- else {
418
- const body = readFileSync(shimPath, 'utf8');
419
- if (body.includes('aios-v2-client')
420
- || body.includes('/packages/client/bin/gramatr')
421
- || body.includes('/fabric/')
422
- || body.includes('/pai/')) {
423
- stale = true;
424
- }
425
- }
426
- if (!stale)
427
- continue;
428
- if (dryRun) {
429
- process.stderr.write(`[gramatr-mcp] clean-install dry-run: would remove stale shim ${shimPath}\n`);
430
- }
431
- else {
432
- unlinkSync(shimPath);
433
- process.stderr.write(`[gramatr-mcp] clean-install: removed stale shim ${shimPath}\n`);
434
- }
435
- }
436
- catch {
437
- // non-critical
438
- }
439
- }
440
- }
441
- export function runCleanInstall(dryRun) {
442
- process.stderr.write('[gramatr-mcp] clean-install: running global legacy cleanup across known install targets\n');
443
- cleanAllInstallTargets(dryRun);
444
- cleanLegacyClaudeArtifacts(dryRun);
445
- cleanLegacyHomeNodeShims(dryRun);
446
- }
447
- export function emitInstallPromptSuggestion(target) {
448
- const promptBlock = buildInstallPromptSuggestion(target);
449
- process.stderr.write('\n━━━ Prompt Suggestion (copy into custom instructions) ━━━\n\n');
450
- process.stdout.write(promptBlock + '\n');
451
- process.stderr.write('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
452
- process.stderr.write('[gramatr-mcp] Paste the prompt block above into your custom instructions / project knowledge.\n');
453
- }
45
+ // ── setupClaude — the primary Claude Code setup ──
454
46
  export function setupClaude(dryRun = false, cleanInstall = false, showPrompts = false) {
455
47
  if (cleanInstall) {
456
48
  runCleanInstall(dryRun);
@@ -536,60 +128,7 @@ export function setupClaude(dryRun = false, cleanInstall = false, showPrompts =
536
128
  if (showPrompts)
537
129
  emitInstallPromptSuggestion('claude-code');
538
130
  }
539
- export function setupCodex(dryRun = false, showPrompts = false) {
540
- deployPlatformBinary(dryRun);
541
- const hooksPath = getCodexHooksPath();
542
- const configPath = getCodexConfigPath();
543
- const agentsPath = getCodexAgentsPath();
544
- const hooksConfig = readClaudeConfig(hooksPath);
545
- const managedHooks = buildCodexHooksFile(join(HOME, '.gramatr'));
546
- const mergedHooks = mergeManagedHooks(hooksConfig, managedHooks);
547
- const existingToml = existsSync(configPath) ? readFileSync(configPath, 'utf8') : '';
548
- const updatedToml = ensureCodexMcpServerConfig(ensureCodexHooksFeature(existingToml));
549
- const existingAgents = existsSync(agentsPath) ? readFileSync(agentsPath, 'utf8') : '';
550
- const updatedAgents = upsertManagedBlock(existingAgents, CODEX_GUIDANCE, CODEX_BLOCK_START, CODEX_BLOCK_END);
551
- if (dryRun) {
552
- process.stderr.write('[gramatr-mcp] Dry run — would write to: ' + hooksPath + '\n');
553
- process.stderr.write(JSON.stringify(mergedHooks, null, 2) + '\n');
554
- process.stderr.write('[gramatr-mcp] Dry run — would write to: ' + configPath + '\n');
555
- process.stderr.write(updatedToml);
556
- process.stderr.write('\n[gramatr-mcp] Dry run — would write to: ' + agentsPath + '\n');
557
- process.stderr.write(updatedAgents + '\n');
558
- return;
559
- }
560
- mkdirSync(join(HOME, '.codex'), { recursive: true });
561
- ensureLocalSettings();
562
- writeFileSync(hooksPath, JSON.stringify(mergedHooks, null, 2) + '\n', 'utf8');
563
- writeFileSync(configPath, updatedToml, 'utf8');
564
- writeFileSync(agentsPath, updatedAgents, 'utf8');
565
- process.stderr.write(`[gramatr-mcp] Configured Codex hooks in ${hooksPath}\n`);
566
- process.stderr.write(`[gramatr-mcp] Configured Codex MCP + hooks in ${configPath}\n`);
567
- process.stderr.write(`[gramatr-mcp] Configured Codex guidance in ${agentsPath}\n`);
568
- process.stderr.write('[gramatr-mcp] Restart Codex or start a new session to pick up the change.\n');
569
- if (showPrompts)
570
- emitInstallPromptSuggestion('codex');
571
- }
572
- /**
573
- * Generic MCP-only target setup — merges the gramatr MCP server entry into
574
- * the target's JSON config file. Used by all platforms that support MCP via
575
- * a JSON config (Claude Desktop, ChatGPT Desktop, Cursor, Windsurf, VS Code).
576
- */
577
- export function setupMcpTarget(targetName, configPath, dryRun) {
578
- deployPlatformBinary(dryRun);
579
- const current = readJsonFile(configPath, {});
580
- const gramatrUrl = getGramatrUrlFromEnv() || 'https://api.gramatr.com/mcp';
581
- const next = mergeMcpServerConfig(current, buildLocalMcpServerEntry(HOME, gramatrUrl));
582
- if (dryRun) {
583
- process.stderr.write(`[gramatr-mcp] Dry run — would write ${targetName} config to: ${configPath}\n`);
584
- process.stderr.write(JSON.stringify(next, null, 2) + '\n');
585
- return;
586
- }
587
- mkdirSync(dirname(configPath), { recursive: true });
588
- ensureLocalSettings();
589
- writeFileSync(configPath, JSON.stringify(next, null, 2) + '\n', 'utf8');
590
- process.stderr.write(`[gramatr-mcp] Configured ${targetName} MCP server in ${configPath}\n`);
591
- process.stderr.write(`[gramatr-mcp] Restart ${targetName} to pick up the change.\n`);
592
- }
131
+ // ── Convenience wrappers for MCP-only targets ──
593
132
  export function setupClaudeDesktop(dryRun = false, showPrompts = false) {
594
133
  setupMcpTarget('Claude Desktop', getClaudeDesktopConfigPath(HOME), dryRun);
595
134
  if (showPrompts)
@@ -615,6 +154,7 @@ export function setupVscode(dryRun = false, showPrompts = false) {
615
154
  if (showPrompts)
616
155
  emitInstallPromptSuggestion('vscode');
617
156
  }
157
+ // ── Auto-detect + auto-install ──
618
158
  export function getAutoDetectedTargets() {
619
159
  const checks = {
620
160
  claude: existsSync(join(HOME, '.claude')) || existsSync(getClaudeConfigPath()),
@@ -696,130 +236,10 @@ export function setupAutoInstall(options = {}) {
696
236
  process.stderr.write(`[gramatr-mcp] Auto setup completed for ${selected.length} target(s).\n`);
697
237
  return selected.length;
698
238
  }
699
- export function setupGemini(dryRun = false, showPrompts = false) {
700
- deployPlatformBinary(dryRun);
701
- const extensionDir = getGeminiExtensionDir(HOME);
702
- const manifestPath = getGeminiManifestPath(HOME);
703
- const hooksPath = getGeminiHooksPath(HOME);
704
- const gramatrUrl = getGramatrUrlFromEnv() || 'https://api.gramatr.com/mcp';
705
- const manifest = buildGeminiExtensionManifest(HOME, gramatrUrl);
706
- const hooks = buildGeminiHooksFile(HOME);
707
- if (dryRun) {
708
- process.stderr.write('[gramatr-mcp] Dry run — would write Gemini manifest to: ' + manifestPath + '\n');
709
- process.stderr.write(JSON.stringify(manifest, null, 2) + '\n');
710
- process.stderr.write('[gramatr-mcp] Dry run — would write Gemini hooks to: ' + hooksPath + '\n');
711
- process.stderr.write(JSON.stringify(hooks, null, 2) + '\n');
712
- if (showPrompts)
713
- emitInstallPromptSuggestion('gemini-cli');
714
- return;
715
- }
716
- mkdirSync(join(extensionDir, 'hooks'), { recursive: true });
717
- ensureLocalSettings();
718
- writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + '\n', 'utf8');
719
- writeFileSync(hooksPath, JSON.stringify(hooks, null, 2) + '\n', 'utf8');
720
- process.stderr.write(`[gramatr-mcp] Configured Gemini extension manifest in ${manifestPath}\n`);
721
- process.stderr.write(`[gramatr-mcp] Configured Gemini hooks in ${hooksPath}\n`);
722
- process.stderr.write('[gramatr-mcp] Restart Gemini CLI to pick up the change.\n');
723
- if (showPrompts)
724
- emitInstallPromptSuggestion('gemini-cli');
725
- }
726
- export function setupOpenCode(dryRun = false, showPrompts = false) {
727
- deployPlatformBinary(dryRun);
728
- const configPath = getOpenCodeConfigPath(HOME);
729
- const gramatrUrl = getGramatrUrlFromEnv() || 'https://api.gramatr.com/mcp';
730
- const mcpEntry = buildOpenCodeMcpServerEntry(HOME, gramatrUrl);
731
- // OpenCode stores its config as JSON with an mcp section (similar to Cursor/VS Code)
732
- const current = readJsonFile(configPath, {});
733
- const next = mergeMcpServerConfig(current, mcpEntry);
734
- if (dryRun) {
735
- process.stderr.write('[gramatr-mcp] Dry run — would write OpenCode config to: ' + configPath + '\n');
736
- process.stderr.write(JSON.stringify(next, null, 2) + '\n');
737
- if (showPrompts)
738
- emitInstallPromptSuggestion('opencode');
739
- return;
740
- }
741
- mkdirSync(dirname(configPath), { recursive: true });
742
- ensureLocalSettings();
743
- writeFileSync(configPath, JSON.stringify(next, null, 2) + '\n', 'utf8');
744
- process.stderr.write(`[gramatr-mcp] Configured OpenCode MCP server in ${configPath}\n`);
745
- process.stderr.write('[gramatr-mcp] Copy the plugin scaffold from packages/mcp/src/setup/examples/gramatr-opencode-plugin.ts\n');
746
- process.stderr.write('[gramatr-mcp] Restart OpenCode to pick up the change.\n');
747
- if (showPrompts)
748
- emitInstallPromptSuggestion('opencode');
749
- }
750
- export async function setupWeb(target = 'claude-web') {
751
- const gramatrUrl = getGramatrUrlFromEnv() || 'https://api.gramatr.com/mcp';
752
- // Check reachability first
753
- process.stderr.write(`[gramatr-mcp] Checking server reachability at ${gramatrUrl}...\n`);
754
- const reachability = await validateServerReachability(gramatrUrl);
755
- if (reachability.reachable) {
756
- process.stderr.write(`[gramatr-mcp] Server is reachable (HTTP ${reachability.statusCode})\n\n`);
757
- }
758
- else {
759
- process.stderr.write(`[gramatr-mcp] Warning: server unreachable — ${reachability.error}\n`);
760
- process.stderr.write('[gramatr-mcp] Setup instructions generated anyway. Verify connectivity before use.\n\n');
761
- }
762
- // Build and display connector instructions
763
- const instructions = buildConnectorInstructions({ serverUrl: gramatrUrl, target });
764
- process.stderr.write('━━━ Connector Setup Instructions ━━━\n\n');
765
- process.stderr.write(` Target: ${instructions.target}\n`);
766
- process.stderr.write(` Server URL: ${instructions.serverUrl}\n`);
767
- process.stderr.write(` Auth: ${instructions.authMethod}\n`);
768
- process.stderr.write(` Server card: ${instructions.serverCardUrl}\n\n`);
769
- for (let i = 0; i < instructions.steps.length; i++) {
770
- process.stderr.write(` ${i + 1}. ${instructions.steps[i]}\n`);
771
- }
772
- // Build and display the prompt suggestion
773
- const promptBlock = buildPromptSuggestion(target);
774
- process.stderr.write('\n━━━ Prompt Suggestion (copy into custom instructions) ━━━\n\n');
775
- // Write to stdout so it's easily pipeable/copyable
776
- process.stdout.write(promptBlock + '\n');
777
- process.stderr.write('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
778
- process.stderr.write('[gramatr-mcp] Paste the prompt block above into your custom instructions / project knowledge.\n');
779
- }
239
+ // ── Verification ──
780
240
  function addResult(items, severity, label, detail) {
781
241
  items.push({ severity, label, detail });
782
242
  }
783
- function parseJson(path) {
784
- try {
785
- return JSON.parse(readFileSync(path, 'utf8'));
786
- }
787
- catch {
788
- return null;
789
- }
790
- }
791
- function hasHookCommand(config, eventName, needle) {
792
- if (!config || typeof config !== 'object')
793
- return false;
794
- const hooksRoot = config.hooks;
795
- if (!hooksRoot || typeof hooksRoot !== 'object')
796
- return false;
797
- const entries = hooksRoot[eventName];
798
- if (!Array.isArray(entries))
799
- return false;
800
- for (const entry of entries) {
801
- if (!entry || typeof entry !== 'object')
802
- continue;
803
- const commands = entry.hooks;
804
- if (!Array.isArray(commands))
805
- continue;
806
- for (const command of commands) {
807
- const value = command?.command;
808
- if (typeof value === 'string' && value.includes(needle)) {
809
- return true;
810
- }
811
- }
812
- }
813
- return false;
814
- }
815
- function readManagedBlock(path, startMarker, endMarker) {
816
- if (!existsSync(path))
817
- return null;
818
- const body = readFileSync(path, 'utf8');
819
- const pattern = new RegExp(`${escapeRegExp(startMarker)}[\\s\\S]*?${escapeRegExp(endMarker)}`, 'm');
820
- const match = body.match(pattern);
821
- return match ? match[0] : null;
822
- }
823
243
  function verifyClaude(items) {
824
244
  const configPath = getClaudeConfigPath();
825
245
  const settingsPath = getClaudeSettingsPath();
@@ -852,8 +272,8 @@ function verifyClaude(items) {
852
272
  }
853
273
  }
854
274
  function verifyCodex(items) {
855
- const hooksPath = getCodexHooksPath();
856
- const configPath = getCodexConfigPath();
275
+ const hooksPath = getCodexHooksPathFn();
276
+ const configPath = getCodexConfigPathFn();
857
277
  const agentsPath = getCodexAgentsPath();
858
278
  const hooks = parseJson(hooksPath);
859
279
  const hasPromptHook = hasHookCommand(hooks, 'UserPromptSubmit', 'hook user-prompt-submit');
@@ -928,7 +348,7 @@ function verifyGemini(items) {
928
348
  }
929
349
  }
930
350
  function verifyLocalSettings(items) {
931
- const settingsPath = getGramatrSettingsPath();
351
+ const settingsPath = getGramatrSettingsPathFn();
932
352
  const settings = parseJson(settingsPath);
933
353
  if (!settings) {
934
354
  addResult(items, 'error', 'runtime.settings', `${settingsPath} is missing or invalid JSON`);