@alexeiled/claude-router 1.23.1 → 1.25.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 +62 -1
- package/README.md +39 -7
- package/docs/architecture.md +27 -4
- package/docs/auto-engine.md +152 -0
- package/docs/configuration.md +47 -3
- package/docs/evaluation.md +222 -3
- package/docs/native-router.md +11 -3
- package/docs/providers.md +28 -0
- package/docs/router-band.svg +124 -133
- package/docs/router-pane-classifier.svg +24 -23
- package/docs/router-pane-now.svg +9 -7
- package/docs/router-pane-policy.svg +4 -4
- package/docs/router-pane-routes.svg +8 -8
- package/docs/router-pane-usage-whatif.svg +14 -14
- package/docs/router-pane-usage.svg +20 -20
- package/docs/routing-modes.md +13 -4
- package/docs/user-guide.md +79 -19
- package/hooks/native-router.mjs +1194 -215
- package/lib/activity-stats.mjs +23 -4
- package/lib/activity.mjs +7 -5
- package/lib/auto-engine.mjs +414 -0
- package/lib/auto-route.mjs +253 -0
- package/lib/band.mjs +35 -440
- package/lib/classifier-client.mjs +12 -8
- package/lib/config-file.mjs +7 -2
- package/lib/config.mjs +64 -15
- package/lib/cost.mjs +5 -1
- package/lib/costs.mjs +4 -2
- package/lib/decisions.mjs +80 -5
- package/lib/display.mjs +69 -20
- package/lib/facts.mjs +30 -0
- package/lib/modes.mjs +8 -4
- package/lib/panel.mjs +75 -2163
- package/lib/policy.mjs +7 -6
- package/lib/providers.mjs +7 -0
- package/lib/replay-trace.mjs +550 -0
- package/lib/replay.mjs +718 -0
- package/lib/route-line.mjs +46 -0
- package/lib/route.mjs +35 -21
- package/lib/savings.mjs +14 -3
- package/lib/subagents.mjs +41 -11
- package/lib/subcommands.mjs +60 -0
- package/lib/ui/fallback.mjs +22 -0
- package/lib/ui/graphical/band.mjs +256 -0
- package/lib/ui/graphical/charts.mjs +163 -0
- package/lib/ui/graphical/classifier.mjs +153 -0
- package/lib/ui/graphical/frame.mjs +227 -0
- package/lib/ui/graphical/kit.mjs +156 -0
- package/lib/ui/graphical/meter.mjs +25 -0
- package/lib/ui/graphical/now.mjs +353 -0
- package/lib/ui/graphical/policy.mjs +56 -0
- package/lib/ui/graphical/routes.mjs +264 -0
- package/lib/ui/graphical/squares.mjs +29 -0
- package/lib/ui/graphical/usage.mjs +376 -0
- package/lib/ui/model/band.mjs +439 -0
- package/lib/ui/model/classifier.mjs +308 -0
- package/lib/ui/model/draft.mjs +62 -0
- package/lib/ui/model/format.mjs +41 -0
- package/lib/ui/model/frame.mjs +359 -0
- package/lib/ui/model/now.mjs +390 -0
- package/lib/ui/model/policy.mjs +194 -0
- package/lib/ui/model/routes.mjs +392 -0
- package/lib/ui/model/usage-agents.mjs +311 -0
- package/lib/ui/model/usage-breakdown.mjs +237 -0
- package/lib/ui/model/usage-shared.mjs +58 -0
- package/lib/ui/model/usage.mjs +330 -0
- package/lib/ui/terminal/band.mjs +220 -0
- package/lib/ui/terminal/classifier.mjs +142 -0
- package/lib/ui/terminal/frame.mjs +169 -0
- package/lib/ui/terminal/kit.mjs +133 -0
- package/lib/ui/terminal/now.mjs +300 -0
- package/lib/ui/terminal/policy.mjs +52 -0
- package/lib/ui/terminal/routes.mjs +167 -0
- package/lib/ui/terminal/usage.mjs +363 -0
- package/lib/ui/terminal/wrap.mjs +37 -0
- package/lib/ui/tokens.mjs +106 -0
- package/lib/usage.mjs +5 -5
- package/lib/view.mjs +5 -2
- package/package.json +3 -2
- package/types/index.d.ts +15 -0
package/lib/panel.mjs
CHANGED
|
@@ -1,2171 +1,83 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from './
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
routeLabel,
|
|
47
|
-
routeName,
|
|
48
|
-
routingModeOf,
|
|
49
|
-
routingModeText,
|
|
50
|
-
savingsColor,
|
|
51
|
-
sessionCost,
|
|
52
|
-
sessionUsage,
|
|
53
|
-
shortReason,
|
|
54
|
-
shortRoute,
|
|
55
|
-
shownConfig,
|
|
56
|
-
signedUsd,
|
|
57
|
-
sparkline,
|
|
58
|
-
switchesLost,
|
|
59
|
-
TIER_COLOR,
|
|
60
|
-
tierBars,
|
|
61
|
-
UNROUTED,
|
|
62
|
-
unavailableReason,
|
|
63
|
-
uncomparedText,
|
|
64
|
-
unpricedLine,
|
|
65
|
-
usageMetrics,
|
|
66
|
-
usd,
|
|
67
|
-
whyText,
|
|
68
|
-
yourModel,
|
|
69
|
-
} from './display.mjs';
|
|
70
|
-
import { cellForModel, isSameModel } from './route.mjs';
|
|
71
|
-
import { modelSpec } from './savings.mjs';
|
|
72
|
-
import {
|
|
73
|
-
modelTotals,
|
|
74
|
-
PASS_LABEL,
|
|
75
|
-
PASS_REASONS,
|
|
76
|
-
PASS_WHY,
|
|
77
|
-
readSubagentStore,
|
|
78
|
-
summarizeSubagents,
|
|
79
|
-
} from './subagents.mjs';
|
|
80
|
-
import {
|
|
81
|
-
allTime,
|
|
82
|
-
breakdown,
|
|
83
|
-
cellsWhere,
|
|
84
|
-
dayKey,
|
|
85
|
-
INSIGHT_REPLIES,
|
|
86
|
-
insights,
|
|
87
|
-
mostlyOn,
|
|
88
|
-
PERIODS,
|
|
89
|
-
periodRange,
|
|
90
|
-
summarize,
|
|
91
|
-
trend,
|
|
92
|
-
} from './usage.mjs';
|
|
93
|
-
import { mergePairs, STRIP_CELLS } from './view.mjs';
|
|
94
|
-
|
|
95
|
-
// Routes and Policy edit one draft and save together; Classifier saves each pick at once.
|
|
1
|
+
import { orTerminal } from './ui/fallback.mjs';
|
|
2
|
+
import { graphicalClassifier } from './ui/graphical/classifier.mjs';
|
|
3
|
+
import { graphicalFrame } from './ui/graphical/frame.mjs';
|
|
4
|
+
import { graphicalKit } from './ui/graphical/kit.mjs';
|
|
5
|
+
import { graphicalNow } from './ui/graphical/now.mjs';
|
|
6
|
+
import { graphicalPolicy } from './ui/graphical/policy.mjs';
|
|
7
|
+
import { graphicalRoutes } from './ui/graphical/routes.mjs';
|
|
8
|
+
import { graphicalUsage } from './ui/graphical/usage.mjs';
|
|
9
|
+
import { classifierOf } from './ui/model/classifier.mjs';
|
|
10
|
+
import { frameOf } from './ui/model/frame.mjs';
|
|
11
|
+
import { nowOf } from './ui/model/now.mjs';
|
|
12
|
+
import { policyOf } from './ui/model/policy.mjs';
|
|
13
|
+
import { routesOf } from './ui/model/routes.mjs';
|
|
14
|
+
import { usageOf } from './ui/model/usage.mjs';
|
|
15
|
+
import { terminalClassifier } from './ui/terminal/classifier.mjs';
|
|
16
|
+
import { terminalFrame } from './ui/terminal/frame.mjs';
|
|
17
|
+
import { kit } from './ui/terminal/kit.mjs';
|
|
18
|
+
import { terminalNow } from './ui/terminal/now.mjs';
|
|
19
|
+
import { terminalPolicy } from './ui/terminal/policy.mjs';
|
|
20
|
+
import { terminalRoutes } from './ui/terminal/routes.mjs';
|
|
21
|
+
import { terminalUsage } from './ui/terminal/usage.mjs';
|
|
22
|
+
import { isGraphical } from './ui/tokens.mjs';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A tab: the model it builds from the pane's context, and how each kind of surface draws that model. The generic ties a
|
|
26
|
+
* tab's renderers to its model, so tsc catches a renderer that reads what the model no longer holds.
|
|
27
|
+
* @template M
|
|
28
|
+
* @param {string} id
|
|
29
|
+
* @param {string} key the hotkey
|
|
30
|
+
* @param {string} label
|
|
31
|
+
* @param {(ctx: PaneContext) => M} modelOf
|
|
32
|
+
* @param {(ui: any, model: M, ctx: PaneContext) => object[]} terminal
|
|
33
|
+
* @param {(ui: any, model: M, ctx: PaneContext) => object[]} graphical
|
|
34
|
+
*/
|
|
35
|
+
const tabOf = (id, key, label, modelOf, terminal, graphical) => ({
|
|
36
|
+
id,
|
|
37
|
+
key,
|
|
38
|
+
label,
|
|
39
|
+
draw: (ui, ctx) => (isGraphical(ctx.surface) ? graphical : terminal)(ui, modelOf(ctx), ctx),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/** @typedef {import('./ui/model/frame.mjs').PaneContext} PaneContext */
|
|
43
|
+
|
|
44
|
+
// The tabs, in order. Routes and Policy edit one draft and save together, Classifier saves each pick at once.
|
|
45
|
+
const NOW_TAB = tabOf('now', '1', 'Now', nowOf, terminalNow, graphicalNow);
|
|
96
46
|
const TABS = [
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
];
|
|
103
|
-
const DRAFT_TABS = ['routes', 'policy'];
|
|
104
|
-
const TUNING_CHOICES = {
|
|
105
|
-
timeoutMs: [500, 1000, 1500, 3000, 5000],
|
|
106
|
-
downgradeVotes: [1, 2, 3],
|
|
107
|
-
horizon: [1, 3, 5, 10],
|
|
108
|
-
cashCapUsd: [0.5, 1, 2, 5],
|
|
109
|
-
activityMass: [0.5, 0.6, 0.7, 0.8],
|
|
110
|
-
};
|
|
111
|
-
const LADDER = [...TIERS].reverse();
|
|
112
|
-
// The effort option that sends no effort of its own: the request keeps the one Claude Code sends.
|
|
113
|
-
const SESSION_EFFORT = 'session';
|
|
114
|
-
const INHERIT = 'inherit';
|
|
115
|
-
// The Routes grid's row of the tiers' own routes; every other row is an activity.
|
|
116
|
-
const TIER_ROW = 'tier';
|
|
117
|
-
// Activity verbs are orange italic, as on the band.
|
|
118
|
-
const ACTIVITY_STYLE = { color: '#d7875f', italic: true };
|
|
119
|
-
|
|
120
|
-
const ACTIVITY_MODE_HINT = {
|
|
121
|
-
off: 'not asked; every turn runs its tier’s model',
|
|
122
|
-
on: 'an activity can change its tier’s model',
|
|
123
|
-
};
|
|
124
|
-
// What `subagentRouting` does, under the router.json path: it is set in the file, not in the pane.
|
|
125
|
-
const SUBAGENT_LINE = {
|
|
126
|
-
off: 'Subagents keep their own model (subagentRouting off).',
|
|
127
|
-
on: 'Subagents: on. Agents that pin no model get one at spawn, kept for every step.',
|
|
128
|
-
};
|
|
129
|
-
const EFFORT_SHORT = { low: 'low', medium: 'med', high: 'high', xhigh: 'xh', max: 'max', [INHERIT]: 'inh' };
|
|
130
|
-
|
|
131
|
-
// The route draft the Routes and Policy tabs edit: the saved routes, activity overrides and mode until the person
|
|
132
|
-
// changes one.
|
|
133
|
-
export function routeDraftOf(config, view) {
|
|
134
|
-
if (view.routeDraft) return view.routeDraft;
|
|
135
|
-
const base = structuredClone({
|
|
136
|
-
...(config.provider && config.provider !== 'anthropic' ? { provider: config.provider } : {}),
|
|
137
|
-
routes: config.routes,
|
|
138
|
-
baselineTier: config.baselineTier,
|
|
139
|
-
activities: config.activities,
|
|
140
|
-
activityRouting: config.activityRouting,
|
|
141
|
-
});
|
|
142
|
-
return { ...structuredClone(base), base };
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const dollars = (value) => (Number.isFinite(value) ? usd(value) : 'not reported');
|
|
146
|
-
// The host and port as the request sees them: user info before an `@` is not the host. An endpoint that does not
|
|
147
|
-
// parse, such as one with a placeholder in its host, shows as written up to its path, and is not this machine.
|
|
148
|
-
function urlOf(endpoint) {
|
|
149
|
-
try {
|
|
150
|
-
return new URL(endpoint);
|
|
151
|
-
} catch {
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
const hostOf = (endpoint) => urlOf(endpoint)?.host ?? endpoint.replace(/^https?:\/\//, '').split('/')[0];
|
|
156
|
-
const LOCAL_HOSTS = ['127.0.0.1', 'localhost', '[::1]'];
|
|
157
|
-
const isLocal = (endpoint) => LOCAL_HOSTS.includes(urlOf(endpoint)?.hostname);
|
|
158
|
-
const windowSize = (tokens) => (tokens >= 1e6 ? `${tokens / 1e6}M` : `${Math.round(tokens / 1e3)}K`);
|
|
159
|
-
const plural = (n, word, many = `${word}s`) => `${n} ${n === 1 ? word : many}`;
|
|
160
|
-
const contextSuffix = (metrics) =>
|
|
161
|
-
Number.isFinite(metrics.nextContext)
|
|
162
|
-
? `~${formatTokens(metrics.nextContext)} / ${formatTokens(metrics.window)}`
|
|
163
|
-
: 'no reading yet';
|
|
164
|
-
|
|
165
|
-
function kit({ Box, Text, Button, Select }) {
|
|
166
|
-
const text = (children, style = {}) => Text({ ...style, children });
|
|
167
|
-
return {
|
|
168
|
-
Box,
|
|
169
|
-
Button,
|
|
170
|
-
Select,
|
|
171
|
-
text,
|
|
172
|
-
dim: (children) => text(children, { dimColor: true }),
|
|
173
|
-
head: (children) => text(children, { bold: true, color: 'cyan' }),
|
|
174
|
-
tier: (tier, children = tier) => text(children, { bold: true, color: TIER_COLOR[tier] }),
|
|
175
|
-
// A tier as its bars, the way the band and Now draw it.
|
|
176
|
-
bars: (tier) => text(tierBars(tier), { color: TIER_COLOR[tier] }),
|
|
177
|
-
activity: (activity) => text(activity, ACTIVITY_STYLE),
|
|
178
|
-
// One row of mixed parts; strings become plain Text, nulls are dropped.
|
|
179
|
-
line: (...parts) =>
|
|
180
|
-
Box({
|
|
181
|
-
children: parts
|
|
182
|
-
.filter((part) => part !== null && part !== '')
|
|
183
|
-
.map((p) => (typeof p === 'string' ? text(p) : p)),
|
|
184
|
-
}),
|
|
185
|
-
// A fixed-width column, so controls of different widths still line up; it never shrinks in a narrow pane.
|
|
186
|
-
cell: (width, part) => Box({ width, flexShrink: 0, children: [typeof part === 'string' ? text(part) : part] }),
|
|
187
|
-
gauge: (name, value, suffix) =>
|
|
188
|
-
text(`${name}${value.text}${value.percent === null ? '' : ` ${String(value.percent).padStart(3)}%`} ${suffix}`, {
|
|
189
|
-
color: value.color,
|
|
190
|
-
}),
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export function renderPanel(
|
|
195
|
-
elements,
|
|
196
|
-
config,
|
|
197
|
-
view,
|
|
198
|
-
usage,
|
|
199
|
-
actions,
|
|
200
|
-
{ modelOptions = Object.keys(config.models), columns = null, now = Date.now() } = {},
|
|
201
|
-
) {
|
|
202
|
-
const ui = kit(elements);
|
|
203
|
-
const tab = TABS.some(([id]) => id === view.tab) ? view.tab : 'now';
|
|
204
|
-
const body = { now: nowTab, routes: routesTab, policy: policyTab, classifier: classifierTab, usage: usageTab }[tab];
|
|
205
|
-
// No Close button: Esc and the pane's own close dismiss it.
|
|
206
|
-
return ui.Box({
|
|
207
|
-
flexDirection: 'column',
|
|
208
|
-
children: [
|
|
209
|
-
...header(ui, config, view, tab, actions),
|
|
210
|
-
...(view.help ? helpLines(ui) : []),
|
|
211
|
-
...(config.provider && config.provider !== 'anthropic'
|
|
212
|
-
? [ui.dim(`profile ${[config.provider, view.configPath ?? config.nativePath].filter(Boolean).join(' · ')}`)]
|
|
213
|
-
: []),
|
|
214
|
-
...body(ui, config, view, usage, actions, modelOptions, columns, now),
|
|
215
|
-
...statusBar(ui, config, view, tab, actions),
|
|
216
|
-
],
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// Routing's state, its one mode control and the classifier's state on one line. While routing is unavailable, on and
|
|
221
|
-
// off would change nothing, so the line says why instead of offering them.
|
|
222
|
-
function routingState(ui, config, view, actions) {
|
|
223
|
-
if (view.phase === 'unavailable')
|
|
224
|
-
return [
|
|
225
|
-
ui.line(ui.head('ROUTER'), ' ', ui.text('unavailable', { color: 'yellow' }), ui.dim(' Claude’s model is kept')),
|
|
226
|
-
ui.text(unavailableReason(view), { color: 'yellow' }),
|
|
227
|
-
...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP.map((line) => ui.text(line, { color: 'yellow' })) : []),
|
|
228
|
-
];
|
|
229
|
-
const label = classifierLabel(config);
|
|
230
|
-
const error = view.error ?? missingCredentials(config, view, config.classifier);
|
|
231
|
-
// A radio pair, marked as the Classifier tab marks its active row.
|
|
232
|
-
const option = (mode, label, hotkey) =>
|
|
233
|
-
ui.Button({
|
|
234
|
-
key: mode,
|
|
235
|
-
label: `${view.mode === mode ? '◉' : '○'} ${label}`,
|
|
236
|
-
hotkey,
|
|
237
|
-
variant: view.mode === mode ? 'primary' : 'secondary',
|
|
238
|
-
onPress: () => actions.mode(mode),
|
|
239
|
-
});
|
|
240
|
-
return [
|
|
241
|
-
ui.line(
|
|
242
|
-
ui.head('ROUTER'),
|
|
243
|
-
' ',
|
|
244
|
-
option('auto', 'on', 'o'),
|
|
245
|
-
' ',
|
|
246
|
-
option('manual', 'off', 'f'),
|
|
247
|
-
' ',
|
|
248
|
-
error
|
|
249
|
-
? ui.text(classifierStatus(config, error), { color: 'yellow' })
|
|
250
|
-
: ui.text(`${label} ready${Number.isFinite(view.adviceMs) ? ` · ${view.adviceMs} ms` : ''}`, {
|
|
251
|
-
color: 'green',
|
|
252
|
-
}),
|
|
253
|
-
),
|
|
254
|
-
];
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// The tabs carry their number keys in their labels, so every key the pane answers is on screen.
|
|
258
|
-
function header(ui, config, view, tab, actions) {
|
|
259
|
-
const changes = routingChanges(config, view);
|
|
260
|
-
const unsaved = {
|
|
261
|
-
routes: changes.tiers.length + changes.cells.length + Number(changes.mode) > 0,
|
|
262
|
-
policy: changes.policy.length + Number(changes.baseline) > 0,
|
|
263
|
-
};
|
|
264
|
-
return [
|
|
265
|
-
...routingState(ui, config, view, actions),
|
|
266
|
-
ui.line(
|
|
267
|
-
...TABS.flatMap(([id, hotkey, label]) => [
|
|
268
|
-
ui.Button({
|
|
269
|
-
key: `tab-${id}`,
|
|
270
|
-
label: `${hotkey} ${label}${unsaved[id] ? ' ●' : ''}`,
|
|
271
|
-
hotkey,
|
|
272
|
-
variant: tab === id ? 'primary' : 'secondary',
|
|
273
|
-
onPress: () => actions.tab(id),
|
|
274
|
-
}),
|
|
275
|
-
' ',
|
|
276
|
-
]),
|
|
277
|
-
ui.Button({ key: 'help', label: '?', variant: view.help ? 'primary' : 'secondary', onPress: actions.help }),
|
|
278
|
-
),
|
|
279
|
-
ui.text(' '),
|
|
280
|
-
];
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// "Jev 93% ≥ 90% · switch ≈ $0.48": the support for the switch the policy weighed against the bar it set, and the switch
|
|
284
|
-
// cost. Null without a reading.
|
|
285
|
-
function supportText(label, estimate) {
|
|
286
|
-
if (!Number.isFinite(estimate?.threshold)) return null;
|
|
287
|
-
const mass = Number.isFinite(estimate.upgradeMass) ? estimate.upgradeMass : estimate.downgradeMass;
|
|
288
|
-
const cost = Number.isFinite(estimate.taxUsd) ? ` · switch ≈ ${usd(estimate.taxUsd)}` : '';
|
|
289
|
-
if (!Number.isFinite(mass)) return `${label} needs ${percent(estimate.threshold)}${cost}`;
|
|
290
|
-
const [gave, needs] = percentPair(mass, estimate.threshold);
|
|
291
|
-
return `${label} ${gave} ${mass >= estimate.threshold ? '≥' : '<'} ${needs}${cost}`;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// Three questions, in order: what runs next and why, what the plan and the session cost, and how full the context is.
|
|
295
|
-
// The cost table, today’s totals and the subagent list are on Usage.
|
|
296
|
-
function nowTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
|
|
297
|
-
const cost = sessionCost(config, view, usage, now);
|
|
298
|
-
return [
|
|
299
|
-
...nextLines(ui, config, view, actions, columns),
|
|
300
|
-
...planLines(ui, cost, now),
|
|
301
|
-
...sessionLines(ui, config, view, cost, actions, columns, now),
|
|
302
|
-
...contextLines(ui, config, view, usage, actions, now),
|
|
303
|
-
];
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// NEXT TURN. With routing off or unavailable only the model applies: no tier, activity or reason from an earlier turn.
|
|
307
|
-
function nextLines(ui, config, view, actions, columns) {
|
|
308
|
-
const unavailable = view.phase === 'unavailable';
|
|
309
|
-
const auto = view.mode === 'auto' && !unavailable;
|
|
310
|
-
const current = view.selectedModel ?? view.nativeModel;
|
|
311
|
-
const out = [ui.head('NEXT TURN')];
|
|
312
|
-
if (!auto) return [...out, ...offLines(ui, view, unavailable, current)];
|
|
313
|
-
const mode = config.activityRouting;
|
|
314
|
-
const reading = mode === 'off' ? null : activityReading(view);
|
|
315
|
-
const shown = shownConfig(config, view);
|
|
316
|
-
const base = view.tier ? resolveRoute(shown, view.tier) : null;
|
|
317
|
-
const override =
|
|
318
|
-
mode === 'on' && view.activity && base && !sameRoute(resolveRoute(shown, view.tier, view.activity), base);
|
|
319
|
-
const served = view.actualModel && current && !isSameModel(current, view.actualModel);
|
|
320
|
-
const support = supportText(classifierLabel(config), view.estimate);
|
|
321
|
-
const missing = missingCredentials(config, view, config.classifier);
|
|
322
|
-
const { mode: routingMode, source } = routingModeOf(view);
|
|
323
|
-
const lever = modeLever(config, view);
|
|
324
|
-
out.push(
|
|
325
|
-
ui.line(
|
|
326
|
-
' ',
|
|
327
|
-
view.tier ? ui.bars(view.tier) : ui.dim('○'),
|
|
328
|
-
' ',
|
|
329
|
-
ui.text(routeLabel(current, view.effort), { bold: true }),
|
|
330
|
-
reading
|
|
331
|
-
? ui.line(
|
|
332
|
-
' ',
|
|
333
|
-
// `uncertain` is an answer, not an activity: it applies none.
|
|
334
|
-
ACTIVITIES.includes(reading.choice) ? ui.activity(reading.choice) : ui.dim(reading.choice),
|
|
335
|
-
reading.share ? ` ${reading.share}` : '',
|
|
336
|
-
reading.others.length ? ui.dim(` ${reading.others[0]}`) : null,
|
|
337
|
-
view.taskPhase ? ui.dim(` phase ${view.taskPhase} · route activity ${view.activity ?? 'base'}`) : null,
|
|
338
|
-
)
|
|
339
|
-
: null,
|
|
340
|
-
),
|
|
341
|
-
view.reason
|
|
342
|
-
? ui.line(
|
|
343
|
-
ui.dim(' why '),
|
|
344
|
-
shortReason(view.reason, reasonActivity(config, view)),
|
|
345
|
-
support ? ui.dim(` ${support}`) : null,
|
|
346
|
-
)
|
|
347
|
-
: null,
|
|
348
|
-
override ? ui.dim(` ${whyText(config, view)} · tier route ${routeName(shown, base)}`) : null,
|
|
349
|
-
routingMode !== 'balanced' || source === 'session'
|
|
350
|
-
? ui.line(
|
|
351
|
-
ui.dim(' mode '),
|
|
352
|
-
ui.text(` ${routingMode} `, { backgroundColor: '#2a3040', color: '#c0c6d4' }),
|
|
353
|
-
ui.dim(` from ${source}${lever ? ` · ${lever} on this tier` : ''}`),
|
|
354
|
-
source === 'session' ? ui.dim(' · /router mode default clears it') : null,
|
|
355
|
-
)
|
|
356
|
-
: null,
|
|
357
|
-
view.projectError ? ui.text(` Ignored: ${view.projectError}`, { color: 'yellow' }) : null,
|
|
358
|
-
served ? ui.text(` ! served by ${modelName(view.actualModel)} · native fallback`, { color: 'yellow' }) : null,
|
|
359
|
-
missing
|
|
360
|
-
? ui.line(
|
|
361
|
-
ui.text(` ${classifierStatus(config, missing)}, keeping the model `, { color: 'yellow' }),
|
|
362
|
-
ui.Button({ key: 'key', label: 'Set up', hotkey: 'k', onPress: actions.key }),
|
|
363
|
-
)
|
|
364
|
-
: null,
|
|
365
|
-
...pinLines(ui, config, view, actions, columns),
|
|
366
|
-
ui.text(' '),
|
|
367
|
-
);
|
|
368
|
-
return out.filter(Boolean);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// Routing off or unavailable: the model that runs, and why nothing else applies.
|
|
372
|
-
function offLines(ui, view, unavailable, current) {
|
|
373
|
-
const model = unavailable ? routeLabel(view.nativeModel, view.nativeEffort) : routeLabel(current, view.effort);
|
|
374
|
-
return [
|
|
375
|
-
ui.line(
|
|
376
|
-
unavailable ? ui.text(' ✕ ', { color: 'yellow' }) : ui.dim(' ○ '),
|
|
377
|
-
ui.text(model, { bold: true }),
|
|
378
|
-
ui.dim(unavailable ? ' routing unavailable · Claude’s model' : ' routing off · /model choice'),
|
|
379
|
-
),
|
|
380
|
-
ui.line(ui.dim(' pin '), ui.dim(unavailable ? 'needs routing' : 'needs routing on (o)')),
|
|
381
|
-
ui.text(' '),
|
|
382
|
-
];
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
// One button per tier, low to high, named by the route it runs ("▂▄ Haiku·high") with the classifier's support on the
|
|
386
|
-
// last turn; the current tier is marked and a pending pin is filled. A press pins the next turn only.
|
|
387
|
-
function pinLines(ui, config, view, actions, columns) {
|
|
388
|
-
const shown = shownConfig(config, view);
|
|
389
|
-
const pin = (tier) => {
|
|
390
|
-
const support = view.probabilities?.[tier];
|
|
391
|
-
return ui.Button({
|
|
392
|
-
key: `pin-${tier}`,
|
|
393
|
-
label: `${view.tier === tier ? '▶ ' : ''}${tierBars(tier)} ${shortRoute(config, resolveRoute(shown, tier))} ${Number.isFinite(support) ? percent(support) : '—'}`,
|
|
394
|
-
variant: view.pendingPin === tier ? 'primary' : 'secondary',
|
|
395
|
-
onPress: () => actions.pin(tier),
|
|
396
|
-
});
|
|
397
|
-
};
|
|
398
|
-
const narrow = Number.isFinite(columns) && columns < PIN_ROW_COLUMNS;
|
|
399
|
-
const rows = narrow ? TIERS.map((tier) => [tier]) : [TIERS.slice(0, 2), TIERS.slice(2)];
|
|
400
|
-
return [
|
|
401
|
-
...rows.map((tiers, i) =>
|
|
402
|
-
ui.line(i ? ' ' : ui.dim(' pin '), ...tiers.flatMap((tier) => [pin(tier), ' '])),
|
|
403
|
-
),
|
|
404
|
-
view.pendingPin
|
|
405
|
-
? ui.line(
|
|
406
|
-
ui.text(' ⏵ next turn ', { color: 'yellow' }),
|
|
407
|
-
ui.bars(view.pendingPin),
|
|
408
|
-
` ${shortRoute(config, resolveRoute(shown, view.pendingPin))} `,
|
|
409
|
-
ui.Button({ key: 'unpin', label: 'unpin', onPress: actions.unpin }),
|
|
410
|
-
)
|
|
411
|
-
: null,
|
|
412
|
-
].filter(Boolean);
|
|
413
|
-
}
|
|
414
|
-
// Below this a pin row holds one pin.
|
|
415
|
-
const PIN_ROW_COLUMNS = 64;
|
|
416
|
-
|
|
417
|
-
// PLAN: Claude Code's plan windows, when it reports them.
|
|
418
|
-
function planLines(ui, cost, now) {
|
|
419
|
-
if (!cost.limits.length) return [];
|
|
420
|
-
return [
|
|
421
|
-
ui.line(ui.head('PLAN'), ui.dim(' Claude plan usage')),
|
|
422
|
-
...cost.limits.map((limit) => {
|
|
423
|
-
const [filled, empty] = gaugeCells(limit.percent / 100, 20);
|
|
424
|
-
const color = limitColor(limit.percent);
|
|
425
|
-
const reset = resetText(limit.resetsAt, now);
|
|
426
|
-
return ui.line(
|
|
427
|
-
` ${limit.label.padEnd(6)}`,
|
|
428
|
-
ui.text(filled, { color }),
|
|
429
|
-
ui.dim(empty),
|
|
430
|
-
ui.text(` ${String(limit.percent).padStart(3)}%`, { color }),
|
|
431
|
-
reset ? ui.dim(` resets ${reset}`) : null,
|
|
432
|
-
);
|
|
433
|
-
}),
|
|
434
|
-
ui.text(' '),
|
|
435
|
-
];
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// The estimate's billing product is context, never evidence of an invoice or measured quota use.
|
|
439
|
-
function billingWords(cost) {
|
|
440
|
-
if (cost.billing === 'plan') return `${API_ESTIMATE} · plan quota, not a charge`;
|
|
441
|
-
if (cost.billing === 'mixed') return `${API_ESTIMATE} · credits ${costText(cost.creditsUsd, cost.billing)}`;
|
|
442
|
-
return `${API_ESTIMATE} · configured rates, not an invoice`;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// SESSION: one total with the subagents, the difference against no routing, the main thread and the subagents as bars
|
|
446
|
-
// on one scale, then the replies by tier.
|
|
447
|
-
function sessionLines(ui, config, view, cost, actions, columns, now) {
|
|
448
|
-
const since = liveUsage(view, view.usageSession).since;
|
|
449
|
-
const running = view.subagentsRunning ?? 0;
|
|
450
|
-
const switches = sessionUsage(view, now).switches;
|
|
451
|
-
const switchCount = switches.tier + switches.activity;
|
|
452
|
-
const head = [
|
|
453
|
-
Number.isFinite(since) ? `since ${sinceText(since, now)}` : null,
|
|
454
|
-
cost.replies ? plural(cost.replies, 'reply', 'replies') : null,
|
|
455
|
-
cost.agents ? plural(cost.agents, 'agent') : null,
|
|
456
|
-
switchCount ? plural(switchCount, 'switch', 'switches') : null,
|
|
457
|
-
].filter(Boolean);
|
|
458
|
-
const out = [
|
|
459
|
-
ui.line(
|
|
460
|
-
ui.head('SESSION'),
|
|
461
|
-
head.length ? ui.dim(` ${head.join(' · ')}`) : null,
|
|
462
|
-
running ? ui.text(` ● ${running} running`, { color: 'yellow' }) : null,
|
|
463
|
-
),
|
|
464
|
-
];
|
|
465
|
-
// Replies without a list price reach the strip but not the usage record: the tiers still show, the cost cannot.
|
|
466
|
-
if (!cost.replies && !cost.agents)
|
|
467
|
-
return [
|
|
468
|
-
...out,
|
|
469
|
-
ui.dim(view.strip?.cells?.length ? ' no priced replies: cost unknown' : ' no replies yet'),
|
|
470
|
-
...stripLines(ui, view, columns, since, now),
|
|
471
|
-
ui.text(' '),
|
|
472
|
-
];
|
|
473
|
-
out.push(
|
|
474
|
-
ui.line(
|
|
475
|
-
' ',
|
|
476
|
-
ui.text(costText(cost.totalUsd, cost.billing), { bold: true }),
|
|
477
|
-
ui.dim(` ${billingWords(cost)} `),
|
|
478
|
-
ui.Button({ key: 'usage-session', label: 'Usage', onPress: () => actions.usageFor('session') }),
|
|
479
|
-
),
|
|
480
|
-
);
|
|
481
|
-
if (cost.diffUsd !== null) {
|
|
482
|
-
const diff = diffText(cost.diffUsd, cost.baseUsd);
|
|
483
|
-
const versus = ` vs ${baselineName(view, sessionUsage(view, now), cost)}`;
|
|
484
|
-
const of = ` ${costText(cost.baseUsd, cost.billing)}`;
|
|
485
|
-
const fits = !Number.isFinite(columns) || 2 + diff.length + versus.length + of.length <= columns;
|
|
486
|
-
out.push(
|
|
487
|
-
ui.line(' ', ui.text(diff, { bold: true, color: savingsColor(cost.diffUsd) }), versus, fits ? ui.dim(of) : null),
|
|
488
|
-
);
|
|
489
|
-
if (cost.early) out.push(ui.dim(` ${repliesText(cost)}: ${EARLY_NOTE}`));
|
|
490
|
-
} else if (cost.effortOnly) out.push(ui.dim(' effort routing on one model: no saving is estimated'));
|
|
491
|
-
else if (cost.uncomparedReplies) out.push(ui.dim(` ${uncomparedText(cost)}: no saving shown`));
|
|
492
|
-
out.push(...costBars(ui, config, view, cost, now));
|
|
493
|
-
const r = cost.readout;
|
|
494
|
-
if (cost.comparable && !r.early && switchesLost(r)) out.push(lostLine(ui, actions));
|
|
495
|
-
out.push(...stripLines(ui, view, columns, since, now), ui.text(' '));
|
|
496
|
-
return out;
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
// The main thread and the subagents as bars on one scale, each with its difference against no routing; an effort-only
|
|
500
|
-
// profile shows its effort mix.
|
|
501
|
-
function costBars(ui, config, view, cost, now) {
|
|
502
|
-
const out = [];
|
|
503
|
-
const scale = Math.max(cost.mainUsd, cost.agentUsd);
|
|
504
|
-
const bar = (value, color) => {
|
|
505
|
-
const filled = scale > 0 ? Math.max(value > 0 ? 1 : 0, Math.round((value / scale) * SESSION_BAR)) : 0;
|
|
506
|
-
return [ui.text('█'.repeat(filled), { color }), ui.dim('░'.repeat(SESSION_BAR - filled))];
|
|
507
|
-
};
|
|
508
|
-
const amount = (value) => costText(value, cost.billing).padStart(7);
|
|
509
|
-
const against = (diff, words) =>
|
|
510
|
-
diff === null || cost.diffUsd === null
|
|
511
|
-
? null
|
|
512
|
-
: ui.line(
|
|
513
|
-
' ',
|
|
514
|
-
ui.text(signedUsd(diff), savingsColor(diff) ? { color: savingsColor(diff) } : {}),
|
|
515
|
-
ui.dim(` vs ${words}`),
|
|
516
|
-
);
|
|
517
|
-
out.push(
|
|
518
|
-
ui.line(
|
|
519
|
-
' main ',
|
|
520
|
-
amount(cost.mainUsd),
|
|
521
|
-
' ',
|
|
522
|
-
...bar(cost.mainUsd, 'cyan'),
|
|
523
|
-
against(cost.replies ? cost.mainDiff : null, comparedModel(config, view, sessionUsage(view, now))),
|
|
524
|
-
),
|
|
525
|
-
);
|
|
526
|
-
if (cost.agents)
|
|
527
|
-
out.push(
|
|
528
|
-
ui.line(
|
|
529
|
-
' agents',
|
|
530
|
-
amount(cost.agentUsd),
|
|
531
|
-
' ',
|
|
532
|
-
...bar(cost.agentUsd, 'cyan'),
|
|
533
|
-
against(cost.agentDiff, 'their picks'),
|
|
534
|
-
),
|
|
535
|
-
);
|
|
536
|
-
if (cost.effortOnly && cost.effortMix.length)
|
|
537
|
-
out.push(
|
|
538
|
-
ui.line(
|
|
539
|
-
ui.dim(' effort'),
|
|
540
|
-
...cost.effortMix.flatMap(([effort, n]) => [ui.dim(` ${effort} `), ui.text(String(n), { bold: true })]),
|
|
541
|
-
),
|
|
542
|
-
);
|
|
543
|
-
return out;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
const SESSION_BAR = 16;
|
|
547
|
-
|
|
548
|
-
// The session's replies as a strip coloured by tier, then the replies per tier, low to high, as their bars.
|
|
549
|
-
function stripLines(ui, view, columns, since, now) {
|
|
550
|
-
if (!view.strip?.cells?.length) return [];
|
|
551
|
-
const strip = fittedStrip(view.strip, Number.isFinite(columns) ? columns - 8 : columns, since, now);
|
|
552
|
-
const counts = {};
|
|
553
|
-
for (const cell of view.strip.cells)
|
|
554
|
-
for (const [key, n] of Object.entries(cell)) counts[key] = (counts[key] ?? 0) + n;
|
|
555
|
-
return [
|
|
556
|
-
ui.line(
|
|
557
|
-
ui.dim(' tiers '),
|
|
558
|
-
...strip.cells.map((cell) => {
|
|
559
|
-
const tier = cellTier(cell);
|
|
560
|
-
return tier === 'none' ? ui.dim('·') : ui.tier(tier, '█');
|
|
561
|
-
}),
|
|
562
|
-
strip.note ? ui.dim(` ${strip.note}`) : null,
|
|
563
|
-
),
|
|
564
|
-
ui.line(
|
|
565
|
-
' ',
|
|
566
|
-
...TIERS.filter((tier) => counts[tier]).flatMap((tier) => [ui.bars(tier), ui.dim(` ${counts[tier]} `)]),
|
|
567
|
-
counts.none ? ui.dim(`· not routed ${counts.none}`) : null,
|
|
568
|
-
),
|
|
569
|
-
];
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
// CONTEXT: the window's fill on the model that runs next, and the cache reuse of the last reply and of the session.
|
|
573
|
-
function contextLines(ui, config, view, usage, actions, now) {
|
|
574
|
-
const metrics = usageMetrics(config, view, usage);
|
|
575
|
-
const session = sessionUsage(view, now).observed;
|
|
576
|
-
const fill = metrics.contextBar.percent;
|
|
577
|
-
const [filled, empty] = gaugeCells((fill ?? 0) / 100, 20);
|
|
578
|
-
return [
|
|
579
|
-
ui.head('CONTEXT'),
|
|
580
|
-
fill === null
|
|
581
|
-
? ui.line(' ctx ', ui.dim('no reading yet'))
|
|
582
|
-
: ui.line(
|
|
583
|
-
' ctx ',
|
|
584
|
-
ui.text(filled, { color: metrics.contextBar.color }),
|
|
585
|
-
ui.dim(empty),
|
|
586
|
-
` ${String(fill).padStart(3)}%`,
|
|
587
|
-
ui.dim(` ${contextSuffix(metrics)}`),
|
|
588
|
-
),
|
|
589
|
-
ui.line(
|
|
590
|
-
' cache ',
|
|
591
|
-
metrics.cacheBar.percent === null
|
|
592
|
-
? ui.dim('no reply yet')
|
|
593
|
-
: ui.line(ui.text(`${metrics.cacheBar.percent}%`, { color: metrics.cacheBar.color }), ui.dim(' last reply')),
|
|
594
|
-
session.inputTokens
|
|
595
|
-
? ui.line(ui.dim(' · '), percent(session.cacheReadTokens / session.inputTokens), ui.dim(' session'))
|
|
596
|
-
: null,
|
|
597
|
-
' ',
|
|
598
|
-
detailButton(ui, view, actions),
|
|
599
|
-
),
|
|
600
|
-
...(view.usageDetail ? readingDetail(ui, view, metrics) : []),
|
|
601
|
-
];
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
const detailButton = (ui, view, actions) =>
|
|
605
|
-
ui.Button({
|
|
606
|
-
key: 'usage-detail',
|
|
607
|
-
label: view.usageDetail ? 'less' : 'details',
|
|
608
|
-
variant: view.usageDetail ? 'primary' : 'secondary',
|
|
609
|
-
onPress: actions.usageDetail,
|
|
610
|
-
});
|
|
611
|
-
|
|
612
|
-
// Behind `details` on the Now tab: Claude's readings of the last reply, the input per reply, and the estimates the
|
|
613
|
-
// last decision weighed.
|
|
614
|
-
function readingDetail(ui, view, metrics) {
|
|
615
|
-
const history = view.history ?? [];
|
|
616
|
-
const tiers = view.tiers ?? [];
|
|
617
|
-
const glyphs = sparkline(history);
|
|
618
|
-
const trend = history.length
|
|
619
|
-
? ui.line(
|
|
620
|
-
' ',
|
|
621
|
-
...[...glyphs].map((glyph, i) => {
|
|
622
|
-
const tier = tiers[tiers.length - history.length + i];
|
|
623
|
-
return tier ? ui.tier(tier, glyph) : ui.text(glyph);
|
|
624
|
-
}),
|
|
625
|
-
ui.dim(` ${formatTokens(Math.min(...history))} … ${formatTokens(Math.max(...history))}`),
|
|
626
|
-
)
|
|
627
|
-
: ui.dim(' no replies yet');
|
|
628
|
-
return [
|
|
629
|
-
ui.dim(` context observed ${formatTokens(metrics.observed)} · estimated ${formatTokens(view.contextTokens)}`),
|
|
630
|
-
ui.dim(` cache read ${formatTokens(view.cacheRead)} · written ${formatTokens(view.cacheWrite)} tokens`),
|
|
631
|
-
ui.dim(` output ${formatTokens(view.outputTokens)} tokens on the last reply`),
|
|
632
|
-
ui.text(' '),
|
|
633
|
-
ui.head(`INPUT PER REPLY · last ${plural(history.length, 'reply', 'replies')} · lowest to highest`),
|
|
634
|
-
trend,
|
|
635
|
-
ui.text(' '),
|
|
636
|
-
ui.head('ESTIMATES · configured API rates'),
|
|
637
|
-
ui.text(`Cache hits saved ${dollars(metrics.cacheBenefit)}`),
|
|
638
|
-
Number.isFinite(view.estimate?.taxUsd) ? ui.text(`Switch cost ${dollars(view.estimate.taxUsd)}`) : null,
|
|
639
|
-
...(view.comparison
|
|
640
|
-
? [
|
|
641
|
-
ui.text(`Compared tiers ${view.comparison.incumbent} → ${view.comparison.candidate}`),
|
|
642
|
-
ui.text(`Next-turn difference ${signedUsd(view.comparison.minUsd)} to ${signedUsd(view.comparison.maxUsd)}`),
|
|
643
|
-
ui.text(
|
|
644
|
-
`Payback ${view.comparison.paybackTurns === null ? 'none projected' : `${view.comparison.paybackTurns} later turns`}`,
|
|
645
|
-
),
|
|
646
|
-
]
|
|
647
|
-
: []),
|
|
648
|
-
];
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
// "13:41", or "Oct 12 13:41" for another day than `now`'s.
|
|
652
|
-
const clockText = (ms) => new Date(ms).toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit' });
|
|
653
|
-
const sinceText = (ms, now) =>
|
|
654
|
-
dayKey(ms) === dayKey(now) ? clockText(ms) : `${dayLabel(dayKey(ms))} ${clockText(ms)}`;
|
|
655
|
-
|
|
656
|
-
// The strip cell's tier: the one with most replies, the stronger on a tie; `none` where routing served none.
|
|
657
|
-
const STRIP_ORDER = [...LADDER, 'none'];
|
|
658
|
-
const cellTier = (cell) => STRIP_ORDER.reduce((top, key) => ((cell[key] ?? 0) > (cell[top] ?? 0) ? key : top));
|
|
659
|
-
|
|
660
|
-
// The session strip fitted to the pane beside its note: pairs of cells merge until both fit. The note says how many
|
|
661
|
-
// replies a cell holds, and since when the strip runs when Claude Code restarted after the session's first reply.
|
|
662
|
-
function fittedStrip(strip, columns, since, now) {
|
|
663
|
-
let { per, cells } = strip;
|
|
664
|
-
const restarted = Number.isFinite(strip.from) && Number.isFinite(since) && strip.from > since;
|
|
665
|
-
const note = () =>
|
|
666
|
-
[per > 1 ? `${per} replies per cell` : null, restarted ? `since ${sinceText(strip.from, now)}` : null]
|
|
667
|
-
.filter(Boolean)
|
|
668
|
-
.join(' · ');
|
|
669
|
-
const room = () => (Number.isFinite(columns) ? Math.max(8, columns - 4 - note().length) : STRIP_CELLS);
|
|
670
|
-
while (cells.length > room()) {
|
|
671
|
-
cells = mergePairs(cells);
|
|
672
|
-
per *= 2;
|
|
673
|
-
}
|
|
674
|
-
return { cells, note: note() };
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
// Cache rebuilds cost more than the cheaper models saved: the policy's switch weight is the lever.
|
|
678
|
-
const lostLine = (ui, actions) =>
|
|
679
|
-
ui.line(
|
|
680
|
-
ui.text(' ▲ switches cost more than they saved ', { color: 'yellow' }),
|
|
681
|
-
ui.Button({ key: 'session-policy', label: 'Policy', onPress: () => actions.tab('policy') }),
|
|
682
|
-
);
|
|
683
|
-
|
|
684
|
-
// Rows: [key, label, format, router.json field]. Typed as tuples so tsc keeps each position's type.
|
|
685
|
-
/** @type {[string, string, (value: number) => string, string][]} */
|
|
686
|
-
const POLICY_ROWS = [
|
|
687
|
-
['downgradeVotes', 'Votes to go down', String, 'policy.downgradeVotes'],
|
|
688
|
-
['horizon', 'Payback horizon', (v) => plural(v, 'turn'), 'policy.downgradeHorizonTurns'],
|
|
689
|
-
['cashCapUsd', 'Credits cap', (v) => `$${v.toFixed(2)}`, 'policy.cashCapUsd'],
|
|
690
|
-
['activityMass', 'Activity threshold', percent, 'policy.activityMass'],
|
|
47
|
+
NOW_TAB,
|
|
48
|
+
tabOf('routes', '2', 'Routes', routesOf, terminalRoutes, graphicalRoutes),
|
|
49
|
+
tabOf('policy', '3', 'Policy', policyOf, terminalPolicy, graphicalPolicy),
|
|
50
|
+
tabOf('classifier', '4', 'Classifier', classifierOf, terminalClassifier, graphicalClassifier),
|
|
51
|
+
tabOf('usage', '5', 'Usage', usageOf, terminalUsage, graphicalUsage),
|
|
691
52
|
];
|
|
692
|
-
const POLICY_FORMAT = Object.fromEntries(POLICY_ROWS.map(([key, , format]) => [key, format]));
|
|
693
|
-
|
|
694
|
-
// What Routes and Policy changed and have not saved: tiers, baseline, activity cells and mode against the saved config,
|
|
695
|
-
// policy values against the values the draft started from. `cells` lists [activity, tier] pairs.
|
|
696
|
-
export function routingChanges(config, view) {
|
|
697
|
-
const raw = routeDraftOf(config, view);
|
|
698
|
-
const draft = { ...effectiveRoutes(raw, config), ...effectiveActivities(raw, config) };
|
|
699
|
-
const saved = tuningOf(config);
|
|
700
|
-
const base = view.tuningBase ?? saved;
|
|
701
|
-
const tuning = { ...saved, ...view.tuning };
|
|
702
|
-
const tiers = TIERS.filter((tier) => !sameRoute(draft.routes[tier], config.routes[tier]));
|
|
703
|
-
const baseline = draft.baselineTier !== config.baselineTier;
|
|
704
|
-
const cells = ACTIVITIES.flatMap((activity) =>
|
|
705
|
-
TIERS.filter((tier) => !sameCell(draft.activities[activity]?.[tier], config.activities[activity]?.[tier])).map(
|
|
706
|
-
(tier) => [activity, tier],
|
|
707
|
-
),
|
|
708
|
-
);
|
|
709
|
-
const mode = draft.activityRouting !== config.activityRouting;
|
|
710
|
-
const policy = POLICY_ROWS.map(([key]) => key).filter(
|
|
711
|
-
(key) => Object.hasOwn(view.tuning ?? {}, key) && view.tuning[key] !== base[key],
|
|
712
|
-
);
|
|
713
|
-
const count = tiers.length + Number(baseline) + cells.length + Number(mode) + policy.length;
|
|
714
|
-
return { draft, saved, tuning, tiers, baseline, cells, mode, policy, count };
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
const marker = (ui, changed) => ui.cell(2, changed ? ui.text('●', { color: 'yellow' }) : '');
|
|
718
|
-
|
|
719
|
-
// "sonnet·high": the route's models key and its effort, short enough for the grid; `inherit` for a route that leaves
|
|
720
|
-
// the effort to the session, and no effort for a model without efforts. The key, not the model name: two Haiku versions
|
|
721
|
-
// in router.json still read apart.
|
|
722
|
-
function gridRoute(config, route, narrow) {
|
|
723
|
-
if (!config.models[route.model].efforts.length) return route.model;
|
|
724
|
-
const effort = route.effort ?? INHERIT;
|
|
725
|
-
return `${route.model}·${narrow ? (EFFORT_SHORT[effort] ?? effort) : effort}`;
|
|
726
|
-
}
|
|
727
|
-
const routeKey = (route) => `${route.model}@${route.effort ?? ''}`;
|
|
728
|
-
const builtIn = (config, activity, tier) => defaultsFor(config.provider).activities[activity]?.[tier] ?? null;
|
|
729
|
-
const sharesEffort = (config, route) => effortSharesCache(config, config.models[route.model]?.id);
|
|
730
|
-
// The cache a route runs on: one per model on a model that keeps one cache across efforts, else one per route.
|
|
731
|
-
const cacheOf = (config, route) => (sharesEffort(config, route) ? route.model : routeKey(route));
|
|
732
|
-
|
|
733
|
-
// Why an override may be a mistake: no effect, a route above the next tier's, or a cache no other cell runs on.
|
|
734
|
-
function overrideWarnings(config, uses, activity, tier) {
|
|
735
|
-
const route = resolveRoute(config, tier, activity);
|
|
736
|
-
if (sameRoute(route, config.routes[tier])) return [];
|
|
737
|
-
const above = config.routes[TIERS[TIERS.indexOf(tier) + 1]];
|
|
738
|
-
return [
|
|
739
|
-
above && compareRoutes(config, route, above) > 0 ? 'stronger than the tier above' : null,
|
|
740
|
-
uses.get(cacheOf(config, route)) === 1
|
|
741
|
-
? config.cache.mode === 'automatic'
|
|
742
|
-
? 'a route only this cell uses; cache sharing is unknown'
|
|
743
|
-
: `${sharesEffort(config, route) ? 'a model' : 'a model + effort'} only this cell uses: one more cache`
|
|
744
|
-
: null,
|
|
745
|
-
].filter(Boolean);
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
// A model Select and an effort Select for one route; `session` is offered as `inherit`.
|
|
749
|
-
function routeSelects(ui, config, key, route, modelOptions, onModel, onEffort) {
|
|
750
|
-
const model = config.models[route.model];
|
|
751
|
-
const aliases = modelOptions.includes(route.model) ? modelOptions : [route.model, ...modelOptions];
|
|
752
|
-
return [
|
|
753
|
-
ui.cell(
|
|
754
|
-
14,
|
|
755
|
-
ui.Select
|
|
756
|
-
? ui.Select({
|
|
757
|
-
key: `${key}-model`,
|
|
758
|
-
value: route.model,
|
|
759
|
-
options: aliases.map((alias) => ({ value: alias, label: modelName(config.models[alias].id) })),
|
|
760
|
-
onSelect: onModel,
|
|
761
|
-
})
|
|
762
|
-
: modelName(model.id),
|
|
763
|
-
),
|
|
764
|
-
ui.cell(
|
|
765
|
-
12,
|
|
766
|
-
!model.efforts.length
|
|
767
|
-
? ui.dim('none')
|
|
768
|
-
: ui.Select
|
|
769
|
-
? ui.Select({
|
|
770
|
-
key: `${key}-effort`,
|
|
771
|
-
value: route.effort ?? SESSION_EFFORT,
|
|
772
|
-
options: [
|
|
773
|
-
{ value: SESSION_EFFORT, label: INHERIT },
|
|
774
|
-
...model.efforts.map((effort) => ({ value: effort, label: effort })),
|
|
775
|
-
],
|
|
776
|
-
onSelect: (effort) => onEffort(effort === SESSION_EFFORT ? null : effort),
|
|
777
|
-
})
|
|
778
|
-
: (route.effort ?? INHERIT),
|
|
779
|
-
),
|
|
780
|
-
];
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
const GRID_LABEL = 13;
|
|
784
|
-
|
|
785
|
-
function fitRouteCell(label, width) {
|
|
786
|
-
if (label.length <= width) return label;
|
|
787
|
-
const mark = /[°●]+$/.exec(label)?.[0] ?? '';
|
|
788
|
-
if (mark.length >= width) return mark.slice(-width);
|
|
789
|
-
return `${label.slice(0, Math.max(0, width - 1 - mark.length))}…${mark}`;
|
|
790
|
-
}
|
|
791
53
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
const shared = [];
|
|
798
|
-
const count = (route) => {
|
|
799
|
-
shared.push(sharesEffort(draft, route));
|
|
800
|
-
uses.set(cacheOf(draft, route), (uses.get(cacheOf(draft, route)) ?? 0) + 1);
|
|
801
|
-
};
|
|
802
|
-
for (const tier of TIERS) count(draft.routes[tier]);
|
|
803
|
-
if (overrides)
|
|
804
|
-
for (const activity of ACTIVITIES)
|
|
805
|
-
for (const tier of TIERS) {
|
|
806
|
-
const route = resolveRoute(draft, tier, activity);
|
|
807
|
-
if (!sameRoute(route, draft.routes[tier])) count(route);
|
|
808
|
-
}
|
|
809
|
-
return { uses, shared };
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
// The Routes tab: one grid of tier (columns) by activity (rows), the tiers' own routes on top. A cell press opens its
|
|
813
|
-
// editor under the grid. Nothing is written until Save; the cache count reads the whole draft.
|
|
814
|
-
function routesTab(ui, config, view, _usage, actions, modelOptions, columns) {
|
|
815
|
-
const changes = routingChanges(config, view);
|
|
816
|
-
const draft = { ...config, ...changes.draft };
|
|
817
|
-
const mode = draft.activityRouting;
|
|
818
|
-
const narrow = Number.isFinite(columns) && columns < 80;
|
|
819
|
-
const width = Number.isFinite(columns)
|
|
820
|
-
? Math.max(1, Math.min(narrow ? 14 : 16, Math.floor((columns - GRID_LABEL) / TIERS.length)))
|
|
821
|
-
: 16;
|
|
822
|
-
const { uses, shared } = cacheUses(draft, mode === 'on');
|
|
823
|
-
const selected = view.routeCell ?? null;
|
|
824
|
-
const cellButton = (row, tier, text, dim) =>
|
|
825
|
-
ui.cell(
|
|
826
|
-
width,
|
|
827
|
-
ui.Button({
|
|
828
|
-
key: `cell-${row}-${tier}`,
|
|
829
|
-
label: fitRouteCell(`${selected === `${row}.${tier}` ? '▸' : ' '}${text}`, width),
|
|
830
|
-
plain: true,
|
|
831
|
-
...(dim ? { dimColor: true } : {}),
|
|
832
|
-
onPress: () => actions.selectCell(`${row}.${tier}`),
|
|
833
|
-
}),
|
|
834
|
-
);
|
|
835
|
-
const gridRow = (name, cells) => ui.line(ui.cell(GRID_LABEL, name), ...cells);
|
|
836
|
-
const out = [
|
|
837
|
-
ui.line(ui.head('ROUTES'), ui.dim(' · which model runs, by tier and activity · edit, then Save')),
|
|
838
|
-
ui.line(
|
|
839
|
-
ui.cell(
|
|
840
|
-
26,
|
|
841
|
-
ui.Select
|
|
842
|
-
? ui.Select({
|
|
843
|
-
key: 'activity-mode',
|
|
844
|
-
label: 'Activity routing',
|
|
845
|
-
value: mode,
|
|
846
|
-
options: ACTIVITY_MODES.map((m) => ({ value: m, label: m })),
|
|
847
|
-
onSelect: (m) => actions.activityMode(m),
|
|
848
|
-
})
|
|
849
|
-
: `Activity routing: ${mode}`,
|
|
850
|
-
),
|
|
851
|
-
marker(ui, changes.mode),
|
|
852
|
-
ui.dim(ACTIVITY_MODE_HINT[mode]),
|
|
853
|
-
),
|
|
854
|
-
...overridesState(ui, view, config.activityRouting, mode),
|
|
855
|
-
ui.text(' '),
|
|
856
|
-
ui.line(
|
|
857
|
-
ui.cell(GRID_LABEL, ''),
|
|
858
|
-
...TIERS.map((tier) => ui.cell(width, ui.tier(tier, fitRouteCell(` ${tier}`, width)))),
|
|
859
|
-
),
|
|
860
|
-
gridRow(
|
|
861
|
-
ui.text('every turn', { bold: true }),
|
|
862
|
-
TIERS.map((tier) =>
|
|
863
|
-
cellButton(
|
|
864
|
-
TIER_ROW,
|
|
865
|
-
tier,
|
|
866
|
-
`${gridRoute(draft, draft.routes[tier], narrow)}${changes.tiers.includes(tier) ? '●' : ''}`,
|
|
867
|
-
false,
|
|
868
|
-
),
|
|
869
|
-
),
|
|
870
|
-
),
|
|
871
|
-
];
|
|
872
|
-
for (const activity of ACTIVITIES)
|
|
873
|
-
out.push(
|
|
874
|
-
gridRow(
|
|
875
|
-
ui.activity(activity),
|
|
876
|
-
TIERS.map((tier) => {
|
|
877
|
-
const route = resolveRoute(draft, tier, activity);
|
|
878
|
-
const edited = changes.cells.some(([a, t]) => a === activity && t === tier) ? '●' : '';
|
|
879
|
-
if (sameRoute(route, draft.routes[tier])) return cellButton(activity, tier, `·${edited}`, true);
|
|
880
|
-
const preset = builtIn(draft, activity, tier);
|
|
881
|
-
const mark = preset && sameRoute(route, { model: preset.model, effort: preset.effort ?? null }) ? '°' : '';
|
|
882
|
-
return cellButton(activity, tier, `${gridRoute(draft, route, narrow)}${mark}${edited}`, mode !== 'on');
|
|
883
|
-
}),
|
|
884
|
-
),
|
|
885
|
-
);
|
|
886
|
-
out.push(
|
|
887
|
-
ui.dim(' · runs the tier’s model ° built-in default ● unsaved'),
|
|
888
|
-
ui.text(' '),
|
|
889
|
-
...cellEditor(ui, draft, changes, selected, cacheUses(draft, true).uses, actions, modelOptions),
|
|
890
|
-
ui.text(' '),
|
|
891
|
-
...(config.cache.mode === 'automatic'
|
|
892
|
-
? [
|
|
893
|
-
ui.dim(
|
|
894
|
-
'Automatic prefix cache: hits are observed; lifetime and sharing across efforts are unknown. No cache-write premium.',
|
|
895
|
-
),
|
|
896
|
-
]
|
|
897
|
-
: config.cache.mode === 'disabled'
|
|
898
|
-
? [ui.dim('Prompt caching disabled: input is priced at the uncached rate.')]
|
|
899
|
-
: cacheLines(ui, uses.size, shared, mode, columns)),
|
|
900
|
-
...LADDER.slice(0, -1)
|
|
901
|
-
.filter((upper, i) => sameRoute(draft.routes[upper], draft.routes[LADDER[i + 1]]))
|
|
902
|
-
.map((upper) =>
|
|
903
|
-
ui.text(
|
|
904
|
-
`! ${LADDER[LADDER.indexOf(upper) + 1]} and ${upper} run the same model and effort: that step changes nothing`,
|
|
905
|
-
{ color: 'yellow' },
|
|
906
|
-
),
|
|
907
|
-
),
|
|
908
|
-
ui.Button({ key: 'reset-routes', label: 'Reset routes to defaults', hotkey: 'r', onPress: actions.resetRoutes }),
|
|
909
|
-
ui.dim('A new model ID needs the active file → models: price, window, efforts.'),
|
|
54
|
+
export function renderPanel(elements, config, view, usage, actions, opts = {}) {
|
|
55
|
+
return orTerminal(
|
|
56
|
+
opts.surface,
|
|
57
|
+
(surface) => drawPanel(elements, config, view, usage, actions, { ...opts, surface }),
|
|
58
|
+
opts.onFallback,
|
|
910
59
|
);
|
|
911
|
-
return out;
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
// Why the cache count matters, in one or two lines: a model keeps one cache across efforts (the 5.5 models on an API key
|
|
915
|
-
// or a subscription) or one per effort (other providers).
|
|
916
|
-
function cacheLines(ui, n, shared, mode, columns) {
|
|
917
|
-
const scope = mode === 'on' ? '' : ' (tiers only: activity routing is not on)';
|
|
918
|
-
const per = shared.every(Boolean)
|
|
919
|
-
? 'one per model; effort changes keep it'
|
|
920
|
-
: shared.some(Boolean)
|
|
921
|
-
? 'one per model, or per model + effort where efforts cache apart'
|
|
922
|
-
: 'one per model + effort';
|
|
923
|
-
return dimLines(
|
|
924
|
-
ui,
|
|
925
|
-
`${plural(n, 'prompt cache')} in use${scope}: ${per}. A switch to a cold cache writes the whole context again, the switch cost on Now.`,
|
|
926
|
-
Math.min(columns ?? 78, 78),
|
|
927
|
-
);
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
// The editor of the selected grid cell: a tier's own route, or an activity's cell with what it runs, its built-in
|
|
931
|
-
// default, and the two ways back.
|
|
932
|
-
function cellEditor(ui, draft, changes, selected, uses, actions, modelOptions) {
|
|
933
|
-
const [row, tier] = selected?.split('.') ?? [];
|
|
934
|
-
if (!TIERS.includes(tier) || (row !== TIER_ROW && !ACTIVITIES.includes(row)))
|
|
935
|
-
return [ui.dim('Select a cell to change its model and effort.')];
|
|
936
|
-
if (row === TIER_ROW) {
|
|
937
|
-
const route = draft.routes[tier];
|
|
938
|
-
const model = draft.models[route.model];
|
|
939
|
-
return [
|
|
940
|
-
ui.line(
|
|
941
|
-
ui.cell(22, ui.line(ui.tier(tier), ui.dim(' · every turn'))),
|
|
942
|
-
...routeSelects(
|
|
943
|
-
ui,
|
|
944
|
-
draft,
|
|
945
|
-
`route-${tier}`,
|
|
946
|
-
route,
|
|
947
|
-
modelOptions,
|
|
948
|
-
(alias) => actions.routeModel(tier, alias),
|
|
949
|
-
(effort) => actions.routeEffort(tier, effort),
|
|
950
|
-
),
|
|
951
|
-
marker(ui, changes.tiers.includes(tier)),
|
|
952
|
-
),
|
|
953
|
-
ui.dim(
|
|
954
|
-
`${''.padEnd(22)}$${model.input} in · $${model.output ?? '?'} out per M tokens · ${windowSize(model.contextWindow)} window`,
|
|
955
|
-
),
|
|
956
|
-
];
|
|
957
|
-
}
|
|
958
|
-
const activity = row;
|
|
959
|
-
const route = resolveRoute(draft, tier, activity);
|
|
960
|
-
const base = draft.routes[tier];
|
|
961
|
-
const preset = builtIn(draft, activity, tier);
|
|
962
|
-
const presetRoute = preset ? { model: preset.model, effort: preset.effort ?? null } : null;
|
|
963
|
-
const edited = changes.cells.some(([a, t]) => a === activity && t === tier);
|
|
964
|
-
const own = !sameRoute(route, base);
|
|
965
|
-
// A saved cell that resolves to the tier's route and is no built-in one changes nothing; 1.6 wrote such cells.
|
|
966
|
-
const noEffect = !own && !presetRoute && Boolean(draft.activities[activity]?.[tier]);
|
|
967
|
-
const drop = own || noEffect;
|
|
968
|
-
const state = !own
|
|
969
|
-
? presetRoute && !sameRoute(route, presetRoute)
|
|
970
|
-
? 'runs the tier’s model; the built-in default is removed'
|
|
971
|
-
: noEffect
|
|
972
|
-
? 'same as the tier’s model: no effect'
|
|
973
|
-
: 'runs the tier’s model'
|
|
974
|
-
: presetRoute && sameRoute(route, presetRoute)
|
|
975
|
-
? 'built-in default'
|
|
976
|
-
: 'your override';
|
|
977
|
-
const warnings = overrideWarnings(draft, uses, activity, tier);
|
|
978
|
-
const back = Boolean(presetRoute) && !sameRoute(route, presetRoute);
|
|
979
|
-
return [
|
|
980
|
-
ui.line(
|
|
981
|
-
ui.cell(22, ui.line(ui.activity(activity), ' at ', ui.tier(tier))),
|
|
982
|
-
...routeSelects(
|
|
983
|
-
ui,
|
|
984
|
-
draft,
|
|
985
|
-
`activity-${activity}-${tier}`,
|
|
986
|
-
route,
|
|
987
|
-
modelOptions,
|
|
988
|
-
(alias) => actions.activityModel(activity, tier, alias),
|
|
989
|
-
(effort) => actions.activityEffort(activity, tier, effort),
|
|
990
|
-
),
|
|
991
|
-
marker(ui, edited),
|
|
992
|
-
),
|
|
993
|
-
ui.line(ui.cell(22, ''), ui.dim(state)),
|
|
994
|
-
...(drop || back
|
|
995
|
-
? [
|
|
996
|
-
ui.line(
|
|
997
|
-
ui.cell(22, ''),
|
|
998
|
-
drop
|
|
999
|
-
? ui.Button({
|
|
1000
|
-
key: `activity-tier-${activity}-${tier}`,
|
|
1001
|
-
label: `Use tier model: ${routeName(draft, base)}`,
|
|
1002
|
-
onPress: () => actions.removeActivity(activity, tier),
|
|
1003
|
-
})
|
|
1004
|
-
: null,
|
|
1005
|
-
drop && back ? ' ' : null,
|
|
1006
|
-
back
|
|
1007
|
-
? ui.Button({
|
|
1008
|
-
key: `activity-default-${activity}-${tier}`,
|
|
1009
|
-
label: `Restore default: ${routeName(draft, presetRoute)}`,
|
|
1010
|
-
onPress: () => actions.restoreActivity(activity, tier),
|
|
1011
|
-
})
|
|
1012
|
-
: null,
|
|
1013
|
-
),
|
|
1014
|
-
]
|
|
1015
|
-
: []),
|
|
1016
|
-
...warnings.map((warning) => ui.text(`${''.padEnd(22)}! ${warning}`, { color: 'yellow' })),
|
|
1017
|
-
];
|
|
1018
|
-
}
|
|
1019
|
-
|
|
1020
|
-
// Whether the overrides run now: the saved mode decides, since a draft runs nothing until Save. A different draft mode
|
|
1021
|
-
// says what Save will change. Nothing while they run.
|
|
1022
|
-
function overridesState(ui, view, saved, drafted) {
|
|
1023
|
-
const why = (mode) =>
|
|
1024
|
-
view.phase === 'unavailable'
|
|
1025
|
-
? 'routing is unavailable'
|
|
1026
|
-
: view.mode === 'manual'
|
|
1027
|
-
? 'routing is off'
|
|
1028
|
-
: mode === 'on'
|
|
1029
|
-
? null
|
|
1030
|
-
: `activity routing is ${mode}`;
|
|
1031
|
-
const now = why(saved);
|
|
1032
|
-
const after = drafted === saved || Boolean(why(drafted)) === Boolean(now) ? null : why(drafted) ? 'not' : 'in';
|
|
1033
|
-
if (!now && !after) return [];
|
|
1034
|
-
return [
|
|
1035
|
-
ui.line(
|
|
1036
|
-
now ? ui.text(`Activity overrides not in use: ${now}`, { color: 'yellow' }) : ui.dim('Activity overrides in use'),
|
|
1037
|
-
after ? ui.dim(` · ${after === 'not' ? 'not in use' : 'in use'} after Save`) : null,
|
|
1038
|
-
),
|
|
1039
|
-
];
|
|
1040
|
-
}
|
|
1041
|
-
|
|
1042
|
-
// A Select over TUNING_CHOICES[key], with a saved value outside them kept as the first option.
|
|
1043
|
-
function choice(ui, key, value, format, pick) {
|
|
1044
|
-
if (!ui.Select) return format(value);
|
|
1045
|
-
const values = TUNING_CHOICES[key].includes(value) ? TUNING_CHOICES[key] : [value, ...TUNING_CHOICES[key]];
|
|
1046
|
-
return ui.Select({
|
|
1047
|
-
key,
|
|
1048
|
-
value: String(value),
|
|
1049
|
-
options: values.map((v) => ({ value: String(v), label: format(v) })),
|
|
1050
|
-
onSelect: (picked) => (TUNING_CHOICES[key].includes(Number(picked)) ? pick(Number(picked)) : undefined),
|
|
1051
|
-
});
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
|
-
// The Policy tab: each control inside the sentence it completes, its default named when the value differs. One Save
|
|
1055
|
-
// writes it with the Routes draft.
|
|
1056
|
-
// What the profile's routing mode does, under its control on the Policy tab.
|
|
1057
|
-
const MODE_HINT = {
|
|
1058
|
-
cost: 'a cheaper model or one effort level less where a turn allows',
|
|
1059
|
-
balanced: 'each tier runs its route as configured',
|
|
1060
|
-
quality: 'one effort level more, or the tier above, where a turn allows',
|
|
1061
|
-
};
|
|
1062
|
-
|
|
1063
|
-
function policyTab(ui, config, view, _usage, actions) {
|
|
1064
|
-
const changes = routingChanges(config, view);
|
|
1065
|
-
const { draft, tuning, policy } = changes;
|
|
1066
|
-
const preset = defaultsFor(config.provider);
|
|
1067
|
-
const defaults = tuningOf(preset);
|
|
1068
|
-
const label = classifierLabel(config);
|
|
1069
|
-
const pad = ''.padEnd(13);
|
|
1070
|
-
const tune = (key) => choice(ui, key, tuning[key], POLICY_FORMAT[key], (value) => actions.tune(key, value));
|
|
1071
|
-
const note = (key) => [
|
|
1072
|
-
marker(ui, policy.includes(key)),
|
|
1073
|
-
tuning[key] === defaults[key] ? null : ui.dim(`default ${POLICY_FORMAT[key](defaults[key])}`),
|
|
1074
|
-
];
|
|
1075
|
-
const { upgradeBase, upgradeSlope } = config.policy;
|
|
1076
|
-
const credits = Object.values(config.models).some((model) => model.billing === 'credits');
|
|
1077
|
-
const active = routingModeOf(view);
|
|
1078
|
-
return [
|
|
1079
|
-
ui.line(ui.head('POLICY'), ui.dim(' · how readily the router changes models · edit, then Save')),
|
|
1080
|
-
ui.text(' '),
|
|
1081
|
-
ui.line(
|
|
1082
|
-
ui.cell(13, ui.text('Mode', { bold: true })),
|
|
1083
|
-
ui.Select
|
|
1084
|
-
? ui.Select({
|
|
1085
|
-
key: 'routing-mode',
|
|
1086
|
-
value: config.mode,
|
|
1087
|
-
options: ROUTING_MODES.map((mode) => ({ value: mode, label: mode })),
|
|
1088
|
-
onSelect: (mode) => actions.routingMode(mode),
|
|
1089
|
-
})
|
|
1090
|
-
: config.mode,
|
|
1091
|
-
ui.dim(' saved at once'),
|
|
1092
|
-
),
|
|
1093
|
-
ui.dim(`${pad}${MODE_HINT[config.mode]}`),
|
|
1094
|
-
active.source === 'session' || active.source === 'project'
|
|
1095
|
-
? ui.text(`${pad}this session runs ${routingModeText(view)}`, { color: 'yellow' })
|
|
1096
|
-
: null,
|
|
1097
|
-
ui.line(
|
|
1098
|
-
ui.cell(13, ui.text('Start', { bold: true })),
|
|
1099
|
-
'at ',
|
|
1100
|
-
ui.Select
|
|
1101
|
-
? ui.Select({
|
|
1102
|
-
key: 'baseline',
|
|
1103
|
-
value: draft.baselineTier,
|
|
1104
|
-
options: TIERS.map((tier) => ({ value: tier, label: tier })),
|
|
1105
|
-
onSelect: (tier) => actions.baseline(tier),
|
|
1106
|
-
})
|
|
1107
|
-
: draft.baselineTier,
|
|
1108
|
-
' ',
|
|
1109
|
-
marker(ui, changes.baseline),
|
|
1110
|
-
draft.baselineTier === preset.baselineTier ? null : ui.dim(`default ${preset.baselineTier}`),
|
|
1111
|
-
),
|
|
1112
|
-
ui.dim(`${pad}when the session’s model is no tier’s model`),
|
|
1113
|
-
ui.line(
|
|
1114
|
-
ui.cell(13, ui.text('Going down', { bold: true })),
|
|
1115
|
-
'after ',
|
|
1116
|
-
tune('downgradeVotes'),
|
|
1117
|
-
' agreeing turns ',
|
|
1118
|
-
...note('downgradeVotes'),
|
|
1119
|
-
),
|
|
1120
|
-
ui.line(pad, 'if a cache write pays back within ', tune('horizon'), ' ', ...note('horizon')),
|
|
1121
|
-
ui.line(
|
|
1122
|
-
ui.cell(13, ui.text('Going up', { bold: true })),
|
|
1123
|
-
ui.dim(
|
|
1124
|
-
`needs ${label} support of ${percent(upgradeBase)}, up to ${percent(upgradeBase + upgradeSlope)} as the switch costs more`,
|
|
1125
|
-
),
|
|
1126
|
-
),
|
|
1127
|
-
ui.line(
|
|
1128
|
-
ui.cell(13, ui.text('Activities', { bold: true })),
|
|
1129
|
-
'apply at ',
|
|
1130
|
-
tune('activityMass'),
|
|
1131
|
-
' certainty or more ',
|
|
1132
|
-
...note('activityMass'),
|
|
1133
|
-
),
|
|
1134
|
-
ui.dim(`${pad}else a new task runs its tier’s model; a continuation keeps it`),
|
|
1135
|
-
ui.line(
|
|
1136
|
-
ui.cell(13, ui.text('Credits', { bold: true })),
|
|
1137
|
-
'cap one cold cache write at ',
|
|
1138
|
-
tune('cashCapUsd'),
|
|
1139
|
-
' ',
|
|
1140
|
-
...note('cashCapUsd'),
|
|
1141
|
-
),
|
|
1142
|
-
ui.dim(
|
|
1143
|
-
credits
|
|
1144
|
-
? `${pad}on a model that bills credits`
|
|
1145
|
-
: `${pad}no model in router.json bills credits, so this has no effect`,
|
|
1146
|
-
),
|
|
1147
|
-
ui.text(' '),
|
|
1148
|
-
ui.Button({ key: 'reset-policy', label: 'Reset policy to defaults', onPress: actions.resetPolicy }),
|
|
1149
|
-
ui.text(' '),
|
|
1150
|
-
ui.line(
|
|
1151
|
-
ui.head('FILE'),
|
|
1152
|
-
` ${view.configPath ?? 'profile router.json'} `,
|
|
1153
|
-
view.configPath ? ui.Button({ key: 'copy-path', label: 'copy', onPress: actions.copyPath }) : null,
|
|
1154
|
-
),
|
|
1155
|
-
ui.dim(SUBAGENT_LINE[config.subagentRouting]),
|
|
1156
|
-
];
|
|
1157
60
|
}
|
|
1158
61
|
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
const active = activeClassifier(config);
|
|
1165
|
-
const health = view.health ?? { failures: 0, pausedUntil: 0 };
|
|
1166
|
-
const where = (entry) => (isLocal(entry.endpoint) ? 'this machine' : hostOf(entry.endpoint));
|
|
1167
|
-
const rows = Object.entries(config.classifiers).map(([id, entry]) => {
|
|
1168
|
-
const isActive = id === config.classifier;
|
|
1169
|
-
const missing = missingCredentials(config, view, id);
|
|
1170
|
-
const probe = probeOf(config, id);
|
|
1171
|
-
return ui.line(
|
|
1172
|
-
ui.cell(
|
|
1173
|
-
17,
|
|
1174
|
-
ui.Button({
|
|
1175
|
-
key: `classifier-${id}`,
|
|
1176
|
-
label: `${isActive ? '◉' : '○'} ${entry.label.padEnd(10)}`,
|
|
1177
|
-
variant: isActive ? 'primary' : 'secondary',
|
|
1178
|
-
onPress: () => actions.classifier(id),
|
|
1179
|
-
}),
|
|
1180
|
-
),
|
|
1181
|
-
ui.cell(21, isLocal(entry.endpoint) ? ui.text(where(entry), { color: 'green' }) : ui.dim(where(entry))),
|
|
1182
|
-
ui.cell(8, probe ? `${probe.correct}/${probe.probes}` : ui.dim('—')),
|
|
1183
|
-
ui.cell(10, probe ? `${probe.p95Ms} ms` : ui.dim('—')),
|
|
1184
|
-
missing
|
|
1185
|
-
? ui.text(`○ ${missingText(config, id, missing)} `, { color: 'yellow' })
|
|
1186
|
-
: ui.text('● ready', { color: 'green' }),
|
|
1187
|
-
missing ? ui.Button({ key: `key-${id}`, label: 'Set up', onPress: actions.key }) : null,
|
|
1188
|
-
);
|
|
1189
|
-
});
|
|
1190
|
-
const { latency } = readMetrics(view.activityMetrics);
|
|
1191
|
-
const live = latencyP95(Object.hasOwn(latency, config.classifier) ? latency[config.classifier] : null);
|
|
1192
|
-
const probe = probeOf(config, config.classifier);
|
|
1193
|
-
const p95 = live ? live.ms : probe?.p95Ms;
|
|
1194
|
-
const wait = live
|
|
1195
|
-
? `p95 ${live.over ? '>' : '≤'} ${live.ms} ms over ${plural(live.turns, 'turn')}`
|
|
1196
|
-
: probe
|
|
1197
|
-
? `p95 ${probe.p95Ms} ms in its probe`
|
|
1198
|
-
: 'no wait measured yet';
|
|
1199
|
-
const slow = Number.isFinite(p95) && (p95 > active.timeoutMs || live?.over);
|
|
1200
|
-
const paused = health.pausedUntil > Date.now();
|
|
1201
|
-
return [
|
|
1202
|
-
ui.line(ui.head('CLASSIFIER'), ui.dim(' · asked once per new turn · a pick saves at once')),
|
|
1203
|
-
ui.dim(`${''.padEnd(17)}${'prompt goes to'.padEnd(21)}${'probe'.padEnd(8)}${'p95'.padEnd(10)}status`),
|
|
1204
|
-
...rows,
|
|
1205
|
-
...dimLines(ui, ' probe: historical turn-activity questions, not goal/phase accuracy; p95 from that run', columns),
|
|
1206
|
-
ui.text(' '),
|
|
1207
|
-
ui.line(ui.head(active.label.toUpperCase()), ui.dim(' · active')),
|
|
1208
|
-
ui.line(
|
|
1209
|
-
ui.cell(12, 'Deadline'),
|
|
1210
|
-
ui.cell(
|
|
1211
|
-
12,
|
|
1212
|
-
choice(ui, 'timeoutMs', active.timeoutMs, (v) => `${v} ms`, actions.classifierTimeout),
|
|
1213
|
-
),
|
|
1214
|
-
slow ? ui.text(`${wait}: slower than the deadline`, { color: 'yellow' }) : ui.dim(wait),
|
|
1215
|
-
),
|
|
1216
|
-
ui.dim(`${''.padEnd(12)}past the deadline the turn keeps its model`),
|
|
1217
|
-
ui.line(ui.cell(12, 'Sends'), `prompt + ${config.context.recentTurns} recent turns → ${where(active)}`),
|
|
1218
|
-
ui.dim(' the bounded active goal and current step also go to the classifier'),
|
|
1219
|
-
ui.line(
|
|
1220
|
-
ui.cell(12, 'Health'),
|
|
1221
|
-
paused
|
|
1222
|
-
? ui.text(`paused until ${new Date(health.pausedUntil).toLocaleTimeString()}`, { color: 'yellow' })
|
|
1223
|
-
: 'not paused',
|
|
1224
|
-
` · ${plural(health.failures, 'recent failure')}`,
|
|
1225
|
-
Number.isFinite(view.adviceMs) ? ui.dim(` · last ${view.adviceMs} ms`) : null,
|
|
1226
|
-
),
|
|
1227
|
-
ui.line(
|
|
1228
|
-
ui.cell(12, 'Credentials'),
|
|
1229
|
-
ui.Button({ key: 'key', label: 'Edit', hotkey: 'k', onPress: actions.key }),
|
|
1230
|
-
ui.dim(' opens /plugin configure'),
|
|
1231
|
-
),
|
|
1232
|
-
...sessionAgreement(ui, view),
|
|
1233
|
-
...(storeSummary(readStore(view.activityStore)).labelled > 0
|
|
1234
|
-
? [ui.text(' '), ...storeUsage(ui, view.activityStore, view.activityMetrics)]
|
|
1235
|
-
: []),
|
|
1236
|
-
];
|
|
1237
|
-
}
|
|
1238
|
-
|
|
1239
|
-
// How often this session's tools fit the classifier's accepted activity; none before a labelled turn.
|
|
1240
|
-
function sessionAgreement(ui, view) {
|
|
1241
|
-
const { matched = 0, total = 0 } = view.activityStats?.agreement ?? {};
|
|
1242
|
-
if (!total) return [];
|
|
1243
|
-
return [
|
|
1244
|
-
ui.text(' '),
|
|
1245
|
-
ui.head('THIS SESSION'),
|
|
1246
|
-
ui.text(`Tool compatibility (not accuracy): ${matched} of ${total} turns (${percent(matched / total)})`),
|
|
1247
|
-
];
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
// Green for a write or its undo, red for a refused write, yellow for everything else that needs a look.
|
|
1251
|
-
const noticeColor = (notice) =>
|
|
1252
|
-
/^Not saved/.test(notice) ? 'red' : /^(Saved|Undid|Path copied)/.test(notice) ? 'green' : 'yellow';
|
|
1253
|
-
|
|
1254
|
-
// The one place for commit state on every tab: the unsaved Routes and Policy draft with Save and Discard (the
|
|
1255
|
-
// router.json lines it writes on those two tabs, a count elsewhere), the last notice, and Undo for the last write.
|
|
1256
|
-
function statusBar(ui, config, view, tab, actions) {
|
|
1257
|
-
const changes = routingChanges(config, view);
|
|
1258
|
-
const out = [];
|
|
1259
|
-
if (changes.count) {
|
|
1260
|
-
out.push(ui.text(' '));
|
|
1261
|
-
if (DRAFT_TABS.includes(tab)) {
|
|
1262
|
-
const diff = (key, before, after) => [
|
|
1263
|
-
ui.text(`- ${key.padEnd(30)} ${before}`, { color: 'red' }),
|
|
1264
|
-
ui.text(`+ ${key.padEnd(30)} ${after}`, { color: 'green' }),
|
|
1265
|
-
];
|
|
1266
|
-
out.push(ui.dim('router.json changes:'));
|
|
1267
|
-
for (const tier of changes.tiers)
|
|
1268
|
-
out.push(
|
|
1269
|
-
...diff(
|
|
1270
|
-
`routes.${tier}`,
|
|
1271
|
-
routeName(config, config.routes[tier]),
|
|
1272
|
-
routeName(config, changes.draft.routes[tier]),
|
|
1273
|
-
),
|
|
1274
|
-
);
|
|
1275
|
-
if (changes.baseline) out.push(...diff('baselineTier', config.baselineTier, changes.draft.baselineTier));
|
|
1276
|
-
const draft = { ...config, ...changes.draft };
|
|
1277
|
-
const cellName = (c, activity, tier) =>
|
|
1278
|
-
c.activities[activity]?.[tier] ? routeName(c, resolveRoute(c, tier, activity)) : 'no override';
|
|
1279
|
-
for (const [activity, tier] of changes.cells)
|
|
1280
|
-
out.push(
|
|
1281
|
-
...diff(`activities.${activity}.${tier}`, cellName(config, activity, tier), cellName(draft, activity, tier)),
|
|
1282
|
-
);
|
|
1283
|
-
if (changes.mode) out.push(...diff('activityRouting', config.activityRouting, changes.draft.activityRouting));
|
|
1284
|
-
for (const [key, , format, field] of POLICY_ROWS.filter(([key]) => changes.policy.includes(key)))
|
|
1285
|
-
out.push(...diff(field, format(changes.saved[key]), format(changes.tuning[key])));
|
|
1286
|
-
}
|
|
1287
|
-
out.push(
|
|
1288
|
-
ui.line(
|
|
1289
|
-
ui.text(`● ${changes.count} unsaved routing change${changes.count === 1 ? '' : 's'} `, { color: 'yellow' }),
|
|
1290
|
-
ui.Button({
|
|
1291
|
-
key: 'save-routing',
|
|
1292
|
-
label: 'Save',
|
|
1293
|
-
hotkey: 's',
|
|
1294
|
-
variant: 'primary',
|
|
1295
|
-
onPress: actions.saveRouting,
|
|
1296
|
-
}),
|
|
1297
|
-
' ',
|
|
1298
|
-
ui.Button({ key: 'discard-routing', label: 'Discard', hotkey: 'd', onPress: actions.discardRouting }),
|
|
1299
|
-
DRAFT_TABS.includes(tab) ? null : ui.dim(' listed on Routes and Policy'),
|
|
1300
|
-
),
|
|
1301
|
-
);
|
|
1302
|
-
}
|
|
1303
|
-
const undo = view.lastWrite ? ui.Button({ key: 'undo', label: 'Undo', hotkey: 'u', onPress: actions.undo }) : null;
|
|
1304
|
-
const confirmsWrite = Boolean(undo && view.notice?.startsWith('Saved'));
|
|
1305
|
-
if (view.notice || undo) out.push(ui.text(' '));
|
|
1306
|
-
if (view.notice)
|
|
1307
|
-
out.push(ui.line(ui.text(`${view.notice} `, { color: noticeColor(view.notice) }), confirmsWrite ? undo : null));
|
|
1308
|
-
if (undo && !confirmsWrite) out.push(ui.line(ui.dim(`Last change: ${view.lastWrite.label} `), undo));
|
|
1309
|
-
return out;
|
|
1310
|
-
}
|
|
1311
|
-
|
|
1312
|
-
// The counts kept across sessions, read from the stores as last loaded: how often the tools a turn used fit the
|
|
1313
|
-
// classifier's activity, and what the activity moves led to. Agreement uses the classifier's raw answer, so it can
|
|
1314
|
-
// differ from the session's.
|
|
1315
|
-
function storeUsage(ui, store, metrics) {
|
|
1316
|
-
const summary = storeSummary(readStore(store));
|
|
1317
|
-
const { downMoves } = readMetrics(metrics);
|
|
1318
|
-
const out = [ui.head('ACROSS SESSIONS · since the last reset')];
|
|
1319
|
-
out.push(ui.text(`Labelled ${summary.labelled} turns`));
|
|
1320
|
-
const { matched, total } = summary.agreement;
|
|
1321
|
-
if (total > 0)
|
|
1322
|
-
out.push(ui.text(`Tool compatibility (not accuracy): ${matched} of ${total} turns (${percent(matched / total)})`));
|
|
1323
|
-
const { focus } = summary;
|
|
1324
|
-
if (focus.total > 0)
|
|
1325
|
-
out.push(
|
|
1326
|
-
ui.text(
|
|
1327
|
-
`Code/ops/explore classifier vs tools: ${focus.matched} of ${focus.total} turns (${percent(focus.matched / focus.total)})`,
|
|
1328
|
-
),
|
|
1329
|
-
);
|
|
1330
|
-
if (summary.disagreements.length)
|
|
1331
|
-
out.push(
|
|
1332
|
-
ui.text(`Mismatch ${summary.disagreements.map((d) => `${d.predicted} → ${d.observed} ${d.n}`).join(' · ')}`),
|
|
1333
|
-
);
|
|
1334
|
-
const { taken, refused } = summary.lateral;
|
|
1335
|
-
if (taken + refused > 0) out.push(ui.text(`Activity moves ${taken} taken · ${refused} refused`));
|
|
1336
|
-
const { moves, escalations } = downMoves;
|
|
1337
|
-
if (moves > 0)
|
|
1338
|
-
out.push(
|
|
1339
|
-
ui.text(`Escalations after a cheaper activity move: ${escalations} in ${moves} move${moves === 1 ? '' : 's'}`),
|
|
1340
|
-
);
|
|
1341
|
-
const unpriced = unpricedLine(metrics);
|
|
1342
|
-
if (unpriced) out.push(ui.text(unpriced));
|
|
1343
|
-
return out;
|
|
1344
|
-
}
|
|
1345
|
-
|
|
1346
|
-
// The footer as [text, joiner] pieces, packed to the pane: two lines at full width, more in a narrow pane, never a
|
|
1347
|
-
// line that ends in a joiner.
|
|
1348
|
-
const SAVINGS_FOOTER = [
|
|
1349
|
-
['≈$ = tokens × configured API rates (list/custom)', ' · '],
|
|
1350
|
-
['not invoices or measured quota savings', ' · '],
|
|
1351
|
-
['answer quality not measured', ''],
|
|
1352
|
-
];
|
|
1353
|
-
const FULL_WIDTH = 68;
|
|
1354
|
-
|
|
1355
|
-
// `pieces` packed into lines of at most `width` cells after `indent`.
|
|
1356
|
-
function pack(pieces, width, indent) {
|
|
1357
|
-
const lines = [];
|
|
1358
|
-
let line = '';
|
|
1359
|
-
let joiner = '';
|
|
1360
|
-
for (const [text, next] of pieces) {
|
|
1361
|
-
if (line && indent.length + line.length + joiner.length + text.length > width) {
|
|
1362
|
-
lines.push(indent + line);
|
|
1363
|
-
line = text;
|
|
1364
|
-
} else line += (line ? joiner : '') + text;
|
|
1365
|
-
joiner = next;
|
|
1366
|
-
}
|
|
1367
|
-
return line ? [...lines, indent + line] : lines;
|
|
1368
|
-
}
|
|
1369
|
-
|
|
1370
|
-
// A dim sentence wrapped by words to a narrow pane, keeping its indent, which Ink's own wrap would drop.
|
|
1371
|
-
const dimLines = (ui, text, columns) => {
|
|
1372
|
-
const indent = /^ */.exec(text)[0];
|
|
1373
|
-
const words = text
|
|
1374
|
-
.trim()
|
|
1375
|
-
.split(' ')
|
|
1376
|
-
.map((word) => [word, ' ']);
|
|
1377
|
-
return (Number.isFinite(columns) ? pack(words, columns, indent) : [text]).map((line) => ui.dim(line));
|
|
1378
|
-
};
|
|
1379
|
-
const footer = (ui, columns) =>
|
|
1380
|
-
pack(SAVINGS_FOOTER, Math.min(FULL_WIDTH, columns ?? FULL_WIDTH), ' ').map((line) => ui.dim(line));
|
|
1381
|
-
|
|
1382
|
-
// The tier whose route is your model at your effort, for its color; else any tier on your model.
|
|
1383
|
-
function yourTier(config, view) {
|
|
1384
|
-
const runs = (tier) => {
|
|
1385
|
-
const route = resolveRoute(config, tier);
|
|
1386
|
-
return (
|
|
1387
|
-
isSameModel(config.models[route.model].id, view.nativeModel) &&
|
|
1388
|
-
routeEffort(config, route, view.nativeEffort) === (view.nativeEffort ?? null)
|
|
1389
|
-
);
|
|
1390
|
-
};
|
|
1391
|
-
return TIERS.find(runs) ?? cellForModel(config, view.nativeModel)?.tier ?? null;
|
|
1392
|
-
}
|
|
1393
|
-
|
|
1394
|
-
// Prices summed over the priced requests, and how many were not priced (a model without a price in router.json).
|
|
1395
|
-
const pricedText = (text, counts) =>
|
|
1396
|
-
counts.pricedRequests < counts.requests ? `${text} (${counts.requests - counts.pricedRequests} unpriced)` : text;
|
|
1397
|
-
|
|
1398
|
-
// "Oct 7 – Oct 13" for a period's span, or "since Oct 10", its `first` day, for one without a range; null without one.
|
|
1399
|
-
function spanText(period, now, first) {
|
|
1400
|
-
const { from, to } = periodRange(period, now);
|
|
1401
|
-
if (period === 'today') return `today, ${dayLabel(to)}`;
|
|
1402
|
-
if (from) return `${dayLabel(from)} – ${dayLabel(to)}`;
|
|
1403
|
-
return first ? `since ${dayLabel(first)}` : null;
|
|
1404
|
-
}
|
|
1405
|
-
|
|
1406
|
-
// The subagent rows: the open mark, the type, its squares, agents, cost, and against Claude Code's pick with its arrow.
|
|
1407
|
-
const AGENT_TYPE = 19;
|
|
1408
|
-
const AGENT_SQUARES = 12;
|
|
1409
|
-
const AGENT_ROW = 4 + AGENT_TYPE + AGENT_SQUARES + 3 + 9 + 11;
|
|
1410
|
-
// The summary strip's squares at most: past this each model gets its share of them, not one per agent.
|
|
1411
|
-
const AGENT_STRIP = 25;
|
|
1412
|
-
// The models in the order their squares go, by family; any other after them by name.
|
|
1413
|
-
const FAMILIES = ['haiku', 'sonnet', 'opus', 'fable'];
|
|
1414
|
-
const clipped = (text, width) => (text.length < width ? text : `${text.slice(0, width - 2)}…`);
|
|
1415
|
-
const familyRank = (id) => {
|
|
1416
|
-
const rank = FAMILIES.findIndex((family) => id.includes(family));
|
|
1417
|
-
return rank < 0 ? FAMILIES.length : rank;
|
|
1418
|
-
};
|
|
1419
|
-
const byFamily = (a, b) => familyRank(a[0]) - familyRank(b[0]) || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0);
|
|
1420
|
-
const colored = (ui, id, text) => {
|
|
1421
|
-
const color = modelColor(id);
|
|
1422
|
-
return ui.text(text, color ? { color } : {});
|
|
1423
|
-
};
|
|
1424
|
-
|
|
1425
|
-
// Each model's agents in a period and how many the router moved, across the routed types and the pass-throughs.
|
|
1426
|
-
function agentsByModel(s) {
|
|
1427
|
-
const models = new Map();
|
|
1428
|
-
for (const kind of ['routed', 'passed'])
|
|
1429
|
-
for (const counts of Object.values(s[kind]))
|
|
1430
|
-
for (const [id, m] of Object.entries(counts.models)) {
|
|
1431
|
-
const sum = models.get(id) ?? { agents: 0, moved: 0 };
|
|
1432
|
-
models.set(id, { agents: sum.agents + m.agents, moved: sum.moved + (m.moved ?? 0) });
|
|
1433
|
-
}
|
|
1434
|
-
return [...models].filter(([, m]) => m.agents > 0).sort(byFamily);
|
|
1435
|
-
}
|
|
1436
|
-
|
|
1437
|
-
// Squares for `models` ([id, { agents, moved }]), colored by model, filled for the agents the router moved. `scale`:
|
|
1438
|
-
// one per agent up to `max`, then each model's share of `max`, at least one. Else one per agent up to `max`, and
|
|
1439
|
-
// `rest` counts the agents past them.
|
|
1440
|
-
function squares(ui, models, max, scale) {
|
|
1441
|
-
const total = models.reduce((n, [, m]) => n + m.agents, 0);
|
|
1442
|
-
const shown = (n) => (!scale || total <= max || !n ? n : Math.max(1, Math.round((n / total) * max)));
|
|
1443
|
-
const parts = [];
|
|
1444
|
-
let left = max;
|
|
1445
|
-
for (const [id, m] of models) {
|
|
1446
|
-
const n = Math.min(shown(m.agents), left);
|
|
1447
|
-
const filled = Math.min(n, shown(m.moved));
|
|
1448
|
-
if (filled) parts.push(colored(ui, id, '■'.repeat(filled)));
|
|
1449
|
-
if (n > filled) parts.push(colored(ui, id, '□'.repeat(n - filled)));
|
|
1450
|
-
left -= n;
|
|
1451
|
-
}
|
|
1452
|
-
return { parts, width: max - left, rest: scale ? 0 : total - (max - left) };
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
// "routing −$0.21 (−5%) vs Claude Code’s picks on 7 routed agents", in the session readout's words: the routed
|
|
1456
|
-
// agents' cost against Claude Code's picks.
|
|
1457
|
-
function agentVerdict(ui, routed) {
|
|
1458
|
-
const agents = routed.reduce((n, [, c]) => n + c.agents, 0);
|
|
1459
|
-
const totals = routed.map(([, c]) => modelTotals(c));
|
|
1460
|
-
const cost = totals.reduce((n, t) => n + t.usd, 0);
|
|
1461
|
-
const core = totals.reduce((n, t) => n + t.coreUsd, 0);
|
|
1462
|
-
const counts = {
|
|
1463
|
-
requests: routed.reduce((n, [, c]) => n + c.requests, 0),
|
|
1464
|
-
pricedRequests: routed.reduce((n, [, c]) => n + c.pricedRequests, 0),
|
|
1465
|
-
};
|
|
1466
|
-
if (!counts.pricedRequests) return ui.dim(` ${plural(agents, 'routed agent')}, not priced`);
|
|
1467
|
-
const difference = cents(cost - core);
|
|
1468
|
-
const on = pricedText(` on ${plural(agents, 'routed agent')}`, counts);
|
|
1469
|
-
const share = core > 0 && difference ? ` (${difference < 0 ? '−' : '+'}${percent(Math.abs(difference) / core)})` : '';
|
|
1470
|
-
return ui.line(
|
|
1471
|
-
' ',
|
|
1472
|
-
ui.text(`routing ${signedUsd(difference)}${share}`, { bold: true, color: savingsColor(difference) }),
|
|
1473
|
-
' vs Claude Code’s picks',
|
|
1474
|
-
ui.dim(on),
|
|
1475
|
-
);
|
|
1476
|
-
}
|
|
1477
|
-
|
|
1478
|
-
// The strip of every agent by model, what the router moved, and how routing paid off.
|
|
1479
|
-
function agentSummary(ui, s, columns) {
|
|
1480
|
-
const models = agentsByModel(s);
|
|
1481
|
-
const total = models.reduce((n, [, m]) => n + m.agents, 0);
|
|
1482
|
-
const moved = models.reduce((n, [, m]) => n + m.moved, 0);
|
|
1483
|
-
const strip = squares(ui, models, AGENT_STRIP, true);
|
|
1484
|
-
const labels = models.map(([id, m]) => [id, `${modelName(id)} ${m.agents}`]);
|
|
1485
|
-
const names = labels.flatMap(([id, text], i) => [i ? ui.dim(' · ') : null, colored(ui, id, text)]);
|
|
1486
|
-
const namesWidth = labels.reduce((n, [, text], i) => n + (i ? 3 : 0) + text.length, 0);
|
|
1487
|
-
const out =
|
|
1488
|
-
Number.isFinite(columns) && 2 + strip.width + 2 + namesWidth > columns
|
|
1489
|
-
? [ui.line(' ', ...strip.parts), ui.line(' ', ...names)]
|
|
1490
|
-
: [ui.line(' ', ...strip.parts, ' ', ...names)];
|
|
1491
|
-
out.push(
|
|
1492
|
-
ui.line(
|
|
1493
|
-
` ${plural(total, 'agent')}`,
|
|
1494
|
-
ui.dim(' · '),
|
|
1495
|
-
`■ ${moved} moved by the router`,
|
|
1496
|
-
ui.dim(' · '),
|
|
1497
|
-
`□ ${total - moved} on Claude Code’s pick`,
|
|
1498
|
-
),
|
|
1499
|
-
);
|
|
1500
|
-
const routed = Object.entries(s.routed).filter(([, c]) => c.agents > 0);
|
|
1501
|
-
if (routed.length) out.push(agentVerdict(ui, routed));
|
|
1502
|
-
return out;
|
|
1503
|
-
}
|
|
1504
|
-
|
|
1505
|
-
// Agents per day from the period's first day, or the first recorded one, to today, with the busiest day.
|
|
1506
|
-
function agentTrend(ui, s, period, now) {
|
|
1507
|
-
if (period === 'session' || period === 'today' || !s.perDay.length) return [];
|
|
1508
|
-
const counts = new Map(s.perDay);
|
|
1509
|
-
const start = periodRange(period, now).from ?? s.perDay[0][0];
|
|
1510
|
-
const [y, m, d] = start.split('-').map(Number);
|
|
1511
|
-
const days = [];
|
|
1512
|
-
for (let i = 0; ; i += 1) {
|
|
1513
|
-
const key = dayKey(new Date(y, m - 1, d + i).getTime());
|
|
1514
|
-
if (key > dayKey(now)) break;
|
|
1515
|
-
days.push([key, counts.get(key) ?? 0]);
|
|
1516
|
-
}
|
|
1517
|
-
if (days.length < 2) return [];
|
|
1518
|
-
const [peakDay, peak] = days.reduce((top, day) => (day[1] > top[1] ? day : top));
|
|
1519
|
-
// A long All period keeps its last days, as many as the row has room for.
|
|
1520
|
-
const values = days.map(([, n]) => n).slice(-(AGENT_ROW - 30));
|
|
1521
|
-
return [
|
|
1522
|
-
ui.line(' per day ', ui.text(sparkline(values), { bold: true }), ui.dim(` peak ${peak} on ${dayLabel(peakDay)}`)),
|
|
1523
|
-
];
|
|
1524
|
-
}
|
|
1525
|
-
|
|
1526
|
-
// "+$0.44 ▲" against Claude Code's pick, in the savings colors; a dim dash when it rounds to nothing.
|
|
1527
|
-
const pickDifference = (ui, value) =>
|
|
1528
|
-
value
|
|
1529
|
-
? ui.text(`${signedUsd(value)} ${value > 0 ? '▲' : '▼'}`.padStart(11), { color: savingsColor(value) })
|
|
1530
|
-
: ui.dim('—'.padStart(9));
|
|
1531
|
-
|
|
1532
|
-
// What a routed model row holds: "2 moved up", or "1 kept, 2 down".
|
|
1533
|
-
function moveText(m) {
|
|
1534
|
-
const kept = m.agents - m.moved;
|
|
1535
|
-
const other = m.moved - m.up - m.down;
|
|
1536
|
-
const parts = [
|
|
1537
|
-
kept ? `${kept} kept` : null,
|
|
1538
|
-
m.up ? `${m.up} up` : null,
|
|
1539
|
-
m.down ? `${m.down} down` : null,
|
|
1540
|
-
other ? `${other} moved` : null,
|
|
1541
|
-
].filter(Boolean);
|
|
1542
|
-
if (parts.length > 1 || kept) return parts.join(', ');
|
|
1543
|
-
return `${m.moved} moved${m.up ? ' up' : m.down ? ' down' : ''}`;
|
|
1544
|
-
}
|
|
1545
|
-
|
|
1546
|
-
// One row of the subagent table; open, the models it ran on with their cost, and its requests and retry proxies.
|
|
1547
|
-
function agentRow(ui, view, actions, { id, name, counts, routed, why, narrow, columns, billing }) {
|
|
1548
|
-
const open = view.usageOpen === id;
|
|
1549
|
-
const models = Object.entries(counts.models)
|
|
1550
|
-
.filter(([, m]) => m.agents > 0)
|
|
1551
|
-
.sort(byFamily);
|
|
1552
|
-
const marks = squares(
|
|
1553
|
-
ui,
|
|
1554
|
-
models.map(([model, m]) => [model, { agents: m.agents, moved: m.moved ?? 0 }]),
|
|
1555
|
-
AGENT_SQUARES - 3,
|
|
1556
|
-
false,
|
|
1557
|
-
);
|
|
1558
|
-
const t = modelTotals(counts);
|
|
1559
|
-
// A row none of whose requests had a price says so rather than $0.00.
|
|
1560
|
-
const priced = counts.pricedRequests > 0;
|
|
1561
|
-
const costCell = (value) => (priced ? costText(value, billing).padStart(9) : ui.dim('no price'.padStart(9)));
|
|
1562
|
-
const out = [
|
|
1563
|
-
ui.line(
|
|
1564
|
-
' ',
|
|
1565
|
-
ui.Button({
|
|
1566
|
-
key: `row-${id}`,
|
|
1567
|
-
label: open ? '▾' : '▸',
|
|
1568
|
-
plain: true,
|
|
1569
|
-
onPress: () => actions.usageOpen(open ? null : id),
|
|
1570
|
-
}),
|
|
1571
|
-
' ',
|
|
1572
|
-
ui.cell(AGENT_TYPE, clipped(name, AGENT_TYPE)),
|
|
1573
|
-
ui.cell(AGENT_SQUARES, ui.line(...marks.parts, marks.rest ? ui.dim(`+${marks.rest}`) : null)),
|
|
1574
|
-
String(counts.agents).padStart(3),
|
|
1575
|
-
narrow ? null : costCell(t.usd),
|
|
1576
|
-
routed && priced ? pickDifference(ui, cents(t.usd - t.coreUsd)) : null,
|
|
1577
|
-
!routed && why && !narrow ? ui.dim(` ${why}`) : null,
|
|
1578
|
-
),
|
|
1579
|
-
];
|
|
1580
|
-
if (!open) return out;
|
|
1581
|
-
for (const [model, m] of models)
|
|
1582
|
-
out.push(
|
|
1583
|
-
ui.line(
|
|
1584
|
-
' ',
|
|
1585
|
-
ui.cell(17, colored(ui, model, `${m.moved ? '■' : '□'} ${modelName(model)}`)),
|
|
1586
|
-
ui.cell(15, ui.dim(routed ? moveText(m) : plural(m.agents, 'agent'))),
|
|
1587
|
-
narrow ? null : costCell(m.usd),
|
|
1588
|
-
routed && priced ? pickDifference(ui, cents(m.usd - m.coreUsd)) : null,
|
|
1589
|
-
),
|
|
1590
|
-
);
|
|
1591
|
-
const notes = [
|
|
1592
|
-
`${(counts.requests / counts.agents).toFixed(1)} requests per agent`,
|
|
1593
|
-
routed ? `${counts.respawns || 'none'} respawned within 10 min` : null,
|
|
1594
|
-
counts.failures ? plural(counts.failures, 'classifier failure') : null,
|
|
1595
|
-
counts.pricedRequests < counts.requests ? `${counts.requests - counts.pricedRequests} requests not priced` : null,
|
|
1596
|
-
].filter(Boolean);
|
|
1597
|
-
out.push(...dimLines(ui, ` ${notes.join(' · ')}`, columns));
|
|
1598
|
-
return out;
|
|
1599
|
-
}
|
|
1600
|
-
|
|
1601
|
-
// Subagents in the chosen period: this session's record, or every session's in every project (one $.store per
|
|
1602
|
-
// plugin install). A strip of every agent by the model it ran on, filled where the router moved it off Claude Code's
|
|
1603
|
-
// pick, and the routed agents' cost against those picks. Then the routed types and the pass-throughs, each with its
|
|
1604
|
-
// squares, agents and cost, opening to its models; a pass-through says why Claude Code picked.
|
|
1605
|
-
function subagentUsage(ui, config, view, actions, period, now, columns, billing) {
|
|
1606
|
-
const mode = config.subagentRouting;
|
|
1607
|
-
const record = readSubagentStore(period === 'session' ? view.subagentSession : view.subagentStore);
|
|
1608
|
-
const s = summarizeSubagents(record, periodRange(period, now));
|
|
1609
|
-
const first = Number.isFinite(record.since) ? dayKey(record.since) : null;
|
|
1610
|
-
const scope =
|
|
1611
|
-
period === 'session' ? 'this session' : `all projects · ${spanText(period, now, first) ?? 'nothing yet'}`;
|
|
1612
|
-
const out = [ui.line(ui.head('SUBAGENTS'), ui.dim(` · ${scope} subagent routing ${mode}`))];
|
|
1613
|
-
const routed = Object.entries(s.routed)
|
|
1614
|
-
.filter(([, c]) => c.agents > 0)
|
|
1615
|
-
.sort((a, b) => b[1].agents - a[1].agents || (a[0] < b[0] ? -1 : 1));
|
|
1616
|
-
const passed = PASS_REASONS.filter((reason) => s.passed[reason]?.agents > 0).sort(
|
|
1617
|
-
(a, b) => s.passed[b].agents - s.passed[a].agents,
|
|
1618
|
-
);
|
|
1619
|
-
const undated = s.undated
|
|
1620
|
-
? [
|
|
1621
|
-
ui.dim(
|
|
1622
|
-
` + ${plural(s.undated.agents, 'agent')} from before 1.14.0, not split by model · ${costText(s.undated.usd, billing)}`,
|
|
1623
|
-
),
|
|
1624
|
-
]
|
|
1625
|
-
: [];
|
|
1626
|
-
if (!routed.length && !passed.length) {
|
|
1627
|
-
const older = !s.undated && periodRange(period, now).from !== null && readSubagentStore(view.subagentStore).undated;
|
|
1628
|
-
return [
|
|
1629
|
-
...out,
|
|
1630
|
-
ui.dim(
|
|
1631
|
-
mode === 'off'
|
|
1632
|
-
? ' Not recorded: subagentRouting is off in router.json.'
|
|
1633
|
-
: ` No subagents ${PERIOD_PHRASE[period]}.`,
|
|
1634
|
-
),
|
|
1635
|
-
...undated,
|
|
1636
|
-
...(older ? [ui.dim(' Agents from before 1.14.0 have no date: All shows them.')] : []),
|
|
1637
|
-
];
|
|
1638
|
-
}
|
|
1639
|
-
const narrow = Number.isFinite(columns) && columns < AGENT_ROW;
|
|
1640
|
-
out.push(...agentSummary(ui, s, columns), ...agentTrend(ui, s, period, now), ...undated);
|
|
1641
|
-
const rowOf = (fields) => agentRow(ui, view, actions, { narrow, columns, billing, ...fields });
|
|
1642
|
-
if (routed.length) {
|
|
1643
|
-
out.push(
|
|
1644
|
-
ui.text(' '),
|
|
1645
|
-
ui.line(ui.text(' ROUTED', { bold: true }), ui.dim(' · the router picked the model at spawn')),
|
|
1646
|
-
ui.dim(
|
|
1647
|
-
` ${'type'.padEnd(AGENT_TYPE)}${'agents'.padEnd(AGENT_SQUARES + 3)}${narrow ? '' : 'cost'.padStart(9)}${'vs CC'.padStart(9)}`,
|
|
1648
|
-
),
|
|
1649
|
-
);
|
|
1650
|
-
for (const [type, counts] of routed)
|
|
1651
|
-
out.push(...rowOf({ id: `subagent:${type}`, name: type, counts, routed: true }));
|
|
1652
|
-
}
|
|
1653
|
-
if (passed.length) {
|
|
1654
|
-
out.push(ui.text(' '), ui.line(ui.text(' NOT ROUTED', { bold: true }), ui.dim(' · Claude Code picked the model')));
|
|
1655
|
-
for (const reason of passed)
|
|
1656
|
-
out.push(
|
|
1657
|
-
...rowOf({
|
|
1658
|
-
id: `subagent-pass:${reason}`,
|
|
1659
|
-
name: PASS_LABEL[reason],
|
|
1660
|
-
counts: s.passed[reason],
|
|
1661
|
-
routed: false,
|
|
1662
|
-
why: PASS_WHY[reason],
|
|
1663
|
-
}),
|
|
1664
|
-
);
|
|
1665
|
-
}
|
|
1666
|
-
out.push(
|
|
1667
|
-
ui.text(' '),
|
|
1668
|
-
...dimLines(ui, ' ■ moved by the router · □ Claude Code’s pick · color: the model', columns),
|
|
1669
|
-
...(routed.length
|
|
1670
|
-
? dimLines(ui, ' vs CC: the same tokens on Claude Code’s pick, at configured API rates', columns)
|
|
1671
|
-
: []),
|
|
1672
|
-
);
|
|
1673
|
-
return out;
|
|
1674
|
-
}
|
|
1675
|
-
|
|
1676
|
-
const PERIOD_LABEL = { session: 'Session', today: 'Today', '7d': '7d', '30d': '30d', month: 'Month', all: 'All' };
|
|
1677
|
-
// "No routed replies …" for each period.
|
|
1678
|
-
const PERIOD_PHRASE = {
|
|
1679
|
-
session: 'this session',
|
|
1680
|
-
today: 'today',
|
|
1681
|
-
'7d': 'in the last 7 days',
|
|
1682
|
-
'30d': 'in the last 30 days',
|
|
1683
|
-
month: 'this month',
|
|
1684
|
-
all: 'yet',
|
|
1685
|
-
};
|
|
1686
|
-
const GROUPS = [
|
|
1687
|
-
['activity', 'Activity'],
|
|
1688
|
-
['model', 'Model'],
|
|
1689
|
-
['tier', 'Tier'],
|
|
1690
|
-
];
|
|
1691
|
-
// What an open row lists: the routes of an activity, the activities of a model or a tier.
|
|
1692
|
-
const CHILD_BY = { activity: 'route', model: 'activity', tier: 'activity' };
|
|
1693
|
-
// The ledger's label column after its sign, and its amount column.
|
|
1694
|
-
const LEDGER_AMOUNT = 9;
|
|
1695
|
-
// The breakdown: the open mark, the name, replies, cost and vs yours; "mostly on" or the share only where it fits.
|
|
1696
|
-
const NAME = 20;
|
|
1697
|
-
const BREAKDOWN = 4 + NAME + 8 + 10 + 15;
|
|
1698
|
-
// Tiers in their own order; `none` (not routed) last.
|
|
1699
|
-
const TIER_RANK = (name) => (TIERS.includes(name) ? TIERS.indexOf(name) : TIERS.length);
|
|
1700
|
-
const TAIL = 23;
|
|
1701
|
-
// Bars at full scale: the routed share of your model, and the costliest row of What if.
|
|
1702
|
-
const BAR = 22;
|
|
1703
|
-
const SPARK = '▁▂▃▄▅▆▇█';
|
|
1704
|
-
|
|
1705
|
-
const dayLabel = (key) => {
|
|
1706
|
-
const [y, m, d] = key.split('-').map(Number);
|
|
1707
|
-
return new Date(y, m - 1, d).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
|
1708
|
-
};
|
|
1709
|
-
// "Opus 5.5 · high" for a cell's `id@effort`.
|
|
1710
|
-
function routeText(route) {
|
|
1711
|
-
const at = route.lastIndexOf('@');
|
|
1712
|
-
const effort = route.slice(at + 1);
|
|
1713
|
-
return routeLabel(route.slice(0, at), effort === 'session' ? null : effort);
|
|
1714
|
-
}
|
|
1715
|
-
// A cents difference, so a row never reads "+$0.00" for a fraction of a cent.
|
|
1716
|
-
const cents = (usd) => Math.round(usd * 100) / 100;
|
|
1717
|
-
const differenceText = (ui, usdValue, width) => {
|
|
1718
|
-
const value = cents(usdValue);
|
|
1719
|
-
return ui.text(signedUsd(value).padStart(width), savingsColor(value) ? { color: savingsColor(value) } : {});
|
|
1720
|
-
};
|
|
1721
|
-
|
|
1722
|
-
// A breakdown row's name: an activity as its verb, a tier in its color, a model or a route by name.
|
|
1723
|
-
function groupName(ui, by, name) {
|
|
1724
|
-
if (name === 'none') return ui.dim(by === 'activity' ? 'no activity' : 'not routed');
|
|
1725
|
-
if (by === 'activity') return ui.activity(name);
|
|
1726
|
-
if (by === 'tier') return ui.bars(name);
|
|
1727
|
-
return ui.text(by === 'model' ? modelName(name) : routeText(name));
|
|
1728
|
-
}
|
|
1729
|
-
|
|
1730
|
-
// The period buttons; `p` presses the one after the shown period, so it steps through them.
|
|
1731
|
-
function periodRow(ui, period, actions) {
|
|
1732
|
-
const next = PERIODS[(PERIODS.indexOf(period) + 1) % PERIODS.length];
|
|
1733
|
-
return ui.line(
|
|
1734
|
-
...PERIODS.flatMap((id) => [
|
|
1735
|
-
ui.Button({
|
|
1736
|
-
key: `period-${id}`,
|
|
1737
|
-
label: PERIOD_LABEL[id],
|
|
1738
|
-
...(id === next ? { hotkey: 'p' } : {}),
|
|
1739
|
-
variant: id === period ? 'primary' : 'secondary',
|
|
1740
|
-
onPress: () => actions.period(id),
|
|
1741
|
-
}),
|
|
1742
|
-
' ',
|
|
1743
|
-
]),
|
|
1744
|
-
ui.dim(' p next'),
|
|
1745
|
-
);
|
|
1746
|
-
}
|
|
1747
|
-
|
|
1748
|
-
// Whose replies the period holds and what each is compared with.
|
|
1749
|
-
function scopeLine(ui, config, view, period, summary, now) {
|
|
1750
|
-
if (period === 'session') {
|
|
1751
|
-
const recorded = recordedModels(config, view, summary);
|
|
1752
|
-
if (recorded)
|
|
1753
|
-
return ui.dim(
|
|
1754
|
-
Object.keys(summary.yours).length > 1
|
|
1755
|
-
? `this session · each reply vs the model /model selected: ${recorded}`
|
|
1756
|
-
: `this session · your model ${recorded}`,
|
|
1757
|
-
);
|
|
1758
|
-
const tier = yourTier(config, view);
|
|
1759
|
-
return ui.line(
|
|
1760
|
-
ui.dim('this session · your model '),
|
|
1761
|
-
tier ? ui.tier(tier, yourModel(view)) : ui.text(yourModel(view)),
|
|
1762
|
-
);
|
|
1763
|
-
}
|
|
1764
|
-
const span = spanText(period, now, summary.days[0]?.[0]) ?? 'no dated replies yet';
|
|
1765
|
-
return ui.dim(`all projects · ${span} · each session vs its own model`);
|
|
1766
|
-
}
|
|
1767
|
-
|
|
1768
|
-
// Replies, turns, tokens and the cache share of the input.
|
|
1769
|
-
const statsLine = (ui, s) =>
|
|
1770
|
-
ui.dim(
|
|
1771
|
-
` ${plural(s.replies, 'reply', 'replies')} · ${plural(s.turns, 'turn')} · input ${formatTokens(s.inputTokens)}` +
|
|
1772
|
-
` · cache hit ${s.inputTokens ? percent(s.cacheReadTokens / s.inputTokens) : '—'} · output ${formatTokens(s.outputTokens)}`,
|
|
1773
|
-
);
|
|
1774
|
-
|
|
1775
|
-
// The period's cost by activity, model or tier, most first; `g` presses the next grouping. A row opens to what it
|
|
1776
|
-
// holds and its cost parts. A narrow pane drops "mostly on" (or the share) before any number.
|
|
1777
|
-
function breakdownLines(ui, view, actions, summary, columns, compare, billing) {
|
|
1778
|
-
const by = GROUPS.some(([id]) => id === view.usageGroup) ? view.usageGroup : 'activity';
|
|
1779
|
-
const next = GROUPS[(GROUPS.findIndex(([id]) => id === by) + 1) % GROUPS.length][0];
|
|
1780
|
-
const out = [
|
|
1781
|
-
ui.line(
|
|
1782
|
-
ui.head('BY '),
|
|
1783
|
-
...GROUPS.flatMap(([id, label]) => [
|
|
1784
|
-
ui.Button({
|
|
1785
|
-
key: `group-${id}`,
|
|
1786
|
-
label,
|
|
1787
|
-
...(id === next ? { hotkey: 'g' } : {}),
|
|
1788
|
-
variant: id === by ? 'primary' : 'secondary',
|
|
1789
|
-
onPress: () => actions.usageGroup(id),
|
|
1790
|
-
}),
|
|
1791
|
-
' ',
|
|
1792
|
-
]),
|
|
1793
|
-
ui.dim(' g next'),
|
|
1794
|
-
),
|
|
1795
|
-
];
|
|
1796
|
-
const cells = summary.observed.cells;
|
|
1797
|
-
// Tiers read low to high, as the band's bars; activities and models lead with the costliest.
|
|
1798
|
-
const groups =
|
|
1799
|
-
by === 'tier' ? breakdown(cells, by).sort((a, b) => TIER_RANK(a.name) - TIER_RANK(b.name)) : breakdown(cells, by);
|
|
1800
|
-
const missing = new Map(breakdown(summary.observed.uncompared, by).map((g) => [g.name, g]));
|
|
1801
|
-
if (!groups.length) return [...out, ui.dim(' no routed replies in this period')];
|
|
1802
|
-
const tail = !Number.isFinite(columns) || columns >= BREAKDOWN + TAIL;
|
|
1803
|
-
const tailHead = by === 'model' ? 'share' : 'mostly on';
|
|
1804
|
-
out.push(
|
|
1805
|
-
ui.dim(
|
|
1806
|
-
` ${''.padEnd(NAME)}${'replies'.padStart(8)}${'cost'.padStart(10)}${compare ? `vs ${UNROUTED}`.padStart(15) : ''}${tail ? ` ${tailHead}` : ''}`,
|
|
1807
|
-
),
|
|
1808
|
-
);
|
|
1809
|
-
const numbers = (sums, unavailable) => [
|
|
1810
|
-
`${String(sums[0]).padStart(8)}${costText(sums[1], billing).padStart(10)}`,
|
|
1811
|
-
!compare ? null : unavailable ? ui.dim('—'.padStart(15)) : differenceText(ui, sums[1] - sums[2], 15),
|
|
1812
|
-
];
|
|
1813
|
-
for (const group of groups) {
|
|
1814
|
-
const id = `${by}:${group.name}`;
|
|
1815
|
-
const open = view.usageOpen === id;
|
|
1816
|
-
const route = by === 'model' ? null : mostlyOn(cellsWhere(cells, by, group.name));
|
|
1817
|
-
out.push(
|
|
1818
|
-
ui.line(
|
|
1819
|
-
' ',
|
|
1820
|
-
ui.Button({
|
|
1821
|
-
key: `row-${id}`,
|
|
1822
|
-
label: open ? '▾' : '▸',
|
|
1823
|
-
plain: true,
|
|
1824
|
-
onPress: () => actions.usageOpen(open ? null : id),
|
|
1825
|
-
}),
|
|
1826
|
-
' ',
|
|
1827
|
-
ui.cell(NAME, groupName(ui, by, group.name)),
|
|
1828
|
-
...numbers(group.sums, missing.has(group.name)),
|
|
1829
|
-
!tail
|
|
1830
|
-
? null
|
|
1831
|
-
: by === 'model'
|
|
1832
|
-
? ui.dim(` ${percent(summary.observed.routedUsd > 0 ? group.sums[1] / summary.observed.routedUsd : 0)}`)
|
|
1833
|
-
: route
|
|
1834
|
-
? ui.dim(` ${routeText(route)}`)
|
|
1835
|
-
: null,
|
|
1836
|
-
),
|
|
1837
|
-
);
|
|
1838
|
-
if (!open) continue;
|
|
1839
|
-
for (const child of group.children)
|
|
1840
|
-
out.push(
|
|
1841
|
-
ui.line(
|
|
1842
|
-
' ',
|
|
1843
|
-
ui.cell(NAME - 2, groupName(ui, CHILD_BY[by], child.name)),
|
|
1844
|
-
...numbers(
|
|
1845
|
-
child.sums,
|
|
1846
|
-
missing.get(group.name)?.children.some((c) => c.name === child.name),
|
|
1847
|
-
),
|
|
1848
|
-
),
|
|
1849
|
-
);
|
|
1850
|
-
const [, , , cheaper, stronger, switched, input, , output] = group.sums;
|
|
1851
|
-
out.push(
|
|
1852
|
-
...dimLines(
|
|
1853
|
-
ui,
|
|
1854
|
-
missing.has(group.name)
|
|
1855
|
-
? ` comparison unavailable · input ${formatTokens(input)} · output ${formatTokens(output)}`
|
|
1856
|
-
: ` cheaper ${signedUsd(cents(cheaper))} · stronger ${signedUsd(cents(stronger))} · cache rebuilds ${signedUsd(cents(switched))} · input ${formatTokens(input)} · output ${formatTokens(output)}`,
|
|
1857
|
-
columns,
|
|
1858
|
-
),
|
|
1859
|
-
);
|
|
1860
|
-
}
|
|
1861
|
-
return out;
|
|
1862
|
-
}
|
|
1863
|
-
|
|
1864
|
-
// What the period's tokens would cost on each model alone, as your model for every reply (compareModels), beside what
|
|
1865
|
-
// routing spent and your own models; a model some reply had no price on is left out.
|
|
1866
|
-
function compareLines(ui, summary, billing) {
|
|
1867
|
-
if (summary.observed.uncomparedReplies)
|
|
1868
|
-
return [ui.dim('What if unavailable: some replies have no comparable cache history.')];
|
|
1869
|
-
const yours = Object.keys(summary.yours);
|
|
1870
|
-
const single = yours.length === 1 ? yours[0] : null;
|
|
1871
|
-
const rows = Object.entries(summary.whatIf)
|
|
1872
|
-
.filter(([, [n]]) => n === summary.replies)
|
|
1873
|
-
.map(([model, [, value]]) => ({
|
|
1874
|
-
label: `${modelName(model)}${model === single ? ' · yours' : ''}`,
|
|
1875
|
-
value,
|
|
1876
|
-
routed: false,
|
|
1877
|
-
yours: model === single,
|
|
1878
|
-
}));
|
|
1879
|
-
if (!rows.some((row) => row.yours))
|
|
1880
|
-
rows.push({
|
|
1881
|
-
label: yours.length > 1 ? 'Your models' : 'Your model',
|
|
1882
|
-
value: summary.yoursUsd,
|
|
1883
|
-
routed: false,
|
|
1884
|
-
yours: true,
|
|
1885
|
-
});
|
|
1886
|
-
rows.push({ label: 'Routed', value: summary.routedUsd, routed: true, yours: false });
|
|
1887
|
-
rows.sort((a, b) => a.value - b.value);
|
|
1888
|
-
const max = Math.max(...rows.map((row) => row.value));
|
|
1889
|
-
return [
|
|
1890
|
-
...rows.map((row) => {
|
|
1891
|
-
const filled = max > 0 ? Math.max(0, Math.min(BAR, Math.round((row.value / max) * BAR))) : 0;
|
|
1892
|
-
return ui.line(
|
|
1893
|
-
' ',
|
|
1894
|
-
ui.cell(22, ui.text(row.label, row.routed ? { bold: true } : {})),
|
|
1895
|
-
ui.text('█'.repeat(filled), { color: row.routed ? 'cyan' : 'gray' }),
|
|
1896
|
-
' '.repeat(BAR - filled),
|
|
1897
|
-
costText(row.value, billing).padStart(LEDGER_AMOUNT),
|
|
1898
|
-
row.routed ? '' : row.yours ? ui.dim('—'.padStart(11)) : differenceText(ui, row.value - summary.yoursUsd, 11),
|
|
1899
|
-
);
|
|
1900
|
-
}),
|
|
1901
|
-
ui.dim(' same tokens and output on each model alone · answer quality not measured'),
|
|
1902
|
-
];
|
|
1903
|
-
}
|
|
1904
|
-
|
|
1905
|
-
// Routed cost per hour, day or week as one line of blocks, a dot for a bucket without replies; and the What if switch.
|
|
1906
|
-
function trendLines(ui, view, actions, summary, period, now, billing) {
|
|
1907
|
-
const toggle = ui.Button({
|
|
1908
|
-
key: 'usage-compare',
|
|
1909
|
-
label: view.usageCompare ? 'Trend' : 'What if',
|
|
1910
|
-
hotkey: 'c',
|
|
1911
|
-
onPress: actions.usageCompare,
|
|
1912
|
-
});
|
|
1913
|
-
const t = trend(summary, period, now);
|
|
1914
|
-
if (t.values.length < 2)
|
|
1915
|
-
return [ui.line(ui.head(`COST BY ${t.unit.toUpperCase()}`), ui.dim(` one ${t.unit} so far `), toggle)];
|
|
1916
|
-
const max = Math.max(...t.values);
|
|
1917
|
-
const glyphs = t.values.map((v) => (v > 0 ? SPARK[Math.min(7, Math.floor((v / max) * 8))] : '·')).join('');
|
|
1918
|
-
return [
|
|
1919
|
-
ui.line(
|
|
1920
|
-
ui.head(`COST BY ${t.unit.toUpperCase()} `),
|
|
1921
|
-
ui.text(glyphs, { color: 'cyan' }),
|
|
1922
|
-
ui.dim(` peak ${costText(max, billing)} `),
|
|
1923
|
-
toggle,
|
|
1924
|
-
),
|
|
1925
|
-
];
|
|
1926
|
-
}
|
|
1927
|
-
|
|
1928
|
-
// The evidence worth a look, each with the tab that changes it: the cell that carried most of the cost, the switch
|
|
1929
|
-
// writes against the saving, the cell that ran above your model. Never which model to use.
|
|
1930
|
-
function insightLines(ui, summary, actions, columns) {
|
|
1931
|
-
if (summary.observed.uncomparedReplies || summary.replies < INSIGHT_REPLIES) return [];
|
|
1932
|
-
const list = insights(summary);
|
|
1933
|
-
if (!list.length) return [];
|
|
1934
|
-
return [
|
|
1935
|
-
ui.text(' '),
|
|
1936
|
-
ui.line(ui.head('WORTH A LOOK'), ui.dim(' · from the numbers; answer quality is not measured')),
|
|
1937
|
-
...list.map((insight) => insightLine(ui, insight, actions, columns)),
|
|
1938
|
-
];
|
|
1939
|
-
}
|
|
1940
|
-
|
|
1941
|
-
// One finding with the tab that changes it.
|
|
1942
|
-
function insightLine(ui, insight, actions, columns) {
|
|
1943
|
-
const where = ({ activity, tier }) => [
|
|
1944
|
-
...(ACTIVITIES.includes(activity) ? [ui.activity(activity), ui.dim(' · ')] : []),
|
|
1945
|
-
TIERS.includes(tier) ? ui.bars(tier) : ui.dim('not routed'),
|
|
1946
|
-
];
|
|
1947
|
-
const link = (insight) =>
|
|
1948
|
-
TIERS.includes(insight.tier)
|
|
1949
|
-
? ui.Button({
|
|
1950
|
-
key: `insight-${insight.kind}`,
|
|
1951
|
-
label: 'Routes',
|
|
1952
|
-
onPress: () =>
|
|
1953
|
-
actions.openRoute(
|
|
1954
|
-
ACTIVITIES.includes(insight.activity) ? `${insight.activity}.${insight.tier}` : `tier.${insight.tier}`,
|
|
1955
|
-
),
|
|
1956
|
-
})
|
|
1957
|
-
: null;
|
|
1958
|
-
return insight.kind === 'switch'
|
|
1959
|
-
? ui.line(
|
|
1960
|
-
` Cache rebuilds used ${percent(insight.share)} of the cheaper-model saving `,
|
|
1961
|
-
ui.Button({ key: 'insight-switch', label: 'Policy', onPress: () => actions.tab('policy') }),
|
|
1962
|
-
)
|
|
1963
|
-
: ui.line(
|
|
1964
|
-
' ',
|
|
1965
|
-
...where(insight),
|
|
1966
|
-
insight.kind === 'cost'
|
|
1967
|
-
? ` carried ${percent(insight.share)} of the cost`
|
|
1968
|
-
: ` ran ${usd(insight.usd)} above your model`,
|
|
1969
|
-
insight.route && (!Number.isFinite(columns) || columns >= 90)
|
|
1970
|
-
? ui.dim(` · mostly ${routeText(insight.route)}`)
|
|
1971
|
-
: null,
|
|
1972
|
-
' ',
|
|
1973
|
-
link(insight),
|
|
1974
|
-
);
|
|
1975
|
-
}
|
|
1976
|
-
|
|
1977
|
-
// Delete history waits for a second press, as nothing brings the records back.
|
|
1978
|
-
function deleteLine(ui, view, actions) {
|
|
1979
|
-
if (!view.confirmDelete)
|
|
1980
|
-
return ui.Button({
|
|
1981
|
-
key: 'delete-history',
|
|
1982
|
-
label: 'Delete history',
|
|
1983
|
-
dimColor: true,
|
|
1984
|
-
onPress: () => actions.confirmDelete(true),
|
|
1985
|
-
});
|
|
1986
|
-
return ui.line(
|
|
1987
|
-
ui.text('Delete the usage, activity and subagent history of every session? ', { color: 'yellow' }),
|
|
1988
|
-
ui.Button({ key: 'delete-confirm', label: 'Delete', variant: 'primary', onPress: actions.resetStats }),
|
|
1989
|
-
' ',
|
|
1990
|
-
ui.Button({ key: 'delete-keep', label: 'Keep', onPress: () => actions.confirmDelete(false) }),
|
|
1991
|
-
);
|
|
1992
|
-
}
|
|
1993
|
-
|
|
1994
|
-
// Answer first for the chosen period: routing against your model as a ledger that adds up, the tokens, the cost by
|
|
1995
|
-
// activity, model or tier, the trend or What if, and what is worth a look. Subagents follow, for the same period.
|
|
1996
|
-
function usageTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
|
|
1997
|
-
const period = PERIODS.includes(view.usagePeriod) ? view.usagePeriod : 'session';
|
|
1998
|
-
const record = liveUsage(view, period === 'session' ? view.usageSession : view.usageStore);
|
|
1999
|
-
const summary = summarize(record, periodRange(period, now));
|
|
2000
|
-
// All adds the undated savings totals kept from before the usage records.
|
|
2001
|
-
const kept = period === 'all' ? allTime(record, view.savingsStore) : null;
|
|
2002
|
-
const legacy = kept?.legacy ?? null;
|
|
2003
|
-
const agents = summarizeSubagents(
|
|
2004
|
-
readSubagentStore(period === 'session' ? view.subagentSession : view.subagentStore),
|
|
2005
|
-
period === 'session' ? { from: null, to: null } : periodRange(period, now),
|
|
2006
|
-
);
|
|
2007
|
-
const cost = periodCost(config, {
|
|
2008
|
-
summary: kept ? { ...summary, ...kept.totals, observed: kept.observed } : summary,
|
|
2009
|
-
agents,
|
|
62
|
+
function drawPanel(elements, config, view, usage, actions, opts) {
|
|
63
|
+
/** @type {import('./ui/model/frame.mjs').PaneContext} */
|
|
64
|
+
const ctx = {
|
|
65
|
+
config,
|
|
66
|
+
view,
|
|
2010
67
|
usage,
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
ui.dim(' · the same tokens on one model '),
|
|
2027
|
-
ui.Button({ key: 'usage-compare', label: 'Trend', hotkey: 'c', onPress: actions.usageCompare }),
|
|
2028
|
-
),
|
|
2029
|
-
...compareLines(ui, summary, cost.billing),
|
|
2030
|
-
]
|
|
2031
|
-
: trendLines(ui, view, actions, summary, period, now, cost.billing)),
|
|
2032
|
-
...insightLines(ui, summary, actions, columns),
|
|
2033
|
-
);
|
|
2034
|
-
}
|
|
2035
|
-
out.push(
|
|
2036
|
-
ui.text(' '),
|
|
2037
|
-
...subagentUsage(ui, config, view, actions, period, now, columns, cost.billing),
|
|
2038
|
-
ui.text(' '),
|
|
2039
|
-
...footer(ui, columns),
|
|
2040
|
-
);
|
|
2041
|
-
out.push(ui.text(' '), deleteLine(ui, view, actions));
|
|
2042
|
-
return out;
|
|
2043
|
-
}
|
|
2044
|
-
|
|
2045
|
-
// Why a period has no cost to show.
|
|
2046
|
-
function emptyCost(ui, config, view, period) {
|
|
2047
|
-
if (period === 'session' && !modelSpec(config, view.nativeModel))
|
|
2048
|
-
return [ui.dim(`${modelName(view.nativeModel)} has no list price in router.json models.`)];
|
|
2049
|
-
const off = period === 'session' && (view.phase === 'unavailable' || view.mode === 'manual');
|
|
2050
|
-
return [
|
|
2051
|
-
ui.dim(
|
|
2052
|
-
off
|
|
2053
|
-
? `Routing is ${view.phase === 'unavailable' ? 'unavailable' : 'off'}: replies use your model and are not counted.`
|
|
2054
|
-
: `No routed replies ${PERIOD_PHRASE[period]}.`,
|
|
2055
|
-
),
|
|
2056
|
-
];
|
|
2057
|
-
}
|
|
2058
|
-
|
|
2059
|
-
// The answer first: the difference against no routing, or why there is none.
|
|
2060
|
-
function costHeadline(ui, cost) {
|
|
2061
|
-
if (cost.diffUsd !== null)
|
|
2062
|
-
return ui.line(
|
|
2063
|
-
ui.text(diffText(cost.diffUsd, cost.baseUsd), { bold: true, color: savingsColor(cost.diffUsd) }),
|
|
2064
|
-
` vs ${UNROUTED}`,
|
|
2065
|
-
cost.early ? ui.text(` ${repliesText(cost)}: ${EARLY_NOTE}`, { color: 'yellow' }) : null,
|
|
2066
|
-
);
|
|
2067
|
-
return ui.dim(
|
|
2068
|
-
cost.effortOnly
|
|
2069
|
-
? 'Effort routing on one model: no saving is estimated; cost uses configured API rates.'
|
|
2070
|
-
: `${uncomparedText(cost).replace(/^./, (c) => c.toUpperCase())}: no saving is shown.`,
|
|
2071
|
-
);
|
|
2072
|
-
}
|
|
2073
|
-
|
|
2074
|
-
// The period's cost as one table that adds up: the main thread with the parts of its difference, the subagents and the
|
|
2075
|
-
// total. "No routing" is your model on the main thread and Claude Code's pick for each subagent; zero rows are left out.
|
|
2076
|
-
function costTable(ui, config, view, period, cost, legacy, actions, columns) {
|
|
2077
|
-
if (!cost.replies && !cost.agents) return emptyCost(ui, config, view, period);
|
|
2078
|
-
const compared = cost.diffUsd !== null;
|
|
2079
|
-
const r = cost.readout;
|
|
2080
|
-
// All products use configured-rate estimates, including the credits subtotal.
|
|
2081
|
-
const row = (label, amount, diff, base, style = {}, billing = cost.billing) =>
|
|
2082
|
-
ui.line(
|
|
2083
|
-
ui.cell(COST_LABEL, ui.text(label, style)),
|
|
2084
|
-
ui.text(amount === null ? ''.padStart(COST_AMOUNT) : costText(amount, billing).padStart(COST_AMOUNT), style),
|
|
2085
|
-
compared
|
|
2086
|
-
? diff === null
|
|
2087
|
-
? ui.text(''.padStart(COST_DIFF))
|
|
2088
|
-
: ui.text(signedUsd(diff).padStart(COST_DIFF), {
|
|
2089
|
-
...style,
|
|
2090
|
-
...(savingsColor(diff) ? { color: savingsColor(diff) } : {}),
|
|
2091
|
-
})
|
|
2092
|
-
: null,
|
|
2093
|
-
compared && base !== null ? ui.dim(costText(base, billing).padStart(COST_BASE)) : null,
|
|
2094
|
-
);
|
|
2095
|
-
const part = (label, value, note = null) =>
|
|
2096
|
-
value
|
|
2097
|
-
? ui.line(
|
|
2098
|
-
ui.cell(COST_LABEL, ui.dim(` ${label}`)),
|
|
2099
|
-
''.padStart(COST_AMOUNT),
|
|
2100
|
-
ui.text(signedUsd(value).padStart(COST_DIFF), savingsColor(value) ? { color: savingsColor(value) } : {}),
|
|
2101
|
-
note ? ui.dim(` ${note}`) : null,
|
|
2102
|
-
)
|
|
2103
|
-
: null;
|
|
2104
|
-
// The rebuilds' share of what the cheaper models saved; past all of it the switches lost money, which lostLine says.
|
|
2105
|
-
const share = r.switchUsd > 0 && -r.cheaperUsd > 0 ? r.switchUsd / -r.cheaperUsd : null;
|
|
2106
|
-
const switches = share !== null && share <= 1 ? `${percent(share)} of the saving` : null;
|
|
2107
|
-
const total =
|
|
2108
|
-
period === 'session'
|
|
2109
|
-
? 'session'
|
|
2110
|
-
: { today: 'today', '7d': '7 days', '30d': '30 days', month: 'month', all: 'all' }[period];
|
|
2111
|
-
const out = [
|
|
2112
|
-
costHeadline(ui, cost),
|
|
2113
|
-
ui.text(' '),
|
|
2114
|
-
ui.line(
|
|
2115
|
-
ui.cell(COST_LABEL, ''),
|
|
2116
|
-
ui.dim('cost'.padStart(COST_AMOUNT)),
|
|
2117
|
-
compared ? ui.dim(`vs ${UNROUTED}`.padStart(COST_DIFF)) : null,
|
|
2118
|
-
compared ? ui.dim(UNROUTED.padStart(COST_BASE)) : null,
|
|
2119
|
-
),
|
|
2120
|
-
row(
|
|
2121
|
-
`main · ${plural(cost.replies, 'reply', 'replies')}`,
|
|
2122
|
-
cost.mainUsd,
|
|
2123
|
-
cost.mainDiff,
|
|
2124
|
-
compared ? r.yoursUsd : null,
|
|
2125
|
-
),
|
|
2126
|
-
compared ? part('cheaper models', r.cheaperUsd) : null,
|
|
2127
|
-
compared ? part('stronger models', r.strongerUsd) : null,
|
|
2128
|
-
compared ? part('cache rebuilds', r.switchUsd, switches) : null,
|
|
2129
|
-
cost.agents
|
|
2130
|
-
? row(
|
|
2131
|
-
`agents · ${cost.agents}`,
|
|
2132
|
-
cost.agentUsd,
|
|
2133
|
-
compared ? cost.agentDiff : null,
|
|
2134
|
-
compared ? cost.agentBaseUsd : null,
|
|
2135
|
-
)
|
|
2136
|
-
: null,
|
|
2137
|
-
ui.dim('─'.repeat(COST_LABEL + COST_AMOUNT + (compared ? COST_DIFF + COST_BASE : 0))),
|
|
2138
|
-
row(`${total} · ${API_ESTIMATE}`, cost.totalUsd, cost.diffUsd, cost.baseUsd, { bold: true }),
|
|
2139
|
-
cost.billing === 'mixed' ? row(' credits estimate', cost.creditsUsd, null, null, {}, 'billed') : null,
|
|
2140
|
-
].filter(Boolean);
|
|
2141
|
-
if (legacy?.replies)
|
|
2142
|
-
out.push(
|
|
2143
|
-
...dimLines(
|
|
2144
|
-
ui,
|
|
2145
|
-
`includes ${usd(legacy.routedUsd)} routed before dated records, kept without a breakdown`,
|
|
2146
|
-
columns,
|
|
2147
|
-
),
|
|
2148
|
-
);
|
|
2149
|
-
if (compared && !r.early && switchesLost(r)) out.push(lostLine(ui, actions));
|
|
2150
|
-
return out;
|
|
2151
|
-
}
|
|
2152
|
-
// The cost table's columns: label, cost, difference, no routing.
|
|
2153
|
-
const COST_LABEL = 26;
|
|
2154
|
-
const COST_AMOUNT = 8;
|
|
2155
|
-
const COST_DIFF = 15;
|
|
2156
|
-
const COST_BASE = 12;
|
|
2157
|
-
|
|
2158
|
-
function helpLines(ui) {
|
|
2159
|
-
return [
|
|
2160
|
-
ui.head('ABOUT THESE NUMBERS'),
|
|
2161
|
-
ui.dim('≈$ is tokens × configured API rates in router.json (list/custom).'),
|
|
2162
|
-
ui.dim('These are estimates, not invoices or measured plan-quota savings.'),
|
|
2163
|
-
ui.dim('No routing: your model on the main thread, Claude Code’s pick per agent,'),
|
|
2164
|
-
ui.dim('same tokens and output length. Answer quality is not measured.'),
|
|
2165
|
-
ui.dim('For the whole session cost, with side calls such as titles and compaction'),
|
|
2166
|
-
ui.dim('that the router does not see, run /cost.'),
|
|
2167
|
-
ui.dim('Plan bars are Claude Code’s 5-hour and 7-day readings. Tiers show as'),
|
|
2168
|
-
ui.dim('bars ▂▄▆█, low to high; a route never moves past 80% of its window.'),
|
|
2169
|
-
ui.text(' '),
|
|
2170
|
-
];
|
|
68
|
+
actions,
|
|
69
|
+
modelOptions: opts.modelOptions ?? Object.keys(config.models),
|
|
70
|
+
columns: opts.columns ?? null,
|
|
71
|
+
now: opts.now ?? Date.now(),
|
|
72
|
+
surface: opts.surface,
|
|
73
|
+
pluginId: opts.pluginId ?? 'router',
|
|
74
|
+
};
|
|
75
|
+
const graphical = isGraphical(ctx.surface);
|
|
76
|
+
const ui = graphical ? graphicalKit(elements) : kit(elements);
|
|
77
|
+
const tab = TABS.find(({ id }) => id === view.tab) ?? NOW_TAB;
|
|
78
|
+
const content = tab.draw(ui, ctx);
|
|
79
|
+
const frame = frameOf(ctx, tab.id);
|
|
80
|
+
return graphical
|
|
81
|
+
? graphicalFrame(ui, frame, TABS, tab.id, actions, content)
|
|
82
|
+
: terminalFrame(ui, frame, TABS, tab.id, actions, content, ctx.columns);
|
|
2171
83
|
}
|