@alexeiled/claude-router 1.17.1 → 1.19.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.
@@ -96,13 +96,10 @@ import {
96
96
  resetHistory,
97
97
  } from '../lib/route.mjs';
98
98
  import {
99
- addReplies,
100
99
  afterReset,
101
100
  compareModels,
102
101
  emptySavingsStore,
103
102
  mergeSavingsStores,
104
- modelAlias,
105
- modelEntry,
106
103
  modelSpec,
107
104
  observedReplyUsd,
108
105
  readSavingsStore,
@@ -424,10 +421,6 @@ async function readProject($, profilePath) {
424
421
  async function updateView($, router, patch) {
425
422
  const value = router.view ?? (await readView($, router));
426
423
  router.view = { ...value, ...patch };
427
- // The session readout is against the model /model selects now, so a /model change shows the whole session against
428
- // the new one.
429
- if ('nativeModel' in patch || 'savingsBy' in patch)
430
- router.view.savings = modelEntry(router.view.savingsBy, modelAlias(router.config, router.view.nativeModel)) ?? null;
431
424
  await $.state.set(VIEW, router.view);
432
425
  }
433
426
 
@@ -484,9 +477,14 @@ async function readOthers($, router, kind, { sessionId, resetAt, keys }) {
484
477
 
485
478
  // The view's sums of `kinds`, this session's record and the others', from what the router holds; a kind whose others
486
479
  // were never read gives nothing.
480
+ // The replies of turns still running, in the view, so the band and the pane count them before the turn is recorded.
481
+ const liveUsage = (router) => [...router.turnUsage.values()].flat();
482
+
487
483
  function ledgerView(router, kinds = LEDGER_KINDS) {
488
- const patch = {};
489
- for (const kind of kinds) {
484
+ // The running turns' replies always go with the usage sums: a turn moves from one to the other, so a view written
485
+ // with only one of them would count it twice or miss it.
486
+ const patch = { usageLive: liveUsage(router) };
487
+ for (const kind of new Set([...kinds, 'usage'])) {
490
488
  const { others, own } = router.ledgers[kind];
491
489
  const { field, ownField, merge, empty } = LEDGERS[kind];
492
490
  if (!others) continue;
@@ -734,7 +732,7 @@ function detailText(config, view) {
734
732
  ...activityDetailLines(config, view),
735
733
  storeAgreementLine(view),
736
734
  ...[unpricedLine(view.activityMetrics)].filter(Boolean),
737
- savingsLine(view),
735
+ savingsLine(config, view, Date.now()),
738
736
  view.error || missing ? classifierStatus(config, view.error ?? missing) : `${activeClassifier(config).label} ready`,
739
737
  `Context: ${view.contextKnown ? `${view.contextTokens} tokens (estimate)` : 'unknown'}`,
740
738
  `Observed cache: ${view.cacheRead ?? 'unknown'} read, ${view.cacheWrite ?? 'unknown'} written tokens`,
@@ -1138,8 +1136,8 @@ async function publishReply($, router, turnId, response, tier, route, routedUsd,
1138
1136
  });
1139
1137
  }
1140
1138
 
1141
- // A routed reply written to the loop at `loopVersion`, then published: the view's metrics and the routing-vs-your-model
1142
- // totals. A lost write publishes nothing.
1139
+ // A routed reply written to the loop at `loopVersion`, then published: the view's metrics and the reply's usage entry
1140
+ // for routing vs your model. A lost write publishes nothing.
1143
1141
  async function observeRouted($, router, { turnId, cfg, loop, loopVersion, request, response, nativeModel }) {
1144
1142
  // When the reply arrived: the awaits below can cross midnight or the hour it is kept under.
1145
1143
  const at = Date.now();
@@ -1179,7 +1177,7 @@ async function observeRouted($, router, { turnId, cfg, loop, loopVersion, reques
1179
1177
  // key can name another model later.
1180
1178
  const idOf = (model) => modelSpec(cfg, model)?.id ?? model;
1181
1179
  if (compared)
1182
- await addSavings($, router, {
1180
+ await addUsageEntry($, router, {
1183
1181
  turnId,
1184
1182
  compared,
1185
1183
  tier,
@@ -1213,7 +1211,7 @@ async function cacheTtlOf($) {
1213
1211
  // A main reply against every configured model (compareModels): the loop's next states, the reply's prices by models
1214
1212
  // key, and `reply`, its prices against your model now, or null. The readout never breaks a turn. A reply whose cache
1215
1213
  // lifetime cannot be read still moves the models' caches and is not counted; any other failure leaves the loop's
1216
- // states and the totals as they were.
1214
+ // states as they were and counts nothing.
1217
1215
  async function compareYours($, router, loop, response, servedEffort, nativeModel, cfg = router.config) {
1218
1216
  try {
1219
1217
  if (!response.usage?.model) return null;
@@ -1233,10 +1231,10 @@ async function compareYours($, router, loop, response, servedEffort, nativeModel
1233
1231
  }
1234
1232
  }
1235
1233
 
1236
- // A routed reply's prices added to each model's session totals in the view, and the reply, priced against your model
1237
- // now, to the turn's replies for the usage record. `route` is the served model's `id@effort`, `yours` your model's id,
1234
+ // A routed reply, priced against your model now, added to the turn's running replies, which the view counts at once
1235
+ // and turn.complete adds to the usage record. `route` is the served model's `id@effort`, `yours` your model's id,
1238
1236
  // `byId` the reply priced against each model as yours, keyed by model id; `at` when the reply arrived (ms).
1239
- async function addSavings($, router, { turnId, compared, tier, route, yours, byId, usage, at }) {
1237
+ async function addUsageEntry($, router, { turnId, compared, tier, route, yours, byId, usage, at }) {
1240
1238
  try {
1241
1239
  const view = router.view ?? (await readView($, router));
1242
1240
  if (compared.reply) {
@@ -1253,17 +1251,28 @@ async function addSavings($, router, { turnId, compared, tier, route, yours, byI
1253
1251
  usage,
1254
1252
  });
1255
1253
  router.turnUsage.set(turnId, [...(router.turnUsage.get(turnId) ?? []), entry]);
1254
+ await updateView($, router, ledgerView(router, ['usage']));
1256
1255
  }
1257
- await updateView($, router, { savingsBy: addReplies(view.savingsBy ?? {}, compared.replies) });
1258
1256
  } catch {}
1259
1257
  }
1260
1258
 
1261
- // A finished turn's replies, and the switch that started it, added to this session's usage record.
1262
- async function flushUsage($, router, entries, switched) {
1259
+ // A finished turn's replies, and the switch that started it, added to this session's usage record. The turn leaves
1260
+ // the running replies in the tick it enters the held record, so a view written at any point counts it once. A failed
1261
+ // store write keeps it in the held record, which the next turn writes; a failed read or a Reset before that drops it
1262
+ // from both.
1263
+ async function flushUsage($, router, turnId, switched) {
1263
1264
  try {
1264
- if (await addToLedger($, router, 'usage', (record, now) => recordUsage(record, entries, { switched, now })))
1265
- await updateView($, router, ledgerView(router, ['usage']));
1266
- } catch {}
1265
+ await addToLedger($, router, 'usage', (record, now) => {
1266
+ // Read here, not before the awaits: a reply can still join the turn while the record is read.
1267
+ const entries = router.turnUsage.get(turnId) ?? [];
1268
+ router.turnUsage.delete(turnId);
1269
+ return recordUsage(record, entries, { switched, now });
1270
+ });
1271
+ } catch {
1272
+ } finally {
1273
+ router.turnUsage.delete(turnId);
1274
+ await updateView($, router, ledgerView(router, ['usage'])).catch(() => {});
1275
+ }
1267
1276
  }
1268
1277
 
1269
1278
  // A turn's prompt, its origin and the classifier's request text. A progress turn asks about the goal it serves, with
@@ -1865,7 +1874,6 @@ function paneActions($, router, view) {
1865
1874
  }
1866
1875
  await updateView($, router, {
1867
1876
  activityStats: null,
1868
- savingsBy: {},
1869
1877
  confirmDelete: false,
1870
1878
  usageOpen: null,
1871
1879
  ...ledgerView(router),
@@ -2186,9 +2194,7 @@ export function register(on, options) {
2186
2194
  if (turn?.mode === 'off') {
2187
2195
  if ((await $.session.id()) === turn.sessionId) router.afterDown = advanceDown(router.afterDown, turn).after;
2188
2196
  } else if (turn) await recordActivity($, router, turn);
2189
- const entries = router.turnUsage.get(e.turnId);
2190
- router.turnUsage.delete(e.turnId);
2191
- if (entries) await flushUsage($, router, entries, turn?.switched ?? null);
2197
+ if (router.turnUsage.has(e.turnId)) await flushUsage($, router, e.turnId, turn?.switched ?? null);
2192
2198
  await flushDecisions($, router, e.turnId, turn);
2193
2199
  return next(e);
2194
2200
  });
package/lib/band.mjs CHANGED
@@ -4,7 +4,8 @@ import {
4
4
  classifierLabel,
5
5
  classifierStatus,
6
6
  classifierWarns,
7
- differenceShare,
7
+ comparedModel,
8
+ costParts,
8
9
  modeLever,
9
10
  modelColor,
10
11
  modelName,
@@ -12,17 +13,18 @@ import {
12
13
  percentPair,
13
14
  reasonActivity,
14
15
  routeLabel,
16
+ routingAnswer,
15
17
  routingModeText,
16
- SAVINGS_COLOR,
18
+ savingsColor,
19
+ sessionUsage,
17
20
  shortReason,
18
21
  TIER_COLOR,
19
22
  unavailableReason,
20
23
  usageMetrics,
21
24
  usd,
22
- yourModel,
23
25
  } from './display.mjs';
24
26
  import { isSameModel } from './route.mjs';
25
- import { emptyTotals, modelSpec, readout } from './savings.mjs';
27
+ import { modelSpec, readout } from './savings.mjs';
26
28
  import { PASS_WHY } from './subagents.mjs';
27
29
 
28
30
  const BARS = '▂▄▆█';
@@ -202,7 +204,7 @@ export function bandSegments(config, view, usage, actions) {
202
204
  segment(5, part([routingModeText(view), modeLever(config, view)].filter(Boolean).join(' · '), { dimColor: true })),
203
205
  support ? segment(4, part(support, { dimColor: true })) : null,
204
206
  Number.isFinite(switchUsd) && switchUsd >= 0.005
205
- ? segment(4, part(`switch cost ≈ ${usd(switchUsd)}`, { dimColor: true }))
207
+ ? segment(4, part(`next switch ≈ ${usd(switchUsd)}`, { dimColor: true }))
206
208
  : null,
207
209
  metrics.contextBar.percent === null && metrics.cacheBar.percent === null
208
210
  ? null
@@ -210,42 +212,45 @@ export function bandSegments(config, view, usage, actions) {
210
212
  ].filter(Boolean);
211
213
  }
212
214
 
213
- // The larger of the two costs that cut a saving or made an extra, if the session had one: "stronger models +$0.68".
214
- function costPart(r) {
215
- const [label, value] =
216
- r.strongerUsd >= r.switchUsd ? ['stronger models', r.strongerUsd] : ['cold-cache writes', r.switchUsd];
217
- return value > 0 ? `${label} +${usd(value)}` : null;
218
- }
219
-
220
- // The second row of the detailed band: this session against the model /model selects now, one scope end to end, so a
221
- // /model change shows the whole session against the new one. The answer stays; the reply count and the cost behind it
222
- // drop first when the band is narrow.
223
- export function sessionRow(config, view, columns) {
215
+ // The second row of the detailed band: this session against your model, as the pane's SESSION block starts, and the
216
+ // parts that add up to it: "routing +$0.18 (+4%) vs Opus 5.5 · high = −$0.42 cheaper models +$0.60 cache
217
+ // rebuilds · 34 replies". When the band is narrow the parts lose their words, then the reply count goes, then the
218
+ // parts.
219
+ export function sessionRow(config, view, columns, now = Date.now()) {
224
220
  const label = part('session ', { dimColor: true });
225
221
  if (!modelSpec(config, view.nativeModel))
226
222
  return [label, part(`${modelName(view.nativeModel)} has no list price`, { dimColor: true })];
227
- const r = readout(view.savings ?? emptyTotals());
223
+ const summary = sessionUsage(view, now);
224
+ const r = readout(summary);
228
225
  if (!r.replies) return [part('no replies yet', { dimColor: true })];
229
- const replies = `${r.replies} repl${r.replies === 1 ? 'y' : 'ies'}`;
230
- if (r.early) return [label, part(`${replies} · too early to compare`, { dimColor: true })];
231
- const share = differenceShare(r)?.replace(/^[−+]/, '');
232
- const amount = `${usd(r.differenceUsd)}${share ? ` (${share})` : ''}`;
233
- const answer =
234
- r.differenceUsd < 0
235
- ? [part(`saved ${amount}`, { color: SAVINGS_COLOR.less, bold: true }), part(` vs ${yourModel(view)}`)]
236
- : r.differenceUsd > 0
237
- ? [part(`extra ${amount}`, { color: SAVINGS_COLOR.more, bold: true }), part(` vs ${yourModel(view)}`)]
238
- : [part(`same cost as ${yourModel(view)}`)];
239
- const cost = costPart(r);
240
- const kept = fitSegments(
241
- [
242
- segment(0, label, answer),
243
- segment(2, part(replies, { dimColor: true })),
244
- cost ? segment(3, part(cost, { dimColor: true })) : null,
245
- ].filter(Boolean),
246
- columns,
247
- );
248
- return kept.flatMap((s, i) => (i ? [part(SEPARATOR, { dimColor: true }), ...s.parts] : s.parts));
226
+ const count = `${r.replies} repl${r.replies === 1 ? 'y' : 'ies'}`;
227
+ const answer = [
228
+ label,
229
+ part(routingAnswer(r), { color: savingsColor(r.differenceUsd), bold: true }),
230
+ part(` vs ${comparedModel(config, view, summary)}`),
231
+ ];
232
+ const width = (row) => row.reduce((n, p) => n + p.text.length, 0);
233
+ const fit = (rows) =>
234
+ fitSegments(
235
+ [segment(0, ...(rows.find((row) => !Number.isFinite(columns) || width(row) <= columns) ?? answer))],
236
+ columns,
237
+ ).flatMap((s) => s.parts);
238
+ // Below EARLY_REPLIES one switch decides the sum, so the parts wait, as on the pane.
239
+ if (r.early) return fit([[...answer, part(`${SEPARATOR}${count} · early sample`, { dimColor: true })], answer]);
240
+ const replies = part(`${SEPARATOR}${count}`, { dimColor: true });
241
+ const parts = costParts(r);
242
+ const named = parts.flatMap(([amount, words, value], i) => [
243
+ part(i ? ' ' : ' = ', { dimColor: true }),
244
+ part(`${amount} ${words}`, { color: savingsColor(value) }),
245
+ ]);
246
+ const bare = parts.length
247
+ ? [
248
+ part(` = ${parts.map(([amount], i) => (i ? amount.replace(/^([−+])/, '$1 ') : amount)).join(' ')}`, {
249
+ dimColor: true,
250
+ }),
251
+ ]
252
+ : [];
253
+ return fit([[...answer, ...named, replies], [...answer, ...bare, replies], [...answer, ...bare], answer]);
249
254
  }
250
255
 
251
256
  // The row revealed under the pointer: pins and the mode control while routing is on, and the one- or two-row choice.
package/lib/display.mjs CHANGED
@@ -13,8 +13,8 @@ import { ratesAt } from './cost.mjs';
13
13
  import { applyMode, modeLabel, modeRoute } from './modes.mjs';
14
14
  import { PROBE_RESULTS } from './probe-results.mjs';
15
15
  import { cellForModel } from './route.mjs';
16
- import { emptyTotals, readout } from './savings.mjs';
17
- import { allTime } from './usage.mjs';
16
+ import { modelSpec, readout } from './savings.mjs';
17
+ import { allTime, periodRange, readUsage, summarize, withReplies } from './usage.mjs';
18
18
 
19
19
  // Cost order, cheapest first, so the colors read as a scale. Hex values: every surface draws them.
20
20
  export const TIER_COLOR = { micro: '#5fd7af', low: '#5f9fe0', medium: '#d7af5f', high: '#e0708a' };
@@ -339,33 +339,58 @@ export const yourModel = (view) => routeLabel(view.nativeModel, view.nativeEffor
339
339
  export const sinceDate = (ms) => new Date(ms).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
340
340
  const replyCount = (n, what = 'replies') => `${n} ${n === 1 ? what.replace(/ies$/, 'y') : what}`;
341
341
 
342
- // The model most stronger replies ran on, and how many ran on stronger models.
343
- export function strongerReplies(totals) {
344
- const [model] = Object.entries(totals?.stronger ?? {}).sort((a, b) => b[1] - a[1])[0] ?? [];
345
- const count = Object.values(totals?.stronger ?? {}).reduce((sum, n) => sum + n, 0);
346
- return { model: model ?? null, count, several: Object.keys(totals?.stronger ?? {}).length > 1 };
342
+ // A usage record with the running turns' replies (view.usageLive) added, which every readout counts: the band, the
343
+ // pane and /router show the same replies at the same moment.
344
+ export const liveUsage = (view, record) => withReplies(readUsage(record), view.usageLive ?? []);
345
+ // This session's routed replies, the running turns' included.
346
+ export const sessionUsage = (view, now) => summarize(liveUsage(view, view.usageSession), periodRange('session', now));
347
+
348
+ // The models a session's replies were priced against, when they are not the one `/model` selects now: after a
349
+ // `/model` change the record keeps each reply against the model selected when it ran. Null when they are.
350
+ export function recordedModels(config, view, summary) {
351
+ const ids = Object.keys(summary.yours ?? {});
352
+ if (!ids.length || (ids.length === 1 && ids[0] === modelSpec(config, view.nativeModel)?.id)) return null;
353
+ return ids.map(modelName).join(', ');
347
354
  }
355
+ // The model a session's replies were priced against: the one /model selects now, with its effort, or the models the
356
+ // record kept them against.
357
+ export const comparedModel = (config, view, summary) => recordedModels(config, view, summary) ?? yourModel(view);
358
+
359
+ // The words every readout uses. Routing against your model: "routing −$1.84 (−22%)", minus less than your model, plus
360
+ // more; then the parts that add up to it.
361
+ export const routingAnswer = (r) => `routing ${savingsAmount(r)}`;
362
+ export const COST_PART = { cheaper: 'cheaper models', stronger: 'stronger models', switches: 'cache rebuilds' };
363
+ // The parts of a readout that are not zero, as [signed amount, words]: "−$2.31", "cheaper models".
364
+ export function costParts(r) {
365
+ return [
366
+ [r.cheaperUsd, COST_PART.cheaper],
367
+ [r.strongerUsd, COST_PART.stronger],
368
+ [r.switchUsd, COST_PART.switches],
369
+ ]
370
+ .filter(([value]) => value !== 0)
371
+ .map(([value, words]) => [signedUsd(value), words, value]);
372
+ }
373
+ // Cache rebuilds cost more than the cheaper models saved: the switches lost money.
374
+ export const switchesLost = (r) => r.switchUsd > 0 && r.switchUsd > -r.cheaperUsd;
348
375
 
349
- // The `/router` line without a surface: this session's difference, or why there is none yet, with the saved totals
376
+ // The `/router` line without a surface: this session's readout, or why there is none yet, with the saved totals
350
377
  // when this session has no routed reply.
351
- export function savingsLine(view) {
352
- const label = `vs your model (${yourModel(view)})`;
353
- const session = readout(view.savings ?? emptyTotals());
354
- const kept = allTime(view.usageStore, view.savingsStore);
355
- const across = readout(kept.totals);
378
+ export function savingsLine(config, view, now) {
379
+ const summary = sessionUsage(view, now);
380
+ const session = readout(summary);
381
+ const label = `vs ${comparedModel(config, view, summary)}`;
356
382
  if (!session.replies) {
383
+ const kept = allTime(view.usageStore, view.savingsStore);
384
+ const across = readout(kept.totals);
357
385
  const saved = across.early
358
386
  ? ''
359
- : `; ${savingsAmount(across)} since ${sinceDate(kept.since)}, each session compared with its own model`;
360
- return `${label}: no routed replies this session${saved}`;
387
+ : `; ${routingAnswer(across)} since ${sinceDate(kept.since)}, each session against its own model`;
388
+ return `Session ${label}: no routed replies yet${saved}`;
361
389
  }
362
- if (session.early) return `${label}: too early, ${replyCount(session.replies, 'routed replies')}`;
363
- const stronger = strongerReplies(view.savings);
364
- const replies =
365
- session.differenceUsd > 0 && stronger.count
366
- ? `${stronger.count} of ${replyCount(session.replies)} on ${stronger.several ? 'stronger models' : modelName(stronger.model)}`
367
- : replyCount(session.replies);
368
- return `${label}: ${savingsAmount(session)} this session, ${replies}, est. at list prices`;
390
+ if (session.early)
391
+ return `Session: ${routingAnswer(session)} ${label}, ${replyCount(session.replies)}, early sample, est. at list prices`;
392
+ const parts = costParts(session).map(([amount, words]) => `${amount} ${words}`);
393
+ return `Session: ${routingAnswer(session)} ${label}${parts.length ? ` = ${parts.join(' ')}` : ''}, ${replyCount(session.replies)}, est. at list prices`;
369
394
  }
370
395
 
371
396
  export const GATEWAY_SETTINGS = 'v0.8 gateway settings remain · see /router';
package/lib/modes.mjs CHANGED
@@ -3,19 +3,6 @@
3
3
  import { ACTIVITIES, DEFAULTS, EFFORTS, ROUTING_MODES, rank, resolveRoute, sameRoute, TIERS } from './config.mjs';
4
4
  import { clampEffort } from './cost.mjs';
5
5
 
6
- // `wanted` on a model that lists `supported`: itself, else the next higher listed, else the highest lower one.
7
- export function projectEffort(supported, wanted) {
8
- if (supported.includes(wanted)) return wanted;
9
- const at = EFFORTS.indexOf(wanted);
10
- return (
11
- EFFORTS.slice(at + 1).find((e) => supported.includes(e)) ??
12
- EFFORTS.slice(0, at)
13
- .reverse()
14
- .find((e) => supported.includes(e)) ??
15
- null
16
- );
17
- }
18
-
19
6
  // The route's effort one level up (`step` 1) or down (-1), on what the model lists; null when it cannot move that
20
7
  // way: a null effort (the session's), a model without efforts, or no listed level in that direction. The model's
21
8
  // lowest listed level is the floor, so a route never loses its effort.
@@ -23,9 +10,9 @@ function steppedEffort(config, route, step) {
23
10
  const supported = config.models[route.model].efforts;
24
11
  const from = clampEffort(route.effort, supported);
25
12
  if (!from) return null;
26
- const wanted = EFFORTS[EFFORTS.indexOf(from) + step];
27
- const effort = wanted ? projectEffort(supported, wanted) : null;
28
- return effort && Math.sign(EFFORTS.indexOf(effort) - EFFORTS.indexOf(from)) === step ? effort : null;
13
+ const at = EFFORTS.indexOf(from);
14
+ const choices = step < 0 ? EFFORTS.slice(0, at).reverse() : EFFORTS.slice(at + 1);
15
+ return choices.find((effort) => supported.includes(effort)) ?? null;
29
16
  }
30
17
 
31
18
  // The route `mode` runs for tier and activity, always from the configured routes. `cost` takes the tier below (never