@yugimob/pi-opencode-go-rotation 1.7.2 → 1.7.3
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 +8 -5
- package/dist/index.js +59 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,7 +24,9 @@ The first key added becomes active immediately.
|
|
|
24
24
|
|
|
25
25
|
If `OPENCODE_API_KEY` is set in the environment and no keys are configured, the extension recognizes it as an unmanaged `env` key: it reports that key's usage and keeps the footer working, but never writes it to the config file. Pi already uses that variable for its own requests, so nothing changes about how the model is called. `/opencode add` stores a managed copy that participates in rotation.
|
|
26
26
|
|
|
27
|
-
The extension never imports keys from `auth.json` automatically. When no keys are configured,
|
|
27
|
+
The extension never imports keys from `auth.json` automatically. When no keys are configured, it observes the credential pi resolves from `auth.json` as an unmanaged `auth` key, so status, usage, and the footer work without writing it to the config file. It does not participate in rotation until imported.
|
|
28
|
+
|
|
29
|
+
`/opencode` offers to import the `auth.json` key once. Declining is remembered, and the observed `auth` key keeps working. Run `/opencode import` to import it later without the prompt.
|
|
28
30
|
|
|
29
31
|
## Interactive key manager
|
|
30
32
|
|
|
@@ -42,7 +44,7 @@ Changes are saved as soon as they are confirmed. Outside interactive mode the co
|
|
|
42
44
|
|
|
43
45
|
## How it works
|
|
44
46
|
|
|
45
|
-
The extension sets the active key as a runtime override, which takes priority over `OPENCODE_API_KEY` environment variables and `auth.json` credentials. When no keys are configured, the environment key
|
|
47
|
+
The extension sets the active key as a runtime override, which takes priority over `OPENCODE_API_KEY` environment variables and `auth.json` credentials. When no keys are configured, the environment key and an `auth.json` credential are only observed (as `env` and `auth`) and never overridden or persisted.
|
|
46
48
|
|
|
47
49
|
Automatic rotation is governed by `rotationPolicy`:
|
|
48
50
|
|
|
@@ -79,18 +81,19 @@ personal: wk 40% · mo 55% | ↻ Nov 12
|
|
|
79
81
|
```
|
|
80
82
|
The rolling 5-hour window is omitted from the footer.
|
|
81
83
|
|
|
82
|
-
It refreshes on session start, model changes, turns, key rotations, manual key commands, and every 5 minutes. Under `quota-only` and `full`, a window at 100% or rate-limited blocks the active key until the window's reset and the next request moves to another key; under `off` the footer reports the window without changing keys. The
|
|
84
|
+
It refreshes on session start, model changes, turns, key rotations, manual key commands, and every 5 minutes. Under `quota-only` and `full`, a window at 100% or rate-limited blocks the active key until the window's reset and the next request moves to another key; under `off` the footer reports the window without changing keys. The key name — or `env`/`auth` for an unmanaged key — leads the line, and switching to another provider clears it.
|
|
83
85
|
|
|
84
86
|
## Commands
|
|
85
87
|
|
|
86
88
|
| Command | Description |
|
|
87
89
|
|---------|-------------|
|
|
88
90
|
| `/opencode` or `/opencode config` | Open the interactive key manager |
|
|
89
|
-
| `/opencode status` | Show all keys, active key marker, cooldown and quota-blocked status, or the `env` key when
|
|
91
|
+
| `/opencode status` | Show all keys, active key marker, cooldown and quota-blocked status, or the unmanaged `env`/`auth` key when no managed keys are configured |
|
|
90
92
|
| `/opencode usage` or `/opencode quota` | Fetch OpenCode Go usage for the active key without showing key material |
|
|
91
93
|
| `/opencode use <n>` | Switch to key number `n` (1-based) and clear its cooldown and quota block |
|
|
92
94
|
| `/opencode next` | Advance to the next configured key and clear its cooldown and quota block before activating it |
|
|
93
95
|
| `/opencode add <name> <key>` | Add a new key |
|
|
96
|
+
| `/opencode import` | Import the `auth.json` key into the config even after a declined prompt |
|
|
94
97
|
| `/opencode rm <n>` | Remove key number `n` |
|
|
95
98
|
| `/opencode reset` | Clear all cooldowns and quota blocks |
|
|
96
99
|
| `/opencode cooldown <min>` | Set or view cooldown duration in minutes |
|
|
@@ -146,7 +149,7 @@ Set `maxRetries` to at least the number of keys so all keys get a chance before
|
|
|
146
149
|
- Recorded blocks are never re-probed before their stored reset, so there is no plan-top-up recovery; use `/opencode reset` or `/opencode use <n>` to clear a block early.
|
|
147
150
|
- Go plan usage limits are tied to the subscription workspace; multiple keys from one workspace should not be assumed to provide independent quota. See the [OpenCode Go documentation](https://opencode.ai/docs/go/).
|
|
148
151
|
- Keys added via `/opencode add` are stored in plaintext. The config file is created and maintained with `0600` permissions.
|
|
149
|
-
- An `
|
|
152
|
+
- An unmanaged `env` or `auth` key is never stored, so it cannot rotate and is not remembered as a managed key; `/opencode add` stores a managed copy, and `/opencode import` stores the `auth.json` credential. A stored `auth.json` credential takes precedence over the environment variable, and the extension follows the credential pi resolves.
|
|
150
153
|
|
|
151
154
|
## License
|
|
152
155
|
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,8 @@ import { formatDuration, formatResetIn } from "./format-time.js";
|
|
|
4
4
|
export { formatResetIn };
|
|
5
5
|
const PROVIDER = "opencode-go";
|
|
6
6
|
const OPENCODE_KEY_ENV = "OPENCODE_API_KEY";
|
|
7
|
-
const
|
|
7
|
+
const AMBIENT_ENV_KEY_NAME = "env";
|
|
8
|
+
const AMBIENT_AUTH_KEY_NAME = "auth";
|
|
8
9
|
const OPENCODE_GO_USAGE_URL = "https://opencode.ai/zen/go/v1/usage";
|
|
9
10
|
const OPENCODE_GO_USAGE_TIMEOUT_MS = 10_000;
|
|
10
11
|
const FIXED_WINDOW_QUOTA_RE = /\b(?:5[- ]hour|weekly|monthly)\b[\s\S]*\b(?:usage\s+)?(?:quota|limit)\b|\b(?:usage|plan)\s+allocated\s+quota\s+exceeded\b|\b(?:quota|limit)\b[\s\S]*\b(?:will\s+reset|resets?\s+at|fixed[- ]window)\b/i;
|
|
@@ -613,13 +614,15 @@ function usageWindowPercent(window) {
|
|
|
613
614
|
return (window.used / window.limit) * 100;
|
|
614
615
|
return undefined;
|
|
615
616
|
}
|
|
616
|
-
function formatStatus(config, now = Date.now(),
|
|
617
|
+
function formatStatus(config, now = Date.now(), ambientKeyName) {
|
|
617
618
|
const watchdogStatus = `Watchdog: ${config.watchdogEnabled ? "on" : "off"} (${Math.ceil(getWatchdogIdleMs(config) / 1000)}s idle)`;
|
|
618
619
|
const policyStatus = `Rotation: ${config.rotationPolicy} (candidate verification ${config.verifyCandidates ? "on" : "off"})`;
|
|
619
620
|
if (config.keys.length === 0) {
|
|
620
|
-
const keyStatus =
|
|
621
|
-
? `Using ${
|
|
622
|
-
:
|
|
621
|
+
const keyStatus = ambientKeyName === AMBIENT_ENV_KEY_NAME
|
|
622
|
+
? `Using ${AMBIENT_ENV_KEY_NAME} key from ${OPENCODE_KEY_ENV}.`
|
|
623
|
+
: ambientKeyName === AMBIENT_AUTH_KEY_NAME
|
|
624
|
+
? "Using auth key from auth.json."
|
|
625
|
+
: "No keys configured. Use /opencode add <name> <key>.";
|
|
623
626
|
return `${keyStatus}\n${policyStatus}\n${watchdogStatus}`;
|
|
624
627
|
}
|
|
625
628
|
const cdMs = getCooldownMs(config);
|
|
@@ -674,6 +677,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
674
677
|
let usageStatusTimer;
|
|
675
678
|
let usageStatusDebounce;
|
|
676
679
|
let usageStatusKeyIndex;
|
|
680
|
+
let usageStatusBearerToken;
|
|
677
681
|
let usageStatusFetchedAt = 0;
|
|
678
682
|
let usageStatusInFlight;
|
|
679
683
|
let ambientKey;
|
|
@@ -701,16 +705,19 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
701
705
|
return false;
|
|
702
706
|
}
|
|
703
707
|
}
|
|
704
|
-
async function
|
|
705
|
-
|
|
706
|
-
if (!envKey)
|
|
708
|
+
async function resolveAmbientKey(ctx, current) {
|
|
709
|
+
if (current.keys.length > 0)
|
|
707
710
|
return undefined;
|
|
708
711
|
const resolved = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER);
|
|
709
|
-
|
|
712
|
+
const envKey = process.env[OPENCODE_KEY_ENV]?.trim();
|
|
713
|
+
if (envKey && (resolved === undefined || resolved === envKey)) {
|
|
714
|
+
return { name: AMBIENT_ENV_KEY_NAME, key: envKey };
|
|
715
|
+
}
|
|
716
|
+
return resolved ? { name: AMBIENT_AUTH_KEY_NAME, key: resolved } : undefined;
|
|
710
717
|
}
|
|
711
718
|
function getAmbientUsageTarget() {
|
|
712
719
|
return ambientKey
|
|
713
|
-
? { keyIndex: -1, keyName:
|
|
720
|
+
? { keyIndex: -1, keyName: ambientKey.name, bearerToken: ambientKey.key }
|
|
714
721
|
: undefined;
|
|
715
722
|
}
|
|
716
723
|
function getStatusUsageTarget(current) {
|
|
@@ -802,13 +809,14 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
802
809
|
return;
|
|
803
810
|
if (!refreshConfig())
|
|
804
811
|
return;
|
|
812
|
+
ambientKey = config.keys.length === 0 ? await resolveAmbientKey(ctx, config) : undefined;
|
|
805
813
|
const target = getStatusUsageTarget(config);
|
|
806
814
|
if (!target) {
|
|
807
815
|
setUsageStatus("opencode-go: no keys configured");
|
|
808
816
|
return;
|
|
809
817
|
}
|
|
810
818
|
const currentTime = now();
|
|
811
|
-
const keyChanged = target.keyIndex !== usageStatusKeyIndex;
|
|
819
|
+
const keyChanged = target.keyIndex !== usageStatusKeyIndex || target.bearerToken !== usageStatusBearerToken;
|
|
812
820
|
if (!force && !keyChanged && currentTime - usageStatusFetchedAt < USAGE_STATUS_REFRESH_MS)
|
|
813
821
|
return;
|
|
814
822
|
if (usageStatusInFlight)
|
|
@@ -817,6 +825,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
817
825
|
const usage = await fetchOpenCodeGoUsage(target, fetchApi, options.timers);
|
|
818
826
|
const receivedAt = now();
|
|
819
827
|
usageStatusKeyIndex = target.keyIndex;
|
|
828
|
+
usageStatusBearerToken = target.bearerToken;
|
|
820
829
|
usageStatusFetchedAt = receivedAt;
|
|
821
830
|
if (usageStatusCtx !== ctx)
|
|
822
831
|
return;
|
|
@@ -1064,15 +1073,19 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1064
1073
|
async function promptAuthImport(ctx) {
|
|
1065
1074
|
if (!ctx.hasUI || config.keys.length > 0 || config.authImportDeclined)
|
|
1066
1075
|
return false;
|
|
1067
|
-
ambientKey
|
|
1068
|
-
if (ambientKey)
|
|
1076
|
+
ambientKey = await resolveAmbientKey(ctx, config);
|
|
1077
|
+
if (ambientKey?.name === AMBIENT_ENV_KEY_NAME)
|
|
1069
1078
|
return false;
|
|
1070
1079
|
const authKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER);
|
|
1071
1080
|
if (!authKey)
|
|
1072
1081
|
return false;
|
|
1073
1082
|
const confirmed = await ctx.ui.confirm("OpenCode Go key found", "An OpenCode Go API key exists in auth.json. Import it into the rotation config?");
|
|
1074
|
-
if (confirmed === true)
|
|
1075
|
-
|
|
1083
|
+
if (confirmed === true) {
|
|
1084
|
+
const imported = await importAuthKey(authKey);
|
|
1085
|
+
if (imported)
|
|
1086
|
+
ambientKey = undefined;
|
|
1087
|
+
return imported;
|
|
1088
|
+
}
|
|
1076
1089
|
const persisted = mutateSharedConfig((freshConfig) => {
|
|
1077
1090
|
freshConfig.authImportDeclined = true;
|
|
1078
1091
|
return true;
|
|
@@ -1093,7 +1106,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1093
1106
|
usageStatusCtx = ctx;
|
|
1094
1107
|
if (!ensureConfig(ctx))
|
|
1095
1108
|
return;
|
|
1096
|
-
ambientKey = config.keys.length === 0 ? await
|
|
1109
|
+
ambientKey = config.keys.length === 0 ? await resolveAmbientKey(ctx, config) : undefined;
|
|
1097
1110
|
if (event.reason === "reload") {
|
|
1098
1111
|
applySynchronizedActiveKey(ctx);
|
|
1099
1112
|
syncUsageStatus(ctx, true);
|
|
@@ -1101,7 +1114,10 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1101
1114
|
}
|
|
1102
1115
|
if (config.keys.length === 0) {
|
|
1103
1116
|
if (ambientKey) {
|
|
1104
|
-
|
|
1117
|
+
const message = ambientKey.name === AMBIENT_ENV_KEY_NAME
|
|
1118
|
+
? `OpenCode: Using ${AMBIENT_ENV_KEY_NAME} key from ${OPENCODE_KEY_ENV}. Run /opencode add to store a managed copy.`
|
|
1119
|
+
: "OpenCode: Using auth key from auth.json. Run /opencode to import it into rotation.";
|
|
1120
|
+
ctx.ui.notify(message, "info");
|
|
1105
1121
|
syncUsageStatus(ctx, true);
|
|
1106
1122
|
return;
|
|
1107
1123
|
}
|
|
@@ -1383,7 +1399,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1383
1399
|
handler: async (args, ctx) => {
|
|
1384
1400
|
if (!ensureConfig(ctx))
|
|
1385
1401
|
return;
|
|
1386
|
-
ambientKey = config.keys.length === 0 ?
|
|
1402
|
+
ambientKey = config.keys.length === 0 ? await resolveAmbientKey(ctx, config) : undefined;
|
|
1387
1403
|
const parts = args.trim().split(/\s+/);
|
|
1388
1404
|
const subcommand = parts[0] || "";
|
|
1389
1405
|
const indexArg = parseInt(parts[1] ?? "", 10);
|
|
@@ -1397,7 +1413,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1397
1413
|
ctx.ui.notify(`OpenCode: Imported key from auth.json → ${importedName}`, "info");
|
|
1398
1414
|
}
|
|
1399
1415
|
if (ctx.mode !== "tui") {
|
|
1400
|
-
ctx.ui.notify(formatStatus(config, now(), ambientKey
|
|
1416
|
+
ctx.ui.notify(formatStatus(config, now(), ambientKey?.name), "info");
|
|
1401
1417
|
break;
|
|
1402
1418
|
}
|
|
1403
1419
|
const result = await openOpencodeConfig(ctx);
|
|
@@ -1415,7 +1431,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1415
1431
|
case "status":
|
|
1416
1432
|
case "list":
|
|
1417
1433
|
case "ls": {
|
|
1418
|
-
const status = formatStatus(config, now(), ambientKey
|
|
1434
|
+
const status = formatStatus(config, now(), ambientKey?.name);
|
|
1419
1435
|
ctx.ui.notify(status, "info");
|
|
1420
1436
|
break;
|
|
1421
1437
|
}
|
|
@@ -1427,6 +1443,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1427
1443
|
case "usage":
|
|
1428
1444
|
case "quota": {
|
|
1429
1445
|
applySynchronizedActiveKey(ctx);
|
|
1446
|
+
ambientKey = config.keys.length === 0 ? await resolveAmbientKey(ctx, config) : undefined;
|
|
1430
1447
|
const usage = await fetchOpenCodeGoUsage(getStatusUsageTarget(config), fetchApi, options.timers);
|
|
1431
1448
|
ctx.ui.notify(formatUsageStatus(usage), usage.ok ? "info" : "warning");
|
|
1432
1449
|
break;
|
|
@@ -1506,6 +1523,27 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1506
1523
|
syncUsageStatus(ctx, true);
|
|
1507
1524
|
break;
|
|
1508
1525
|
}
|
|
1526
|
+
case "import": {
|
|
1527
|
+
const authKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER);
|
|
1528
|
+
if (!authKey) {
|
|
1529
|
+
ctx.ui.notify("OpenCode: no auth.json key found for opencode-go.", "warning");
|
|
1530
|
+
return;
|
|
1531
|
+
}
|
|
1532
|
+
invalidateAutomaticDecisions();
|
|
1533
|
+
const imported = await importAuthKey(authKey);
|
|
1534
|
+
if (!imported) {
|
|
1535
|
+
if (configError)
|
|
1536
|
+
ctx.ui.notify(`OpenCode: ${configError}.`, "error");
|
|
1537
|
+
else
|
|
1538
|
+
ctx.ui.notify("OpenCode: that auth.json key is already configured.", "info");
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
ambientKey = undefined;
|
|
1542
|
+
const importedName = applyActiveKey(config, ctx.modelRegistry, now()) ?? "auth";
|
|
1543
|
+
ctx.ui.notify(`OpenCode: Imported key from auth.json → ${importedName}`, "info");
|
|
1544
|
+
syncUsageStatus(ctx, true);
|
|
1545
|
+
break;
|
|
1546
|
+
}
|
|
1509
1547
|
case "remove":
|
|
1510
1548
|
case "rm": {
|
|
1511
1549
|
const removeIndex = indexArg - 1;
|
|
@@ -1658,7 +1696,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1658
1696
|
break;
|
|
1659
1697
|
}
|
|
1660
1698
|
default:
|
|
1661
|
-
ctx.ui.notify("Usage: /opencode [config|status|usage|quota|events|use <n>|next|add <name> <key>|rm <n>|reset|cooldown <min>|policy [off|quota-only|full]|verify <on|off>|watchdog [status|on|off|<seconds>]]", "info");
|
|
1699
|
+
ctx.ui.notify("Usage: /opencode [config|status|usage|quota|events|use <n>|next|add <name> <key>|import|rm <n>|reset|cooldown <min>|policy [off|quota-only|full]|verify <on|off>|watchdog [status|on|off|<seconds>]]", "info");
|
|
1662
1700
|
}
|
|
1663
1701
|
},
|
|
1664
1702
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yugimob/pi-opencode-go-rotation",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.3",
|
|
4
4
|
"description": "Rotate between multiple OpenCode Go API keys with reactive 429, rate-limit, and watchdog stall detection, plus live footer usage for the active key",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|