@yugimob/pi-opencode-go-rotation 1.6.2 → 1.7.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/README.md +30 -10
- package/dist/config-store.d.ts +2 -0
- package/dist/config-store.js +15 -0
- package/dist/config-ui.d.ts +17 -0
- package/dist/config-ui.js +549 -0
- package/dist/format-time.d.ts +2 -0
- package/dist/format-time.js +20 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +185 -272
- package/package.json +11 -2
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
import { DEFAULT_COOLDOWN_MINUTES, DEFAULT_WATCHDOG_IDLE_MS, getConfigPath, loadConfig, reindexAfterRemoval, updateConfig, } from "./config-store.js";
|
|
2
|
+
import { formatResetIn } from "./format-time.js";
|
|
3
|
+
let tuiModulePromise;
|
|
4
|
+
async function loadTuiModule() {
|
|
5
|
+
tuiModulePromise ??= import("@earendil-works/pi-tui");
|
|
6
|
+
try {
|
|
7
|
+
return await tuiModulePromise;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
tuiModulePromise = undefined;
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function parsePositiveInteger(value, minimum) {
|
|
15
|
+
const parsed = Number(value.trim());
|
|
16
|
+
return Number.isInteger(parsed) && parsed >= minimum ? parsed : undefined;
|
|
17
|
+
}
|
|
18
|
+
export function validateNewKey(config, name, key) {
|
|
19
|
+
if (!name)
|
|
20
|
+
return "Enter a name for the key.";
|
|
21
|
+
if (!key)
|
|
22
|
+
return "Enter the API key value.";
|
|
23
|
+
if (config.keys.some((entry) => entry.key === key))
|
|
24
|
+
return "That API key is already configured.";
|
|
25
|
+
if (config.keys.some((entry) => entry.name === name))
|
|
26
|
+
return "That name is already in use.";
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
export function keyStatus(config, keyIndex, now) {
|
|
30
|
+
const parts = [];
|
|
31
|
+
if (keyIndex === config.activeKeyIndex)
|
|
32
|
+
parts.push("active");
|
|
33
|
+
const blockedUntil = quotaBlockedUntil(config, keyIndex, now);
|
|
34
|
+
const cooldownStart = config.cooldowns[keyIndex];
|
|
35
|
+
if (blockedUntil !== undefined) {
|
|
36
|
+
parts.push(`quota-blocked ${formatResetIn(Math.ceil((blockedUntil - now) / 1000))}`);
|
|
37
|
+
}
|
|
38
|
+
else if (cooldownStart !== undefined) {
|
|
39
|
+
const remaining = cooldownRemaining(config, cooldownStart, now);
|
|
40
|
+
if (remaining > 0)
|
|
41
|
+
parts.push(`cooldown ${Math.ceil(remaining / 60_000)}m`);
|
|
42
|
+
}
|
|
43
|
+
return parts.length === 0 ? "ready" : parts.join(" · ");
|
|
44
|
+
}
|
|
45
|
+
export function summarizeRestrictions(config, now) {
|
|
46
|
+
let blocked = 0;
|
|
47
|
+
let cooling = 0;
|
|
48
|
+
for (let index = 0; index < config.keys.length; index++) {
|
|
49
|
+
if (quotaBlockedUntil(config, index, now) !== undefined) {
|
|
50
|
+
blocked++;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const cooldownStart = config.cooldowns[index];
|
|
54
|
+
if (cooldownStart !== undefined && cooldownRemaining(config, cooldownStart, now) > 0)
|
|
55
|
+
cooling++;
|
|
56
|
+
}
|
|
57
|
+
if (blocked === 0 && cooling === 0)
|
|
58
|
+
return "nothing to clear";
|
|
59
|
+
return `${blocked} quota-blocked · ${cooling} cooling`;
|
|
60
|
+
}
|
|
61
|
+
export function selectKey(config, keyIndex) {
|
|
62
|
+
if (config.keys[keyIndex] === undefined)
|
|
63
|
+
return;
|
|
64
|
+
config.activeKeyIndex = keyIndex;
|
|
65
|
+
delete config.cooldowns[keyIndex];
|
|
66
|
+
delete config.quotaBlockedUntil[keyIndex];
|
|
67
|
+
}
|
|
68
|
+
export function clearKeyRestrictions(config, keyIndex) {
|
|
69
|
+
delete config.cooldowns[keyIndex];
|
|
70
|
+
delete config.quotaBlockedUntil[keyIndex];
|
|
71
|
+
}
|
|
72
|
+
export function addKey(config, name, key) {
|
|
73
|
+
config.keys.push({ name, key });
|
|
74
|
+
if (config.keys.length === 1)
|
|
75
|
+
config.activeKeyIndex = 0;
|
|
76
|
+
}
|
|
77
|
+
export function renameKey(config, keyIndex, name) {
|
|
78
|
+
const entry = config.keys[keyIndex];
|
|
79
|
+
if (entry)
|
|
80
|
+
entry.name = name;
|
|
81
|
+
}
|
|
82
|
+
export function replaceKeySecret(config, keyIndex, key) {
|
|
83
|
+
const entry = config.keys[keyIndex];
|
|
84
|
+
if (entry)
|
|
85
|
+
entry.key = key;
|
|
86
|
+
}
|
|
87
|
+
export function removeKey(config, keyIndex) {
|
|
88
|
+
if (config.keys[keyIndex] === undefined)
|
|
89
|
+
return;
|
|
90
|
+
config.keys.splice(keyIndex, 1);
|
|
91
|
+
config.cooldowns = reindexAfterRemoval(config.cooldowns, keyIndex);
|
|
92
|
+
config.quotaBlockedUntil = reindexAfterRemoval(config.quotaBlockedUntil, keyIndex);
|
|
93
|
+
if (keyIndex < config.activeKeyIndex)
|
|
94
|
+
config.activeKeyIndex--;
|
|
95
|
+
else if (config.activeKeyIndex >= config.keys.length)
|
|
96
|
+
config.activeKeyIndex = 0;
|
|
97
|
+
}
|
|
98
|
+
export function clearAllRestrictions(config) {
|
|
99
|
+
config.cooldowns = {};
|
|
100
|
+
config.quotaBlockedUntil = {};
|
|
101
|
+
}
|
|
102
|
+
function quotaBlockedUntil(config, keyIndex, now) {
|
|
103
|
+
const blockedUntil = config.quotaBlockedUntil[keyIndex];
|
|
104
|
+
return typeof blockedUntil === "number" && Number.isFinite(blockedUntil) && blockedUntil > now ? blockedUntil : undefined;
|
|
105
|
+
}
|
|
106
|
+
function cooldownDurationMs(config) {
|
|
107
|
+
return (config.cooldownMinutes || DEFAULT_COOLDOWN_MINUTES) * 60_000;
|
|
108
|
+
}
|
|
109
|
+
function cooldownRemaining(config, cooldownStart, now) {
|
|
110
|
+
return Math.max(0, cooldownDurationMs(config) - (now - cooldownStart));
|
|
111
|
+
}
|
|
112
|
+
function watchdogIdleMs(config) {
|
|
113
|
+
return config.watchdogIdleMs > 0 ? config.watchdogIdleMs : DEFAULT_WATCHDOG_IDLE_MS;
|
|
114
|
+
}
|
|
115
|
+
function rule(tui, theme) {
|
|
116
|
+
return {
|
|
117
|
+
render: (width) => [theme.fg("border", "─".repeat(Math.max(1, width)))],
|
|
118
|
+
invalidate: () => { },
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function settingsTheme(theme) {
|
|
122
|
+
return {
|
|
123
|
+
label: (text, selected) => (selected ? theme.fg("accent", text) : text),
|
|
124
|
+
value: (text) => {
|
|
125
|
+
if (text === "on" || text === "active")
|
|
126
|
+
return theme.fg("success", text);
|
|
127
|
+
if (text.startsWith("quota-blocked"))
|
|
128
|
+
return theme.fg("warning", text);
|
|
129
|
+
if (text.startsWith("cooldown") || text === "off")
|
|
130
|
+
return theme.fg("dim", text);
|
|
131
|
+
return theme.fg("accent", text);
|
|
132
|
+
},
|
|
133
|
+
description: (text) => theme.fg("muted", text),
|
|
134
|
+
cursor: theme.fg("accent", "> "),
|
|
135
|
+
hint: (text) => theme.fg("dim", text),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function selectTheme(theme) {
|
|
139
|
+
return {
|
|
140
|
+
selectedPrefix: (text) => theme.fg("accent", text),
|
|
141
|
+
selectedText: (text) => theme.fg("accent", text),
|
|
142
|
+
description: (text) => theme.fg("muted", text),
|
|
143
|
+
scrollInfo: (text) => theme.fg("dim", text),
|
|
144
|
+
noMatch: (text) => theme.fg("muted", text),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function createMenu(tui, theme, items, onSelect, onCancel) {
|
|
148
|
+
const list = new tui.SelectList(items, Math.max(1, items.length), selectTheme(theme));
|
|
149
|
+
list.onSelect = (item) => onSelect(item.value);
|
|
150
|
+
list.onCancel = onCancel;
|
|
151
|
+
return {
|
|
152
|
+
render: (width) => list.render(width),
|
|
153
|
+
invalidate: () => list.invalidate(),
|
|
154
|
+
handleInput: (data) => list.handleInput(data),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function createConfirm(tui, theme, message, onConfirm, onCancel) {
|
|
158
|
+
return createMenu(tui, theme, [
|
|
159
|
+
{ value: "yes", label: "Yes", description: message },
|
|
160
|
+
{ value: "no", label: "No", description: "Keep the current state." },
|
|
161
|
+
], (value) => (value === "yes" ? onConfirm() : onCancel()), onCancel);
|
|
162
|
+
}
|
|
163
|
+
function createForm(tui, options) {
|
|
164
|
+
const { theme, title, fields, isFocused, submit, cancel } = options;
|
|
165
|
+
const inputs = fields.map((field) => {
|
|
166
|
+
const input = new tui.Input({ placeholder: field.placeholder ?? "" });
|
|
167
|
+
input.setValue(field.value ?? "");
|
|
168
|
+
return input;
|
|
169
|
+
});
|
|
170
|
+
let focusIndex = 0;
|
|
171
|
+
let error = "";
|
|
172
|
+
const render = (width) => {
|
|
173
|
+
const lines = [tui.truncateToWidth(theme.fg("accent", theme.bold(title)), width)];
|
|
174
|
+
fields.forEach((field, index) => {
|
|
175
|
+
const input = inputs[index];
|
|
176
|
+
const selected = index === focusIndex;
|
|
177
|
+
input.focused = isFocused() && selected;
|
|
178
|
+
lines.push(tui.truncateToWidth(`${selected ? theme.fg("accent", "> ") : " "}${theme.fg("muted", field.label)}`, width));
|
|
179
|
+
lines.push(...input.render(Math.max(1, width - 2)).map((line) => tui.truncateToWidth(` ${line}`, width)));
|
|
180
|
+
});
|
|
181
|
+
if (error)
|
|
182
|
+
lines.push(tui.truncateToWidth(theme.fg("error", error), width));
|
|
183
|
+
lines.push(tui.truncateToWidth(theme.fg("dim", "Tab or ↑/↓ switch · Enter submits · Esc cancels"), width));
|
|
184
|
+
return lines;
|
|
185
|
+
};
|
|
186
|
+
const handleInput = (data) => {
|
|
187
|
+
if (tui.matchesKey(data, "escape")) {
|
|
188
|
+
cancel();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (tui.matchesKey(data, "up") || tui.matchesKey(data, "shift+tab")) {
|
|
192
|
+
focusIndex = (focusIndex + inputs.length - 1) % inputs.length;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (tui.matchesKey(data, "down") || tui.matchesKey(data, "tab")) {
|
|
196
|
+
focusIndex = (focusIndex + 1) % inputs.length;
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (tui.matchesKey(data, "return")) {
|
|
200
|
+
error = submit(inputs.map((input) => input.getValue())) ?? "";
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
inputs[focusIndex].handleInput(data);
|
|
204
|
+
};
|
|
205
|
+
return {
|
|
206
|
+
render,
|
|
207
|
+
invalidate: () => {
|
|
208
|
+
for (const input of inputs)
|
|
209
|
+
input.invalidate();
|
|
210
|
+
},
|
|
211
|
+
handleInput,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function createKeyManager(tui, options) {
|
|
215
|
+
const { theme, config, keyIndex, isFocused, persist, rebuild, requestUsage, close } = options;
|
|
216
|
+
const name = config.keys[keyIndex]?.name ?? `key-${keyIndex + 1}`;
|
|
217
|
+
let form;
|
|
218
|
+
let error = "";
|
|
219
|
+
const finish = (selectId) => {
|
|
220
|
+
close();
|
|
221
|
+
rebuild(selectId);
|
|
222
|
+
};
|
|
223
|
+
const showError = (message) => {
|
|
224
|
+
if (message === undefined)
|
|
225
|
+
return false;
|
|
226
|
+
error = message;
|
|
227
|
+
return true;
|
|
228
|
+
};
|
|
229
|
+
const menu = createMenu(tui, theme, [
|
|
230
|
+
{ value: "use", label: "Set active", description: `Use "${name}" for the next request and clear its cooldown and quota block.` },
|
|
231
|
+
{ value: "rename", label: "Rename", description: `Rename "${name}".` },
|
|
232
|
+
{ value: "replace", label: "Replace key", description: "Store a new secret for this entry." },
|
|
233
|
+
{ value: "clear", label: "Clear restrictions", description: "Remove this key's cooldown and quota block." },
|
|
234
|
+
{ value: "usage", label: "Check usage", description: "Fetch plan usage for this key after the window closes." },
|
|
235
|
+
{ value: "remove", label: "Remove", description: `Delete "${name}" from the configuration.` },
|
|
236
|
+
{ value: "back", label: "Back", description: "Return to the key list." },
|
|
237
|
+
], (value) => {
|
|
238
|
+
if (value === "use") {
|
|
239
|
+
if (!showError(persist((fresh) => selectKey(fresh, keyIndex))))
|
|
240
|
+
finish(`key:${keyIndex}`);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (value === "clear") {
|
|
244
|
+
if (!showError(persist((fresh) => clearKeyRestrictions(fresh, keyIndex))))
|
|
245
|
+
finish(`key:${keyIndex}`);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (value === "usage") {
|
|
249
|
+
requestUsage(keyIndex);
|
|
250
|
+
close();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (value === "back") {
|
|
254
|
+
close();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (value === "rename") {
|
|
258
|
+
form = createForm(tui, {
|
|
259
|
+
theme,
|
|
260
|
+
title: `Rename ${name}`,
|
|
261
|
+
fields: [{ label: "Name", value: name }],
|
|
262
|
+
isFocused,
|
|
263
|
+
submit: (values) => {
|
|
264
|
+
const next = values[0].trim();
|
|
265
|
+
if (!next)
|
|
266
|
+
return "Enter a name.";
|
|
267
|
+
if (next !== name && config.keys.some((entry, index) => index !== keyIndex && entry.name === next)) {
|
|
268
|
+
return "That name is already in use.";
|
|
269
|
+
}
|
|
270
|
+
const saved = persist((fresh) => renameKey(fresh, keyIndex, next));
|
|
271
|
+
if (saved !== undefined)
|
|
272
|
+
return saved;
|
|
273
|
+
finish(`key:${keyIndex}`);
|
|
274
|
+
return undefined;
|
|
275
|
+
},
|
|
276
|
+
cancel: () => {
|
|
277
|
+
form = undefined;
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (value === "replace") {
|
|
283
|
+
form = createForm(tui, {
|
|
284
|
+
theme,
|
|
285
|
+
title: `Replace ${name}`,
|
|
286
|
+
fields: [{ label: "New API key", placeholder: "sk-..." }],
|
|
287
|
+
isFocused,
|
|
288
|
+
submit: (values) => {
|
|
289
|
+
const secret = values[0].trim();
|
|
290
|
+
if (!secret)
|
|
291
|
+
return "Enter the API key value.";
|
|
292
|
+
if (config.keys.some((entry, index) => index !== keyIndex && entry.key === secret)) {
|
|
293
|
+
return "That API key is already configured.";
|
|
294
|
+
}
|
|
295
|
+
const saved = persist((fresh) => replaceKeySecret(fresh, keyIndex, secret));
|
|
296
|
+
if (saved !== undefined)
|
|
297
|
+
return saved;
|
|
298
|
+
finish(`key:${keyIndex}`);
|
|
299
|
+
return undefined;
|
|
300
|
+
},
|
|
301
|
+
cancel: () => {
|
|
302
|
+
form = undefined;
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
form = createConfirm(tui, theme, `Delete "${name}"?`, () => {
|
|
308
|
+
const saved = persist((fresh) => removeKey(fresh, keyIndex));
|
|
309
|
+
if (saved !== undefined) {
|
|
310
|
+
error = saved;
|
|
311
|
+
form = undefined;
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
finish(keyIndex > 0 ? `key:${keyIndex - 1}` : undefined);
|
|
315
|
+
}, () => {
|
|
316
|
+
form = undefined;
|
|
317
|
+
});
|
|
318
|
+
}, () => close());
|
|
319
|
+
const active = () => form ?? menu;
|
|
320
|
+
return {
|
|
321
|
+
render: (width) => [...active().render(width), ...(error ? [tui.truncateToWidth(theme.fg("error", error), width)] : [])],
|
|
322
|
+
invalidate: () => active().invalidate?.(),
|
|
323
|
+
handleInput: (data) => active().handleInput?.(data),
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
export async function openOpencodeConfig(ctx) {
|
|
327
|
+
if (ctx.mode !== "tui") {
|
|
328
|
+
ctx.ui.notify("/opencode config requires interactive mode", "error");
|
|
329
|
+
return {};
|
|
330
|
+
}
|
|
331
|
+
const tui = await loadTuiModule();
|
|
332
|
+
if (!tui) {
|
|
333
|
+
ctx.ui.notify("/opencode config is unavailable in this environment", "error");
|
|
334
|
+
return {};
|
|
335
|
+
}
|
|
336
|
+
try {
|
|
337
|
+
loadConfig();
|
|
338
|
+
}
|
|
339
|
+
catch (error) {
|
|
340
|
+
ctx.ui.notify(`OpenCode config: ${error instanceof Error ? error.message : String(error)}`, "error");
|
|
341
|
+
return {};
|
|
342
|
+
}
|
|
343
|
+
return await ctx.ui.custom((interactive, theme, _keybindings, done) => {
|
|
344
|
+
const result = {};
|
|
345
|
+
const status = new tui.Text(`Config file: ${getConfigPath()}`, 1, 0);
|
|
346
|
+
const container = new tui.Container();
|
|
347
|
+
let focused = false;
|
|
348
|
+
let list;
|
|
349
|
+
const isFocused = () => focused;
|
|
350
|
+
const persist = (mutator) => {
|
|
351
|
+
try {
|
|
352
|
+
updateConfig(mutator);
|
|
353
|
+
status.setText(theme.fg("dim", `Saved to ${getConfigPath()}`));
|
|
354
|
+
return undefined;
|
|
355
|
+
}
|
|
356
|
+
catch (error) {
|
|
357
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
358
|
+
status.setText(theme.fg("error", `Save failed: ${message}`));
|
|
359
|
+
return message;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
const buildItems = (current) => {
|
|
363
|
+
const now = Date.now();
|
|
364
|
+
const items = [];
|
|
365
|
+
if (current.keys.length === 0) {
|
|
366
|
+
items.push({
|
|
367
|
+
id: "empty",
|
|
368
|
+
label: "No keys configured",
|
|
369
|
+
currentValue: "none",
|
|
370
|
+
description: "Use Add key to register an OpenCode Go API key.",
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
current.keys.forEach((entry, index) => {
|
|
374
|
+
items.push({
|
|
375
|
+
id: `key:${index}`,
|
|
376
|
+
label: `${index + 1}. ${entry.name}`,
|
|
377
|
+
currentValue: keyStatus(current, index, now),
|
|
378
|
+
description: "Enter to set active, rename, replace, clear restrictions, check usage, or remove.",
|
|
379
|
+
submenu: (_value, close) => createKeyManager(tui, {
|
|
380
|
+
theme,
|
|
381
|
+
config: current,
|
|
382
|
+
keyIndex: index,
|
|
383
|
+
isFocused,
|
|
384
|
+
persist,
|
|
385
|
+
rebuild,
|
|
386
|
+
requestUsage: (keyIndex) => {
|
|
387
|
+
result.usageKeyIndex = keyIndex;
|
|
388
|
+
},
|
|
389
|
+
close: () => close(),
|
|
390
|
+
}),
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
items.push({
|
|
394
|
+
id: "add",
|
|
395
|
+
label: "Add key",
|
|
396
|
+
currentValue: "new",
|
|
397
|
+
description: "Register another OpenCode Go API key.",
|
|
398
|
+
submenu: (_value, close) => createForm(tui, {
|
|
399
|
+
theme,
|
|
400
|
+
title: "Add key",
|
|
401
|
+
fields: [
|
|
402
|
+
{ label: "Name", placeholder: "personal" },
|
|
403
|
+
{ label: "API key", placeholder: "sk-..." },
|
|
404
|
+
],
|
|
405
|
+
isFocused,
|
|
406
|
+
submit: (values) => {
|
|
407
|
+
const name = values[0].trim();
|
|
408
|
+
const key = values[1].trim();
|
|
409
|
+
const validation = validateNewKey(current, name, key);
|
|
410
|
+
if (validation)
|
|
411
|
+
return validation;
|
|
412
|
+
let addedIndex;
|
|
413
|
+
const saved = persist((fresh) => {
|
|
414
|
+
addKey(fresh, name, key);
|
|
415
|
+
addedIndex = fresh.keys.length - 1;
|
|
416
|
+
});
|
|
417
|
+
if (saved)
|
|
418
|
+
return saved;
|
|
419
|
+
close();
|
|
420
|
+
rebuild(addedIndex === undefined ? undefined : `key:${addedIndex}`);
|
|
421
|
+
return undefined;
|
|
422
|
+
},
|
|
423
|
+
cancel: () => close(),
|
|
424
|
+
}),
|
|
425
|
+
});
|
|
426
|
+
items.push({
|
|
427
|
+
id: "cooldown",
|
|
428
|
+
label: "Cooldown",
|
|
429
|
+
currentValue: `${current.cooldownMinutes || DEFAULT_COOLDOWN_MINUTES} min`,
|
|
430
|
+
description: "Rest period applied to a key after a transient rate limit.",
|
|
431
|
+
submenu: (_value, close) => createForm(tui, {
|
|
432
|
+
theme,
|
|
433
|
+
title: "Cooldown",
|
|
434
|
+
fields: [{ label: "Minutes (at least 1)", value: String(current.cooldownMinutes || DEFAULT_COOLDOWN_MINUTES) }],
|
|
435
|
+
isFocused,
|
|
436
|
+
submit: (values) => {
|
|
437
|
+
const minutes = parsePositiveInteger(values[0], 1);
|
|
438
|
+
if (minutes === undefined)
|
|
439
|
+
return "Enter a whole number of at least 1.";
|
|
440
|
+
const saved = persist((fresh) => {
|
|
441
|
+
fresh.cooldownMinutes = minutes;
|
|
442
|
+
});
|
|
443
|
+
if (saved)
|
|
444
|
+
return saved;
|
|
445
|
+
close();
|
|
446
|
+
rebuild("cooldown");
|
|
447
|
+
return undefined;
|
|
448
|
+
},
|
|
449
|
+
cancel: () => close(),
|
|
450
|
+
}),
|
|
451
|
+
});
|
|
452
|
+
items.push({
|
|
453
|
+
id: "watchdog",
|
|
454
|
+
label: "Watchdog",
|
|
455
|
+
currentValue: current.watchdogEnabled ? "on" : "off",
|
|
456
|
+
values: ["on", "off"],
|
|
457
|
+
description: "Abort and rotate an opencode-go request that stalls with no response or stream activity.",
|
|
458
|
+
});
|
|
459
|
+
items.push({
|
|
460
|
+
id: "watchdogIdle",
|
|
461
|
+
label: "Watchdog idle",
|
|
462
|
+
currentValue: `${Math.ceil(watchdogIdleMs(current) / 1000)}s`,
|
|
463
|
+
description: "Seconds without activity before the watchdog declares a stall.",
|
|
464
|
+
submenu: (_value, close) => createForm(tui, {
|
|
465
|
+
theme,
|
|
466
|
+
title: "Watchdog idle",
|
|
467
|
+
fields: [{ label: "Seconds (at least 1)", value: String(Math.ceil(watchdogIdleMs(current) / 1000)) }],
|
|
468
|
+
isFocused,
|
|
469
|
+
submit: (values) => {
|
|
470
|
+
const seconds = parsePositiveInteger(values[0], 1);
|
|
471
|
+
if (seconds === undefined)
|
|
472
|
+
return "Enter a whole number of at least 1.";
|
|
473
|
+
const saved = persist((fresh) => {
|
|
474
|
+
fresh.watchdogIdleMs = seconds * 1000;
|
|
475
|
+
});
|
|
476
|
+
if (saved)
|
|
477
|
+
return saved;
|
|
478
|
+
close();
|
|
479
|
+
rebuild("watchdogIdle");
|
|
480
|
+
return undefined;
|
|
481
|
+
},
|
|
482
|
+
cancel: () => close(),
|
|
483
|
+
}),
|
|
484
|
+
});
|
|
485
|
+
items.push({
|
|
486
|
+
id: "reset",
|
|
487
|
+
label: "Reset blocks",
|
|
488
|
+
currentValue: summarizeRestrictions(current, now),
|
|
489
|
+
description: "Clear every cooldown and quota block.",
|
|
490
|
+
submenu: (_value, close) => createConfirm(tui, theme, "Clear all cooldowns and quota blocks?", () => {
|
|
491
|
+
const saved = persist((fresh) => clearAllRestrictions(fresh));
|
|
492
|
+
close();
|
|
493
|
+
if (!saved)
|
|
494
|
+
rebuild("reset");
|
|
495
|
+
}, () => close()),
|
|
496
|
+
});
|
|
497
|
+
return items;
|
|
498
|
+
};
|
|
499
|
+
const rebuild = (selectId) => {
|
|
500
|
+
let current;
|
|
501
|
+
try {
|
|
502
|
+
current = loadConfig();
|
|
503
|
+
}
|
|
504
|
+
catch (error) {
|
|
505
|
+
status.setText(theme.fg("error", `Reload failed: ${error instanceof Error ? error.message : String(error)}`));
|
|
506
|
+
interactive.requestRender();
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
const items = buildItems(current);
|
|
510
|
+
list = new tui.SettingsList(items, Math.max(1, Math.min(items.length, 12)), settingsTheme(theme), onChange, () => done(result));
|
|
511
|
+
if (selectId)
|
|
512
|
+
list.selectItem(selectId);
|
|
513
|
+
container.clear();
|
|
514
|
+
container.addChild(rule(tui, theme));
|
|
515
|
+
container.addChild(new tui.Text(theme.fg("accent", theme.bold("OpenCode Go keys")), 1, 0));
|
|
516
|
+
container.addChild(new tui.Text(theme.fg("dim", "Keys, cooldowns, quota blocks, and the stall watchdog. Esc closes."), 1, 0));
|
|
517
|
+
container.addChild(list);
|
|
518
|
+
container.addChild(status);
|
|
519
|
+
container.addChild(rule(tui, theme));
|
|
520
|
+
interactive.requestRender();
|
|
521
|
+
};
|
|
522
|
+
const onChange = (id, value) => {
|
|
523
|
+
if (id !== "watchdog")
|
|
524
|
+
return;
|
|
525
|
+
const enabled = value === "on";
|
|
526
|
+
if (persist((fresh) => {
|
|
527
|
+
fresh.watchdogEnabled = enabled;
|
|
528
|
+
}) === undefined) {
|
|
529
|
+
rebuild("watchdog");
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
rebuild();
|
|
533
|
+
const component = {
|
|
534
|
+
get focused() {
|
|
535
|
+
return focused;
|
|
536
|
+
},
|
|
537
|
+
set focused(value) {
|
|
538
|
+
focused = value;
|
|
539
|
+
},
|
|
540
|
+
render: (width) => container.render(width),
|
|
541
|
+
invalidate: () => container.invalidate(),
|
|
542
|
+
handleInput: (data) => {
|
|
543
|
+
list.handleInput(data);
|
|
544
|
+
interactive.requestRender();
|
|
545
|
+
},
|
|
546
|
+
};
|
|
547
|
+
return component;
|
|
548
|
+
}, { overlay: true, overlayOptions: { anchor: "center", width: "72%", minWidth: 60, maxHeight: "80%" } });
|
|
549
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function formatResetIn(seconds) {
|
|
2
|
+
if (seconds <= 0)
|
|
3
|
+
return "now";
|
|
4
|
+
const days = Math.floor(seconds / 86_400);
|
|
5
|
+
const hours = Math.floor((seconds % 86_400) / 3_600);
|
|
6
|
+
const minutes = Math.ceil((seconds % 3_600) / 60);
|
|
7
|
+
if (days > 0)
|
|
8
|
+
return hours > 0 ? `${days}d ${hours}h` : `${days}d`;
|
|
9
|
+
if (hours > 0)
|
|
10
|
+
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
|
|
11
|
+
return minutes > 0 ? `${minutes}m` : "less than 1m";
|
|
12
|
+
}
|
|
13
|
+
export function formatDuration(ms) {
|
|
14
|
+
const seconds = Math.max(0, Math.ceil(ms / 1000));
|
|
15
|
+
if (seconds < 60)
|
|
16
|
+
return `${seconds}s`;
|
|
17
|
+
const minutes = Math.floor(seconds / 60);
|
|
18
|
+
const remainder = seconds % 60;
|
|
19
|
+
return remainder ? `${minutes}m ${remainder}s` : `${minutes}m`;
|
|
20
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { formatResetIn } from "./format-time.ts";
|
|
3
|
+
export { formatResetIn };
|
|
2
4
|
export declare function shouldWatchProvider(provider: string | undefined): boolean;
|
|
3
5
|
export type RateLimitKind = "transient" | "fixed-window-quota";
|
|
4
6
|
export declare function classifyRateLimitError(message: string): RateLimitKind | undefined;
|
|
@@ -88,7 +90,6 @@ export declare class ProviderIdleWatchdog {
|
|
|
88
90
|
private clear;
|
|
89
91
|
}
|
|
90
92
|
export declare function parseOpenCodeGoUsage(value: unknown): OpenCodeGoUsageResponse | undefined;
|
|
91
|
-
export declare function formatResetIn(seconds: number): string;
|
|
92
93
|
export declare function formatUsageStatus(result: UsageFetchResult): string;
|
|
93
94
|
export declare function shortUsageWindowLabel(name: string | undefined, index: number): string;
|
|
94
95
|
export interface FooterUsageOptions {
|