@bazilio-san/glm-cc 1.0.20 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -13
- package/bin/glm-config.js +71 -97
- package/bin/glm.cmd +43 -45
- package/bin/glm.sh +7 -5
- package/bin/lib/api.js +92 -0
- package/bin/lib/config.js +151 -0
- package/bin/lib/models.js +97 -0
- package/bin/lib/ui.js +418 -0
- package/bin/lib/wizard.js +702 -0
- package/package.json +2 -1
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
// Interactive screens for `glm -c`.
|
|
2
|
+
//
|
|
3
|
+
// Settings are stored in ~/.glm-claude-code.env. Claude Code's own configuration is only ever read, and
|
|
4
|
+
// only by the health check, which reports conflicts instead of fixing them.
|
|
5
|
+
|
|
6
|
+
import { spawnSync } from 'node:child_process';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import os from 'node:os';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
ENV_PATH,
|
|
13
|
+
FIELDS,
|
|
14
|
+
DEFAULTS,
|
|
15
|
+
effective,
|
|
16
|
+
isComplete,
|
|
17
|
+
load,
|
|
18
|
+
maskSecret,
|
|
19
|
+
missingKeys,
|
|
20
|
+
save,
|
|
21
|
+
} from './config.js';
|
|
22
|
+
import { listModels, validateApiKey } from './api.js';
|
|
23
|
+
import {
|
|
24
|
+
ENDPOINTS,
|
|
25
|
+
contextWindowFor,
|
|
26
|
+
describeContext,
|
|
27
|
+
findEndpoint,
|
|
28
|
+
formatForClaudeCode,
|
|
29
|
+
formatTokens,
|
|
30
|
+
modelsUrlFor,
|
|
31
|
+
stripSuffix,
|
|
32
|
+
} from './models.js';
|
|
33
|
+
import {
|
|
34
|
+
box,
|
|
35
|
+
c,
|
|
36
|
+
clearScreen,
|
|
37
|
+
confirm,
|
|
38
|
+
input,
|
|
39
|
+
password,
|
|
40
|
+
pause,
|
|
41
|
+
select,
|
|
42
|
+
separator,
|
|
43
|
+
sleep,
|
|
44
|
+
spinner,
|
|
45
|
+
write,
|
|
46
|
+
} from './ui.js';
|
|
47
|
+
|
|
48
|
+
const BANNER = 'glm — GLM configuration for Claude Code';
|
|
49
|
+
|
|
50
|
+
function header (version, title) {
|
|
51
|
+
clearScreen();
|
|
52
|
+
box(BANNER, `v${version}`);
|
|
53
|
+
if (title) {
|
|
54
|
+
write(c.bold(title));
|
|
55
|
+
write('');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ── status ────────────────────────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
function endpointLabel (baseUrl) {
|
|
62
|
+
const known = findEndpoint(baseUrl);
|
|
63
|
+
return known ? `${known.label} ${c.gray(`(${baseUrl})`)}` : `${c.gray('custom')} ${baseUrl}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function printStatus (config) {
|
|
67
|
+
const values = effective(config);
|
|
68
|
+
const mainModel = stripSuffix(values.ANTHROPIC_DEFAULT_SONNET_MODEL);
|
|
69
|
+
const opusModel = stripSuffix(values.ANTHROPIC_DEFAULT_OPUS_MODEL);
|
|
70
|
+
const smallModel = stripSuffix(values.ANTHROPIC_DEFAULT_HAIKU_MODEL);
|
|
71
|
+
const token = config.ANTHROPIC_AUTH_TOKEN;
|
|
72
|
+
|
|
73
|
+
write(c.bold('Current configuration'));
|
|
74
|
+
write(` ${c.gray('Endpoint :')} ${endpointLabel(values.ANTHROPIC_BASE_URL)}`);
|
|
75
|
+
write(` ${c.gray('API key :')} ${token ? c.green(maskSecret(token)) : c.red('not set')}`);
|
|
76
|
+
write(` ${c.gray('Main model :')} ${c.green(mainModel)} ${c.gray(`— ${describeContext(mainModel)}`)}`);
|
|
77
|
+
if (opusModel !== mainModel) {
|
|
78
|
+
write(` ${c.gray('Opus slot :')} ${c.green(opusModel)} ${c.gray(`— ${describeContext(opusModel)}`)}`);
|
|
79
|
+
}
|
|
80
|
+
write(` ${c.gray('Small model :')} ${c.green(smallModel)} ${c.gray(`— ${describeContext(smallModel)}`)}`);
|
|
81
|
+
write(` ${c.gray('Timeout :')} ${formatTokens(values.API_TIMEOUT_MS)} ms`);
|
|
82
|
+
write(` ${c.gray('Telemetry :')} ${values.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC === '1' ? 'non-essential traffic off' : 'left at the Claude Code default'}`);
|
|
83
|
+
write(` ${c.gray('Compact at :')} ${formatTokens(values.CLAUDE_CODE_AUTO_COMPACT_WINDOW)} tokens`);
|
|
84
|
+
write('');
|
|
85
|
+
write(c.gray(` File: ${ENV_PATH}`));
|
|
86
|
+
write('');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function showConfig () {
|
|
90
|
+
const config = load();
|
|
91
|
+
const values = effective(config);
|
|
92
|
+
write('');
|
|
93
|
+
write(c.bold('glm configuration'));
|
|
94
|
+
write(c.gray(` ${ENV_PATH}`));
|
|
95
|
+
write('');
|
|
96
|
+
for (const field of FIELDS) {
|
|
97
|
+
const stored = config[field.key];
|
|
98
|
+
const value = values[field.key];
|
|
99
|
+
let shown;
|
|
100
|
+
if (stored == null) shown = c.gray(`${value || '(unset)'} — default`);
|
|
101
|
+
else if (stored === '') shown = c.gray('(not exported)');
|
|
102
|
+
else shown = field.secret ? c.green(maskSecret(value)) : c.green(value);
|
|
103
|
+
write(` ${field.key.padEnd(42)} ${shown}`);
|
|
104
|
+
}
|
|
105
|
+
write('');
|
|
106
|
+
if (!isComplete(config)) {
|
|
107
|
+
write(c.yellow(` Missing required settings: ${missingKeys(config).join(', ')}`));
|
|
108
|
+
write(c.gray(' Run `glm -c` to fill them in.'));
|
|
109
|
+
write('');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── endpoint ──────────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
async function pickEndpoint (currentBaseUrl) {
|
|
116
|
+
const known = findEndpoint(currentBaseUrl);
|
|
117
|
+
const choices = ENDPOINTS.map(endpoint => ({
|
|
118
|
+
name: endpoint.label + (endpoint.baseUrl === currentBaseUrl ? c.green(' ✓ in use') : ''),
|
|
119
|
+
value: endpoint.baseUrl,
|
|
120
|
+
hint: `— ${endpoint.baseUrl}`,
|
|
121
|
+
}));
|
|
122
|
+
choices.push({
|
|
123
|
+
name: 'Custom endpoint…',
|
|
124
|
+
value: '__custom__',
|
|
125
|
+
hint: '— any Anthropic-compatible server',
|
|
126
|
+
});
|
|
127
|
+
choices.push(separator(), { name: 'Back', value: null });
|
|
128
|
+
|
|
129
|
+
const chosen = await select({
|
|
130
|
+
message: 'Which API endpoint should Claude Code use?',
|
|
131
|
+
choices,
|
|
132
|
+
initial: known ? ENDPOINTS.indexOf(known) : (currentBaseUrl ? ENDPOINTS.length : 0),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
if (chosen === null) return null;
|
|
136
|
+
if (chosen !== '__custom__') return chosen;
|
|
137
|
+
|
|
138
|
+
write('');
|
|
139
|
+
return input({
|
|
140
|
+
message: 'Base URL',
|
|
141
|
+
defaultValue: currentBaseUrl || DEFAULTS.ANTHROPIC_BASE_URL,
|
|
142
|
+
validate: (value) => {
|
|
143
|
+
try {
|
|
144
|
+
const url = new URL(value);
|
|
145
|
+
if (!/^https?:$/.test(url.protocol)) return 'The URL must start with http:// or https://';
|
|
146
|
+
return true;
|
|
147
|
+
} catch {
|
|
148
|
+
return 'That is not a valid URL';
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ── api key ───────────────────────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
function apiKeyHint (baseUrl) {
|
|
157
|
+
const known = findEndpoint(baseUrl);
|
|
158
|
+
if (known) {
|
|
159
|
+
write(c.blue(` Get or manage your API key at ${known.apiKeysUrl}`));
|
|
160
|
+
} else {
|
|
161
|
+
write(c.gray(` Key check will use ${modelsUrlFor(baseUrl) || '(unknown model-list URL)'}`));
|
|
162
|
+
}
|
|
163
|
+
write('');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Ask for a key and verify it against the endpoint. Returns the key, or null
|
|
168
|
+
* if the user backed out.
|
|
169
|
+
*/
|
|
170
|
+
async function askApiKey (baseUrl, currentKey) {
|
|
171
|
+
for (;;) {
|
|
172
|
+
apiKeyHint(baseUrl);
|
|
173
|
+
if (currentKey) write(` ${c.gray('Stored key:')} ${c.green(maskSecret(currentKey))}\n`);
|
|
174
|
+
|
|
175
|
+
const key = await password({
|
|
176
|
+
message: currentKey ? 'New API key (leave empty to keep the stored one)' : 'API key',
|
|
177
|
+
validate: (value) => {
|
|
178
|
+
if (!value && currentKey) return true;
|
|
179
|
+
if (!value) return 'An API key is required';
|
|
180
|
+
return true;
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
if (!key) return currentKey;
|
|
185
|
+
|
|
186
|
+
const check = spinner('Checking the key against the endpoint…');
|
|
187
|
+
const result = await validateApiKey(key, baseUrl);
|
|
188
|
+
|
|
189
|
+
if (result.valid) {
|
|
190
|
+
check.succeed('API key accepted by the server');
|
|
191
|
+
await sleep(500);
|
|
192
|
+
return key;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (result.reason === 'invalid_key') check.fail(`API key rejected: ${result.message}`);
|
|
196
|
+
else if (result.reason === 'unreachable') check.fail(`Endpoint unreachable: ${result.message}`);
|
|
197
|
+
else check.fail(result.message);
|
|
198
|
+
write('');
|
|
199
|
+
|
|
200
|
+
const next = await select({
|
|
201
|
+
message: 'What now?',
|
|
202
|
+
choices: [
|
|
203
|
+
{ name: 'Enter the key again', value: 'retry' },
|
|
204
|
+
{ name: 'Save it anyway (skip the check)', value: 'force' },
|
|
205
|
+
{ name: 'Back', value: 'back' },
|
|
206
|
+
],
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
if (next === 'force') return key;
|
|
210
|
+
if (next === 'back') return currentKey ?? null;
|
|
211
|
+
write('');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ── models ────────────────────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
function modelChoice (id) {
|
|
218
|
+
return { name: id, value: id, hint: `— ${describeContext(id)}` };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function pickFromList (apiKey, baseUrl, message, current) {
|
|
222
|
+
const progress = spinner('Fetching the available models…');
|
|
223
|
+
const result = await listModels(apiKey, baseUrl);
|
|
224
|
+
|
|
225
|
+
if (result.error) {
|
|
226
|
+
progress.warn(`Could not fetch the model list: ${result.error}`);
|
|
227
|
+
await sleep(1200);
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
progress.succeed(`Fetched ${result.models.length} models`);
|
|
232
|
+
await sleep(300);
|
|
233
|
+
write('');
|
|
234
|
+
|
|
235
|
+
const choices = result.models.map(modelChoice);
|
|
236
|
+
const initial = result.models.indexOf(stripSuffix(current));
|
|
237
|
+
choices.push(separator(), { name: 'Back', value: null });
|
|
238
|
+
|
|
239
|
+
return select({
|
|
240
|
+
message,
|
|
241
|
+
choices,
|
|
242
|
+
pageSize: 14,
|
|
243
|
+
initial: initial >= 0 ? initial : 0,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Model selection screen. Returns { main, small } or null when cancelled.
|
|
249
|
+
* The Opus and Sonnet slots always get the same model: with GLM there is no
|
|
250
|
+
* separate stronger tier, and Claude Code picks between them on its own.
|
|
251
|
+
*/
|
|
252
|
+
async function pickModels (config) {
|
|
253
|
+
const values = effective(config);
|
|
254
|
+
const currentMain = stripSuffix(values.ANTHROPIC_DEFAULT_SONNET_MODEL);
|
|
255
|
+
const currentSmall = stripSuffix(values.ANTHROPIC_DEFAULT_HAIKU_MODEL);
|
|
256
|
+
const apiKey = config.ANTHROPIC_AUTH_TOKEN;
|
|
257
|
+
|
|
258
|
+
for (;;) {
|
|
259
|
+
write(` ${c.gray('Main model :')} ${c.green(currentMain)} ${c.gray(`— ${describeContext(currentMain)}`)}`);
|
|
260
|
+
write(` ${c.gray('Small model :')} ${c.green(currentSmall)} ${c.gray(`— ${describeContext(currentSmall)}`)}`);
|
|
261
|
+
write('');
|
|
262
|
+
|
|
263
|
+
const how = await select({
|
|
264
|
+
message: 'How do you want to choose the models?',
|
|
265
|
+
choices: [
|
|
266
|
+
{
|
|
267
|
+
name: 'Use the defaults',
|
|
268
|
+
value: 'default',
|
|
269
|
+
hint: `— ${stripSuffix(DEFAULTS.ANTHROPIC_DEFAULT_SONNET_MODEL)} and ${stripSuffix(DEFAULTS.ANTHROPIC_DEFAULT_HAIKU_MODEL)}`,
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
name: 'Pick from the models this key can use',
|
|
273
|
+
value: 'list',
|
|
274
|
+
hint: apiKey ? '— asks the server for the list' : c.gray('— needs an API key first'),
|
|
275
|
+
disabled: !apiKey,
|
|
276
|
+
},
|
|
277
|
+
{ name: 'Type the model ids by hand', value: 'manual' },
|
|
278
|
+
separator(),
|
|
279
|
+
{ name: 'Back', value: null },
|
|
280
|
+
],
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
if (how === null) return null;
|
|
284
|
+
|
|
285
|
+
if (how === 'default') {
|
|
286
|
+
return {
|
|
287
|
+
main: DEFAULTS.ANTHROPIC_DEFAULT_SONNET_MODEL,
|
|
288
|
+
small: DEFAULTS.ANTHROPIC_DEFAULT_HAIKU_MODEL,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (how === 'list') {
|
|
293
|
+
const main = await pickFromList(apiKey, values.ANTHROPIC_BASE_URL, 'Main model — used for ordinary work', currentMain);
|
|
294
|
+
if (main === undefined) continue;
|
|
295
|
+
if (main === null) continue;
|
|
296
|
+
write('');
|
|
297
|
+
const small = await pickFromList(apiKey, values.ANTHROPIC_BASE_URL, 'Small model — used for cheap background tasks', currentSmall);
|
|
298
|
+
if (small === undefined || small === null) continue;
|
|
299
|
+
return { main, small };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
write('');
|
|
303
|
+
const main = await input({
|
|
304
|
+
message: 'Main model id',
|
|
305
|
+
defaultValue: currentMain,
|
|
306
|
+
validate: (value) => (value.trim() ? true : 'A model id is required'),
|
|
307
|
+
});
|
|
308
|
+
const small = await input({
|
|
309
|
+
message: 'Small model id',
|
|
310
|
+
defaultValue: currentSmall,
|
|
311
|
+
validate: (value) => (value.trim() ? true : 'A model id is required'),
|
|
312
|
+
});
|
|
313
|
+
return { main, small };
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Write a model pair into the config. The `[1m]` suffix and the auto-compact
|
|
319
|
+
* window are derived here so the two can never contradict each other.
|
|
320
|
+
*/
|
|
321
|
+
function applyModels (config, { main, small }) {
|
|
322
|
+
const next = { ...config };
|
|
323
|
+
next.ANTHROPIC_DEFAULT_SONNET_MODEL = formatForClaudeCode(main);
|
|
324
|
+
next.ANTHROPIC_DEFAULT_OPUS_MODEL = formatForClaudeCode(main);
|
|
325
|
+
next.ANTHROPIC_DEFAULT_HAIKU_MODEL = formatForClaudeCode(small);
|
|
326
|
+
next.CLAUDE_CODE_AUTO_COMPACT_WINDOW = String(contextWindowFor(main));
|
|
327
|
+
return next;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// ── advanced ──────────────────────────────────────────────────────────────
|
|
331
|
+
|
|
332
|
+
async function advancedMenu (version, config) {
|
|
333
|
+
let current = config;
|
|
334
|
+
|
|
335
|
+
for (;;) {
|
|
336
|
+
const values = effective(current);
|
|
337
|
+
header(version, 'Advanced options');
|
|
338
|
+
write(c.gray(' These map one-to-one onto environment variables Claude Code reads.'));
|
|
339
|
+
write('');
|
|
340
|
+
|
|
341
|
+
const action = await select({
|
|
342
|
+
message: 'Which setting do you want to change?',
|
|
343
|
+
choices: [
|
|
344
|
+
{
|
|
345
|
+
name: 'Request timeout',
|
|
346
|
+
value: 'timeout',
|
|
347
|
+
hint: `— API_TIMEOUT_MS = ${formatTokens(values.API_TIMEOUT_MS)} ms`,
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
name: 'Non-essential traffic',
|
|
351
|
+
value: 'traffic',
|
|
352
|
+
hint: values.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC === '1' ? '— switched off' : '— left at the Claude Code default',
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
name: 'Auto-compact window',
|
|
356
|
+
value: 'compact',
|
|
357
|
+
hint: `— CLAUDE_CODE_AUTO_COMPACT_WINDOW = ${formatTokens(values.CLAUDE_CODE_AUTO_COMPACT_WINDOW)} tokens`,
|
|
358
|
+
},
|
|
359
|
+
separator(),
|
|
360
|
+
{ name: 'Back', value: null },
|
|
361
|
+
],
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
if (action === null) return current;
|
|
365
|
+
|
|
366
|
+
if (action === 'timeout') {
|
|
367
|
+
write('');
|
|
368
|
+
write(c.gray(' How long Claude Code waits for a single reply. 3000000 ms is 50 minutes.'));
|
|
369
|
+
write('');
|
|
370
|
+
const ms = await input({
|
|
371
|
+
message: 'Timeout in milliseconds',
|
|
372
|
+
defaultValue: values.API_TIMEOUT_MS,
|
|
373
|
+
validate: (value) => (/^\d+$/.test(value) && Number(value) > 0 ? true : 'Enter a positive whole number'),
|
|
374
|
+
});
|
|
375
|
+
current = { ...current, API_TIMEOUT_MS: ms };
|
|
376
|
+
save(current);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (action === 'traffic') {
|
|
380
|
+
write('');
|
|
381
|
+
const off = await select({
|
|
382
|
+
message: 'Optional telemetry and update pings',
|
|
383
|
+
choices: [
|
|
384
|
+
{ name: 'Switch off non-essential traffic', value: '1', hint: '— sets the variable to 1' },
|
|
385
|
+
{ name: 'Leave Claude Code at its default', value: '', hint: '— omits the variable entirely' },
|
|
386
|
+
],
|
|
387
|
+
initial: values.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC === '1' ? 0 : 1,
|
|
388
|
+
});
|
|
389
|
+
current = { ...current, CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: off };
|
|
390
|
+
save(current);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (action === 'compact') {
|
|
394
|
+
const mainModel = stripSuffix(values.ANTHROPIC_DEFAULT_SONNET_MODEL);
|
|
395
|
+
const matching = String(contextWindowFor(mainModel));
|
|
396
|
+
write('');
|
|
397
|
+
write(c.gray(' Claude Code compacts the conversation once it reaches this many tokens.'));
|
|
398
|
+
write(c.yellow(' Note: changing the main model recalculates this value.'));
|
|
399
|
+
write('');
|
|
400
|
+
const mode = await select({
|
|
401
|
+
message: 'Auto-compact window',
|
|
402
|
+
choices: [
|
|
403
|
+
{
|
|
404
|
+
name: 'Match the main model',
|
|
405
|
+
value: 'auto',
|
|
406
|
+
hint: `— ${formatTokens(matching)} tokens for ${mainModel}`,
|
|
407
|
+
},
|
|
408
|
+
{ name: 'Custom value', value: 'custom' },
|
|
409
|
+
separator(),
|
|
410
|
+
{ name: 'Back', value: null },
|
|
411
|
+
],
|
|
412
|
+
initial: values.CLAUDE_CODE_AUTO_COMPACT_WINDOW === matching ? 0 : 1,
|
|
413
|
+
});
|
|
414
|
+
if (mode === 'auto') {
|
|
415
|
+
current = { ...current, CLAUDE_CODE_AUTO_COMPACT_WINDOW: matching };
|
|
416
|
+
save(current);
|
|
417
|
+
} else if (mode === 'custom') {
|
|
418
|
+
write('');
|
|
419
|
+
const tokens = await input({
|
|
420
|
+
message: 'Tokens',
|
|
421
|
+
defaultValue: values.CLAUDE_CODE_AUTO_COMPACT_WINDOW,
|
|
422
|
+
validate: (value) => (/^\d+$/.test(value) && Number(value) > 0 ? true : 'Enter a positive whole number'),
|
|
423
|
+
});
|
|
424
|
+
current = { ...current, CLAUDE_CODE_AUTO_COMPACT_WINDOW: tokens };
|
|
425
|
+
save(current);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// ── health check ──────────────────────────────────────────────────────────
|
|
432
|
+
|
|
433
|
+
function commandExists (name) {
|
|
434
|
+
const probe = process.platform === 'win32'
|
|
435
|
+
? spawnSync('where', [name], { encoding: 'utf-8', windowsHide: true })
|
|
436
|
+
: spawnSync('sh', ['-c', `command -v ${name}`], { encoding: 'utf-8' });
|
|
437
|
+
return probe.status === 0 && String(probe.stdout || '').trim().length > 0;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Claude Code applies the `env` block of ~/.claude/settings.json on top of the
|
|
442
|
+
* inherited environment, so anything glm exports can be silently overridden
|
|
443
|
+
* there. This only reads that file and reports what it finds.
|
|
444
|
+
*/
|
|
445
|
+
function inspectClaudeSettings () {
|
|
446
|
+
const file = path.join(os.homedir(), '.claude', 'settings.json');
|
|
447
|
+
if (!fs.existsSync(file)) return { file, exists: false, overrides: [] };
|
|
448
|
+
try {
|
|
449
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
450
|
+
const env = parsed?.env && typeof parsed.env === 'object' ? parsed.env : {};
|
|
451
|
+
const managed = FIELDS.map(f => f.key);
|
|
452
|
+
const overrides = Object.keys(env).filter(key => managed.includes(key) || key === 'ANTHROPIC_API_KEY');
|
|
453
|
+
return { file, exists: true, overrides };
|
|
454
|
+
} catch (error) {
|
|
455
|
+
return { file, exists: true, overrides: [], error: String(error?.message || error) };
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export async function runDoctor () {
|
|
460
|
+
const config = load();
|
|
461
|
+
const values = effective(config);
|
|
462
|
+
const results = [];
|
|
463
|
+
const add = (name, passed, message) => results.push({ name, passed, message });
|
|
464
|
+
|
|
465
|
+
const check = spinner('Checking the configuration file…');
|
|
466
|
+
const hasConfig = fs.existsSync(ENV_PATH);
|
|
467
|
+
add('Configuration file', hasConfig, hasConfig ? undefined : 'Run `glm -c` to create it');
|
|
468
|
+
|
|
469
|
+
const complete = isComplete(config);
|
|
470
|
+
add('Required settings', complete, complete ? undefined : `Missing: ${missingKeys(config).join(', ')}`);
|
|
471
|
+
|
|
472
|
+
check.update('Looking for the claude executable…');
|
|
473
|
+
const claudeFound = commandExists('claude');
|
|
474
|
+
add('claude on PATH', claudeFound, claudeFound ? undefined : 'Install it with `npm install -g @anthropic-ai/claude-code`');
|
|
475
|
+
|
|
476
|
+
if (process.platform === 'win32') {
|
|
477
|
+
check.update('Looking for git…');
|
|
478
|
+
const gitFound = commandExists('git');
|
|
479
|
+
add('git on PATH', gitFound, gitFound ? undefined : 'Claude Code needs Git for Windows for its shell tools');
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
check.update('Checking the API key and the endpoint…');
|
|
483
|
+
if (!config.ANTHROPIC_AUTH_TOKEN) {
|
|
484
|
+
add('API key', false, 'No key stored — run `glm -c`');
|
|
485
|
+
} else {
|
|
486
|
+
const result = await validateApiKey(config.ANTHROPIC_AUTH_TOKEN, values.ANTHROPIC_BASE_URL);
|
|
487
|
+
add(`Endpoint ${values.ANTHROPIC_BASE_URL}`, result.valid, result.valid ? undefined : result.message);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
check.update('Checking the selected models…');
|
|
491
|
+
if (config.ANTHROPIC_AUTH_TOKEN) {
|
|
492
|
+
const result = await listModels(config.ANTHROPIC_AUTH_TOKEN, values.ANTHROPIC_BASE_URL);
|
|
493
|
+
if (result.models) {
|
|
494
|
+
const wanted = [
|
|
495
|
+
['Main model', stripSuffix(values.ANTHROPIC_DEFAULT_SONNET_MODEL)],
|
|
496
|
+
['Small model', stripSuffix(values.ANTHROPIC_DEFAULT_HAIKU_MODEL)],
|
|
497
|
+
];
|
|
498
|
+
for (const [label, model] of wanted) {
|
|
499
|
+
const known = result.models.includes(model);
|
|
500
|
+
add(`${label} ${model}`, known, known ? undefined : 'The server did not list this model id');
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
check.update('Checking for settings that would override glm…');
|
|
506
|
+
const settings = inspectClaudeSettings();
|
|
507
|
+
if (settings.error) {
|
|
508
|
+
add('Claude Code settings.json', false, `Could not be parsed: ${settings.error}`);
|
|
509
|
+
} else if (settings.overrides.length > 0) {
|
|
510
|
+
add('Claude Code settings.json', false,
|
|
511
|
+
`Its "env" block sets ${settings.overrides.join(', ')}, which wins over what glm exports. `
|
|
512
|
+
+ `Remove those keys from ${settings.file} — glm never edits that file.`);
|
|
513
|
+
} else {
|
|
514
|
+
add('Claude Code settings.json', true);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const strayKey = process.env.ANTHROPIC_API_KEY;
|
|
518
|
+
add('ANTHROPIC_API_KEY not set globally', !strayKey,
|
|
519
|
+
strayKey ? 'This variable is set in your shell and Claude Code may prefer it over the key glm exports' : undefined);
|
|
520
|
+
|
|
521
|
+
check.stop();
|
|
522
|
+
|
|
523
|
+
write('');
|
|
524
|
+
write(c.bold('Health check'));
|
|
525
|
+
write('');
|
|
526
|
+
let allPassed = true;
|
|
527
|
+
for (const result of results) {
|
|
528
|
+
write(` ${result.passed ? c.green('✔') : c.red('✘')} ${result.name}`);
|
|
529
|
+
if (result.message) write(` ${c.gray(result.message)}`);
|
|
530
|
+
if (!result.passed) allPassed = false;
|
|
531
|
+
}
|
|
532
|
+
write('');
|
|
533
|
+
if (allPassed) write(c.green(' Everything looks fine.'));
|
|
534
|
+
else write(c.gray(' Run `glm -c` to fix the settings glm owns; the notes above say where anything else lives.'));
|
|
535
|
+
write('');
|
|
536
|
+
return allPassed;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// ── setup wizard ──────────────────────────────────────────────────────────
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* First-run flow: endpoint, then key, then models. Saves as it goes so a
|
|
543
|
+
* half-finished run still leaves something useful behind.
|
|
544
|
+
*/
|
|
545
|
+
export async function runSetup (version, { reason } = {}) {
|
|
546
|
+
let config = load();
|
|
547
|
+
|
|
548
|
+
header(version, 'Setup');
|
|
549
|
+
if (reason) {
|
|
550
|
+
write(c.yellow(` ${reason}`));
|
|
551
|
+
write('');
|
|
552
|
+
}
|
|
553
|
+
write(c.gray(' Three questions: which server, which key, which models.'));
|
|
554
|
+
write(c.gray(` Answers are stored in ${ENV_PATH}`));
|
|
555
|
+
write(c.gray(' Claude Code\'s own configuration is left untouched.'));
|
|
556
|
+
write('');
|
|
557
|
+
|
|
558
|
+
const baseUrl = await pickEndpoint(effective(config).ANTHROPIC_BASE_URL);
|
|
559
|
+
if (baseUrl === null) return null;
|
|
560
|
+
config = { ...config, ANTHROPIC_BASE_URL: baseUrl };
|
|
561
|
+
save(config);
|
|
562
|
+
|
|
563
|
+
header(version, 'Setup — API key');
|
|
564
|
+
const key = await askApiKey(baseUrl, config.ANTHROPIC_AUTH_TOKEN);
|
|
565
|
+
if (!key) {
|
|
566
|
+
write(c.red('\n Without an API key Claude Code cannot start.'));
|
|
567
|
+
write('');
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
config = { ...config, ANTHROPIC_AUTH_TOKEN: key };
|
|
571
|
+
save(config);
|
|
572
|
+
|
|
573
|
+
header(version, 'Setup — models');
|
|
574
|
+
const models = await pickModels(config);
|
|
575
|
+
if (models) {
|
|
576
|
+
config = applyModels(config, models);
|
|
577
|
+
save(config);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
header(version, 'Setup complete');
|
|
581
|
+
printStatus(config);
|
|
582
|
+
write(c.green(' Saved. Run `glm` to start Claude Code with these settings.'));
|
|
583
|
+
write('');
|
|
584
|
+
return config;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// ── main menu ─────────────────────────────────────────────────────────────
|
|
588
|
+
|
|
589
|
+
export async function runMainMenu (version) {
|
|
590
|
+
let config = load();
|
|
591
|
+
|
|
592
|
+
for (;;) {
|
|
593
|
+
header(version);
|
|
594
|
+
printStatus(config);
|
|
595
|
+
|
|
596
|
+
const action = await select({
|
|
597
|
+
message: 'What do you want to do?',
|
|
598
|
+
choices: [
|
|
599
|
+
{ name: 'Run the setup wizard', value: 'setup', hint: '— endpoint, key and models in one pass' },
|
|
600
|
+
{ name: 'Change the API endpoint', value: 'endpoint' },
|
|
601
|
+
{ name: 'Change the API key', value: 'key' },
|
|
602
|
+
{ name: 'Change the models', value: 'models' },
|
|
603
|
+
{ name: 'Advanced options', value: 'advanced', hint: '— timeout, telemetry, compact window' },
|
|
604
|
+
separator(),
|
|
605
|
+
{ name: 'Health check', value: 'doctor', hint: '— verify the key, the endpoint and the models' },
|
|
606
|
+
{ name: 'Remove the glm configuration', value: 'remove' },
|
|
607
|
+
separator(),
|
|
608
|
+
{ name: 'Exit', value: 'exit' },
|
|
609
|
+
],
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
if (action === 'exit') {
|
|
613
|
+
write('');
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
if (action === 'setup') {
|
|
618
|
+
const updated = await runSetup(version);
|
|
619
|
+
if (updated) config = updated;
|
|
620
|
+
await pause();
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
if (action === 'endpoint') {
|
|
625
|
+
header(version, 'API endpoint');
|
|
626
|
+
const baseUrl = await pickEndpoint(effective(config).ANTHROPIC_BASE_URL);
|
|
627
|
+
if (baseUrl) {
|
|
628
|
+
config = { ...config, ANTHROPIC_BASE_URL: baseUrl };
|
|
629
|
+
save(config);
|
|
630
|
+
write('');
|
|
631
|
+
if (config.ANTHROPIC_AUTH_TOKEN) {
|
|
632
|
+
const check = spinner('Re-checking the stored key against the new endpoint…');
|
|
633
|
+
const result = await validateApiKey(config.ANTHROPIC_AUTH_TOKEN, baseUrl);
|
|
634
|
+
if (result.valid) check.succeed('The stored key works with this endpoint');
|
|
635
|
+
else check.warn(`The stored key does not work here: ${result.message}`);
|
|
636
|
+
} else {
|
|
637
|
+
write(c.yellow(' No API key stored yet — set one from the main menu.'));
|
|
638
|
+
}
|
|
639
|
+
write('');
|
|
640
|
+
await pause();
|
|
641
|
+
}
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if (action === 'key') {
|
|
646
|
+
header(version, 'API key');
|
|
647
|
+
const key = await askApiKey(effective(config).ANTHROPIC_BASE_URL, config.ANTHROPIC_AUTH_TOKEN);
|
|
648
|
+
if (key) {
|
|
649
|
+
config = { ...config, ANTHROPIC_AUTH_TOKEN: key };
|
|
650
|
+
save(config);
|
|
651
|
+
write(c.green('\n Saved.'));
|
|
652
|
+
write('');
|
|
653
|
+
await pause();
|
|
654
|
+
}
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
if (action === 'models') {
|
|
659
|
+
header(version, 'Models');
|
|
660
|
+
const models = await pickModels(config);
|
|
661
|
+
if (models) {
|
|
662
|
+
config = applyModels(config, models);
|
|
663
|
+
save(config);
|
|
664
|
+
const values = effective(config);
|
|
665
|
+
write('');
|
|
666
|
+
write(c.green(` Main model : ${values.ANTHROPIC_DEFAULT_SONNET_MODEL}`));
|
|
667
|
+
write(c.green(` Small model : ${values.ANTHROPIC_DEFAULT_HAIKU_MODEL}`));
|
|
668
|
+
write(c.gray(` Auto-compact window set to ${formatTokens(values.CLAUDE_CODE_AUTO_COMPACT_WINDOW)} tokens.`));
|
|
669
|
+
write('');
|
|
670
|
+
await pause();
|
|
671
|
+
}
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
if (action === 'advanced') {
|
|
676
|
+
config = await advancedMenu(version, config);
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
if (action === 'doctor') {
|
|
681
|
+
header(version, 'Health check');
|
|
682
|
+
await runDoctor();
|
|
683
|
+
await pause();
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
if (action === 'remove') {
|
|
688
|
+
header(version, 'Remove the glm configuration');
|
|
689
|
+
write(c.gray(` ${ENV_PATH}`));
|
|
690
|
+
write('');
|
|
691
|
+
const sure = await confirm({ message: 'Delete this configuration file?', defaultValue: false });
|
|
692
|
+
if (sure) {
|
|
693
|
+
fs.rmSync(ENV_PATH, { force: true });
|
|
694
|
+
write('');
|
|
695
|
+
write(c.gray(' The next `glm` run will ask you to set things up again.'));
|
|
696
|
+
config = {};
|
|
697
|
+
}
|
|
698
|
+
write('');
|
|
699
|
+
await pause();
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|