@alexeiled/claude-router 1.7.4 → 1.8.1
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 +16 -0
- package/docs/activity-routing.md +1 -1
- package/docs/architecture.md +1 -1
- package/docs/evaluation.md +3 -3
- package/docs/native-router.md +2 -2
- package/docs/router-band.svg +68 -87
- package/docs/router-pane-now.svg +2 -2
- package/docs/router-pane-usage.svg +98 -97
- package/docs/user-guide.md +11 -10
- package/hooks/native-router.mjs +25 -14
- package/lib/band.mjs +66 -52
- package/lib/display.mjs +24 -1
- package/lib/panel.mjs +36 -6
- package/lib/route.mjs +4 -3
- package/lib/savings.mjs +39 -0
- package/lib/view.mjs +2 -0
- package/package.json +1 -1
package/lib/band.mjs
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
import { activeClassifier, TIERS } from './config.mjs';
|
|
2
2
|
import {
|
|
3
|
+
activityVerb,
|
|
3
4
|
classifierLabel,
|
|
4
5
|
classifierStatus,
|
|
5
6
|
classifierWarns,
|
|
6
7
|
differenceShare,
|
|
7
8
|
modelName,
|
|
8
9
|
NOT_A_TIER_REASON,
|
|
9
|
-
|
|
10
|
+
percentPair,
|
|
10
11
|
reasonActivity,
|
|
11
12
|
routeLabel,
|
|
12
13
|
SAVINGS_COLOR,
|
|
13
14
|
shortReason,
|
|
14
|
-
signedUsd,
|
|
15
|
-
switchCount,
|
|
16
15
|
TIER_COLOR,
|
|
17
16
|
unavailableReason,
|
|
18
17
|
usageMetrics,
|
|
18
|
+
usd,
|
|
19
|
+
yourModel,
|
|
19
20
|
} from './display.mjs';
|
|
20
21
|
import { isSameModel } from './route.mjs';
|
|
21
|
-
import { emptyTotals, readout } from './savings.mjs';
|
|
22
|
+
import { emptyTotals, modelSpec, readout } from './savings.mjs';
|
|
22
23
|
|
|
23
24
|
const BARS = '▂▄▆█';
|
|
24
25
|
const SEPARATOR = ' · ';
|
|
@@ -26,10 +27,17 @@ const SEPARATOR = ' · ';
|
|
|
26
27
|
const GAP = ' ';
|
|
27
28
|
// Space the band keeps for the trailing Router button.
|
|
28
29
|
const ROUTER_BUTTON = 8;
|
|
29
|
-
const STRIP = 20;
|
|
30
30
|
|
|
31
31
|
// Hex, like TIER_COLOR, so every surface draws the same shades. Yellow stays out: it marks a fault that needs action.
|
|
32
|
-
|
|
32
|
+
// Activity is Claude Code's orange for a turn's action; italic sets it apart from the high and medium tier colors.
|
|
33
|
+
const COLOR = {
|
|
34
|
+
accent: '#7c9cff',
|
|
35
|
+
off: '#2a3040',
|
|
36
|
+
muted: '#8b93a7',
|
|
37
|
+
warn: '#f0b429',
|
|
38
|
+
ink: '#0d1016',
|
|
39
|
+
activity: '#d77757',
|
|
40
|
+
};
|
|
33
41
|
|
|
34
42
|
const part = (text, style = {}) => ({ text, style });
|
|
35
43
|
// A filled chip: the text on a background, as the prototype's status pills.
|
|
@@ -83,12 +91,14 @@ const pendingMeter = () => [...BARS].map((bar) => part(bar, { color: COLOR.accen
|
|
|
83
91
|
|
|
84
92
|
const route = (view) => routeLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel, view.effort);
|
|
85
93
|
|
|
86
|
-
// The turn's activity between the tier and the route,
|
|
87
|
-
//
|
|
94
|
+
// The turn's activity between the tier and the route, as a verb in orange italic; dim and marked in shadow, where the
|
|
95
|
+
// route shown is still the one in use. One part with its arrow, so a narrow band drops both before the reason.
|
|
88
96
|
function activityPart(config, view) {
|
|
89
97
|
if (!view.activity || config.activityRouting === 'off') return null;
|
|
90
|
-
const
|
|
91
|
-
return
|
|
98
|
+
const verb = activityVerb(view.activity);
|
|
99
|
+
return config.activityRouting === 'shadow'
|
|
100
|
+
? { ...part(`${verb} (shadow) → `, { dimColor: true }), priority: 2 }
|
|
101
|
+
: { ...part(`${verb} → `, { color: COLOR.activity, italic: true }), priority: 2 };
|
|
92
102
|
}
|
|
93
103
|
|
|
94
104
|
function served(view) {
|
|
@@ -100,7 +110,8 @@ function supportText(config, estimate) {
|
|
|
100
110
|
if (!Number.isFinite(estimate?.threshold)) return null;
|
|
101
111
|
const mass = Number.isFinite(estimate.upgradeMass) ? estimate.upgradeMass : estimate.downgradeMass;
|
|
102
112
|
if (!Number.isFinite(mass)) return null;
|
|
103
|
-
|
|
113
|
+
const [shown, bar] = percentPair(mass, estimate.threshold);
|
|
114
|
+
return `${classifierLabel(config)} ${shown} ${mass >= estimate.threshold ? '≥' : '<'} ${bar}`;
|
|
104
115
|
}
|
|
105
116
|
|
|
106
117
|
export function bandSegments(config, view, usage, actions) {
|
|
@@ -155,7 +166,8 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
155
166
|
return [segment(0, meter(null, true), part(' Routing on · ready', { dimColor: true })), pin].filter(Boolean);
|
|
156
167
|
const main = segment(
|
|
157
168
|
0,
|
|
158
|
-
|
|
169
|
+
// The space after the pill keeps its background off the next word.
|
|
170
|
+
view.tier ? [...meter(view.tier), pill(view.tier, TIER_COLOR[view.tier]), part(' ')] : part('○ '),
|
|
159
171
|
activityPart(config, view),
|
|
160
172
|
part(route(view)),
|
|
161
173
|
served(view) ? part(' fallback', { color: 'yellow' }) : null,
|
|
@@ -175,57 +187,59 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
175
187
|
const metrics = usageMetrics(config, view, usage);
|
|
176
188
|
const reading = (bar) => part(bar.percent === null ? '?' : `${bar.percent}%`, { color: bar.color });
|
|
177
189
|
const support = supportText(config, view.estimate);
|
|
190
|
+
// The policy's price for the switch it weighed: mostly the prompt cache the other model must write. For a move to a
|
|
191
|
+
// cheaper model it is net of what that model saves over the downgrade horizon.
|
|
192
|
+
const switchUsd = view.estimate?.taxUsd;
|
|
178
193
|
return [
|
|
179
194
|
main,
|
|
180
195
|
pin,
|
|
181
196
|
segment(1, part(shortReason(view.reason, reasonActivity(config, view)), { dimColor: true })),
|
|
182
197
|
support ? segment(4, part(support, { dimColor: true })) : null,
|
|
198
|
+
Number.isFinite(switchUsd) && switchUsd >= 0.005
|
|
199
|
+
? segment(4, part(`switch cost ≈ ${usd(switchUsd)}`, { dimColor: true }))
|
|
200
|
+
: null,
|
|
183
201
|
metrics.contextBar.percent === null && metrics.cacheBar.percent === null
|
|
184
202
|
? null
|
|
185
|
-
: segment(3, part('ctx '), reading(metrics.contextBar), part(' · cache '), reading(metrics.cacheBar)),
|
|
203
|
+
: segment(3, part('ctx '), reading(metrics.contextBar), part(' · cache hit '), reading(metrics.cacheBar)),
|
|
186
204
|
].filter(Boolean);
|
|
187
205
|
}
|
|
188
206
|
|
|
189
|
-
// The
|
|
190
|
-
|
|
191
|
-
|
|
207
|
+
// The larger of the two costs that cut a saving or made an extra, if the session had one: "stronger models +$0.68".
|
|
208
|
+
function costPart(r) {
|
|
209
|
+
const [label, value] =
|
|
210
|
+
r.strongerUsd >= r.switchUsd ? ['stronger models', r.strongerUsd] : ['cold-cache writes', r.switchUsd];
|
|
211
|
+
return value > 0 ? `${label} +${usd(value)}` : null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// The second row of the detailed band: this session against the model /model selects now, one scope end to end, so a
|
|
215
|
+
// /model change shows the whole session against the new one. The answer stays; the reply count and the cost behind it
|
|
216
|
+
// drop first when the band is narrow.
|
|
217
|
+
export function sessionRow(config, view, columns) {
|
|
218
|
+
const label = part('session ', { dimColor: true });
|
|
219
|
+
if (!modelSpec(config, view.nativeModel))
|
|
220
|
+
return [label, part(`${modelName(view.nativeModel)} has no list price`, { dimColor: true })];
|
|
192
221
|
const r = readout(view.savings ?? emptyTotals());
|
|
193
|
-
if (r.
|
|
194
|
-
const
|
|
195
|
-
|
|
222
|
+
if (!r.replies) return [part('no replies yet', { dimColor: true })];
|
|
223
|
+
const replies = `${r.replies} repl${r.replies === 1 ? 'y' : 'ies'}`;
|
|
224
|
+
if (r.early) return [label, part(`${replies} · too early to compare`, { dimColor: true })];
|
|
225
|
+
const share = differenceShare(r)?.replace(/^[−+]/, '');
|
|
226
|
+
const amount = `${usd(r.differenceUsd)}${share ? ` (${share})` : ''}`;
|
|
227
|
+
const answer =
|
|
196
228
|
r.differenceUsd < 0
|
|
197
|
-
? [
|
|
229
|
+
? [part(`saved ${amount}`, { color: SAVINGS_COLOR.less, bold: true }), part(` vs ${yourModel(view)}`)]
|
|
198
230
|
: r.differenceUsd > 0
|
|
199
|
-
? [
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const tiers = (view.tiers ?? []).slice(-STRIP);
|
|
212
|
-
if (!tiers.length) return [part('no replies yet', { dimColor: true })];
|
|
213
|
-
const switches = switchCount((view.routes ?? []).slice(-tiers.length));
|
|
214
|
-
const metrics = usageMetrics(config, view, usage);
|
|
215
|
-
const savings = savingsParts(view);
|
|
216
|
-
const row = [
|
|
217
|
-
part('replies ', { dimColor: true }),
|
|
218
|
-
...tiers.map((tier) => (tier ? part('█', { color: TIER_COLOR[tier] }) : part('·', { dimColor: true }))),
|
|
219
|
-
part(
|
|
220
|
-
` ${switches} switch${switches === 1 ? '' : 'es'}` +
|
|
221
|
-
`${Number.isFinite(view.estimate?.taxUsd) ? ` · tax ≈ $${view.estimate.taxUsd.toFixed(2)}` : ''}` +
|
|
222
|
-
`${Number.isFinite(metrics.cacheBenefit) ? ` · cache saved ≈ $${metrics.cacheBenefit.toFixed(2)}` : ''}`,
|
|
223
|
-
{ dimColor: true },
|
|
224
|
-
),
|
|
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;
|
|
231
|
+
? [part(`extra ${amount}`, { color: SAVINGS_COLOR.more, bold: true }), part(` vs ${yourModel(view)}`)]
|
|
232
|
+
: [part(`same cost as ${yourModel(view)}`)];
|
|
233
|
+
const cost = costPart(r);
|
|
234
|
+
const kept = fitSegments(
|
|
235
|
+
[
|
|
236
|
+
segment(0, label, answer),
|
|
237
|
+
segment(2, part(replies, { dimColor: true })),
|
|
238
|
+
cost ? segment(3, part(cost, { dimColor: true })) : null,
|
|
239
|
+
].filter(Boolean),
|
|
240
|
+
columns,
|
|
241
|
+
);
|
|
242
|
+
return kept.flatMap((s, i) => (i ? [part(SEPARATOR, { dimColor: true }), ...s.parts] : s.parts));
|
|
229
243
|
}
|
|
230
244
|
|
|
231
245
|
// The row revealed under the pointer: pins and the mode control while routing is on, and the one- or two-row choice.
|
|
@@ -268,7 +282,7 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
|
|
|
268
282
|
flexDirection: 'column',
|
|
269
283
|
children: [
|
|
270
284
|
row(line),
|
|
271
|
-
view.bandDetail && routing ? row(
|
|
285
|
+
view.bandDetail && routing ? row(sessionRow(config, view, columns)) : null,
|
|
272
286
|
Box({
|
|
273
287
|
display: 'none',
|
|
274
288
|
hover: { display: 'flex' },
|
|
@@ -281,7 +295,7 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
|
|
|
281
295
|
export function switchToast(config, previous, next, estimate) {
|
|
282
296
|
const label = (decision) => routeLabel(decision.model, decision.effort);
|
|
283
297
|
const support = supportText(config, estimate);
|
|
284
|
-
// An activity move names the activity: "Model changed: Sonnet 5.5 · medium → Haiku 5.5 · high ·
|
|
298
|
+
// An activity move names the activity: "Model changed: Sonnet 5.5 · medium → Haiku 5.5 · high · running".
|
|
285
299
|
const why = shortReason(next.reason, next.activity).replace(/^\W+\s*/, '');
|
|
286
300
|
return `Model changed: ${label(previous)} → ${label(next)} · ${why}${support ? ` (${support})` : ''}`;
|
|
287
301
|
}
|
package/lib/display.mjs
CHANGED
|
@@ -47,6 +47,17 @@ export const switchCount = (routes) =>
|
|
|
47
47
|
|
|
48
48
|
export const percent = (value) => `${Math.round(value * 100)}%`;
|
|
49
49
|
|
|
50
|
+
// Two shares that are compared, such as a classifier's support and the bar it must clear: whole percents, or as many
|
|
51
|
+
// decimals (up to two) as it takes for unequal values not to read as equal, "81.0%" against "81.4%".
|
|
52
|
+
export function percentPair(a, b) {
|
|
53
|
+
const at = (digits) => [a, b].map((value) => `${(value * 100).toFixed(digits)}%`);
|
|
54
|
+
for (const digits of [0, 1]) {
|
|
55
|
+
const pair = at(digits);
|
|
56
|
+
if (a === b || pair[0] !== pair[1]) return pair;
|
|
57
|
+
}
|
|
58
|
+
return at(2);
|
|
59
|
+
}
|
|
60
|
+
|
|
50
61
|
export const REASONS = {
|
|
51
62
|
ready: 'ready for the next turn',
|
|
52
63
|
'same-tier': 'the task fits the current tier',
|
|
@@ -156,9 +167,21 @@ export const SHORT_REASONS = {
|
|
|
156
167
|
'activity-pending': '… {activity}: not worth a switch',
|
|
157
168
|
};
|
|
158
169
|
|
|
170
|
+
// What the band calls each activity: the verb for the work, as Claude Code names a turn's action.
|
|
171
|
+
const ACTIVITY_VERB = {
|
|
172
|
+
code: 'coding',
|
|
173
|
+
debug: 'debugging',
|
|
174
|
+
explore: 'exploring',
|
|
175
|
+
plan: 'planning',
|
|
176
|
+
review: 'reviewing',
|
|
177
|
+
ops: 'running',
|
|
178
|
+
docs: 'documenting',
|
|
179
|
+
};
|
|
180
|
+
export const activityVerb = (activity) => ACTIVITY_VERB[activity] ?? activity;
|
|
181
|
+
|
|
159
182
|
// The band's words for `reason`; an activity move names the activity it is about, `base` for the tier's own route.
|
|
160
183
|
export const shortReason = (reason, activity) =>
|
|
161
|
-
(SHORT_REASONS[reason] ?? reason ?? '').replace('{activity}', activity
|
|
184
|
+
(SHORT_REASONS[reason] ?? reason ?? '').replace('{activity}', activity ? activityVerb(activity) : 'base');
|
|
162
185
|
|
|
163
186
|
// The activity a reason is about: the applied one for a move, and for a refused move the classifier's activity when
|
|
164
187
|
// it carries enough mass to apply, else null (the tier's base route).
|
package/lib/panel.mjs
CHANGED
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
missingText,
|
|
31
31
|
modelName,
|
|
32
32
|
percent,
|
|
33
|
+
percentPair,
|
|
33
34
|
probeText,
|
|
34
35
|
routeLabel,
|
|
35
36
|
routeName,
|
|
@@ -47,7 +48,15 @@ import {
|
|
|
47
48
|
yourModel,
|
|
48
49
|
} from './display.mjs';
|
|
49
50
|
import { cellForModel, isSameModel } from './route.mjs';
|
|
50
|
-
import {
|
|
51
|
+
import {
|
|
52
|
+
EARLY_REPLIES,
|
|
53
|
+
emptyTotals,
|
|
54
|
+
modelAlias,
|
|
55
|
+
modelEntry,
|
|
56
|
+
modelSpec,
|
|
57
|
+
readout,
|
|
58
|
+
readSavingsStore,
|
|
59
|
+
} from './savings.mjs';
|
|
51
60
|
|
|
52
61
|
const TABS = [
|
|
53
62
|
['now', '1', 'Now'],
|
|
@@ -264,10 +273,11 @@ function nowTab(ui, config, view, usage, actions) {
|
|
|
264
273
|
const [direction, mass] = Number.isFinite(estimate.upgradeMass)
|
|
265
274
|
? ['up', estimate.upgradeMass]
|
|
266
275
|
: ['down', estimate.downgradeMass];
|
|
276
|
+
const [bar, gave] = percentPair(estimate.threshold, Number.isFinite(mass) ? mass : estimate.threshold);
|
|
267
277
|
out.push(
|
|
268
278
|
ui.dim(
|
|
269
|
-
` switch needs ${
|
|
270
|
-
`${Number.isFinite(estimate.taxUsd) ? ` ·
|
|
279
|
+
` switch needs ${bar} · ${label} gave ${Number.isFinite(mass) ? gave : '—'} ${direction}` +
|
|
280
|
+
`${Number.isFinite(estimate.taxUsd) ? ` · switch cost ≈ ${usd(estimate.taxUsd)}` : ''}`,
|
|
271
281
|
),
|
|
272
282
|
);
|
|
273
283
|
}
|
|
@@ -292,7 +302,7 @@ function nowTab(ui, config, view, usage, actions) {
|
|
|
292
302
|
ui.line(
|
|
293
303
|
`Cost ${dollars(metrics.cost)} `,
|
|
294
304
|
ui.dim('by Claude'),
|
|
295
|
-
Number.isFinite(metrics.cacheBenefit) ? ` · cache saved ≈
|
|
305
|
+
Number.isFinite(metrics.cacheBenefit) ? ` · cache hits saved ≈ ${usd(metrics.cacheBenefit)}` : '',
|
|
296
306
|
),
|
|
297
307
|
);
|
|
298
308
|
return out;
|
|
@@ -1026,6 +1036,24 @@ function savingsDetail(ui, config, view, { totals, session, across, row, tier, n
|
|
|
1026
1036
|
return out;
|
|
1027
1037
|
}
|
|
1028
1038
|
|
|
1039
|
+
// This session against each other priced model as yours, those past "too early", in router.json order:
|
|
1040
|
+
// "Opus 5.5 saved $6.20 · Haiku 5.5 extra $1.10". Null when there is none.
|
|
1041
|
+
function otherModels(config, view) {
|
|
1042
|
+
const current = modelAlias(config, view.nativeModel);
|
|
1043
|
+
const parts = Object.keys(config.models)
|
|
1044
|
+
.filter((alias) => alias !== current && modelEntry(view.savingsBy, alias))
|
|
1045
|
+
.map((alias) => [modelName(config.models[alias].id), readout(modelEntry(view.savingsBy, alias))])
|
|
1046
|
+
.filter(([, r]) => !r.early)
|
|
1047
|
+
.map(([name, r]) =>
|
|
1048
|
+
r.differenceUsd < 0
|
|
1049
|
+
? `${name} saved ${usd(r.differenceUsd)}`
|
|
1050
|
+
: r.differenceUsd > 0
|
|
1051
|
+
? `${name} extra ${usd(r.differenceUsd)}`
|
|
1052
|
+
: `${name} same cost`,
|
|
1053
|
+
);
|
|
1054
|
+
return parts.length ? parts.join(' · ') : null;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1029
1057
|
// The tier whose route is your model at your effort, for its color; else any tier on your model.
|
|
1030
1058
|
function yourTier(config, view) {
|
|
1031
1059
|
const runs = (tier) => {
|
|
@@ -1110,6 +1138,8 @@ function savingsSection(ui, config, view, columns) {
|
|
|
1110
1138
|
);
|
|
1111
1139
|
return [...out, ...notes()];
|
|
1112
1140
|
}
|
|
1141
|
+
const others = otherModels(config, view);
|
|
1142
|
+
if (others) out.push(...dimLines(ui, ` vs other models: ${others}`, columns));
|
|
1113
1143
|
out.push(...savingsDetail(ui, config, view, { totals, session, across, row, tier, narrow, columns }));
|
|
1114
1144
|
return [...out, ...notes()];
|
|
1115
1145
|
}
|
|
@@ -1149,8 +1179,8 @@ function usageTab(ui, config, view, usage, actions, _modelOptions, columns) {
|
|
|
1149
1179
|
...activityUsage(ui, config, view, actions, columns),
|
|
1150
1180
|
ui.text(' '),
|
|
1151
1181
|
ui.head('ESTIMATES · configured list prices'),
|
|
1152
|
-
ui.text(`Cache
|
|
1153
|
-
Number.isFinite(view.estimate?.taxUsd) ? ui.text(`Switch
|
|
1182
|
+
ui.text(`Cache hits saved ${dollars(metrics.cacheBenefit)}`),
|
|
1183
|
+
Number.isFinite(view.estimate?.taxUsd) ? ui.text(`Switch cost ${dollars(view.estimate.taxUsd)}`) : null,
|
|
1154
1184
|
...(view.comparison
|
|
1155
1185
|
? [
|
|
1156
1186
|
ui.text(`Compared tiers ${view.comparison.incumbent} → ${view.comparison.candidate}`),
|
package/lib/route.mjs
CHANGED
|
@@ -53,8 +53,9 @@ export function emptyLoop(config, model) {
|
|
|
53
53
|
turnId: null,
|
|
54
54
|
decision: null,
|
|
55
55
|
ineligible: [],
|
|
56
|
-
//
|
|
57
|
-
|
|
56
|
+
// Each configured model's simulated cache, by models key, for the routing-vs-your-model readout (compareModels in
|
|
57
|
+
// savings.mjs).
|
|
58
|
+
yoursBy: {},
|
|
58
59
|
};
|
|
59
60
|
}
|
|
60
61
|
|
|
@@ -71,7 +72,7 @@ export function resetHistory(loop) {
|
|
|
71
72
|
would: loop.would ? { ...loop.would, state: freshState(loop.would.state) } : null,
|
|
72
73
|
// The active turn keeps its route, pin and fallback suspension; the next turn decides afresh.
|
|
73
74
|
ineligible: [],
|
|
74
|
-
|
|
75
|
+
yoursBy: {},
|
|
75
76
|
};
|
|
76
77
|
}
|
|
77
78
|
|
package/lib/savings.mjs
CHANGED
|
@@ -25,6 +25,17 @@ export function modelSpec(config, id) {
|
|
|
25
25
|
return Object.values(config.models).find((model) => isSameModel(model.id, bare) || dated(model)) ?? null;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
// A by-model record's own entry for a models key, else undefined: router.json may name a model `toString`, which a
|
|
29
|
+
// plain object would otherwise answer from its prototype, or `null`, which a missing alias would coerce to.
|
|
30
|
+
export const modelEntry = (byModel, alias) =>
|
|
31
|
+
typeof alias === 'string' && byModel && Object.hasOwn(byModel, alias) ? byModel[alias] : undefined;
|
|
32
|
+
|
|
33
|
+
// The router.json models key (`opus`) of the entry an API model id runs on, or null.
|
|
34
|
+
export function modelAlias(config, id) {
|
|
35
|
+
const spec = modelSpec(config, id);
|
|
36
|
+
return spec ? (Object.keys(config.models).find((alias) => config.models[alias] === spec) ?? null) : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
28
39
|
// One request's list price in USD: `counts` are its uncached input, cache read, cache write and output tokens, the
|
|
29
40
|
// writes at `writeMultiplier` times input. Long-context rates follow the whole prompt.
|
|
30
41
|
export function requestUsd(model, { input, cacheRead, cacheWrite, output }, writeMultiplier) {
|
|
@@ -116,6 +127,34 @@ export function compareReply(config, state, { usage, served, yours, ttl, now })
|
|
|
116
127
|
};
|
|
117
128
|
}
|
|
118
129
|
|
|
130
|
+
// One main reply against every configured model as yours, at your effort, so the session readout can follow a
|
|
131
|
+
// `/model` change without replaying the session: `states` and `replies` by models key, each as compareReply returns,
|
|
132
|
+
// and `reply`, the prices against `yours`, the API model id /model selects now, or null.
|
|
133
|
+
export function compareModels(config, states, { usage, served, effort, ttl, now, yours }) {
|
|
134
|
+
const out = { states: {}, replies: {}, reply: null };
|
|
135
|
+
for (const [alias, model] of Object.entries(config.models)) {
|
|
136
|
+
const { state, reply } = compareReply(config, modelEntry(states, alias) ?? null, {
|
|
137
|
+
usage,
|
|
138
|
+
served,
|
|
139
|
+
yours: { model: model.id, effort },
|
|
140
|
+
ttl,
|
|
141
|
+
now,
|
|
142
|
+
});
|
|
143
|
+
out.states[alias] = state;
|
|
144
|
+
if (reply) out.replies[alias] = reply;
|
|
145
|
+
}
|
|
146
|
+
out.reply = modelEntry(out.replies, modelAlias(config, yours)) ?? null;
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Each model's session totals with this reply's prices added; models without a price for it keep theirs.
|
|
151
|
+
export function addReplies(byModel, replies, tier) {
|
|
152
|
+
const next = { ...byModel };
|
|
153
|
+
for (const [alias, reply] of Object.entries(replies))
|
|
154
|
+
next[alias] = addReply(modelEntry(next, alias) ?? emptyTotals(), reply, tier);
|
|
155
|
+
return next;
|
|
156
|
+
}
|
|
157
|
+
|
|
119
158
|
const SUMS = ['routedUsd', 'yoursUsd', 'cheaperUsd', 'strongerUsd', 'switchUsd'];
|
|
120
159
|
|
|
121
160
|
// A session's totals: replies, the five sums, and per tier (`none`: not chosen by routing) the replies and their
|
package/lib/view.mjs
CHANGED
|
@@ -44,6 +44,8 @@ export function initialView(model) {
|
|
|
44
44
|
activityStore: null,
|
|
45
45
|
activityMetrics: null,
|
|
46
46
|
// Routing vs your model: this session's totals and the totals kept across sessions as last read or written.
|
|
47
|
+
// Routing vs each configured model this session, by models key; `savings` is the one /model selects now.
|
|
48
|
+
savingsBy: {},
|
|
47
49
|
savings: null,
|
|
48
50
|
savingsStore: null,
|
|
49
51
|
configPath: null,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexeiled/claude-router",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "Native Claude Code Mod: model and effort routing for the main conversation, advised by Jev, Clef, Clef Flash, OpenAI or Ollama.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alexei Ledenev",
|