@alexeiled/claude-router 1.11.0 → 1.13.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 +34 -0
- package/README.md +1 -1
- package/docs/activity-routing.md +3 -3
- package/docs/architecture.md +4 -4
- package/docs/evaluation.md +7 -7
- package/docs/native-router.md +6 -6
- package/docs/router-pane-now.svg +111 -75
- package/docs/router-pane-usage-whatif.svg +128 -0
- package/docs/router-pane-usage.svg +100 -87
- package/docs/user-guide.md +47 -21
- package/hooks/native-router.mjs +170 -45
- package/lib/display.mjs +5 -9
- package/lib/panel.mjs +719 -436
- package/lib/savings.mjs +8 -23
- package/lib/usage.mjs +399 -0
- package/lib/view.mjs +61 -11
- package/package.json +1 -1
- package/types/index.d.ts +45 -11
package/lib/panel.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { compareRoutes } from './activity.mjs';
|
|
2
|
-
import { latencyP95,
|
|
2
|
+
import { latencyP95, readMetrics, readStore, storeSummary } from './activity-stats.mjs';
|
|
3
3
|
import {
|
|
4
4
|
ACTIVITIES,
|
|
5
5
|
ACTIVITY_MODES,
|
|
@@ -17,7 +17,6 @@ import { effortSharesCache, routeEffort } from './cost.mjs';
|
|
|
17
17
|
import {
|
|
18
18
|
activityReading,
|
|
19
19
|
activityVerb,
|
|
20
|
-
bar,
|
|
21
20
|
classifierLabel,
|
|
22
21
|
classifierStatus,
|
|
23
22
|
differenceShare,
|
|
@@ -34,10 +33,7 @@ import {
|
|
|
34
33
|
routeName,
|
|
35
34
|
savingsColor,
|
|
36
35
|
signedUsd,
|
|
37
|
-
sinceDate,
|
|
38
36
|
sparkline,
|
|
39
|
-
strongerReplies,
|
|
40
|
-
switchCount,
|
|
41
37
|
TIER_COLOR,
|
|
42
38
|
unavailableReason,
|
|
43
39
|
usageMetrics,
|
|
@@ -46,16 +42,23 @@ import {
|
|
|
46
42
|
yourModel,
|
|
47
43
|
} from './display.mjs';
|
|
48
44
|
import { cellForModel, isSameModel } from './route.mjs';
|
|
49
|
-
import {
|
|
50
|
-
EARLY_REPLIES,
|
|
51
|
-
emptyTotals,
|
|
52
|
-
modelAlias,
|
|
53
|
-
modelEntry,
|
|
54
|
-
modelSpec,
|
|
55
|
-
readout,
|
|
56
|
-
readSavingsStore,
|
|
57
|
-
} from './savings.mjs';
|
|
45
|
+
import { modelSpec, readout } from './savings.mjs';
|
|
58
46
|
import { PASS_REASONS, readSubagentStore } from './subagents.mjs';
|
|
47
|
+
import {
|
|
48
|
+
allTime,
|
|
49
|
+
breakdown,
|
|
50
|
+
cellsWhere,
|
|
51
|
+
dayKey,
|
|
52
|
+
INSIGHT_REPLIES,
|
|
53
|
+
insights,
|
|
54
|
+
mostlyOn,
|
|
55
|
+
PERIODS,
|
|
56
|
+
periodRange,
|
|
57
|
+
readUsage,
|
|
58
|
+
summarize,
|
|
59
|
+
trend,
|
|
60
|
+
} from './usage.mjs';
|
|
61
|
+
import { mergePairs, STRIP_CELLS } from './view.mjs';
|
|
59
62
|
|
|
60
63
|
// Routes and Policy edit one draft and save together; Classifier saves each pick at once.
|
|
61
64
|
const TABS = [
|
|
@@ -121,7 +124,6 @@ const hostOf = (endpoint) => urlOf(endpoint)?.host ?? endpoint.replace(/^https?:
|
|
|
121
124
|
const LOCAL_HOSTS = ['127.0.0.1', 'localhost', '[::1]'];
|
|
122
125
|
const isLocal = (endpoint) => LOCAL_HOSTS.includes(urlOf(endpoint)?.hostname);
|
|
123
126
|
const windowSize = (tokens) => (tokens >= 1e6 ? `${tokens / 1e6}M` : `${Math.round(tokens / 1e3)}K`);
|
|
124
|
-
const share = (value) => (value === null ? '—' : `${value}%`);
|
|
125
127
|
const plural = (n, word, many = `${word}s`) => `${n} ${n === 1 ? word : many}`;
|
|
126
128
|
const contextSuffix = (metrics) =>
|
|
127
129
|
Number.isFinite(metrics.nextContext)
|
|
@@ -161,7 +163,7 @@ export function renderPanel(
|
|
|
161
163
|
view,
|
|
162
164
|
usage,
|
|
163
165
|
actions,
|
|
164
|
-
{ modelOptions = Object.keys(config.models), columns = null } = {},
|
|
166
|
+
{ modelOptions = Object.keys(config.models), columns = null, now = Date.now() } = {},
|
|
165
167
|
) {
|
|
166
168
|
const ui = kit(elements);
|
|
167
169
|
const tab = TABS.some(([id]) => id === view.tab) ? view.tab : 'now';
|
|
@@ -170,7 +172,7 @@ export function renderPanel(
|
|
|
170
172
|
flexDirection: 'column',
|
|
171
173
|
children: [
|
|
172
174
|
...header(ui, config, view, tab, actions),
|
|
173
|
-
...body(ui, config, view, usage, actions, modelOptions, columns),
|
|
175
|
+
...body(ui, config, view, usage, actions, modelOptions, columns, now),
|
|
174
176
|
...(view.help ? helpLines(ui) : []),
|
|
175
177
|
...statusBar(ui, config, view, tab, actions),
|
|
176
178
|
ui.Button({ key: 'close', label: 'Close', hotkey: 'q', role: 'dismiss', onPress: actions.close }),
|
|
@@ -257,9 +259,9 @@ function supportText(label, estimate) {
|
|
|
257
259
|
return `${label} gave ${direction} tiers ${Number.isFinite(mass) ? gave : '—'} (needs ${needs})${cost}`;
|
|
258
260
|
}
|
|
259
261
|
|
|
260
|
-
// What runs next turn and why first
|
|
261
|
-
// context and cost readings.
|
|
262
|
-
function nowTab(ui, config, view, usage, actions) {
|
|
262
|
+
// What runs next turn and why first, with the one-turn pins; then this session and today as headlines, the finding
|
|
263
|
+
// worth a look, and Claude's context and cost readings.
|
|
264
|
+
function nowTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
|
|
263
265
|
const metrics = usageMetrics(config, view, usage);
|
|
264
266
|
const unavailable = view.phase === 'unavailable';
|
|
265
267
|
const auto = view.mode === 'auto' && !unavailable;
|
|
@@ -312,30 +314,16 @@ function nowTab(ui, config, view, usage, actions) {
|
|
|
312
314
|
ui.Button({ key: 'key', label: 'Set up', hotkey: 'k', onPress: actions.key }),
|
|
313
315
|
)
|
|
314
316
|
: null,
|
|
315
|
-
ui
|
|
316
|
-
ui.line(
|
|
317
|
-
ui.head('TIERS'),
|
|
318
|
-
ui.dim(auto ? ` · ${label} support on the last turn · pin = next turn only` : ` · ${label} support`),
|
|
319
|
-
),
|
|
317
|
+
pinLine(ui, view, auto, unavailable, actions, columns),
|
|
320
318
|
];
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
ui.tier(tier, tier.padEnd(8)),
|
|
328
|
-
routeName(config, config.routes[tier]).padEnd(22),
|
|
329
|
-
meter ? ui.text(meter.text, { color: TIER_COLOR[tier] }) : ui.dim('—'.padEnd(10)),
|
|
330
|
-
meter ? ` ${percent(support).padStart(4)} ` : ' ',
|
|
331
|
-
auto ? ui.Button({ key: `pin-${tier}`, label: 'pin', onPress: () => actions.pin(tier) }) : null,
|
|
332
|
-
),
|
|
333
|
-
);
|
|
334
|
-
}
|
|
335
|
-
if (!auto)
|
|
336
|
-
out.push(ui.dim(unavailable ? ' Pins need routing, which is unavailable.' : ' Pins need routing on (o above).'));
|
|
337
|
-
out.push(ui.text(' '), ...replies(ui, view));
|
|
319
|
+
const session = summarize(readUsage(view.usageSession), periodRange('session', now));
|
|
320
|
+
const today = summarize(readUsage(view.usageStore), periodRange('today', now));
|
|
321
|
+
out.push(ui.text(' '), ...sessionLines(ui, view, session, actions, columns, now));
|
|
322
|
+
// Today repeats this session's part of it until another session adds to it.
|
|
323
|
+
const ownToday = summarize(readUsage(view.usageSession), periodRange('today', now));
|
|
324
|
+
if (today.replies > ownToday.replies) out.push(ui.text(' '), ...todayLines(ui, today, actions, columns));
|
|
338
325
|
out.push(
|
|
326
|
+
...worthLines(ui, today, actions, columns),
|
|
339
327
|
ui.text(' '),
|
|
340
328
|
ui.gauge('Context ', metrics.contextBar, contextSuffix(metrics)),
|
|
341
329
|
ui.line(
|
|
@@ -345,55 +333,242 @@ function nowTab(ui, config, view, usage, actions) {
|
|
|
345
333
|
: ui.text(`${metrics.cacheBar.percent}% reused on the last reply`, { color: metrics.cacheBar.color }),
|
|
346
334
|
ui.dim(' · '),
|
|
347
335
|
`cost ${dollars(metrics.cost)} `,
|
|
348
|
-
ui.dim('by Claude'),
|
|
336
|
+
ui.dim('by Claude '),
|
|
337
|
+
detailButton(ui, view, actions),
|
|
349
338
|
),
|
|
339
|
+
...(view.usageDetail ? readingDetail(ui, view, metrics) : []),
|
|
350
340
|
);
|
|
351
341
|
return out;
|
|
352
342
|
}
|
|
353
343
|
|
|
354
|
-
const
|
|
344
|
+
const detailButton = (ui, view, actions) =>
|
|
345
|
+
ui.Button({
|
|
346
|
+
key: 'usage-detail',
|
|
347
|
+
label: view.usageDetail ? 'less' : 'details',
|
|
348
|
+
variant: view.usageDetail ? 'primary' : 'secondary',
|
|
349
|
+
onPress: actions.usageDetail,
|
|
350
|
+
});
|
|
355
351
|
|
|
356
|
-
//
|
|
357
|
-
//
|
|
358
|
-
function
|
|
352
|
+
// Behind `details` on the Now tab: Claude's readings of the last reply, the input per reply, and the estimates the
|
|
353
|
+
// last decision weighed.
|
|
354
|
+
function readingDetail(ui, view, metrics) {
|
|
355
|
+
const history = view.history ?? [];
|
|
359
356
|
const tiers = view.tiers ?? [];
|
|
360
|
-
|
|
361
|
-
const
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
357
|
+
const glyphs = sparkline(history);
|
|
358
|
+
const trend = history.length
|
|
359
|
+
? ui.line(
|
|
360
|
+
' ',
|
|
361
|
+
...[...glyphs].map((glyph, i) => {
|
|
362
|
+
const tier = tiers[tiers.length - history.length + i];
|
|
363
|
+
return tier ? ui.tier(tier, glyph) : ui.text(glyph);
|
|
364
|
+
}),
|
|
365
|
+
ui.dim(` ${formatTokens(Math.min(...history))} … ${formatTokens(Math.max(...history))}`),
|
|
366
|
+
)
|
|
367
|
+
: ui.dim(' no replies yet');
|
|
369
368
|
return [
|
|
370
|
-
ui.
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
),
|
|
374
|
-
ui.
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
notRouted ? ui.dim(`· not routed ${notRouted}`) : null,
|
|
382
|
-
),
|
|
383
|
-
...(used.length
|
|
369
|
+
ui.dim(` context observed ${formatTokens(metrics.observed)} · estimated ${formatTokens(view.contextTokens)}`),
|
|
370
|
+
ui.dim(` cache read ${formatTokens(view.cacheRead)} · written ${formatTokens(view.cacheWrite)} tokens`),
|
|
371
|
+
ui.dim(` output ${formatTokens(view.outputTokens)} tokens on the last reply`),
|
|
372
|
+
ui.text(' '),
|
|
373
|
+
ui.head(`INPUT PER REPLY · last ${plural(history.length, 'reply', 'replies')} · lowest to highest`),
|
|
374
|
+
trend,
|
|
375
|
+
ui.text(' '),
|
|
376
|
+
ui.head('ESTIMATES · configured list prices'),
|
|
377
|
+
ui.text(`Cache hits saved ${dollars(metrics.cacheBenefit)}`),
|
|
378
|
+
Number.isFinite(view.estimate?.taxUsd) ? ui.text(`Switch cost ${dollars(view.estimate.taxUsd)}`) : null,
|
|
379
|
+
...(view.comparison
|
|
384
380
|
? [
|
|
385
|
-
ui.
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
381
|
+
ui.text(`Compared tiers ${view.comparison.incumbent} → ${view.comparison.candidate}`),
|
|
382
|
+
ui.text(
|
|
383
|
+
`Next-turn difference ${difference(view.comparison.minUsd)} to ${difference(view.comparison.maxUsd)}`,
|
|
384
|
+
),
|
|
385
|
+
ui.text(
|
|
386
|
+
`Payback ${view.comparison.paybackTurns === null ? 'none projected' : `${view.comparison.paybackTurns} later turns`}`,
|
|
391
387
|
),
|
|
392
388
|
]
|
|
393
389
|
: []),
|
|
394
390
|
];
|
|
395
391
|
}
|
|
396
392
|
|
|
393
|
+
const verbShare = (text) => text.replace(/^(\S+)/, (name) => activityVerb(name));
|
|
394
|
+
|
|
395
|
+
// One button per tier with the classifier's support on the last turn, the current tier marked; a press pins the next
|
|
396
|
+
// turn only. Without routing there is nothing to pin.
|
|
397
|
+
function pinLine(ui, view, auto, unavailable, actions, columns) {
|
|
398
|
+
if (!auto)
|
|
399
|
+
return ui.line(
|
|
400
|
+
'Pin next ',
|
|
401
|
+
ui.dim(unavailable ? 'needs routing, which is unavailable' : 'needs routing on (o above)'),
|
|
402
|
+
);
|
|
403
|
+
return ui.line(
|
|
404
|
+
'Pin next ',
|
|
405
|
+
...LADDER.flatMap((tier) => {
|
|
406
|
+
const support = view.probabilities?.[tier];
|
|
407
|
+
return [
|
|
408
|
+
ui.Button({
|
|
409
|
+
key: `pin-${tier}`,
|
|
410
|
+
label: `${view.tier === tier ? '▶ ' : ''}${tier} ${Number.isFinite(support) ? percent(support) : '—'}`,
|
|
411
|
+
variant: view.pendingPin === tier ? 'primary' : 'secondary',
|
|
412
|
+
onPress: () => actions.pin(tier),
|
|
413
|
+
}),
|
|
414
|
+
' ',
|
|
415
|
+
];
|
|
416
|
+
}),
|
|
417
|
+
!Number.isFinite(columns) || columns >= PIN_HINT_COLUMNS ? ui.dim(' next turn only') : null,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
const PIN_HINT_COLUMNS = 80;
|
|
421
|
+
|
|
422
|
+
// "13:41", or "Oct 12 13:41" for another day than `now`'s.
|
|
423
|
+
const clockText = (ms) => new Date(ms).toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit' });
|
|
424
|
+
const sinceText = (ms, now) =>
|
|
425
|
+
dayKey(ms) === dayKey(now) ? clockText(ms) : `${dayLabel(dayKey(ms))} ${clockText(ms)}`;
|
|
426
|
+
|
|
427
|
+
// The strip cell's tier: the one with most replies, the stronger on a tie; `none` where routing served none.
|
|
428
|
+
const STRIP_ORDER = [...LADDER, 'none'];
|
|
429
|
+
const cellTier = (cell) => STRIP_ORDER.reduce((top, key) => ((cell[key] ?? 0) > (cell[top] ?? 0) ? key : top));
|
|
430
|
+
|
|
431
|
+
// The session strip fitted to the pane beside its note: pairs of cells merge until both fit. The note says how many
|
|
432
|
+
// replies a cell holds, and since when the strip runs when Claude Code restarted after the session's first reply.
|
|
433
|
+
function fittedStrip(strip, columns, since, now) {
|
|
434
|
+
let { per, cells } = strip;
|
|
435
|
+
const restarted = Number.isFinite(strip.from) && Number.isFinite(since) && strip.from > since;
|
|
436
|
+
const note = () =>
|
|
437
|
+
[per > 1 ? `${per} replies per cell` : null, restarted ? `since ${sinceText(strip.from, now)}` : null]
|
|
438
|
+
.filter(Boolean)
|
|
439
|
+
.join(' · ');
|
|
440
|
+
const room = () => (Number.isFinite(columns) ? Math.max(8, columns - 4 - note().length) : STRIP_CELLS);
|
|
441
|
+
while (cells.length > room()) {
|
|
442
|
+
cells = mergePairs(cells);
|
|
443
|
+
per *= 2;
|
|
444
|
+
}
|
|
445
|
+
return { cells, note: note() };
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// This session: since its first routed reply, its routed replies and route switches; its replies as a strip colored
|
|
449
|
+
// by tier, with the replies per tier, and the routed replies per activity; the last switch; and routing against your
|
|
450
|
+
// model, linked to Usage.
|
|
451
|
+
function sessionLines(ui, view, summary, actions, columns, now) {
|
|
452
|
+
const since = readUsage(view.usageSession).since;
|
|
453
|
+
const strip = view.strip?.cells?.length ? fittedStrip(view.strip, columns, since, now) : null;
|
|
454
|
+
const counts = {};
|
|
455
|
+
for (const cell of view.strip?.cells ?? [])
|
|
456
|
+
for (const [key, n] of Object.entries(cell)) counts[key] = (counts[key] ?? 0) + n;
|
|
457
|
+
const replies = Object.values(counts).reduce((sum, n) => sum + n, 0);
|
|
458
|
+
const switches = summary.switches.tier + summary.switches.activity;
|
|
459
|
+
const head = [
|
|
460
|
+
Number.isFinite(since) ? `since ${sinceText(since, now)}` : null,
|
|
461
|
+
summary.replies
|
|
462
|
+
? plural(summary.replies, 'routed reply', 'routed replies')
|
|
463
|
+
: replies
|
|
464
|
+
? plural(replies, 'reply', 'replies')
|
|
465
|
+
: null,
|
|
466
|
+
summary.turns ? plural(switches, 'route switch', 'route switches') : null,
|
|
467
|
+
].filter(Boolean);
|
|
468
|
+
if (!strip && !summary.replies) return [ui.line(ui.head('SESSION'), ui.dim(' · no replies yet'))];
|
|
469
|
+
const out = [ui.line(ui.head('SESSION'), head.length ? ui.dim(` · ${head.join(' · ')}`) : null)];
|
|
470
|
+
if (strip) {
|
|
471
|
+
out.push(
|
|
472
|
+
ui.line(
|
|
473
|
+
' ',
|
|
474
|
+
...strip.cells.map((cell) => {
|
|
475
|
+
const tier = cellTier(cell);
|
|
476
|
+
return tier === 'none' ? ui.dim('·') : ui.tier(tier, '█');
|
|
477
|
+
}),
|
|
478
|
+
strip.note ? ui.dim(` ${strip.note}`) : null,
|
|
479
|
+
),
|
|
480
|
+
ui.line(
|
|
481
|
+
' ',
|
|
482
|
+
...TIERS.filter((tier) => counts[tier]).flatMap((tier) => [ui.tier(tier, '■ '), `${tier} ${counts[tier]} `]),
|
|
483
|
+
counts.none ? ui.dim(`· not routed ${counts.none}`) : null,
|
|
484
|
+
),
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
const used = breakdown(summary.cells, 'activity')
|
|
488
|
+
.filter((group) => ACTIVITIES.includes(group.name))
|
|
489
|
+
.sort((a, b) => b.sums[0] - a.sums[0])
|
|
490
|
+
.map((group) => [group.name, `${activityVerb(group.name)} ${group.sums[0]}`]);
|
|
491
|
+
// The most used first, as many as fit the pane; the rest as a count.
|
|
492
|
+
const shown = [];
|
|
493
|
+
for (const [i, entry] of used.entries()) {
|
|
494
|
+
const rest = used.length - i - 1;
|
|
495
|
+
const width =
|
|
496
|
+
2 + [...shown, entry].map(([, text]) => text).join(' · ').length + (rest ? ` · +${rest} more`.length : 0);
|
|
497
|
+
if (Number.isFinite(columns) && width > columns && shown.length) break;
|
|
498
|
+
shown.push(entry);
|
|
499
|
+
}
|
|
500
|
+
if (shown.length)
|
|
501
|
+
out.push(
|
|
502
|
+
ui.line(
|
|
503
|
+
' ',
|
|
504
|
+
...shown.flatMap(([name, text], i) => [
|
|
505
|
+
ui.activity(name),
|
|
506
|
+
ui.dim(`${text.slice(activityVerb(name).length)}${i < shown.length - 1 ? ' · ' : ''}`),
|
|
507
|
+
]),
|
|
508
|
+
shown.length < used.length ? ui.dim(` · +${used.length - shown.length} more`) : null,
|
|
509
|
+
),
|
|
510
|
+
);
|
|
511
|
+
const last = view.lastSwitch;
|
|
512
|
+
if (Number.isFinite(last?.at) && last.from && last.to) out.push(lastSwitchLine(ui, last, now));
|
|
513
|
+
if (summary.replies)
|
|
514
|
+
out.push(verdictLine(ui, readout(summary), () => actions.usageFor('session'), 'session', columns));
|
|
515
|
+
return out;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// "last switch 13:41 medium → high · debugging"; routes by name when the switch kept the tier.
|
|
519
|
+
function lastSwitchLine(ui, last, now) {
|
|
520
|
+
const byTier = TIERS.includes(last.from.tier) && TIERS.includes(last.to.tier) && last.from.tier !== last.to.tier;
|
|
521
|
+
return ui.line(
|
|
522
|
+
' ',
|
|
523
|
+
ui.dim(`last switch ${sinceText(last.at, now)} `),
|
|
524
|
+
...(byTier
|
|
525
|
+
? [ui.tier(last.from.tier), ui.dim(' → '), ui.tier(last.to.tier)]
|
|
526
|
+
: [routeLabel(last.from.model, last.from.effort), ui.dim(' → '), routeLabel(last.to.model, last.to.effort)]),
|
|
527
|
+
...(ACTIVITIES.includes(last.activity) ? [ui.dim(' · '), ui.activity(last.activity)] : []),
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Routing against your model, with the Usage tab on the same period one press away. The switch writes in it and an
|
|
532
|
+
// early sample follow where the pane has room, the early sample dropping first.
|
|
533
|
+
function verdictLine(ui, r, open, period, columns) {
|
|
534
|
+
const notes = [
|
|
535
|
+
r.switchUsd > 0 ? ` · switch writes ${usd(r.switchUsd)}` : null,
|
|
536
|
+
r.early ? ' · early sample' : null,
|
|
537
|
+
].filter(Boolean);
|
|
538
|
+
const width = () => 2 + verdictText(r).join('').length + notes.join('').length + USAGE_LINK;
|
|
539
|
+
while (notes.length && Number.isFinite(columns) && width() > columns) notes.pop();
|
|
540
|
+
return ui.line(
|
|
541
|
+
' ',
|
|
542
|
+
...verdictParts(ui, r),
|
|
543
|
+
...notes.map((note) => ui.dim(note)),
|
|
544
|
+
' ',
|
|
545
|
+
ui.Button({ key: `usage-${period}`, label: 'Usage', onPress: open }),
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
// " [ Usage ]" after the verdict.
|
|
549
|
+
const USAGE_LINK = 11;
|
|
550
|
+
|
|
551
|
+
// Today across sessions, shown once another session added to it.
|
|
552
|
+
function todayLines(ui, summary, actions, columns) {
|
|
553
|
+
const switches = summary.switches.tier + summary.switches.activity;
|
|
554
|
+
return [
|
|
555
|
+
ui.line(
|
|
556
|
+
ui.head('TODAY'),
|
|
557
|
+
ui.dim(
|
|
558
|
+
` · ${plural(summary.replies, 'routed reply', 'routed replies')} · ${plural(switches, 'route switch', 'route switches')}`,
|
|
559
|
+
),
|
|
560
|
+
),
|
|
561
|
+
verdictLine(ui, readout(summary), () => actions.usageFor('today'), 'today', columns),
|
|
562
|
+
];
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Today's first finding from Usage's WORTH A LOOK, with the tab that changes it; nothing when there is none.
|
|
566
|
+
function worthLines(ui, summary, actions, columns) {
|
|
567
|
+
const [first] = summary.replies >= INSIGHT_REPLIES ? insights(summary) : [];
|
|
568
|
+
if (!first) return [];
|
|
569
|
+
return [ui.text(' '), ui.line(ui.head('WORTH A LOOK'), ui.dim(' · today')), insightLine(ui, first, actions, columns)];
|
|
570
|
+
}
|
|
571
|
+
|
|
397
572
|
// Rows: [key, label, format, router.json field]. Typed as tuples so tsc keeps each position's type.
|
|
398
573
|
/** @type {[string, string, (value: number) => string, string][]} */
|
|
399
574
|
const POLICY_ROWS = [
|
|
@@ -892,12 +1067,24 @@ function classifierTab(ui, config, view, _usage, actions, _modelOptions, columns
|
|
|
892
1067
|
ui.Button({ key: 'key', label: 'Edit', hotkey: 'k', onPress: actions.key }),
|
|
893
1068
|
ui.dim(' opens /plugin configure'),
|
|
894
1069
|
),
|
|
1070
|
+
...sessionAgreement(ui, view),
|
|
895
1071
|
...(storeSummary(readStore(view.activityStore)).labelled > 0
|
|
896
1072
|
? [ui.text(' '), ...storeUsage(ui, view.activityStore, view.activityMetrics)]
|
|
897
1073
|
: []),
|
|
898
1074
|
];
|
|
899
1075
|
}
|
|
900
1076
|
|
|
1077
|
+
// How often this session's tools fit the classifier's accepted activity; none before a labelled turn.
|
|
1078
|
+
function sessionAgreement(ui, view) {
|
|
1079
|
+
const { matched = 0, total = 0 } = view.activityStats?.agreement ?? {};
|
|
1080
|
+
if (!total) return [];
|
|
1081
|
+
return [
|
|
1082
|
+
ui.text(' '),
|
|
1083
|
+
ui.head('THIS SESSION'),
|
|
1084
|
+
ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`),
|
|
1085
|
+
];
|
|
1086
|
+
}
|
|
1087
|
+
|
|
901
1088
|
// Green for a write or its undo, red for a refused write, yellow for everything else that needs a look.
|
|
902
1089
|
const noticeColor = (notice) =>
|
|
903
1090
|
/^Not saved/.test(notice) ? 'red' : /^(Saved|Undid|Path copied)/.test(notice) ? 'green' : 'yellow';
|
|
@@ -992,81 +1179,6 @@ function storeUsage(ui, store, metrics) {
|
|
|
992
1179
|
return out;
|
|
993
1180
|
}
|
|
994
1181
|
|
|
995
|
-
// The ACTIVITY table's columns before the cost, and the cost column.
|
|
996
|
-
const ACTIVITY_TABLE = 48;
|
|
997
|
-
const ACTIVITY_COST = 11;
|
|
998
|
-
|
|
999
|
-
// Session counts by activity: turns, requests, share, the estimated list price of their replies and the route most of
|
|
1000
|
-
// them ran on; the switches by cause and the tool agreement. The counts kept across sessions are on
|
|
1001
|
-
// the Classifier tab.
|
|
1002
|
-
function activityUsage(ui, config, view, columns) {
|
|
1003
|
-
const stats = view.activityStats ?? null;
|
|
1004
|
-
const mode = config.activityRouting;
|
|
1005
|
-
if (mode === 'off' && !stats)
|
|
1006
|
-
return [
|
|
1007
|
-
ui.head('BY ACTIVITY · activity routing off'),
|
|
1008
|
-
ui.dim(' Not asked. Set activity routing to on in the Routes tab.'),
|
|
1009
|
-
];
|
|
1010
|
-
const out = [ui.line(ui.head('BY ACTIVITY · this session'), ui.dim(` activity routing ${mode}`))];
|
|
1011
|
-
const rows = Object.entries(stats?.byActivity ?? {}).filter(([, counts]) => counts?.turns > 0);
|
|
1012
|
-
if (!rows.length) out.push(ui.dim(' no activity readings yet'));
|
|
1013
|
-
else {
|
|
1014
|
-
const total = rows.reduce((sum, [, counts]) => sum + counts.turns, 0);
|
|
1015
|
-
// A partial sum would read as the whole: a cost shows only when every reply had a price.
|
|
1016
|
-
const cost = (counts) =>
|
|
1017
|
-
Number.isFinite(counts.routedUsd) && counts.pricedRequests === counts.requests ? usd(counts.routedUsd) : '—';
|
|
1018
|
-
const route = (counts) => {
|
|
1019
|
-
const key = mostlyOn(counts);
|
|
1020
|
-
if (!key) return '—';
|
|
1021
|
-
const at = key.lastIndexOf('@');
|
|
1022
|
-
const effort = key.slice(at + 1);
|
|
1023
|
-
return routeLabel(key.slice(0, at), effort === 'session' ? null : effort);
|
|
1024
|
-
};
|
|
1025
|
-
// A narrow pane drops "mostly on", then the cost, before any column it had before them.
|
|
1026
|
-
const widest = Math.max('mostly on'.length, ...rows.map(([, counts]) => route(counts).length));
|
|
1027
|
-
const fits = (width) => !Number.isFinite(columns) || width <= columns;
|
|
1028
|
-
const showCost = fits(ACTIVITY_TABLE + ACTIVITY_COST);
|
|
1029
|
-
const showRoute = showCost && fits(ACTIVITY_TABLE + ACTIVITY_COST + 3 + widest);
|
|
1030
|
-
out.push(
|
|
1031
|
-
ui.dim(
|
|
1032
|
-
` ${''.padEnd(12)}${''.padEnd(10)}${'turns'.padStart(7)}${'requests'.padStart(10)}${'share'.padStart(7)}` +
|
|
1033
|
-
`${showCost ? 'est. cost'.padStart(ACTIVITY_COST) : ''}${showRoute ? ' mostly on' : ''}`,
|
|
1034
|
-
),
|
|
1035
|
-
);
|
|
1036
|
-
for (const [name, counts] of rows.sort((a, b) => b[1].turns - a[1].turns)) {
|
|
1037
|
-
const share = counts.turns / total;
|
|
1038
|
-
out.push(
|
|
1039
|
-
ui.line(
|
|
1040
|
-
' ',
|
|
1041
|
-
ui.cell(12, ui.activity(name)),
|
|
1042
|
-
ui.text(bar(share, 1, 10).text, { color: 'cyan' }),
|
|
1043
|
-
`${String(counts.turns).padStart(7)}${String(counts.requests).padStart(10)}${percent(share).padStart(7)}` +
|
|
1044
|
-
`${showCost ? cost(counts).padStart(ACTIVITY_COST) : ''}${showRoute ? ` ${route(counts)}` : ''}`,
|
|
1045
|
-
),
|
|
1046
|
-
);
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
if (stats) {
|
|
1050
|
-
const { tier, activity } = stats.switches;
|
|
1051
|
-
// Refused moves change no route.
|
|
1052
|
-
const refused = stats.lateral?.refused ?? 0;
|
|
1053
|
-
out.push(
|
|
1054
|
-
ui.text(
|
|
1055
|
-
`Switches ${tier + activity} · ${tier} by tier · ${activity} by activity${refused ? ` · ${refused} refused` : ''}`,
|
|
1056
|
-
),
|
|
1057
|
-
);
|
|
1058
|
-
const { matched, total } = stats.agreement;
|
|
1059
|
-
if (total > 0)
|
|
1060
|
-
out.push(ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`));
|
|
1061
|
-
}
|
|
1062
|
-
return out;
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
// Bars are this wide at the larger of routed and your model; the section drops them before any number when the pane is
|
|
1066
|
-
// narrower than the table.
|
|
1067
|
-
const SAVINGS_BAR = 22;
|
|
1068
|
-
const SAVINGS_COLUMN = 15;
|
|
1069
|
-
const SAVINGS_PARTS = [' cheaper models', ' stronger than yours', ' cache writes from switches'];
|
|
1070
1182
|
// The footer as [text, joiner] pieces, packed to the pane: two lines at full width, more in a narrow pane, never a
|
|
1071
1183
|
// line that ends in a joiner.
|
|
1072
1184
|
const SAVINGS_FOOTER = [
|
|
@@ -1105,112 +1217,6 @@ const dimLines = (ui, text, columns) => {
|
|
|
1105
1217
|
const footer = (ui, columns) =>
|
|
1106
1218
|
pack(SAVINGS_FOOTER, Math.min(FULL_WIDTH, columns ?? FULL_WIDTH), ' ').map((line) => ui.dim(line));
|
|
1107
1219
|
|
|
1108
|
-
// One readout cell of the Difference row: "too early" below EARLY_REPLIES replies, else the signed amount and share.
|
|
1109
|
-
function differenceCell(ui, r) {
|
|
1110
|
-
if (!r) return '';
|
|
1111
|
-
if (r.early) return ui.dim('too early');
|
|
1112
|
-
const share = differenceShare(r);
|
|
1113
|
-
return ui.text(`${signedUsd(r.differenceUsd)}${share ? ` ${share}` : ''}`, {
|
|
1114
|
-
bold: true,
|
|
1115
|
-
color: savingsColor(r.differenceUsd),
|
|
1116
|
-
});
|
|
1117
|
-
}
|
|
1118
|
-
|
|
1119
|
-
const partCell = (ui, value) => (value === null ? '' : ui.text(signedUsd(value), { color: savingsColor(value) }));
|
|
1120
|
-
|
|
1121
|
-
// A bar of `parts` ([usd, color]) at `scale` cells per dollar, filled to SAVINGS_BAR with dim cells. Each part ends at
|
|
1122
|
-
// its rounded running total, so the parts add up to the bar.
|
|
1123
|
-
function moneyBar(ui, parts, scale) {
|
|
1124
|
-
let at = 0;
|
|
1125
|
-
let sum = 0;
|
|
1126
|
-
const out = [];
|
|
1127
|
-
for (const [value, color] of parts) {
|
|
1128
|
-
sum += value;
|
|
1129
|
-
const end = Math.round(sum * scale);
|
|
1130
|
-
if (end > at) out.push(ui.text('█'.repeat(end - at), color ? { color } : {}));
|
|
1131
|
-
at = Math.max(at, end);
|
|
1132
|
-
}
|
|
1133
|
-
if (at < SAVINGS_BAR) out.push(ui.dim('░'.repeat(SAVINGS_BAR - at)));
|
|
1134
|
-
return out;
|
|
1135
|
-
}
|
|
1136
|
-
|
|
1137
|
-
// The parts of a session readout past "too early": each part, the stronger replies, the two costs as bars (not in a
|
|
1138
|
-
// narrow pane), the replies by tier and why routing spent more.
|
|
1139
|
-
function savingsDetail(ui, view, { totals, session, across, row, tier, narrow, columns }) {
|
|
1140
|
-
const out = [];
|
|
1141
|
-
const savedPart = (key) => (across && !across.early ? partCell(ui, across[key]) : null);
|
|
1142
|
-
const stronger = strongerReplies(totals);
|
|
1143
|
-
out.push(
|
|
1144
|
-
row(ui.dim(SAVINGS_PARTS[0]), partCell(ui, session.cheaperUsd), savedPart('cheaperUsd')),
|
|
1145
|
-
row(ui.dim(SAVINGS_PARTS[1]), partCell(ui, session.strongerUsd), savedPart('strongerUsd')),
|
|
1146
|
-
...(stronger.count
|
|
1147
|
-
? dimLines(
|
|
1148
|
-
ui,
|
|
1149
|
-
` ${stronger.count} repl${stronger.count === 1 ? 'y' : 'ies'} on ${stronger.several ? 'stronger models' : modelName(stronger.model)}`,
|
|
1150
|
-
columns,
|
|
1151
|
-
)
|
|
1152
|
-
: []),
|
|
1153
|
-
row(ui.dim(SAVINGS_PARTS[2]), partCell(ui, session.switchUsd), savedPart('switchUsd')),
|
|
1154
|
-
ui.text(' '),
|
|
1155
|
-
);
|
|
1156
|
-
const largest = Math.max(totals.routedUsd, totals.yoursUsd);
|
|
1157
|
-
if (!narrow && largest > 0) {
|
|
1158
|
-
const scale = SAVINGS_BAR / largest;
|
|
1159
|
-
const tiers = [...TIERS, 'none'];
|
|
1160
|
-
const routedParts = tiers.map((t) => [totals.tierUsd[t] ?? 0, TIER_COLOR[t]]);
|
|
1161
|
-
out.push(
|
|
1162
|
-
ui.line(ui.dim(' routed '), ...moneyBar(ui, routedParts, scale), ` ${usd(session.routedUsd)}`),
|
|
1163
|
-
ui.line(
|
|
1164
|
-
ui.dim(' your model '),
|
|
1165
|
-
...moneyBar(ui, [[totals.yoursUsd, TIER_COLOR[tier]]], scale),
|
|
1166
|
-
` ${usd(session.yoursUsd)}`,
|
|
1167
|
-
),
|
|
1168
|
-
);
|
|
1169
|
-
}
|
|
1170
|
-
const shares = [...TIERS, 'none'].filter((t) => totals.tiers[t] > 0);
|
|
1171
|
-
out.push(
|
|
1172
|
-
ui.line(
|
|
1173
|
-
' ',
|
|
1174
|
-
...shares.flatMap((t, i) => [
|
|
1175
|
-
t === 'none' ? ui.dim('■ ') : ui.tier(t, '■ '),
|
|
1176
|
-
ui.dim(
|
|
1177
|
-
`${t === 'none' ? 'not routed' : t} ${percent(totals.tiers[t] / totals.replies)}${i === shares.length - 1 ? ' of replies' : ' '}`,
|
|
1178
|
-
),
|
|
1179
|
-
]),
|
|
1180
|
-
),
|
|
1181
|
-
);
|
|
1182
|
-
if (session.differenceUsd > 0 && stronger.count && session.strongerUsd >= session.switchUsd) {
|
|
1183
|
-
const models = stronger.several ? 'stronger models' : modelName(stronger.model);
|
|
1184
|
-
out.push(
|
|
1185
|
-
...dimLines(
|
|
1186
|
-
ui,
|
|
1187
|
-
` Routing spent more than ${modelName(view.nativeModel)} alone because upper tiers ran on ${models}.`,
|
|
1188
|
-
columns,
|
|
1189
|
-
),
|
|
1190
|
-
...dimLines(ui, ' To spend less, pick cheaper models for those tiers in the Routes tab.', columns),
|
|
1191
|
-
);
|
|
1192
|
-
}
|
|
1193
|
-
return out;
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
// This session against each other priced model as yours, those past "too early", in router.json order:
|
|
1197
|
-
// "Opus 5.5 saved $6.20 · Haiku 5.5 extra $1.10". Null when there is none.
|
|
1198
|
-
function otherModels(config, view) {
|
|
1199
|
-
const current = modelAlias(config, view.nativeModel);
|
|
1200
|
-
const parts = Object.keys(config.models)
|
|
1201
|
-
.filter((alias) => alias !== current && modelEntry(view.savingsBy, alias))
|
|
1202
|
-
.map((alias) => [modelName(config.models[alias].id), readout(modelEntry(view.savingsBy, alias))])
|
|
1203
|
-
.filter(([, r]) => !r.early)
|
|
1204
|
-
.map(([name, r]) =>
|
|
1205
|
-
r.differenceUsd < 0
|
|
1206
|
-
? `${name} saved ${usd(r.differenceUsd)}`
|
|
1207
|
-
: r.differenceUsd > 0
|
|
1208
|
-
? `${name} extra ${usd(r.differenceUsd)}`
|
|
1209
|
-
: `${name} same cost`,
|
|
1210
|
-
);
|
|
1211
|
-
return parts.length ? parts.join(' · ') : null;
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
1220
|
// The tier whose route is your model at your effort, for its color; else any tier on your model.
|
|
1215
1221
|
function yourTier(config, view) {
|
|
1216
1222
|
const runs = (tier) => {
|
|
@@ -1223,107 +1229,6 @@ function yourTier(config, view) {
|
|
|
1223
1229
|
return TIERS.find(runs) ?? cellForModel(config, view.nativeModel)?.tier ?? null;
|
|
1224
1230
|
}
|
|
1225
1231
|
|
|
1226
|
-
// ROUTING VS YOUR MODEL: what routed replies cost at list prices against the same tokens on the session's own model
|
|
1227
|
-
// and effort, this session and since the last reset, split into its three parts; then the two costs as bars and the
|
|
1228
|
-
// replies by tier. Below EARLY_REPLIES replies a column reads "too early"; with routing off or unavailable and no
|
|
1229
|
-
// routed reply, one sentence says so.
|
|
1230
|
-
function savingsSection(ui, config, view, columns) {
|
|
1231
|
-
const name = yourModel(view);
|
|
1232
|
-
const tier = yourTier(config, view);
|
|
1233
|
-
const title = [ui.head('ROUTING VS YOUR MODEL'), ui.dim(' · '), tier ? ui.tier(tier, name) : ui.text(name)];
|
|
1234
|
-
if (!modelSpec(config, view.nativeModel))
|
|
1235
|
-
return [ui.line(...title), ui.dim(` ${modelName(view.nativeModel)} has no list price in router.json models.`)];
|
|
1236
|
-
const totals = view.savings ?? emptyTotals();
|
|
1237
|
-
const session = readout(totals);
|
|
1238
|
-
const store = readSavingsStore(view.savingsStore);
|
|
1239
|
-
let across = store.replies ? readout(store) : null;
|
|
1240
|
-
const same = 'Same tokens on your model';
|
|
1241
|
-
const unavailable = view.phase === 'unavailable';
|
|
1242
|
-
const quiet = (unavailable || view.mode === 'manual') && !session.replies;
|
|
1243
|
-
// The label column fits the longest label shown, two spaces before the amounts.
|
|
1244
|
-
const labels = [` ${same}`, ...(quiet || session.early ? [] : SAVINGS_PARTS)];
|
|
1245
|
-
const widest = Math.max(...labels.map((text) => text.length)) + 2;
|
|
1246
|
-
const titleWidth = 'ROUTING VS YOUR MODEL · '.length + name.length;
|
|
1247
|
-
// The last column needs only its widest value, "−$11.42 −22%".
|
|
1248
|
-
const table = (label) => label + SAVINGS_COLUMN + (across ? 13 : 0);
|
|
1249
|
-
// The column heads share the title's line when it fits; a narrow pane puts them on their own and drops the bars,
|
|
1250
|
-
// and a narrower one the saved column too.
|
|
1251
|
-
let label = Math.max(titleWidth + 3, widest);
|
|
1252
|
-
const narrow = Number.isFinite(columns) && table(label) > columns;
|
|
1253
|
-
if (narrow) label = widest;
|
|
1254
|
-
if (narrow && table(label) > columns) across = null;
|
|
1255
|
-
const heads = [
|
|
1256
|
-
ui.cell(SAVINGS_COLUMN, ui.dim('this session')),
|
|
1257
|
-
across ? ui.dim(`since ${sinceDate(store.since)}`) : null,
|
|
1258
|
-
];
|
|
1259
|
-
const row = (name, mine, saved = null) =>
|
|
1260
|
-
ui.line(ui.cell(label, name), ui.cell(SAVINGS_COLUMN, mine), across && saved !== null ? saved : null);
|
|
1261
|
-
// The saved column adds up sessions that each had their own model.
|
|
1262
|
-
const notes = () => [
|
|
1263
|
-
ui.text(' '),
|
|
1264
|
-
...(across
|
|
1265
|
-
? dimLines(ui, ` since ${sinceDate(store.since)}: each session compared with its own model`, columns)
|
|
1266
|
-
: []),
|
|
1267
|
-
...footer(ui, columns),
|
|
1268
|
-
];
|
|
1269
|
-
const out = narrow
|
|
1270
|
-
? [ui.line(...title), ui.line(ui.cell(label, ''), ...heads)]
|
|
1271
|
-
: [ui.line(ui.cell(label, ui.line(...title)), ...heads)];
|
|
1272
|
-
if (quiet) {
|
|
1273
|
-
out.push(
|
|
1274
|
-
...dimLines(
|
|
1275
|
-
ui,
|
|
1276
|
-
` Routing is ${unavailable ? 'unavailable' : 'off'}: every reply this session used your model.`,
|
|
1277
|
-
columns,
|
|
1278
|
-
),
|
|
1279
|
-
row(' Difference', '$0.00', differenceCell(ui, across)),
|
|
1280
|
-
);
|
|
1281
|
-
return [...out, ...notes()];
|
|
1282
|
-
}
|
|
1283
|
-
out.push(
|
|
1284
|
-
row(' Routed replies', usd(session.routedUsd), across ? usd(across.routedUsd) : null),
|
|
1285
|
-
row(` ${same}`, usd(session.yoursUsd), across ? usd(across.yoursUsd) : null),
|
|
1286
|
-
row(ui.text(' Difference', { bold: true }), differenceCell(ui, session), differenceCell(ui, across)),
|
|
1287
|
-
);
|
|
1288
|
-
if (session.early) {
|
|
1289
|
-
out.push(
|
|
1290
|
-
...dimLines(
|
|
1291
|
-
ui,
|
|
1292
|
-
` ${session.replies} routed repl${session.replies === 1 ? 'y' : 'ies'} so far; the session figure appears after ${EARLY_REPLIES}`,
|
|
1293
|
-
columns,
|
|
1294
|
-
),
|
|
1295
|
-
);
|
|
1296
|
-
return [...out, ...notes()];
|
|
1297
|
-
}
|
|
1298
|
-
const others = otherModels(config, view);
|
|
1299
|
-
if (others) out.push(...dimLines(ui, ` vs other models: ${others}`, columns));
|
|
1300
|
-
out.push(...savingsDetail(ui, view, { totals, session, across, row, tier, narrow, columns }));
|
|
1301
|
-
return [...out, ...notes()];
|
|
1302
|
-
}
|
|
1303
|
-
|
|
1304
|
-
// The answer before the table: "Routing saved $1.84 (22%) this session vs Opus 5.5 · xhigh", or what it cost more.
|
|
1305
|
-
// None before EARLY_REPLIES routed replies, or while the section says every reply used your model.
|
|
1306
|
-
function savingsHeadline(ui, config, view) {
|
|
1307
|
-
if (!modelSpec(config, view.nativeModel)) return [];
|
|
1308
|
-
const session = readout(view.savings ?? emptyTotals());
|
|
1309
|
-
if (session.early || !session.replies) return [];
|
|
1310
|
-
const tier = yourTier(config, view);
|
|
1311
|
-
const name = yourModel(view);
|
|
1312
|
-
const share = differenceShare(session)?.replace(/^[−+]/, '');
|
|
1313
|
-
const amount = `${usd(session.differenceUsd)}${share ? ` (${share})` : ''}`;
|
|
1314
|
-
const verb = session.differenceUsd < 0 ? 'saved' : session.differenceUsd > 0 ? 'cost' : 'cost the same';
|
|
1315
|
-
return [
|
|
1316
|
-
ui.line(
|
|
1317
|
-
`Routing ${verb} `,
|
|
1318
|
-
session.differenceUsd === 0 ? '' : ui.text(amount, { bold: true, color: savingsColor(session.differenceUsd) }),
|
|
1319
|
-
session.differenceUsd > 0 ? ' more' : '',
|
|
1320
|
-
session.differenceUsd > 0 ? ' this session than ' : ' this session vs ',
|
|
1321
|
-
tier ? ui.tier(tier, name) : ui.text(name),
|
|
1322
|
-
),
|
|
1323
|
-
ui.text(' '),
|
|
1324
|
-
];
|
|
1325
|
-
}
|
|
1326
|
-
|
|
1327
1232
|
// Prices summed over the priced requests, and how many were not priced (a model without a price in router.json).
|
|
1328
1233
|
const pricedText = (text, counts) =>
|
|
1329
1234
|
counts.pricedRequests < counts.requests ? `${text} (${counts.requests - counts.pricedRequests} unpriced)` : text;
|
|
@@ -1367,93 +1272,471 @@ function subagentUsage(ui, config, view, columns) {
|
|
|
1367
1272
|
return out;
|
|
1368
1273
|
}
|
|
1369
1274
|
|
|
1370
|
-
|
|
1371
|
-
//
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1275
|
+
const PERIOD_LABEL = { session: 'Session', today: 'Today', '7d': '7d', '30d': '30d', month: 'Month', all: 'All' };
|
|
1276
|
+
// "No routed replies …" for each period.
|
|
1277
|
+
const PERIOD_PHRASE = {
|
|
1278
|
+
session: 'this session',
|
|
1279
|
+
today: 'today',
|
|
1280
|
+
'7d': 'in the last 7 days',
|
|
1281
|
+
'30d': 'in the last 30 days',
|
|
1282
|
+
month: 'this month',
|
|
1283
|
+
all: 'yet',
|
|
1284
|
+
};
|
|
1285
|
+
const GROUPS = [
|
|
1286
|
+
['activity', 'Activity'],
|
|
1287
|
+
['model', 'Model'],
|
|
1288
|
+
['tier', 'Tier'],
|
|
1289
|
+
];
|
|
1290
|
+
// What an open row lists: the routes of an activity, the activities of a model or a tier.
|
|
1291
|
+
const CHILD_BY = { activity: 'route', model: 'activity', tier: 'activity' };
|
|
1292
|
+
// The ledger's label column after its sign, and its amount column.
|
|
1293
|
+
const LEDGER_LABEL = 20;
|
|
1294
|
+
const LEDGER_AMOUNT = 9;
|
|
1295
|
+
// The breakdown: the open mark, the name, replies, cost and vs yours; "mostly on" or the share only where it fits.
|
|
1296
|
+
const NAME = 20;
|
|
1297
|
+
const BREAKDOWN = 4 + NAME + 8 + 10 + 11;
|
|
1298
|
+
const TAIL = 23;
|
|
1299
|
+
// Bars at full scale: the routed share of your model, and the costliest row of What if.
|
|
1300
|
+
const BAR = 22;
|
|
1301
|
+
const SPARK = '▁▂▃▄▅▆▇█';
|
|
1302
|
+
|
|
1303
|
+
const dayLabel = (key) => {
|
|
1304
|
+
const [y, m, d] = key.split('-').map(Number);
|
|
1305
|
+
return new Date(y, m - 1, d).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
|
1306
|
+
};
|
|
1307
|
+
// "Opus 5.5 · high" for a cell's `id@effort`.
|
|
1308
|
+
function routeText(route) {
|
|
1309
|
+
const at = route.lastIndexOf('@');
|
|
1310
|
+
const effort = route.slice(at + 1);
|
|
1311
|
+
return routeLabel(route.slice(0, at), effort === 'session' ? null : effort);
|
|
1312
|
+
}
|
|
1313
|
+
// A cents difference, so a row never reads "+$0.00" for a fraction of a cent.
|
|
1314
|
+
const cents = (usd) => Math.round(usd * 100) / 100;
|
|
1315
|
+
const differenceText = (ui, usdValue, width) => {
|
|
1316
|
+
const value = cents(usdValue);
|
|
1317
|
+
return ui.text(signedUsd(value).padStart(width), savingsColor(value) ? { color: savingsColor(value) } : {});
|
|
1318
|
+
};
|
|
1319
|
+
|
|
1320
|
+
// A breakdown row's name: an activity as its verb, a tier in its color, a model or a route by name.
|
|
1321
|
+
function groupName(ui, by, name) {
|
|
1322
|
+
if (name === 'none') return ui.dim(by === 'activity' ? 'no activity' : 'not routed');
|
|
1323
|
+
if (by === 'activity') return ui.activity(name);
|
|
1324
|
+
if (by === 'tier') return ui.tier(name);
|
|
1325
|
+
return ui.text(by === 'model' ? modelName(name) : routeText(name));
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// The period buttons; `p` presses the one after the shown period, so it steps through them.
|
|
1329
|
+
function periodRow(ui, period, actions) {
|
|
1330
|
+
const next = PERIODS[(PERIODS.indexOf(period) + 1) % PERIODS.length];
|
|
1331
|
+
return ui.line(
|
|
1332
|
+
...PERIODS.flatMap((id) => [
|
|
1333
|
+
ui.Button({
|
|
1334
|
+
key: `period-${id}`,
|
|
1335
|
+
label: PERIOD_LABEL[id],
|
|
1336
|
+
...(id === next ? { hotkey: 'p' } : {}),
|
|
1337
|
+
variant: id === period ? 'primary' : 'secondary',
|
|
1338
|
+
onPress: () => actions.period(id),
|
|
1339
|
+
}),
|
|
1340
|
+
' ',
|
|
1341
|
+
]),
|
|
1342
|
+
ui.dim(' p next'),
|
|
1343
|
+
);
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
// Whose replies the period holds and what each is compared with.
|
|
1347
|
+
function scopeLine(ui, config, view, period, summary, now) {
|
|
1348
|
+
if (period === 'session') {
|
|
1349
|
+
const recorded = recordedModels(config, view, summary);
|
|
1350
|
+
if (recorded)
|
|
1351
|
+
return ui.dim(
|
|
1352
|
+
Object.keys(summary.yours).length > 1
|
|
1353
|
+
? `this session · each reply vs the model /model selected: ${recorded}`
|
|
1354
|
+
: `this session · your model ${recorded}`,
|
|
1355
|
+
);
|
|
1356
|
+
const tier = yourTier(config, view);
|
|
1357
|
+
return ui.line(
|
|
1358
|
+
ui.dim('this session · your model '),
|
|
1359
|
+
tier ? ui.tier(tier, yourModel(view)) : ui.text(yourModel(view)),
|
|
1360
|
+
);
|
|
1361
|
+
}
|
|
1362
|
+
const { from, to } = periodRange(period, now);
|
|
1363
|
+
const first = summary.days[0]?.[0];
|
|
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`);
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
// The models a session's replies were priced against, when they are not the one `/model` selects now: after a
|
|
1376
|
+
// `/model` change the record keeps each reply against the model selected when it ran. Null when they are.
|
|
1377
|
+
function recordedModels(config, view, summary) {
|
|
1378
|
+
const ids = Object.keys(summary.yours);
|
|
1379
|
+
if (!ids.length || (ids.length === 1 && ids[0] === modelSpec(config, view.nativeModel)?.id)) return null;
|
|
1380
|
+
return ids.map(modelName).join(', ');
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
// The answer first: "Routing saved $1.84 (22%) vs your model", what it cost more, or why there is nothing to compare.
|
|
1384
|
+
function headline(ui, config, view, period, r) {
|
|
1385
|
+
if (!r.replies) {
|
|
1386
|
+
// A reply is priced against your model, so on a model without a price none is counted.
|
|
1387
|
+
if (period === 'session' && !modelSpec(config, view.nativeModel))
|
|
1388
|
+
return [ui.dim(`${modelName(view.nativeModel)} has no list price in router.json models.`)];
|
|
1389
|
+
const off = period === 'session' && (view.phase === 'unavailable' || view.mode === 'manual');
|
|
1390
|
+
return [
|
|
1391
|
+
ui.dim(
|
|
1392
|
+
off
|
|
1393
|
+
? `Routing is ${view.phase === 'unavailable' ? 'unavailable' : 'off'}: replies use your model and are not counted.`
|
|
1394
|
+
: `No routed replies ${PERIOD_PHRASE[period]}.`,
|
|
1395
|
+
),
|
|
1396
|
+
];
|
|
1397
|
+
}
|
|
1415
1398
|
return [
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1399
|
+
ui.line(
|
|
1400
|
+
...verdictParts(ui, r),
|
|
1401
|
+
r.early ? ui.text(` early sample · ${plural(r.replies, 'reply', 'replies')}`, { color: 'yellow' }) : null,
|
|
1402
|
+
),
|
|
1403
|
+
];
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
// "Routing saved $1.84 (22%) vs your model", or what it cost more: the text before the amount, the amount, the rest.
|
|
1407
|
+
function verdictText(r) {
|
|
1408
|
+
const share = differenceShare(r)?.replace(/^[−+]/, '');
|
|
1409
|
+
return [
|
|
1410
|
+
r.differenceUsd < 0 ? 'Routing saved ' : r.differenceUsd > 0 ? 'Routing cost ' : 'Routing cost the same as',
|
|
1411
|
+
r.differenceUsd === 0 ? '' : `${usd(r.differenceUsd)}${share ? ` (${share})` : ''}`,
|
|
1412
|
+
r.differenceUsd < 0 ? ' vs your model' : r.differenceUsd > 0 ? ' more than your model' : ' your model',
|
|
1413
|
+
];
|
|
1414
|
+
}
|
|
1415
|
+
// The verdict as line parts, the amount bold in the savings color.
|
|
1416
|
+
function verdictParts(ui, r) {
|
|
1417
|
+
const [before, amount, after] = verdictText(r);
|
|
1418
|
+
return [before, amount ? ui.text(amount, { bold: true, color: savingsColor(r.differenceUsd) }) : '', after];
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
// Your model − cheaper models + stronger models + switch cache writes = routed, in whole cents that add up (readout).
|
|
1422
|
+
function ledger(ui, config, view, { period, r, summary, legacy, columns }) {
|
|
1423
|
+
const tier = yourTier(config, view);
|
|
1424
|
+
const row = (sign, label, value, color, note = null, bold = false) =>
|
|
1425
|
+
ui.line(
|
|
1426
|
+
ui.dim(` ${sign} `),
|
|
1427
|
+
ui.cell(LEDGER_LABEL, ui.text(label, bold ? { bold: true } : {})),
|
|
1428
|
+
ui.text(usd(value).padStart(LEDGER_AMOUNT), { ...(color ? { color } : {}), ...(bold ? { bold: true } : {}) }),
|
|
1429
|
+
note ? ' ' : null,
|
|
1430
|
+
note,
|
|
1431
|
+
);
|
|
1432
|
+
// The undated totals kept no switch count, so All with them names none.
|
|
1433
|
+
const switches = legacy?.replies ? null : summary.switches.tier + summary.switches.activity;
|
|
1434
|
+
const recorded = period === 'session' ? recordedModels(config, view, summary) : null;
|
|
1435
|
+
const saving = -r.cheaperUsd;
|
|
1436
|
+
const ratio = r.yoursUsd > 0 ? r.routedUsd / r.yoursUsd : null;
|
|
1437
|
+
const wide = !Number.isFinite(columns) || columns >= 4 + LEDGER_LABEL + LEDGER_AMOUNT + 3 + BAR + 16;
|
|
1438
|
+
const filled = ratio === null ? 0 : Math.max(0, Math.min(BAR, Math.round(ratio * BAR)));
|
|
1439
|
+
const out = [
|
|
1440
|
+
row(
|
|
1441
|
+
' ',
|
|
1442
|
+
'Your model',
|
|
1443
|
+
r.yoursUsd,
|
|
1444
|
+
undefined,
|
|
1445
|
+
period !== 'session'
|
|
1446
|
+
? ui.dim('each session’s own model')
|
|
1447
|
+
: recorded
|
|
1448
|
+
? ui.dim(recorded)
|
|
1449
|
+
: tier
|
|
1450
|
+
? ui.tier(tier, yourModel(view))
|
|
1451
|
+
: ui.text(yourModel(view)),
|
|
1452
|
+
),
|
|
1453
|
+
row('−', 'Cheaper models', r.cheaperUsd, savingsColor(r.cheaperUsd)),
|
|
1454
|
+
row('+', 'Stronger models', r.strongerUsd, savingsColor(r.strongerUsd)),
|
|
1455
|
+
row(
|
|
1456
|
+
'+',
|
|
1457
|
+
'Switch cache writes',
|
|
1458
|
+
r.switchUsd,
|
|
1459
|
+
savingsColor(r.switchUsd),
|
|
1460
|
+
ui.dim(
|
|
1461
|
+
[
|
|
1462
|
+
switches === null ? null : plural(switches, 'route switch', 'route switches'),
|
|
1463
|
+
saving > 0 && r.switchUsd > 0 ? `${percent(r.switchUsd / saving)} of the saving` : null,
|
|
1425
1464
|
]
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1465
|
+
.filter(Boolean)
|
|
1466
|
+
.join(' · '),
|
|
1467
|
+
),
|
|
1468
|
+
),
|
|
1469
|
+
row(
|
|
1470
|
+
'=',
|
|
1471
|
+
'Routed',
|
|
1472
|
+
r.routedUsd,
|
|
1473
|
+
undefined,
|
|
1474
|
+
wide && ratio !== null
|
|
1475
|
+
? ui.line(
|
|
1476
|
+
ui.text('█'.repeat(filled), { color: 'cyan' }),
|
|
1477
|
+
ui.dim('░'.repeat(BAR - filled)),
|
|
1478
|
+
ui.dim(` ${ratio < 2 ? percent(ratio) : `×${ratio.toFixed(1)}`} of your model`),
|
|
1479
|
+
)
|
|
1480
|
+
: null,
|
|
1481
|
+
true,
|
|
1482
|
+
),
|
|
1483
|
+
];
|
|
1484
|
+
if (legacy?.replies)
|
|
1485
|
+
out.push(
|
|
1486
|
+
...dimLines(
|
|
1487
|
+
ui,
|
|
1488
|
+
` includes ${usd(legacy.routedUsd)} routed before dated records, kept without a breakdown`,
|
|
1489
|
+
columns,
|
|
1490
|
+
),
|
|
1491
|
+
);
|
|
1492
|
+
return out;
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
// Replies, turns, tokens and the cache share of the input.
|
|
1496
|
+
const statsLine = (ui, s) =>
|
|
1497
|
+
ui.dim(
|
|
1498
|
+
` ${plural(s.replies, 'reply', 'replies')} · ${plural(s.turns, 'turn')} · input ${formatTokens(s.inputTokens)}` +
|
|
1499
|
+
` · cache hit ${s.inputTokens ? percent(s.cacheReadTokens / s.inputTokens) : '—'} · output ${formatTokens(s.outputTokens)}`,
|
|
1500
|
+
);
|
|
1501
|
+
|
|
1502
|
+
// The period's cost by activity, model or tier, most first; `g` presses the next grouping. A row opens to what it
|
|
1503
|
+
// holds and its cost parts. A narrow pane drops "mostly on" (or the share) before any number.
|
|
1504
|
+
function breakdownLines(ui, view, actions, summary, columns) {
|
|
1505
|
+
const by = GROUPS.some(([id]) => id === view.usageGroup) ? view.usageGroup : 'activity';
|
|
1506
|
+
const next = GROUPS[(GROUPS.findIndex(([id]) => id === by) + 1) % GROUPS.length][0];
|
|
1507
|
+
const out = [
|
|
1432
1508
|
ui.line(
|
|
1433
|
-
ui.head('
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
}),
|
|
1509
|
+
ui.head('BY '),
|
|
1510
|
+
...GROUPS.flatMap(([id, label]) => [
|
|
1511
|
+
ui.Button({
|
|
1512
|
+
key: `group-${id}`,
|
|
1513
|
+
label,
|
|
1514
|
+
...(id === next ? { hotkey: 'g' } : {}),
|
|
1515
|
+
variant: id === by ? 'primary' : 'secondary',
|
|
1516
|
+
onPress: () => actions.usageGroup(id),
|
|
1517
|
+
}),
|
|
1518
|
+
' ',
|
|
1519
|
+
]),
|
|
1520
|
+
ui.dim(' g next'),
|
|
1446
1521
|
),
|
|
1447
|
-
|
|
1448
|
-
|
|
1522
|
+
];
|
|
1523
|
+
const groups = breakdown(summary.cells, by);
|
|
1524
|
+
if (!groups.length) return [...out, ui.dim(' no routed replies in this period')];
|
|
1525
|
+
const tail = !Number.isFinite(columns) || columns >= BREAKDOWN + TAIL;
|
|
1526
|
+
const tailHead = by === 'model' ? 'share' : 'mostly on';
|
|
1527
|
+
out.push(
|
|
1528
|
+
ui.dim(
|
|
1529
|
+
` ${''.padEnd(NAME)}${'replies'.padStart(8)}${'cost'.padStart(10)}${'vs yours'.padStart(11)}${tail ? ` ${tailHead}` : ''}`,
|
|
1530
|
+
),
|
|
1531
|
+
);
|
|
1532
|
+
const numbers = (sums) => [
|
|
1533
|
+
`${String(sums[0]).padStart(8)}${usd(sums[1]).padStart(10)}`,
|
|
1534
|
+
differenceText(ui, sums[1] - sums[2], 11),
|
|
1535
|
+
];
|
|
1536
|
+
for (const group of groups) {
|
|
1537
|
+
const id = `${by}:${group.name}`;
|
|
1538
|
+
const open = view.usageOpen === id;
|
|
1539
|
+
const route = by === 'model' ? null : mostlyOn(cellsWhere(summary.cells, by, group.name));
|
|
1540
|
+
out.push(
|
|
1541
|
+
ui.line(
|
|
1542
|
+
' ',
|
|
1543
|
+
ui.Button({
|
|
1544
|
+
key: `row-${id}`,
|
|
1545
|
+
label: open ? '▾' : '▸',
|
|
1546
|
+
plain: true,
|
|
1547
|
+
onPress: () => actions.usageOpen(open ? null : id),
|
|
1548
|
+
}),
|
|
1549
|
+
' ',
|
|
1550
|
+
ui.cell(NAME, groupName(ui, by, group.name)),
|
|
1551
|
+
...numbers(group.sums),
|
|
1552
|
+
!tail
|
|
1553
|
+
? null
|
|
1554
|
+
: by === 'model'
|
|
1555
|
+
? ui.dim(` ${percent(summary.routedUsd > 0 ? group.sums[1] / summary.routedUsd : 0)}`)
|
|
1556
|
+
: route
|
|
1557
|
+
? ui.dim(` ${routeText(route)}`)
|
|
1558
|
+
: null,
|
|
1559
|
+
),
|
|
1560
|
+
);
|
|
1561
|
+
if (!open) continue;
|
|
1562
|
+
for (const child of group.children)
|
|
1563
|
+
out.push(ui.line(' ', ui.cell(NAME - 2, groupName(ui, CHILD_BY[by], child.name)), ...numbers(child.sums)));
|
|
1564
|
+
const [, , , cheaper, stronger, switched, input, , output] = group.sums;
|
|
1565
|
+
out.push(
|
|
1566
|
+
...dimLines(
|
|
1567
|
+
ui,
|
|
1568
|
+
` cheaper ${signedUsd(cents(cheaper))} · stronger ${signedUsd(cents(stronger))} · switch ${signedUsd(cents(switched))} · input ${formatTokens(input)} · output ${formatTokens(output)}`,
|
|
1569
|
+
columns,
|
|
1570
|
+
),
|
|
1571
|
+
);
|
|
1572
|
+
}
|
|
1573
|
+
return out;
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
// What the period's tokens would cost on each model alone, as your model for every reply (compareModels), beside what
|
|
1577
|
+
// routing spent and your own models; a model some reply had no price on is left out.
|
|
1578
|
+
function compareLines(ui, summary) {
|
|
1579
|
+
const yours = Object.keys(summary.yours);
|
|
1580
|
+
const single = yours.length === 1 ? yours[0] : null;
|
|
1581
|
+
const rows = Object.entries(summary.whatIf)
|
|
1582
|
+
.filter(([, [n]]) => n === summary.replies)
|
|
1583
|
+
.map(([model, [, value]]) => ({
|
|
1584
|
+
label: `${modelName(model)}${model === single ? ' · yours' : ''}`,
|
|
1585
|
+
value,
|
|
1586
|
+
routed: false,
|
|
1587
|
+
yours: model === single,
|
|
1588
|
+
}));
|
|
1589
|
+
if (!rows.some((row) => row.yours))
|
|
1590
|
+
rows.push({
|
|
1591
|
+
label: yours.length > 1 ? 'Your models' : 'Your model',
|
|
1592
|
+
value: summary.yoursUsd,
|
|
1593
|
+
routed: false,
|
|
1594
|
+
yours: true,
|
|
1595
|
+
});
|
|
1596
|
+
rows.push({ label: 'Routed', value: summary.routedUsd, routed: true, yours: false });
|
|
1597
|
+
rows.sort((a, b) => a.value - b.value);
|
|
1598
|
+
const max = Math.max(...rows.map((row) => row.value));
|
|
1599
|
+
return [
|
|
1600
|
+
...rows.map((row) => {
|
|
1601
|
+
const filled = max > 0 ? Math.max(0, Math.min(BAR, Math.round((row.value / max) * BAR))) : 0;
|
|
1602
|
+
return ui.line(
|
|
1603
|
+
' ',
|
|
1604
|
+
ui.cell(22, ui.text(row.label, row.routed ? { bold: true } : {})),
|
|
1605
|
+
ui.text('█'.repeat(filled), { color: row.routed ? 'cyan' : 'gray' }),
|
|
1606
|
+
' '.repeat(BAR - filled),
|
|
1607
|
+
usd(row.value).padStart(LEDGER_AMOUNT),
|
|
1608
|
+
row.routed ? '' : row.yours ? ui.dim('—'.padStart(11)) : differenceText(ui, row.value - summary.yoursUsd, 11),
|
|
1609
|
+
);
|
|
1610
|
+
}),
|
|
1611
|
+
ui.dim(' same tokens and output on each model alone · answer quality not measured'),
|
|
1612
|
+
];
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
// Routed cost per hour, day or week as one line of blocks, a dot for a bucket without replies; and the What if switch.
|
|
1616
|
+
function trendLines(ui, view, actions, summary, period, now) {
|
|
1617
|
+
const toggle = ui.Button({
|
|
1618
|
+
key: 'usage-compare',
|
|
1619
|
+
label: view.usageCompare ? 'Trend' : 'What if',
|
|
1620
|
+
hotkey: 'c',
|
|
1621
|
+
onPress: actions.usageCompare,
|
|
1622
|
+
});
|
|
1623
|
+
const t = trend(summary, period, now);
|
|
1624
|
+
if (t.values.length < 2) return [ui.line(ui.head('COST'), ui.dim(' one bucket so far '), toggle)];
|
|
1625
|
+
const max = Math.max(...t.values);
|
|
1626
|
+
const glyphs = t.values.map((v) => (v > 0 ? SPARK[Math.min(7, Math.floor((v / max) * 8))] : '·')).join('');
|
|
1627
|
+
return [
|
|
1449
1628
|
ui.line(
|
|
1450
|
-
ui.
|
|
1451
|
-
ui.
|
|
1629
|
+
ui.head(`COST BY ${t.unit.toUpperCase()} `),
|
|
1630
|
+
ui.text(glyphs, { color: 'cyan' }),
|
|
1631
|
+
ui.dim(` peak ${usd(max)} `),
|
|
1632
|
+
toggle,
|
|
1452
1633
|
),
|
|
1453
|
-
ui.dim('Press ? for what these estimates leave out.'),
|
|
1454
1634
|
];
|
|
1455
1635
|
}
|
|
1456
1636
|
|
|
1637
|
+
// The evidence worth a look, each with the tab that changes it: the cell that carried most of the cost, the switch
|
|
1638
|
+
// writes against the saving, the cell that ran above your model. Never which model to use.
|
|
1639
|
+
function insightLines(ui, summary, actions, columns) {
|
|
1640
|
+
if (summary.replies < INSIGHT_REPLIES) return [];
|
|
1641
|
+
const list = insights(summary);
|
|
1642
|
+
if (!list.length) return [];
|
|
1643
|
+
return [
|
|
1644
|
+
ui.text(' '),
|
|
1645
|
+
ui.line(ui.head('WORTH A LOOK'), ui.dim(' · from the numbers; answer quality is not measured')),
|
|
1646
|
+
...list.map((insight) => insightLine(ui, insight, actions, columns)),
|
|
1647
|
+
];
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
// One finding with the tab that changes it.
|
|
1651
|
+
function insightLine(ui, insight, actions, columns) {
|
|
1652
|
+
const where = ({ activity, tier }) => [
|
|
1653
|
+
...(ACTIVITIES.includes(activity) ? [ui.activity(activity), ui.dim(' · ')] : []),
|
|
1654
|
+
TIERS.includes(tier) ? ui.tier(tier) : ui.dim('not routed'),
|
|
1655
|
+
];
|
|
1656
|
+
const link = (insight) =>
|
|
1657
|
+
TIERS.includes(insight.tier)
|
|
1658
|
+
? ui.Button({
|
|
1659
|
+
key: `insight-${insight.kind}`,
|
|
1660
|
+
label: 'Routes',
|
|
1661
|
+
onPress: () =>
|
|
1662
|
+
actions.openRoute(
|
|
1663
|
+
ACTIVITIES.includes(insight.activity) ? `${insight.activity}.${insight.tier}` : `tier.${insight.tier}`,
|
|
1664
|
+
),
|
|
1665
|
+
})
|
|
1666
|
+
: null;
|
|
1667
|
+
return insight.kind === 'switch'
|
|
1668
|
+
? ui.line(
|
|
1669
|
+
` Switch cache writes used ${percent(insight.share)} of the cheaper-model saving `,
|
|
1670
|
+
ui.Button({ key: 'insight-switch', label: 'Policy', onPress: () => actions.tab('policy') }),
|
|
1671
|
+
)
|
|
1672
|
+
: ui.line(
|
|
1673
|
+
' ',
|
|
1674
|
+
...where(insight),
|
|
1675
|
+
insight.kind === 'cost'
|
|
1676
|
+
? ` carried ${percent(insight.share)} of the cost`
|
|
1677
|
+
: ` ran ${usd(insight.usd)} above your model`,
|
|
1678
|
+
insight.route && (!Number.isFinite(columns) || columns >= 90)
|
|
1679
|
+
? ui.dim(` · mostly ${routeText(insight.route)}`)
|
|
1680
|
+
: null,
|
|
1681
|
+
' ',
|
|
1682
|
+
link(insight),
|
|
1683
|
+
);
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
// Delete history waits for a second press, as nothing brings the records back.
|
|
1687
|
+
function deleteLine(ui, view, actions) {
|
|
1688
|
+
if (!view.confirmDelete)
|
|
1689
|
+
return ui.Button({
|
|
1690
|
+
key: 'delete-history',
|
|
1691
|
+
label: 'Delete history',
|
|
1692
|
+
dimColor: true,
|
|
1693
|
+
onPress: () => actions.confirmDelete(true),
|
|
1694
|
+
});
|
|
1695
|
+
return ui.line(
|
|
1696
|
+
ui.text('Delete the usage, activity and subagent history of every session? ', { color: 'yellow' }),
|
|
1697
|
+
ui.Button({ key: 'delete-confirm', label: 'Delete', variant: 'primary', onPress: actions.resetStats }),
|
|
1698
|
+
' ',
|
|
1699
|
+
ui.Button({ key: 'delete-keep', label: 'Keep', onPress: () => actions.confirmDelete(false) }),
|
|
1700
|
+
);
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
// 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, across sessions.
|
|
1705
|
+
function usageTab(ui, config, view, _usage, actions, _modelOptions, columns, now) {
|
|
1706
|
+
const period = PERIODS.includes(view.usagePeriod) ? view.usagePeriod : 'session';
|
|
1707
|
+
const record = readUsage(period === 'session' ? view.usageSession : view.usageStore);
|
|
1708
|
+
const summary = summarize(record, periodRange(period, now));
|
|
1709
|
+
// All adds the undated savings totals kept from before the usage records.
|
|
1710
|
+
const kept = period === 'all' ? allTime(view.usageStore, view.savingsStore) : null;
|
|
1711
|
+
const legacy = kept?.legacy ?? null;
|
|
1712
|
+
const r = readout(kept?.totals ?? summary);
|
|
1713
|
+
const out = [periodRow(ui, period, actions), scopeLine(ui, config, view, period, summary, now), ui.text(' ')];
|
|
1714
|
+
out.push(...headline(ui, config, view, period, r));
|
|
1715
|
+
if (r.replies) {
|
|
1716
|
+
out.push(ui.text(' '), ...ledger(ui, config, view, { period, r, summary, legacy, columns }));
|
|
1717
|
+
if (summary.replies) out.push(ui.text(' '), statsLine(ui, summary));
|
|
1718
|
+
}
|
|
1719
|
+
if (summary.replies) {
|
|
1720
|
+
out.push(ui.text(' '), ...breakdownLines(ui, view, actions, summary, columns), ui.text(' '));
|
|
1721
|
+
out.push(
|
|
1722
|
+
...(view.usageCompare
|
|
1723
|
+
? [
|
|
1724
|
+
ui.line(
|
|
1725
|
+
ui.head('WHAT IF'),
|
|
1726
|
+
ui.dim(' · the same tokens on one model '),
|
|
1727
|
+
ui.Button({ key: 'usage-compare', label: 'Trend', hotkey: 'c', onPress: actions.usageCompare }),
|
|
1728
|
+
),
|
|
1729
|
+
...compareLines(ui, summary),
|
|
1730
|
+
]
|
|
1731
|
+
: trendLines(ui, view, actions, summary, period, now)),
|
|
1732
|
+
...insightLines(ui, summary, actions, columns),
|
|
1733
|
+
);
|
|
1734
|
+
}
|
|
1735
|
+
out.push(ui.text(' '), ...subagentUsage(ui, config, view, columns), ui.text(' '), ...footer(ui, columns));
|
|
1736
|
+
out.push(ui.text(' '), deleteLine(ui, view, actions));
|
|
1737
|
+
return out;
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1457
1740
|
function helpLines(ui) {
|
|
1458
1741
|
return [
|
|
1459
1742
|
ui.text(' '),
|