@openxiaobu/codexl 0.1.1 → 0.1.2

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.
@@ -51,6 +51,25 @@ function readAuthFile(codexHome) {
51
51
  }
52
52
  return JSON.parse(node_fs_1.default.readFileSync(authPath, "utf8"));
53
53
  }
54
+ /**
55
+ * 从 `id_token` 中解析邮箱。
56
+ *
57
+ * @param auth 认证文件对象。
58
+ * @returns 邮箱地址;缺失或解析失败时返回 `undefined`。
59
+ */
60
+ function resolveEmailFromAuth(auth) {
61
+ const idToken = auth?.tokens?.id_token;
62
+ if (!idToken) {
63
+ return undefined;
64
+ }
65
+ try {
66
+ const payload = JSON.parse(Buffer.from(idToken.split(".")[1] ?? "", "base64url").toString("utf8"));
67
+ return payload.email;
68
+ }
69
+ catch {
70
+ return undefined;
71
+ }
72
+ }
54
73
  /**
55
74
  * 将来源 HOME 下的官方 `.codex` 登录态复制到目标 HOME。
56
75
  *
@@ -92,23 +111,18 @@ function cloneCodexAuthState(sourceHome, targetHome) {
92
111
  *
93
112
  * 完整标准:
94
113
  * 1. 存在 `.codex/auth.json`
95
- * 2. 存在 `.codex/accounts/registry.json`
96
- * 3. 至少存在一个账户级 `*.auth.json`
114
+ * 2. `auth.json` 中存在 `access_token`
115
+ * 3. `auth.json` 中存在 `refresh_token`
116
+ * 4. `auth.json` 中存在 `account_id`
97
117
  *
98
118
  * @param codexHome 待检查的 HOME 目录。
99
119
  * @returns 为 `true` 表示登录态完整,可用于调度;否则为 `false`。
100
120
  */
101
121
  function hasCompleteCodexAuthState(codexHome) {
102
- const codexDir = getCodexDataDir(codexHome);
103
- const authPath = node_path_1.default.join(codexDir, "auth.json");
104
- const accountsDir = node_path_1.default.join(codexDir, "accounts");
105
- const registryPath = node_path_1.default.join(accountsDir, "registry.json");
106
- if (!node_fs_1.default.existsSync(authPath) || !node_fs_1.default.existsSync(registryPath) || !node_fs_1.default.existsSync(accountsDir)) {
107
- return false;
108
- }
109
- return node_fs_1.default
110
- .readdirSync(accountsDir, { withFileTypes: true })
111
- .some((entry) => entry.isFile() && entry.name.endsWith(".auth.json"));
122
+ const auth = readAuthFile(codexHome);
123
+ return Boolean(auth?.tokens?.access_token &&
124
+ auth?.tokens?.refresh_token &&
125
+ auth?.tokens?.account_id);
112
126
  }
113
127
  /**
114
128
  * 将最新认证信息回写到指定账号的 `auth.json`。
@@ -153,11 +167,12 @@ function registerManagedAccount(accountId, codexHome) {
153
167
  // 预先创建账号隔离目录,方便后续直接执行 codex login。
154
168
  node_fs_1.default.mkdirSync(home, { recursive: true });
155
169
  const primary = resolvePrimaryRegistryAccount(home);
170
+ const auth = readAuthFile(home);
156
171
  const account = {
157
172
  id: accountId,
158
173
  name: accountId,
159
174
  codex_home: home,
160
- email: primary?.email,
175
+ email: primary?.email ?? resolveEmailFromAuth(auth),
161
176
  enabled: true,
162
177
  imported_at: new Date().toISOString()
163
178
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openxiaobu/codexl",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "本地 Codex 多账号切换与状态管理工具",
5
5
  "type": "commonjs",
6
6
  "main": "dist/cli.js",