@alexeiled/claude-router 1.13.0 → 1.15.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 +46 -0
- package/README.md +3 -0
- package/docs/architecture.md +11 -1
- package/docs/configuration.md +6 -2
- package/docs/evaluation.md +8 -7
- package/docs/native-router.md +2 -1
- package/docs/router-band.svg +30 -8
- package/docs/router-pane-now.svg +134 -111
- package/docs/router-pane-policy.svg +47 -43
- package/docs/router-pane-usage-whatif.svg +75 -12
- package/docs/router-pane-usage.svg +79 -13
- package/docs/routing-modes.md +139 -0
- package/docs/user-guide.md +11 -5
- package/hooks/native-router.mjs +170 -23
- package/lib/activity.mjs +8 -2
- package/lib/band.mjs +43 -11
- package/lib/config.mjs +69 -17
- package/lib/cost.mjs +3 -0
- package/lib/display.mjs +21 -2
- package/lib/modes.mjs +98 -0
- package/lib/panel.mjs +338 -45
- package/lib/route.mjs +80 -23
- package/lib/subagents.mjs +182 -64
- package/lib/usage.mjs +32 -20
- package/lib/view.mjs +3 -0
- package/package.json +2 -1
- package/types/index.d.ts +34 -10
package/lib/cost.mjs
CHANGED
|
@@ -82,7 +82,10 @@ export function coldWriteUsd(config, alias, tokens) {
|
|
|
82
82
|
);
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
// Routes that share a cache key (one model at two efforts, where effort keeps the cache) write the same new tokens
|
|
86
|
+
// to the same cache: switching between them costs no write. Bounding them apart would charge the new tokens as a tax.
|
|
85
87
|
export function switchingTaxUsd(config, candidate, incumbent, facts, now) {
|
|
88
|
+
if (routeCacheKey(config, candidate, facts.effort) === routeCacheKey(config, incumbent, facts.effort)) return 0;
|
|
86
89
|
const tokens = nextContextTokens(facts);
|
|
87
90
|
return (
|
|
88
91
|
inputBounds(config, candidate, tokens, facts, now).max - inputBounds(config, incumbent, tokens, facts, now).min
|
package/lib/display.mjs
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
sameRoute,
|
|
10
10
|
} from './config.mjs';
|
|
11
11
|
import { ratesAt } from './cost.mjs';
|
|
12
|
+
import { applyMode, modeLabel } from './modes.mjs';
|
|
12
13
|
import { PROBE_RESULTS } from './probe-results.mjs';
|
|
13
14
|
import { cellForModel } from './route.mjs';
|
|
14
15
|
import { emptyTotals, readout } from './savings.mjs';
|
|
@@ -16,6 +17,18 @@ import { allTime } from './usage.mjs';
|
|
|
16
17
|
|
|
17
18
|
// Cost order, cheapest first, so the colors read as a scale. Hex values: every surface draws them.
|
|
18
19
|
export const TIER_COLOR = { micro: '#5fd7af', low: '#5f9fe0', medium: '#d7af5f', high: '#e0708a' };
|
|
20
|
+
// A subagent's square by the model family it ran on, apart from the tier colors so a model never reads as a tier.
|
|
21
|
+
const MODEL_COLOR = { haiku: '#c3e88d', sonnet: '#f78c6c', opus: '#c792ea', fable: '#89ddff' };
|
|
22
|
+
export const modelColor = (id) =>
|
|
23
|
+
Object.entries(MODEL_COLOR).find(([family]) => String(id ?? '').includes(family))?.[1] ?? null;
|
|
24
|
+
|
|
25
|
+
// The config as the view's routing mode routes it: the route texts name what runs, not the configured route. A view
|
|
26
|
+
// from before 1.15 has no mode and reads as balanced.
|
|
27
|
+
export const DEFAULT_ROUTING_MODE = { mode: 'balanced', source: 'default' };
|
|
28
|
+
export const routingModeOf = (view) => view.routingMode ?? DEFAULT_ROUTING_MODE;
|
|
29
|
+
export const shownConfig = (config, view) => applyMode(config, routingModeOf(view));
|
|
30
|
+
// `cost (session)`; the status band and the Now tab show it while routing is on.
|
|
31
|
+
export const routingModeText = (view) => modeLabel(routingModeOf(view));
|
|
19
32
|
|
|
20
33
|
// Why routing starts off: the session's model is no tier's model. Set at session start, read by the band.
|
|
21
34
|
export const NOT_A_TIER_REASON = 'not a routing tier';
|
|
@@ -187,7 +200,8 @@ export function activityReading(view) {
|
|
|
187
200
|
}
|
|
188
201
|
|
|
189
202
|
// The route the turn's cell resolves to, against the tier's base route. Null outside `on`, or before a turn has a tier.
|
|
190
|
-
function activityRouteText(
|
|
203
|
+
function activityRouteText(configured, view) {
|
|
204
|
+
const config = shownConfig(configured, view);
|
|
191
205
|
if (config.activityRouting !== 'on' || !view.tier) return null;
|
|
192
206
|
const cell = `${view.tier}${view.activity ? ` + ${view.activity}` : ''}`;
|
|
193
207
|
const route = resolveRoute(config, view.tier, view.activity ?? null);
|
|
@@ -203,7 +217,8 @@ const STAYED = ['ready', 'same-tier', 'no-advice', 'uncertain', 'downgrade-held'
|
|
|
203
217
|
// The Now tab's Why. In `on`, an activity move, or a stay on an override cell, says the cell plainly: "coding at low
|
|
204
218
|
// runs on Sonnet 5.5 · high", or for a move to no activity "low runs on its base route, Haiku 5.5 · high".
|
|
205
219
|
// Every other decision keeps its reason: a refusal, a hold or a tier move says more than the cell.
|
|
206
|
-
export function whyText(
|
|
220
|
+
export function whyText(configured, view) {
|
|
221
|
+
const config = shownConfig(configured, view);
|
|
207
222
|
const reason = REASONS[view.reason] ?? view.reason ?? 'ready';
|
|
208
223
|
if (config.activityRouting !== 'on' || !view.tier) return reason;
|
|
209
224
|
const activity = view.activity ?? null;
|
|
@@ -211,6 +226,10 @@ export function whyText(config, view) {
|
|
|
211
226
|
const override = activity !== null && !sameRoute(route, resolveRoute(config, view.tier));
|
|
212
227
|
const moved = view.reason === 'activity-up' || view.reason === 'activity-down';
|
|
213
228
|
if (!moved && !(override && STAYED.includes(view.reason ?? 'ready'))) return reason;
|
|
229
|
+
// A held move keeps the route in use, which a routing mode may have moved off this cell's route: say only why.
|
|
230
|
+
const runs =
|
|
231
|
+
routeSpec(config, route).id === view.selectedModel && (route.effort === null || route.effort === view.effort);
|
|
232
|
+
if (!moved && view.selectedModel && !runs) return reason;
|
|
214
233
|
return activity
|
|
215
234
|
? `${activityVerb(activity)} at ${view.tier} runs on ${routeName(config, route)}`
|
|
216
235
|
: `${view.tier} runs on its base route, ${routeName(config, route)}`;
|
package/lib/modes.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Routing modes (docs/plans/2026-10-21-routing-modes.md): one setting that trades cost against answer quality. The
|
|
2
|
+
// classifier and the configured routes stay as they are; the mode moves each tier's route by one fixed step. Pure.
|
|
3
|
+
import { ACTIVITIES, DEFAULTS, EFFORTS, ROUTING_MODES, rank, resolveRoute, sameRoute, TIERS } from './config.mjs';
|
|
4
|
+
import { clampEffort } from './cost.mjs';
|
|
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
|
+
// The route's effort one level up (`step` 1) or down (-1), on what the model lists; null when it cannot move that
|
|
20
|
+
// way: a null effort (the session's), a model without efforts, or no listed level in that direction. The model's
|
|
21
|
+
// lowest listed level is the floor, so a route never loses its effort.
|
|
22
|
+
function steppedEffort(config, route, step) {
|
|
23
|
+
const supported = config.models[route.model].efforts;
|
|
24
|
+
const from = clampEffort(route.effort, supported);
|
|
25
|
+
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;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The route `mode` runs for tier and activity, always from the configured routes. `cost` takes the tier below (never
|
|
32
|
+
// below `low`) when it runs a model with a lower input price, else one effort level less; `quality` one effort level
|
|
33
|
+
// more, else the tier above. One lever per decision: cost pays mostly for input and cache tokens, which effort does
|
|
34
|
+
// not lower, and quality keeps the model and its cache. `micro` and `balanced` keep the configured route.
|
|
35
|
+
export function modeRoute(config, mode, tier, activity = null) {
|
|
36
|
+
const routeAt = (t) => resolveRoute(config, t, activity);
|
|
37
|
+
const route = routeAt(tier);
|
|
38
|
+
if (mode === 'balanced' || tier === 'micro') return route;
|
|
39
|
+
if (mode === 'cost') {
|
|
40
|
+
const below = routeAt(TIERS[Math.max(rank('low'), rank(tier) - 1)]);
|
|
41
|
+
if (below.model !== route.model && config.models[below.model].input < config.models[route.model].input)
|
|
42
|
+
return below;
|
|
43
|
+
const effort = steppedEffort(config, route, -1);
|
|
44
|
+
return effort ? { model: route.model, effort } : route;
|
|
45
|
+
}
|
|
46
|
+
const effort = steppedEffort(config, route, 1);
|
|
47
|
+
return effort ? { model: route.model, effort } : routeAt(TIERS[Math.min(TIERS.length - 1, rank(tier) + 1)]);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The config a turn routes on in `routingMode` ({ mode, source }): every tier's and activity's route as modeRoute
|
|
51
|
+
// gives it, and the mode's policy fields over `policy`. `config` must be the configured one (router.config), never a
|
|
52
|
+
// result of this function: the transform starts from the configured routes, so repeated turns never step twice.
|
|
53
|
+
// `balanced` keeps the routes object as configured.
|
|
54
|
+
export function applyMode(config, routingMode) {
|
|
55
|
+
const { mode } = routingMode;
|
|
56
|
+
const policy = { ...config.policy, ...config.modes[mode]?.policy };
|
|
57
|
+
if (mode === 'balanced') return { ...config, policy, routingMode };
|
|
58
|
+
const routes = Object.fromEntries(TIERS.map((tier) => [tier, modeRoute(config, mode, tier)]));
|
|
59
|
+
const activities = {};
|
|
60
|
+
for (const activity of ACTIVITIES)
|
|
61
|
+
for (const tier of TIERS) {
|
|
62
|
+
const route = modeRoute(config, mode, tier, activity);
|
|
63
|
+
if (!sameRoute(route, routes[tier])) activities[activity] = { ...activities[activity], [tier]: route };
|
|
64
|
+
}
|
|
65
|
+
return { ...config, routes, activities, policy, routingMode };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The active mode and its source: a session override, then the project file, then the profile router.json.
|
|
69
|
+
export function resolveRoutingMode({ session = null, project = null, profile = DEFAULTS.mode } = {}) {
|
|
70
|
+
if (ROUTING_MODES.includes(session)) return { mode: session, source: 'session' };
|
|
71
|
+
if (ROUTING_MODES.includes(project)) return { mode: project, source: 'project' };
|
|
72
|
+
if (profile !== DEFAULTS.mode) return { mode: profile, source: 'profile' };
|
|
73
|
+
return { mode: DEFAULTS.mode, source: 'default' };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// `cost (session)`.
|
|
77
|
+
export const modeLabel = ({ mode, source }) => `${mode} (${source})`;
|
|
78
|
+
|
|
79
|
+
// A project's `.claude/router.json`, which may hold only `mode`: the repo chooses among routes the user configured,
|
|
80
|
+
// never the routes, models or classifier. Returns { mode, error }: a file that does not read is ignored, with a reason
|
|
81
|
+
// that names the key, never its value. No file: { mode: null, error: null }.
|
|
82
|
+
export function readProjectMode(text) {
|
|
83
|
+
if (text === null) return { mode: null, error: null };
|
|
84
|
+
let file;
|
|
85
|
+
try {
|
|
86
|
+
file = JSON.parse(text);
|
|
87
|
+
} catch {
|
|
88
|
+
return { mode: null, error: 'project .claude/router.json is not valid JSON' };
|
|
89
|
+
}
|
|
90
|
+
if (typeof file !== 'object' || file === null || Array.isArray(file))
|
|
91
|
+
return { mode: null, error: 'project .claude/router.json must be an object' };
|
|
92
|
+
const other = Object.keys(file).find((key) => key !== 'mode');
|
|
93
|
+
if (other !== undefined) return { mode: null, error: `project .claude/router.json may only set mode, not ${other}` };
|
|
94
|
+
if (!Object.hasOwn(file, 'mode')) return { mode: null, error: null };
|
|
95
|
+
if (!ROUTING_MODES.includes(file.mode))
|
|
96
|
+
return { mode: null, error: `project .claude/router.json mode must be one of ${ROUTING_MODES.join(', ')}` };
|
|
97
|
+
return { mode: file.mode, error: null };
|
|
98
|
+
}
|
package/lib/panel.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
DEFAULTS,
|
|
8
8
|
effectiveActivities,
|
|
9
9
|
effectiveRoutes,
|
|
10
|
+
ROUTING_MODES,
|
|
10
11
|
resolveRoute,
|
|
11
12
|
sameCell,
|
|
12
13
|
sameRoute,
|
|
@@ -25,13 +26,17 @@ import {
|
|
|
25
26
|
GATEWAY_SETTINGS,
|
|
26
27
|
missingCredentials,
|
|
27
28
|
missingText,
|
|
29
|
+
modelColor,
|
|
28
30
|
modelName,
|
|
29
31
|
percent,
|
|
30
32
|
percentPair,
|
|
31
33
|
probeOf,
|
|
32
34
|
routeLabel,
|
|
33
35
|
routeName,
|
|
36
|
+
routingModeOf,
|
|
37
|
+
routingModeText,
|
|
34
38
|
savingsColor,
|
|
39
|
+
shownConfig,
|
|
35
40
|
signedUsd,
|
|
36
41
|
sparkline,
|
|
37
42
|
TIER_COLOR,
|
|
@@ -43,7 +48,14 @@ import {
|
|
|
43
48
|
} from './display.mjs';
|
|
44
49
|
import { cellForModel, isSameModel } from './route.mjs';
|
|
45
50
|
import { modelSpec, readout } from './savings.mjs';
|
|
46
|
-
import {
|
|
51
|
+
import {
|
|
52
|
+
modelTotals,
|
|
53
|
+
PASS_LABEL,
|
|
54
|
+
PASS_REASONS,
|
|
55
|
+
PASS_WHY,
|
|
56
|
+
readSubagentStore,
|
|
57
|
+
summarizeSubagents,
|
|
58
|
+
} from './subagents.mjs';
|
|
47
59
|
import {
|
|
48
60
|
allTime,
|
|
49
61
|
breakdown,
|
|
@@ -271,10 +283,11 @@ function nowTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
|
|
|
271
283
|
const served = view.actualModel && current && !isSameModel(current, view.actualModel);
|
|
272
284
|
const mode = config.activityRouting;
|
|
273
285
|
const reading = mode === 'off' ? null : activityReading(view);
|
|
274
|
-
// In `on` an override names the tier's own model.
|
|
275
|
-
const
|
|
286
|
+
// In `on` an override names the tier's own model, as the routing mode runs it.
|
|
287
|
+
const shown = shownConfig(config, view);
|
|
288
|
+
const base = view.tier ? resolveRoute(shown, view.tier) : null;
|
|
276
289
|
const override =
|
|
277
|
-
mode === 'on' && view.activity && base && !sameRoute(resolveRoute(
|
|
290
|
+
mode === 'on' && view.activity && base && !sameRoute(resolveRoute(shown, view.tier, view.activity), base);
|
|
278
291
|
const support = auto ? supportText(label, view.estimate) : null;
|
|
279
292
|
const out = [
|
|
280
293
|
ui.line(
|
|
@@ -296,6 +309,12 @@ function nowTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
|
|
|
296
309
|
: null,
|
|
297
310
|
ui.line('Why ', whyText(config, view)),
|
|
298
311
|
support ? ui.dim(` ${support}`) : null,
|
|
312
|
+
ui.line(
|
|
313
|
+
'Mode ',
|
|
314
|
+
routingModeText(view),
|
|
315
|
+
routingModeOf(view).source === 'session' ? ui.dim(' /router mode default clears it') : null,
|
|
316
|
+
),
|
|
317
|
+
view.projectError ? ui.text(` Ignored: ${view.projectError}`, { color: 'yellow' }) : null,
|
|
299
318
|
served
|
|
300
319
|
? ui.line(ui.text('Served ', { color: 'yellow' }), `${modelName(view.actualModel)} · native fallback`)
|
|
301
320
|
: null,
|
|
@@ -323,6 +342,7 @@ function nowTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
|
|
|
323
342
|
const ownToday = summarize(readUsage(view.usageSession), periodRange('today', now));
|
|
324
343
|
if (today.replies > ownToday.replies) out.push(ui.text(' '), ...todayLines(ui, today, actions, columns));
|
|
325
344
|
out.push(
|
|
345
|
+
...sessionAgentLines(ui, view, actions, columns),
|
|
326
346
|
...worthLines(ui, today, actions, columns),
|
|
327
347
|
ui.text(' '),
|
|
328
348
|
ui.gauge('Context ', metrics.contextBar, contextSuffix(metrics)),
|
|
@@ -917,6 +937,13 @@ function choice(ui, key, value, format, pick) {
|
|
|
917
937
|
|
|
918
938
|
// The Policy tab: each control inside the sentence it completes, its default named when the value differs. One Save
|
|
919
939
|
// writes it with the Routes draft.
|
|
940
|
+
// What the profile's routing mode does, under its control on the Policy tab.
|
|
941
|
+
const MODE_HINT = {
|
|
942
|
+
cost: 'a cheaper model or one effort level less where a turn allows',
|
|
943
|
+
balanced: 'each tier runs its route as configured',
|
|
944
|
+
quality: 'one effort level more, or the tier above, where a turn allows',
|
|
945
|
+
};
|
|
946
|
+
|
|
920
947
|
function policyTab(ui, config, view, _usage, actions) {
|
|
921
948
|
const changes = routingChanges(config, view);
|
|
922
949
|
const { draft, tuning, policy } = changes;
|
|
@@ -930,9 +957,26 @@ function policyTab(ui, config, view, _usage, actions) {
|
|
|
930
957
|
];
|
|
931
958
|
const { upgradeBase, upgradeSlope } = config.policy;
|
|
932
959
|
const credits = Object.values(config.models).some((model) => model.billing === 'credits');
|
|
960
|
+
const active = routingModeOf(view);
|
|
933
961
|
return [
|
|
934
962
|
ui.line(ui.head('POLICY'), ui.dim(' · how readily the router changes models · edit, then Save')),
|
|
935
963
|
ui.text(' '),
|
|
964
|
+
ui.line(
|
|
965
|
+
ui.cell(13, ui.text('Mode', { bold: true })),
|
|
966
|
+
ui.Select
|
|
967
|
+
? ui.Select({
|
|
968
|
+
key: 'routing-mode',
|
|
969
|
+
value: config.mode,
|
|
970
|
+
options: ROUTING_MODES.map((mode) => ({ value: mode, label: mode })),
|
|
971
|
+
onSelect: (mode) => actions.routingMode(mode),
|
|
972
|
+
})
|
|
973
|
+
: config.mode,
|
|
974
|
+
ui.dim(' saved at once'),
|
|
975
|
+
),
|
|
976
|
+
ui.dim(`${pad}${MODE_HINT[config.mode]}`),
|
|
977
|
+
active.source === 'session' || active.source === 'project'
|
|
978
|
+
? ui.text(`${pad}this session runs ${routingModeText(view)}`, { color: 'yellow' })
|
|
979
|
+
: null,
|
|
936
980
|
ui.line(
|
|
937
981
|
ui.cell(13, ui.text('Start', { bold: true })),
|
|
938
982
|
'at ',
|
|
@@ -1233,45 +1277,298 @@ function yourTier(config, view) {
|
|
|
1233
1277
|
const pricedText = (text, counts) =>
|
|
1234
1278
|
counts.pricedRequests < counts.requests ? `${text} (${counts.requests - counts.pricedRequests} unpriced)` : text;
|
|
1235
1279
|
|
|
1236
|
-
//
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1280
|
+
// "Oct 7 – Oct 13" for a period's span, or "since Oct 10", its `first` day, for one without a range; null without one.
|
|
1281
|
+
function spanText(period, now, first) {
|
|
1282
|
+
const { from, to } = periodRange(period, now);
|
|
1283
|
+
if (period === 'today') return `today, ${dayLabel(to)}`;
|
|
1284
|
+
if (from) return `${dayLabel(from)} – ${dayLabel(to)}`;
|
|
1285
|
+
return first ? `since ${dayLabel(first)}` : null;
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
// The subagent rows: the open mark, the type, its squares, agents, cost, and against Claude Code's pick with its arrow.
|
|
1289
|
+
const AGENT_TYPE = 19;
|
|
1290
|
+
const AGENT_SQUARES = 12;
|
|
1291
|
+
const AGENT_ROW = 4 + AGENT_TYPE + AGENT_SQUARES + 3 + 9 + 11;
|
|
1292
|
+
// The summary strip's squares at most: past this each model gets its share of them, not one per agent.
|
|
1293
|
+
const AGENT_STRIP = 25;
|
|
1294
|
+
// The models in the order their squares go, by family; any other after them by name.
|
|
1295
|
+
const FAMILIES = ['haiku', 'sonnet', 'opus', 'fable'];
|
|
1296
|
+
const clipped = (text, width) => (text.length < width ? text : `${text.slice(0, width - 2)}…`);
|
|
1297
|
+
const familyRank = (id) => {
|
|
1298
|
+
const rank = FAMILIES.findIndex((family) => id.includes(family));
|
|
1299
|
+
return rank < 0 ? FAMILIES.length : rank;
|
|
1300
|
+
};
|
|
1301
|
+
const byFamily = (a, b) => familyRank(a[0]) - familyRank(b[0]) || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0);
|
|
1302
|
+
const colored = (ui, id, text) => {
|
|
1303
|
+
const color = modelColor(id);
|
|
1304
|
+
return ui.text(text, color ? { color } : {});
|
|
1305
|
+
};
|
|
1306
|
+
|
|
1307
|
+
// Each model's agents in a period and how many the router moved, across the routed types and the pass-throughs.
|
|
1308
|
+
function agentsByModel(s) {
|
|
1309
|
+
const models = new Map();
|
|
1310
|
+
for (const kind of ['routed', 'passed'])
|
|
1311
|
+
for (const counts of Object.values(s[kind]))
|
|
1312
|
+
for (const [id, m] of Object.entries(counts.models)) {
|
|
1313
|
+
const sum = models.get(id) ?? { agents: 0, moved: 0 };
|
|
1314
|
+
models.set(id, { agents: sum.agents + m.agents, moved: sum.moved + (m.moved ?? 0) });
|
|
1315
|
+
}
|
|
1316
|
+
return [...models].filter(([, m]) => m.agents > 0).sort(byFamily);
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
// Squares for `models` ([id, { agents, moved }]), colored by model, filled for the agents the router moved. `scale`:
|
|
1320
|
+
// one per agent up to `max`, then each model's share of `max`, at least one. Else one per agent up to `max`, and
|
|
1321
|
+
// `rest` counts the agents past them.
|
|
1322
|
+
function squares(ui, models, max, scale) {
|
|
1323
|
+
const total = models.reduce((n, [, m]) => n + m.agents, 0);
|
|
1324
|
+
const shown = (n) => (!scale || total <= max || !n ? n : Math.max(1, Math.round((n / total) * max)));
|
|
1325
|
+
const parts = [];
|
|
1326
|
+
let left = max;
|
|
1327
|
+
for (const [id, m] of models) {
|
|
1328
|
+
const n = Math.min(shown(m.agents), left);
|
|
1329
|
+
const filled = Math.min(n, shown(m.moved));
|
|
1330
|
+
if (filled) parts.push(colored(ui, id, '■'.repeat(filled)));
|
|
1331
|
+
if (n > filled) parts.push(colored(ui, id, '□'.repeat(n - filled)));
|
|
1332
|
+
left -= n;
|
|
1333
|
+
}
|
|
1334
|
+
return { parts, width: max - left, rest: scale ? 0 : total - (max - left) };
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
// "Routing saved $0.21 (5%) on 7 routed agents": the routed agents' cost against Claude Code's picks.
|
|
1338
|
+
function agentVerdict(ui, routed) {
|
|
1339
|
+
const agents = routed.reduce((n, [, c]) => n + c.agents, 0);
|
|
1340
|
+
const totals = routed.map(([, c]) => modelTotals(c));
|
|
1341
|
+
const cost = totals.reduce((n, t) => n + t.usd, 0);
|
|
1342
|
+
const core = totals.reduce((n, t) => n + t.coreUsd, 0);
|
|
1343
|
+
const counts = {
|
|
1344
|
+
requests: routed.reduce((n, [, c]) => n + c.requests, 0),
|
|
1345
|
+
pricedRequests: routed.reduce((n, [, c]) => n + c.pricedRequests, 0),
|
|
1346
|
+
};
|
|
1347
|
+
if (!counts.pricedRequests) return ui.dim(` ${plural(agents, 'routed agent')}, not priced`);
|
|
1348
|
+
const difference = cents(cost - core);
|
|
1349
|
+
const on = pricedText(` on ${plural(agents, 'routed agent')}`, counts);
|
|
1350
|
+
if (!difference) return ui.line(' Routing cost the same as Claude Code’s picks', ui.dim(on));
|
|
1351
|
+
return ui.line(
|
|
1352
|
+
` Routing ${difference < 0 ? 'saved' : 'cost'} `,
|
|
1353
|
+
ui.text(usd(difference), { color: savingsColor(difference) }),
|
|
1354
|
+
difference < 0 ? '' : ' more',
|
|
1355
|
+
core > 0 ? ` (${percent(Math.abs(difference) / core)})` : '',
|
|
1356
|
+
ui.dim(on),
|
|
1357
|
+
);
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
// The strip of every agent by model, what the router moved, and how routing paid off.
|
|
1361
|
+
function agentSummary(ui, s, columns) {
|
|
1362
|
+
const models = agentsByModel(s);
|
|
1363
|
+
const total = models.reduce((n, [, m]) => n + m.agents, 0);
|
|
1364
|
+
const moved = models.reduce((n, [, m]) => n + m.moved, 0);
|
|
1365
|
+
const strip = squares(ui, models, AGENT_STRIP, true);
|
|
1366
|
+
const labels = models.map(([id, m]) => [id, `${modelName(id)} ${m.agents}`]);
|
|
1367
|
+
const names = labels.flatMap(([id, text], i) => [i ? ui.dim(' · ') : null, colored(ui, id, text)]);
|
|
1368
|
+
const namesWidth = labels.reduce((n, [, text], i) => n + (i ? 3 : 0) + text.length, 0);
|
|
1369
|
+
const out =
|
|
1370
|
+
Number.isFinite(columns) && 2 + strip.width + 2 + namesWidth > columns
|
|
1371
|
+
? [ui.line(' ', ...strip.parts), ui.line(' ', ...names)]
|
|
1372
|
+
: [ui.line(' ', ...strip.parts, ' ', ...names)];
|
|
1373
|
+
out.push(
|
|
1374
|
+
ui.line(
|
|
1375
|
+
` ${plural(total, 'agent')}`,
|
|
1376
|
+
ui.dim(' · '),
|
|
1377
|
+
`■ ${moved} moved by the router`,
|
|
1378
|
+
ui.dim(' · '),
|
|
1379
|
+
`□ ${total - moved} on Claude Code’s pick`,
|
|
1380
|
+
),
|
|
1381
|
+
);
|
|
1382
|
+
const routed = Object.entries(s.routed).filter(([, c]) => c.agents > 0);
|
|
1383
|
+
if (routed.length) out.push(agentVerdict(ui, routed));
|
|
1384
|
+
return out;
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
// Agents per day from the period's first day, or the first recorded one, to today, with the busiest day.
|
|
1388
|
+
function agentTrend(ui, s, period, now) {
|
|
1389
|
+
if (period === 'session' || period === 'today' || !s.perDay.length) return [];
|
|
1390
|
+
const counts = new Map(s.perDay);
|
|
1391
|
+
const start = periodRange(period, now).from ?? s.perDay[0][0];
|
|
1392
|
+
const [y, m, d] = start.split('-').map(Number);
|
|
1393
|
+
const days = [];
|
|
1394
|
+
for (let i = 0; ; i += 1) {
|
|
1395
|
+
const key = dayKey(new Date(y, m - 1, d + i).getTime());
|
|
1396
|
+
if (key > dayKey(now)) break;
|
|
1397
|
+
days.push([key, counts.get(key) ?? 0]);
|
|
1398
|
+
}
|
|
1399
|
+
if (days.length < 2) return [];
|
|
1400
|
+
const [peakDay, peak] = days.reduce((top, day) => (day[1] > top[1] ? day : top));
|
|
1401
|
+
// A long All period keeps its last days, as many as the row has room for.
|
|
1402
|
+
const values = days.map(([, n]) => n).slice(-(AGENT_ROW - 30));
|
|
1403
|
+
return [
|
|
1404
|
+
ui.line(' per day ', ui.text(sparkline(values), { bold: true }), ui.dim(` peak ${peak} on ${dayLabel(peakDay)}`)),
|
|
1405
|
+
];
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
// "+$0.44 ▲" against Claude Code's pick, in the savings colors; a dim dash when it rounds to nothing.
|
|
1409
|
+
const pickDifference = (ui, value) =>
|
|
1410
|
+
value
|
|
1411
|
+
? ui.text(`${signedUsd(value)} ${value > 0 ? '▲' : '▼'}`.padStart(11), { color: savingsColor(value) })
|
|
1412
|
+
: ui.dim('—'.padStart(9));
|
|
1413
|
+
|
|
1414
|
+
// What a routed model row holds: "2 moved up", or "1 kept, 2 down".
|
|
1415
|
+
function moveText(m) {
|
|
1416
|
+
const kept = m.agents - m.moved;
|
|
1417
|
+
const other = m.moved - m.up - m.down;
|
|
1418
|
+
const parts = [
|
|
1419
|
+
kept ? `${kept} kept` : null,
|
|
1420
|
+
m.up ? `${m.up} up` : null,
|
|
1421
|
+
m.down ? `${m.down} down` : null,
|
|
1422
|
+
other ? `${other} moved` : null,
|
|
1423
|
+
].filter(Boolean);
|
|
1424
|
+
if (parts.length > 1 || kept) return parts.join(', ');
|
|
1425
|
+
return `${m.moved} moved${m.up ? ' up' : m.down ? ' down' : ''}`;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
// One row of the subagent table; open, the models it ran on with their cost, and its requests and retry proxies.
|
|
1429
|
+
function agentRow(ui, view, actions, { id, name, counts, routed, why, narrow, columns }) {
|
|
1430
|
+
const open = view.usageOpen === id;
|
|
1431
|
+
const models = Object.entries(counts.models)
|
|
1432
|
+
.filter(([, m]) => m.agents > 0)
|
|
1433
|
+
.sort(byFamily);
|
|
1434
|
+
const marks = squares(
|
|
1435
|
+
ui,
|
|
1436
|
+
models.map(([model, m]) => [model, { agents: m.agents, moved: m.moved ?? 0 }]),
|
|
1437
|
+
AGENT_SQUARES - 3,
|
|
1438
|
+
false,
|
|
1439
|
+
);
|
|
1440
|
+
const t = modelTotals(counts);
|
|
1441
|
+
// A row none of whose requests had a price says so rather than $0.00.
|
|
1442
|
+
const priced = counts.pricedRequests > 0;
|
|
1443
|
+
const costCell = (value) => (priced ? usd(value).padStart(9) : ui.dim('no price'.padStart(9)));
|
|
1444
|
+
const out = [
|
|
1445
|
+
ui.line(
|
|
1446
|
+
' ',
|
|
1447
|
+
ui.Button({
|
|
1448
|
+
key: `row-${id}`,
|
|
1449
|
+
label: open ? '▾' : '▸',
|
|
1450
|
+
plain: true,
|
|
1451
|
+
onPress: () => actions.usageOpen(open ? null : id),
|
|
1452
|
+
}),
|
|
1453
|
+
' ',
|
|
1454
|
+
ui.cell(AGENT_TYPE, clipped(name, AGENT_TYPE)),
|
|
1455
|
+
ui.cell(AGENT_SQUARES, ui.line(...marks.parts, marks.rest ? ui.dim(`+${marks.rest}`) : null)),
|
|
1456
|
+
String(counts.agents).padStart(3),
|
|
1457
|
+
narrow ? null : costCell(t.usd),
|
|
1458
|
+
routed && priced ? pickDifference(ui, cents(t.usd - t.coreUsd)) : null,
|
|
1459
|
+
!routed && why && !narrow ? ui.dim(` ${why}`) : null,
|
|
1460
|
+
),
|
|
1461
|
+
];
|
|
1462
|
+
if (!open) return out;
|
|
1463
|
+
for (const [model, m] of models)
|
|
1464
|
+
out.push(
|
|
1465
|
+
ui.line(
|
|
1466
|
+
' ',
|
|
1467
|
+
ui.cell(17, colored(ui, model, `${m.moved ? '■' : '□'} ${modelName(model)}`)),
|
|
1468
|
+
ui.cell(15, ui.dim(routed ? moveText(m) : plural(m.agents, 'agent'))),
|
|
1469
|
+
narrow ? null : costCell(m.usd),
|
|
1470
|
+
routed && priced ? pickDifference(ui, cents(m.usd - m.coreUsd)) : null,
|
|
1471
|
+
),
|
|
1472
|
+
);
|
|
1473
|
+
const notes = [
|
|
1474
|
+
`${(counts.requests / counts.agents).toFixed(1)} requests per agent`,
|
|
1475
|
+
routed ? `${counts.respawns || 'none'} respawned within 10 min` : null,
|
|
1476
|
+
counts.failures ? plural(counts.failures, 'classifier failure') : null,
|
|
1477
|
+
counts.pricedRequests < counts.requests ? `${counts.requests - counts.pricedRequests} requests not priced` : null,
|
|
1478
|
+
].filter(Boolean);
|
|
1479
|
+
out.push(...dimLines(ui, ` ${notes.join(' · ')}`, columns));
|
|
1480
|
+
return out;
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
// Subagents in the chosen period: this session's record, or every session's in every project (one $.store per
|
|
1484
|
+
// plugin install). A strip of every agent by the model it ran on, filled where the router moved it off Claude Code's
|
|
1485
|
+
// pick, and the routed agents' cost against those picks. Then the routed types and the pass-throughs, each with its
|
|
1486
|
+
// squares, agents and cost, opening to its models; a pass-through says why Claude Code picked.
|
|
1487
|
+
function subagentUsage(ui, config, view, actions, period, now, columns) {
|
|
1240
1488
|
const mode = config.subagentRouting;
|
|
1241
|
-
const
|
|
1242
|
-
const
|
|
1243
|
-
const
|
|
1244
|
-
const
|
|
1245
|
-
|
|
1489
|
+
const record = readSubagentStore(period === 'session' ? view.subagentSession : view.subagentStore);
|
|
1490
|
+
const s = summarizeSubagents(record, periodRange(period, now));
|
|
1491
|
+
const first = Number.isFinite(record.since) ? dayKey(record.since) : null;
|
|
1492
|
+
const scope =
|
|
1493
|
+
period === 'session' ? 'this session' : `all projects · ${spanText(period, now, first) ?? 'nothing yet'}`;
|
|
1494
|
+
const out = [ui.line(ui.head('SUBAGENTS'), ui.dim(` · ${scope} subagent routing ${mode}`))];
|
|
1495
|
+
const routed = Object.entries(s.routed)
|
|
1496
|
+
.filter(([, c]) => c.agents > 0)
|
|
1497
|
+
.sort((a, b) => b[1].agents - a[1].agents || (a[0] < b[0] ? -1 : 1));
|
|
1498
|
+
const passed = PASS_REASONS.filter((reason) => s.passed[reason]?.agents > 0).sort(
|
|
1499
|
+
(a, b) => s.passed[b].agents - s.passed[a].agents,
|
|
1500
|
+
);
|
|
1501
|
+
const undated = s.undated
|
|
1502
|
+
? [
|
|
1503
|
+
ui.dim(
|
|
1504
|
+
` + ${plural(s.undated.agents, 'agent')} from before 1.14.0, not split by model · ${usd(s.undated.usd)}`,
|
|
1505
|
+
),
|
|
1506
|
+
]
|
|
1507
|
+
: [];
|
|
1508
|
+
if (!routed.length && !passed.length) {
|
|
1509
|
+
const older = !s.undated && periodRange(period, now).from !== null && readSubagentStore(view.subagentStore).undated;
|
|
1246
1510
|
return [
|
|
1247
1511
|
...out,
|
|
1248
|
-
ui.dim(
|
|
1512
|
+
ui.dim(
|
|
1513
|
+
mode === 'off'
|
|
1514
|
+
? ' Not recorded: subagentRouting is off in router.json.'
|
|
1515
|
+
: ` No subagents ${PERIOD_PHRASE[period]}.`,
|
|
1516
|
+
),
|
|
1517
|
+
...undated,
|
|
1518
|
+
...(older ? [ui.dim(' Agents from before 1.14.0 have no date: All shows them.')] : []),
|
|
1249
1519
|
];
|
|
1250
|
-
if (rows.length) {
|
|
1251
|
-
out.push(ui.dim(' core’s model (est.) → routed'));
|
|
1252
|
-
for (const [type, c] of rows.sort((a, b) => b[1].agents - a[1].agents)) {
|
|
1253
|
-
const cost = pricedText(`${usd(c.coreUsd)} → ${usd(c.routedUsd)}`, c);
|
|
1254
|
-
out.push(
|
|
1255
|
-
ui.text(
|
|
1256
|
-
` ${type} ${c.agents} agent${c.agents === 1 ? '' : 's'} · ${c.differs} moved · ${(c.requests / c.agents).toFixed(1)} req/agent · ${c.respawns} respawns · ${cost}`,
|
|
1257
|
-
),
|
|
1258
|
-
);
|
|
1259
|
-
const choices = Object.entries(c.choices).map(([choice, n]) => `${choice} ${n}`);
|
|
1260
|
-
if (c.failures) choices.push(`${c.failures} classifier failures`);
|
|
1261
|
-
out.push(...dimLines(ui, ` ${choices.join(' · ')}`, columns));
|
|
1262
|
-
}
|
|
1263
1520
|
}
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1521
|
+
const narrow = Number.isFinite(columns) && columns < AGENT_ROW;
|
|
1522
|
+
out.push(...agentSummary(ui, s, columns), ...agentTrend(ui, s, period, now), ...undated);
|
|
1523
|
+
const rowOf = (fields) => agentRow(ui, view, actions, { narrow, columns, ...fields });
|
|
1524
|
+
if (routed.length) {
|
|
1267
1525
|
out.push(
|
|
1268
|
-
ui.text(
|
|
1269
|
-
ui.
|
|
1526
|
+
ui.text(' '),
|
|
1527
|
+
ui.line(ui.text(' ROUTED', { bold: true }), ui.dim(' · the router picked the model at spawn')),
|
|
1528
|
+
ui.dim(
|
|
1529
|
+
` ${'type'.padEnd(AGENT_TYPE)}${'agents'.padEnd(AGENT_SQUARES + 3)}${narrow ? '' : 'cost'.padStart(9)}${'vs CC'.padStart(9)}`,
|
|
1530
|
+
),
|
|
1270
1531
|
);
|
|
1532
|
+
for (const [type, counts] of routed)
|
|
1533
|
+
out.push(...rowOf({ id: `subagent:${type}`, name: type, counts, routed: true }));
|
|
1271
1534
|
}
|
|
1535
|
+
if (passed.length) {
|
|
1536
|
+
out.push(ui.text(' '), ui.line(ui.text(' NOT ROUTED', { bold: true }), ui.dim(' · Claude Code picked the model')));
|
|
1537
|
+
for (const reason of passed)
|
|
1538
|
+
out.push(
|
|
1539
|
+
...rowOf({
|
|
1540
|
+
id: `subagent-pass:${reason}`,
|
|
1541
|
+
name: PASS_LABEL[reason],
|
|
1542
|
+
counts: s.passed[reason],
|
|
1543
|
+
routed: false,
|
|
1544
|
+
why: PASS_WHY[reason],
|
|
1545
|
+
}),
|
|
1546
|
+
);
|
|
1547
|
+
}
|
|
1548
|
+
out.push(
|
|
1549
|
+
ui.text(' '),
|
|
1550
|
+
...dimLines(ui, ' ■ moved by the router · □ Claude Code’s pick · color: the model', columns),
|
|
1551
|
+
...(routed.length ? dimLines(ui, ' vs CC: the same tokens on Claude Code’s pick, at list prices', columns) : []),
|
|
1552
|
+
);
|
|
1272
1553
|
return out;
|
|
1273
1554
|
}
|
|
1274
1555
|
|
|
1556
|
+
// This session's subagents on the Now tab: how many run now, the strip, and how routing paid off. Nothing without any.
|
|
1557
|
+
function sessionAgentLines(ui, view, actions, columns) {
|
|
1558
|
+
const s = summarizeSubagents(readSubagentStore(view.subagentSession), { from: null, to: null });
|
|
1559
|
+
const running = view.subagentsRunning ?? 0;
|
|
1560
|
+
const any = [...Object.values(s.routed), ...Object.values(s.passed)].some((c) => c.agents > 0);
|
|
1561
|
+
if (!any && !running) return [];
|
|
1562
|
+
const head = ui.line(
|
|
1563
|
+
ui.head('SUBAGENTS'),
|
|
1564
|
+
ui.dim(' · this session'),
|
|
1565
|
+
running ? ui.text(` · ● ${running} running`, { color: 'yellow' }) : null,
|
|
1566
|
+
' ',
|
|
1567
|
+
ui.Button({ key: 'usage-agents', label: 'Usage', onPress: () => actions.usageFor('session') }),
|
|
1568
|
+
);
|
|
1569
|
+
return [ui.text(' '), head, ...(any ? agentSummary(ui, s, columns) : [])];
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1275
1572
|
const PERIOD_LABEL = { session: 'Session', today: 'Today', '7d': '7d', '30d': '30d', month: 'Month', all: 'All' };
|
|
1276
1573
|
// "No routed replies …" for each period.
|
|
1277
1574
|
const PERIOD_PHRASE = {
|
|
@@ -1359,17 +1656,8 @@ function scopeLine(ui, config, view, period, summary, now) {
|
|
|
1359
1656
|
tier ? ui.tier(tier, yourModel(view)) : ui.text(yourModel(view)),
|
|
1360
1657
|
);
|
|
1361
1658
|
}
|
|
1362
|
-
const
|
|
1363
|
-
|
|
1364
|
-
const span =
|
|
1365
|
-
period === 'today'
|
|
1366
|
-
? `today, ${dayLabel(to)}`
|
|
1367
|
-
: from
|
|
1368
|
-
? `${dayLabel(from)} – ${dayLabel(to)}`
|
|
1369
|
-
: first
|
|
1370
|
-
? `since ${dayLabel(first)}`
|
|
1371
|
-
: 'no dated replies yet';
|
|
1372
|
-
return ui.dim(`all sessions · ${span} · each session vs its own model`);
|
|
1659
|
+
const span = spanText(period, now, summary.days[0]?.[0]) ?? 'no dated replies yet';
|
|
1660
|
+
return ui.dim(`all projects · ${span} · each session vs its own model`);
|
|
1373
1661
|
}
|
|
1374
1662
|
|
|
1375
1663
|
// The models a session's replies were priced against, when they are not the one `/model` selects now: after a
|
|
@@ -1701,7 +1989,7 @@ function deleteLine(ui, view, actions) {
|
|
|
1701
1989
|
}
|
|
1702
1990
|
|
|
1703
1991
|
// Answer first for the chosen period: routing against your model as a ledger that adds up, the tokens, the cost by
|
|
1704
|
-
// activity, model or tier, the trend or What if, and what is worth a look. Subagents follow,
|
|
1992
|
+
// activity, model or tier, the trend or What if, and what is worth a look. Subagents follow, for the same period.
|
|
1705
1993
|
function usageTab(ui, config, view, _usage, actions, _modelOptions, columns, now) {
|
|
1706
1994
|
const period = PERIODS.includes(view.usagePeriod) ? view.usagePeriod : 'session';
|
|
1707
1995
|
const record = readUsage(period === 'session' ? view.usageSession : view.usageStore);
|
|
@@ -1732,7 +2020,12 @@ function usageTab(ui, config, view, _usage, actions, _modelOptions, columns, now
|
|
|
1732
2020
|
...insightLines(ui, summary, actions, columns),
|
|
1733
2021
|
);
|
|
1734
2022
|
}
|
|
1735
|
-
out.push(
|
|
2023
|
+
out.push(
|
|
2024
|
+
ui.text(' '),
|
|
2025
|
+
...subagentUsage(ui, config, view, actions, period, now, columns),
|
|
2026
|
+
ui.text(' '),
|
|
2027
|
+
...footer(ui, columns),
|
|
2028
|
+
);
|
|
1736
2029
|
out.push(ui.text(' '), deleteLine(ui, view, actions));
|
|
1737
2030
|
return out;
|
|
1738
2031
|
}
|