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.
@@ -1,69 +1,25 @@
1
- import { execFileSync } from "node:child_process";
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 a variable from the Windows User Environment via PowerShell (spec §10).
11
- * Returns undefined on any failure callers fall back or fail with their own error.
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
- assertEnvVarName(name);
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
- * Write a variable to the Windows User Environment (spec §11).
35
- * The value is passed through a child-process env var so it never needs
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
- assertEnvVarName(name);
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. Windows User Environment
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 ?? readWindowsUserEnv;
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: "windows-user-env" };
37
+ return { key: fromUserEnv.trim(), source: "user-store" };
82
38
  }
83
39
  return undefined;
84
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glm-coding-router",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "GLM Coding Plan workers for Claude Code and Codex",
5
5
  "type": "module",
6
6
  "license": "MIT",