dsh-bottom-info-bar 1.27.0 → 1.27.1
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/lib/index.js +88 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -900,6 +900,55 @@ function parseAppAuthCodexUsage(result, windowLabels) {
|
|
|
900
900
|
return { status: 'unavailable', reason: reason, plan: plan, planType: planType, windows: [] }
|
|
901
901
|
}
|
|
902
902
|
|
|
903
|
+
// /backend-api/wham/usage → 统一的 { provider, plan, planType, expiryAt, windows }。
|
|
904
|
+
// 这是 Codex 后端的额度接口:primary_window/secondary_window 带 used_percent、
|
|
905
|
+
// reset_at(Unix 秒)和 limit_window_seconds。SIWC 的 token 官方明确不接受,
|
|
906
|
+
// 所以只有本机存在 Codex 登录文件时才有数据。
|
|
907
|
+
function parseCodexWhamUsage(body, windowLabels) {
|
|
908
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) return null
|
|
909
|
+
const wl = windowLabels || WINDOW_LABELS
|
|
910
|
+
const windows = []
|
|
911
|
+
const seen = {}
|
|
912
|
+
const addWindow = function (win, id) {
|
|
913
|
+
if (!win || typeof win !== 'object') return
|
|
914
|
+
const percent = parsePercent(win.used_percent)
|
|
915
|
+
const key = appAuthWindowKey(id, win.limit_window_seconds)
|
|
916
|
+
if (percent == null || !key || seen[key]) return
|
|
917
|
+
seen[key] = true
|
|
918
|
+
windows.push({
|
|
919
|
+
key: key,
|
|
920
|
+
label: wl[key],
|
|
921
|
+
usedPercent: Math.round(percent),
|
|
922
|
+
resetsAt: normalizeResetAt(win.reset_at),
|
|
923
|
+
})
|
|
924
|
+
}
|
|
925
|
+
const rateLimit = body.rate_limit && typeof body.rate_limit === 'object' ? body.rate_limit : {}
|
|
926
|
+
addWindow(rateLimit.primary_window, 'primary')
|
|
927
|
+
addWindow(rateLimit.secondary_window, 'secondary')
|
|
928
|
+
// Business 计划没有 rate_limit,只有管理员设的月度上限。
|
|
929
|
+
const spend = body.spend_control && typeof body.spend_control === 'object' ? body.spend_control.individual_limit : null
|
|
930
|
+
if (windows.length === 0 && spend && typeof spend === 'object') {
|
|
931
|
+
let percent = parsePercent(spend.used_percent)
|
|
932
|
+
if (percent == null) {
|
|
933
|
+
const used = Number(spend.used)
|
|
934
|
+
const limit = Number(spend.limit)
|
|
935
|
+
if (isFinite(used) && isFinite(limit) && limit > 0) percent = (used / limit) * 100
|
|
936
|
+
}
|
|
937
|
+
if (percent != null) {
|
|
938
|
+
windows.push({ key: 'monthly', label: wl.monthly, usedPercent: Math.round(percent), resetsAt: normalizeResetAt(spend.reset_at) })
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
if (windows.length === 0) return null
|
|
942
|
+
const planType = typeof body.plan_type === 'string' && body.plan_type.length > 0 ? body.plan_type : null
|
|
943
|
+
return {
|
|
944
|
+
provider: 'codex',
|
|
945
|
+
plan: planType ? planDisplayName(planType) : null,
|
|
946
|
+
planType: planType,
|
|
947
|
+
expiryAt: null,
|
|
948
|
+
windows: windows,
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
|
|
903
952
|
// ---------- FR-9 / D8:小米 MiMo 解析(数值一律容错:字符串/数字、百分比 0-1 或 0-100 双形态) ----------
|
|
904
953
|
function xiaomiRegionBaseUrl(region) {
|
|
905
954
|
if (region === 'sgp') return 'https://token-plan-sgp.xiaomimimo.com'
|
|
@@ -2930,11 +2979,49 @@ export default {
|
|
|
2930
2979
|
}
|
|
2931
2980
|
}
|
|
2932
2981
|
|
|
2982
|
+
// Codex 后端额度通道:需要本机 ~/.codex/auth.json(Codex CLI / 旧版插件留下的登录)。
|
|
2983
|
+
// SIWC 的 token 被官方明确拒绝在 ChatGPT 用量端点之外,所以这是当前唯一能拿到
|
|
2984
|
+
// 5 小时 / 每周 / 月度窗口百分比的路径。文件不存在或过期时返回 null,退回套餐/有效期。
|
|
2985
|
+
async function fetchCodexUsageFromWham() {
|
|
2986
|
+
const read = readCodexAuthFile(CODEX_AUTH_FILE);
|
|
2987
|
+
if (!read.ok) return null;
|
|
2988
|
+
const tokens = read.auth && read.auth.tokens;
|
|
2989
|
+
const access = tokens && typeof tokens.access_token === 'string' && tokens.access_token.length > 0 ? tokens.access_token : null;
|
|
2990
|
+
if (!access) return null;
|
|
2991
|
+
// 账号一致性:两边的 id_token 都能读到邮箱且不一致时,不拿另一个账号的额度冒充。
|
|
2992
|
+
try {
|
|
2993
|
+
if (ctx.credentials && typeof ctx.credentials.readRecord === 'function') {
|
|
2994
|
+
const record = await ctx.credentials.readRecord('chatgpt-sub/session');
|
|
2995
|
+
const payload = record && record.kind === 'grant' ? record.payload : null;
|
|
2996
|
+
const nativeClaims = payload && typeof payload.idToken === 'string' ? decodeJwtPayload(payload.idToken) : null;
|
|
2997
|
+
const codexClaims = typeof tokens.id_token === 'string' ? decodeJwtPayload(tokens.id_token) : null;
|
|
2998
|
+
if (nativeClaims && codexClaims && typeof nativeClaims.email === 'string' && typeof codexClaims.email === 'string'
|
|
2999
|
+
&& nativeClaims.email.toLowerCase() !== codexClaims.email.toLowerCase()) {
|
|
3000
|
+
return null;
|
|
3001
|
+
}
|
|
3002
|
+
}
|
|
3003
|
+
} catch (err) { /* 读不到会话就按纯 Codex 账号处理 */ }
|
|
3004
|
+
const headers = { Authorization: 'Bearer ' + access, Accept: 'application/json' };
|
|
3005
|
+
if (typeof tokens.account_id === 'string' && tokens.account_id.length > 0) headers['ChatGPT-Account-Id'] = tokens.account_id;
|
|
3006
|
+
try {
|
|
3007
|
+
const res = await fetch('https://chatgpt.com/backend-api/wham/usage', { headers: headers, signal: timeoutSignal(HTTP_TIMEOUT_MS) });
|
|
3008
|
+
if (!res.ok) return null;
|
|
3009
|
+
const body = await res.json();
|
|
3010
|
+
const data = parseCodexWhamUsage(body, windowLabels);
|
|
3011
|
+
return data ? { data: data } : null;
|
|
3012
|
+
} catch (err) {
|
|
3013
|
+
return null;
|
|
3014
|
+
}
|
|
3015
|
+
}
|
|
3016
|
+
|
|
2933
3017
|
async function fetchCodexUsage() {
|
|
2934
3018
|
// ① appAuth 优先:拿到额度窗口就用它(DSH Go 的唯一可用通道)
|
|
2935
3019
|
const viaAppAuth = await fetchCodexUsageViaAppAuth();
|
|
2936
3020
|
if (viaAppAuth && viaAppAuth.data) return viaAppAuth;
|
|
2937
|
-
// ②
|
|
3021
|
+
// ② Codex 后端额度:桌面端唯一能拿到窗口百分比的通道
|
|
3022
|
+
const viaWham = await fetchCodexUsageFromWham();
|
|
3023
|
+
if (viaWham && viaWham.data) return viaWham;
|
|
3024
|
+
// ③ 新版 dsh-chatgpt-sub 的 SIWC 会话(没有 Codex 文件也能显示套餐与有效期)
|
|
2938
3025
|
const fromSession = await fetchCodexUsageFromSession();
|
|
2939
3026
|
if (fromSession && fromSession.data) return fromSession;
|
|
2940
3027
|
// ③ 旧版桌面文件通道:取到数据就用文件的数据,不被 appAuth 的失败状态遮住
|