@alexeiled/claude-router 1.6.0 → 1.7.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/lib/band.mjs CHANGED
@@ -3,21 +3,27 @@ import {
3
3
  classifierLabel,
4
4
  classifierStatus,
5
5
  classifierWarns,
6
- GATEWAY_SETTINGS,
6
+ differenceShare,
7
7
  modelName,
8
8
  NOT_A_TIER_REASON,
9
9
  percent,
10
10
  reasonActivity,
11
11
  routeLabel,
12
+ SAVINGS_COLOR,
12
13
  shortReason,
14
+ signedUsd,
13
15
  switchCount,
14
16
  TIER_COLOR,
17
+ unavailableReason,
15
18
  usageMetrics,
16
19
  } from './display.mjs';
17
20
  import { isSameModel } from './route.mjs';
21
+ import { emptyTotals, readout } from './savings.mjs';
18
22
 
19
23
  const BARS = '▂▄▆█';
20
24
  const SEPARATOR = ' · ';
25
+ // The hover row's controls sit apart without the dot: they are buttons, not readings.
26
+ const GAP = ' ';
21
27
  // Space the band keeps for the trailing Router button.
22
28
  const ROUTER_BUTTON = 8;
23
29
  const STRIP = 20;
@@ -33,15 +39,15 @@ const button = (props) => ({ button: props });
33
39
  // drop from inside a segment it shares no separator with.
34
40
  const segment = (priority, ...parts) => ({ priority, parts: parts.flat().filter(Boolean) });
35
41
  const partWidth = (p) => (p.button ? p.button.label.length + (p.button.plain ? 0 : 4) : p.text.length);
36
- const lineWidth = (segments) =>
37
- segments.reduce((sum, s, i) => sum + (i ? SEPARATOR.length : 0) + s.parts.reduce((n, p) => n + partWidth(p), 0), 0);
42
+ const lineWidth = (segments, separator) =>
43
+ segments.reduce((sum, s, i) => sum + (i ? separator.length : 0) + s.parts.reduce((n, p) => n + partWidth(p), 0), 0);
38
44
 
39
45
  // Drops the highest-priority segments and parts, later ones first on a tie, until the line fits `columns`. If the
40
46
  // priority-0 segments alone are too wide, the later ones go too, and the first is cut with an ellipsis: Ink would
41
47
  // otherwise shrink every Text in the row and garble it.
