@alexeiled/claude-router 1.25.1 → 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.
@@ -37,6 +37,7 @@ import {
37
37
  usd,
38
38
  whyText,
39
39
  } from '../../display.mjs';
40
+ import { heldRows, heldWhy } from '../../holds.mjs';
40
41
  import { limitTone, savingTone } from '../tokens.mjs';
41
42
  import { NO_PIN, noPinLabel } from './band.mjs';
42
43
  import { plural, sentence, sinceText } from './format.mjs';
@@ -132,8 +133,15 @@ function supportText(label, estimate) {
132
133
  * @property {{ tone: import('../tokens.mjs').Tone, last: { figure: string, words: string } | null, session: { figure: string, words: string } | null, empty: import('./format.mjs').Words }} cache
133
134
  * @property {{ open: boolean, toggle: () => void, readings: { terminal: string[], graphical: string }, input: import('./format.mjs').Words, history: number[], tiers: string[], chart: { points: number[], labels: string[], alt: string, tone: import('../tokens.mjs').Tone } | null, range: import('./format.mjs').Words | null, empty: import('./format.mjs').Words, estimatesHead: import('./format.mjs').Words, estimates: [string, string][] }} detail
134
135
  *
136
+ * @typedef {object} HeldOutModel HELD OUT: what routing skips while it is held, drawn only then.
137
+ * @property {import('./format.mjs').Words} note
138
+ * @property {{ name: string, text: string, covers: string | null, release: { key: string, label: string, press: () => void } }[]} rows
139
+ * one per held account or model: who, why and until when, the models it covers, and the Release that ends it
140
+ * @property {import('./format.mjs').Words} footer where chains are set and where holds are listed
141
+ *
135
142
  * @typedef {object} NowModel The Now tab: what runs next, the plan, the session and the context.
136
143
  * @property {NextModel} next
144
+ * @property {HeldOutModel | null} held
137
145
  * @property {string[]} decisions
138
146
  * @property {{ label: string, percent: number, tone: import('../tokens.mjs').Tone, reset: string | null }[]} plan
139
147
  * @property {SessionModel} session
@@ -147,6 +155,7 @@ export function nowOf({ config, view, usage, actions, now }) {
147
155
  const cost = sessionCost(config, view, usage, now);
148
156
  return {
149
157
  next: nextOf(config, view, actions),
158
+ held: heldOf(config, view, actions, now),
150
159
  decisions: view.decisionRows?.length ? decisionLines(view.decisionRows, 5) : [],
151
160
  plan: cost.limits.map((limit) => ({
152
161
  label: limit.label,
@@ -203,7 +212,7 @@ function nextOf(config, view, actions) {
203
212
  other: reading.others[0] ?? null,
204
213
  },
205
214
  phase: view.taskPhase ? `phase ${view.taskPhase} · route activity ${view.activity ?? 'base'}` : null,
206
- why: view.reason ? shortReason(view.reason, reasonActivity(config, view)) : null,
215
+ why: heldWhy(config, view) ?? (view.reason ? shortReason(view.reason, reasonActivity(config, view)) : null),
207
216
  support: supportText(classifierLabel(config), view.estimate),
208
217
  override: override ? `${whyText(config, view)} · tier route ${routeName(shown, base)}` : null,
209
218
  mode:
@@ -257,6 +266,31 @@ function nextOf(config, view, actions) {
257
266
  };
258
267
  }
259
268
 
269
+ // HELD OUT: one row per hold in force, with its Release. Null while nothing is held. A hold outlives routing being off,
270
+ // so it stays here to be released.
271
+ /** @returns {HeldOutModel | null} */
272
+ function heldOf(config, view, actions, now) {
273
+ const rows = heldRows(config, view.holds, now);
274
+ if (!rows.length) return null;
275
+ return {
276
+ note: { terminal: 'routing skips these until they are back', graphical: 'Routing skips these until they are back' },
277
+ rows: rows.map((row) => ({
278
+ name: row.name,
279
+ text: row.text,
280
+ covers: row.covers.length ? row.covers.join(', ') : null,
281
+ release: {
282
+ key: `release-${row.scope}-${row.key}`,
283
+ label: 'Release',
284
+ press: () => actions.release(row.scope, row.key),
285
+ },
286
+ })),
287
+ footer: {
288
+ terminal: 'chains: router.json → fallbacks · /router holds',
289
+ graphical: 'Fallback chains are set in router.json → fallbacks. /router holds lists what is held.',
290
+ },
291
+ };
292
+ }
293
+
260
294
  // The session's empty states: no reply carries a price, or no reply yet.
261
295
  const UNPRICED_SESSION = { terminal: 'no priced replies: cost unknown', graphical: 'No priced replies: cost unknown' };
262
296
  const EMPTY_SESSION = { terminal: 'no replies yet', graphical: 'No routed replies yet' };
@@ -5,6 +5,7 @@ import { compareRoutes } from '../../activity.mjs';
5
5
  import { ACTIVITIES, ACTIVITY_MODES, defaultsFor, resolveRoute, sameRoute, TIERS } from '../../config.mjs';
6
6
  import { effortSharesCache } from '../../cost.mjs';
7
7
  import { cardRoute, modelName, routeName } from '../../display.mjs';
8
+ import { heldRows } from '../../holds.mjs';
8
9
  import { routingChanges } from './draft.mjs';
9
10
  import { plural, sentence, tierLabel } from './format.mjs';
10
11
 
@@ -14,11 +15,12 @@ const SESSION_EFFORT = 'session';
14
15
  const INHERIT = 'inherit';
15
16
  // The Routes grid's marks, as its legend explains them, each in its tone.
16
17
  // `card` is the glyph a card draws where the terminal's is too slight in a Button.
17
- /** @type {{ inherits: RouteMark, preset: RouteMark, edited: RouteMark }} */
18
+ /** @type {{ inherits: RouteMark, preset: RouteMark, edited: RouteMark, held: RouteMark }} */
18
19
  export const ROUTE_MARKS = {
19
20
  inherits: { glyph: '·', card: '—', tone: 'muted', text: 'runs the tier’s model' },
20
21
  preset: { glyph: '°', tone: 'muted', text: 'built-in default' },
21
22
  edited: { glyph: '●', tone: 'warning', text: 'unsaved' },
23
+ held: { glyph: '↷', tone: 'warning', text: 'held out now' },
22
24
  };
23
25
  const UNPRICED_OUTPUT = '$? out';
24
26
 
@@ -158,12 +160,27 @@ function cacheUses(draft, overrides) {
158
160
  *
159
161
  * @typedef {TierEditor | ActivityEditor} RouteEditor the selected cell's editor
160
162
  *
161
- * @typedef {object} RoutesModel The Routes tab: the grid, the cell editor and the cache footer.
163
+ * @typedef {object} FallbackTarget one entry of a model's chain
164
+ * @property {string} label the target as the grid names it: the models key and its effort, "glm·max"
165
+ * @property {boolean} held it is held out now
166
+ * @property {boolean} runs it stands in for the model on the turn shown on Now
167
+ *
168
+ * @typedef {object} FallbackRow one model with a chain
169
+ * @property {string} model the models key
170
+ * @property {{ text: string, tone: import('../tokens.mjs').Tone }} status
171
+ * @property {FallbackTarget[]} targets in the order they are tried
172
+ *
173
+ * @typedef {object} FallbacksModel FALLBACKS: the chains in router.json, read-only.
174
+ * @property {FallbackRow[]} rows
175
+ * @property {import('./format.mjs').Words} note where chains are set
176
+ *
177
+ * @typedef {object} RoutesModel The Routes tab: the grid, the cell editor, the fallback chains and the cache footer.
162
178
  * @property {{ id: string, label: import('./format.mjs').Words }[]} tiers
163
179
  * @property {{ label: string, value: string, options: { value: string, label: import('./format.mjs').Words }[], changed: boolean, hint: import('./format.mjs').Words, pick: (mode: string) => void }} mode
164
180
  * @property {{ warning: boolean, text: string, after: string | null } | null} overrides
165
181
  * @property {GridRow[]} rows
166
182
  * @property {RouteEditor | null} editor
183
+ * @property {FallbacksModel | null} fallbacks null with no chain
167
184
  * @property {string | null} hint what a cell press does, until one is pressed
168
185
  * @property {RouteMark} inheritMark
169
186
  * @property {RouteMark[]} legend
@@ -177,7 +194,9 @@ function cacheUses(draft, overrides) {
177
194
  * @param {import('./frame.mjs').PaneContext} ctx
178
195
  * @returns {RoutesModel}
179
196
  */
180
- export function routesOf({ config, view, actions, modelOptions }) {
197
+ export function routesOf({ config, view, actions, modelOptions, now }) {
198
+ const held = heldRows(config, view.holds, now);
199
+ const heldModel = (alias) => held.some((row) => row.aliases.includes(alias));
181
200
  const changes = routingChanges(config, view);
182
201
  const draft = { ...config, ...changes.draft };
183
202
  const mode = draft.activityRouting;
@@ -191,7 +210,11 @@ export function routesOf({ config, view, actions, modelOptions }) {
191
210
  route: inherits ? null : gridRoute(draft, route),
192
211
  label: inherits ? ROUTE_MARKS.inherits.card : cardRoute(draft, route),
193
212
  inherits,
194
- marks: [preset ? ROUTE_MARKS.preset : null, edited ? ROUTE_MARKS.edited : null].filter((mark) => mark !== null),
213
+ marks: [
214
+ preset ? ROUTE_MARKS.preset : null,
215
+ edited ? ROUTE_MARKS.edited : null,
216
+ !inherits && heldModel(route.model) ? ROUTE_MARKS.held : null,
217
+ ].filter((mark) => mark !== null),
195
218
  // Runs nothing now: the tier's model, or an override while activity routing is not on.
196
219
  muted,
197
220
  selected: selected === `${row}.${tier}`,
@@ -250,10 +273,16 @@ export function routesOf({ config, view, actions, modelOptions }) {
250
273
  overrides: overridesOf(view, config.activityRouting, mode),
251
274
  rows,
252
275
  editor,
276
+ fallbacks: fallbacksOf(config, view, held),
253
277
  // What a cell press does, until one is pressed.
254
278
  hint: editor ? null : 'Select a cell to change its model and effort.',
255
279
  inheritMark: ROUTE_MARKS.inherits,
256
- legend: [ROUTE_MARKS.inherits, ROUTE_MARKS.preset, ROUTE_MARKS.edited],
280
+ legend: [
281
+ ROUTE_MARKS.inherits,
282
+ ROUTE_MARKS.preset,
283
+ ROUTE_MARKS.edited,
284
+ ...(rows.some((row) => row.cells.some((one) => one.marks.includes(ROUTE_MARKS.held))) ? [ROUTE_MARKS.held] : []),
285
+ ],
257
286
  unsaved: ROUTE_MARKS.edited,
258
287
  // `wrap`: the terminal wraps the sentence that counts caches, not the fixed ones.
259
288
  cache: {
@@ -269,6 +298,40 @@ export function routesOf({ config, view, actions, modelOptions }) {
269
298
  };
270
299
  }
271
300
 
301
+ // FALLBACKS: each model with a chain, its targets in order with the held ones and the one standing in on the turn Now
302
+ // shows, and the model's status. Null with no chain.
303
+ /** @returns {FallbacksModel | null} */
304
+ function fallbacksOf(config, view, held) {
305
+ const chains = Object.entries(config.fallbacks);
306
+ if (!chains.length) return null;
307
+ const holder = (alias) => held.find((row) => row.aliases.includes(alias));
308
+ const standing = view.reason === 'model-held' ? view.fallback : null;
309
+ return {
310
+ rows: chains.map(([model, chain]) => {
311
+ const targets = chain.map((entry, index) => {
312
+ const [alias, effort] = entry.split('@');
313
+ return {
314
+ label: effort ? `${alias}·${effort}` : alias,
315
+ held: Boolean(holder(alias)),
316
+ runs: standing?.from === model && standing.step === index + 1,
317
+ };
318
+ });
319
+ const cause = holder(model);
320
+ return {
321
+ model,
322
+ targets,
323
+ status: cause
324
+ ? {
325
+ text: `held · ${cause.text}${targets.every((target) => target.held) ? ' · every target held' : ''}`,
326
+ tone: /** @type {import('../tokens.mjs').Tone} */ ('warning'),
327
+ }
328
+ : { text: 'ready', tone: /** @type {import('../tokens.mjs').Tone} */ ('muted') },
329
+ };
330
+ }),
331
+ note: { terminal: 'read-only · router.json → fallbacks', graphical: 'Read-only · set in router.json → fallbacks' },
332
+ };
333
+ }
334
+
272
335
  // Why the cache count matters: a model keeps one cache across efforts (the 5.5 models on an API key or a subscription)
273
336
  // or one per effort (other providers).
274
337
  function cacheSentence(n, shared, mode) {
@@ -137,7 +137,7 @@ export function terminalBand(elements, config, view, usage, { columns, agentId,
137
137
  ...agentParts(config, agent, back ? room : columns),
138
138
  ]);
139
139
  }
140
- const segments = bandSegments(config, view, usage, actions);
140
+ const segments = bandSegments(config, view, usage, actions, now);
141
141
  // The brackets go first: Router keeps them only where they cost nothing the plain label leaves room for, neither a
142
142
  // reading nor a hover control.
143
143
  const plain = bandLayout(config, view, actions, segments, columns, false);
@@ -10,6 +10,8 @@ const SESSION_BAR = 16;
10
10
  const PART_LABEL = 8;
11
11
  const AMOUNT = 7;
12
12
  const ESTIMATE_LABEL = 22;
13
+ // A Release button: its label in `[ ` and ` ]`.
14
+ const RELEASE_CELLS = 'Release'.length + 4;
13
15
 
14
16
  /**
15
17
  * @param {any} ui the terminal kit
@@ -21,6 +23,7 @@ export function terminalNow(ui, model, { columns, now }) {
21
23
  const room = ui.inner(columns);
22
24
  return [
23
25
  ...ui.panel(columns, nextLines(ui, model.next, room)),
26
+ ...(model.held ? ui.panel(columns, heldLines(ui, model.held, room)) : []),
24
27
  ...(model.decisions.length
25
28
  ? ui.panel(columns, [ui.head('RECENT DECISIONS'), ...model.decisions.map((line) => ui.text(line))])
26
29
  : []),
@@ -103,6 +106,32 @@ function pinLines(ui, next, columns) {
103
106
  ];
104
107
  }
105
108
 
109
+ /**
110
+ * @param {any} ui
111
+ * @param {import('../model/now.mjs').HeldOutModel} held
112
+ * @param {number | null} columns the section's own columns
113
+ */
114
+ // HELD OUT: each hold with its cause and end, the models it covers and its Release, on the row's own line where it fits.
115
+ function heldLines(ui, held, columns) {
116
+ const out = [ui.line(ui.head('HELD OUT'), ui.dim(` ${ui.say(held.note)}`))];
117
+ for (const row of held.rows) {
118
+ const release = ui.Button({ key: row.release.key, label: row.release.label, onPress: row.release.press });
119
+ const beside = !isFiniteNumber(columns) || ` ${row.name} ${row.text} `.length + RELEASE_CELLS <= columns;
120
+ out.push(
121
+ ui.line(
122
+ ui.text(` ${row.name}`, { weight: 'semibold' }),
123
+ ui.dim(` ${row.text}`),
124
+ beside ? ' ' : null,
125
+ beside ? release : null,
126
+ ),
127
+ );
128
+ if (row.covers) out.push(...dimLines(ui, ` covers ${row.covers}`, columns));
129
+ if (!beside) out.push(ui.line(' ', release));
130
+ }
131
+ out.push(ui.dim(` ${ui.say(held.footer)}`));
132
+ return out;
133
+ }
134
+
106
135
  /**
107
136
  * @param {any} ui
108
137
  * @param {import('../model/now.mjs').NowModel['plan']} plan
@@ -4,6 +4,8 @@ import { dimLines } from './wrap.mjs';
4
4
  // The grid's row-label column, and the editor's label column.
5
5
  const GRID_LABEL = 13;
6
6
  const EDITOR_LABEL = 22;
7
+ // The FALLBACKS model column, wide enough for a models key and the row's indent.
8
+ const FALLBACK_LABEL = 12;
7
9
  const HEAD_HINT = ' · which model runs, by tier and activity · edit, then Save';
8
10
  // Below this many columns the grid's efforts are cut short.
9
11
  const NARROW = 80;
@@ -84,6 +86,7 @@ export function terminalRoutes(ui, model, { columns }) {
84
86
  ui.text(' '),
85
87
  ...(model.editor ? editorLines(ui, model.editor, model.unsaved) : [ui.dim(model.hint)]),
86
88
  ui.text(' '),
89
+ ...(model.fallbacks ? [...fallbackLines(ui, model.fallbacks), ui.text(' ')] : []),
87
90
  ...(model.cache.wrap
88
91
  ? dimLines(ui, ui.say(model.cache.text), Math.min(columns ?? 78, 78))
89
92
  : [ui.dim(ui.say(model.cache.text))]),
@@ -93,6 +96,34 @@ export function terminalRoutes(ui, model, { columns }) {
93
96
  ];
94
97
  }
95
98
 
99
+ /**
100
+ * @param {any} ui
101
+ * @param {import('../model/routes.mjs').FallbacksModel} fallbacks
102
+ */
103
+ // FALLBACKS: a model and its status, then its targets in the order they are tried: a held one ✕, the one standing in
104
+ // ◀ runs.
105
+ function fallbackLines(ui, fallbacks) {
106
+ const pad = ''.padEnd(FALLBACK_LABEL);
107
+ return [
108
+ ui.line(ui.head('FALLBACKS'), ui.dim(` ${ui.say(fallbacks.note)}`)),
109
+ ...fallbacks.rows.flatMap((row) => [
110
+ ui.line(
111
+ ui.cell(FALLBACK_LABEL, ui.text(` ${row.model}`, { weight: 'semibold' })),
112
+ ui.text(row.status.text, { color: ui.color(row.status.tone) }),
113
+ ),
114
+ ui.line(
115
+ pad,
116
+ ...row.targets.flatMap((target, index) => [
117
+ ui.dim(`${index ? ' ' : ''}${index + 1} `),
118
+ ui.text(target.label, target.held ? { color: ui.color('muted'), dimColor: true } : {}),
119
+ target.held ? ui.text(' ✕', { color: ui.color('warning') }) : null,
120
+ target.runs ? ui.text(' ◀ runs', { color: ui.color('accent') }) : null,
121
+ ]),
122
+ ),
123
+ ]),
124
+ ];
125
+ }
126
+
96
127
  // A label and its marks cut to their cell with an ellipsis, keeping the marks.
97
128
  function fitRouteCell(text, mark, width) {
98
129
  const label = `${text}${mark}`;
package/lib/view.mjs CHANGED
@@ -26,6 +26,10 @@ export function initialView(model) {
26
26
  tier: null,
27
27
  effort: null,
28
28
  reason: 'ready',
29
+ // The chain step a held-out model's stand-in runs (`{ from, step, of }`), and the holds in force when the pane last
30
+ // read them (`{ accounts, models }`, lib/holds.mjs).
31
+ fallback: null,
32
+ holds: null,
29
33
  error: null,
30
34
  pendingPin: null,
31
35
  credentials: null,
@@ -111,6 +115,7 @@ export function continuationView(config, decision, context) {
111
115
  effort: decision.effort,
112
116
  tier: decision.tier,
113
117
  reason: decision.reason,
118
+ fallback: decision.fallback ?? null,
114
119
  contextTokens: context.tokens,
115
120
  contextKnown: context.known,
116
121
  ...(config.activityRouting === 'on'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexeiled/claude-router",
3
- "version": "1.25.1",
3
+ "version": "1.27.0",
4
4
  "description": "Native Claude Code model and effort routing with Anthropic, DeepSeek, z.ai, MiniMax, MiMo and Kimi defaults, custom provider profiles, and classifier advice.",
5
5
  "license": "MIT",
6
6
  "author": "Alexei Ledenev",