sideby 0.35.8 → 0.36.0
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/build.json +1 -1
- package/dist/src/cli/commands.d.ts +12 -0
- package/dist/src/cli/commands.js +480 -6
- package/dist/src/cli/format.d.ts +12 -1
- package/dist/src/cli/format.js +23 -3
- package/dist/src/cli/json-schemas.js +166 -1
- package/dist/src/cli/main.js +18 -4
- package/dist/src/core/advisor.d.ts +176 -0
- package/dist/src/core/advisor.js +472 -0
- package/dist/src/core/after-install.js +11 -4
- package/dist/src/core/ask.d.ts +6 -1
- package/dist/src/core/ask.js +16 -5
- package/dist/src/core/browser-profiles.d.ts +16 -0
- package/dist/src/core/browser-profiles.js +45 -0
- package/dist/src/core/cli-commands.js +25 -1
- package/dist/src/core/config.d.ts +29 -0
- package/dist/src/core/config.js +25 -0
- package/dist/src/core/handoff-select.d.ts +13 -2
- package/dist/src/core/handoff-select.js +6 -2
- package/dist/src/core/launch.d.ts +4 -0
- package/dist/src/core/launch.js +7 -0
- package/dist/src/core/limit-reset.d.ts +198 -0
- package/dist/src/core/limit-reset.js +299 -0
- package/dist/src/core/local-build.d.ts +4 -3
- package/dist/src/core/local-build.js +6 -4
- package/dist/src/core/quota-fetch.d.ts +57 -2
- package/dist/src/core/quota-fetch.js +291 -13
- package/dist/src/core/quota-levels.d.ts +70 -0
- package/dist/src/core/quota-levels.js +80 -0
- package/dist/src/core/quota-store.d.ts +15 -1
- package/dist/src/core/quota-store.js +26 -1
- package/dist/src/core/same-login.d.ts +8 -0
- package/dist/src/core/same-login.js +39 -2
- package/dist/src/core/search-catalog.js +38 -0
- package/dist/src/families/claude/index.js +2 -0
- package/dist/src/families/codex/index.js +3 -1
- package/dist/src/families/codex/quota.d.ts +23 -1
- package/dist/src/families/codex/quota.js +122 -21
- package/dist/src/families/grok/quota-log.d.ts +1 -0
- package/dist/src/menubar/script.js +70 -18
- package/dist/src/menubar/snapshot.d.ts +33 -1
- package/dist/src/menubar/snapshot.js +79 -11
- package/dist/src/menubar/strings.d.ts +16 -0
- package/dist/src/menubar/strings.js +16 -0
- package/dist/src/panel/handler.d.ts +21 -0
- package/dist/src/panel/handler.js +111 -9
- package/dist/src/panel/i18n.d.ts +87 -0
- package/dist/src/panel/i18n.js +174 -0
- package/dist/src/panel/page-logic.d.ts +17 -0
- package/dist/src/panel/page-logic.js +23 -0
- package/dist/src/panel/page-script.js +440 -19
- package/dist/src/panel/page-styles.js +38 -2
- package/dist/src/plugins/loader.d.ts +2 -0
- package/dist/src/plugins/loader.js +27 -0
- package/dist/src/runtime.d.ts +63 -1
- package/dist/src/runtime.js +529 -9
- package/dist/src/types.d.ts +67 -0
- package/package.json +1 -1
- package/schemas/advisor.json +332 -0
- package/schemas/config.json +22 -0
- package/schemas/error.json +4 -0
- package/schemas/menubar-reset.json +93 -0
- package/schemas/menubar-snapshot.json +70 -2
- package/schemas/quota-reset.json +369 -0
- package/schemas/quota.json +30 -0
- package/schemas/update.json +4 -0
- package/skills/sideby/SKILL.md +6 -2
- package/skills/sideby/references/ai-tasks.md +1 -1
- package/skills/sideby/references/panel.md +14 -0
- package/skills/sideby/references/quota.md +27 -0
- package/skills/sideby/references/update.md +1 -1
|
@@ -24,6 +24,24 @@ export function isSameLogin(a, b) {
|
|
|
24
24
|
return false;
|
|
25
25
|
return norm(a.identity?.org) === norm(b.identity?.org);
|
|
26
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Everything two Accounts must have equal to be the same login, as one string: the Family, the email, the
|
|
29
|
+
* organization and, in a Family of `LOGIN_KEY_FAMILIES`, the Login Key. Undefined when the login cannot be told: not
|
|
30
|
+
* a subscription, no email, or no Login Key where one is needed. Any Family with an Identity has one, also one whose
|
|
31
|
+
* Accounts never group. Only for comparing inside the Runtime: it holds the Login Key and the Identity, so it is
|
|
32
|
+
* never put into an output, an error or a file as it is (a Limit Reset record keeps a salted digest of it, ADR-0072).
|
|
33
|
+
*/
|
|
34
|
+
export function loginComparisonKey(m) {
|
|
35
|
+
if (m.kind !== 'subscription')
|
|
36
|
+
return undefined;
|
|
37
|
+
const email = norm(m.identity?.email);
|
|
38
|
+
if (!email)
|
|
39
|
+
return undefined;
|
|
40
|
+
const keyed = LOGIN_KEY_FAMILIES.has(m.family);
|
|
41
|
+
if (keyed && !m.loginKey)
|
|
42
|
+
return undefined;
|
|
43
|
+
return JSON.stringify([m.family, email, norm(m.identity?.org) ?? '', keyed ? m.loginKey : '']);
|
|
44
|
+
}
|
|
27
45
|
/** Ref → the other refs of its Same Login, in the order given; Accounts without one are left out. */
|
|
28
46
|
export function sameLoginGroups(members) {
|
|
29
47
|
const out = new Map();
|
|
@@ -34,6 +52,8 @@ export function sameLoginGroups(members) {
|
|
|
34
52
|
}
|
|
35
53
|
return out;
|
|
36
54
|
}
|
|
55
|
+
/** How much older than the reading shown a count of Limit Resets from another reading of the login may be. */
|
|
56
|
+
const RESETS_CARRY_MS = 60 * 60_000;
|
|
37
57
|
/**
|
|
38
58
|
* The Quota an Account of a Same Login shows: the freshest `ok` reading of the group (its own wins a tie), marked
|
|
39
59
|
* with `from` when it is another member's. Without any `ok` reading the Account keeps its own result.
|
|
@@ -53,8 +73,25 @@ export function sharedQuota(ref, readings, peers, localFallbackOnly = false) {
|
|
|
53
73
|
if (!best || rank > best.rank || (rank === best.rank && t > best.t))
|
|
54
74
|
best = { ref: r, q, t, rank };
|
|
55
75
|
}
|
|
56
|
-
if (!best
|
|
76
|
+
if (!best)
|
|
57
77
|
return own;
|
|
78
|
+
// The count of Limit Resets belongs to the login. A reading that does not tell it (kept by a sideby from before
|
|
79
|
+
// the count was read, which may still run in an open session) takes the newest count another reading of the login
|
|
80
|
+
// tells, while that count was read within the hour; it carries its own read time.
|
|
81
|
+
let resets = best.q.limitResets;
|
|
82
|
+
if (!resets)
|
|
83
|
+
for (const r of [ref, ...peers]) {
|
|
84
|
+
const q = readings.get(r);
|
|
85
|
+
const c = q?.status === 'ok' ? q.limitResets : undefined;
|
|
86
|
+
const at = Date.parse(c?.observedAt ?? '');
|
|
87
|
+
if (!c || !(best.t - at <= RESETS_CARRY_MS))
|
|
88
|
+
continue;
|
|
89
|
+
if (!resets || at > Date.parse(resets.observedAt))
|
|
90
|
+
resets = c;
|
|
91
|
+
}
|
|
92
|
+
const carried = resets && !best.q.limitResets ? { limitResets: resets } : {};
|
|
93
|
+
if (best.ref === ref)
|
|
94
|
+
return { ...own, ...carried };
|
|
58
95
|
const { from: _from, ...reading } = best.q;
|
|
59
|
-
return { ...reading, from: best.ref };
|
|
96
|
+
return { ...reading, ...carried, from: best.ref };
|
|
60
97
|
}
|
|
@@ -186,6 +186,25 @@ const ENTRIES = [
|
|
|
186
186
|
target: { settings: 'accounts', find: 'rc' },
|
|
187
187
|
cliCommand: 'sideby remote-control',
|
|
188
188
|
},
|
|
189
|
+
{
|
|
190
|
+
id: 'setting.advisor',
|
|
191
|
+
kind: 'setting',
|
|
192
|
+
title: { en: 'Advisor', zh: '顾问模型(Advisor)' },
|
|
193
|
+
synonyms: [
|
|
194
|
+
'/advisor',
|
|
195
|
+
'advisor model',
|
|
196
|
+
'stronger model',
|
|
197
|
+
'second opinion',
|
|
198
|
+
'fable',
|
|
199
|
+
'opus',
|
|
200
|
+
'顾问',
|
|
201
|
+
'请教',
|
|
202
|
+
'更强的模型',
|
|
203
|
+
],
|
|
204
|
+
panel: at(SET, ACCOUNTS),
|
|
205
|
+
target: { settings: 'accounts', find: 'advisor' },
|
|
206
|
+
cliCommand: 'sideby advisor',
|
|
207
|
+
},
|
|
189
208
|
{
|
|
190
209
|
id: 'setting.quota-checks',
|
|
191
210
|
kind: 'setting',
|
|
@@ -249,6 +268,25 @@ const ENTRIES = [
|
|
|
249
268
|
target: { action: 'new-account', write: true },
|
|
250
269
|
cliCommand: 'sideby new <family> --next',
|
|
251
270
|
},
|
|
271
|
+
{
|
|
272
|
+
id: 'action.limit-reset',
|
|
273
|
+
kind: 'action',
|
|
274
|
+
title: { en: 'Use a limit reset', zh: '使用一次重置' },
|
|
275
|
+
synonyms: [
|
|
276
|
+
'reset quota',
|
|
277
|
+
'reset limit',
|
|
278
|
+
'reset credit',
|
|
279
|
+
'restore quota',
|
|
280
|
+
'usage reset',
|
|
281
|
+
'重置额度',
|
|
282
|
+
'重置次数',
|
|
283
|
+
'恢复额度',
|
|
284
|
+
'用重置',
|
|
285
|
+
],
|
|
286
|
+
panel: at(ACCOUNTS, { en: "an account's menu", zh: '账号的菜单' }),
|
|
287
|
+
target: { action: 'limit-reset', write: true },
|
|
288
|
+
cliCommand: 'sideby quota reset <account>',
|
|
289
|
+
},
|
|
252
290
|
{
|
|
253
291
|
id: 'action.remote-control-off',
|
|
254
292
|
kind: 'action',
|
|
@@ -136,6 +136,8 @@ export const claudeFamily = {
|
|
|
136
136
|
readUsage: readClaudeUsage,
|
|
137
137
|
readTuning: readClaudeTuning,
|
|
138
138
|
quotaSetup: claudeStatusline,
|
|
139
|
+
// Claude Code cannot spend a Limit Reset or tell how many an Account holds: only the vendor's page can (ADR-0072).
|
|
140
|
+
limitReset: { vendorPage: 'https://claude.ai/settings/usage' },
|
|
139
141
|
resumedSession: resumeFlagSession,
|
|
140
142
|
sessionWrittenAt: (account, id) => groupedEntryWrittenAt(join(account.dir, 'projects'), `${id}.jsonl`, 'file'),
|
|
141
143
|
// Claude Code makes this directory for SessionStart hooks before the first message is saved (ADR-0009).
|
|
@@ -6,7 +6,7 @@ import { nestedFileWrittenAt, resumeSubcommandSession } from '../shared/sessions
|
|
|
6
6
|
import { topLevelTomlString } from '../shared/toml.js';
|
|
7
7
|
import { codexHandoff } from './handoff.js';
|
|
8
8
|
import { codexModelChoice } from './model.js';
|
|
9
|
-
import { codexQuotaFetch } from './quota.js';
|
|
9
|
+
import { codexQuotaFetch, useCodexLimitReset } from './quota.js';
|
|
10
10
|
import { readCodexQuota, readCodexUsage } from './rollout.js';
|
|
11
11
|
import { readCodexTuning } from './tuning.js';
|
|
12
12
|
const FILE_STORE_KEY = 'cli_auth_credentials_store';
|
|
@@ -89,6 +89,8 @@ export const codexFamily = {
|
|
|
89
89
|
identity: codexIdentity,
|
|
90
90
|
readQuota: (account) => readCodexQuota(account.dir),
|
|
91
91
|
fetchQuota: codexQuotaFetch,
|
|
92
|
+
// Spending a Limit Reset the Account holds, only after the user confirmed it (ADR-0072).
|
|
93
|
+
limitReset: { use: useCodexLimitReset },
|
|
92
94
|
readUsage: (account, ctx) => readCodexUsage(account.dir, ctx.now),
|
|
93
95
|
readTuning: readCodexTuning,
|
|
94
96
|
resumedSession: resumeSubcommandSession,
|
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
import type { Account, QuotaFetch, QuotaFetchContext, QuotaResult } from '../../types.ts';
|
|
1
|
+
import type { Account, LimitResetContext, LimitResetOutcome, LimitResets, QuotaFetch, QuotaFetchContext, QuotaResult } from '../../types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The Limit Resets the answer tells (`rateLimitResetCredits`, ADR-0072), or undefined when it does not tell: the
|
|
4
|
+
* field is missing or null, or its count is not a whole number. `availableCount` is the count; the detail rows may be
|
|
5
|
+
* missing or cut short, so the next expiry is kept only when they cover every available credit. No credit id, title
|
|
6
|
+
* or description is kept.
|
|
7
|
+
*/
|
|
8
|
+
export declare function limitResetsOf(raw: unknown, observedAt: string): LimitResets | undefined;
|
|
2
9
|
export declare function fetchCodexQuota(account: Account, ctx: QuotaFetchContext): Promise<QuotaResult>;
|
|
10
|
+
/**
|
|
11
|
+
* A spend that got no final answer. `sent` says whether the request left sideby: false means the Host never got it,
|
|
12
|
+
* so nothing was spent; true means the outcome is unknown and only the same idempotency key may ask again.
|
|
13
|
+
*/
|
|
14
|
+
export declare class CodexLimitResetError extends Error {
|
|
15
|
+
readonly sent: boolean;
|
|
16
|
+
constructor(message: string, sent: boolean);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Spends one Limit Reset of the Account through its own `codex app-server` (ADR-0072): the stable request
|
|
20
|
+
* `account/rateLimitResetCredit/consume` with the idempotency key and no `creditId`, so the vendor picks the credit.
|
|
21
|
+
* The Host sends it with its own login; no credential is read and nothing of the answer but its outcome is kept.
|
|
22
|
+
* Throws when the Host gave no final outcome.
|
|
23
|
+
*/
|
|
24
|
+
export declare function useCodexLimitReset(account: Account, ctx: LimitResetContext): Promise<LimitResetOutcome>;
|
|
3
25
|
export declare const codexQuotaFetch: QuotaFetch;
|
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
// Current quota through Codex's documented app-server protocol (ADR-0038)
|
|
2
|
-
// no thread, model turn, credential extraction or direct HTTP request.
|
|
1
|
+
// Current quota through Codex's documented app-server protocol (ADR-0038), and the one request that spends a Limit
|
|
2
|
+
// Reset (ADR-0072). The Host handles its own login; no thread, model turn, credential extraction or direct HTTP request.
|
|
3
3
|
import { spawn } from 'node:child_process';
|
|
4
4
|
import { tmpdir } from 'node:os';
|
|
5
5
|
import { createInterface } from 'node:readline';
|
|
6
6
|
import { isObj, rateLimits } from './rate-limits.js';
|
|
7
7
|
const failed = (detail) => ({ status: 'unavailable', reason: 'fetch-failed', detail });
|
|
8
|
+
/**
|
|
9
|
+
* The Limit Resets the answer tells (`rateLimitResetCredits`, ADR-0072), or undefined when it does not tell: the
|
|
10
|
+
* field is missing or null, or its count is not a whole number. `availableCount` is the count; the detail rows may be
|
|
11
|
+
* missing or cut short, so the next expiry is kept only when they cover every available credit. No credit id, title
|
|
12
|
+
* or description is kept.
|
|
13
|
+
*/
|
|
14
|
+
export function limitResetsOf(raw, observedAt) {
|
|
15
|
+
if (!isObj(raw))
|
|
16
|
+
return undefined;
|
|
17
|
+
const available = raw.availableCount;
|
|
18
|
+
if (typeof available !== 'number' || !Number.isInteger(available) || available < 0)
|
|
19
|
+
return undefined;
|
|
20
|
+
const out = { available, observedAt };
|
|
21
|
+
const rows = raw.credits;
|
|
22
|
+
if (available === 0 || !Array.isArray(rows))
|
|
23
|
+
return out;
|
|
24
|
+
const open = rows.filter((c) => isObj(c) && c.status === 'available');
|
|
25
|
+
if (open.length < available)
|
|
26
|
+
return out;
|
|
27
|
+
const expiries = open
|
|
28
|
+
.map((c) => (isObj(c) ? c.expiresAt : null))
|
|
29
|
+
.filter((t) => typeof t === 'number' && Number.isFinite(t) && t > 0);
|
|
30
|
+
if (expiries.length)
|
|
31
|
+
out.nextExpiresAt = new Date(Math.min(...expiries) * 1000).toISOString();
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
8
34
|
/** Prefer the general Codex bucket; a model-specific bucket is not the account's general quota. */
|
|
9
35
|
function quotaAnswer(result) {
|
|
10
36
|
if (!isObj(result))
|
|
@@ -30,16 +56,24 @@ function quotaAnswer(result) {
|
|
|
30
56
|
});
|
|
31
57
|
if (!parsed)
|
|
32
58
|
return failed('Codex returned an unrecognized quota; update Codex, then refresh');
|
|
59
|
+
const observedAt = new Date().toISOString();
|
|
60
|
+
const limitResets = limitResetsOf(result.rateLimitResetCredits, observedAt);
|
|
33
61
|
return {
|
|
34
62
|
status: 'ok',
|
|
35
63
|
...parsed,
|
|
36
|
-
observedAt
|
|
64
|
+
observedAt,
|
|
37
65
|
source: 'codex app-server account/rateLimits/read',
|
|
66
|
+
...(limitResets ? { limitResets } : {}),
|
|
38
67
|
};
|
|
39
68
|
}
|
|
40
|
-
|
|
69
|
+
/**
|
|
70
|
+
* One request to the Account's own `codex app-server`: starts it with the Account's launch environment, initializes,
|
|
71
|
+
* sends `request` and resolves with its answer. Only this short-lived process is ended afterwards, never a session.
|
|
72
|
+
* `sent` is called once the request has been written, so a caller can tell "never sent" from "sent, no answer".
|
|
73
|
+
*/
|
|
74
|
+
function askAppServer(account, ctx, request, sent) {
|
|
41
75
|
if (ctx.signal.aborted)
|
|
42
|
-
return
|
|
76
|
+
return Promise.resolve({ ok: false, why: 'stopped' });
|
|
43
77
|
const args = [...(!account.isMain ? ['-c', 'cli_auth_credentials_store="file"'] : []), 'app-server'];
|
|
44
78
|
const child = spawn('codex', args, {
|
|
45
79
|
cwd: tmpdir(),
|
|
@@ -54,7 +88,7 @@ export async function fetchCodexQuota(account, ctx) {
|
|
|
54
88
|
if (child.exitCode === null && child.signalCode === null)
|
|
55
89
|
child.kill('SIGTERM');
|
|
56
90
|
};
|
|
57
|
-
const settle = (
|
|
91
|
+
const settle = (a) => {
|
|
58
92
|
if (done)
|
|
59
93
|
return;
|
|
60
94
|
done = true;
|
|
@@ -65,18 +99,17 @@ export async function fetchCodexQuota(account, ctx) {
|
|
|
65
99
|
const timer = setTimeout(stop, 1000);
|
|
66
100
|
timer.unref();
|
|
67
101
|
child.once('close', () => clearTimeout(timer));
|
|
68
|
-
resolve(
|
|
102
|
+
resolve(a);
|
|
69
103
|
};
|
|
104
|
+
const fail = (why) => settle({ ok: false, why });
|
|
70
105
|
const abort = () => {
|
|
71
106
|
stop();
|
|
72
|
-
|
|
107
|
+
fail('stopped');
|
|
73
108
|
};
|
|
74
|
-
const send = (
|
|
75
|
-
child.stdin.on('error', () =>
|
|
76
|
-
child.on('error', (err) =>
|
|
77
|
-
|
|
78
|
-
: 'could not start Codex; check its installation, then refresh')));
|
|
79
|
-
child.on('close', () => settle(failed('Codex exited without answering; update Codex, then refresh')));
|
|
109
|
+
const send = (message) => child.stdin.write(`${JSON.stringify(message)}\n`);
|
|
110
|
+
child.stdin.on('error', () => fail('closed'));
|
|
111
|
+
child.on('error', (err) => fail(err.code === 'ENOENT' ? 'not-installed' : 'no-start'));
|
|
112
|
+
child.on('close', () => fail('exited'));
|
|
80
113
|
ctx.signal.addEventListener('abort', abort, { once: true });
|
|
81
114
|
lines.on('line', (line) => {
|
|
82
115
|
if (done)
|
|
@@ -91,19 +124,17 @@ export async function fetchCodexQuota(account, ctx) {
|
|
|
91
124
|
if (!isObj(msg) || msg.id !== pending)
|
|
92
125
|
return;
|
|
93
126
|
if (isObj(msg.error)) {
|
|
94
|
-
// Never
|
|
95
|
-
|
|
96
|
-
? 'this Codex version cannot report quota; update Codex, then refresh'
|
|
97
|
-
: `Codex could not read quota; refresh or run \`sideby login ${account.ref}\` if signed out`;
|
|
98
|
-
settle(failed(detail));
|
|
127
|
+
// Never keep the Host's raw error: it may contain credentials or account identifiers.
|
|
128
|
+
fail(msg.error.code === -32601 ? 'unsupported' : pending === 1 ? 'refused' : 'error');
|
|
99
129
|
}
|
|
100
130
|
else if (pending === 1) {
|
|
101
131
|
pending = 2;
|
|
102
132
|
send({ method: 'initialized' });
|
|
103
|
-
send({ id: 2,
|
|
133
|
+
send({ id: 2, ...request });
|
|
134
|
+
sent?.();
|
|
104
135
|
}
|
|
105
136
|
else {
|
|
106
|
-
settle(
|
|
137
|
+
settle({ ok: true, result: msg.result });
|
|
107
138
|
}
|
|
108
139
|
});
|
|
109
140
|
send({
|
|
@@ -115,6 +146,76 @@ export async function fetchCodexQuota(account, ctx) {
|
|
|
115
146
|
abort();
|
|
116
147
|
});
|
|
117
148
|
}
|
|
149
|
+
export async function fetchCodexQuota(account, ctx) {
|
|
150
|
+
const a = await askAppServer(account, ctx, { method: 'account/rateLimits/read' });
|
|
151
|
+
if (a.ok)
|
|
152
|
+
return quotaAnswer(a.result);
|
|
153
|
+
switch (a.why) {
|
|
154
|
+
case 'stopped':
|
|
155
|
+
return failed('the Codex quota check was stopped; refresh to try again');
|
|
156
|
+
case 'not-installed':
|
|
157
|
+
return failed('Codex is not installed or not on PATH; install Codex, then refresh');
|
|
158
|
+
case 'no-start':
|
|
159
|
+
return failed('could not start Codex; check its installation, then refresh');
|
|
160
|
+
case 'closed':
|
|
161
|
+
return failed('Codex closed the quota query; refresh to try again');
|
|
162
|
+
case 'exited':
|
|
163
|
+
return failed('Codex exited without answering; update Codex, then refresh');
|
|
164
|
+
case 'unsupported':
|
|
165
|
+
return failed('this Codex version cannot report quota; update Codex, then refresh');
|
|
166
|
+
default:
|
|
167
|
+
return failed(`Codex could not read quota; refresh or run \`sideby login ${account.ref}\` if signed out`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const CONSUME = 'account/rateLimitResetCredit/consume';
|
|
171
|
+
const OUTCOMES = {
|
|
172
|
+
reset: 'reset',
|
|
173
|
+
nothingToReset: 'nothing-to-reset',
|
|
174
|
+
noCredit: 'none-left',
|
|
175
|
+
alreadyRedeemed: 'already-done',
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* A spend that got no final answer. `sent` says whether the request left sideby: false means the Host never got it,
|
|
179
|
+
* so nothing was spent; true means the outcome is unknown and only the same idempotency key may ask again.
|
|
180
|
+
*/
|
|
181
|
+
export class CodexLimitResetError extends Error {
|
|
182
|
+
sent;
|
|
183
|
+
constructor(message, sent) {
|
|
184
|
+
super(message);
|
|
185
|
+
this.sent = sent;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Spends one Limit Reset of the Account through its own `codex app-server` (ADR-0072): the stable request
|
|
190
|
+
* `account/rateLimitResetCredit/consume` with the idempotency key and no `creditId`, so the vendor picks the credit.
|
|
191
|
+
* The Host sends it with its own login; no credential is read and nothing of the answer but its outcome is kept.
|
|
192
|
+
* Throws when the Host gave no final outcome.
|
|
193
|
+
*/
|
|
194
|
+
export async function useCodexLimitReset(account, ctx) {
|
|
195
|
+
let sent = false;
|
|
196
|
+
const a = await askAppServer(account, ctx, { method: CONSUME, params: { idempotencyKey: ctx.idempotencyKey } }, () => {
|
|
197
|
+
sent = true;
|
|
198
|
+
});
|
|
199
|
+
if (a.ok) {
|
|
200
|
+
const outcome = isObj(a.result) && typeof a.result.outcome === 'string' ? OUTCOMES[a.result.outcome] : undefined;
|
|
201
|
+
if (outcome)
|
|
202
|
+
return outcome;
|
|
203
|
+
throw new CodexLimitResetError('Codex answered the reset with something sideby does not know; update sideby', true);
|
|
204
|
+
}
|
|
205
|
+
const why = {
|
|
206
|
+
stopped: 'Codex did not answer in time',
|
|
207
|
+
'not-installed': 'Codex is not installed or not on PATH; install Codex',
|
|
208
|
+
'no-start': 'could not start Codex; check its installation',
|
|
209
|
+
closed: 'Codex closed the connection before answering',
|
|
210
|
+
exited: 'Codex exited without answering',
|
|
211
|
+
unsupported: 'this Codex version cannot use a limit reset; update Codex',
|
|
212
|
+
refused: `Codex did not start the connection; run \`sideby login ${account.ref}\` if signed out`,
|
|
213
|
+
error: `Codex refused the reset; run \`sideby login ${account.ref}\` if signed out`,
|
|
214
|
+
};
|
|
215
|
+
// An error answer to the request itself is final for this attempt but not an outcome: the Host did not say whether
|
|
216
|
+
// it spent one, so it stays unresolved like a lost answer. A method the Host does not know was never run.
|
|
217
|
+
throw new CodexLimitResetError(why[a.why], sent && a.why !== 'unsupported');
|
|
218
|
+
}
|
|
118
219
|
export const codexQuotaFetch = {
|
|
119
220
|
everyMinutes: 15,
|
|
120
221
|
summary: 'Ask Codex for each account’s current quota every 15 minutes',
|
|
@@ -75,16 +75,21 @@ function run(argv) {
|
|
|
75
75
|
}
|
|
76
76
|
var forceNext = false;
|
|
77
77
|
// While a manual refresh runs: when it started (ISO, for --refreshing-since) and the cache-only snapshot in flight.
|
|
78
|
-
var refreshSince = '', peekTask = null, peekHandle = null, peekOut = out + '.peek';
|
|
78
|
+
var refreshSince = '', peekTask = null, peekHandle = null, peekOut = out + '.peek', peekManual = false;
|
|
79
|
+
// The account whose "Use a reset…" was clicked, and whether the menu has opened since: the first snapshot after
|
|
80
|
+
// that asks it once. A manual refresh asks every account, so it settles this too.
|
|
81
|
+
var askNext = '', askDue = false;
|
|
79
82
|
function startSnapshot(force) {
|
|
80
83
|
if (task) { if (force) forceNext = true; return; }
|
|
81
84
|
force = force || forceNext;
|
|
82
85
|
forceNext = false;
|
|
86
|
+
var ask = !force && askDue ? askNext : '';
|
|
87
|
+
if (force || ask) { askNext = ''; askDue = false; }
|
|
83
88
|
if (force) refreshSince = new Date().toISOString();
|
|
84
89
|
$.NSFileManager.defaultManager.createFileAtPathContentsAttributes(out, $(), $());
|
|
85
90
|
hotkeyReported = hotkeyReport();
|
|
86
91
|
iconReported = iconStable;
|
|
87
|
-
var t = sideby(['menubar', 'snapshot', '--json', '--lang', lang].concat(force ? ['--refresh'] : [], hotkeyReported ? ['--hotkey', hotkeyStatus, '--hotkey-label', hotkeyWant.label] : [], iconReported ? ['--icon', iconReported] : []));
|
|
92
|
+
var t = sideby(['menubar', 'snapshot', '--json', '--lang', lang].concat(force ? ['--refresh'] : [], ask ? ['--ask', ask] : [], hotkeyReported ? ['--hotkey', hotkeyStatus, '--hotkey-label', hotkeyWant.label] : [], iconReported ? ['--icon', iconReported] : []));
|
|
88
93
|
handle = $.NSFileHandle.fileHandleForWritingAtPath(out);
|
|
89
94
|
t.standardOutput = handle;
|
|
90
95
|
lastStart = now();
|
|
@@ -123,20 +128,23 @@ function run(argv) {
|
|
|
123
128
|
});
|
|
124
129
|
build();
|
|
125
130
|
}
|
|
126
|
-
//
|
|
127
|
-
// numbers
|
|
131
|
+
// While a snapshot asks Hosts, a cache-only snapshot that never asks one: accounts already answered show their new
|
|
132
|
+
// numbers as they land. During a manual refresh the others say they are refreshing; during an automatic one they
|
|
133
|
+
// keep what was kept, and it starts only once the snapshot has run for a while (it is asking). At most one at a time.
|
|
128
134
|
function peek() {
|
|
129
135
|
if (task && !task.running) { finishSnapshot(); return; }
|
|
130
136
|
if (peekTask && !peekTask.running) {
|
|
131
137
|
var ok = peekTask.terminationStatus === 0;
|
|
132
138
|
peekTask = null;
|
|
133
139
|
closePeek();
|
|
134
|
-
var next = ok && task && manual ? readSnapshot(peekOut) : null;
|
|
140
|
+
var next = ok && task && manual === peekManual ? readSnapshot(peekOut) : null;
|
|
135
141
|
if (next) { snap = next; build(); }
|
|
136
142
|
}
|
|
137
|
-
if (peekTask || !task
|
|
143
|
+
if (peekTask || !task) return;
|
|
144
|
+
if (manual ? !refreshSince : now() - lastStart < PEEK) return;
|
|
145
|
+
peekManual = manual;
|
|
138
146
|
$.NSFileManager.defaultManager.createFileAtPathContentsAttributes(peekOut, $(), $());
|
|
139
|
-
var t = sideby(['menubar', 'snapshot', '--json', '--cached', '--
|
|
147
|
+
var t = sideby(['menubar', 'snapshot', '--json', '--cached', '--lang', lang].concat(manual ? ['--refreshing-since', refreshSince] : []));
|
|
140
148
|
peekHandle = $.NSFileHandle.fileHandleForWritingAtPath(peekOut);
|
|
141
149
|
t.standardOutput = peekHandle;
|
|
142
150
|
if (t.launchAndReturnError(null)) peekTask = t;
|
|
@@ -377,7 +385,7 @@ function run(argv) {
|
|
|
377
385
|
return (bars[key] = $.NSAttributedString.attributedStringWithAttachment(att));
|
|
378
386
|
}
|
|
379
387
|
|
|
380
|
-
var keptW = { name: 0, reset: 0, tag: 0 };
|
|
388
|
+
var keptW = { name: 0, reset: 0, tag: 0, aside: 0 };
|
|
381
389
|
// Columns: the name, then per window its bar and percent, then the reset time. One set of tab stops lines them up
|
|
382
390
|
// across every tool; the window names sit once in each tool's header, over their column. A tool with one window
|
|
383
391
|
// draws it in the second column, under the others' long window.
|
|
@@ -392,7 +400,7 @@ function run(argv) {
|
|
|
392
400
|
// The reset column names its window ("7d", then "tomorrow 4:59 PM"), so it is as wide as its longest entry. While the
|
|
393
401
|
// menu is open it never gets narrower, so rows changing during a refresh do not make the menu jump.
|
|
394
402
|
// The times start at one tab stop after the widest window tag, so 7d and 31d rows line their times up.
|
|
395
|
-
var nameW = 0, first = false, tagW = width('7d', L.font), timeW = width('Wed 12:59 PM', L.font), plainW = 0;
|
|
403
|
+
var nameW = 0, first = false, tagW = width('7d', L.font), timeW = width('Wed 12:59 PM', L.font), plainW = 0, asideW = 0;
|
|
396
404
|
function measure(r) {
|
|
397
405
|
var c = resetCell(r);
|
|
398
406
|
if (c[0]) { tagW = Math.max(tagW, width(c[0], L.font)); timeW = Math.max(timeW, width(c[1], L.font)); }
|
|
@@ -403,17 +411,18 @@ function run(argv) {
|
|
|
403
411
|
if (s.columns[0]) first = true;
|
|
404
412
|
s.rows.concat(s.more ? s.more.rows : []).forEach(function (r) {
|
|
405
413
|
nameW = Math.max(nameW, width(r.label, L.font));
|
|
414
|
+
asideW = Math.max(asideW, width(aside(r), L.small));
|
|
406
415
|
if (r.bars) measure(r);
|
|
407
416
|
(r.groups || []).forEach(function (g) {
|
|
408
417
|
nameW = Math.max(nameW, width(GROUP_INDENT + g.label, L.font));
|
|
409
|
-
measure({ reset: g.reset, resetLabel: g.resetLabel, resetTime: g.resetTime, refreshing: r.refreshing, refreshFailed: r.refreshFailed, stale: r
|
|
418
|
+
measure({ reset: g.reset, resetLabel: g.resetLabel, resetTime: g.resetTime, refreshing: r.refreshing, refreshFailed: r.refreshFailed, stale: groupStale(r, g), note: g.reset });
|
|
410
419
|
});
|
|
411
420
|
});
|
|
412
421
|
});
|
|
413
422
|
tagW = Math.ceil(tagW) + 8;
|
|
414
423
|
var resetW = Math.max(tagW + timeW, plainW);
|
|
415
|
-
if (menuOpen) { nameW = Math.max(nameW, keptW.name); resetW = Math.max(resetW, keptW.reset); tagW = Math.max(tagW, keptW.tag); }
|
|
416
|
-
keptW = { name: nameW, reset: resetW, tag: tagW };
|
|
424
|
+
if (menuOpen) { nameW = Math.max(nameW, keptW.name); resetW = Math.max(resetW, keptW.reset); tagW = Math.max(tagW, keptW.tag); asideW = Math.max(asideW, keptW.aside); }
|
|
425
|
+
keptW = { name: nameW, reset: resetW, tag: tagW, aside: asideW };
|
|
417
426
|
var pctW = width('100%', L.mono), x = Math.ceil(nameW) + 20, stops = [], at = [];
|
|
418
427
|
function tab(pos) { stops.push($.NSTextTab.alloc.initWithTextAlignmentLocationOptions($.NSTextAlignmentLeft, pos, $())); }
|
|
419
428
|
for (var c = 0; c < (first ? 2 : 1); c++) {
|
|
@@ -423,6 +432,8 @@ function run(argv) {
|
|
|
423
432
|
}
|
|
424
433
|
tab(x);
|
|
425
434
|
tab(x + tagW);
|
|
435
|
+
// A last column for quiet remarks (a Limit Reset the account holds, how old its numbers are), only when a row has one.
|
|
436
|
+
if (asideW) tab(x + resetW + 16);
|
|
426
437
|
L.reset = x;
|
|
427
438
|
L.first = first;
|
|
428
439
|
L.para = $.NSMutableParagraphStyle.alloc.init;
|
|
@@ -432,9 +443,18 @@ function run(argv) {
|
|
|
432
443
|
L.headPara.tabStops = $(at.concat([x]).map(function (pos) {
|
|
433
444
|
return $.NSTextTab.alloc.initWithTextAlignmentLocationOptions($.NSTextAlignmentLeft, pos, $());
|
|
434
445
|
}));
|
|
435
|
-
L.width = x + resetW + 70;
|
|
446
|
+
L.width = x + resetW + 70 + (asideW ? Math.ceil(asideW) + 16 : 0);
|
|
447
|
+
L.aside = asideW > 0;
|
|
448
|
+
L.tag = tagW;
|
|
436
449
|
return L;
|
|
437
450
|
}
|
|
451
|
+
// The quiet remarks after a row's reset time: the Limit Resets it holds, then how old its numbers are.
|
|
452
|
+
function aside(r) {
|
|
453
|
+
return [r.limitResets, r.age].filter(function (x) { return x; }).join(' · ');
|
|
454
|
+
}
|
|
455
|
+
// Whether a line's numbers are old: a model group may have been read at another time than the account's own bars.
|
|
456
|
+
function barsStale(r) { return r.barsStale === undefined ? r.stale : r.barsStale; }
|
|
457
|
+
function groupStale(r, g) { return g.stale === undefined ? r.stale : g.stale; }
|
|
438
458
|
// The reset cell: [window tag, time]. The tag is drawn grey and the time in the text colour; a cell that says
|
|
439
459
|
// something else (refreshing, failed, an old reading, every window past its reset) has only grey words.
|
|
440
460
|
function resetCell(r) {
|
|
@@ -464,13 +484,18 @@ function run(argv) {
|
|
|
464
484
|
if (cell[0]) {
|
|
465
485
|
line.appendAttributedString(str('\t' + cell[0], dim));
|
|
466
486
|
line.appendAttributedString(str('\t' + cell[1], stale() ? dim : plain));
|
|
467
|
-
} else
|
|
487
|
+
} else {
|
|
488
|
+
line.appendAttributedString(str('\t' + cell[1], dim));
|
|
489
|
+
// Words shorter than the window tag stop before the time's tab stop: one more tab reaches the remarks column.
|
|
490
|
+
if (L.aside && r.aside && width(cell[1], L.font) < L.tag) line.appendAttributedString(str('\t', dim));
|
|
491
|
+
}
|
|
492
|
+
if (L.aside && r.aside) line.appendAttributedString(str('\t' + r.aside, attrs($.NSColor.secondaryLabelColor, L.small, L.para)));
|
|
468
493
|
return line;
|
|
469
494
|
}
|
|
470
495
|
function accountItem(menu, r, L) {
|
|
471
496
|
refs.push(r.ref);
|
|
472
497
|
var mi = add(menu, r.label, 'start:', refs.length);
|
|
473
|
-
mi.attributedTitle = rowTitle(r, L);
|
|
498
|
+
mi.attributedTitle = rowTitle({ label: r.label, bars: r.bars, reset: r.reset, resetLabel: r.resetLabel, resetTime: r.resetTime, refreshing: r.refreshing, refreshFailed: r.refreshFailed, stale: barsStale(r), note: r.note, aside: aside(r) }, L);
|
|
474
499
|
// An empty image as wide as the tools' logos, so every row's text starts where the headers' text does and the
|
|
475
500
|
// window names sit over their bars.
|
|
476
501
|
mi.image = GUTTER;
|
|
@@ -488,10 +513,18 @@ function run(argv) {
|
|
|
488
513
|
(r.groups || []).forEach(function (g) {
|
|
489
514
|
refs.push(r.ref);
|
|
490
515
|
var gi = add(menu, g.label, 'start:', refs.length);
|
|
491
|
-
gi.attributedTitle = rowTitle({ label: GROUP_INDENT + g.label, bars: g.bars, reset: g.reset, resetLabel: g.resetLabel, resetTime: g.resetTime, refreshing: r.refreshing, refreshFailed: r.refreshFailed, stale: r
|
|
516
|
+
gi.attributedTitle = rowTitle({ label: GROUP_INDENT + g.label, bars: g.bars, reset: g.reset, resetLabel: g.resetLabel, resetTime: g.resetTime, refreshing: r.refreshing, refreshFailed: r.refreshFailed, stale: groupStale(r, g), note: g.reset }, L);
|
|
492
517
|
gi.image = GUTTER;
|
|
493
518
|
gi.toolTip = g.tip;
|
|
494
519
|
});
|
|
520
|
+
// The way to using a Limit Reset (ADR-0072), under the account: it opens a page and uses nothing by itself.
|
|
521
|
+
if (r.useReset) {
|
|
522
|
+
refs.push(r.ref);
|
|
523
|
+
var ui = add(menu, GROUP_INDENT + r.useReset.title, 'useReset:', refs.length);
|
|
524
|
+
ui.attributedTitle = str(GROUP_INDENT + r.useReset.title, attrs($.NSColor.secondaryLabelColor, L.font, L.para));
|
|
525
|
+
ui.image = GUTTER;
|
|
526
|
+
ui.toolTip = r.useReset.tip;
|
|
527
|
+
}
|
|
495
528
|
return mi;
|
|
496
529
|
}
|
|
497
530
|
// A tool's header: its mark, its name, and the window names over the bar columns. A view, not a title: macOS dims
|
|
@@ -643,7 +676,15 @@ function run(argv) {
|
|
|
643
676
|
name: 'SidebyMenuBar',
|
|
644
677
|
protocols: ['NSMenuDelegate'],
|
|
645
678
|
methods: {
|
|
646
|
-
'menuWillOpen:': {
|
|
679
|
+
'menuWillOpen:': {
|
|
680
|
+
types: ['void', ['id']],
|
|
681
|
+
implementation: function () {
|
|
682
|
+
menuOpen = true;
|
|
683
|
+
try { unregisterHotkey(); } catch (e) {}
|
|
684
|
+
// After "Use a reset…": ask that one account once, now that the menu is looked at again.
|
|
685
|
+
if (askNext) { askDue = true; try { startSnapshot(); } catch (e) {} }
|
|
686
|
+
},
|
|
687
|
+
},
|
|
647
688
|
'menuDidClose:': {
|
|
648
689
|
types: ['void', ['id']],
|
|
649
690
|
implementation: function () { menuOpen = false; closedAt = uptime(); lastUse = now(); try { registerHotkey(); } catch (e) {} },
|
|
@@ -660,7 +701,7 @@ function run(argv) {
|
|
|
660
701
|
if (task && !task.running) finishSnapshot();
|
|
661
702
|
lookAtIcon();
|
|
662
703
|
// A shortcut that just registered or failed, and an icon that just showed or went, are told at once.
|
|
663
|
-
if (!task && (forceNext || now() - lastStart >= SNAPSHOT || hotkeyReport() !== hotkeyReported || iconStable !== iconReported)) startSnapshot();
|
|
704
|
+
if (!task && (forceNext || askDue || now() - lastStart >= SNAPSHOT || hotkeyReport() !== hotkeyReported || iconStable !== iconReported)) startSnapshot();
|
|
664
705
|
// The bars are pictures in the colours of the moment; redraw them when light or dark mode changes.
|
|
665
706
|
if (ObjC.unwrap(app.effectiveAppearance.name) !== appearance) build();
|
|
666
707
|
else drawTitle();
|
|
@@ -714,6 +755,17 @@ function run(argv) {
|
|
|
714
755
|
if (ref) act(['menubar', 'start', ref]);
|
|
715
756
|
},
|
|
716
757
|
},
|
|
758
|
+
// "Use a reset…": the command opens the Panel on that account's confirmation, or the vendor's page where only
|
|
759
|
+
// that can use one. The numbers may change there, so the next time the menu opens that account is asked once.
|
|
760
|
+
'useReset:': {
|
|
761
|
+
types: ['void', ['id']],
|
|
762
|
+
implementation: function (sender) {
|
|
763
|
+
var ref = refs[sender.tag - 1];
|
|
764
|
+
if (!ref) return;
|
|
765
|
+
askNext = ref;
|
|
766
|
+
act(['menubar', 'reset', ref]);
|
|
767
|
+
},
|
|
768
|
+
},
|
|
717
769
|
},
|
|
718
770
|
});
|
|
719
771
|
var target = $.SidebyMenuBar.alloc.init;
|
|
@@ -22,8 +22,13 @@ export interface MenubarRow {
|
|
|
22
22
|
recommended: boolean;
|
|
23
23
|
/** When the quota was observed, independently of the menu's check time; null without quota. */
|
|
24
24
|
observedAt: string | null;
|
|
25
|
-
/** An old or undated reading, or a failed quota query. */
|
|
25
|
+
/** An old or undated reading, or a failed quota query; true when any of the row's windows is. */
|
|
26
26
|
stale: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Whether the account's own bars are old, when that differs from `stale`: a model group under them was read at
|
|
29
|
+
* another time (ADR-0073). Left out, `stale` stands for them.
|
|
30
|
+
*/
|
|
31
|
+
barsStale?: boolean;
|
|
27
32
|
/** Hover text: when the numbers were read, each window's use and reset, and what a click does. */
|
|
28
33
|
tip: string;
|
|
29
34
|
/**
|
|
@@ -39,6 +44,8 @@ export interface MenubarRow {
|
|
|
39
44
|
label: string;
|
|
40
45
|
bars: MenubarRow['bars'];
|
|
41
46
|
tip: string;
|
|
47
|
+
/** Whether this group's numbers are old by their own read time (ADR-0073); left out, the row's `stale` stands. */
|
|
48
|
+
stale?: boolean;
|
|
42
49
|
} & Pick<MenubarRow, 'reset' | 'resetLabel' | 'resetTime'>)[] | null;
|
|
43
50
|
/**
|
|
44
51
|
* Which window resets and when, such as `7d · tomorrow 4:59 PM`: the window closest to its own red level (85% for 5
|
|
@@ -59,6 +66,23 @@ export interface MenubarRow {
|
|
|
59
66
|
refreshFailed?: true;
|
|
60
67
|
/** A no-quota explanation, or an old-reading warning beside the bars. */
|
|
61
68
|
note: string;
|
|
69
|
+
/** The Limit Resets the account holds, in words (`1 reset available`, ADR-0072); left out for none or unknown. */
|
|
70
|
+
limitResets?: string;
|
|
71
|
+
/**
|
|
72
|
+
* A line under the account that leads to using a Limit Reset (spec §3.40, ADR-0072): its words and hover text.
|
|
73
|
+
* `sideby menubar reset <account>` is what a click runs; it opens a page and never uses one itself. Present for an
|
|
74
|
+
* Account that holds one where the Host can use it (Codex), and for one with a window at its warning level where
|
|
75
|
+
* only the vendor's page can (Claude Code). Left out otherwise.
|
|
76
|
+
*/
|
|
77
|
+
useReset?: {
|
|
78
|
+
title: string;
|
|
79
|
+
tip: string;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* How old the numbers are, in words (`read 25m ago`), once they are older than the Family's check interval or old
|
|
83
|
+
* (ADR-0073); left out while they are current. The hover text always says when they were read.
|
|
84
|
+
*/
|
|
85
|
+
age?: string;
|
|
62
86
|
}
|
|
63
87
|
export interface MenubarSection {
|
|
64
88
|
family: string;
|
|
@@ -113,6 +137,14 @@ export interface MenubarFamilyInput {
|
|
|
113
137
|
* changes only when a session runs in the account; old readings then say so. Default true.
|
|
114
138
|
*/
|
|
115
139
|
refreshable?: boolean;
|
|
140
|
+
/** How often the Family asks its Host for Quota (`fetchQuota.everyMinutes`); the age shows once a reading is older. */
|
|
141
|
+
everyMinutes?: number;
|
|
142
|
+
/**
|
|
143
|
+
* How a Limit Reset is used in this Family (`FamilyDef.limitReset`): `use` when the Host's CLI can, `page` when only
|
|
144
|
+
* the vendor's page can, with that page's host name in `limitResetSite`. Left out when it offers neither.
|
|
145
|
+
*/
|
|
146
|
+
limitReset?: 'use' | 'page';
|
|
147
|
+
limitResetSite?: string;
|
|
116
148
|
/** Its Handoff ranking (`sideby next`); null when the Family has no Quota source. */
|
|
117
149
|
plan: HandoffPlan | null;
|
|
118
150
|
/** In Display Order. */
|