opencode-usage 0.5.10 → 0.5.11
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/dist/index.js +54 -3
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -366,6 +366,56 @@ function getAccountIdFromJwt(claims) {
|
|
|
366
366
|
const auth = claims?.["https://api.openai.com/auth"];
|
|
367
367
|
return auth?.chatgpt_account_id ?? null;
|
|
368
368
|
}
|
|
369
|
+
async function tryReadCodexCliAuth() {
|
|
370
|
+
try {
|
|
371
|
+
const raw = JSON.parse(await Bun.file(CODEX_CLI_AUTH_PATH).text());
|
|
372
|
+
const tokens = raw.tokens;
|
|
373
|
+
if (!tokens?.access_token)
|
|
374
|
+
return null;
|
|
375
|
+
const accessClaims = decodeJwtPayload(tokens.access_token);
|
|
376
|
+
const idClaims = tokens.id_token ? decodeJwtPayload(tokens.id_token) : null;
|
|
377
|
+
const expiresAt = getExpiryFromJwt(accessClaims) ?? getExpiryFromJwt(idClaims) ?? 0;
|
|
378
|
+
if (expiresAt <= Date.now())
|
|
379
|
+
return null;
|
|
380
|
+
return {
|
|
381
|
+
accessToken: tokens.access_token,
|
|
382
|
+
refreshToken: tokens.refresh_token ?? "",
|
|
383
|
+
idToken: tokens.id_token ?? null,
|
|
384
|
+
accountId: getAccountIdFromJwt(idClaims) ?? getAccountIdFromJwt(accessClaims),
|
|
385
|
+
expiresAt
|
|
386
|
+
};
|
|
387
|
+
} catch {
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
async function tryImportFromCodexCli(alias, account, storePath) {
|
|
392
|
+
const cli = await tryReadCodexCliAuth();
|
|
393
|
+
if (!cli || !cli.refreshToken)
|
|
394
|
+
return false;
|
|
395
|
+
const pluginAccountId = account.accountId;
|
|
396
|
+
if (!pluginAccountId || cli.accountId !== pluginAccountId)
|
|
397
|
+
return false;
|
|
398
|
+
const pluginExpiry = typeof account.expiresAt === "number" ? account.expiresAt : 0;
|
|
399
|
+
if (cli.expiresAt <= pluginExpiry)
|
|
400
|
+
return false;
|
|
401
|
+
console.log(`[proactiveRefresh] ${alias}: importing fresher token from ~/.codex/auth.json`);
|
|
402
|
+
const freshStore = JSON.parse(await Bun.file(storePath).text());
|
|
403
|
+
const freshAccounts = freshStore.accounts ?? {};
|
|
404
|
+
const freshAcct = freshAccounts[alias];
|
|
405
|
+
if (!freshAcct)
|
|
406
|
+
return false;
|
|
407
|
+
freshAcct.accessToken = cli.accessToken;
|
|
408
|
+
freshAcct.refreshToken = cli.refreshToken;
|
|
409
|
+
if (cli.idToken)
|
|
410
|
+
freshAcct.idToken = cli.idToken;
|
|
411
|
+
freshAcct.expiresAt = cli.expiresAt;
|
|
412
|
+
freshAcct.lastRefresh = new Date().toISOString();
|
|
413
|
+
freshAcct.authInvalid = false;
|
|
414
|
+
await Bun.write(storePath, JSON.stringify(freshStore, null, 2));
|
|
415
|
+
const daysLeft = ((cli.expiresAt - Date.now()) / (24 * 60 * 60 * 1000)).toFixed(1);
|
|
416
|
+
console.log(`[proactiveRefresh] ${alias}: imported from CLI, expiry in ${daysLeft}d`);
|
|
417
|
+
return true;
|
|
418
|
+
}
|
|
369
419
|
async function refreshSingleCodexToken(alias, account, storePath) {
|
|
370
420
|
const refreshToken = account.refreshToken;
|
|
371
421
|
if (typeof refreshToken !== "string" || !refreshToken) {
|
|
@@ -385,7 +435,7 @@ async function refreshSingleCodexToken(alias, account, storePath) {
|
|
|
385
435
|
});
|
|
386
436
|
if (!res.ok) {
|
|
387
437
|
console.log(`[proactiveRefresh] ${alias}: refresh failed HTTP ${res.status} in ${Date.now() - t0}ms`);
|
|
388
|
-
return
|
|
438
|
+
return tryImportFromCodexCli(alias, account, storePath);
|
|
389
439
|
}
|
|
390
440
|
const tokens = await res.json();
|
|
391
441
|
const accessClaims = decodeJwtPayload(tokens.access_token);
|
|
@@ -633,7 +683,7 @@ function reauthCliCommand(provider) {
|
|
|
633
683
|
throw new Error(`Re-auth not supported for provider: ${provider}`);
|
|
634
684
|
return cmd;
|
|
635
685
|
}
|
|
636
|
-
var isBun4, PROVIDER_SOURCE, CODEX_TOKEN_URL = "https://auth.openai.com/oauth/token", CODEX_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann", REFRESH_COOLDOWN_MS, _proactiveRefreshRunning = false, bunxQueue, REAUTH_PROVIDERS;
|
|
686
|
+
var isBun4, PROVIDER_SOURCE, CODEX_TOKEN_URL = "https://auth.openai.com/oauth/token", CODEX_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann", REFRESH_COOLDOWN_MS, CODEX_CLI_AUTH_PATH, _proactiveRefreshRunning = false, bunxQueue, REAUTH_PROVIDERS;
|
|
637
687
|
var init_plugin_adapters = __esm(() => {
|
|
638
688
|
init_command_runner();
|
|
639
689
|
init_config_service();
|
|
@@ -735,6 +785,7 @@ var init_plugin_adapters = __esm(() => {
|
|
|
735
785
|
}
|
|
736
786
|
});
|
|
737
787
|
REFRESH_COOLDOWN_MS = 24 * 60 * 60 * 1000;
|
|
788
|
+
CODEX_CLI_AUTH_PATH = join8(homedir6(), ".codex", "auth.json");
|
|
738
789
|
bunxQueue = new Map;
|
|
739
790
|
registerCommand({
|
|
740
791
|
id: "accounts.ping",
|
|
@@ -31172,4 +31223,4 @@ async function main2() {
|
|
|
31172
31223
|
var WATCH_INTERVAL_MS = 5 * 60 * 1000;
|
|
31173
31224
|
main2().catch(console.error);
|
|
31174
31225
|
|
|
31175
|
-
//# debugId=
|
|
31226
|
+
//# debugId=F9964D445D944A6564756E2164756E21
|