@alexeiled/claude-router 1.24.0 → 1.25.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/CHANGELOG.md +74 -0
  3. package/README.md +55 -7
  4. package/docs/architecture.md +30 -4
  5. package/docs/configuration.md +23 -14
  6. package/docs/evaluation.md +3 -3
  7. package/docs/native-router.md +12 -4
  8. package/docs/router-band.svg +140 -142
  9. package/docs/router-pane-classifier.svg +48 -31
  10. package/docs/router-pane-now.svg +162 -120
  11. package/docs/router-pane-policy.svg +79 -47
  12. package/docs/router-pane-routes.svg +8 -8
  13. package/docs/router-pane-usage-whatif.svg +14 -14
  14. package/docs/router-pane-usage.svg +20 -20
  15. package/docs/routing-modes.md +10 -8
  16. package/docs/user-guide.md +151 -22
  17. package/hooks/native-router.mjs +415 -88
  18. package/lib/activity-stats.mjs +23 -4
  19. package/lib/activity.mjs +7 -5
  20. package/lib/auto-engine.mjs +13 -6
  21. package/lib/auto-route.mjs +10 -4
  22. package/lib/band.mjs +35 -440
  23. package/lib/classifier-client.mjs +12 -8
  24. package/lib/config-file.mjs +7 -2
  25. package/lib/config.mjs +24 -9
  26. package/lib/cost.mjs +5 -1
  27. package/lib/costs.mjs +4 -2
  28. package/lib/decisions.mjs +13 -9
  29. package/lib/display.mjs +71 -20
  30. package/lib/facts.mjs +3 -0
  31. package/lib/failover.mjs +55 -0
  32. package/lib/modes.mjs +8 -4
  33. package/lib/panel.mjs +75 -2167
  34. package/lib/policy.mjs +7 -6
  35. package/lib/replay-trace.mjs +5 -2
  36. package/lib/replay.mjs +7 -1
  37. package/lib/route-line.mjs +46 -0
  38. package/lib/route.mjs +51 -32
  39. package/lib/savings.mjs +14 -3
  40. package/lib/subagents.mjs +41 -11
  41. package/lib/subcommands.mjs +60 -0
  42. package/lib/ui/fallback.mjs +22 -0
  43. package/lib/ui/graphical/band.mjs +256 -0
  44. package/lib/ui/graphical/charts.mjs +163 -0
  45. package/lib/ui/graphical/classifier.mjs +153 -0
  46. package/lib/ui/graphical/frame.mjs +231 -0
  47. package/lib/ui/graphical/kit.mjs +156 -0
  48. package/lib/ui/graphical/meter.mjs +25 -0
  49. package/lib/ui/graphical/now.mjs +357 -0
  50. package/lib/ui/graphical/policy.mjs +56 -0
  51. package/lib/ui/graphical/routes.mjs +264 -0
  52. package/lib/ui/graphical/squares.mjs +29 -0
  53. package/lib/ui/graphical/usage.mjs +376 -0
  54. package/lib/ui/model/band.mjs +441 -0
  55. package/lib/ui/model/classifier.mjs +308 -0
  56. package/lib/ui/model/draft.mjs +62 -0
  57. package/lib/ui/model/format.mjs +41 -0
  58. package/lib/ui/model/frame.mjs +359 -0
  59. package/lib/ui/model/now.mjs +421 -0
  60. package/lib/ui/model/policy.mjs +194 -0
  61. package/lib/ui/model/routes.mjs +392 -0
  62. package/lib/ui/model/usage-agents.mjs +311 -0
  63. package/lib/ui/model/usage-breakdown.mjs +237 -0
  64. package/lib/ui/model/usage-shared.mjs +58 -0
  65. package/lib/ui/model/usage.mjs +330 -0
  66. package/lib/ui/terminal/band.mjs +253 -0
  67. package/lib/ui/terminal/classifier.mjs +150 -0
  68. package/lib/ui/terminal/frame.mjs +169 -0
  69. package/lib/ui/terminal/kit.mjs +162 -0
  70. package/lib/ui/terminal/now.mjs +293 -0
  71. package/lib/ui/terminal/policy.mjs +55 -0
  72. package/lib/ui/terminal/routes.mjs +167 -0
  73. package/lib/ui/terminal/usage.mjs +363 -0
  74. package/lib/ui/terminal/wrap.mjs +37 -0
  75. package/lib/ui/tokens.mjs +106 -0
  76. package/lib/usage.mjs +5 -5
  77. package/lib/view.mjs +5 -2
  78. package/package.json +4 -2
  79. package/types/index.d.ts +17 -0
