codex-id 0.2.2 → 0.2.4
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 +32 -53
- package/bin/cid +8 -14
- package/package.json +1 -1
- package/src/accounts.js +49 -19
- package/src/auth.js +20 -15
- package/src/codex.js +11 -63
- package/src/commands.js +132 -182
- package/src/paths.js +11 -9
- package/src/proxy.js +66 -0
- package/src/refresh.js +6 -7
- package/src/terminal.js +0 -25
- package/src/usage.js +33 -73
package/src/terminal.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
const readline = require('node:readline/promises');
|
|
2
|
-
|
|
3
1
|
const INDEX_WIDTH = 4;
|
|
4
2
|
const PLAN_WIDTH = 8;
|
|
5
3
|
const USAGE_WIDTH = 14;
|
|
6
|
-
const STATUS_WIDTH = 10;
|
|
7
4
|
const PROGRESS_INTERVAL_MS = 120;
|
|
8
5
|
const PROGRESS_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
|
9
6
|
|
|
@@ -61,26 +58,6 @@ const formatListIndex = (index, isCurrent) => {
|
|
|
61
58
|
return isCurrent ? `${index}*` : String(index);
|
|
62
59
|
};
|
|
63
60
|
|
|
64
|
-
// 删除离线账号必须由交互终端确认,避免脚本环境误删。
|
|
65
|
-
const confirmRemoval = async (
|
|
66
|
-
account,
|
|
67
|
-
input = process.stdin,
|
|
68
|
-
output = process.stdout,
|
|
69
|
-
) => {
|
|
70
|
-
if (!input.isTTY) throw new Error('删除离线账号需要交互式终端确认');
|
|
71
|
-
output.write(`检测到离线账号:${account}\n`);
|
|
72
|
-
const rl = readline.createInterface({ input, output });
|
|
73
|
-
const answer = await rl.question('是否删除?1:确定 2:取消\n');
|
|
74
|
-
rl.close();
|
|
75
|
-
if (answer === '1') return true;
|
|
76
|
-
if (answer === '2') {
|
|
77
|
-
output.write(`已取消删除离线账号:${account}\n`);
|
|
78
|
-
} else {
|
|
79
|
-
output.write(`输入无效,已取消删除离线账号:${account}\n`);
|
|
80
|
-
}
|
|
81
|
-
return false;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
61
|
const printRow = (
|
|
85
62
|
row,
|
|
86
63
|
{ isCurrent = false, color = '' } = {},
|
|
@@ -141,11 +118,9 @@ const withProgress = async (
|
|
|
141
118
|
module.exports = {
|
|
142
119
|
INDEX_WIDTH,
|
|
143
120
|
PLAN_WIDTH,
|
|
144
|
-
STATUS_WIDTH,
|
|
145
121
|
USAGE_WIDTH,
|
|
146
122
|
bold,
|
|
147
123
|
colorize,
|
|
148
|
-
confirmRemoval,
|
|
149
124
|
formatListIndex,
|
|
150
125
|
formatPlanType,
|
|
151
126
|
formatResetTime,
|
package/src/usage.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
const path = require('node:path');
|
|
2
|
-
const { readAuth } = require('./auth');
|
|
3
|
-
|
|
4
1
|
const USAGE_ENDPOINT = 'https://chatgpt.com/backend-api/wham/usage';
|
|
5
2
|
const REQUEST_TIMEOUT_MS = 8000;
|
|
6
3
|
const USAGE_5H_WINDOW_SECONDS = 18000;
|
|
@@ -11,30 +8,26 @@ const ACCOUNT_USAGE_STATES = Object.freeze({
|
|
|
11
8
|
LIMITED: 'limited',
|
|
12
9
|
COOLING: 'cooling',
|
|
13
10
|
OFFLINE: 'offline',
|
|
11
|
+
UNKNOWN: 'unknown',
|
|
14
12
|
});
|
|
15
13
|
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
const auth = typeof homeDir === 'string' ? readAuth(homeDir) : homeDir && homeDir.auth;
|
|
21
|
-
if (auth && typeof auth.personal_access_token === 'string') {
|
|
22
|
-
return {
|
|
23
|
-
accessToken: auth.personal_access_token,
|
|
24
|
-
accountId: '',
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
const tokens = auth && auth.tokens;
|
|
28
|
-
if (!tokens || typeof tokens.access_token !== 'string' || typeof tokens.account_id !== 'string') {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
14
|
+
// 从账号 auth 中提取调用 Usage 接口所需的最小凭据。
|
|
15
|
+
const extractAuthFields = (account) => {
|
|
16
|
+
const auth = account && account.auth;
|
|
17
|
+
if (auth && typeof auth.personal_access_token === 'string') {
|
|
31
18
|
return {
|
|
32
|
-
accessToken:
|
|
33
|
-
accountId:
|
|
19
|
+
accessToken: auth.personal_access_token,
|
|
20
|
+
accountId: '',
|
|
34
21
|
};
|
|
35
|
-
}
|
|
22
|
+
}
|
|
23
|
+
const tokens = auth && auth.tokens;
|
|
24
|
+
if (!tokens || typeof tokens.access_token !== 'string' || typeof tokens.account_id !== 'string') {
|
|
36
25
|
return null;
|
|
37
26
|
}
|
|
27
|
+
return {
|
|
28
|
+
accessToken: tokens.access_token,
|
|
29
|
+
accountId: tokens.account_id,
|
|
30
|
+
};
|
|
38
31
|
};
|
|
39
32
|
|
|
40
33
|
const normalizePercent = (value) => {
|
|
@@ -52,8 +45,6 @@ const hasUsageFields = (item) => {
|
|
|
52
45
|
item.plan
|
|
53
46
|
|| isValidPercent(item.next_used)
|
|
54
47
|
|| isValidPercent(item.total_used)
|
|
55
|
-
|| isValidPercent(item.h5_used)
|
|
56
|
-
|| isValidPercent(item.d7_used)
|
|
57
48
|
));
|
|
58
49
|
};
|
|
59
50
|
|
|
@@ -64,12 +55,12 @@ const buildUsageHeaders = (token, accountId = '') => {
|
|
|
64
55
|
};
|
|
65
56
|
|
|
66
57
|
// 将账号条目转换成可并发查询 Usage 的请求条目。
|
|
67
|
-
const buildUsageEntries = (
|
|
68
|
-
return
|
|
58
|
+
const buildUsageEntries = (accounts) => {
|
|
59
|
+
return accounts.flatMap((account) => {
|
|
69
60
|
const fields = extractAuthFields(account);
|
|
70
61
|
if (!fields) return [];
|
|
71
62
|
return [{
|
|
72
|
-
key:
|
|
63
|
+
key: account.key,
|
|
73
64
|
token: fields.accessToken,
|
|
74
65
|
accountId: fields.accountId,
|
|
75
66
|
}];
|
|
@@ -99,50 +90,21 @@ const fetchUsageOne = async (entry, fetchImpl = fetch) => {
|
|
|
99
90
|
const windows = [rateLimit.primary_window, rateLimit.secondary_window].filter(Boolean);
|
|
100
91
|
const next = findWindow(windows, USAGE_5H_WINDOW_SECONDS);
|
|
101
92
|
const total = findTotalWindow(windows);
|
|
102
|
-
const nextUsed = normalizePercent(next.used_percent);
|
|
103
|
-
const totalUsed = normalizePercent(total.used_percent);
|
|
104
93
|
return {
|
|
105
94
|
key: entry.key,
|
|
106
95
|
email: data.email || '',
|
|
107
96
|
plan: data.plan_type || '',
|
|
108
|
-
next_used:
|
|
97
|
+
next_used: normalizePercent(next.used_percent),
|
|
109
98
|
next_reset: next.reset_at ?? null,
|
|
110
|
-
|
|
111
|
-
total_used: totalUsed,
|
|
99
|
+
total_used: normalizePercent(total.used_percent),
|
|
112
100
|
total_reset: total.reset_at ?? null,
|
|
113
|
-
total_seconds: total.limit_window_seconds ?? null,
|
|
114
|
-
h5_used: nextUsed,
|
|
115
|
-
h5_reset: next.reset_at ?? null,
|
|
116
|
-
d7_used: totalUsed,
|
|
117
|
-
d7_reset: total.reset_at ?? null,
|
|
118
101
|
};
|
|
119
102
|
} catch (error) {
|
|
120
|
-
return { key: entry.key, error: error.
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
// 用 Usage 接口探活账号,避免依赖本地 codex 命令或模型列表输出。
|
|
125
|
-
const probeAccountStatus = async (homeDir, fetchImpl = fetch) => {
|
|
126
|
-
const fields = extractAuthFields(homeDir);
|
|
127
|
-
if (!fields) return 'offline';
|
|
128
|
-
if (typeof fetchImpl !== 'function' || typeof AbortSignal.timeout !== 'function') return 'unknown';
|
|
129
|
-
try {
|
|
130
|
-
const response = await fetchImpl(USAGE_ENDPOINT, {
|
|
131
|
-
headers: buildUsageHeaders(fields.accessToken, fields.accountId),
|
|
132
|
-
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
133
|
-
});
|
|
134
|
-
if (response.ok) return 'online';
|
|
135
|
-
if (response.status === 401 || response.status === 403) return 'offline';
|
|
136
|
-
return 'unknown';
|
|
137
|
-
} catch (_) {
|
|
138
|
-
return 'unknown';
|
|
103
|
+
return { key: entry.key, error: (error.cause && error.cause.code) || error.name };
|
|
139
104
|
}
|
|
140
105
|
};
|
|
141
106
|
|
|
142
107
|
const fetchPersonalAccessTokenProfile = async (token, fetchImpl = fetch) => {
|
|
143
|
-
if (typeof fetchImpl !== 'function' || typeof AbortSignal.timeout !== 'function') {
|
|
144
|
-
throw new Error('当前 Node 运行时不支持访问令牌验证');
|
|
145
|
-
}
|
|
146
108
|
const response = await fetchImpl(USAGE_ENDPOINT, {
|
|
147
109
|
headers: buildUsageHeaders(token),
|
|
148
110
|
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
@@ -155,9 +117,8 @@ const fetchPersonalAccessTokenProfile = async (token, fetchImpl = fetch) => {
|
|
|
155
117
|
};
|
|
156
118
|
};
|
|
157
119
|
|
|
158
|
-
const loadUsage = async (
|
|
159
|
-
|
|
160
|
-
const entries = buildUsageEntries(accountDirs);
|
|
120
|
+
const loadUsage = async (accounts, fetchImpl = fetch) => {
|
|
121
|
+
const entries = buildUsageEntries(accounts);
|
|
161
122
|
if (entries.length === 0) return [];
|
|
162
123
|
return Promise.all(entries.map((entry) => fetchUsageOne(entry, fetchImpl)));
|
|
163
124
|
};
|
|
@@ -170,10 +131,6 @@ const usageFor = (results, key) => {
|
|
|
170
131
|
return results.find((item) => item.key === key) || null;
|
|
171
132
|
};
|
|
172
133
|
|
|
173
|
-
const usageAvailableForAccount = (results, key) => {
|
|
174
|
-
return hasUsageFields(usageFor(results, key));
|
|
175
|
-
};
|
|
176
|
-
|
|
177
134
|
const classifyPrimaryWindow = (usedPercent) => {
|
|
178
135
|
if (!isValidPercent(usedPercent)) return ACCOUNT_USAGE_STATES.COOLING;
|
|
179
136
|
if (usedPercent >= QUOTA_COOLING_PERCENT) return ACCOUNT_USAGE_STATES.COOLING;
|
|
@@ -185,14 +142,18 @@ const classifyPrimaryWindow = (usedPercent) => {
|
|
|
185
142
|
|
|
186
143
|
// 统一账号额度状态,供自动切换和后续展示逻辑复用。
|
|
187
144
|
const getAccountUsageState = (item) => {
|
|
188
|
-
if (!
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
145
|
+
if (!item || (!isValidPercent(item.next_used) && !isValidPercent(item.total_used))) {
|
|
146
|
+
// 只有凭据缺失或被拒才算离线;网络、服务异常和缺额度数据不能当作账号不可用。
|
|
147
|
+
const rejected = !item || item.error === 401 || item.error === 403;
|
|
148
|
+
return rejected ? ACCOUNT_USAGE_STATES.OFFLINE : ACCOUNT_USAGE_STATES.UNKNOWN;
|
|
149
|
+
}
|
|
150
|
+
// 免费账号和只有总周期限额的账号没有 Next 窗口,只按 Total 判断。
|
|
151
|
+
if (item.plan === 'free' || !isValidPercent(item.next_used)) return classifyPrimaryWindow(item.total_used);
|
|
152
|
+
// 只有 Next 窗口的账号(如部分 team 访问令牌账号)只按 Next 判断。
|
|
153
|
+
if (isValidPercent(item.total_used) && item.total_used >= QUOTA_COOLING_PERCENT) {
|
|
193
154
|
return ACCOUNT_USAGE_STATES.COOLING;
|
|
194
155
|
}
|
|
195
|
-
return classifyPrimaryWindow(
|
|
156
|
+
return classifyPrimaryWindow(item.next_used);
|
|
196
157
|
};
|
|
197
158
|
|
|
198
159
|
const accountShouldAutoSwitch = (results, key) => {
|
|
@@ -214,8 +175,7 @@ module.exports = {
|
|
|
214
175
|
fetchPersonalAccessTokenProfile,
|
|
215
176
|
getAccountUsageState,
|
|
216
177
|
hasUsageData,
|
|
178
|
+
hasUsageFields,
|
|
217
179
|
loadUsage,
|
|
218
|
-
probeAccountStatus,
|
|
219
|
-
usageAvailableForAccount,
|
|
220
180
|
usageFor,
|
|
221
181
|
};
|