glm-coding-router 1.0.0 → 1.1.0
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 +419 -415
- package/dist/bin/glm-worker.js +37 -4
- package/dist/commands/doctor.js +21 -5
- package/dist/commands/init.js +5 -3
- package/dist/commands/key.js +46 -9
- package/dist/commands/uninstall.js +2 -1
- package/dist/core/config.js +31 -2
- package/dist/core/errors.js +33 -10
- package/dist/core/platform.js +37 -8
- package/dist/core/profile.js +6 -1
- package/dist/core/user-env.js +181 -0
- package/dist/core/zai-key.js +15 -59
- package/package.json +1 -1
package/dist/core/zai-key.js
CHANGED
|
@@ -1,69 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { deleteUserEnv, detectUserEnvStore, readUserEnv as readUserEnvStore, writeUserEnv, } from "./user-env.js";
|
|
2
2
|
export const ZAI_API_KEY_ENV = "ZAI_API_KEY";
|
|
3
|
-
/** Only well-formed variable names may reach powershell.exe (spec §38). */
|
|
4
|
-
function assertEnvVarName(name) {
|
|
5
|
-
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
|
|
6
|
-
throw new Error(`Invalid environment variable name: ${name}`);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
3
|
/**
|
|
10
|
-
* Read
|
|
11
|
-
*
|
|
4
|
+
* Read the variable from this platform's per-user store (spec §10).
|
|
5
|
+
* The Windows implementation is unchanged; it now lives in user-env.ts.
|
|
12
6
|
*/
|
|
13
|
-
export function readWindowsUserEnv(name) {
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
const result = execFileSync("powershell.exe", [
|
|
17
|
-
"-NoProfile",
|
|
18
|
-
"-NonInteractive",
|
|
19
|
-
"-Command",
|
|
20
|
-
`[Environment]::GetEnvironmentVariable('${name}','User')`,
|
|
21
|
-
], {
|
|
22
|
-
encoding: "utf8",
|
|
23
|
-
windowsHide: true,
|
|
24
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
25
|
-
});
|
|
26
|
-
const value = result.trim();
|
|
27
|
-
return value || undefined;
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
7
|
+
export function readWindowsUserEnv(name, deps = {}) {
|
|
8
|
+
return readUserEnvStore(name, deps);
|
|
32
9
|
}
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
* PowerShell string escaping (spec §38: no unescaped user data in commands).
|
|
37
|
-
*/
|
|
38
|
-
export function setWindowsUserEnv(name, value) {
|
|
39
|
-
assertEnvVarName(name);
|
|
40
|
-
execFileSync("powershell.exe", [
|
|
41
|
-
"-NoProfile",
|
|
42
|
-
"-NonInteractive",
|
|
43
|
-
"-Command",
|
|
44
|
-
`[Environment]::SetEnvironmentVariable('${name}', $env:GLM_ROUTER_VALUE, 'User')`,
|
|
45
|
-
], {
|
|
46
|
-
windowsHide: true,
|
|
47
|
-
stdio: ["ignore", "ignore", "pipe"],
|
|
48
|
-
env: { ...process.env, GLM_ROUTER_VALUE: value },
|
|
49
|
-
});
|
|
10
|
+
/** Write the variable to this platform's per-user store (spec §11). */
|
|
11
|
+
export function setWindowsUserEnv(name, value, deps = {}) {
|
|
12
|
+
writeUserEnv(name, value, deps);
|
|
50
13
|
}
|
|
51
|
-
export function deleteWindowsUserEnv(name) {
|
|
52
|
-
|
|
53
|
-
execFileSync("powershell.exe", [
|
|
54
|
-
"-NoProfile",
|
|
55
|
-
"-NonInteractive",
|
|
56
|
-
"-Command",
|
|
57
|
-
`[Environment]::SetEnvironmentVariable('${name}', $null, 'User')`,
|
|
58
|
-
], {
|
|
59
|
-
windowsHide: true,
|
|
60
|
-
stdio: ["ignore", "ignore", "pipe"],
|
|
61
|
-
});
|
|
14
|
+
export function deleteWindowsUserEnv(name, deps = {}) {
|
|
15
|
+
deleteUserEnv(name, deps);
|
|
62
16
|
}
|
|
17
|
+
/** Re-exported so callers do not need two imports. */
|
|
18
|
+
export { detectUserEnvStore };
|
|
63
19
|
/**
|
|
64
20
|
* Resolve the Z.ai key with the mandatory fallback order (spec §10):
|
|
65
21
|
* 1. process.env.ZAI_API_KEY
|
|
66
|
-
* 2.
|
|
22
|
+
* 2. this platform's per-user store
|
|
67
23
|
* 3. fail (undefined)
|
|
68
24
|
*
|
|
69
25
|
* The fallback exists because Orca terminals snapshot a stale environment and
|
|
@@ -71,14 +27,14 @@ export function deleteWindowsUserEnv(name) {
|
|
|
71
27
|
*/
|
|
72
28
|
export function resolveZaiApiKey(options = {}) {
|
|
73
29
|
const env = options.env ?? process.env;
|
|
74
|
-
const readUserEnv = options.readUserEnv ??
|
|
30
|
+
const readUserEnv = options.readUserEnv ?? ((name) => readUserEnvStore(name));
|
|
75
31
|
const fromProcess = env[ZAI_API_KEY_ENV];
|
|
76
32
|
if (fromProcess && fromProcess.trim()) {
|
|
77
33
|
return { key: fromProcess.trim(), source: "process-env" };
|
|
78
34
|
}
|
|
79
35
|
const fromUserEnv = readUserEnv(ZAI_API_KEY_ENV);
|
|
80
36
|
if (fromUserEnv && fromUserEnv.trim()) {
|
|
81
|
-
return { key: fromUserEnv.trim(), source: "
|
|
37
|
+
return { key: fromUserEnv.trim(), source: "user-store" };
|
|
82
38
|
}
|
|
83
39
|
return undefined;
|
|
84
40
|
}
|