sideby 0.35.9 → 0.36.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/dist/build.json +1 -1
- package/dist/src/cli/commands.d.ts +12 -0
- package/dist/src/cli/commands.js +489 -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 +187 -0
- package/dist/src/core/advisor.js +519 -0
- package/dist/src/core/browser-profiles.d.ts +16 -0
- package/dist/src/core/browser-profiles.js +45 -0
- package/dist/src/core/claude-settings-file.d.ts +14 -0
- package/dist/src/core/claude-settings-file.js +62 -23
- 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/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 +134 -12
- 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 +442 -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 +65 -1
- package/dist/src/runtime.js +530 -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
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
// Advisor (spec §3.41, ADR-0074): Claude Code's advisor lets a session ask a stronger model for advice at key moments.
|
|
2
|
+
// sideby reads two things of each Claude subscription Account's `settings.json` (grouped by the real file, as Remote
|
|
3
|
+
// Control does, ADR-0017): the top-level `advisorModel`, and whether `env.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` is
|
|
4
|
+
// true, which makes Claude Code hide the advisor. Choosing a model writes `advisorModel` and, where that variable stands
|
|
5
|
+
// in the way, removes it, in one confirmed write with one undo record. No other key or `env` entry is ever read into an
|
|
6
|
+
// output, an error or the undo record; writes go through `writeIfUnchanged`, keep every other key and `env` entry, and
|
|
7
|
+
// never create, replace or retarget a link. While config `advisor.unlock` is on, sideby also unlocks the advisor at
|
|
8
|
+
// launch with one environment variable. API Accounts that share such a file keep the variable another way: the same
|
|
9
|
+
// change turns config `claude.apiBetasOff` on, and sideby then sets it when it starts them (gateways often reject the
|
|
10
|
+
// headers experimental features send).
|
|
11
|
+
import { rm } from 'node:fs/promises';
|
|
12
|
+
import { isObj, keyRecordPath, lockedSettingsChange, readKeyRecord, readSettingsGroups, writeKeyRecord, writeSettingsKey, } from './claude-settings-file.js';
|
|
13
|
+
import { editConfigAdvisor, editConfigClaude } from './config.js';
|
|
14
|
+
import { UserError } from './errors.js';
|
|
15
|
+
export const ADVISOR_KEY = 'advisorModel';
|
|
16
|
+
/** The aliases Claude Code's `/advisor` takes, in the order the Panel and the CLI show them. */
|
|
17
|
+
export const ADVISOR_MODELS = ['fable', 'opus', 'sonnet'];
|
|
18
|
+
export const ADVISOR_CHOICES = [...ADVISOR_MODELS, 'off'];
|
|
19
|
+
export const isAdvisorChoice = (v) => typeof v === 'string' && ADVISOR_CHOICES.includes(v);
|
|
20
|
+
/** Set at launch while config `advisor.unlock` is on: Claude Code then offers the advisor without waiting for Anthropic's rollout. */
|
|
21
|
+
export const ADVISOR_UNLOCK_VAR = 'CLAUDE_CODE_ENABLE_EXPERIMENTAL_ADVISOR_TOOL';
|
|
22
|
+
/**
|
|
23
|
+
* Turns off every experimental feature of Claude Code, the advisor among them, also with `ADVISOR_UNLOCK_VAR` set
|
|
24
|
+
* (tried with Claude Code 2.1.322 on 2026-11-13). Users often keep it in a settings file's `env`.
|
|
25
|
+
*/
|
|
26
|
+
export const BETAS_VAR = 'CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS';
|
|
27
|
+
/** Variables that make Claude Code hide the advisor whatever else is set. sideby never changes them in the shell. */
|
|
28
|
+
export const ADVISOR_OFF_VARS = ['CLAUDE_CODE_DISABLE_ADVISOR_TOOL', BETAS_VAR];
|
|
29
|
+
const TITLE = { fable: 'Fable', opus: 'Opus', sonnet: 'Sonnet' };
|
|
30
|
+
const RECORDS = 'advisor';
|
|
31
|
+
const ENV_NOT_OBJECT = 'settings.json has an `env` that is not a JSON object; fix it in the file, then try again';
|
|
32
|
+
/** Whether `obj` still holds what `seen` showed: the same `advisorModel` reading and the same true env entry. */
|
|
33
|
+
function looksAsSeen(obj, seen) {
|
|
34
|
+
const now = stateOf(obj[ADVISOR_KEY]);
|
|
35
|
+
const betas = isObj(obj.env) ? obj.env[BETAS_VAR] : undefined;
|
|
36
|
+
const value = truthy(betas) ? JSON.stringify(betas) : undefined;
|
|
37
|
+
return now.state === seen.state && now.model === seen.model && value === seen.betasValue;
|
|
38
|
+
}
|
|
39
|
+
const CHANGED_SINCE = 'settings.json changed after sideby read it, so nothing was changed. Look again and try again.';
|
|
40
|
+
const MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:@[\]-]{0,99}$/;
|
|
41
|
+
function stateOf(v) {
|
|
42
|
+
if (v === undefined)
|
|
43
|
+
return { state: 'off' };
|
|
44
|
+
if (typeof v !== 'string')
|
|
45
|
+
return { state: 'other' };
|
|
46
|
+
const text = v.trim();
|
|
47
|
+
if (!text)
|
|
48
|
+
return { state: 'off' };
|
|
49
|
+
const alias = text.toLowerCase();
|
|
50
|
+
if (ADVISOR_MODELS.includes(alias))
|
|
51
|
+
return { state: alias };
|
|
52
|
+
return MODEL_ID.test(text) ? { state: 'other', model: text } : { state: 'other' };
|
|
53
|
+
}
|
|
54
|
+
/** Claude Code's own reading of an environment value as true. */
|
|
55
|
+
function truthy(v) {
|
|
56
|
+
if (v === true)
|
|
57
|
+
return true;
|
|
58
|
+
return typeof v === 'string' && ['1', 'true', 'yes', 'on'].includes(v.trim().toLowerCase());
|
|
59
|
+
}
|
|
60
|
+
/** The first variable of `ADVISOR_OFF_VARS` that `env` sets to a true value, or undefined. */
|
|
61
|
+
export function advisorOffVar(env) {
|
|
62
|
+
return ADVISOR_OFF_VARS.find((v) => truthy(env[v]));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Unlocks the advisor in `env` for a Claude subscription Account's Launch: sets `ADVISOR_UNLOCK_VAR` unless the
|
|
66
|
+
* environment already sets it or one of `ADVISOR_OFF_VARS`. Returns whether it set it.
|
|
67
|
+
*/
|
|
68
|
+
export function unlockAdvisorEnv(env, familyId, account) {
|
|
69
|
+
if (familyId !== 'claude' || account.kind === 'api')
|
|
70
|
+
return false;
|
|
71
|
+
if (advisorOffVar(env) || env[ADVISOR_UNLOCK_VAR] !== undefined)
|
|
72
|
+
return false;
|
|
73
|
+
env[ADVISOR_UNLOCK_VAR] = '1';
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Config `claude.apiBetasOff` at a Claude API Account's Launch: sets `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1` unless
|
|
78
|
+
* the environment already sets it. Runs before the Account's Secret File is read, so `proxy.env` still decides.
|
|
79
|
+
* Returns whether it set it.
|
|
80
|
+
*/
|
|
81
|
+
export function apiBetasEnv(env, familyId, account) {
|
|
82
|
+
if (familyId !== 'claude' || account.kind !== 'api' || env[BETAS_VAR] !== undefined)
|
|
83
|
+
return false;
|
|
84
|
+
env[BETAS_VAR] = '1';
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
88
|
+
/** Whether `obj` still holds what the record says sideby wrote (or still lacks the `advisorModel` it removed). */
|
|
89
|
+
function holds(obj, r) {
|
|
90
|
+
return r.removed ? !Object.hasOwn(obj, ADVISOR_KEY) : same(obj[ADVISOR_KEY], r.wrote);
|
|
91
|
+
}
|
|
92
|
+
async function readRecord(stateDir, file) {
|
|
93
|
+
const r = await readKeyRecord(stateDir, RECORDS, file);
|
|
94
|
+
if (!r || typeof r.hadKey !== 'boolean')
|
|
95
|
+
return null;
|
|
96
|
+
if (r.removed !== true && typeof r.wrote !== 'string')
|
|
97
|
+
return null;
|
|
98
|
+
const api = r.apiBetas;
|
|
99
|
+
if (api !== undefined && !(isObj(api) && (api.previous === null || typeof api.previous === 'boolean')))
|
|
100
|
+
delete r.apiBetas;
|
|
101
|
+
return r;
|
|
102
|
+
}
|
|
103
|
+
/** What undo would change in `obj`, from the record of the change it undoes (`AdvisorGroup.undoModel` and on). */
|
|
104
|
+
function undoParts(obj, r) {
|
|
105
|
+
const model = r.removed ? r.hadKey : !(r.hadKey && same(r.previous, r.wrote));
|
|
106
|
+
const env = obj.env;
|
|
107
|
+
const betas = r.betas !== undefined && (env === undefined || (isObj(env) && !Object.hasOwn(env, BETAS_VAR)));
|
|
108
|
+
return {
|
|
109
|
+
...(r.apiBetas ? { undoApiBetas: true } : {}),
|
|
110
|
+
...(model ? { undoModel: r.hadKey ? JSON.stringify(r.previous) : 'removed' } : {}),
|
|
111
|
+
...(betas ? { undoBetas: JSON.stringify(r.betas?.previous) } : {}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** `obj` with `key` set to `value` at `index` among its keys (at the end without one), keeping the others' order. */
|
|
115
|
+
function insertAt(obj, key, value, index) {
|
|
116
|
+
const entries = Object.entries(obj).filter(([k]) => k !== key);
|
|
117
|
+
entries.splice(Math.min(index ?? entries.length, entries.length), 0, [key, value]);
|
|
118
|
+
return Object.fromEntries(entries);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Every Claude subscription Account's advisor, grouped by the file it lives in, with the API Accounts that share each
|
|
122
|
+
* file. Writes nothing. A file only API Accounts use is left out: the advisor needs a Claude login.
|
|
123
|
+
*/
|
|
124
|
+
export async function advisorGroups(accounts, ctx) {
|
|
125
|
+
const claude = accounts.filter((a) => a.family === 'claude');
|
|
126
|
+
const api = new Set(claude.filter((a) => a.kind === 'api').map((a) => a.ref));
|
|
127
|
+
const out = [];
|
|
128
|
+
for (const g of await readSettingsGroups(claude)) {
|
|
129
|
+
const subs = g.accounts.filter((ref) => !api.has(ref));
|
|
130
|
+
if (!subs.length)
|
|
131
|
+
continue;
|
|
132
|
+
const apiAccounts = g.accounts.filter((ref) => api.has(ref));
|
|
133
|
+
const base = { file: g.file, accounts: subs, ...(apiAccounts.length ? { apiAccounts } : {}) };
|
|
134
|
+
if ('why' in g)
|
|
135
|
+
out.push({
|
|
136
|
+
...base,
|
|
137
|
+
state: g.why === 'no-settings' ? 'missing' : 'blocked',
|
|
138
|
+
why: g.why,
|
|
139
|
+
reason: g.reason,
|
|
140
|
+
undo: false,
|
|
141
|
+
});
|
|
142
|
+
else if (g.obj.env !== undefined && !isObj(g.obj.env))
|
|
143
|
+
out.push({ ...base, state: 'blocked', why: 'unsafe', reason: ENV_NOT_OBJECT, undo: false });
|
|
144
|
+
else {
|
|
145
|
+
const betas = isObj(g.obj.env) ? g.obj.env[BETAS_VAR] : undefined;
|
|
146
|
+
const record = await readRecord(ctx.stateDir, g.file);
|
|
147
|
+
const undo = record !== null && holds(g.obj, record);
|
|
148
|
+
out.push({
|
|
149
|
+
...base,
|
|
150
|
+
...stateOf(g.obj[ADVISOR_KEY]),
|
|
151
|
+
...(truthy(betas) ? { betasOff: true, betasValue: JSON.stringify(betas) } : {}),
|
|
152
|
+
undo,
|
|
153
|
+
...(undo ? undoParts(g.obj, record) : {}),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The groups, the two sideby settings and any variable in sideby's own environment that hides the advisor. Writes
|
|
161
|
+
* nothing.
|
|
162
|
+
*/
|
|
163
|
+
export async function advisorStatus(accounts, ctx, cfg) {
|
|
164
|
+
const envOff = advisorOffVar(ctx.env);
|
|
165
|
+
return {
|
|
166
|
+
groups: await advisorGroups(accounts, ctx),
|
|
167
|
+
unlock: cfg.unlock,
|
|
168
|
+
apiBetasOff: cfg.apiBetasOff,
|
|
169
|
+
...(envOff ? { envOff } : {}),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** `account` when the advisor can be set for it; else a UserError that says what to do. */
|
|
173
|
+
export function checkAdvisorAccount(account) {
|
|
174
|
+
if (account.family !== 'claude')
|
|
175
|
+
throw new UserError(`${account.ref} is not a Claude Code account; the advisor is a Claude Code feature. Pick a Claude account, or leave out --account for all of them`);
|
|
176
|
+
if (account.kind === 'api')
|
|
177
|
+
throw new UserError(`${account.ref} is an API account, and the advisor needs a Claude login. Pick a Claude subscription account, or leave out --account for all of them`);
|
|
178
|
+
return account;
|
|
179
|
+
}
|
|
180
|
+
/** The groups `account` (a ref) uses, or every group without one. */
|
|
181
|
+
export function advisorScope(groups, account) {
|
|
182
|
+
return account ? groups.filter((g) => g.accounts.includes(account)) : [...groups];
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* What choosing `to` changes, the one rule for the CLI, the Panel's server and its page: `todo`, the groups sideby can
|
|
186
|
+
* write that do not hold `to` yet or, for a model, whose `env` turns experimental features off (also when they already
|
|
187
|
+
* name it); `unlocks`, whether config `advisor.unlock` turns on (a model, while it is off, when some group has or gets
|
|
188
|
+
* that model); and `apiBetas`, whether config `claude.apiBetasOff` turns on (while it is off, when the variable leaves
|
|
189
|
+
* a file that API Accounts also use, so they keep it). Choosing the model the files already name is still a change
|
|
190
|
+
* while either is set. Self-contained, so the Panel page runs this same function (page-script.ts).
|
|
191
|
+
*/
|
|
192
|
+
export function advisorPending(groups, to, cfg) {
|
|
193
|
+
const todo = groups.filter((g) => g.state !== 'missing' &&
|
|
194
|
+
g.state !== 'blocked' &&
|
|
195
|
+
(g.state !== to || (to !== 'off' && g.betasOff === true)));
|
|
196
|
+
const unlocks = to !== 'off' && !cfg.unlock && (todo.length > 0 || groups.some((g) => g.state === to));
|
|
197
|
+
const apiBetas = to !== 'off' &&
|
|
198
|
+
!cfg.apiBetasOff &&
|
|
199
|
+
todo.some((g) => g.betasOff === true && (g.apiAccounts?.length ?? 0) > 0);
|
|
200
|
+
return { todo, unlocks, apiBetas };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* What choosing `to` would do in `scope` (`advisorPending`), with the groups whose
|
|
204
|
+
* `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` is removed with it, the API Accounts that use those files, and the groups
|
|
205
|
+
* sideby cannot change. Nothing to do when there are no groups to write and no unlock.
|
|
206
|
+
*/
|
|
207
|
+
export function advisorPlan(scope, to, cfg) {
|
|
208
|
+
const { todo, unlocks, apiBetas } = advisorPending(scope, to, cfg);
|
|
209
|
+
const betas = to === 'off' ? [] : todo.filter((g) => g.betasOff === true);
|
|
210
|
+
return {
|
|
211
|
+
todo,
|
|
212
|
+
betas,
|
|
213
|
+
apiAccounts: betas.flatMap((g) => g.apiAccounts ?? []),
|
|
214
|
+
skipped: scope.filter((g) => g.state === 'missing' || g.state === 'blocked'),
|
|
215
|
+
unlock: unlocks,
|
|
216
|
+
apiBetas,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/** What a change says when there is nothing to do: always `ok: false`, so the CLI exits 1. */
|
|
220
|
+
export function advisorNothingToDo(action, scope) {
|
|
221
|
+
const none = (message) => ({ ok: false, results: [], unlocked: false, message });
|
|
222
|
+
if (action === 'undo')
|
|
223
|
+
return none('There is nothing from sideby to undo.');
|
|
224
|
+
if (!scope.length)
|
|
225
|
+
return none('No Claude subscription accounts found, so nothing was changed. The advisor needs a Claude login; API accounts cannot use it.');
|
|
226
|
+
const stuck = scope.find((g) => g.state === 'blocked' || g.state === 'missing');
|
|
227
|
+
if (!scope.some((g) => g.state === action))
|
|
228
|
+
return none(`Nothing was changed: ${stuck?.reason}`);
|
|
229
|
+
const already = action === 'off'
|
|
230
|
+
? 'The advisor is already off (nothing changed).'
|
|
231
|
+
: `The advisor is already ${TITLE[action]} and sideby already unlocks it at launch (nothing changed).`;
|
|
232
|
+
return none(stuck ? `${already} ${stuck.accounts.join(', ')} cannot be changed: ${stuck.reason}.` : already);
|
|
233
|
+
}
|
|
234
|
+
function doneMessage(to, betas, api) {
|
|
235
|
+
if (to === 'off')
|
|
236
|
+
return 'Done. New sessions no longer ask an advisor; restart sessions that are already running.';
|
|
237
|
+
return [
|
|
238
|
+
`Done. Sessions sideby starts ask ${TITLE[to]} for advice at key moments, which uses more quota; restart sessions that are already running.`,
|
|
239
|
+
betas ? `Removing ${BETAS_VAR} also turned Claude Code's other experimental features back on.` : '',
|
|
240
|
+
api.length
|
|
241
|
+
? `API accounts ${api.join(', ')} still get ${BETAS_VAR}=1 from sideby when it starts them (config claude.apiBetasOff), so their gateway keeps working.`
|
|
242
|
+
: '',
|
|
243
|
+
'Sessions started with plain `claude` get it only where Anthropic has turned the advisor on. If a session says "Advisor unavailable", Anthropic has not enabled it for that account.',
|
|
244
|
+
]
|
|
245
|
+
.filter(Boolean)
|
|
246
|
+
.join(' ');
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Puts config `claude.apiBetasOff` back to `previous` (null: not set, which removes the key and an emptied section),
|
|
250
|
+
* only while it still holds the `true` sideby wrote. Whether it changed it.
|
|
251
|
+
*/
|
|
252
|
+
async function restoreApiBetas(configFile, previous) {
|
|
253
|
+
const r = await editConfigClaude(configFile, (c) => {
|
|
254
|
+
if (c.apiBetasOff !== true)
|
|
255
|
+
return null;
|
|
256
|
+
const { apiBetasOff: _drop, ...rest } = c;
|
|
257
|
+
if (previous !== null)
|
|
258
|
+
return { ...rest, apiBetasOff: previous };
|
|
259
|
+
return Object.keys(rest).length ? rest : undefined;
|
|
260
|
+
});
|
|
261
|
+
return r.changed;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* One settings file's change: `advisorModel` set to `to` (`off` removes it) and, for a model, a true
|
|
265
|
+
* `env.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` removed (the `env` object stays, even when empty), in one write. Leaves
|
|
266
|
+
* an undo record of exactly those two entries, and of `apiBetas` (the earlier config `claude.apiBetasOff`) when the
|
|
267
|
+
* same change turned that on for this file's API Accounts.
|
|
268
|
+
*/
|
|
269
|
+
async function writeGroup(stateDir, g, to, apiBetas, seen = g) {
|
|
270
|
+
const dropBetas = to !== 'off';
|
|
271
|
+
const box = {};
|
|
272
|
+
const failed = await lockedSettingsChange(stateDir, g.file, async () => {
|
|
273
|
+
const failed = await writeSettingsKey(g.file, (obj) => {
|
|
274
|
+
const r = {
|
|
275
|
+
file: g.file,
|
|
276
|
+
hadKey: Object.hasOwn(obj, ADVISOR_KEY),
|
|
277
|
+
at: new Date().toISOString(),
|
|
278
|
+
};
|
|
279
|
+
if (r.hadKey)
|
|
280
|
+
r.previous = obj[ADVISOR_KEY];
|
|
281
|
+
if (to === 'off') {
|
|
282
|
+
r.removed = true;
|
|
283
|
+
if (r.hadKey)
|
|
284
|
+
r.index = Object.keys(obj).indexOf(ADVISOR_KEY);
|
|
285
|
+
delete obj[ADVISOR_KEY];
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
r.wrote = to;
|
|
289
|
+
obj[ADVISOR_KEY] = to;
|
|
290
|
+
}
|
|
291
|
+
const env = obj.env;
|
|
292
|
+
if (dropBetas && isObj(env) && truthy(env[BETAS_VAR])) {
|
|
293
|
+
r.betas = { previous: env[BETAS_VAR], index: Object.keys(env).indexOf(BETAS_VAR) };
|
|
294
|
+
obj.env = Object.fromEntries(Object.entries(env).filter(([k]) => k !== BETAS_VAR));
|
|
295
|
+
if (apiBetas && g.apiAccounts?.length)
|
|
296
|
+
r.apiBetas = apiBetas;
|
|
297
|
+
}
|
|
298
|
+
box.record = r;
|
|
299
|
+
return obj;
|
|
300
|
+
}, (obj) => {
|
|
301
|
+
if (obj.env !== undefined && !isObj(obj.env))
|
|
302
|
+
return `${ENV_NOT_OBJECT}.`;
|
|
303
|
+
const betas = dropBetas && isObj(obj.env) && truthy(obj.env[BETAS_VAR]);
|
|
304
|
+
if (stateOf(obj[ADVISOR_KEY]).state === to && !betas)
|
|
305
|
+
return 'It was set that way meanwhile; nothing changed.';
|
|
306
|
+
// Read under the lock: the write must be the one the plan (or the Panel's preview) was made for.
|
|
307
|
+
return looksAsSeen(obj, seen) ? null : CHANGED_SINCE;
|
|
308
|
+
});
|
|
309
|
+
if (!failed && box.record)
|
|
310
|
+
await writeKeyRecord(stateDir, RECORDS, g.file, box.record);
|
|
311
|
+
return failed;
|
|
312
|
+
});
|
|
313
|
+
const betas = !failed && box.record?.betas !== undefined;
|
|
314
|
+
const done = (to === 'off' ? 'Turned off.' : `Set to ${to}.`) + (betas ? ` Removed env.${BETAS_VAR}.` : '');
|
|
315
|
+
return {
|
|
316
|
+
file: g.file,
|
|
317
|
+
accounts: g.accounts,
|
|
318
|
+
ok: !failed,
|
|
319
|
+
message: failed ?? done,
|
|
320
|
+
betas,
|
|
321
|
+
api: betas && box.record?.apiBetas !== undefined,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Sets `advisorModel` to `to` (`off` removes the key) in every group of the scope that does not hold it yet, and for a
|
|
326
|
+
* model removes a true `env.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`, remembering how to undo it. Where that file also
|
|
327
|
+
* serves API Accounts, config `claude.apiBetasOff` turns on first, so they never start without the variable. A model
|
|
328
|
+
* also turns config `advisor.unlock` on when it is off, once a file was written or none needed it.
|
|
329
|
+
*/
|
|
330
|
+
export async function setAdvisor(accounts, ctx, to, opts) {
|
|
331
|
+
const scope = advisorScope(await advisorGroups(accounts, ctx), opts.account);
|
|
332
|
+
const plan = advisorPlan(scope, to, opts);
|
|
333
|
+
if (!plan.todo.length && !plan.unlock)
|
|
334
|
+
return advisorNothingToDo(to, scope);
|
|
335
|
+
let apiPrev;
|
|
336
|
+
if (plan.apiBetas) {
|
|
337
|
+
try {
|
|
338
|
+
const r = await editConfigClaude(opts.configFile, (c) => ({ ...c, apiBetasOff: true }));
|
|
339
|
+
if (r.changed)
|
|
340
|
+
apiPrev = { previous: typeof r.before?.apiBetasOff === 'boolean' ? r.before.apiBetasOff : null };
|
|
341
|
+
}
|
|
342
|
+
catch (err) {
|
|
343
|
+
return {
|
|
344
|
+
ok: false,
|
|
345
|
+
results: [],
|
|
346
|
+
unlocked: false,
|
|
347
|
+
message: `Nothing was changed: sideby could not turn on config claude.apiBetasOff, which keeps ${BETAS_VAR} for ${plan.apiAccounts.join(', ')}: ${err.message}`,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const written = [];
|
|
352
|
+
for (const g of plan.todo) {
|
|
353
|
+
const seen = opts.seen ? opts.seen.find((s) => s.file === g.file) : g;
|
|
354
|
+
if (seen)
|
|
355
|
+
written.push(await writeGroup(ctx.stateDir, g, to, apiPrev, seen));
|
|
356
|
+
else
|
|
357
|
+
written.push({
|
|
358
|
+
file: g.file,
|
|
359
|
+
accounts: g.accounts,
|
|
360
|
+
ok: false,
|
|
361
|
+
message: CHANGED_SINCE,
|
|
362
|
+
betas: false,
|
|
363
|
+
api: false,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
const results = written.map(({ betas: _b, api: _a, ...r }) => r);
|
|
367
|
+
// No file the API Accounts use lost the variable after all: the config goes back as it was.
|
|
368
|
+
const apiOn = apiPrev !== undefined && written.some((r) => r.api);
|
|
369
|
+
if (apiPrev && !apiOn)
|
|
370
|
+
await restoreApiBetas(opts.configFile, apiPrev.previous).catch(() => false);
|
|
371
|
+
let ok = results.every((r) => r.ok);
|
|
372
|
+
let unlocked = false;
|
|
373
|
+
let unlockFailed = '';
|
|
374
|
+
if (plan.unlock && (!results.length || results.some((r) => r.ok))) {
|
|
375
|
+
try {
|
|
376
|
+
unlocked = (await editConfigAdvisor(opts.configFile, (c) => ({ ...c, unlock: true }))).changed;
|
|
377
|
+
}
|
|
378
|
+
catch (err) {
|
|
379
|
+
ok = false;
|
|
380
|
+
unlockFailed = err.message;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const api = written
|
|
384
|
+
.filter((r) => r.betas)
|
|
385
|
+
.flatMap((r) => scope.find((g) => g.file === r.file)?.apiAccounts ?? []);
|
|
386
|
+
return {
|
|
387
|
+
ok,
|
|
388
|
+
results,
|
|
389
|
+
unlocked,
|
|
390
|
+
...(apiOn ? { apiBetas: true } : {}),
|
|
391
|
+
message: unlockFailed
|
|
392
|
+
? `The settings were changed, but sideby could not turn on unlocking at launch: ${unlockFailed}. Then run \`sideby advisor unlock on\`.`
|
|
393
|
+
: !ok
|
|
394
|
+
? 'Some files could not be changed; see each line.'
|
|
395
|
+
: doneMessage(to, written.some((r) => r.betas), api),
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
/** Groups sideby can put back: it wrote there and the key still holds what it wrote. */
|
|
399
|
+
export function advisorGroupsToUndo(groups) {
|
|
400
|
+
return groups.filter((g) => g.undo);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Puts back what each file had before the last change, each entry at its old place, only while `advisorModel` still
|
|
404
|
+
* holds what sideby wrote; a removed `env.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` comes back only while it is still
|
|
405
|
+
* absent. Then config `claude.apiBetasOff` goes back to its earlier value, when that change turned it on, it still
|
|
406
|
+
* holds `true` and no file the API Accounts use still lacks the variable because of sideby. Leaves config
|
|
407
|
+
* `advisor.unlock` as it is.
|
|
408
|
+
*/
|
|
409
|
+
export async function undoAdvisor(accounts, ctx, opts) {
|
|
410
|
+
const groups = await advisorGroups(accounts, ctx);
|
|
411
|
+
const todo = advisorGroupsToUndo(groups);
|
|
412
|
+
if (todo.length === 0)
|
|
413
|
+
return advisorNothingToDo('undo', []);
|
|
414
|
+
const results = [];
|
|
415
|
+
let apiPrev;
|
|
416
|
+
let apiBack = true;
|
|
417
|
+
for (const g of todo) {
|
|
418
|
+
// The record is read under the lock, so it is the one of the last change that finished.
|
|
419
|
+
const box = {};
|
|
420
|
+
let back = false;
|
|
421
|
+
const failed = await lockedSettingsChange(ctx.stateDir, g.file, async () => {
|
|
422
|
+
const record = await readRecord(ctx.stateDir, g.file);
|
|
423
|
+
if (!record)
|
|
424
|
+
return null;
|
|
425
|
+
box.record = record;
|
|
426
|
+
const failed = await writeSettingsKey(g.file, (obj) => {
|
|
427
|
+
let out = obj;
|
|
428
|
+
if (record.removed) {
|
|
429
|
+
if (record.hadKey)
|
|
430
|
+
out = insertAt(out, ADVISOR_KEY, record.previous, record.index);
|
|
431
|
+
}
|
|
432
|
+
else if (record.hadKey)
|
|
433
|
+
out[ADVISOR_KEY] = record.previous;
|
|
434
|
+
else
|
|
435
|
+
delete out[ADVISOR_KEY];
|
|
436
|
+
const b = record.betas;
|
|
437
|
+
if (b) {
|
|
438
|
+
if (out.env === undefined)
|
|
439
|
+
out.env = { [BETAS_VAR]: b.previous };
|
|
440
|
+
else if (isObj(out.env) && !Object.hasOwn(out.env, BETAS_VAR))
|
|
441
|
+
out.env = insertAt(out.env, BETAS_VAR, b.previous, b.index);
|
|
442
|
+
}
|
|
443
|
+
back = isObj(out.env) && truthy(out.env[BETAS_VAR]);
|
|
444
|
+
return out;
|
|
445
|
+
}, (obj) => holds(obj, record) ? null : 'The setting was changed after sideby set it, so it was left alone.');
|
|
446
|
+
if (!failed)
|
|
447
|
+
await rm(keyRecordPath(ctx.stateDir, RECORDS, g.file), { force: true });
|
|
448
|
+
return failed;
|
|
449
|
+
});
|
|
450
|
+
const record = box.record;
|
|
451
|
+
if (!record && !failed)
|
|
452
|
+
continue;
|
|
453
|
+
// The lock stayed held, so its record was not read: the file may still need config claude.apiBetasOff.
|
|
454
|
+
if (!record)
|
|
455
|
+
apiBack = false;
|
|
456
|
+
if (record?.apiBetas) {
|
|
457
|
+
apiPrev ??= record.apiBetas.previous;
|
|
458
|
+
if (failed || !back)
|
|
459
|
+
apiBack = false;
|
|
460
|
+
}
|
|
461
|
+
results.push({ file: g.file, accounts: g.accounts, ok: !failed, message: failed ?? 'Put back.' });
|
|
462
|
+
}
|
|
463
|
+
// A file sideby did not put back keeps its record: its API Accounts still need the variable from sideby.
|
|
464
|
+
for (const g of groups)
|
|
465
|
+
if (g.apiAccounts?.length && !todo.includes(g) && (await readRecord(ctx.stateDir, g.file))?.apiBetas)
|
|
466
|
+
apiBack = false;
|
|
467
|
+
let apiOff = false;
|
|
468
|
+
if (apiPrev !== undefined && apiBack)
|
|
469
|
+
apiOff = await restoreApiBetas(opts.configFile, apiPrev).catch(() => false);
|
|
470
|
+
const ok = results.length > 0 && results.every((r) => r.ok);
|
|
471
|
+
return {
|
|
472
|
+
ok,
|
|
473
|
+
results,
|
|
474
|
+
unlocked: false,
|
|
475
|
+
...(apiOff ? { apiBetas: false } : {}),
|
|
476
|
+
message: [
|
|
477
|
+
ok
|
|
478
|
+
? 'Undone. New sessions go back to the previous advisor setting.'
|
|
479
|
+
: 'Some files could not be put back; see each line.',
|
|
480
|
+
apiOff
|
|
481
|
+
? `sideby no longer adds ${BETAS_VAR} for API accounts; the settings file sets it for them again.`
|
|
482
|
+
: apiPrev !== undefined
|
|
483
|
+
? `API accounts still get ${BETAS_VAR} from sideby, because a file they use still lacks it. \`sideby config set claude.apiBetasOff false\` stops that.`
|
|
484
|
+
: '',
|
|
485
|
+
ok && opts.unlock
|
|
486
|
+
? 'sideby still unlocks /advisor at launch; `sideby advisor unlock off` stops that.'
|
|
487
|
+
: '',
|
|
488
|
+
]
|
|
489
|
+
.filter(Boolean)
|
|
490
|
+
.join(' '),
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Turns config `advisor.unlock` on or off. Off removes the key (and the section once it is empty). `ok: false` when it
|
|
495
|
+
* already was, so the CLI exits 1.
|
|
496
|
+
*/
|
|
497
|
+
export async function setAdvisorUnlock(configFile, on) {
|
|
498
|
+
const r = await editConfigAdvisor(configFile, (c) => {
|
|
499
|
+
if (on)
|
|
500
|
+
return { ...c, unlock: true };
|
|
501
|
+
const { unlock: _drop, ...rest } = c;
|
|
502
|
+
return Object.keys(rest).length ? rest : undefined;
|
|
503
|
+
});
|
|
504
|
+
if (!r.changed)
|
|
505
|
+
return {
|
|
506
|
+
ok: false,
|
|
507
|
+
unlock: on,
|
|
508
|
+
message: on
|
|
509
|
+
? 'sideby already unlocks /advisor at launch (nothing changed).'
|
|
510
|
+
: 'sideby already leaves /advisor to Claude Code (nothing changed).',
|
|
511
|
+
};
|
|
512
|
+
return {
|
|
513
|
+
ok: true,
|
|
514
|
+
unlock: on,
|
|
515
|
+
message: on
|
|
516
|
+
? 'Done. Claude sessions sideby starts now offer /advisor; restart sessions that are already running.'
|
|
517
|
+
: 'Done. sideby no longer unlocks /advisor; Claude Code shows it only where Anthropic has turned it on. Restart sessions that are already running.',
|
|
518
|
+
};
|
|
519
|
+
}
|
|
@@ -62,6 +62,22 @@ export interface BrowserOpenResult {
|
|
|
62
62
|
* Chrome lists in `Local State`.
|
|
63
63
|
*/
|
|
64
64
|
export declare function openInBrowser(rt: Runtime, account: Account, profile?: string): Promise<BrowserOpenResult>;
|
|
65
|
+
export interface VendorPageOpen {
|
|
66
|
+
ref: string;
|
|
67
|
+
url: string;
|
|
68
|
+
/** The Browser Profile it opened in; left out when the default browser opened it. */
|
|
69
|
+
profile?: {
|
|
70
|
+
dir: string;
|
|
71
|
+
name: string;
|
|
72
|
+
};
|
|
73
|
+
command: string[];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Opens the page where a Limit Reset of `account` is spent (`FamilyDef.limitReset.vendorPage`, ADR-0072): in the
|
|
77
|
+
* Browser Profile the user chose for the Account, or the only one found signed in to it, else in the default browser.
|
|
78
|
+
* sideby never reads which login that browser has. The url passed the loader's https check; no other is opened.
|
|
79
|
+
*/
|
|
80
|
+
export declare function openVendorPage(rt: Runtime, account: Account): Promise<VendorPageOpen>;
|
|
65
81
|
export interface BrowserChoice {
|
|
66
82
|
ref: string;
|
|
67
83
|
/** The browser user now chosen, or null when sideby finds it again by itself. */
|
|
@@ -346,6 +346,51 @@ export async function openInBrowser(rt, account, profile) {
|
|
|
346
346
|
command: [cmd, ...args],
|
|
347
347
|
};
|
|
348
348
|
}
|
|
349
|
+
/**
|
|
350
|
+
* Opens the page where a Limit Reset of `account` is spent (`FamilyDef.limitReset.vendorPage`, ADR-0072): in the
|
|
351
|
+
* Browser Profile the user chose for the Account, or the only one found signed in to it, else in the default browser.
|
|
352
|
+
* sideby never reads which login that browser has. The url passed the loader's https check; no other is opened.
|
|
353
|
+
*/
|
|
354
|
+
export async function openVendorPage(rt, account) {
|
|
355
|
+
const url = account.kind === 'api' ? undefined : rt.families.get(account.family)?.limitReset?.vendorPage;
|
|
356
|
+
if (!url)
|
|
357
|
+
throw new UserError(`${account.ref} has no page for limit resets; check the vendor's own usage page`, {
|
|
358
|
+
code: 'no-limit-reset',
|
|
359
|
+
});
|
|
360
|
+
const deps = depsFor(rt);
|
|
361
|
+
const run = async (cmd, args) => (await deps.exec(cmd, args)).code === 0;
|
|
362
|
+
// The Account's own Browser Profile, when one is known without asking; any problem falls back to the default browser.
|
|
363
|
+
// Nothing of a browser is read unless the user chose a Browser Profile for this Account or turned Browser Sign-ins on.
|
|
364
|
+
const chosen = rt.config.browser?.choices?.[account.ref];
|
|
365
|
+
const findIt = rt.config.browser?.signIns === true;
|
|
366
|
+
if ((deps.platform === 'darwin' || deps.wslMount) && (chosen || findIt)) {
|
|
367
|
+
const problems = [];
|
|
368
|
+
const launch = await chromeLauncher(deps, problems).catch(() => null);
|
|
369
|
+
const entries = launch
|
|
370
|
+
? ((await profileEntries(launch.userData ?? chromeUserData(rt.paths.home), problems)) ?? [])
|
|
371
|
+
: [];
|
|
372
|
+
let pick = chosen ? entries.find((e) => e.dir === chosen) : undefined;
|
|
373
|
+
if (launch && !pick && findIt) {
|
|
374
|
+
const found = (await browserReport(rt, [account]).catch(() => undefined))?.profiles.filter((p) => p.signIns.includes(account.ref));
|
|
375
|
+
if (found?.length === 1)
|
|
376
|
+
pick = entries.find((e) => e.dir === found[0]?.dir);
|
|
377
|
+
}
|
|
378
|
+
if (launch && pick) {
|
|
379
|
+
const [cmd, ...args] = launch.command(`--profile-directory=${pick.dir}`, url);
|
|
380
|
+
if (await run(cmd, args))
|
|
381
|
+
return {
|
|
382
|
+
ref: account.ref,
|
|
383
|
+
url,
|
|
384
|
+
profile: { dir: pick.dir, name: pick.name },
|
|
385
|
+
command: [cmd, ...args],
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const command = deps.platform === 'darwin' ? [OPEN, url] : ['xdg-open', url];
|
|
390
|
+
if (!(await run(command[0], command.slice(1))))
|
|
391
|
+
throw new UserError(`could not open a browser; open ${url} yourself`, { code: 'open-failed' });
|
|
392
|
+
return { ref: account.ref, url, command };
|
|
393
|
+
}
|
|
349
394
|
/** The browser user `profile` names: its folder, else its name or Google account, which must be unique. */
|
|
350
395
|
function findProfile(entries, profile) {
|
|
351
396
|
const byDir = entries.find((e) => e.dir === profile);
|
|
@@ -27,6 +27,20 @@ export declare function settingsFileGroups(accounts: readonly Account[]): Promis
|
|
|
27
27
|
* else what went wrong in plain words.
|
|
28
28
|
*/
|
|
29
29
|
export declare function writeSettingsKey(file: string, change: (obj: Json) => Json, expect: (obj: Json) => string | null): Promise<string | null>;
|
|
30
|
+
/** How long a change waits for another one of the same file. Mutable for tests only. */
|
|
31
|
+
export declare const settingsLockWait: {
|
|
32
|
+
timeoutMs: number;
|
|
33
|
+
pollMs: number;
|
|
34
|
+
};
|
|
35
|
+
/** What a change says when another sideby change of the same file held it for too long. */
|
|
36
|
+
export declare const SETTINGS_BUSY = "Another sideby change of this settings file is still running, so nothing was changed. Try again in a moment.";
|
|
37
|
+
/**
|
|
38
|
+
* Runs one change of `file` and its undo record while no other sideby process (the CLI, the Panel) changes that file,
|
|
39
|
+
* so a record always describes the write it follows and one change never replaces another's record halfway. The lock
|
|
40
|
+
* is `<state>/settings-locks/<hash of file>.lock`. Returns `fn`'s answer, or `SETTINGS_BUSY` when the lock stayed
|
|
41
|
+
* held.
|
|
42
|
+
*/
|
|
43
|
+
export declare function lockedSettingsChange(stateDir: string, file: string, fn: () => Promise<string | null>): Promise<string | null>;
|
|
30
44
|
/** Where sideby keeps the undo record of one key it wrote in `file`: `<state>/<dir>/<hash of file>.json`. */
|
|
31
45
|
export declare const keyRecordPath: (stateDir: string, dir: string, file: string) => string;
|
|
32
46
|
/** Writes an undo record (mode 600) under the state directory. */
|