@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
package/README.md
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
# @bazilio-san/glm-cc
|
|
2
2
|
|
|
3
|
-
CLI tool for running Claude
|
|
3
|
+
CLI tool for running Claude Code against a GLM-compatible API endpoint.
|
|
4
|
+
|
|
5
|
+
`glm` is a thin wrapper: it exports a handful of environment variables and then
|
|
6
|
+
tail-calls `claude` directly, with no Node process left in between. That matters
|
|
7
|
+
on Windows, where an intermediate process would break the parent terminal's
|
|
8
|
+
ConPty session and with it Claude Code's raw keyboard input.
|
|
4
9
|
|
|
5
10
|
## Installation
|
|
6
11
|
|
|
@@ -8,9 +13,12 @@ CLI tool for running Claude with custom GLM model configuration.
|
|
|
8
13
|
npm install -g @bazilio-san/glm-cc
|
|
9
14
|
```
|
|
10
15
|
|
|
16
|
+
No runtime dependencies — the package is plain Node.js plus two shell scripts.
|
|
17
|
+
|
|
11
18
|
## Usage
|
|
12
19
|
|
|
13
|
-
All arguments are passed through to `claude`, so you can use `glm` as a drop-in
|
|
20
|
+
All arguments are passed through to `claude`, so you can use `glm` as a drop-in
|
|
21
|
+
replacement:
|
|
14
22
|
|
|
15
23
|
```bash
|
|
16
24
|
glm # launch claude interactively
|
|
@@ -24,24 +32,74 @@ glm mcp serve # claude mcp serve
|
|
|
24
32
|
### glm-specific commands
|
|
25
33
|
|
|
26
34
|
```bash
|
|
27
|
-
glm -c #
|
|
28
|
-
glm --glm-show #
|
|
29
|
-
glm --glm-
|
|
35
|
+
glm -c # configuration menu (alias: --glm-config)
|
|
36
|
+
glm --glm-show # print the current settings
|
|
37
|
+
glm --glm-doctor # check the key, the endpoint and the models
|
|
38
|
+
glm --glm-version # print the glm version
|
|
39
|
+
glm --glm-help # list these flags
|
|
30
40
|
```
|
|
31
41
|
|
|
42
|
+
`-c` is consumed by glm and never reaches `claude`, so `claude -c`
|
|
43
|
+
(continue the previous session) is not available through `glm`.
|
|
44
|
+
|
|
32
45
|
## Configuration
|
|
33
46
|
|
|
34
|
-
|
|
47
|
+
Running `glm -c` opens an arrow-key menu that can:
|
|
48
|
+
|
|
49
|
+
- **Pick the API endpoint** — Z.AI Global, Z.AI China (BigModel), or any other
|
|
50
|
+
Anthropic-compatible URL you type in.
|
|
51
|
+
- **Set the API key** and verify it against the endpoint before saving. A
|
|
52
|
+
rejected key, an unreachable server and an HTTP error are reported separately,
|
|
53
|
+
so you can tell a wrong key from a network problem.
|
|
54
|
+
- **Choose the models** — keep the defaults, pick from the list of models your
|
|
55
|
+
key can actually use (fetched live from the provider), or type model ids by
|
|
56
|
+
hand. The `[1m]` suffix that unlocks a 1M-token context window and the
|
|
57
|
+
matching auto-compact threshold are derived from the model, not typed in.
|
|
58
|
+
- **Tune the advanced settings** — request timeout, non-essential traffic,
|
|
59
|
+
auto-compact window.
|
|
60
|
+
- **Run a health check** — configuration file, `claude` and `git` on PATH, key
|
|
61
|
+
validity, whether the selected models exist on the server, and whether
|
|
62
|
+
anything else on the machine would override what glm exports.
|
|
63
|
+
- **Remove the glm configuration** — deletes glm's configuration file.
|
|
64
|
+
|
|
65
|
+
The first `glm` run starts the same wizard automatically when no configuration
|
|
66
|
+
exists yet.
|
|
67
|
+
|
|
68
|
+
### Configuration file
|
|
35
69
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
- `ANTHROPIC_AUTH_TOKEN` — primary auth token
|
|
39
|
-
- `ANTHROPIC_DEFAULT_HAIKU_MODEL` — default Haiku model (default: `glm-4.5-air`)
|
|
40
|
-
- `ANTHROPIC_DEFAULT_SONNET_MODEL` — default Sonnet model (default: `glm-5.1`)
|
|
41
|
-
- `ANTHROPIC_DEFAULT_OPUS_MODEL` — default Opus model (default: `glm-5.1`)
|
|
70
|
+
`glm -c` saves settings in `~/.glm-claude-code.env`. The launcher reads the
|
|
71
|
+
same file, one literal `KEY=VALUE` pair per line.
|
|
42
72
|
|
|
43
|
-
|
|
73
|
+
**Claude Code's own configuration is never written to.** `~/.claude/settings.json`
|
|
74
|
+
and `~/.claude.json` are left alone; the health check reads `settings.json` only
|
|
75
|
+
to warn you when its `env` block would silently override glm's values, and tells
|
|
76
|
+
you to edit that file yourself.
|
|
77
|
+
|
|
78
|
+
### Managed variables
|
|
79
|
+
|
|
80
|
+
| Variable | Default | Meaning |
|
|
81
|
+
| --- | --- | --- |
|
|
82
|
+
| `ANTHROPIC_BASE_URL` | `https://api.z.ai/api/anthropic` | API endpoint |
|
|
83
|
+
| `ANTHROPIC_AUTH_TOKEN` | — | bearer token sent with every request |
|
|
84
|
+
| `ANTHROPIC_DEFAULT_SONNET_MODEL` | `glm-5.3[1m]` | model for ordinary work |
|
|
85
|
+
| `ANTHROPIC_DEFAULT_OPUS_MODEL` | `glm-5.3[1m]` | model for the strongest tier |
|
|
86
|
+
| `ANTHROPIC_DEFAULT_HAIKU_MODEL` | `glm-5.3-flash[1m]` | model for cheap background tasks |
|
|
87
|
+
| `API_TIMEOUT_MS` | `3000000` | how long to wait for one reply (50 minutes) |
|
|
88
|
+
| `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC` | `1` | switch off optional telemetry |
|
|
89
|
+
| `CLAUDE_CODE_AUTO_COMPACT_WINDOW` | `1048576` | token count at which history is compacted |
|
|
90
|
+
|
|
91
|
+
Defaults only apply to settings you have never set; existing values are kept as
|
|
92
|
+
they are.
|
|
93
|
+
|
|
94
|
+
## What is out of scope
|
|
95
|
+
|
|
96
|
+
MCP servers and the plugin marketplace are not managed here. Both live in Claude
|
|
97
|
+
Code's own configuration (`~/.claude.json` and the plugin store) rather than in
|
|
98
|
+
environment variables, so managing them would mean writing to files glm
|
|
99
|
+
deliberately keeps its hands off. Use `claude mcp …` and `claude plugin …` for
|
|
100
|
+
those.
|
|
44
101
|
|
|
45
102
|
## License
|
|
46
103
|
|
|
47
104
|
MIT
|
|
105
|
+
|
package/bin/glm-config.js
CHANGED
|
@@ -1,118 +1,92 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
//
|
|
3
|
-
// Invoked only when the user runs:
|
|
4
|
-
// glm --glm-config | -c → interactive reconfiguration
|
|
5
|
-
// glm --glm-show → print current config
|
|
6
|
-
// glm --glm-version → print glm version
|
|
7
|
-
// Or internally by glm.cmd / glm.sh on first launch (--setup) to populate
|
|
8
|
-
// the KEY=VALUE env sidecar file from the JSON config.
|
|
2
|
+
// Configuration front-end for @bazilio-san/glm-cc.
|
|
9
3
|
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
4
|
+
// Invoked when the user runs:
|
|
5
|
+
// glm -c | --glm-config → interactive configuration menu
|
|
6
|
+
// glm --glm-show → print the current settings
|
|
7
|
+
// glm --glm-doctor → health check
|
|
8
|
+
// glm --glm-version → print the glm version
|
|
9
|
+
// glm --glm-help → list the glm-specific flags
|
|
10
|
+
// Or internally by the launchers for first-time setup.
|
|
11
|
+
//
|
|
12
|
+
// CRITICAL: this Node process must NOT stay alive while `claude` runs. glm's
|
|
13
|
+
// entry point is a shell script that sets the environment variables and then
|
|
14
|
+
// tail-calls claude directly; a Node process sitting in between would break
|
|
15
|
+
// the parent terminal's ConPty session and with it Claude Code's raw keyboard
|
|
16
|
+
// input. Everything here therefore finishes and exits before claude starts.
|
|
14
17
|
|
|
15
|
-
import path from 'node:path';
|
|
16
|
-
import os from 'node:os';
|
|
17
|
-
import fs from 'node:fs';
|
|
18
|
-
import readline from 'node:readline';
|
|
19
18
|
import { createRequire } from 'node:module';
|
|
20
19
|
|
|
20
|
+
import { ENV_PATH, isComplete, missingKeys, load } from './lib/config.js';
|
|
21
|
+
import { runDoctor, runMainMenu, runSetup, showConfig } from './lib/wizard.js';
|
|
22
|
+
import { c, write } from './lib/ui.js';
|
|
23
|
+
|
|
21
24
|
const require = createRequire(import.meta.url);
|
|
22
25
|
const pkg = require('../package.json');
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
{
|
|
31
|
-
{
|
|
32
|
-
{
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
27
|
+
function printHelp () {
|
|
28
|
+
write('');
|
|
29
|
+
write(c.bold(`glm v${pkg.version}`));
|
|
30
|
+
write(c.gray(' Runs Claude Code against a GLM-compatible API endpoint.'));
|
|
31
|
+
write('');
|
|
32
|
+
write(c.bold('glm-specific flags'));
|
|
33
|
+
write(` ${'-c, --glm-config'.padEnd(20)} open the configuration menu`);
|
|
34
|
+
write(` ${'--glm-show'.padEnd(20)} print the current settings`);
|
|
35
|
+
write(` ${'--glm-doctor'.padEnd(20)} check the key, the endpoint and the models`);
|
|
36
|
+
write(` ${'--glm-version'.padEnd(20)} print the glm version`);
|
|
37
|
+
write(` ${'--glm-help'.padEnd(20)} this list`);
|
|
38
|
+
write('');
|
|
39
|
+
write(c.gray(' Every other argument is passed straight through to `claude`,'));
|
|
40
|
+
write(c.gray(' so `glm -p "hello"` and `glm mcp serve` behave as usual.'));
|
|
41
|
+
write(c.gray(' Note that -c is taken by glm and never reaches claude.'));
|
|
42
|
+
write('');
|
|
43
|
+
write(c.bold('Configuration file'));
|
|
44
|
+
write(c.gray(` ${ENV_PATH}`));
|
|
45
|
+
write(c.gray(' Claude Code\'s own configuration is never modified.'));
|
|
46
|
+
write('');
|
|
41
47
|
}
|
|
42
48
|
|
|
43
|
-
function
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
fs.writeFileSync(ENV_PATH, `${header}\n${body}\n`, 'utf-8');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function saveBoth (config) {
|
|
50
|
-
saveJson(config);
|
|
51
|
-
saveEnv(config);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function ask (rl, q) {
|
|
55
|
-
return new Promise(resolve => rl.question(q, resolve));
|
|
56
|
-
}
|
|
49
|
+
async function main () {
|
|
50
|
+
const args = process.argv.slice(2);
|
|
51
|
+
const has = (flag) => args.includes(flag);
|
|
57
52
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
console.log('\nConfiguration (press Enter to keep current value):\n');
|
|
62
|
-
for (const field of fieldsToAsk) {
|
|
63
|
-
const current = config[field.key] || field.default;
|
|
64
|
-
const hint = current ? ` [\x1b[32m${current}\x1b[0m]` : '';
|
|
65
|
-
const answer = await ask(rl, `${field.label}${hint}: `);
|
|
66
|
-
config[field.key] = answer.trim() !== '' ? answer.trim() : current;
|
|
53
|
+
if (has('--glm-version')) {
|
|
54
|
+
write(`glm v${pkg.version}`);
|
|
55
|
+
return 0;
|
|
67
56
|
}
|
|
68
|
-
rl.close();
|
|
69
|
-
saveBoth(config);
|
|
70
|
-
console.log(`\nSaved:\n ${JSON_PATH}\n ${ENV_PATH}\n`);
|
|
71
|
-
return config;
|
|
72
|
-
}
|
|
73
57
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
for (const f of FIELDS) {
|
|
78
|
-
const v = config[f.key];
|
|
79
|
-
console.log(` ${f.key} = ${v != null && v !== '' ? v : '(not set)'}`);
|
|
58
|
+
if (has('--glm-help')) {
|
|
59
|
+
printHelp();
|
|
60
|
+
return 0;
|
|
80
61
|
}
|
|
81
|
-
console.log('');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async function main () {
|
|
85
|
-
const args = process.argv.slice(2);
|
|
86
62
|
|
|
87
|
-
if (
|
|
88
|
-
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
if (args.includes('--glm-show')) {
|
|
92
|
-
show(loadJson());
|
|
93
|
-
return;
|
|
63
|
+
if (has('--glm-show')) {
|
|
64
|
+
showConfig();
|
|
65
|
+
return 0;
|
|
94
66
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
67
|
+
|
|
68
|
+
if (has('--glm-doctor')) {
|
|
69
|
+
const healthy = await runDoctor();
|
|
70
|
+
return healthy ? 0 : 1;
|
|
98
71
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
console.log('Some glm settings are missing. Please fill them in:\n');
|
|
109
|
-
await runInteractive(missing);
|
|
110
|
-
}
|
|
111
|
-
return;
|
|
72
|
+
|
|
73
|
+
if (has('--setup')) {
|
|
74
|
+
const config = load();
|
|
75
|
+
if (isComplete(config)) return 0;
|
|
76
|
+
const reason = Object.keys(config).length === 0
|
|
77
|
+
? 'No glm configuration found yet.'
|
|
78
|
+
: `Some settings are missing: ${missingKeys(config).join(', ')}`;
|
|
79
|
+
const result = await runSetup(pkg.version, { reason });
|
|
80
|
+
return result ? 0 : 1;
|
|
112
81
|
}
|
|
113
82
|
|
|
114
|
-
|
|
115
|
-
|
|
83
|
+
await runMainMenu(pkg.version);
|
|
84
|
+
return 0;
|
|
116
85
|
}
|
|
117
86
|
|
|
118
|
-
main()
|
|
87
|
+
main()
|
|
88
|
+
.then((code) => { process.exit(code); })
|
|
89
|
+
.catch((error) => {
|
|
90
|
+
write(c.red(`glm: ${error?.message || error}`));
|
|
91
|
+
process.exit(1);
|
|
92
|
+
});
|
package/bin/glm.cmd
CHANGED
|
@@ -1,45 +1,43 @@
|
|
|
1
|
-
:; SCRIPT="$(readlink -f "$0" 2>/dev/null || echo "$0")"; exec "$(dirname "$SCRIPT")/glm.sh" "$@"
|
|
2
|
-
@echo off
|
|
3
|
-
rem -------------------------------------------------------------------------
|
|
4
|
-
rem glm — launch claude with GLM env vars. Runs as a pure cmd.exe script so
|
|
5
|
-
rem that no intermediate node process sits between the caller (PTY / terminal)
|
|
6
|
-
rem and claude.exe. Critical for parent-PTY scenarios where the ConPty session
|
|
7
|
-
rem must reach claude directly for Ink-based raw-keyboard input to work.
|
|
8
|
-
rem
|
|
9
|
-
rem First line is a bash/batch polyglot:
|
|
10
|
-
rem bash : `:` = no-op, `;` separator, `exec` replaces shell with glm.sh
|
|
11
|
-
rem cmd : `:;` = malformed label, skipped silently
|
|
12
|
-
rem -------------------------------------------------------------------------
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
rem
|
|
37
|
-
rem
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
claude %*
|
|
45
|
-
exit /b %errorlevel%
|
|
1
|
+
:; SCRIPT="$(readlink -f "$0" 2>/dev/null || echo "$0")"; exec bash "$(dirname "$SCRIPT")/glm.sh" "$@" # Windows batch follows
|
|
2
|
+
@echo off
|
|
3
|
+
rem -------------------------------------------------------------------------
|
|
4
|
+
rem glm — launch claude with GLM env vars. Runs as a pure cmd.exe script so
|
|
5
|
+
rem that no intermediate node process sits between the caller (PTY / terminal)
|
|
6
|
+
rem and claude.exe. Critical for parent-PTY scenarios where the ConPty session
|
|
7
|
+
rem must reach claude directly for Ink-based raw-keyboard input to work.
|
|
8
|
+
rem
|
|
9
|
+
rem First line is a bash/batch polyglot:
|
|
10
|
+
rem bash : `:` = no-op, `;` separator, `exec` replaces shell with glm.sh
|
|
11
|
+
rem cmd : `:;` = malformed label, skipped silently
|
|
12
|
+
rem -------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
setlocal DisableDelayedExpansion
|
|
15
|
+
set "GLM_ARGS=%*"
|
|
16
|
+
|
|
17
|
+
rem --- glm-specific flags are delegated to the node helper ----------------
|
|
18
|
+
set "GLM_PAD= %GLM_ARGS% "
|
|
19
|
+
echo.%GLM_PAD%| findstr /I /C:" --glm-config " /C:" --glm-show " /C:" --glm-doctor " /C:" --glm-version " /C:" --glm-help " /C:" -c " >nul
|
|
20
|
+
if not errorlevel 1 goto config
|
|
21
|
+
|
|
22
|
+
rem --- First-time setup if env file is missing ----------------------------
|
|
23
|
+
set "GLM_ENV_FILE=%USERPROFILE%\.glm-claude-code.env"
|
|
24
|
+
if exist "%GLM_ENV_FILE%" goto launch
|
|
25
|
+
|
|
26
|
+
:setup
|
|
27
|
+
node "%~dp0glm-config.js" --setup
|
|
28
|
+
if errorlevel 1 exit /b 1
|
|
29
|
+
|
|
30
|
+
rem --- Load env vars from KEY=VALUE file ----------------------------------
|
|
31
|
+
:launch
|
|
32
|
+
for /f "usebackq eol=# delims=" %%L in ("%GLM_ENV_FILE%") do (
|
|
33
|
+
set "%%L"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
rem --- Tail-call claude directly. `claude.exe` becomes a child of the
|
|
37
|
+
rem outer (PTY-attached) cmd.exe process, not of any node intermediate.
|
|
38
|
+
claude %*
|
|
39
|
+
exit /b %errorlevel%
|
|
40
|
+
|
|
41
|
+
:config
|
|
42
|
+
node "%~dp0glm-config.js" %*
|
|
43
|
+
exit /b %errorlevel%
|
package/bin/glm.sh
CHANGED
|
@@ -18,7 +18,7 @@ GLM_ENV_FILE="${HOME}/.glm-claude-code.env"
|
|
|
18
18
|
|
|
19
19
|
# --- glm-specific flags delegate to the node helper -----------------------
|
|
20
20
|
case " $* " in
|
|
21
|
-
*" --glm-config "*|*" -c "*|*" --glm-show "*|*" --glm-version "*)
|
|
21
|
+
*" --glm-config "*|*" -c "*|*" --glm-show "*|*" --glm-doctor "*|*" --glm-version "*|*" --glm-help "*)
|
|
22
22
|
exec node "${GLM_BIN_DIR}/glm-config.js" "$@"
|
|
23
23
|
;;
|
|
24
24
|
esac
|
|
@@ -33,9 +33,11 @@ if [ ! -f "$GLM_ENV_FILE" ]; then
|
|
|
33
33
|
fi
|
|
34
34
|
|
|
35
35
|
# --- Load env vars and tail-call claude -----------------------------------
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
37
|
+
line=${line%$'\r'}
|
|
38
|
+
if [[ $line =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
|
|
39
|
+
export "$line"
|
|
40
|
+
fi
|
|
41
|
+
done < "$GLM_ENV_FILE"
|
|
40
42
|
|
|
41
43
|
exec claude "$@"
|
package/bin/lib/api.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Talks to the provider's model-list route, which doubles as a key check:
|
|
2
|
+
// a 401 means the key is wrong, a network failure means the endpoint is
|
|
3
|
+
// unreachable, and a successful reply carries the list of usable model ids.
|
|
4
|
+
|
|
5
|
+
import { modelsUrlFor } from './models.js';
|
|
6
|
+
|
|
7
|
+
const TIMEOUT_MS = 30_000;
|
|
8
|
+
|
|
9
|
+
async function getJson (url, apiKey) {
|
|
10
|
+
const controller = new AbortController();
|
|
11
|
+
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
|
12
|
+
try {
|
|
13
|
+
const response = await fetch(url, {
|
|
14
|
+
method: 'GET',
|
|
15
|
+
headers: {
|
|
16
|
+
Authorization: `Bearer ${apiKey}`,
|
|
17
|
+
'Content-Type': 'application/json',
|
|
18
|
+
},
|
|
19
|
+
signal: controller.signal,
|
|
20
|
+
});
|
|
21
|
+
let body = null;
|
|
22
|
+
try {
|
|
23
|
+
body = await response.json();
|
|
24
|
+
} catch {
|
|
25
|
+
body = null;
|
|
26
|
+
}
|
|
27
|
+
return { response, body };
|
|
28
|
+
} finally {
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Check that the key works against the given endpoint.
|
|
35
|
+
* Returns { valid, reason, message } where reason is one of
|
|
36
|
+
* 'invalid_key' | 'unreachable' | 'bad_endpoint' | 'http_error'.
|
|
37
|
+
*/
|
|
38
|
+
export async function validateApiKey (apiKey, baseUrl) {
|
|
39
|
+
const url = modelsUrlFor(baseUrl);
|
|
40
|
+
if (!url) {
|
|
41
|
+
return { valid: false, reason: 'bad_endpoint', message: `Cannot derive a model-list URL from ${baseUrl}` };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const { response } = await getJson(url, apiKey);
|
|
46
|
+
if (response.status === 401 || response.status === 403) {
|
|
47
|
+
return { valid: false, reason: 'invalid_key', message: 'The server rejected this API key' };
|
|
48
|
+
}
|
|
49
|
+
if (response.ok) return { valid: true };
|
|
50
|
+
return {
|
|
51
|
+
valid: false,
|
|
52
|
+
reason: 'http_error',
|
|
53
|
+
message: `Server answered HTTP ${response.status} ${response.statusText}`.trim(),
|
|
54
|
+
};
|
|
55
|
+
} catch (error) {
|
|
56
|
+
const timedOut = error instanceof Error && error.name === 'AbortError';
|
|
57
|
+
return {
|
|
58
|
+
valid: false,
|
|
59
|
+
reason: 'unreachable',
|
|
60
|
+
message: timedOut ? `No answer within ${TIMEOUT_MS / 1000} seconds` : String(error?.message || error),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Fetch the model ids available on this plan.
|
|
67
|
+
* Returns { models } on success, or { error } describing what went wrong.
|
|
68
|
+
*/
|
|
69
|
+
export async function listModels (apiKey, baseUrl) {
|
|
70
|
+
const url = modelsUrlFor(baseUrl);
|
|
71
|
+
if (!url) return { error: `Cannot derive a model-list URL from ${baseUrl}` };
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const { response, body } = await getJson(url, apiKey);
|
|
75
|
+
if (response.status === 401 || response.status === 403) {
|
|
76
|
+
return { error: 'The server rejected this API key' };
|
|
77
|
+
}
|
|
78
|
+
if (!response.ok) return { error: `Server answered HTTP ${response.status}` };
|
|
79
|
+
|
|
80
|
+
const rows = Array.isArray(body?.data) ? body.data : null;
|
|
81
|
+
if (!rows) return { error: 'The server replied in an unexpected shape' };
|
|
82
|
+
|
|
83
|
+
const models = rows
|
|
84
|
+
.map(row => (typeof row === 'string' ? row : row?.id))
|
|
85
|
+
.filter(id => typeof id === 'string' && id.length > 0);
|
|
86
|
+
|
|
87
|
+
return models.length > 0 ? { models } : { error: 'The server returned an empty model list' };
|
|
88
|
+
} catch (error) {
|
|
89
|
+
const timedOut = error instanceof Error && error.name === 'AbortError';
|
|
90
|
+
return { error: timedOut ? `No answer within ${TIMEOUT_MS / 1000} seconds` : String(error?.message || error) };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Storage layer for the glm configuration.
|
|
2
|
+
//
|
|
3
|
+
// The menu and both launchers share ~/.glm-claude-code.env.
|
|
4
|
+
//
|
|
5
|
+
// Claude Code's own files (~/.claude/settings.json, ~/.claude.json) are never
|
|
6
|
+
// read for writing and never modified: glm only ever exports environment
|
|
7
|
+
// variables into the process it launches.
|
|
8
|
+
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import { randomUUID } from 'node:crypto';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
export const ENV_PATH = path.join(os.homedir(), '.glm-claude-code.env');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Every variable glm knows how to manage, in the order they are written to the
|
|
18
|
+
* env file. `label` is what the user sees; `secret` values are masked on screen.
|
|
19
|
+
*/
|
|
20
|
+
export const FIELDS = [
|
|
21
|
+
{
|
|
22
|
+
key: 'ANTHROPIC_BASE_URL',
|
|
23
|
+
label: 'API endpoint',
|
|
24
|
+
describe: 'Anthropic-compatible server Claude Code talks to',
|
|
25
|
+
default: 'https://api.z.ai/api/anthropic',
|
|
26
|
+
required: true,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
key: 'ANTHROPIC_AUTH_TOKEN',
|
|
30
|
+
label: 'API key',
|
|
31
|
+
describe: 'Bearer token sent with every request',
|
|
32
|
+
default: '',
|
|
33
|
+
required: true,
|
|
34
|
+
secret: true,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
key: 'ANTHROPIC_DEFAULT_SONNET_MODEL',
|
|
38
|
+
label: 'Main model (Sonnet slot)',
|
|
39
|
+
describe: 'Model used for ordinary work',
|
|
40
|
+
default: 'glm-5.3[1m]',
|
|
41
|
+
required: true,
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
key: 'ANTHROPIC_DEFAULT_OPUS_MODEL',
|
|
45
|
+
label: 'Main model (Opus slot)',
|
|
46
|
+
describe: 'Model used when Claude Code asks for its strongest model',
|
|
47
|
+
default: 'glm-5.3[1m]',
|
|
48
|
+
required: true,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
key: 'ANTHROPIC_DEFAULT_HAIKU_MODEL',
|
|
52
|
+
label: 'Small model (Haiku slot)',
|
|
53
|
+
describe: 'Model used for cheap background tasks',
|
|
54
|
+
default: 'glm-5.3-flash[1m]',
|
|
55
|
+
required: true,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
key: 'API_TIMEOUT_MS',
|
|
59
|
+
label: 'Request timeout',
|
|
60
|
+
describe: 'Milliseconds Claude Code waits for a reply before giving up',
|
|
61
|
+
default: '3000000',
|
|
62
|
+
required: false,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
key: 'CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC',
|
|
66
|
+
label: 'Non-essential traffic',
|
|
67
|
+
describe: 'Set to 1 to switch off optional telemetry and update pings',
|
|
68
|
+
default: '1',
|
|
69
|
+
required: false,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
key: 'CLAUDE_CODE_AUTO_COMPACT_WINDOW',
|
|
73
|
+
label: 'Auto-compact window',
|
|
74
|
+
describe: 'Token count at which the conversation gets compacted',
|
|
75
|
+
default: '1048576',
|
|
76
|
+
required: false,
|
|
77
|
+
},
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
export const DEFAULTS = Object.fromEntries(FIELDS.map(f => [f.key, f.default]));
|
|
81
|
+
export const REQUIRED_KEYS = FIELDS.filter(f => f.required).map(f => f.key);
|
|
82
|
+
|
|
83
|
+
export function fieldFor (key) {
|
|
84
|
+
return FIELDS.find(f => f.key === key) || null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function load () {
|
|
88
|
+
if (!fs.existsSync(ENV_PATH)) return {};
|
|
89
|
+
// The file contains exactly the values exported at launch. An omitted
|
|
90
|
+
// variable (e.g. optional telemetry) must stay omitted on the next save.
|
|
91
|
+
const config = Object.fromEntries(FIELDS.map(({ key }) => [key, '']));
|
|
92
|
+
for (const line of fs.readFileSync(ENV_PATH, 'utf8').split(/\r?\n/)) {
|
|
93
|
+
const split = line.indexOf('=');
|
|
94
|
+
const key = line.slice(0, split);
|
|
95
|
+
if (split > 0 && Object.hasOwn(config, key)) config[key] = line.slice(split + 1);
|
|
96
|
+
}
|
|
97
|
+
return config;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Stored values with defaults filled in for anything the user has never set.
|
|
102
|
+
*
|
|
103
|
+
* A key stored as an empty string is not "unset" — it is an explicit "do not
|
|
104
|
+
* export this variable at all", so the default must not creep back in.
|
|
105
|
+
*/
|
|
106
|
+
export function effective (config = load()) {
|
|
107
|
+
const merged = { ...DEFAULTS };
|
|
108
|
+
for (const [key, value] of Object.entries(config)) {
|
|
109
|
+
if (value != null) merged[key] = String(value);
|
|
110
|
+
}
|
|
111
|
+
return merged;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function isComplete (config = load()) {
|
|
115
|
+
return REQUIRED_KEYS.every(key => {
|
|
116
|
+
const value = config[key] ?? DEFAULTS[key];
|
|
117
|
+
return value != null && String(value).trim() !== '';
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function missingKeys (config = load()) {
|
|
122
|
+
return REQUIRED_KEYS.filter(key => {
|
|
123
|
+
const value = config[key] ?? DEFAULTS[key];
|
|
124
|
+
return value == null || String(value).trim() === '';
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function save (config) {
|
|
129
|
+
const values = effective(config);
|
|
130
|
+
const lines = ['# glm configuration. Change these values with glm -c.'];
|
|
131
|
+
for (const { key } of FIELDS) {
|
|
132
|
+
const value = values[key];
|
|
133
|
+
if (/[\r\n\0]/.test(value)) throw new Error(`${key} must be a single line of text.`);
|
|
134
|
+
if (value.trim()) lines.push(`${key}=${value}`);
|
|
135
|
+
}
|
|
136
|
+
const temp = `${ENV_PATH}.${randomUUID()}.tmp`;
|
|
137
|
+
try {
|
|
138
|
+
fs.writeFileSync(temp, lines.join('\n') + '\n', { mode: 0o600, flag: 'wx' });
|
|
139
|
+
fs.renameSync(temp, ENV_PATH);
|
|
140
|
+
} finally {
|
|
141
|
+
fs.rmSync(temp, { force: true });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Show only the first and last few characters of a secret. */
|
|
146
|
+
export function maskSecret (value) {
|
|
147
|
+
if (!value) return '';
|
|
148
|
+
const s = String(value);
|
|
149
|
+
if (s.length <= 10) return `${s.slice(0, 2)}${'•'.repeat(Math.max(2, s.length - 2))}`;
|
|
150
|
+
return `${s.slice(0, 4)}${'•'.repeat(8)}${s.slice(-4)}`;
|
|
151
|
+
}
|