@@ -0,0 +1,357 @@
1
+ // The Now tab as cards for desktop, VS Code and mobile, drawn from the model in ui/model/now.mjs: Next turn; Plan usage
2
+ // and Context side by side when the pane is wide, stacked when narrow; This session; Recent decisions.
3
+ import { isFiniteNumber } from '../tokens.mjs';
4
+
5
+ // Two cards of label 10 + bar 10 + figure 7 + note + gaps, plus border and padding, need about 55 cells each.
6
+ const SIDE_BY_SIDE = 110;
7
+
8
+ // A reading's tone on a card: its own where it warns, else the card's ordinary one.
9
+ const alertOr = (name, ordinary) => (name === 'warning' || name === 'danger' ? name : ordinary);
10
+
11
+ // The routing mode as one label: "Cost mode from session · ... · /router mode default clears it".
12
+ const modeWords = (ui, mode) => (mode ? [ui.say(mode.from), mode.clear].filter(Boolean).join(' · ') : null);
13
+
14
+ const figure = (percent) => (Number.isFinite(percent) ? `${percent}%` : '—');
15
+
16
+ /**
17
+ * @param {any} ui the graphical kit
18
+ * @param {import('../model/now.mjs').NowModel} model
19
+ * @param {{ columns: number | null, now: number }} opts
20
+ */
21
+ export function graphicalNow(ui, model, { columns, now }) {
22
+ const { Box } = ui;
23
+ const plan = planCard(ui, model.plan);
24
+ const context = contextCard(ui, model.context);
25
+ const half = (card) => Box({ width: '50%', flexGrow: 1, flexShrink: 1, flexDirection: 'column', children: [card] });
26
+ const pair = !plan
27
+ ? context
28
+ : isFiniteNumber(columns) && columns >= SIDE_BY_SIDE
29
+ ? Box({ flexDirection: 'row', gap: 'lg', alignItems: 'stretch', children: [half(plan), half(context)] })
30
+ : Box({ flexDirection: 'column', gap: 'lg', children: [plan, context] });
31
+ return [
32
+ Box({
33
+ flexDirection: 'column',
34
+ gap: 'lg',
35
+ children: [
36
+ nextCard(ui, model.next),
37
+ pair,
38
+ sessionCard(ui, model.session, columns, now),
39
+ model.decisions.length
40
+ ? ui.section(
41
+ 'Recent decisions',
42
+ {},
43
+ model.decisions.map((line) => ui.text(line, { size: 'footnote', mono: true, wrap: 'truncate-end' })),
44
+ )
45
+ : null,
46
+ ].filter(Boolean),
47
+ }),
48
+ ];
49
+ }
50
+
51
+ /**
52
+ * @param {any} ui
53
+ * @param {import('../model/now.mjs').NextModel} next
54
+ */
55
+ // The tier meter and the route, the activity reading at the right, the why line, then the pins.
56
+ function nextCard(ui, next) {
57
+ const { Box } = ui;
58
+ const routed = next.state === 'routed';
59
+ const meter = ui.tierMeter(routed ? next.tier : null, routed && next.tier ? 'lit' : 'off');
60
+ const reading = routed ? next.reading : null;
61
+ const headline = Box({
62
+ flexDirection: 'row',
63
+ flexWrap: 'nowrap',
64
+ alignItems: 'center',
65
+ justifyContent: 'space-between',
66
+ gap: 'md',
67
+ children: [
68
+ Box({
69
+ flexDirection: 'row',
70
+ alignItems: 'center',
71
+ gap: 'sm',
72
+ flexShrink: 1,
73
+ overflow: 'hidden',
74
+ children: [
75
+ Box({ flexShrink: 0, children: [meter] }),
76
+ ui.text(next.label, { size: 'heading', weight: 'semibold', wrap: 'truncate-end' }),
77
+ ],
78
+ }),
79
+ reading
80
+ ? Box({
81
+ flexShrink: 0,
82
+ children: [
83
+ ui.text([reading.choice, reading.share].filter(Boolean).join(' '), {
84
+ size: 'footnote',
85
+ weight: 'medium',
86
+ tabular: true,
87
+ color: ui.color(reading.isActivity ? 'activity' : 'muted'),
88
+ }),
89
+ ],
90
+ })
91
+ : null,
92
+ ].filter(Boolean),
93
+ });
94
+ if (!routed) {
95
+ const unavailable = next.state === 'unavailable';
96
+ return ui.section('Next turn', {}, [
97
+ Box({
98
+ flexDirection: 'column',
99
+ gap: 'xs',
100
+ children: [
101
+ headline,
102
+ unavailable
103
+ ? ui.text(ui.say(next.note), { color: ui.color('warning'), wrap: 'truncate-end' })
104
+ : ui.muted(ui.say(next.note)),
105
+ ui.muted(ui.say(next.pin), { size: 'footnote' }),
106
+ ],
107
+ }),
108
+ ]);
109
+ }
110
+ // The why, the classifier's support, an override and the routing mode fold into one wrapping line.
111
+ const why = [next.why, next.support, reading?.other, next.phase, next.override, modeWords(ui, next.mode)].filter(
112
+ Boolean,
113
+ );
114
+ return ui.section('Next turn', {}, [
115
+ Box({
116
+ flexDirection: 'column',
117
+ gap: 'xs',
118
+ children: [
119
+ headline,
120
+ next.tier
121
+ ? why.length
122
+ ? ui.muted(why.join(' · '), { tabular: true, wrap: 'wrap' })
123
+ : null
124
+ : ui.muted(next.firstTurn),
125
+ ].filter(Boolean),
126
+ }),
127
+ Box({
128
+ flexDirection: 'column',
129
+ gap: 'sm',
130
+ children: [
131
+ ui.muted(ui.say(next.pinHead), { size: 'footnote', wrap: 'wrap' }),
132
+ pinRow(ui, next),
133
+ ui.muted(ui.say(next.pinNote), { size: 'footnote' }),
134
+ ],
135
+ }),
136
+ ]);
137
+ }
138
+
139
+ /**
140
+ * @param {any} ui
141
+ * @param {import('../model/now.mjs').NextRoutedModel} next
142
+ */
143
+ // The pins: per tier, its meter beside a Button named by its route, then its reading (`current` on the tier that runs
144
+ // now, the classifier's support). The meter stays outside the Button, which holds Text alone; only a pending pin is
145
+ // primary. A group may shrink, its label cut, so a long model name never makes it wider than the card.
146
+ function pinRow(ui, next) {
147
+ const { Box, Button } = ui;
148
+ return Box({
149
+ flexDirection: 'row',
150
+ flexWrap: 'wrap',
151
+ alignItems: 'center',
152
+ justifyContent: 'flex-start',
153
+ gap: 'md',
154
+ children: next.pins.map((pin) =>
155
+ Box({
156
+ flexDirection: 'row',
157
+ alignItems: 'center',
158
+ gap: 'xs',
159
+ flexShrink: 1,
160
+ minWidth: 0,
161
+ children: [
162
+ Box({ flexShrink: 0, children: [ui.tierMeter(pin.tier, 'lit')] }),
163
+ Button({
164
+ key: `pin-${pin.tier}`,
165
+ label: pin.route,
166
+ children: [ui.text(pin.route, { wrap: 'truncate-end' })],
167
+ variant: next.pendingPin === pin.tier ? 'primary' : 'secondary',
168
+ onPress: () => next.pick(pin.tier),
169
+ }),
170
+ pin.reading ? ui.muted(pin.reading, { size: 'footnote', tabular: true }) : null,
171
+ ].filter(Boolean),
172
+ }),
173
+ ),
174
+ });
175
+ }
176
+
177
+ /**
178
+ * @param {any} ui
179
+ * @param {import('../model/now.mjs').NowModel['plan']} plan
180
+ */
181
+ function planCard(ui, plan) {
182
+ if (!plan.length) return null;
183
+ return ui.section(
184
+ 'Plan usage',
185
+ {},
186
+ plan.map((limit) =>
187
+ ui.meter({
188
+ label: limit.label,
189
+ value: limit.percent / 100,
190
+ tone: limit.tone,
191
+ text: figure(limit.percent),
192
+ caption: limit.reset ? `resets ${limit.reset}` : undefined,
193
+ }),
194
+ ),
195
+ );
196
+ }
197
+
198
+ /**
199
+ * @param {any} ui
200
+ * @param {import('../model/now.mjs').ContextModel} context
201
+ */
202
+ function contextCard(ui, context) {
203
+ const { Box } = ui;
204
+ const toggle = ui.Button({
205
+ key: 'usage-detail',
206
+ label: context.detail.open ? 'Less' : 'Details',
207
+ variant: context.detail.open ? 'primary' : 'secondary',
208
+ onPress: context.detail.toggle,
209
+ });
210
+ const { cache } = context;
211
+ // The last reply's figure in the cache's tone, then its words; the session's after a muted dot.
212
+ const reuse = cache.last
213
+ ? [
214
+ ui.text(cache.last.figure, { tabular: true, color: ui.color(alertOr(cache.tone, 'primary')) }),
215
+ ui.text(cache.last.words),
216
+ ...(cache.session
217
+ ? [ui.muted('·'), ui.text(cache.session.figure, { tabular: true }), ui.text(cache.session.words)]
218
+ : []),
219
+ ]
220
+ : [ui.text(ui.say(cache.empty))];
221
+ return ui.section('Context', { action: toggle }, [
222
+ context.fill === null
223
+ ? ui.muted(ui.say(context.empty))
224
+ : ui.meter({
225
+ label: 'Window',
226
+ value: context.fill / 100,
227
+ tone: alertOr(context.fillTone, 'accent'),
228
+ text: figure(context.fill),
229
+ caption: context.suffix,
230
+ }),
231
+ Box({
232
+ flexDirection: 'row',
233
+ flexWrap: 'nowrap',
234
+ alignItems: 'center',
235
+ gap: 'md',
236
+ children: [
237
+ Box({ width: 10, flexShrink: 0, children: [ui.muted('Cache')] }),
238
+ Box({
239
+ flexDirection: 'row',
240
+ alignItems: 'center',
241
+ gap: 'xs',
242
+ flexShrink: 1,
243
+ overflow: 'hidden',
244
+ children: reuse,
245
+ }),
246
+ ],
247
+ }),
248
+ ...(context.detail.open ? readingDetail(ui, context.detail) : []),
249
+ ]);
250
+ }
251
+
252
+ /**
253
+ * @param {any} ui
254
+ * @param {import('../model/now.mjs').ContextModel['detail']} detail
255
+ */
256
+ // Behind Details: Claude's readings of the last reply, the input per reply as a trend, and the estimates the last
257
+ // decision weighed.
258
+ function readingDetail(ui, detail) {
259
+ const heading = (words) => ui.muted(words, { size: 'footnote', weight: 'semibold' });
260
+ return [
261
+ ui.muted(ui.say(detail.readings), { tabular: true, wrap: 'wrap' }),
262
+ heading(ui.say(detail.input)),
263
+ ...(detail.chart
264
+ ? [ui.trend(detail.chart), ui.muted(ui.say(detail.range), { tabular: true })]
265
+ : [ui.muted(ui.say(detail.empty))]),
266
+ heading(ui.say(detail.estimatesHead)),
267
+ ...detail.estimates.map(([label, value]) => ui.field(label, [value])),
268
+ ];
269
+ }
270
+
271
+ /**
272
+ * @param {any} ui
273
+ * @param {import('../model/now.mjs').SessionModel} session
274
+ * @param {number | null} columns
275
+ * @param {number} now
276
+ */
277
+ // The total and how it is paid, the difference against no routing, the main thread and the subagents on one scale,
278
+ // then the replies by tier.
279
+ function sessionCard(ui, session, columns, now) {
280
+ const toggle = ui.Button({
281
+ key: 'band-detail-toggle',
282
+ label: ui.say(session.detailToggle),
283
+ onPress: session.toggleDetail,
284
+ });
285
+ const running = session.running
286
+ ? ui.text(`● ${session.running} running`, { color: ui.color('warning'), size: 'footnote', tabular: true })
287
+ : null;
288
+ const strip = ui.strip(session.strip, { columns, now, since: session.since });
289
+ if (!session.priced)
290
+ return ui.section(
291
+ 'This session',
292
+ { hint: session.head || undefined, action: toggle },
293
+ [running, ui.muted(ui.say(session.empty)), strip].filter(Boolean),
294
+ );
295
+ const { Box } = ui;
296
+ const { diff, scale } = session;
297
+ return ui.section(
298
+ 'This session',
299
+ { hint: session.head || undefined, action: toggle },
300
+ [
301
+ Box({
302
+ flexDirection: 'row',
303
+ alignItems: 'flex-start',
304
+ justifyContent: 'space-between',
305
+ gap: 'md',
306
+ children: [
307
+ ui.stat({ value: session.total, caption: session.billing }),
308
+ Box({
309
+ flexDirection: 'row',
310
+ gap: 'sm',
311
+ alignItems: 'center',
312
+ flexShrink: 0,
313
+ children: [running, ui.Button({ key: 'usage-session', label: 'Usage', onPress: session.usage })].filter(
314
+ Boolean,
315
+ ),
316
+ }),
317
+ ],
318
+ }),
319
+ diff
320
+ ? Box({
321
+ flexDirection: 'row',
322
+ flexWrap: 'wrap',
323
+ gap: 'xs',
324
+ children: [
325
+ ui.text(diff.text, { tabular: true, color: ui.color(diff.tone) }),
326
+ ui.muted(`vs ${diff.versus} ${diff.of}`, { tabular: true }),
327
+ ],
328
+ })
329
+ : null,
330
+ session.note ? ui.muted(session.note, { size: 'footnote', wrap: 'wrap' }) : null,
331
+ Box({
332
+ flexDirection: 'column',
333
+ gap: 'sm',
334
+ children: session.parts.map((part) =>
335
+ ui.meter({
336
+ label: part.name === 'main' ? 'Main' : 'Agents',
337
+ value: scale > 0 ? part.usd / scale : 0,
338
+ tone: 'accent',
339
+ text: part.amount,
340
+ caption: part.against ? `${part.against.text} vs ${part.against.versus}` : undefined,
341
+ }),
342
+ ),
343
+ }),
344
+ session.effortMix.length
345
+ ? ui.muted(`Effort ${session.effortMix.map(([effort, n]) => `${effort} ${n}`).join(' · ')}`, { tabular: true })
346
+ : null,
347
+ session.lost
348
+ ? ui.notice(
349
+ 'warning',
350
+ 'Switches cost more than they saved',
351
+ ui.Button({ key: 'session-policy', label: 'Policy', onPress: session.policy }),
352
+ )
353
+ : null,
354
+ strip,
355
+ ].filter(Boolean),
356
+ );
357
+ }
@@ -0,0 +1,56 @@
1
+ // The Policy tab as a form for desktop, VS Code and mobile, drawn from the model in ui/model/policy.mjs: one card per
2
+ // group (Routing mode, Switching down, Switching up, Activities, Credits), each field its label, its Select and one
3
+ // muted help line; then Reset on its own row, the settings file in its own card, and the subagent line.
4
+
5
+ /**
6
+ * @param {any} ui the graphical kit
7
+ * @param {import('../model/policy.mjs').PolicyModel} model
8
+ */
9
+ export function graphicalPolicy(ui, model) {
10
+ const { Box } = ui;
11
+ const field = (one) =>
12
+ ui.field(
13
+ one.label,
14
+ [
15
+ ui.select(one.control),
16
+ one.changed ? ui.text(model.unsaved.glyph, { color: ui.color(model.unsaved.tone) }) : null,
17
+ one.default ? ui.muted(ui.say(one.default), { tabular: true }) : null,
18
+ ],
19
+ { help: one.hint ? ui.say(one.hint) : one.help },
20
+ );
21
+ const { file } = model;
22
+ return [
23
+ Box({
24
+ flexDirection: 'column',
25
+ gap: 'lg',
26
+ children: [
27
+ ...model.groups.map((group) =>
28
+ ui.section(group.title, {}, [
29
+ ...group.fields.map(field),
30
+ ...group.warnings.map((warning) => ui.notice('warning', warning)),
31
+ ...group.notes.map((note) => ui.muted(note, { wrap: 'wrap' })),
32
+ ]),
33
+ ),
34
+ Box({
35
+ flexDirection: 'row',
36
+ children: [ui.Button({ key: 'reset-policy', label: model.reset.label, onPress: model.reset.press })],
37
+ }),
38
+ ui.section(file.title, {}, [
39
+ Box({
40
+ flexDirection: 'row',
41
+ flexWrap: 'wrap',
42
+ alignItems: 'center',
43
+ gap: 'sm',
44
+ children: [
45
+ ui.muted(file.path, { wrap: 'wrap' }),
46
+ file.copy
47
+ ? ui.Button({ key: 'copy-path', label: ui.say(file.copy.label), onPress: file.copy.press })
48
+ : null,
49
+ ].filter(Boolean),
50
+ }),
51
+ ui.muted(ui.say(model.subagents), { size: 'footnote', wrap: 'wrap' }),
52
+ ]),
53
+ ],
54
+ }),
55
+ ];
56
+ }
@@ -0,0 +1,264 @@
1
+ // The Routes tab as cards for desktop, VS Code and mobile, drawn from the model in ui/model/routes.mjs: Routes (the
2
+ // activity-routing mode in its header, the tier × activity grid, its warnings and Reset), the selected cell's editor
3
+ // directly under it, and Prompt caches.
4
+ import { isFiniteNumber } from '../tokens.mjs';
5
+
6
+ // The row-label column: "Default" and the longest activity name.
7
+ const ROW_LABEL = 12;
8
+ // The grid needs this many columns; a narrower pane, or one that reports no width, lists each row's tiers instead.
9
+ const WIDE = 80;
10
+
11
+ /**
12
+ * @param {any} ui the graphical kit
13
+ * @param {import('../model/routes.mjs').RoutesModel} model
14
+ * @param {{ columns: number | null }} opts
15
+ */
16
+ export function graphicalRoutes(ui, model, { columns }) {
17
+ return [
18
+ ui.Box({
19
+ flexDirection: 'column',
20
+ gap: 'lg',
21
+ children: [
22
+ routesCard(ui, model, columns),
23
+ model.editor ? editorCard(ui, model.editor, model.unsaved) : null,
24
+ ui.section('Prompt caches', {}, [ui.muted(ui.say(model.cache.text), { wrap: 'wrap' })]),
25
+ ].filter(Boolean),
26
+ }),
27
+ ];
28
+ }
29
+
30
+ /**
31
+ * @param {any} ui
32
+ * @param {import('../model/routes.mjs').RouteMark} mark
33
+ */
34
+ const markText = (ui, mark) => ui.text(mark.card ?? mark.glyph, { color: ui.color(mark.tone) });
35
+
36
+ /**
37
+ * @param {any} ui
38
+ * @param {import('../model/routes.mjs').RoutesModel} model
39
+ * @param {number | null} columns
40
+ */
41
+ function routesCard(ui, model, columns) {
42
+ const wide = isFiniteNumber(columns) && columns >= WIDE;
43
+ const { Box } = ui;
44
+ const { mode, overrides } = model;
45
+ const control = Box({
46
+ flexDirection: 'row',
47
+ alignItems: 'center',
48
+ gap: 'xs',
49
+ flexShrink: 0,
50
+ children: [
51
+ ui.Select({
52
+ key: 'activity-mode',
53
+ label: mode.label,
54
+ value: mode.value,
55
+ options: mode.options.map((option) => ({ value: option.value, label: ui.say(option.label) })),
56
+ onSelect: mode.pick,
57
+ }),
58
+ mode.changed ? markText(ui, model.unsaved) : null,
59
+ ].filter(Boolean),
60
+ });
61
+ return ui.section('Routes', { hint: ui.say(mode.hint), action: control }, [
62
+ overrides
63
+ ? Box({
64
+ flexDirection: 'row',
65
+ flexWrap: 'wrap',
66
+ gap: 'xs',
67
+ children: [
68
+ ui.text(overrides.text, { color: ui.color(overrides.warning ? 'warning' : 'muted') }),
69
+ overrides.after ? ui.muted(`· ${overrides.after}`) : null,
70
+ ].filter(Boolean),
71
+ })
72
+ : null,
73
+ Box({
74
+ key: 'routes-grid',
75
+ flexDirection: 'column',
76
+ gap: wide ? 'xs' : 'sm',
77
+ children: wide ? gridRows(ui, model) : listRows(ui, model),
78
+ }),
79
+ ...model.steps.map((step) => ui.notice('warning', ui.say(step))),
80
+ Box({
81
+ flexDirection: 'row',
82
+ alignItems: 'flex-start',
83
+ justifyContent: 'space-between',
84
+ gap: 'md',
85
+ children: [
86
+ Box({
87
+ flexDirection: 'column',
88
+ gap: 'xs',
89
+ flexShrink: 1,
90
+ children: [
91
+ ui.muted(model.legend.map((mark) => `${mark.card ?? mark.glyph} ${mark.text}`).join(' · '), {
92
+ size: 'footnote',
93
+ wrap: 'wrap',
94
+ }),
95
+ model.hint ? ui.muted(model.hint, { size: 'footnote', wrap: 'wrap' }) : null,
96
+ ui.muted(model.note, { size: 'footnote', wrap: 'wrap' }),
97
+ ].filter(Boolean),
98
+ }),
99
+ Box({
100
+ flexShrink: 0,
101
+ children: [
102
+ ui.Button({ key: 'reset-routes', label: model.reset.label, hotkey: 'r', onPress: model.reset.press }),
103
+ ],
104
+ }),
105
+ ],
106
+ }),
107
+ ]);
108
+ }
109
+
110
+ /**
111
+ * @param {any} ui
112
+ * @param {import('../model/routes.mjs').GridCell} cell
113
+ * @param {object | null} icon
114
+ */
115
+ // A cell's Button: its route (a muted dash where it runs the tier's model), then its marks; the selected cell is
116
+ // primary. `icon` leads it where no header names the tier.
117
+ const cellButton = (ui, cell, icon = null) =>
118
+ ui.Button({
119
+ key: cell.key,
120
+ variant: cell.selected ? 'primary' : 'secondary',
121
+ onPress: cell.press,
122
+ children: [
123
+ icon,
124
+ ui.text(cell.label, {
125
+ wrap: 'truncate-end',
126
+ tabular: true,
127
+ ...(cell.muted ? { color: ui.color('muted') } : {}),
128
+ }),
129
+ ...cell.marks.map((mark) => markText(ui, mark)),
130
+ ].filter(Boolean),
131
+ });
132
+
133
+ /**
134
+ * @param {any} ui
135
+ * @param {import('../model/routes.mjs').GridRow} row
136
+ */
137
+ // The tier routes' row in the heavier weight, an activity in its colour; wrapped, never cut.
138
+ const rowLabel = (ui, row) =>
139
+ row.kind === 'tier'
140
+ ? ui.text(ui.say(row.label), { weight: 'semibold', wrap: 'wrap' })
141
+ : ui.text(row.label, { color: ui.color('activity'), wrap: 'wrap' });
142
+
143
+ /**
144
+ * @param {any} ui
145
+ * @param {import('../model/routes.mjs').RoutesModel} model
146
+ */
147
+ // The grid: a header of the tiers, then a row per tier route and activity, each a fixed label column and one equal
148
+ // column per tier.
149
+ function gridRows(ui, model) {
150
+ const { Box } = ui;
151
+ const share = `${100 / model.tiers.length}%`;
152
+ const row = (key, label, columns) =>
153
+ Box({
154
+ key,
155
+ flexDirection: 'row',
156
+ flexWrap: 'nowrap',
157
+ alignItems: 'center',
158
+ children: [
159
+ Box({ width: ROW_LABEL, flexShrink: 0, children: label ? [label] : [] }),
160
+ Box({
161
+ flexDirection: 'row',
162
+ flexWrap: 'nowrap',
163
+ flexGrow: 1,
164
+ flexShrink: 1,
165
+ children: columns.map((children) =>
166
+ Box({ width: share, flexShrink: 1, overflow: 'hidden', paddingRight: 'xs', children }),
167
+ ),
168
+ }),
169
+ ],
170
+ });
171
+ return [
172
+ row(
173
+ 'grid-head',
174
+ null,
175
+ model.tiers.map(({ id: tier, label }) => [
176
+ Box({
177
+ flexDirection: 'row',
178
+ alignItems: 'center',
179
+ gap: 'xs',
180
+ children: [
181
+ Box({ flexShrink: 0, children: [ui.tierMeter(tier, 'lit')] }),
182
+ ui.text(ui.say(label), {
183
+ size: 'footnote',
184
+ weight: 'semibold',
185
+ color: ui.color(`tier:${tier}`),
186
+ wrap: 'truncate-end',
187
+ }),
188
+ ],
189
+ }),
190
+ ]),
191
+ ),
192
+ ...model.rows.map((one) =>
193
+ row(
194
+ `grid-${one.id}`,
195
+ rowLabel(ui, one),
196
+ one.cells.map((cell) => [cellButton(ui, cell)]),
197
+ ),
198
+ ),
199
+ ];
200
+ }
201
+
202
+ /**
203
+ * @param {any} ui
204
+ * @param {import('../model/routes.mjs').RoutesModel} model
205
+ */
206
+ // A narrow pane: each row is its label over its tiers' Buttons, wrapped, each Button led by its tier's bars.
207
+ function listRows(ui, model) {
208
+ const { Box } = ui;
209
+ return model.rows.map((one) =>
210
+ Box({
211
+ key: `grid-${one.id}`,
212
+ flexDirection: 'column',
213
+ gap: 'xs',
214
+ children: [
215
+ rowLabel(ui, one),
216
+ Box({
217
+ flexDirection: 'row',
218
+ flexWrap: 'wrap',
219
+ gap: 'xs',
220
+ children: one.cells.map((cell) => cellButton(ui, cell, ui.tierMeter(cell.tier, 'lit', { inButton: true }))),
221
+ }),
222
+ ],
223
+ }),
224
+ );
225
+ }
226
+
227
+ /**
228
+ * @param {any} ui
229
+ * @param {import('../model/routes.mjs').RouteEditor} editor
230
+ * @param {import('../model/routes.mjs').RouteMark} unsaved
231
+ */
232
+ // The selected cell's editor: its model and effort, then its prices or its state and the ways back, then its warnings.
233
+ function editorCard(ui, editor, unsaved) {
234
+ const { Box } = ui;
235
+ const { key, model, effort } = editor.choices;
236
+ const select = (id, choice) =>
237
+ ui.Select({ key: `${key}-${id}`, value: choice.value, options: choice.options, onSelect: choice.pick });
238
+ const children = [
239
+ ui.field('Model', [select('model', model), editor.changed ? markText(ui, unsaved) : null]),
240
+ ui.field('Effort', [effort ? select('effort', effort) : ui.muted('none')]),
241
+ ];
242
+ if (editor.kind === 'tier') {
243
+ children.push(ui.muted(editor.rates.graphical, { tabular: true, wrap: 'wrap' }));
244
+ } else {
245
+ const { remove, restore } = editor;
246
+ children.push(ui.muted(ui.say(editor.state)));
247
+ if (remove || restore)
248
+ children.push(
249
+ Box({
250
+ flexDirection: 'row',
251
+ flexWrap: 'wrap',
252
+ alignItems: 'center',
253
+ gap: 'sm',
254
+ children: [
255
+ remove ? ui.Button({ key: remove.key, label: remove.label.graphical, onPress: remove.press }) : null,
256
+ remove ? ui.muted(remove.note, { size: 'footnote' }) : null,
257
+ restore ? ui.Button({ key: restore.key, label: restore.label, onPress: restore.press }) : null,
258
+ ].filter(Boolean),
259
+ }),
260
+ );
261
+ children.push(...editor.warnings.map((warning) => ui.notice('warning', ui.say(warning))));
262
+ }
263
+ return ui.section(editor.title, {}, children);
264
+ }