@alexeiled/claude-router 1.26.0 → 1.27.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/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +19 -0
- package/README.md +1 -1
- package/docs/architecture.md +3 -2
- package/docs/configuration.md +65 -1
- package/docs/providers.md +9 -0
- package/docs/user-guide.md +47 -10
- package/hooks/native-router.mjs +243 -25
- package/lib/band.mjs +7 -1
- package/lib/config.mjs +72 -1
- package/lib/decisions.mjs +46 -1
- package/lib/display.mjs +3 -2
- package/lib/failover.mjs +260 -23
- package/lib/holds.mjs +129 -0
- package/lib/providers.mjs +2 -0
- package/lib/route.mjs +217 -86
- package/lib/subcommands.mjs +8 -4
- package/lib/ui/graphical/band.mjs +1 -1
- package/lib/ui/graphical/now.mjs +44 -0
- package/lib/ui/graphical/routes.mjs +45 -0
- package/lib/ui/model/band.mjs +5 -2
- package/lib/ui/model/frame.mjs +2 -2
- package/lib/ui/model/now.mjs +35 -1
- package/lib/ui/model/routes.mjs +68 -5
- package/lib/ui/terminal/band.mjs +1 -1
- package/lib/ui/terminal/now.mjs +29 -0
- package/lib/ui/terminal/routes.mjs +31 -0
- package/lib/view.mjs +5 -0
- package/package.json +1 -1
package/lib/failover.mjs
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
// API failover: a model whose request ended a turn on an API error is held out of routing for a while, so the next
|
|
2
|
-
// turn runs another model. Pure: the hook passes the error Claude Code classified (`StopFailure`'s `error`),
|
|
3
|
-
// and the clock.
|
|
2
|
+
// turn runs another model. Pure: the hook passes the error Claude Code classified (`StopFailure`'s `error`), its
|
|
3
|
+
// optional text, the plan readings, the model and the clock.
|
|
4
|
+
//
|
|
5
|
+
// A failure is one of four kinds (docs/plans/2026-11-19-fallback-chains.md). `availability` holds the model alone, for
|
|
6
|
+
// a time that doubles with each failed retry. `window`, `calendar` and `balance` are used-up quota: they hold every
|
|
7
|
+
// model of the account until the quota comes back, so a retry would only lose another turn.
|
|
8
|
+
|
|
9
|
+
import { accountOf, PLAN_ACCOUNT } from './config.mjs';
|
|
10
|
+
import { canonicalModel } from './providers.mjs';
|
|
11
|
+
import { isRecord } from './records.mjs';
|
|
4
12
|
|
|
5
13
|
const MINUTE = 60_000;
|
|
14
|
+
const DAY = 24 * 60 * MINUTE;
|
|
6
15
|
|
|
7
|
-
// How long each error holds the model out. Only errors that are the model's: busy, limited or
|
|
8
|
-
// side, or not served at all. Claude Code does not report a retry-after, so the times are
|
|
9
|
-
// found does not come back by itself; an hour bounds the hold in case it was a provider's
|
|
10
|
-
// rejected key or request, a cut-off answer and an unknown error are not the model's, so another model would
|
|
16
|
+
// How long each error holds the model out on its first failure. Only errors that are the model's: busy, limited or
|
|
17
|
+
// failing on the provider's side, or not served at all. Claude Code does not report a retry-after, so the times are
|
|
18
|
+
// fixed. A model that is not found does not come back by itself; an hour bounds the hold in case it was a provider's
|
|
19
|
+
// outage. A rejected key or request, a cut-off answer and an unknown error are not the model's, so another model would
|
|
20
|
+
// not help.
|
|
11
21
|
export const HOLD_MS = {
|
|
12
22
|
rate_limit: 5 * MINUTE,
|
|
13
23
|
overloaded: 5 * MINUTE,
|
|
@@ -15,41 +25,268 @@ export const HOLD_MS = {
|
|
|
15
25
|
model_not_found: 60 * MINUTE,
|
|
16
26
|
};
|
|
17
27
|
|
|
18
|
-
//
|
|
28
|
+
// Each failed retry of a model with the same error doubles its hold, up to an hour.
|
|
29
|
+
export const HOLD_CAP_MS = 60 * MINUTE;
|
|
30
|
+
// The plan reports a window's reset to the minute; a request a minute later finds it open.
|
|
31
|
+
const WINDOW_LAG_MS = MINUTE;
|
|
32
|
+
const WINDOW_CAP_MS = 7 * DAY;
|
|
33
|
+
// A calendar limit on an account with no configured reset is retried the next day.
|
|
34
|
+
const CALENDAR_DEFAULT_MS = DAY;
|
|
35
|
+
|
|
36
|
+
// The store key of an account's hold: `holds/<account>`. Account holds outlive a session, so every session sees them.
|
|
37
|
+
export const HOLDS_PREFIX = 'holds/';
|
|
38
|
+
export const HOLD_KINDS = ['availability', 'window', 'calendar', 'balance'];
|
|
39
|
+
|
|
40
|
+
// The words a notice uses for each error, and for each quota kind.
|
|
19
41
|
const ERROR_WORDS = {
|
|
20
42
|
rate_limit: 'rate limited',
|
|
21
43
|
overloaded: 'overloaded',
|
|
22
44
|
server_error: 'server error',
|
|
23
45
|
model_not_found: 'model not found',
|
|
24
46
|
};
|
|
47
|
+
const KIND_WORDS = { window: 'usage limit', calendar: 'spend limit', balance: 'no balance' };
|
|
25
48
|
|
|
26
|
-
/** How long `error` holds the failed model out, in milliseconds, or null when it holds nothing. */
|
|
49
|
+
/** How long `error` holds the failed model out on its first failure, in milliseconds, or null when it holds nothing. */
|
|
27
50
|
export const holdMs = (error) => (Object.hasOwn(HOLD_MS, error) ? HOLD_MS[error] : null);
|
|
28
51
|
|
|
29
|
-
|
|
52
|
+
const PLAN_WINDOWS = ['five_hour', 'seven_day'];
|
|
53
|
+
// Only these errors can be a used-up quota. Any other error with a "credit" in its text is not one: a rejected key
|
|
54
|
+
// names no balance.
|
|
55
|
+
const QUOTA_ERRORS = ['rate_limit', 'billing_error'];
|
|
56
|
+
const SPEND_LIMIT = /spend limit/i;
|
|
57
|
+
const BALANCE = /balance|recharge|credit/i;
|
|
58
|
+
|
|
59
|
+
/** @typedef {{ kind: 'availability' | 'window' | 'calendar' | 'balance' | null, scope: 'model' | 'account', until: number | null, window?: string }} Failure A `window` failure names the plan window that ended last. */
|
|
60
|
+
|
|
61
|
+
// The wall clock of instant `ms` in time zone `tz`.
|
|
62
|
+
function zoneParts(ms, tz) {
|
|
63
|
+
const format = new Intl.DateTimeFormat('en-US', {
|
|
64
|
+
timeZone: tz,
|
|
65
|
+
hourCycle: 'h23',
|
|
66
|
+
year: 'numeric',
|
|
67
|
+
month: 'numeric',
|
|
68
|
+
day: 'numeric',
|
|
69
|
+
hour: 'numeric',
|
|
70
|
+
minute: 'numeric',
|
|
71
|
+
second: 'numeric',
|
|
72
|
+
});
|
|
73
|
+
const parts = { year: 1970, month: 1, day: 1, hour: 0, minute: 0, second: 0 };
|
|
74
|
+
for (const { type, value } of format.formatToParts(ms)) if (type in parts) parts[type] = Number(value);
|
|
75
|
+
return parts;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const zoneOffset = (ms, tz) => {
|
|
79
|
+
const p = zoneParts(ms, tz);
|
|
80
|
+
return Date.UTC(p.year, p.month - 1, p.day, p.hour, p.minute, p.second) - Math.floor(ms / 1000) * 1000;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Midnight of `day` in `month` (0-based, may run into the next year) of `year` in `tz`, as an instant. The zone's
|
|
84
|
+
// offset is read at the guess and again at the result, so a change of offset between them (daylight saving) still lands
|
|
85
|
+
// on midnight. A zone with no midnight that day lands an hour off; resets fall on days 1 to 28, where that is rare, and
|
|
86
|
+
// a request an hour early or late is only a retry.
|
|
87
|
+
function zoneMidnight(year, month, day, tz) {
|
|
88
|
+
const guess = Date.UTC(year, month, day);
|
|
89
|
+
return guess - zoneOffset(guess - zoneOffset(guess, tz), tz);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The next instant after `now` at which `reset` (`{ monthly, tz }`) starts a new month of the account's quota. */
|
|
93
|
+
export function nextReset(reset, now) {
|
|
94
|
+
const { year, month } = zoneParts(now, reset.tz);
|
|
95
|
+
const thisMonth = zoneMidnight(year, month - 1, reset.monthly, reset.tz);
|
|
96
|
+
return thisMonth > now ? thisMonth : zoneMidnight(year, month, reset.monthly, reset.tz);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Why a main request failed, and what to hold. `limits` are the plan windows as planLimits reads them (`resetsAt` in
|
|
101
|
+
* ms), `billing` how the session pays (billingOf), `planAccounts` the accounts of the plan-billed models, `account` the
|
|
102
|
+
* failed model's account (null when no configured model ran it), `accounts` the configured accounts. Only the Claude plan
|
|
103
|
+
* reports window readings, so a full window is the failed model's only when every plan-billed model is in the plan
|
|
104
|
+
* account; with plan models in two accounts the reading cannot say which one it is. A session that also pays credits
|
|
105
|
+
* still has the plan's windows. The evidence is tried in order: the plan readings, the text Claude Code
|
|
106
|
+
* gave (absent on some builds), then the error word. Reset times come only from readings or config, never from text.
|
|
107
|
+
* `until` is when an account's hold ends; a model hold's end depends on its retries (failureHold), and a balance has
|
|
108
|
+
* none, so both read null.
|
|
109
|
+
* @returns {Failure}
|
|
110
|
+
*/
|
|
111
|
+
export function classifyFailure({ error, details, limits, billing, planAccounts, account, accounts, now }) {
|
|
112
|
+
/** @type {Failure} */
|
|
113
|
+
const none = { kind: null, scope: 'model', until: null };
|
|
114
|
+
const isPlan = (name) => name === PLAN_ACCOUNT;
|
|
115
|
+
/** @returns {Failure} */
|
|
116
|
+
const quota = (kind, until) => (account === null ? none : { kind, scope: 'account', until });
|
|
117
|
+
if (error === 'rate_limit' && account === PLAN_ACCOUNT && billing !== 'billed' && planAccounts.every(isPlan)) {
|
|
118
|
+
const used = (limits ?? []).filter(
|
|
119
|
+
(limit) => PLAN_WINDOWS.includes(limit.kind) && limit.percent >= 100 && limit.resetsAt > now,
|
|
120
|
+
);
|
|
121
|
+
const last = used.reduce((a, b) => (b.resetsAt > a.resetsAt ? b : a), used[0]);
|
|
122
|
+
if (last)
|
|
123
|
+
return {
|
|
124
|
+
...quota('window', Math.min(last.resetsAt + WINDOW_LAG_MS, now + WINDOW_CAP_MS)),
|
|
125
|
+
window: last.kind,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (QUOTA_ERRORS.includes(error) && typeof details === 'string') {
|
|
129
|
+
if (error === 'rate_limit' && SPEND_LIMIT.test(details)) {
|
|
130
|
+
const reset = account === null ? undefined : accounts?.[account]?.reset;
|
|
131
|
+
return quota('calendar', reset ? nextReset(reset, now) : now + CALENDAR_DEFAULT_MS);
|
|
132
|
+
}
|
|
133
|
+
if (BALANCE.test(details)) return quota('balance', null);
|
|
134
|
+
}
|
|
135
|
+
if (error === 'billing_error') return quota('balance', null);
|
|
136
|
+
if (holdMs(error) !== null) return { kind: 'availability', scope: 'model', until: null };
|
|
137
|
+
return none;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @typedef {{ kind: string, error: string, since: number, until: number | null, streak: number, window?: string }} Hold
|
|
142
|
+
* A hold: why, since when, until when (null: until released) and how many failures in a row (a quota hold counts one).
|
|
143
|
+
* A window hold also names the plan window it waits for.
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
// A hold with no `until` never ends by itself.
|
|
147
|
+
const endOf = (hold) => hold.until ?? Number.POSITIVE_INFINITY;
|
|
148
|
+
export const isActive = (hold, now) => hold.until === null || hold.until > now;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The hold a failure puts on its scope, given the hold `running` there (active or not), or null when it holds nothing.
|
|
152
|
+
* A model that fails again after its hold ended with the same error doubles the time, up to an hour; a failure during a
|
|
153
|
+
* hold is not a retry and leaves it. A quota hold is never doubled. A hold already running that ends later stays.
|
|
154
|
+
* @param {Failure} failure
|
|
155
|
+
* @param {Hold | undefined} running
|
|
156
|
+
* @returns {Hold | null}
|
|
157
|
+
*/
|
|
158
|
+
export function failureHold(failure, error, running, now) {
|
|
159
|
+
if (failure.kind === null) return null;
|
|
160
|
+
const active = running && isActive(running, now) ? running : null;
|
|
161
|
+
/** @type {Hold} */
|
|
162
|
+
let hold;
|
|
163
|
+
if (failure.kind === 'availability') {
|
|
164
|
+
const base = holdMs(error);
|
|
165
|
+
if (base === null) return null;
|
|
166
|
+
const retry = running?.kind === 'availability' && running.error === error;
|
|
167
|
+
if (active && retry) return active;
|
|
168
|
+
const streak = retry ? running.streak + 1 : 1;
|
|
169
|
+
hold = {
|
|
170
|
+
kind: 'availability',
|
|
171
|
+
error,
|
|
172
|
+
since: now,
|
|
173
|
+
until: now + Math.min(base * 2 ** (streak - 1), HOLD_CAP_MS),
|
|
174
|
+
streak,
|
|
175
|
+
};
|
|
176
|
+
} else
|
|
177
|
+
hold = {
|
|
178
|
+
kind: failure.kind,
|
|
179
|
+
error,
|
|
180
|
+
since: now,
|
|
181
|
+
until: failure.until,
|
|
182
|
+
streak: 1,
|
|
183
|
+
...(failure.window ? { window: failure.window } : {}),
|
|
184
|
+
};
|
|
185
|
+
return active && endOf(active) >= endOf(hold) ? active : hold;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The holds still running at `now`. */
|
|
30
189
|
export function activeHolds(held, now) {
|
|
31
|
-
return Object.fromEntries(Object.entries(held ?? {}).filter(([, hold]) => hold
|
|
190
|
+
return Object.fromEntries(Object.entries(held ?? {}).filter(([, hold]) => isActive(hold, now)));
|
|
32
191
|
}
|
|
33
192
|
|
|
34
193
|
/** The model ids held out at `now`. */
|
|
35
194
|
export const heldModels = (held, now) => Object.keys(activeHolds(held, now));
|
|
36
195
|
|
|
37
196
|
/**
|
|
38
|
-
* The holds after `model` failed with `error` at `now
|
|
39
|
-
*
|
|
40
|
-
* @
|
|
197
|
+
* The model holds after `model` failed with `error` at `now`. A hold that ended stays, as the memory of how many
|
|
198
|
+
* retries failed, until the model answers (clearedModel). A failure that holds nothing changes no hold.
|
|
199
|
+
* @param {Record<string, Hold> | undefined} held
|
|
200
|
+
* @param {Failure} failure
|
|
201
|
+
* @returns {Record<string, Hold>}
|
|
202
|
+
*/
|
|
203
|
+
export function holdModel(held, model, failure, error, now) {
|
|
204
|
+
const hold = typeof model === 'string' && model ? failureHold(failure, error, held?.[model], now) : null;
|
|
205
|
+
return hold ? { ...held, [model]: hold } : (held ?? {});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const sameKey = (a, b) => canonicalModel(a) === canonicalModel(b);
|
|
209
|
+
|
|
210
|
+
/** The model holds without `model`'s, which answered: its streak starts over. The same object when it had none. */
|
|
211
|
+
export function clearedModel(held, model) {
|
|
212
|
+
if (!held || !Object.keys(held).some((key) => sameKey(key, model))) return held;
|
|
213
|
+
return Object.fromEntries(Object.entries(held).filter(([key]) => !sameKey(key, model)));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The holds without `key`: a release of a model or an account. The same object when it held nothing. */
|
|
217
|
+
export function releaseHold(holds, key) {
|
|
218
|
+
if (!holds || !Object.hasOwn(holds, key)) return holds;
|
|
219
|
+
return Object.fromEntries(Object.entries(holds).filter(([name]) => name !== key));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** The account of the configured model that `id` names, or null when none does. */
|
|
223
|
+
export function accountOfModel(config, id) {
|
|
224
|
+
if (typeof id !== 'string') return null;
|
|
225
|
+
const alias = Object.keys(config.models).find((key) => sameKey(config.models[key].id, id));
|
|
226
|
+
return alias === undefined ? null : accountOf(config, alias);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** The distinct accounts of the models billed to a plan, in the order of the models. */
|
|
230
|
+
export const planAccountsOf = (config) => [
|
|
231
|
+
...new Set(
|
|
232
|
+
Object.keys(config.models)
|
|
233
|
+
.filter((alias) => config.models[alias].billing === 'plan')
|
|
234
|
+
.map((alias) => accountOf(config, alias)),
|
|
235
|
+
),
|
|
236
|
+
];
|
|
237
|
+
|
|
238
|
+
/** Every account some configured model belongs to, in the order of the models. */
|
|
239
|
+
export const accountNames = (config) => [
|
|
240
|
+
...new Set(Object.keys(config.models).map((alias) => accountOf(config, alias))),
|
|
241
|
+
];
|
|
242
|
+
|
|
243
|
+
/** The stored value as a hold, or null when it does not read as one: a hand edit never breaks a turn. */
|
|
244
|
+
export function readHold(value) {
|
|
245
|
+
if (!isRecord(value) || !HOLD_KINDS.includes(value.kind) || typeof value.error !== 'string') return null;
|
|
246
|
+
const { since, until, streak } = value;
|
|
247
|
+
if (!Number.isFinite(since) || !(until === null || Number.isFinite(until)) || !Number.isSafeInteger(streak))
|
|
248
|
+
return null;
|
|
249
|
+
return {
|
|
250
|
+
kind: value.kind,
|
|
251
|
+
error: value.error,
|
|
252
|
+
since,
|
|
253
|
+
until,
|
|
254
|
+
streak,
|
|
255
|
+
...(PLAN_WINDOWS.includes(value.window) ? { window: value.window } : {}),
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The ids of the configured models held out at `now`: those with a model hold in `held`, and every model of an account
|
|
261
|
+
* with a hold in `accountHolds` (account name to hold). What routing skips.
|
|
41
262
|
*/
|
|
42
|
-
export function
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
263
|
+
export function heldIds(config, held, accountHolds, now) {
|
|
264
|
+
const covered = Object.keys(config.models)
|
|
265
|
+
.filter((alias) => {
|
|
266
|
+
const hold = accountHolds?.[accountOf(config, alias)];
|
|
267
|
+
return hold && isActive(hold, now);
|
|
268
|
+
})
|
|
269
|
+
.map((alias) => config.models[alias].id);
|
|
270
|
+
return [...new Set([...heldModels(held, now), ...covered])];
|
|
49
271
|
}
|
|
50
272
|
|
|
51
|
-
|
|
273
|
+
const WINDOW_WORDS = { five_hour: '5h limit', seven_day: '7d limit' };
|
|
274
|
+
|
|
275
|
+
/** Why a hold holds, as the pane says it: "5h limit", "spend limit", "no balance", "overloaded ×2". */
|
|
276
|
+
export function holdCause(hold) {
|
|
277
|
+
if (hold.kind === 'window') return WINDOW_WORDS[hold.window] ?? KIND_WORDS.window;
|
|
278
|
+
if (hold.kind !== 'availability') return KIND_WORDS[hold.kind];
|
|
279
|
+
const words = ERROR_WORDS[hold.error] ?? hold.error;
|
|
280
|
+
return hold.streak > 1 ? `${words} ×${hold.streak}` : words;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const duration = (ms) => {
|
|
284
|
+
const minutes = Math.max(1, Math.ceil(ms / MINUTE));
|
|
285
|
+
return minutes < 120 ? `${minutes} min` : `${Math.ceil(minutes / 60)} h`;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/** "overloaded, 5 min": why a model or account is held out and for how long, rounded up. */
|
|
52
289
|
export function holdText(hold, now) {
|
|
53
|
-
const
|
|
54
|
-
return
|
|
290
|
+
const words = hold.kind === 'availability' ? (ERROR_WORDS[hold.error] ?? hold.error) : KIND_WORDS[hold.kind];
|
|
291
|
+
return hold.until === null ? `${words}, until released` : `${words}, ${duration(hold.until - now)}`;
|
|
55
292
|
}
|
package/lib/holds.mjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// What is held out of routing, as the pane, the band, the toast and `/router holds` say it. Pure: the hook reads the
|
|
2
|
+
// holds (`{ accounts, models }`: account name and model id to hold, lib/failover.mjs) and passes them in with the clock.
|
|
3
|
+
|
|
4
|
+
import { accountOf, PLAN_ACCOUNT } from './config.mjs';
|
|
5
|
+
import { resetText } from './costs.mjs';
|
|
6
|
+
import { modelName } from './display.mjs';
|
|
7
|
+
import { holdCause, isActive } from './failover.mjs';
|
|
8
|
+
import { canonicalModel } from './providers.mjs';
|
|
9
|
+
|
|
10
|
+
const DAY = 24 * 60 * 60_000;
|
|
11
|
+
// A time further away than this reads as a date: "Thu 19:00" would be ambiguous.
|
|
12
|
+
const WEEKDAY_SPAN = 6 * DAY;
|
|
13
|
+
const NOTHING = 'Nothing is held out of routing.';
|
|
14
|
+
|
|
15
|
+
/** @typedef {{ accounts?: Record<string, import('./failover.mjs').Hold>, models?: Record<string, import('./failover.mjs').Hold> }} HoldSet Account name and model id to hold. */
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {object} HeldRow one hold that is running
|
|
19
|
+
* @property {'account' | 'model'} scope
|
|
20
|
+
* @property {string} key what the store or the loop keeps it under: the account name or the model id
|
|
21
|
+
* @property {string} word what `/router release` takes: the account name, or the model's key in router.json
|
|
22
|
+
* @property {string} name who is held: "Claude plan", an account, or a model's name
|
|
23
|
+
* @property {string} cause "5h limit", "no balance", "overloaded ×2"
|
|
24
|
+
* @property {string} back "back 19:00" or "until released"
|
|
25
|
+
* @property {string} text cause and back, as one phrase
|
|
26
|
+
* @property {string[]} covers the names of the models an account holds out; none for a model
|
|
27
|
+
* @property {string[]} aliases the models keys in router.json it holds out
|
|
28
|
+
* @property {import('./failover.mjs').Hold} hold
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const sameModel = (a, b) => canonicalModel(a) === canonicalModel(b);
|
|
32
|
+
|
|
33
|
+
/** When a hold ends: "back 19:00", "back Thu 19:00", "back Dec 1", or "until released". */
|
|
34
|
+
export function backText(until, now) {
|
|
35
|
+
if (until === null) return 'until released';
|
|
36
|
+
if (until - now > WEEKDAY_SPAN)
|
|
37
|
+
return `back ${new Date(until).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}`;
|
|
38
|
+
return `back ${resetText(until, now) ?? 'now'}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The holds running at `now`, accounts first. An account's row names the models it holds out; a model's row only itself.
|
|
43
|
+
* @param {Parameters<typeof accountOf>[0]} config
|
|
44
|
+
* @param {HoldSet | null | undefined} holds null before the first read
|
|
45
|
+
* @param {number} now
|
|
46
|
+
* @returns {HeldRow[]}
|
|
47
|
+
*/
|
|
48
|
+
export function heldRows(config, holds, now) {
|
|
49
|
+
const keys = Object.keys(config.models);
|
|
50
|
+
const running = (/** @type {Record<string, import('./failover.mjs').Hold> | undefined} */ byKey) =>
|
|
51
|
+
Object.entries(byKey ?? {}).filter(([, hold]) => isActive(hold, now));
|
|
52
|
+
const row = (scope, key, word, name, hold, aliases) => ({
|
|
53
|
+
scope,
|
|
54
|
+
key,
|
|
55
|
+
word,
|
|
56
|
+
name,
|
|
57
|
+
cause: holdCause(hold),
|
|
58
|
+
back: backText(hold.until, now),
|
|
59
|
+
text: `${holdCause(hold)} · ${backText(hold.until, now)}`,
|
|
60
|
+
covers: scope === 'account' ? [...new Set(aliases.map((alias) => modelName(config.models[alias].id)))] : [],
|
|
61
|
+
aliases,
|
|
62
|
+
hold,
|
|
63
|
+
});
|
|
64
|
+
// A credits model's account is named after it, so the two share a word; the model's row then takes its id.
|
|
65
|
+
const accountWords = new Set(running(holds?.accounts).map(([key]) => key));
|
|
66
|
+
return [
|
|
67
|
+
...running(holds?.accounts).map(([key, hold]) =>
|
|
68
|
+
row(
|
|
69
|
+
'account',
|
|
70
|
+
key,
|
|
71
|
+
key,
|
|
72
|
+
key === PLAN_ACCOUNT ? 'Claude plan' : key,
|
|
73
|
+
hold,
|
|
74
|
+
keys.filter((alias) => accountOf(config, alias) === key),
|
|
75
|
+
),
|
|
76
|
+
),
|
|
77
|
+
...running(holds?.models).map(([key, hold]) => {
|
|
78
|
+
const aliases = keys.filter((alias) => sameModel(config.models[alias].id, key));
|
|
79
|
+
const word = aliases[0] ?? key;
|
|
80
|
+
return row('model', key, accountWords.has(word) ? key : word, modelName(key), hold, aliases);
|
|
81
|
+
}),
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Who holds `view.fallback.from`'s model out, with the cause and the end: "Claude plan 5h limit · back 19:00". Without a
|
|
87
|
+
* chain step to name the model, the one hold running is the cause; null when none or several could be.
|
|
88
|
+
*/
|
|
89
|
+
export function heldCause(config, view, now) {
|
|
90
|
+
const rows = heldRows(config, view.holds, now);
|
|
91
|
+
const from = view.fallback?.from;
|
|
92
|
+
const row = from ? rows.find((one) => one.aliases.includes(from)) : rows.length === 1 ? rows[0] : undefined;
|
|
93
|
+
return row ? `${row.name} ${row.text}` : null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The Now tab's why for a turn a chain's target runs: "↷ Opus 5.5 held · fallback 1 of 2". Null for any other turn. */
|
|
97
|
+
export function heldWhy(config, view) {
|
|
98
|
+
const { fallback } = view;
|
|
99
|
+
if (view.reason !== 'model-held' || !fallback || !Object.hasOwn(config.models, fallback.from)) return null;
|
|
100
|
+
return `↷ ${modelName(config.models[fallback.from].id)} held · fallback ${fallback.step} of ${fallback.of}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The row `word` names for `/router release`: an account, a model's key or id; else the words that say why not. */
|
|
104
|
+
export function releaseRow(config, holds, word, now) {
|
|
105
|
+
const rows = heldRows(config, holds, now);
|
|
106
|
+
if (!word) return { text: rows.length ? `Name what to release: ${rows.map((r) => r.word).join(', ')}.` : NOTHING };
|
|
107
|
+
const own = rows.find((row) => row.word === word || row.key === word);
|
|
108
|
+
if (own) return { row: own };
|
|
109
|
+
const via = rows.find((row) => row.aliases.includes(word));
|
|
110
|
+
if (via)
|
|
111
|
+
return {
|
|
112
|
+
text: `${word} is held through ${via.name}, not on its own: /router release ${via.word}`,
|
|
113
|
+
};
|
|
114
|
+
return { text: rows.length ? `Nothing held under that name. Held: ${rows.map((r) => r.word).join(', ')}.` : NOTHING };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** `/router holds`: one line per hold with its scope, kind, cause and end. */
|
|
118
|
+
export function holdsText(config, holds, now) {
|
|
119
|
+
const rows = heldRows(config, holds, now);
|
|
120
|
+
if (!rows.length) return NOTHING;
|
|
121
|
+
return [
|
|
122
|
+
'Held out of routing:',
|
|
123
|
+
...rows.map(
|
|
124
|
+
(row) =>
|
|
125
|
+
` ${row.word} (${row.scope}) · ${row.hold.kind}: ${row.text}${row.covers.length ? ` · holds out ${row.covers.join(', ')}` : ''}`,
|
|
126
|
+
),
|
|
127
|
+
'End one early with /router release <model|account>.',
|
|
128
|
+
].join('\n');
|
|
129
|
+
}
|
package/lib/providers.mjs
CHANGED
|
@@ -92,6 +92,8 @@ export const PROVIDER_DEFAULTS = {
|
|
|
92
92
|
capability: {},
|
|
93
93
|
strength: 1,
|
|
94
94
|
imageInput: true,
|
|
95
|
+
// Its preserved thinking and signatures are readable only by this model, so a chain cannot hand its history over.
|
|
96
|
+
requiresOwnHistory: true,
|
|
95
97
|
},
|
|
96
98
|
},
|
|
97
99
|
routes: {
|