42
- export function fitSegments(segments, columns) {
48
+ export function fitSegments(segments, columns, separator = SEPARATOR) {
43
49
  const kept = [...segments];
44
- while (lineWidth(kept) > columns) {
50
+ while (lineWidth(kept, separator) > columns) {
45
51
  let drop = null;
46
52
  kept.forEach((s, i) => {
47
53
  s.parts.forEach((p, j) => {
@@ -56,7 +62,7 @@ export function fitSegments(segments, columns) {
56
62
  else if (kept.length > 1) kept.pop();
57
63
  else break;
58
64
  }
59
- const overflow = lineWidth(kept) - columns;
65
+ const overflow = lineWidth(kept, separator) - columns;
60
66
  if (overflow > 0 && kept.length) {
61
67
  const parts = [...kept[0].parts];
62
68
  const last = parts.findLastIndex((p) => !p.button && p.text.length > overflow);
@@ -75,10 +81,6 @@ function meter(tier, dim = false) {
75
81
  // The classifier is working: every bar in the accent colour.
76
82
  const pendingMeter = () => [...BARS].map((bar) => part(bar, { color: COLOR.accent }));
77
83
 
78
- // Turns routing off: the one control in Auto that pauses it.
79
- const routingOffButton = (actions, variant) =>
80
- button({ key: 'band-manual-main', label: 'Routing off', variant, onPress: () => actions.mode('manual') });
81
-
82
84
  const route = (view) => routeLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel, view.effort);
83
85
 
84
86
  // The turn's activity between the tier and the route, in neutral ink so the tier keeps its color; dim and marked in
@@ -105,31 +107,30 @@ export function bandSegments(config, view, usage, actions) {
105
107
  if (view.phase === 'unavailable')
106
108
  return [
107
109
  segment(0, part('✕ Routing unavailable', { color: 'red' })),
108
- segment(
109
- 2,
110
- part(view.error === GATEWAY_SETTINGS ? 'v0.8 gateway settings remain' : (view.error ?? 'unknown error'), {
111
- color: 'red',
112
- }),
113
- ),
110
+ segment(2, part(unavailableReason(view), { color: 'red' })),
114
111
  segment(1, button({ key: 'band-fix', label: 'Fix', onPress: actions.open })),
115
112
  ];
113
+ // The state and Turn on lead in the first segment, which never drops, and a cut keeps buttons whole: the band's one
114
+ // way back on stays at any width. The model and why go first, then the word Routing.
116
115
  if (view.mode === 'manual')
117
116
  return [
118
117
  segment(
119
118
  0,
120
119
  part('○ ', { dimColor: true }),
121
- part('Routing '),
120
+ { ...part('Routing '), priority: 1 },
122
121
  part('off', { backgroundColor: COLOR.off, color: COLOR.muted }),
123
- part(` · every turn uses ${modelName(view.nativeModel)}`),
124
- view.reason === 'model selected manually' ? part(' (/model)', { dimColor: true }) : null,
122
+ part(' '),
123
+ button({ key: 'band-auto', label: 'Turn on', variant: 'primary', onPress: () => actions.mode('auto') }),
125
124
  ),
126
- view.reason === NOT_A_TIER_REASON
127
- ? segment(2, part(`${modelName(view.nativeModel)} is not a routing tier`, { dimColor: true }))
128
- : null,
129
125
  segment(
130
126
  1,
131
- button({ key: 'band-auto', label: 'Routing on', variant: 'primary', onPress: () => actions.mode('auto') }),
127
+ { ...part('every turn uses '), priority: 3 },
128
+ part(modelName(view.nativeModel)),
129
+ view.reason === 'model selected manually' ? { ...part(' (/model)', { dimColor: true }), priority: 2 } : null,
132
130
  ),
131
+ view.reason === NOT_A_TIER_REASON
132
+ ? segment(2, part(`${modelName(view.nativeModel)} is not a routing tier`, { dimColor: true }))
133
+ : null,
133
134
  ].filter(Boolean);
134
135
  if (view.phase === 'choosing')
135
136
  return [
@@ -151,11 +152,7 @@ export function bandSegments(config, view, usage, actions) {
151
152
  )
152
153
  : null;
153
154
  if (!view.tier && view.phase === 'ready' && !view.actualModel)
154
- return [
155
- segment(0, meter(null, true), part(' Routing on · ready', { dimColor: true })),
156
- segment(1, routingOffButton(actions, 'secondary')),
157
- pin,
158
- ].filter(Boolean);
155
+ return [segment(0, meter(null, true), part(' Routing on · ready', { dimColor: true })), pin].filter(Boolean);
159
156
  const main = segment(
160
157
  0,
161
158
  view.tier ? [...meter(view.tier), pill(view.tier, TIER_COLOR[view.tier])] : part('○ '),
@@ -180,7 +177,6 @@ export function bandSegments(config, view, usage, actions) {
180
177
  const support = supportText(config, view.estimate);
181
178
  return [
182
179
  main,
183
- segment(1, routingOffButton(actions, 'secondary')),
184
180
  pin,
185
181
  segment(1, part(shortReason(view.reason, reasonActivity(config, view)), { dimColor: true })),
186
182
  support ? segment(4, part(support, { dimColor: true })) : null,
@@ -190,13 +186,34 @@ export function bandSegments(config, view, usage, actions) {
190
186
  ].filter(Boolean);
191
187
  }
192
188
 
193
- // The second row of the detailed band: recent replies by tier and the session's switch economics.
194
- function detailRow(config, view, usage) {
189
+ // The session against your model, once it is past "too early": "vs your model −22% (−$1.84, list prices)", or the
190
+ // extra with what bought it, "+$2.48 (stronger models, list prices)". Null before that.
191
+ export function savingsParts(view) {
192
+ const r = readout(view.savings ?? emptyTotals());
193
+ if (r.early) return null;
194
+ const share = differenceShare(r);
195
+ const [shown, note] =
196
+ r.differenceUsd < 0
197
+ ? [share ?? signedUsd(r.differenceUsd), `${signedUsd(r.differenceUsd)}, list prices`]
198
+ : r.differenceUsd > 0
199
+ ? [
200
+ signedUsd(r.differenceUsd),
201
+ `${r.strongerUsd >= r.switchUsd ? 'stronger models' : 'switch cache writes'}, list prices`,
202
+ ]
203
+ : ['$0.00', 'list prices'];
204
+ const color = r.differenceUsd < 0 ? SAVINGS_COLOR.less : r.differenceUsd > 0 ? SAVINGS_COLOR.more : undefined;
205
+ return [part('vs your model '), part(shown, color ? { color } : {}), part(` (${note})`, { dimColor: true })];
206
+ }
207
+
208
+ // The second row of the detailed band: recent replies by tier, the session's switch economics, and the session
209
+ // against your model when the row has room for it.
210
+ function detailRow(config, view, usage, columns) {
195
211
  const tiers = (view.tiers ?? []).slice(-STRIP);
196
212
  if (!tiers.length) return [part('no replies yet', { dimColor: true })];
197
- const switches = switchCount(tiers);
213
+ const switches = switchCount((view.routes ?? []).slice(-tiers.length));
198
214
  const metrics = usageMetrics(config, view, usage);
199
- return [
215
+ const savings = savingsParts(view);
216
+ const row = [
200
217
  part('replies ', { dimColor: true }),
201
218
  ...tiers.map((tier) => (tier ? part('█', { color: TIER_COLOR[tier] }) : part('·', { dimColor: true }))),
202
219
  part(
@@ -206,6 +223,35 @@ function detailRow(config, view, usage) {
206
223
  { dimColor: true },
207
224
  ),
208
225
  ];
226
+ const tail = savings ? [part(SEPARATOR, { dimColor: true }), ...savings] : [];
227
+ const width = (parts) => parts.reduce((n, p) => n + partWidth(p), 0);
228
+ return width(row) + width(tail) <= columns ? [...row, ...tail] : row;
229
+ }
230
+
231
+ // The row revealed under the pointer: pins and the mode control while routing is on, and the one- or two-row choice.
232
+ // No digit hotkeys, since a bare digit in an empty prompt would press them. When the row is narrow, the routing on
233
+ // label goes first, then 2 rows, then the pin label, then the pins. Turn off stays, the band's one way to turn
234
+ // routing off, and so does 1 row, the only way back from a second row too wide for the band.
235
+ function hoverSegments(view, actions, routing) {
236
+ const pins = TIERS.flatMap((tier, i) => [
237
+ i ? part(' ') : null,
238
+ button({ key: `band-pin-${tier}`, label: tier, onPress: () => actions.pin(tier) }),
239
+ ]);
240
+ return [
241
+ routing ? segment(1, { ...part('pin next turn ', { dimColor: true }), priority: 2 }, pins) : null,
242
+ // The state, then what a press does. The state goes first: the meter on the row above already says routing is on.
243
+ routing
244
+ ? segment(
245
+ 0,
246
+ { ...part('routing on ', { dimColor: true }), priority: 4 },
247
+ button({ key: 'band-manual', label: 'Turn off', onPress: () => actions.mode('manual') }),
248
+ )
249
+ : null,
250
+ segment(
251
+ view.bandDetail ? 0 : 3,
252
+ button({ key: 'band-detail', label: view.bandDetail ? '1 row' : '2 rows', onPress: actions.toggleDetail }),
253
+ ),
254
+ ].filter(Boolean);
209
255
  }
210
256
 
211
257
  export function renderBand({ Box, Text, Button }, config, view, usage, { columns, agentId }, actions) {
@@ -216,36 +262,17 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
216
262
  const line = kept.flatMap((s, i) => (i ? [part(SEPARATOR, { dimColor: true }), ...s.parts] : s.parts));
217
263
  line.push(part(' '), button({ key: 'details', label: 'Router', plain: true, onPress: actions.open }));
218
264
  const routing = view.mode === 'auto' && view.phase !== 'unavailable';
265
+ const hover = fitSegments(hoverSegments(view, actions, routing), columns, GAP);
219
266
  return Box({
220
267
  key: 'router-band',
221
268
  flexDirection: 'column',
222
269
  children: [
223
270
  row(line),
224
- view.bandDetail && routing ? row(detailRow(config, view, usage)) : null,
225
- // Revealed while the pointer is over the band; no digit hotkeys, since a bare digit in an
226
- // empty prompt would press them.
271
+ view.bandDetail && routing ? row(detailRow(config, view, usage, columns)) : null,
227
272
  Box({
228
273
  display: 'none',
229
274
  hover: { display: 'flex' },
230
- children: [
231
- Text({ dimColor: true, children: routing ? 'pin next turn ' : '' }),
232
- ...(routing
233
- ? TIERS.flatMap((tier) => [
234
- Button({ key: `band-pin-${tier}`, label: tier, onPress: () => actions.pin(tier) }),
235
- Text({ children: ' ' }),
236
- ])
237
- : []),
238
- Text({ children: routing ? ' ' : '' }),
239
- routing
240
- ? Button({ key: 'band-manual', label: 'Routing off', onPress: () => actions.mode('manual') })
241
- : Button({ key: 'band-auto-row', label: 'Routing on', onPress: () => actions.mode('auto') }),
242
- Text({ children: ' ' }),
243
- Button({
244
- key: 'band-detail',
245
- label: view.bandDetail ? '1 row' : '2 rows',
246
- onPress: actions.toggleDetail,
247
- }),
248
- ],
275
+ children: hover.flatMap((s, i) => (i ? [part(GAP), ...s.parts] : s.parts)).map(draw),
249
276
  }),
250
277
  ],
251
278
  });
package/lib/config.mjs CHANGED
@@ -5,9 +5,10 @@ export const EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'];
5
5
  // What kind of work a turn does, named by what it produces. Closed: the classifier question is built for this set.
6
6
  export const ACTIVITIES = ['code', 'debug', 'explore', 'plan', 'review', 'ops', 'docs'];
7
7
  export const ACTIVITY_VALUES = [...ACTIVITIES, 'uncertain'];
8
- // `off` asks nothing new and routes as 1.5; `shadow` asks and records but keeps the 1.5 route; `on` applies overrides.
8
+ // `off` asks nothing new and routes as 1.5; `shadow` asks and records but keeps the 1.5 route; `on`, the default since
9
+ // 1.7, applies overrides.
9
10
  export const ACTIVITY_MODES = ['off', 'shadow', 'on'];
10
- const SONNET_MEDIUM = { model: 'sonnet', effort: 'medium' };
11
+ const SONNET_HIGH = { model: 'sonnet', effort: 'high' };
11
12
  export const DEFAULTS = {
12
13
  baselineTier: 'low',
13
14
  // Haiku 5.5 is the worker at `low` and `micro`: at high effort it scored above Sonnet 5.5 at low, and at xhigh near
@@ -23,22 +24,24 @@ export const DEFAULTS = {
23
24
  },
24
25
  // Sparse per-field overrides of `routes` by activity; a missing activity, tier or field inherits the tier's route.
25
26
  // Coding is where Haiku is weakest (Terminal-Bench 4.0: Haiku 5.5 39.2%, Sonnet 5.5 70.6%), so `low` work that
26
- // edits, debugs, plans or reviews runs on Sonnet. On knowledge and terminal work Sonnet 5.5 matches Opus 5.5
27
- // (GDPval-AA 1844 vs 1846; Terminal-Bench 70.6% vs 66.4%), so docs, ops and explore at `medium` run on Sonnet.
28
- // Every cell reuses Sonnet at medium: one more cache than the base routes, not one per cell.
29
- activityRouting: 'shadow',
27
+ // edits, debugs, plans or reviews runs on Sonnet. At `high`, not `medium`: on FrontierCode Main Sonnet scored ~49%
28
+ // at high and ~37% at medium, below Haiku at high (~42%). Command runs at `medium` lasted 6-11 requests at the
29
+ // median in one developer's replay, long enough for Haiku to repay its cache write (~4 requests from Opus at ~350K
30
+ // context). Haiku trails on multi-step tool work (OSWorld 2.1: ~61% at high, Opus ~79% at medium), so `high` ops
31
+ // stays on Opus, and repeated tool errors still escalate. Moving explore and docs saved nothing in the replay, and
32
+ // Haiku trails on research (HLE with tools 50.1% vs Opus 63.0%), so they keep the base routes. Two cells, one more cache (Sonnet at high) than the base routes.
33
+ activityRouting: 'on',
30
34
  activities: {
31
- code: { low: SONNET_MEDIUM },
32
- debug: { low: SONNET_MEDIUM },
33
- explore: { medium: SONNET_MEDIUM },
34
- plan: { low: SONNET_MEDIUM },
35
- review: { low: SONNET_MEDIUM },
36
- ops: { medium: SONNET_MEDIUM },
37
- docs: { low: SONNET_MEDIUM, medium: SONNET_MEDIUM },
35
+ code: { low: SONNET_HIGH },
36
+ debug: { low: SONNET_HIGH },
37
+ plan: { low: SONNET_HIGH },
38
+ review: { low: SONNET_HIGH },
39
+ ops: { medium: { model: 'haiku', effort: 'high' } },
38
40
  },
39
41
  // `id` selects the native model. List prices in USD per million tokens; `cacheRead` is absolute,
40
- // not a multiplier (Opus 5.5 reads at 0.05x input, the rest at the standard 0.1x). Haiku 5.5 lists the rates for
41
- // prompts up to 100k tokens; above that Anthropic charges 5x on input, output and cache read, so estimates run low there. A price change must also change
42
+ // not a multiplier (Opus 5.5 reads at 0.05x input, the rest at the standard 0.1x). `longContext` multiplies every
43
+ // rate of a request whose prompt (input, cache read and cache write) is over `above` tokens: Haiku 5.5 lists its
44
+ // rates for prompts up to 100,000 tokens and 5x above. A price change must also change
42
45
  // test/fixtures/list-prices.json, with its source and date. `output` feeds the shadow estimate and the downgrade tax.
43
46
  // `efforts` lists what the model accepts; an empty list means no effort field and no adaptive thinking. A change
44
47
  // must also change test/fixtures/effort-support.json, from a new probe against the real API.
@@ -66,6 +69,7 @@ export const DEFAULTS = {
66
69
  input: 0.1,
67
70
  output: 0.5,
68
71
  cacheRead: 0.01,
72
+ longContext: { above: 100_000, multiplier: 5 },
69
73
  contextWindow: 1_000_000,
70
74
  billing: 'plan',
71
75
  efforts: EFFORTS,
@@ -92,8 +96,8 @@ export const DEFAULTS = {
92
96
  // Ceiling on a cold cache write to a `credits` model. No default model bills credits, so this is
93
97
  // inert until a user adds one in router.json; a Claude Code turn starts near 100k tokens.
94
98
  cashCapUsd: 2,
95
- // An activity applies only above this probability; below it the tier's base route runs. Not tuned on any
96
- // classifier yet: calibrate from the probe set and shadow stats.
99
+ // An activity applies only above this probability; below it a new task goes back to the base route. Not tuned on any
100
+ // classifier yet: the probe results record whether the label was right, not its probability.
97
101
  activityMass: 0.6,
98
102
  },
99
103
  // The active entry of `classifiers`. `api` names the wire protocol (CLASSIFIER_APIS); a user entry without one speaks
@@ -151,7 +155,7 @@ export const DEFAULTS = {
151
155
 
152
156
  // Keys a route, a model or a classifier entry may carry. The other closed sections take their key sets from DEFAULTS.
153
157
  const ROUTE_KEYS = ['model', 'effort'];
154
- const MODEL_KEYS = ['id', 'input', 'output', 'cacheRead', 'contextWindow', 'billing', 'efforts'];
158
+ const MODEL_KEYS = ['id', 'input', 'output', 'cacheRead', 'longContext', 'contextWindow', 'billing', 'efforts'];
155
159
  const CLASSIFIER_KEYS = ['label', 'api', 'endpoint', 'model', 'keyOption', 'timeoutMs'];
156
160
  // The wire protocols a classifier can speak. Each has an adapter in classifier-apis.mjs; a test keeps the two in step.
157
161
  export const CLASSIFIER_APIS = ['system-one', 'openai-decisions', 'ollama'];
@@ -337,6 +341,7 @@ const TUNING_FIELDS = {
337
341
  downgradeVotes: ['policy', 'downgradeVotes'],
338
342
  horizon: ['policy', 'downgradeHorizonTurns'],
339
343
  cashCapUsd: ['policy', 'cashCapUsd'],
344
+ activityMass: ['policy', 'activityMass'],
340
345
  };
341
346
 
342
347
  export function tuningOf(config) {
@@ -436,7 +441,12 @@ function checkShape(file) {
436
441
  }
437
442
  if (models !== undefined) {
438
443
  checkObject(models, 'models');
439
- for (const [alias, model] of Object.entries(models)) checkObject(model, `models.${alias}`, MODEL_KEYS);
444
+ for (const [alias, model] of Object.entries(models)) {
445
+ checkObject(model, `models.${alias}`, MODEL_KEYS);
446
+ // null removes a built-in surcharge.
447
+ if (model.longContext != null)
448
+ checkObject(model.longContext, `models.${alias}.longContext`, ['above', 'multiplier']);
449
+ }
440
450
  }
441
451
  if (cache !== undefined) {
442
452
  checkObject(cache, 'cache', Object.keys(DEFAULTS.cache));
@@ -484,19 +494,7 @@ function validate(config) {
484
494
  throw new Error(`${at}.effort must be one of ${EFFORTS.join(', ')} or null`);
485
495
  }
486
496
  }
487
- for (const [alias, model] of Object.entries(config.models)) {
488
- for (const field of ['input', 'cacheRead', 'contextWindow']) {
489
- if (!Number.isFinite(model[field]) || model[field] < 0)
490
- throw new Error(`models.${alias}.${field} must be a non-negative number`);
491
- }
492
- if (model.output !== undefined && !(Number.isFinite(model.output) && model.output >= 0))
493
- throw new Error(`models.${alias}.output must be a non-negative number`);
494
- if (typeof model.id !== 'string' || !model.id) throw new Error(`models.${alias}.id is required`);
495
- if (!['plan', 'credits'].includes(model.billing))
496
- throw new Error(`models.${alias}.billing must be plan or credits`);
497
- if (!Array.isArray(model.efforts) || model.efforts.some((e) => !EFFORTS.includes(e)))
498
- throw new Error(`models.${alias}.efforts must list valid effort levels`);
499
- }
497
+ for (const [alias, model] of Object.entries(config.models)) validateModel(`models.${alias}`, model);
500
498
  if (!TIERS.includes(config.baselineTier)) throw new Error(`baselineTier must be one of ${TIERS.join(', ')}`);
501
499
  const p = config.policy;
502
500
  for (const field of ['upgradeBase', 'jumpConfidence', 'downgradeMass', 'continuationMass', 'activityMass']) {
@@ -525,6 +523,25 @@ function validate(config) {
525
523
  }
526
524
  }
527
525
 
526
+ function validateModel(at, model) {
527
+ for (const field of ['input', 'cacheRead', 'contextWindow']) {
528
+ if (!Number.isFinite(model[field]) || model[field] < 0)
529
+ throw new Error(`${at}.${field} must be a non-negative number`);
530
+ }
531
+ if (model.output !== undefined && !(Number.isFinite(model.output) && model.output >= 0))
532
+ throw new Error(`${at}.output must be a non-negative number`);
533
+ if (model.longContext != null) {
534
+ const { above, multiplier } = model.longContext;
535
+ if (!isNumber(above, 0, { above: true, integer: true }))
536
+ throw new Error(`${at}.longContext.above must be a positive integer`);
537
+ if (!isNumber(multiplier, 0, { above: true })) throw new Error(`${at}.longContext.multiplier must be positive`);
538
+ }
539
+ if (typeof model.id !== 'string' || !model.id) throw new Error(`${at}.id is required`);
540
+ if (!['plan', 'credits'].includes(model.billing)) throw new Error(`${at}.billing must be plan or credits`);
541
+ if (!Array.isArray(model.efforts) || model.efforts.some((e) => !EFFORTS.includes(e)))
542
+ throw new Error(`${at}.efforts must list valid effort levels`);
543
+ }
544
+
528
545
  function validateClassifier(at, entry) {
529
546
  for (const field of ['label', 'endpoint', 'model']) {
530
547
  if (typeof entry[field] !== 'string' || !entry[field]) throw new Error(`${at}.${field} is required`);
package/lib/cost.mjs CHANGED
@@ -11,6 +11,18 @@ export function clampEffort(wanted, supported) {
11
11
  return supported[0];
12
12
  }
13
13
 
14
+ // The one place a model's list prices become rates: per million tokens for a request whose prompt (input, cache read
15
+ // and cache write) is `promptTokens`, with the model's long-context multiplier above its threshold. Every estimate
16
+ // prices through here.
17
+ export function ratesAt(model, promptTokens) {
18
+ const k = model.longContext && promptTokens > model.longContext.above ? model.longContext.multiplier : 1;
19
+ return {
20
+ input: model.input * k,
21
+ cacheRead: model.cacheRead * k,
22
+ output: Number.isFinite(model.output) ? model.output * k : undefined,
23
+ };
24
+ }
25
+
14
26
  export function nextContextTokens(facts) {
15
27
  const previous = facts.lastRequest ? facts.lastRequest.tokens + facts.lastRequest.outputTokens : 0;
16
28
  return Math.max(previous, Number.isFinite(facts.contextTokens) ? facts.contextTokens : 0);
@@ -41,7 +53,7 @@ export function cacheState(state, now, cache) {
41
53
 
42
54
  // Bounds are scenarios, never an independent ledger. Unknown incumbent cache can still be served.
43
55
  export function inputBounds(config, route, tokens, facts, now) {
44
- const model = routeSpec(config, route);
56
+ const model = ratesAt(routeSpec(config, route), tokens);
45
57
  const state = facts.models[routeCacheKey(config, route, facts.effort)];
46
58
  const prefix = Math.min(state?.prefixTokens ?? tokens, tokens);
47
59
  const fresh = isWarm(state, now, config.cache);
@@ -53,7 +65,10 @@ export function inputBounds(config, route, tokens, facts, now) {
53
65
  }
54
66
 
55
67
  export function coldWriteUsd(config, alias, tokens) {
56
- return (config.models[alias].input * Math.max(...Object.values(config.cache.writeMultiplier)) * tokens) / 1e6;
68
+ return (
69
+ (ratesAt(config.models[alias], tokens).input * Math.max(...Object.values(config.cache.writeMultiplier)) * tokens) /
70
+ 1e6
71
+ );
57
72
  }
58
73
 
59
74
  export function switchingTaxUsd(config, candidate, incumbent, facts, now) {
@@ -63,11 +78,23 @@ export function switchingTaxUsd(config, candidate, incumbent, facts, now) {
63
78
  );
64
79
  }
65
80
 
81
+ // The next request on `candidate` against `incumbent` as a range: the input bounds crossed, plus the output difference
82
+ // at the last reply's length when both routes have an output price, as shadowEconomics compares.
83
+ export function nextTurnRange(config, candidate, incumbent, facts, now) {
84
+ const tokens = nextContextTokens(facts);
85
+ const proposed = inputBounds(config, candidate, tokens, facts, now);
86
+ const current = inputBounds(config, incumbent, tokens, facts, now);
87
+ const [to, from] = [ratesAt(routeSpec(config, candidate), tokens), ratesAt(routeSpec(config, incumbent), tokens)];
88
+ const outputTokens = facts.lastRequest?.outputTokens ?? 0;
89
+ const output = to.output > 0 && from.output > 0 ? ((to.output - from.output) * outputTokens) / 1e6 : 0;
90
+ return { minUsd: proposed.min - current.max + output, maxUsd: proposed.max - current.min + output };
91
+ }
92
+
66
93
  export function shadowEconomics(config, candidate, incumbent, facts, now) {
67
- const proposed = routeSpec(config, candidate);
68
- const current = routeSpec(config, incumbent);
69
- if (!(proposed.output > 0) || !(current.output > 0)) return null;
70
94
  const tokens = nextContextTokens(facts);
95
+ const proposed = ratesAt(routeSpec(config, candidate), tokens);
96
+ const current = ratesAt(routeSpec(config, incumbent), tokens);
97
+ if (!(proposed.output > 0) || !(current.output > 0)) return null;
71
98
  const outputTokens = facts.lastRequest?.outputTokens ?? 0;
72
99
  const output = ((proposed.output - current.output) * outputTokens) / 1e6;
73
100
  const nextTurnUsd = switchingTaxUsd(config, candidate, incumbent, facts, now) + output;
package/lib/display.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { acceptActivity } from './activity.mjs';
2
+ import { readStore, storeSummary } from './activity-stats.mjs';
2
3
  import {
3
4
  activeClassifier,
4
5
  CLASSIFIER_OPTIONS,
@@ -7,7 +8,9 @@ import {
7
8
  routeSpec,
8
9
  sameRoute,
9
10
  } from './config.mjs';
11
+ import { ratesAt } from './cost.mjs';
10
12
  import { cellForModel, isSameModel } from './route.mjs';
13
+ import { emptyTotals, readout, readSavingsStore } from './savings.mjs';
11
14
 
12
15
  // Cost order, cheapest first, so the colors read as a scale. Hex values: every surface draws them.
13
16
  export const TIER_COLOR = { micro: '#5fd7af', low: '#5f9fe0', medium: '#d7af5f', high: '#e0708a' };
@@ -36,8 +39,10 @@ export function routeName(config, route) {
36
39
  // One letter per activity for the REPLIES strip: `docs` is `w` (writing), since `debug` has `d`.
37
40
  export const ACTIVITY_LETTER = { code: 'c', debug: 'd', explore: 'e', plan: 'p', review: 'r', ops: 'o', docs: 'w' };
38
41
 
39
- // Changes of tier between consecutive replies; a reply routing did not choose breaks no run.
40
- export const switchCount = (tiers) => tiers.slice(1).filter((tier, i) => tier && tiers[i] && tier !== tiers[i]).length;
42
+ // Changes of route between consecutive replies, from the (model, effort) each request went out with, so a move inside
43
+ // a tier counts and a later change of settings does not rewrite the count. A reply routing did not choose breaks no run.
44
+ export const switchCount = (routes) =>
45
+ routes.slice(1).filter((route, i) => route && routes[i] && route !== routes[i]).length;
41
46
 
42
47
  export const percent = (value) => `${Math.round(value * 100)}%`;
43
48
 
@@ -60,7 +65,7 @@ export const REASONS = {
60
65
  'model-unavailable': 'the requested model is not available',
61
66
  pinned: 'one-turn model pin',
62
67
  'activity-up': 'the activity needs a stronger route',
63
- 'activity-down': 'a cheaper route fits the activity and pays back its cache',
68
+ 'activity-down': 'a cheaper route fits the activity and is estimated to pay back its cache write',
64
69
  'activity-pending': 'a route for the activity does not pay back yet',
65
70
  };
66
71
 
@@ -212,7 +217,76 @@ export function activityDetailLines(config, view) {
212
217
  ];
213
218
  }
214
219
 
220
+ // The tool agreement kept across sessions, for `/router` without a surface. The view holds the store as last read.
221
+ export function storeAgreementLine(view) {
222
+ const { agreement, focus } = storeSummary(readStore(view.activityStore));
223
+ const share = ({ matched, total }) => `${matched} of ${total}`;
224
+ if (!agreement.total) return 'Agreement across sessions: no labelled turns yet';
225
+ const all = `${share(agreement)} turns (${percent(agreement.matched / agreement.total)})`;
226
+ const code = focus.total ? ` · code/ops/explore ${share(focus)} (${percent(focus.matched / focus.total)})` : '';
227
+ return `Agreement across sessions: ${all}${code}`;
228
+ }
229
+
230
+ // Routing vs your model: less is green, more is amber, not red: a stronger model is a purchase, not a fault.
231
+ export const SAVINGS_COLOR = { less: '#6fcf97', more: '#f0b429' };
232
+
233
+ // "$6.40"; signed for a difference or a part: "−$1.84", "+$0.47", "$0.00".
234
+ export const usd = (value) => `$${Math.abs(value).toFixed(2)}`;
235
+ export const signedUsd = (value) => (value === 0 ? '$0.00' : `${value < 0 ? '−' : '+'}${usd(value)}`);
236
+ export const savingsColor = (value) => (value < 0 ? SAVINGS_COLOR.less : value > 0 ? SAVINGS_COLOR.more : undefined);
237
+
238
+ // The difference against your model: "−22%", "+15%", or "×5.0" once routing cost twice your model or more. Null when
239
+ // it rounds to nothing or there is nothing to compare.
240
+ export function differenceShare(r) {
241
+ if (r.differenceUsd === 0 || r.share === null) return null;
242
+ if (r.ratio >= 2) return `×${r.ratio.toFixed(1)}`;
243
+ const share = Math.round(r.share * 100);
244
+ return share === 0 ? null : `${share < 0 ? '−' : '+'}${Math.abs(share)}%`;
245
+ }
246
+
247
+ // "−$1.84 (−22%)".
248
+ export const savingsAmount = (r) => {
249
+ const share = differenceShare(r);
250
+ return `${signedUsd(r.differenceUsd)}${share ? ` (${share})` : ''}`;
251
+ };
252
+
253
+ export const yourModel = (view) => routeLabel(view.nativeModel, view.nativeEffort);
254
+ export const sinceDate = (ms) => new Date(ms).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
255
+ const replyCount = (n, what = 'replies') => `${n} ${n === 1 ? what.replace(/ies$/, 'y') : what}`;
256
+
257
+ // The model most stronger replies ran on, and how many ran on stronger models.
258
+ export function strongerReplies(totals) {
259
+ const [model] = Object.entries(totals?.stronger ?? {}).sort((a, b) => b[1] - a[1])[0] ?? [];
260
+ const count = Object.values(totals?.stronger ?? {}).reduce((sum, n) => sum + n, 0);
261
+ return { model: model ?? null, count, several: Object.keys(totals?.stronger ?? {}).length > 1 };
262
+ }
263
+
264
+ // The `/router` line without a surface: this session's difference, or why there is none yet, with the saved totals
265
+ // when this session has no routed reply.
266
+ export function savingsLine(view) {
267
+ const label = `vs your model (${yourModel(view)})`;
268
+ const session = readout(view.savings ?? emptyTotals());
269
+ const store = readSavingsStore(view.savingsStore);
270
+ const across = readout(store);
271
+ if (!session.replies) {
272
+ const saved = across.early
273
+ ? ''
274
+ : `; ${savingsAmount(across)} since ${sinceDate(store.since)}, each session compared with its own model`;
275
+ return `${label}: no routed replies this session${saved}`;
276
+ }
277
+ if (session.early) return `${label}: too early, ${replyCount(session.replies, 'routed replies')}`;
278
+ const stronger = strongerReplies(view.savings);
279
+ const replies =
280
+ session.differenceUsd > 0 && stronger.count
281
+ ? `${stronger.count} of ${replyCount(session.replies)} on ${stronger.several ? 'stronger models' : modelName(stronger.model)}`
282
+ : replyCount(session.replies);
283
+ return `${label}: ${savingsAmount(session)} this session, ${replies}, est. at list prices`;
284
+ }
285
+
215
286
  export const GATEWAY_SETTINGS = 'v0.8 gateway settings remain · see /router';
287
+ // Why routing is unavailable, as the band and the pane say it: they show the gateway cleanup themselves.
288
+ export const unavailableReason = (view) =>
289
+ view.error === GATEWAY_SETTINGS ? 'v0.8 gateway settings remain' : (view.error ?? 'unknown error');
216
290
  export const GATEWAY_CLEANUP = [
217
291
  'Remove from settings.json, then restart: model jev-router[1m], its modelPicker row,',
218
292
  'env.ANTHROPIC_BASE_URL for 127.0.0.1:43170, env.CLAUDE_CODE_GATEWAY_HINT_HEADERS,',
@@ -249,8 +323,9 @@ export function usageMetrics(config, view, usage) {
249
323
  const nextContext = candidates.length ? Math.max(...candidates) : null;
250
324
  const reuse = view.inputTokens > 0 && Number.isFinite(view.cacheRead) ? view.cacheRead / view.inputTokens : null;
251
325
  const cost = Number.isFinite(usage?.cost?.usd) ? usage.cost.usd : null;
326
+ const rates = spec && ratesAt(spec, view.inputTokens ?? 0);
252
327
  const cacheBenefit =
253
- spec && Number.isFinite(view.cacheRead) ? ((spec.input - spec.cacheRead) * view.cacheRead) / 1e6 : null;
328
+ rates && Number.isFinite(view.cacheRead) ? ((rates.input - rates.cacheRead) * view.cacheRead) / 1e6 : null;
254
329
  return {
255
330
  window,
256
331
  observed,