codemax-pro 1.4.2 → 1.4.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.
Files changed (4) hide show
  1. package/README.md +93 -85
  2. package/index.js +339 -301
  3. package/lib.js +520 -487
  4. package/package.json +30 -30
package/index.js CHANGED
@@ -1,301 +1,339 @@
1
- #!/usr/bin/env node
2
-
3
- /* CodeMax setup for Claude Code and Claude Desktop.
4
- *
5
- * codemax-pro [key] configure everything
6
- * codemax-pro doctor report what is actually configured, and what is fighting it
7
- * codemax-pro reset remove what this tool set, and nothing else
8
- *
9
- * The three commands share one description of every location (lib.js) on purpose: when setup,
10
- * doctor and reset each carry their own idea of where things live, reset starts leaving behind
11
- * things doctor never learned to look for. */
12
-
13
- 'use strict';
14
-
15
- const readline = require('readline');
16
- const L = require('./lib');
17
-
18
- const c = {
19
- reset: '\x1b[0m', bold: '\x1b[1m', dim: '\x1b[2m',
20
- cyan: '\x1b[36m', green: '\x1b[32m', yellow: '\x1b[33m', red: '\x1b[31m',
21
- gray: '\x1b[90m', white: '\x1b[97m',
22
- };
23
- const NO_COLOR = !!process.env.NO_COLOR || !process.stdout.isTTY;
24
- const p = (s = '') => console.log(NO_COLOR ? String(s).replace(/\x1b\[[0-9;]*m/g, '') : s);
25
- const ok = (m) => p(` ${c.green}✓${c.reset} ${m}`);
26
- const bad = (m) => p(` ${c.red}✗${c.reset} ${m}`);
27
- const warn = (m) => p(` ${c.yellow}!${c.reset} ${m}`);
28
- const step = (m) => p(`\n${c.cyan}${m}${c.reset}`);
29
- const dim = (m) => p(` ${c.gray}${m}${c.reset}`);
30
-
31
- function ask(q) {
32
- const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
33
- return new Promise((res) => rl.question(q, (a) => { rl.close(); res(a.trim()); }));
34
- }
35
-
36
- function header() {
37
- p('');
38
- p(` ${c.bold}CodeMax${c.reset} ${c.gray}·${c.reset} ${L.API_ENDPOINT}`);
39
- }
40
-
41
- // ─── doctor ──────────────────────────────────────────────────────────────────
42
-
43
- /* Reads only. Its value is naming the failures that produce no error anywhere: a machine-wide
44
- * policy that silently outranks the per-user one, an environment variable that outranks the
45
- * settings file, and an app still running with the configuration it read at launch. Each of those
46
- * looks exactly like "the installer did not work" from the outside. */
47
- async function doctor() {
48
- header();
49
- let problems = 0;
50
-
51
- step('Claude Code CLI');
52
- const cli = L.readCliState();
53
- if (!cli.exists) {
54
- warn('not configured — no settings file');
55
- dim(cli.file);
56
- problems++;
57
- } else {
58
- const pointed = cli.baseUrl === L.API_ENDPOINT;
59
- (pointed ? ok : bad)(`endpoint ${cli.baseUrl || '(not set)'}`);
60
- if (!pointed) problems++;
61
- (cli.key ? ok : bad)(`key ${L.redact(cli.key)}`);
62
- if (!cli.key) problems++;
63
- if (cli.defaultModelVars.length === 3) ok('default model settings present');
64
- else warn(`default model settings incomplete (${cli.defaultModelVars.length}/3) — the default may not be Opus 5`);
65
- if (cli.stalePresent.length) { warn(`stale variables present: ${cli.stalePresent.join(', ')}`); problems++; }
66
- if (cli.pinnedModel) { warn(`a model is pinned in settings ("${cli.pinnedModel}") — this adds a duplicate picker entry`); problems++; }
67
- dim(cli.file);
68
- }
69
-
70
- const info = L.claudeCliInfo();
71
- if (info.installed) {
72
- ok(`claude CLI installed${info.version ? ` ${info.version}` : ''}`);
73
- if (!info.version && info.at) dim(info.at);
74
- } else warn('claude CLI not found — run: npm install -g @anthropic-ai/claude-code');
75
-
76
- /* Two supported places, for two different situations, and only one of them is the ordinary
77
- * user's. The local profile is the per-user path; the registry branch under SOFTWARE\Policies
78
- * is the managed one and a normal account cannot write there at all — reg import answers
79
- * "Error accessing the registry" — which is by design, since the point of that branch is that
80
- * only whoever administers the machine controls it.
81
- *
82
- * So the profile is what decides whether this is configured. An absent policy is reported as
83
- * information, never as a fault: calling it a failure sent someone looking for a problem on a
84
- * machine that was set up correctly. */
85
- step('Claude Desktop');
86
- const d = L.readDesktopState();
87
- const prof = d.profile;
88
- if (prof && prof.inferenceProvider === 'gateway' && prof.inferenceGatewayBaseUrl === L.API_ENDPOINT) {
89
- ok(`provider gateway → ${prof.inferenceGatewayBaseUrl}`);
90
- const scheme = prof.inferenceGatewayAuthScheme;
91
- (scheme === 'x-api-key' ? ok : bad)(`auth ${scheme || '(default — expect 401)'}`);
92
- if (scheme !== 'x-api-key') problems++;
93
- const n = Array.isArray(prof.inferenceModels) ? prof.inferenceModels.length : 0;
94
- const oneM = Array.isArray(prof.inferenceModels) ? prof.inferenceModels.filter((m) => m && m.supports1m).length : 0;
95
- // A 1M model without `prefer1m` shows twice in the picker (200K and 1M) — say so rather than
96
- // report a count that does not match what the customer sees.
97
- const twice = Array.isArray(prof.inferenceModels) ? prof.inferenceModels.filter((m) => m && m.supports1m && !m.prefer1m).length : 0;
98
- (n ? ok : bad)(`models ${n} configured${oneM ? `, ${oneM} at 1M` : ''}${twice ? ` (${twice} also listed at 200K — re-run setup)` : ''}`);
99
- if (!n) problems++;
100
- ok(`fetch egress ${JSON.stringify(prof.coworkEgressAllowedHosts || [])}, preflight ${prof.skipWebFetchPreflight ? 'off' : 'ON'}`);
101
- dim(prof.file);
102
- } else {
103
- bad('not configured — run `codemax-pro` to set it up');
104
- problems++;
105
- }
106
-
107
- if (L.IS_WIN) {
108
- if (d.policy) dim('a managed policy is also set for this user');
109
- if (d.machinePolicy) {
110
- bad('a machine-wide policy exists and OVERRIDES everything above');
111
- dim(`${L.paths.winMachinePolicy} — whoever manages this device must set it there instead`);
112
- problems++;
113
- }
114
- }
115
-
116
- step('Conflicts');
117
- const conflicts = L.readEnvConflicts();
118
- if (!conflicts.length) ok('no environment variables competing with the settings file');
119
- else {
120
- for (const v of conflicts) bad(`${v.name} = ${v.value} (${v.scope}) — this OUTRANKS the settings file`);
121
- dim('run `codemax-pro` again to clear the persisted ones');
122
- problems += conflicts.length;
123
- }
124
-
125
- step('Connection');
126
- const key = cli.key || (d.policy && d.policy.key) || (d.profile && d.profile.inferenceGatewayApiKey);
127
- if (!key) { bad('no key found to test'); problems++; }
128
- else {
129
- const r = await L.checkKey(key);
130
- if (r.ok) ok(`endpoint reachable, key accepted ${r.ms}ms${r.note ? ` (${r.note})` : ''}`);
131
- else { bad(`${r.reason} ${r.ms}ms`); problems++; }
132
- }
133
-
134
- p('');
135
- if (problems === 0) {
136
- p(` ${c.green}Everything checks out.${c.reset}`);
137
- p(` ${c.gray}If the desktop app still behaves as before, it is still running with the${c.reset}`);
138
- p(` ${c.gray}configuration it read at launch — quit it from the tray, then reopen.${c.reset}`);
139
- } else {
140
- p(` ${c.yellow}${problems} thing${problems > 1 ? 's' : ''} to look at, listed above.${c.reset}`);
141
- }
142
- p('');
143
- return problems === 0 ? 0 : 1;
144
- }
145
-
146
- // ─── reset ───────────────────────────────────────────────────────────────────
147
-
148
- async function reset(opts) {
149
- header();
150
- step('Reset');
151
- p(` ${c.gray}Removes only what this tool set. Anything else in those files stays.${c.reset}`);
152
- if (!opts.yes) {
153
- const a = await ask(`\n Continue? ${c.gray}(y/N)${c.reset} `);
154
- if (a.toLowerCase() !== 'y') { p('\n Cancelled.\n'); return 0; }
155
- }
156
- const removed = L.resetAll();
157
- p('');
158
- if (!removed.length) ok('nothing to remove — no CodeMax settings found');
159
- else for (const r of removed) ok(`removed ${r}`);
160
- p('');
161
- p(` ${c.gray}Restart Claude Code and quit Claude Desktop from the tray to apply.${c.reset}`);
162
- p('');
163
- return 0;
164
- }
165
-
166
- // ─── setup ───────────────────────────────────────────────────────────────────
167
-
168
- async function setup(opts) {
169
- header();
170
-
171
- let key = opts.key || process.env.CODEMAX_API_KEY || '';
172
- if (!key) {
173
- if (opts.yes) { bad('no key given, and --yes means nothing can be asked'); return 1; }
174
- p('');
175
- p(` ${c.white}Paste your CodeMax API key${c.reset} ${c.gray}(from codemax.pro)${c.reset}`);
176
- key = await ask(' › ');
177
- }
178
- if (!key || key.length < 8) { p(''); bad('that does not look like a key'); p(''); return 1; }
179
-
180
- step('Checking the key');
181
- const check = await L.checkKey(key);
182
- if (check.ok) ok(`accepted ${check.ms}ms${check.note ? ` (${check.note})` : ''}`);
183
- else if (check.network) {
184
- warn(`could not reach the endpoint: ${check.reason}`);
185
- if (!opts.yes) {
186
- const a = await ask(` Configure anyway? ${c.gray}(y/N)${c.reset} `);
187
- if (a.toLowerCase() !== 'y') { p(''); return 1; }
188
- }
189
- } else { bad(check.reason); p(''); return 1; }
190
-
191
- const want = (t) => !opts.only || opts.only === t;
192
-
193
- if (want('cli')) {
194
- step('Claude Code CLI and VS Code');
195
- const r = L.configureCli(key);
196
- ok(r.file);
197
- if (r.backup) dim(`previous settings kept at ${r.backup}`);
198
- }
199
-
200
- if (want('desktop')) {
201
- step('Claude Desktop');
202
- const r = L.configureDesktop(key);
203
- for (const w of r.written) ok(w);
204
- /* The managed policy branch is attempted and expected to fail for an ordinary account —
205
- * SOFTWARE\Policies belongs to whoever administers the machine. That is not a problem to
206
- * report; the per-user profile above is the supported path here and is enough on its own. */
207
- if (r.policyError) dim('managed policy not written (needs an administrator — not required here)');
208
- if (!r.written.length) warn('nothing could be written — is the app installed?');
209
- if (r.machinePolicy) {
210
- warn('a machine-wide policy exists and overrides what was just written');
211
- dim(`${L.paths.winMachinePolicy} — ask whoever manages this device to set it there`);
212
- }
213
- }
214
-
215
- const cleaned = L.cleanLegacy();
216
- if (cleaned.length) {
217
- step('Cleaned up');
218
- for (const r of cleaned) dim(r);
219
- }
220
-
221
- if (want('cli') && !L.claudeCliInfo().installed) {
222
- step('Installing Claude Code CLI');
223
- if (L.sh('npm install -g @anthropic-ai/claude-code') !== null) ok('installed');
224
- else warn('could not install — run: npm install -g @anthropic-ai/claude-code');
225
- }
226
-
227
- /* Verifying after writing, not before. The check above proved the key works; this proves the
228
- * configuration that was just written is the configuration a client will read. */
229
- step('Verifying');
230
- const after = L.readCliState();
231
- if (!want('cli')) dim('skipped — CLI was not configured this run');
232
- else if (after.baseUrl === L.API_ENDPOINT && after.key) ok('settings file reads back correctly');
233
- else bad('settings file did not read back as expected — run `codemax-pro doctor`');
234
-
235
- const conflicts = L.readEnvConflicts();
236
- if (conflicts.length) {
237
- for (const v of conflicts) warn(`${v.name} is set (${v.scope}) and outranks the settings file`);
238
- } else ok('nothing competing with the settings file');
239
-
240
- p('');
241
- p(` ${c.green}Done.${c.reset}`);
242
- p('');
243
- p(` ${c.white}Claude Desktop must be quit from the tray, not just closed.${c.reset}`);
244
- p(` ${c.gray}It reads its configuration once at launch, so a window reopened from a${c.reset}`);
245
- p(` ${c.gray}running process keeps the old settings and looks like this did not work.${c.reset}`);
246
- p('');
247
- p(` ${c.gray}Check anything at any time: ${c.reset}codemax-pro doctor`);
248
- p('');
249
- return 0;
250
- }
251
-
252
- // ─── entry ───────────────────────────────────────────────────────────────────
253
-
254
- function help() {
255
- p(`
256
- ${c.bold}codemax-pro${c.reset} ${c.gray}· setup for Claude Code and Claude Desktop${c.reset}
257
-
258
- ${c.white}codemax-pro${c.reset} [key] configure everything
259
- ${c.white}codemax-pro doctor${c.reset} report what is configured, and what is fighting it
260
- ${c.white}codemax-pro reset${c.reset} remove what this tool set
261
-
262
- ${c.gray}--yes${c.reset} never prompt
263
- ${c.gray}--only cli|desktop${c.reset} configure one surface only
264
-
265
- ${c.gray}The key can also come from CODEMAX_API_KEY.${c.reset}
266
- `);
267
- }
268
-
269
- async function main() {
270
- const argv = process.argv.slice(2);
271
- const opts = { yes: false, only: null, key: '' };
272
- const rest = [];
273
- for (let i = 0; i < argv.length; i++) {
274
- const a = argv[i];
275
- if (a === '--yes' || a === '-y') opts.yes = true;
276
- else if (a === '--only') opts.only = (argv[++i] || '').toLowerCase();
277
- else if (a.startsWith('--only=')) opts.only = a.slice(7).toLowerCase();
278
- else if (a === '--help' || a === '-h') { help(); return 0; }
279
- else rest.push(a);
280
- }
281
- if (opts.only && !['cli', 'desktop'].includes(opts.only)) {
282
- bad(`--only takes "cli" or "desktop", not "${opts.only}"`);
283
- return 1;
284
- }
285
-
286
- const cmd = (rest[0] || '').toLowerCase();
287
- if (cmd === 'doctor') return doctor();
288
- if (cmd === 'reset') return reset(opts);
289
- if (cmd === 'help') { help(); return 0; }
290
- // Anything else is treated as the key, so `codemax-pro sk-cp-…` keeps working.
291
- opts.key = rest[0] || '';
292
- return setup(opts);
293
- }
294
-
295
- /* Set the exit code and let Node wind down on its own, rather than calling process.exit().
296
- * Forcing an exit while a socket from the connection check is still tearing down is what produced
297
- * the libuv assertion above; setting the code lets the loop drain first and reports the same
298
- * result to the shell. */
299
- main()
300
- .then((code) => { process.exitCode = code || 0; })
301
- .catch((e) => { p(''); bad(e.message); p(''); process.exitCode = 1; });
1
+ #!/usr/bin/env node
2
+
3
+ /* CodeMax setup for Claude Code and Claude Desktop.
4
+ *
5
+ * codemax-pro [key] configure everything
6
+ * codemax-pro doctor report what is actually configured, and what is fighting it
7
+ * codemax-pro reset remove what this tool set, and nothing else
8
+ *
9
+ * The three commands share one description of every location (lib.js) on purpose: when setup,
10
+ * doctor and reset each carry their own idea of where things live, reset starts leaving behind
11
+ * things doctor never learned to look for. */
12
+
13
+ 'use strict';
14
+
15
+ const readline = require('readline');
16
+ const L = require('./lib');
17
+
18
+ // PowerShell's default execution policy blocks npx.ps1; npx.cmd runs in PowerShell and cmd alike.
19
+ const NPX = L.IS_WIN ? 'npx.cmd' : 'npx';
20
+
21
+ const c = {
22
+ reset: '\x1b[0m', bold: '\x1b[1m', dim: '\x1b[2m',
23
+ cyan: '\x1b[36m', green: '\x1b[32m', yellow: '\x1b[33m', red: '\x1b[31m',
24
+ gray: '\x1b[90m', white: '\x1b[97m',
25
+ };
26
+ const NO_COLOR = !!process.env.NO_COLOR || !process.stdout.isTTY;
27
+ const p = (s = '') => console.log(NO_COLOR ? String(s).replace(/\x1b\[[0-9;]*m/g, '') : s);
28
+ const ok = (m) => p(` ${c.green}✓${c.reset} ${m}`);
29
+ const bad = (m) => p(` ${c.red}✗${c.reset} ${m}`);
30
+ const warn = (m) => p(` ${c.yellow}!${c.reset} ${m}`);
31
+ const step = (m) => p(`\n${c.cyan}${m}${c.reset}`);
32
+ const dim = (m) => p(` ${c.gray}${m}${c.reset}`);
33
+
34
+ function ask(q) {
35
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
36
+ return new Promise((res) => rl.question(q, (a) => { rl.close(); res(a.trim()); }));
37
+ }
38
+
39
+ function header() {
40
+ p('');
41
+ p(` ${c.bold}CodeMax${c.reset} ${c.gray}·${c.reset} ${L.API_ENDPOINT}`);
42
+ }
43
+
44
+ /* A value under SOFTWARE\Policies\Claude outranks everything this tool writes, and the app shows it
45
+ * as "Organization-managed" with every field locked. Shared by setup, doctor and reset so all three
46
+ * describe the same finding the same way. Returns how many findings still lock the app. */
47
+ function reportPolicies(policies) {
48
+ let locked = 0;
49
+ for (const pol of policies) {
50
+ if (pol.removed) {
51
+ ok('removed an old CodeMax policy that had locked Claude Desktop ("Organization-managed")');
52
+ dim(pol.key);
53
+ continue;
54
+ }
55
+ locked++;
56
+ if (pol.ours && pol.scope === 'user') {
57
+ // Our own values still there means the delete was refused: it needs an administrator.
58
+ const ownLeft = !pol.attempted || pol.left.some((n) => L.POLICY_VALUES.includes(n));
59
+ bad('Claude Desktop is locked ("Organization-managed") by an old CodeMax policy, which overrides this setup');
60
+ if (ownLeft) {
61
+ dim('To unlock it, open PowerShell as Administrator and run:');
62
+ dim(`${NPX} codemax-pro@latest`);
63
+ } else {
64
+ dim(`${pol.key} still holds other values: ${pol.left.join(', ')}`);
65
+ dim(`To unlock it, in PowerShell as Administrator: reg delete "${pol.key}" /f`);
66
+ }
67
+ } else if (pol.ours) {
68
+ bad('Claude Desktop is locked ("Organization-managed") by a CodeMax policy set for the whole computer');
69
+ dim('This tool leaves a machine-wide policy alone. To unlock it, in PowerShell as Administrator:');
70
+ dim(`reg delete "${pol.key}" /f`);
71
+ } else {
72
+ bad('Claude Desktop is managed by a policy on this computer that points elsewhere, and it overrides this setup');
73
+ dim(`${pol.key} → ${pol.values.inferenceGatewayBaseUrl || '(no endpoint)'} — whoever manages this computer has to change it`);
74
+ }
75
+ }
76
+ return locked;
77
+ }
78
+
79
+ // ─── doctor ──────────────────────────────────────────────────────────────────
80
+
81
+ /* Reads only. Its value is naming the failures that produce no error anywhere: a machine-wide
82
+ * policy that silently outranks the per-user one, an environment variable that outranks the
83
+ * settings file, and an app still running with the configuration it read at launch. Each of those
84
+ * looks exactly like "the installer did not work" from the outside. */
85
+ async function doctor() {
86
+ header();
87
+ let problems = 0;
88
+
89
+ step('Claude Code CLI');
90
+ const cli = L.readCliState();
91
+ if (!cli.exists) {
92
+ warn('not configured — no settings file');
93
+ dim(cli.file);
94
+ problems++;
95
+ } else {
96
+ const pointed = cli.baseUrl === L.API_ENDPOINT;
97
+ (pointed ? ok : bad)(`endpoint ${cli.baseUrl || '(not set)'}`);
98
+ if (!pointed) problems++;
99
+ (cli.key ? ok : bad)(`key ${L.redact(cli.key)}`);
100
+ if (!cli.key) problems++;
101
+ if (cli.defaultModelVars.length === 3) ok('default model settings present');
102
+ else warn(`default model settings incomplete (${cli.defaultModelVars.length}/3) — the default may not be Opus 5`);
103
+ if (cli.stalePresent.length) { warn(`stale variables present: ${cli.stalePresent.join(', ')}`); problems++; }
104
+ if (cli.pinnedModel) { warn(`a model is pinned in settings ("${cli.pinnedModel}") — this adds a duplicate picker entry`); problems++; }
105
+ dim(cli.file);
106
+ }
107
+
108
+ const info = L.claudeCliInfo();
109
+ if (info.installed) {
110
+ ok(`claude CLI installed${info.version ? ` ${info.version}` : ''}`);
111
+ if (!info.version && info.at) dim(info.at);
112
+ } else warn('claude CLI not found — run: npm install -g @anthropic-ai/claude-code');
113
+
114
+ /* Two places, for two different situations. The local profile is the ordinary user's, and the
115
+ * only one this tool writes. The registry branch under SOFTWARE\Policies is the managed one: when
116
+ * it holds anything, it wins, the app locks every field as "Organization-managed", and the profile
117
+ * is ignored. So an absent policy is the healthy state and says nothing; a present one is the
118
+ * first thing to name, because it makes a correct profile look like a setup that did not work. */
119
+ step('Claude Desktop');
120
+ const d = L.readDesktopState();
121
+ const prof = d.profile;
122
+ if (prof && prof.inferenceProvider === 'gateway' && prof.inferenceGatewayBaseUrl === L.API_ENDPOINT) {
123
+ ok(`provider gateway → ${prof.inferenceGatewayBaseUrl}`);
124
+ const scheme = prof.inferenceGatewayAuthScheme;
125
+ (scheme === 'x-api-key' ? ok : bad)(`auth ${scheme || '(default — expect 401)'}`);
126
+ if (scheme !== 'x-api-key') problems++;
127
+ const n = Array.isArray(prof.inferenceModels) ? prof.inferenceModels.length : 0;
128
+ const oneM = Array.isArray(prof.inferenceModels) ? prof.inferenceModels.filter((m) => m && m.supports1m).length : 0;
129
+ // A 1M model without `prefer1m` shows twice in the picker (200K and 1M) — say so rather than
130
+ // report a count that does not match what the customer sees.
131
+ const twice = Array.isArray(prof.inferenceModels) ? prof.inferenceModels.filter((m) => m && m.supports1m && !m.prefer1m).length : 0;
132
+ (n ? ok : bad)(`models ${n} configured${oneM ? `, ${oneM} at 1M` : ''}${twice ? ` (${twice} also listed at 200K — re-run setup)` : ''}`);
133
+ if (!n) problems++;
134
+ // Old ids still answer, so an outdated list never errors: the picker just shows old names.
135
+ const have = Array.isArray(prof.inferenceModels) ? prof.inferenceModels.map((m) => m && m.name) : [];
136
+ const missing = L.MODELS.map((m) => m.name).filter((id) => !have.includes(id));
137
+ if (n && missing.length) {
138
+ warn(`model list is out of date (no ${missing.join(', ')}) — run: ${NPX} codemax-pro@latest`);
139
+ problems++;
140
+ }
141
+ ok(`fetch egress ${JSON.stringify(prof.coworkEgressAllowedHosts || [])}, preflight ${prof.skipWebFetchPreflight ? 'off' : 'ON'}`);
142
+ dim(prof.file);
143
+ } else {
144
+ bad('not configured — run `codemax-pro` to set it up');
145
+ problems++;
146
+ }
147
+
148
+ problems += reportPolicies(d.policies);
149
+
150
+ step('Conflicts');
151
+ const conflicts = L.readEnvConflicts();
152
+ if (!conflicts.length) ok('no environment variables competing with the settings file');
153
+ else {
154
+ for (const v of conflicts) bad(`${v.name} = ${v.value} (${v.scope}) — this OUTRANKS the settings file`);
155
+ dim('run `codemax-pro` again to clear the persisted ones');
156
+ problems += conflicts.length;
157
+ }
158
+
159
+ step('Connection');
160
+ const ownPolicy = d.policies.find((pol) => pol.ours);
161
+ const key = cli.key || (ownPolicy && ownPolicy.values.inferenceGatewayApiKey) || (d.profile && d.profile.inferenceGatewayApiKey);
162
+ if (!key) { bad('no key found to test'); problems++; }
163
+ else {
164
+ const r = await L.checkKey(key);
165
+ if (r.ok) ok(`endpoint reachable, key accepted ${r.ms}ms${r.note ? ` (${r.note})` : ''}`);
166
+ else { bad(`${r.reason} ${r.ms}ms`); problems++; }
167
+ }
168
+
169
+ p('');
170
+ if (problems === 0) {
171
+ p(` ${c.green}Everything checks out.${c.reset}`);
172
+ p(` ${c.gray}If the desktop app still behaves as before, it is still running with the${c.reset}`);
173
+ p(` ${c.gray}configuration it read at launch — quit it from the tray, then reopen.${c.reset}`);
174
+ } else {
175
+ p(` ${c.yellow}${problems} thing${problems > 1 ? 's' : ''} to look at, listed above.${c.reset}`);
176
+ }
177
+ p('');
178
+ return problems === 0 ? 0 : 1;
179
+ }
180
+
181
+ // ─── reset ───────────────────────────────────────────────────────────────────
182
+
183
+ async function reset(opts) {
184
+ header();
185
+ step('Reset');
186
+ p(` ${c.gray}Removes only what this tool set. Anything else in those files stays.${c.reset}`);
187
+ if (!opts.yes) {
188
+ const a = await ask(`\n Continue? ${c.gray}(y/N)${c.reset} `);
189
+ if (a.toLowerCase() !== 'y') { p('\n Cancelled.\n'); return 0; }
190
+ }
191
+ const { removed, policies } = L.resetAll();
192
+ p('');
193
+ if (!removed.length) ok('nothing to remove — no CodeMax settings found');
194
+ else for (const r of removed) ok(`removed ${r}`);
195
+ reportPolicies(policies);
196
+ p('');
197
+ p(` ${c.gray}Restart Claude Code and quit Claude Desktop from the tray to apply.${c.reset}`);
198
+ p('');
199
+ // Our own policy still in place means reset did not finish its job.
200
+ return policies.some((pol) => pol.ours && pol.scope === 'user') ? 1 : 0;
201
+ }
202
+
203
+ // ─── setup ───────────────────────────────────────────────────────────────────
204
+
205
+ async function setup(opts) {
206
+ header();
207
+
208
+ let key = opts.key || process.env.CODEMAX_API_KEY || '';
209
+ if (!key) {
210
+ if (opts.yes) { bad('no key given, and --yes means nothing can be asked'); return 1; }
211
+ p('');
212
+ p(` ${c.white}Paste your CodeMax API key${c.reset} ${c.gray}(from codemax.pro)${c.reset}`);
213
+ key = await ask(' › ');
214
+ }
215
+ if (!key || key.length < 8) { p(''); bad('that does not look like a key'); p(''); return 1; }
216
+
217
+ step('Checking the key');
218
+ const check = await L.checkKey(key);
219
+ if (check.ok) ok(`accepted ${check.ms}ms${check.note ? ` (${check.note})` : ''}`);
220
+ else if (check.network) {
221
+ warn(`could not reach the endpoint: ${check.reason}`);
222
+ if (!opts.yes) {
223
+ const a = await ask(` Configure anyway? ${c.gray}(y/N)${c.reset} `);
224
+ if (a.toLowerCase() !== 'y') { p(''); return 1; }
225
+ }
226
+ } else { bad(check.reason); p(''); return 1; }
227
+
228
+ const want = (t) => !opts.only || opts.only === t;
229
+ let desktopLocked = 0;
230
+
231
+ if (want('cli')) {
232
+ step('Claude Code CLI and VS Code');
233
+ const r = L.configureCli(key);
234
+ ok(r.file);
235
+ if (r.backup) dim(`previous settings kept at ${r.backup}`);
236
+ }
237
+
238
+ if (want('desktop')) {
239
+ step('Claude Desktop');
240
+ const r = L.configureDesktop(key);
241
+ for (const w of r.written) ok(w);
242
+ if (!r.written.length) warn('nothing could be written — is the app installed?');
243
+ desktopLocked = reportPolicies(r.policies);
244
+ }
245
+
246
+ const cleaned = L.cleanLegacy();
247
+ if (cleaned.length) {
248
+ step('Cleaned up');
249
+ for (const r of cleaned) dim(r);
250
+ }
251
+
252
+ if (want('cli') && !L.claudeCliInfo().installed) {
253
+ step('Installing Claude Code CLI');
254
+ if (L.sh('npm install -g @anthropic-ai/claude-code') !== null) ok('installed');
255
+ else warn('could not install — run: npm install -g @anthropic-ai/claude-code');
256
+ }
257
+
258
+ /* Verifying after writing, not before. The check above proved the key works; this proves the
259
+ * configuration that was just written is the configuration a client will read. */
260
+ step('Verifying');
261
+ const after = L.readCliState();
262
+ if (!want('cli')) dim('skipped — CLI was not configured this run');
263
+ else if (after.baseUrl === L.API_ENDPOINT && after.key) ok('settings file reads back correctly');
264
+ else bad('settings file did not read back as expected — run `codemax-pro doctor`');
265
+
266
+ const conflicts = L.readEnvConflicts();
267
+ if (conflicts.length) {
268
+ for (const v of conflicts) warn(`${v.name} is set (${v.scope}) and outranks the settings file`);
269
+ } else ok('nothing competing with the settings file');
270
+
271
+ p('');
272
+ /* "Done" over a locked app hides the problem: the run looks successful and the picker never
273
+ * changes. Say plainly which half is finished. */
274
+ if (desktopLocked) {
275
+ p(` ${c.yellow}Claude Code is set up. Claude Desktop is still locked: see "Claude Desktop" above.${c.reset}`);
276
+ p('');
277
+ return 1;
278
+ }
279
+ p(` ${c.green}Done.${c.reset}`);
280
+ p('');
281
+ p(` ${c.white}Claude Desktop must be quit from the tray, not just closed.${c.reset}`);
282
+ p(` ${c.gray}It reads its configuration once at launch, so a window reopened from a${c.reset}`);
283
+ p(` ${c.gray}running process keeps the old settings and looks like this did not work.${c.reset}`);
284
+ p('');
285
+ p(` ${c.gray}Check anything at any time: ${c.reset}codemax-pro doctor`);
286
+ p('');
287
+ return 0;
288
+ }
289
+
290
+ // ─── entry ───────────────────────────────────────────────────────────────────
291
+
292
+ function help() {
293
+ p(`
294
+ ${c.bold}codemax-pro${c.reset} ${c.gray}· setup for Claude Code and Claude Desktop${c.reset}
295
+
296
+ ${c.white}codemax-pro${c.reset} [key] configure everything
297
+ ${c.white}codemax-pro doctor${c.reset} report what is configured, and what is fighting it
298
+ ${c.white}codemax-pro reset${c.reset} remove what this tool set
299
+
300
+ ${c.gray}--yes${c.reset} never prompt
301
+ ${c.gray}--only cli|desktop${c.reset} configure one surface only
302
+
303
+ ${c.gray}The key can also come from CODEMAX_API_KEY.${c.reset}
304
+ `);
305
+ }
306
+
307
+ async function main() {
308
+ const argv = process.argv.slice(2);
309
+ const opts = { yes: false, only: null, key: '' };
310
+ const rest = [];
311
+ for (let i = 0; i < argv.length; i++) {
312
+ const a = argv[i];
313
+ if (a === '--yes' || a === '-y') opts.yes = true;
314
+ else if (a === '--only') opts.only = (argv[++i] || '').toLowerCase();
315
+ else if (a.startsWith('--only=')) opts.only = a.slice(7).toLowerCase();
316
+ else if (a === '--help' || a === '-h') { help(); return 0; }
317
+ else rest.push(a);
318
+ }
319
+ if (opts.only && !['cli', 'desktop'].includes(opts.only)) {
320
+ bad(`--only takes "cli" or "desktop", not "${opts.only}"`);
321
+ return 1;
322
+ }
323
+
324
+ const cmd = (rest[0] || '').toLowerCase();
325
+ if (cmd === 'doctor') return doctor();
326
+ if (cmd === 'reset') return reset(opts);
327
+ if (cmd === 'help') { help(); return 0; }
328
+ // Anything else is treated as the key, so `codemax-pro sk-cp-…` keeps working.
329
+ opts.key = rest[0] || '';
330
+ return setup(opts);
331
+ }
332
+
333
+ /* Set the exit code and let Node wind down on its own, rather than calling process.exit().
334
+ * Forcing an exit while a socket from the connection check is still tearing down is what produced
335
+ * the libuv assertion above; setting the code lets the loop drain first and reports the same
336
+ * result to the shell. */
337
+ main()
338
+ .then((code) => { process.exitCode = code || 0; })
339
+ .catch((e) => { p(''); bad(e.message); p(''); process.exitCode = 1; });