@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,293 @@
1
+ // The Now tab as terminal rows: NEXT TURN, RECENT DECISIONS, PLAN, SESSION, CONTEXT, each in a box from PANEL_COLUMNS
2
+ // and a run of lines under the pane below it, drawn from the model in ui/model/now.mjs. The colours are the ones the
3
+ // terminal has always drawn for each reading, derived here from the model's numbers.
4
+ import { gaugeCells, sparkline } from '../../display.mjs';
5
+ import { CHIP_BACKGROUND, isFiniteNumber } from '../tokens.mjs';
6
+ import { dimLines } from './wrap.mjs';
7
+
8
+ const SESSION_BAR = 16;
9
+ // A cost bar's label and its amount, before the bar.
10
+ const PART_LABEL = 8;
11
+ const AMOUNT = 7;
12
+ const ESTIMATE_LABEL = 22;
13
+
14
+ /**
15
+ * @param {any} ui the terminal kit
16
+ * @param {import('../model/now.mjs').NowModel} model
17
+ * @param {{ columns: number | null, now: number }} opts
18
+ */
19
+ export function terminalNow(ui, model, { columns, now }) {
20
+ // Each section stacks full width: from PANEL_COLUMNS in a box, so its lines fit the box's inner width.
21
+ const room = ui.inner(columns);
22
+ return [
23
+ ...ui.panel(columns, nextLines(ui, model.next, room)),
24
+ ...(model.decisions.length
25
+ ? ui.panel(columns, [ui.head('RECENT DECISIONS'), ...model.decisions.map((line) => ui.text(line))])
26
+ : []),
27
+ ...(model.plan.length ? ui.panel(columns, planLines(ui, model.plan)) : []),
28
+ ...ui.panel(columns, sessionLines(ui, model.session, room, now)),
29
+ ...ui.panel(columns, contextLines(ui, model.context), false),
30
+ ];
31
+ }
32
+
33
+ /**
34
+ * @param {any} ui
35
+ * @param {import('../model/now.mjs').NextModel} next
36
+ * @param {number | null} columns the section's own columns
37
+ */
38
+ // NEXT TURN. With routing off or unavailable only the model applies: no tier, activity or reason from an earlier turn.
39
+ function nextLines(ui, next, columns) {
40
+ const out = [ui.head('NEXT TURN')];
41
+ if (next.state !== 'routed') return [...out, ...offLines(ui, next)];
42
+ const { reading } = next;
43
+ out.push(
44
+ ui.line(
45
+ ' ',
46
+ ui.tierMeter(next.tier, next.tier ? 'lit' : 'off'),
47
+ ' ',
48
+ ui.text(next.label, { weight: 'semibold' }),
49
+ reading
50
+ ? ui.line(
51
+ ' ',
52
+ // `uncertain` is an answer, not an activity: it applies none.
53
+ reading.isActivity ? ui.activity(reading.choice) : ui.dim(reading.choice),
54
+ reading.share ? ` ${reading.share}` : '',
55
+ reading.other ? ui.dim(` ${reading.other}`) : null,
56
+ next.phase ? ui.dim(` ${next.phase}`) : null,
57
+ )
58
+ : null,
59
+ ),
60
+ next.why ? ui.line(ui.dim(' why '), next.why, next.support ? ui.dim(` ${next.support}`) : null) : null,
61
+ next.override ? ui.dim(` ${next.override}`) : null,
62
+ next.mode
63
+ ? ui.line(
64
+ ui.dim(' mode '),
65
+ ui.text(` ${next.mode.name} `, { backgroundColor: CHIP_BACKGROUND, color: ui.color('primary') }),
66
+ ui.dim(` ${ui.say(next.mode.from)}`),
67
+ next.mode.clear ? ui.dim(` · ${next.mode.clear}`) : null,
68
+ )
69
+ : null,
70
+ ...pinLines(ui, next, columns),
71
+ );
72
+ return out.filter(Boolean);
73
+ }
74
+
75
+ /**
76
+ * @param {any} ui
77
+ * @param {import('../model/now.mjs').NextOffModel} next
78
+ */
79
+ // Routing off or unavailable: the model that runs, and why nothing else applies.
80
+ function offLines(ui, next) {
81
+ const unavailable = next.state === 'unavailable';
82
+ return [
83
+ ui.line(
84
+ unavailable ? ui.text(' ✕ ', { color: ui.color('warning') }) : ui.dim(' ○ '),
85
+ ui.text(next.label, { weight: 'semibold' }),
86
+ ui.dim(` ${ui.say(next.note)}`),
87
+ ),
88
+ ui.line(ui.dim(' pin '), ui.dim(ui.say(next.pin))),
89
+ ];
90
+ }
91
+
92
+ /**
93
+ * @param {any} ui
94
+ * @param {import('../model/now.mjs').NextRoutedModel} next
95
+ * @param {number | null} columns
96
+ */
97
+ // The pins as one dropdown under what they offer: a tier pins the next turn only, No pin drops a pending pin.
98
+ function pinLines(ui, next, columns) {
99
+ return [
100
+ ...dimLines(ui, ` ${ui.say(next.pinHead)}`, columns),
101
+ ui.line(' ', ui.select(next.pinControl)),
102
+ ...dimLines(ui, ` ${ui.say(next.pinNote)}`, columns),
103
+ ];
104
+ }
105
+
106
+ /**
107
+ * @param {any} ui
108
+ * @param {import('../model/now.mjs').NowModel['plan']} plan
109
+ */
110
+ // PLAN: Claude Code's plan windows, when it reports them.
111
+ function planLines(ui, plan) {
112
+ return [
113
+ ui.line(ui.head('PLAN'), ui.dim(' Claude plan usage')),
114
+ ...plan.map((limit) => {
115
+ const [filled, empty] = gaugeCells(limit.percent / 100, 20);
116
+ const color = ui.color(limit.tone);
117
+ return ui.line(
118
+ ` ${limit.label.padEnd(6)}`,
119
+ ui.text(filled, { color }),
120
+ ui.dim(empty),
121
+ ui.text(` ${String(limit.percent).padStart(3)}%`, { color }),
122
+ limit.reset ? ui.dim(` resets ${limit.reset}`) : null,
123
+ );
124
+ }),
125
+ ];
126
+ }
127
+
128
+ /**
129
+ * @param {any} ui
130
+ * @param {import('../model/now.mjs').SessionModel} session
131
+ * @param {number | null} columns the section's own columns
132
+ * @param {number} now
133
+ */
134
+ // SESSION: one total with the subagents, the difference against no routing, the main thread and the subagents as bars
135
+ // on one scale, then the replies by tier.
136
+ function sessionLines(ui, session, columns, now) {
137
+ const strip = ui.stripRows(session.strip, { columns, now, since: session.since });
138
+ const out = [
139
+ ui.line(
140
+ ui.head('SESSION'),
141
+ session.head ? ui.dim(` ${session.head}`) : null,
142
+ session.running ? ui.text(` ● ${session.running} running`, { color: ui.color('warning') }) : null,
143
+ ' ',
144
+ // The band's second row, on and off, so two rows can be left at any band width.
145
+ ui.Button({ key: 'band-detail-toggle', label: ui.say(session.detailToggle), onPress: session.toggleDetail }),
146
+ ),
147
+ ];
148
+ // Replies without a list price reach the strip but not the usage record: the tiers still show, the cost cannot.
149
+ if (!session.priced) return [...out, ui.dim(` ${ui.say(session.empty)}`), ...strip];
150
+ out.push(
151
+ ui.line(
152
+ ' ',
153
+ ui.text(session.total, { weight: 'semibold' }),
154
+ ui.dim(` ${session.billing} `),
155
+ ui.Button({ key: 'usage-session', label: 'Usage', onPress: session.usage }),
156
+ ),
157
+ );
158
+ const { diff } = session;
159
+ if (diff) {
160
+ const versus = ` vs ${diff.versus}`;
161
+ const of = ` ${diff.of}`;
162
+ const fits = !isFiniteNumber(columns) || 2 + diff.text.length + versus.length + of.length <= columns;
163
+ out.push(
164
+ ui.line(
165
+ ' ',
166
+ ui.text(diff.text, { weight: 'semibold', color: ui.color(diff.tone) }),
167
+ versus,
168
+ fits ? ui.dim(of) : null,
169
+ ),
170
+ );
171
+ }
172
+ if (session.note) out.push(ui.dim(` ${session.note}`));
173
+ out.push(...costBars(ui, session, columns));
174
+ if (session.lost) out.push(ui.lostLine(session.policy));
175
+ out.push(...strip);
176
+ return out;
177
+ }
178
+
179
+ /**
180
+ * @param {any} ui
181
+ * @param {import('../model/now.mjs').SessionModel} session
182
+ * @param {number | null} columns the section's own columns
183
+ */
184
+ // The main thread and the subagents as bars on one scale, each with its difference against no routing where it fits; an
185
+ // effort-only profile shows its effort mix.
186
+ function costBars(ui, session, columns) {
187
+ const { scale } = session;
188
+ const bar = (value) => {
189
+ const filled = scale > 0 ? Math.max(value > 0 ? 1 : 0, Math.round((value / scale) * SESSION_BAR)) : 0;
190
+ return [ui.text('█'.repeat(filled), { color: ui.color('accent') }), ui.dim('░'.repeat(SESSION_BAR - filled))];
191
+ };
192
+ const versus = (against) => {
193
+ if (!against) return null;
194
+ const note = ` vs ${against.versus}`;
195
+ const width = PART_LABEL + AMOUNT + 2 + SESSION_BAR + 2 + against.text.length + note.length;
196
+ if (isFiniteNumber(columns) && width > columns) return null;
197
+ return ui.line(' ', ui.text(against.text, against.tone ? { color: ui.color(against.tone) } : {}), ui.dim(note));
198
+ };
199
+ const out = session.parts.map((part) =>
200
+ ui.line(
201
+ part.name === 'main' ? ' main ' : ' agents',
202
+ part.amount.padStart(AMOUNT),
203
+ ' ',
204
+ ...bar(part.usd),
205
+ versus(part.against),
206
+ ),
207
+ );
208
+ if (session.effortMix.length)
209
+ out.push(
210
+ ui.line(
211
+ ui.dim(' effort'),
212
+ ...session.effortMix.flatMap(([effort, n]) => [
213
+ ui.dim(` ${effort} `),
214
+ ui.text(String(n), { weight: 'semibold' }),
215
+ ]),
216
+ ),
217
+ );
218
+ return out;
219
+ }
220
+
221
+ /**
222
+ * @param {any} ui
223
+ * @param {import('../model/now.mjs').ContextModel} context
224
+ */
225
+ // CONTEXT: the window's fill on the model that runs next, and the cache reuse of the last reply and of the session.
226
+ function contextLines(ui, context) {
227
+ const { fill, cache } = context;
228
+ const [filled, empty] = gaugeCells((fill ?? 0) / 100, 20);
229
+ return [
230
+ ui.head('CONTEXT'),
231
+ fill === null
232
+ ? ui.line(' ctx ', ui.dim(ui.say(context.empty)))
233
+ : ui.line(
234
+ ' ctx ',
235
+ ui.text(filled, { color: ui.color(context.fillTone) }),
236
+ ui.dim(empty),
237
+ ` ${String(fill).padStart(3)}%`,
238
+ ui.dim(` ${context.suffix}`),
239
+ ),
240
+ ui.line(
241
+ ' cache ',
242
+ cache.last
243
+ ? ui.line(ui.text(cache.last.figure, { color: ui.color(cache.tone) }), ui.dim(` ${cache.last.words}`))
244
+ : ui.dim(ui.say(cache.empty)),
245
+ cache.session ? ui.line(ui.dim(' · '), cache.session.figure, ui.dim(` ${cache.session.words}`)) : null,
246
+ ' ',
247
+ detailButton(ui, context),
248
+ ),
249
+ ...(context.detail.open ? readingDetail(ui, context.detail) : []),
250
+ ];
251
+ }
252
+
253
+ /**
254
+ * @param {any} ui
255
+ * @param {import('../model/now.mjs').ContextModel} context
256
+ */
257
+ const detailButton = (ui, context) =>
258
+ ui.Button({
259
+ key: 'usage-detail',
260
+ label: context.detail.open ? 'less' : 'details',
261
+ variant: context.detail.open ? 'primary' : 'secondary',
262
+ onPress: context.detail.toggle,
263
+ });
264
+
265
+ /**
266
+ * @param {any} ui
267
+ * @param {import('../model/now.mjs').ContextModel['detail']} detail
268
+ */
269
+ // Behind `details` on the Now tab: Claude's readings of the last reply, the input per reply, and the estimates the
270
+ // last decision weighed.
271
+ function readingDetail(ui, detail) {
272
+ const { history, tiers } = detail;
273
+ const glyphs = sparkline(history);
274
+ const trend = history.length
275
+ ? ui.line(
276
+ ' ',
277
+ ...[...glyphs].map((glyph, i) => {
278
+ const tier = tiers[tiers.length - history.length + i];
279
+ return tier ? ui.tier(tier, glyph) : ui.text(glyph);
280
+ }),
281
+ ui.dim(` ${ui.say(detail.range)}`),
282
+ )
283
+ : ui.dim(` ${ui.say(detail.empty)}`);
284
+ return [
285
+ ...detail.readings.terminal.map((line) => ui.dim(` ${line}`)),
286
+ ui.text(' '),
287
+ ui.head(ui.say(detail.input)),
288
+ trend,
289
+ ui.text(' '),
290
+ ui.head(ui.say(detail.estimatesHead)),
291
+ ...detail.estimates.map(([label, value]) => ui.text(`${label.padEnd(ESTIMATE_LABEL)}${value}`)),
292
+ ];
293
+ }
@@ -0,0 +1,55 @@
1
+ // The Policy tab as terminal rows: each control inside the sentence it completes, its unsaved mark and its default,
2
+ // its help under it in the same column, then Reset, in a box from PANEL_COLUMNS, and the file. Drawn from the model in
3
+ // ui/model/policy.mjs.
4
+
5
+ // The label column.
6
+ const LABEL = 13;
7
+
8
+ /**
9
+ * @param {any} ui the terminal kit
10
+ * @param {import('../model/policy.mjs').PolicyModel} model
11
+ * @param {{ columns: number | null }} opts
12
+ */
13
+ export function terminalPolicy(ui, model, { columns }) {
14
+ const { mode, baseline, tune, unsaved, file } = model;
15
+ const label = (words) => ui.text(words, { weight: 'semibold' });
16
+ // The control's unsaved mark and its default where the value differs.
17
+ const note = (field) => [ui.marker(field.changed && unsaved), field.default ? ui.dim(ui.say(field.default)) : null];
18
+ const control = (key) => ui.select(tune[key].control);
19
+ return [
20
+ ...ui.panel(columns, [
21
+ ui.line(ui.head('POLICY'), ui.dim(' · how readily the router changes models · edit, then Save')),
22
+ ui.text(' '),
23
+ ui.field(LABEL, label('Mode'), [
24
+ ui.line(ui.select(mode.control), ui.dim(` ${mode.note}`)),
25
+ ui.help(ui.say(mode.hint)),
26
+ mode.session ? ui.help(ui.say(mode.session), { color: ui.color('warning') }) : null,
27
+ ]),
28
+ ui.field(LABEL, label('Start'), [
29
+ ui.line('at ', ui.select(baseline.control), ' ', ...note(baseline)),
30
+ ui.help(ui.say(baseline.hint)),
31
+ ]),
32
+ ui.field(LABEL, label('Going down'), [
33
+ ui.line('after ', control('downgradeVotes'), ' agreeing turns ', ...note(tune.downgradeVotes)),
34
+ ui.line('if a cache write pays back within ', control('horizon'), ' ', ...note(tune.horizon)),
35
+ ]),
36
+ ui.field(LABEL, label('Going up'), [ui.help(ui.say(model.up))]),
37
+ ui.field(LABEL, label('Activities'), [
38
+ ui.line('apply at ', control('activityMass'), ' certainty or more ', ...note(tune.activityMass)),
39
+ ui.help(model.activitiesHint),
40
+ ]),
41
+ ui.field(LABEL, label('Credits'), [
42
+ ui.line('cap one cold cache write at ', control('cashCapUsd'), ' ', ...note(tune.cashCapUsd)),
43
+ ui.help(model.creditsHint),
44
+ ]),
45
+ ui.text(' '),
46
+ ui.Button({ key: 'reset-policy', label: model.reset.label, onPress: model.reset.press }),
47
+ ]),
48
+ ui.line(
49
+ ui.head('FILE'),
50
+ ` ${file.path} `,
51
+ file.copy ? ui.Button({ key: 'copy-path', label: ui.say(file.copy.label), onPress: file.copy.press }) : null,
52
+ ),
53
+ ui.dim(ui.say(model.subagents)),
54
+ ];
55
+ }
@@ -0,0 +1,167 @@
1
+ import { isFiniteNumber } from '../tokens.mjs';
2
+ import { dimLines } from './wrap.mjs';
3
+
4
+ // The grid's row-label column, and the editor's label column.
5
+ const GRID_LABEL = 13;
6
+ const EDITOR_LABEL = 22;
7
+ const HEAD_HINT = ' · which model runs, by tier and activity · edit, then Save';
8
+ // Below this many columns the grid's efforts are cut short.
9
+ const NARROW = 80;
10
+ const EFFORT_SHORT = { low: 'low', medium: 'med', high: 'high', xhigh: 'xh', max: 'max', inherit: 'inh' };
11
+
12
+ // "sonnet·high": the model's key and its effort, short enough for the grid.
13
+ const gridText = ({ model, effort }, narrow) =>
14
+ effort === null ? model : `${model}·${narrow ? (EFFORT_SHORT[effort] ?? effort) : effort}`;
15
+
16
+ /**
17
+ * @param {any} ui the terminal kit
18
+ * @param {import('../model/routes.mjs').RoutesModel} model
19
+ * @param {{ columns: number | null }} opts
20
+ */
21
+ export function terminalRoutes(ui, model, { columns }) {
22
+ const { mode } = model;
23
+ const narrow = isFiniteNumber(columns) && columns < NARROW;
24
+ const width = isFiniteNumber(columns)
25
+ ? Math.max(1, Math.min(narrow ? 14 : 16, Math.floor((columns - GRID_LABEL) / model.tiers.length)))
26
+ : 16;
27
+ const cellButton = (cell) =>
28
+ ui.cell(
29
+ width,
30
+ ui.Button({
31
+ key: cell.key,
32
+ label: fitRouteCell(
33
+ `${cell.selected ? '▸' : ' '}${cell.route ? gridText(cell.route, narrow) : model.inheritMark.glyph}`,
34
+ cell.marks.map((mark) => mark.glyph).join(''),
35
+ width,
36
+ ),
37
+ plain: true,
38
+ ...(cell.muted ? { dimColor: true } : {}),
39
+ onPress: cell.press,
40
+ }),
41
+ );
42
+ const { overrides } = model;
43
+ return [
44
+ ui.line(ui.head('ROUTES'), ui.dim(HEAD_HINT)),
45
+ ui.line(
46
+ ui.cell(
47
+ 26,
48
+ ui.Select
49
+ ? ui.Select({
50
+ key: 'activity-mode',
51
+ label: mode.label,
52
+ value: mode.value,
53
+ options: mode.options.map((option) => ({ value: option.value, label: ui.say(option.label) })),
54
+ onSelect: mode.pick,
55
+ })
56
+ : `${mode.label}: ${mode.value}`,
57
+ ),
58
+ ui.marker(mode.changed && model.unsaved),
59
+ ui.dim(ui.say(mode.hint)),
60
+ ),
61
+ ...(overrides
62
+ ? [
63
+ ui.line(
64
+ overrides.warning ? ui.text(overrides.text, { color: ui.color('warning') }) : ui.dim(overrides.text),
65
+ overrides.after ? ui.dim(` · ${overrides.after}`) : null,
66
+ ),
67
+ ]
68
+ : []),
69
+ ui.text(' '),
70
+ ui.line(
71
+ ui.cell(GRID_LABEL, ''),
72
+ ...model.tiers.map(({ id, label }) => ui.cell(width, ui.tier(id, fitRouteCell(` ${ui.say(label)}`, '', width)))),
73
+ ),
74
+ ...model.rows.map((row) =>
75
+ ui.line(
76
+ ui.cell(
77
+ GRID_LABEL,
78
+ row.kind === 'tier' ? ui.text(ui.say(row.label), { weight: 'semibold' }) : ui.activity(row.label),
79
+ ),
80
+ ...row.cells.map(cellButton),
81
+ ),
82
+ ),
83
+ ui.dim(` ${model.legend.map(({ glyph, text }) => `${glyph} ${text}`).join(' ')}`),
84
+ ui.text(' '),
85
+ ...(model.editor ? editorLines(ui, model.editor, model.unsaved) : [ui.dim(model.hint)]),
86
+ ui.text(' '),
87
+ ...(model.cache.wrap
88
+ ? dimLines(ui, ui.say(model.cache.text), Math.min(columns ?? 78, 78))
89
+ : [ui.dim(ui.say(model.cache.text))]),
90
+ ...model.steps.map((step) => ui.text(`! ${ui.say(step)}`, { color: ui.color('warning') })),
91
+ ui.Button({ key: 'reset-routes', label: model.reset.label, hotkey: 'r', onPress: model.reset.press }),
92
+ ui.dim(model.note),
93
+ ];
94
+ }
95
+
96
+ // A label and its marks cut to their cell with an ellipsis, keeping the marks.
97
+ function fitRouteCell(text, mark, width) {
98
+ const label = `${text}${mark}`;
99
+ if (label.length <= width) return label;
100
+ if (mark.length >= width) return mark.slice(-width);
101
+ return `${label.slice(0, Math.max(0, width - 1 - mark.length))}…${mark}`;
102
+ }
103
+
104
+ /**
105
+ * @param {any} ui
106
+ * @param {import('../model/routes.mjs').RouteEditor['choices']} choices
107
+ */
108
+ // A model Select and an effort Select for one route, or their values as text on a surface without Select.
109
+ function routeSelects(ui, { key, model, effort }) {
110
+ return [
111
+ ui.cell(
112
+ 14,
113
+ ui.Select
114
+ ? ui.Select({ key: `${key}-model`, value: model.value, options: model.options, onSelect: model.pick })
115
+ : model.name,
116
+ ),
117
+ ui.cell(
118
+ 12,
119
+ !effort
120
+ ? ui.dim('none')
121
+ : ui.Select
122
+ ? ui.Select({ key: `${key}-effort`, value: effort.value, options: effort.options, onSelect: effort.pick })
123
+ : effort.text,
124
+ ),
125
+ ];
126
+ }
127
+
128
+ /**
129
+ * @param {any} ui
130
+ * @param {import('../model/routes.mjs').RouteEditor} editor
131
+ * @param {import('../model/routes.mjs').RouteMark} unsaved
132
+ */
133
+ // The selected cell's editor: a tier's route and its prices, or an activity's cell with its state, the ways back and
134
+ // its warnings.
135
+ function editorLines(ui, editor, unsaved) {
136
+ const pad = ''.padEnd(EDITOR_LABEL);
137
+ if (editor.kind === 'tier') {
138
+ return [
139
+ ui.line(
140
+ ui.cell(EDITOR_LABEL, ui.line(ui.tier(editor.tier), ui.dim(' · every turn'))),
141
+ ...routeSelects(ui, editor.choices),
142
+ ui.marker(editor.changed && unsaved),
143
+ ),
144
+ ui.dim(`${pad}${editor.rates.terminal}`),
145
+ ];
146
+ }
147
+ const { remove, restore } = editor;
148
+ return [
149
+ ui.line(
150
+ ui.cell(EDITOR_LABEL, ui.line(ui.activity(editor.activity), ' at ', ui.tier(editor.tier))),
151
+ ...routeSelects(ui, editor.choices),
152
+ ui.marker(editor.changed && unsaved),
153
+ ),
154
+ ui.line(ui.cell(EDITOR_LABEL, ''), ui.dim(ui.say(editor.state))),
155
+ ...(remove || restore
156
+ ? [
157
+ ui.line(
158
+ ui.cell(EDITOR_LABEL, ''),
159
+ remove ? ui.Button({ key: remove.key, label: remove.label.terminal, onPress: remove.press }) : null,
160
+ remove && restore ? ' ' : null,
161
+ restore ? ui.Button({ key: restore.key, label: restore.label, onPress: restore.press }) : null,
162
+ ),
163
+ ]
164
+ : []),
165
+ ...editor.warnings.map((warning) => ui.text(`${pad}! ${ui.say(warning)}`, { color: ui.color('warning') })),
166
+ ];
167
+ }