@alexeiled/claude-router 1.24.0 → 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.
Files changed (78) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/CHANGELOG.md +44 -0
  3. package/README.md +39 -7
  4. package/docs/architecture.md +27 -4
  5. package/docs/configuration.md +23 -14
  6. package/docs/evaluation.md +3 -3
  7. package/docs/native-router.md +11 -3
  8. package/docs/router-band.svg +124 -133
  9. package/docs/router-pane-classifier.svg +24 -23
  10. package/docs/router-pane-now.svg +9 -7
  11. package/docs/router-pane-policy.svg +4 -4
  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 +70 -17
  17. package/hooks/native-router.mjs +333 -80
  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 +69 -20
  30. package/lib/facts.mjs +3 -0
  31. package/lib/modes.mjs +8 -4
  32. package/lib/panel.mjs +75 -2167
  33. package/lib/policy.mjs +7 -6
  34. package/lib/replay-trace.mjs +5 -2
  35. package/lib/replay.mjs +7 -1
  36. package/lib/route-line.mjs +46 -0
  37. package/lib/route.mjs +21 -16
  38. package/lib/savings.mjs +14 -3
  39. package/lib/subagents.mjs +41 -11
  40. package/lib/subcommands.mjs +60 -0
  41. package/lib/ui/fallback.mjs +22 -0
  42. package/lib/ui/graphical/band.mjs +256 -0
  43. package/lib/ui/graphical/charts.mjs +163 -0
  44. package/lib/ui/graphical/classifier.mjs +153 -0
  45. package/lib/ui/graphical/frame.mjs +227 -0
  46. package/lib/ui/graphical/kit.mjs +156 -0
  47. package/lib/ui/graphical/meter.mjs +25 -0
  48. package/lib/ui/graphical/now.mjs +353 -0
  49. package/lib/ui/graphical/policy.mjs +56 -0
  50. package/lib/ui/graphical/routes.mjs +264 -0
  51. package/lib/ui/graphical/squares.mjs +29 -0
  52. package/lib/ui/graphical/usage.mjs +376 -0
  53. package/lib/ui/model/band.mjs +439 -0
  54. package/lib/ui/model/classifier.mjs +308 -0
  55. package/lib/ui/model/draft.mjs +62 -0
  56. package/lib/ui/model/format.mjs +41 -0
  57. package/lib/ui/model/frame.mjs +359 -0
  58. package/lib/ui/model/now.mjs +390 -0
  59. package/lib/ui/model/policy.mjs +194 -0
  60. package/lib/ui/model/routes.mjs +392 -0
  61. package/lib/ui/model/usage-agents.mjs +311 -0
  62. package/lib/ui/model/usage-breakdown.mjs +237 -0
  63. package/lib/ui/model/usage-shared.mjs +58 -0
  64. package/lib/ui/model/usage.mjs +330 -0
  65. package/lib/ui/terminal/band.mjs +220 -0
  66. package/lib/ui/terminal/classifier.mjs +142 -0
  67. package/lib/ui/terminal/frame.mjs +169 -0
  68. package/lib/ui/terminal/kit.mjs +133 -0
  69. package/lib/ui/terminal/now.mjs +300 -0
  70. package/lib/ui/terminal/policy.mjs +52 -0
  71. package/lib/ui/terminal/routes.mjs +167 -0
  72. package/lib/ui/terminal/usage.mjs +363 -0
  73. package/lib/ui/terminal/wrap.mjs +37 -0
  74. package/lib/ui/tokens.mjs +106 -0
  75. package/lib/usage.mjs +5 -5
  76. package/lib/view.mjs +5 -2
  77. package/package.json +3 -2
  78. package/types/index.d.ts +15 -0
@@ -0,0 +1,300 @@
1
+ // The Now tab as terminal rows: NEXT TURN, RECENT DECISIONS, PLAN, SESSION, CONTEXT, each a run of sibling lines under
2
+ // the pane, drawn from the model in ui/model/now.mjs. The colours are the ones the terminal has always drawn for each
3
+ // reading, derived here from the model's numbers.
4
+ import { gaugeCells, sparkline, tierBars } from '../../display.mjs';
5
+ import { CHIP_BACKGROUND, isFiniteNumber } from '../tokens.mjs';
6
+
7
+ // Below this a pin row holds one pin.
8
+ const PIN_ROW_COLUMNS = 64;
9
+ const SESSION_BAR = 16;
10
+ const ESTIMATE_LABEL = 22;
11
+
12
+ /**
13
+ * @param {any} ui the terminal kit
14
+ * @param {import('../model/now.mjs').NowModel} model
15
+ * @param {{ columns: number | null, now: number }} opts
16
+ */
17
+ export function terminalNow(ui, model, { columns, now }) {
18
+ return [
19
+ ...nextLines(ui, model.next, columns),
20
+ ...(model.decisions.length
21
+ ? [ui.head('RECENT DECISIONS'), ...model.decisions.map((line) => ui.text(line)), ui.text(' ')]
22
+ : []),
23
+ ...planLines(ui, model.plan),
24
+ ...sessionLines(ui, model.session, columns, now),
25
+ ...contextLines(ui, model.context),
26
+ ];
27
+ }
28
+
29
+ /**
30
+ * @param {any} ui
31
+ * @param {import('../model/now.mjs').NextModel} next
32
+ * @param {number | null} columns
33
+ */
34
+ // NEXT TURN. With routing off or unavailable only the model applies: no tier, activity or reason from an earlier turn.
35
+ function nextLines(ui, next, columns) {
36
+ const out = [ui.head('NEXT TURN')];
37
+ if (next.state !== 'routed') return [...out, ...offLines(ui, next)];
38
+ const { reading } = next;
39
+ out.push(
40
+ ui.line(
41
+ ' ',
42
+ ui.tierMeter(next.tier, next.tier ? 'lit' : 'off'),
43
+ ' ',
44
+ ui.text(next.label, { weight: 'semibold' }),
45
+ reading
46
+ ? ui.line(
47
+ ' ',
48
+ // `uncertain` is an answer, not an activity: it applies none.
49
+ reading.isActivity ? ui.activity(reading.choice) : ui.dim(reading.choice),
50
+ reading.share ? ` ${reading.share}` : '',
51
+ reading.other ? ui.dim(` ${reading.other}`) : null,
52
+ next.phase ? ui.dim(` ${next.phase}`) : null,
53
+ )
54
+ : null,
55
+ ),
56
+ next.why ? ui.line(ui.dim(' why '), next.why, next.support ? ui.dim(` ${next.support}`) : null) : null,
57
+ next.override ? ui.dim(` ${next.override}`) : null,
58
+ next.mode
59
+ ? ui.line(
60
+ ui.dim(' mode '),
61
+ ui.text(` ${next.mode.name} `, { backgroundColor: CHIP_BACKGROUND, color: ui.color('primary') }),
62
+ ui.dim(` ${ui.say(next.mode.from)}`),
63
+ next.mode.clear ? ui.dim(` · ${next.mode.clear}`) : null,
64
+ )
65
+ : null,
66
+ ...pinLines(ui, next, columns),
67
+ ui.text(' '),
68
+ );
69
+ return out.filter(Boolean);
70
+ }
71
+
72
+ /**
73
+ * @param {any} ui
74
+ * @param {import('../model/now.mjs').NextOffModel} next
75
+ */
76
+ // Routing off or unavailable: the model that runs, and why nothing else applies.
77
+ function offLines(ui, next) {
78
+ const unavailable = next.state === 'unavailable';
79
+ return [
80
+ ui.line(
81
+ unavailable ? ui.text(' ✕ ', { color: ui.color('warning') }) : ui.dim(' ○ '),
82
+ ui.text(next.label, { weight: 'semibold' }),
83
+ ui.dim(` ${ui.say(next.note)}`),
84
+ ),
85
+ ui.line(ui.dim(' pin '), ui.dim(ui.say(next.pin))),
86
+ ui.text(' '),
87
+ ];
88
+ }
89
+
90
+ /**
91
+ * @param {any} ui
92
+ * @param {import('../model/now.mjs').NextRoutedModel} next
93
+ * @param {number | null} columns
94
+ */
95
+ // One button per tier, low to high, named by the route it runs ("▂▄ Haiku·high") with the classifier's support on the
96
+ // last turn; the current tier is marked and a pending pin is filled. A press pins the next turn only; pressing the
97
+ // pending pin again unpins it.
98
+ function pinLines(ui, next, columns) {
99
+ const pin = ({ tier, route, support }) =>
100
+ ui.Button({
101
+ key: `pin-${tier}`,
102
+ label: `${next.tier === tier ? '▶ ' : ''}${tierBars(tier)} ${ui.say(route)} ${support ?? '—'}`,
103
+ variant: next.pendingPin === tier ? 'primary' : 'secondary',
104
+ onPress: () => next.pick(tier),
105
+ });
106
+ const narrow = isFiniteNumber(columns) && columns < PIN_ROW_COLUMNS;
107
+ const rows = narrow ? next.pins.map((one) => [one]) : [next.pins.slice(0, 2), next.pins.slice(2)];
108
+ return rows.map((pins, i) => ui.line(i ? ' ' : ui.dim(' pin '), ...pins.flatMap((one) => [pin(one), ' '])));
109
+ }
110
+
111
+ /**
112
+ * @param {any} ui
113
+ * @param {import('../model/now.mjs').NowModel['plan']} plan
114
+ */
115
+ // PLAN: Claude Code's plan windows, when it reports them.
116
+ function planLines(ui, plan) {
117
+ if (!plan.length) return [];
118
+ return [
119
+ ui.line(ui.head('PLAN'), ui.dim(' Claude plan usage')),
120
+ ...plan.map((limit) => {
121
+ const [filled, empty] = gaugeCells(limit.percent / 100, 20);
122
+ const color = ui.color(limit.tone);
123
+ return ui.line(
124
+ ` ${limit.label.padEnd(6)}`,
125
+ ui.text(filled, { color }),
126
+ ui.dim(empty),
127
+ ui.text(` ${String(limit.percent).padStart(3)}%`, { color }),
128
+ limit.reset ? ui.dim(` resets ${limit.reset}`) : null,
129
+ );
130
+ }),
131
+ ui.text(' '),
132
+ ];
133
+ }
134
+
135
+ /**
136
+ * @param {any} ui
137
+ * @param {import('../model/now.mjs').SessionModel} session
138
+ * @param {number | null} columns
139
+ * @param {number} now
140
+ */
141
+ // SESSION: one total with the subagents, the difference against no routing, the main thread and the subagents as bars
142
+ // on one scale, then the replies by tier.
143
+ function sessionLines(ui, session, columns, now) {
144
+ const strip = ui.stripRows(session.strip, { columns, now, since: session.since });
145
+ const out = [
146
+ ui.line(
147
+ ui.head('SESSION'),
148
+ session.head ? ui.dim(` ${session.head}`) : null,
149
+ session.running ? ui.text(` ● ${session.running} running`, { color: ui.color('warning') }) : null,
150
+ ' ',
151
+ // The band's second row, on and off, so two rows can be left at any band width.
152
+ ui.Button({ key: 'band-detail-toggle', label: ui.say(session.detailToggle), onPress: session.toggleDetail }),
153
+ ),
154
+ ];
155
+ // Replies without a list price reach the strip but not the usage record: the tiers still show, the cost cannot.
156
+ if (!session.priced) return [...out, ui.dim(` ${ui.say(session.empty)}`), ...strip, ui.text(' ')];
157
+ out.push(
158
+ ui.line(
159
+ ' ',
160
+ ui.text(session.total, { weight: 'semibold' }),
161
+ ui.dim(` ${session.billing} `),
162
+ ui.Button({ key: 'usage-session', label: 'Usage', onPress: session.usage }),
163
+ ),
164
+ );
165
+ const { diff } = session;
166
+ if (diff) {
167
+ const versus = ` vs ${diff.versus}`;
168
+ const of = ` ${diff.of}`;
169
+ const fits = !isFiniteNumber(columns) || 2 + diff.text.length + versus.length + of.length <= columns;
170
+ out.push(
171
+ ui.line(
172
+ ' ',
173
+ ui.text(diff.text, { weight: 'semibold', color: ui.color(diff.tone) }),
174
+ versus,
175
+ fits ? ui.dim(of) : null,
176
+ ),
177
+ );
178
+ }
179
+ if (session.note) out.push(ui.dim(` ${session.note}`));
180
+ out.push(...costBars(ui, session));
181
+ if (session.lost) out.push(ui.lostLine(session.policy));
182
+ out.push(...strip, ui.text(' '));
183
+ return out;
184
+ }
185
+
186
+ /**
187
+ * @param {any} ui
188
+ * @param {import('../model/now.mjs').SessionModel} session
189
+ */
190
+ // The main thread and the subagents as bars on one scale, each with its difference against no routing; an effort-only
191
+ // profile shows its effort mix.
192
+ function costBars(ui, session) {
193
+ const { scale } = session;
194
+ const bar = (value) => {
195
+ const filled = scale > 0 ? Math.max(value > 0 ? 1 : 0, Math.round((value / scale) * SESSION_BAR)) : 0;
196
+ return [ui.text('█'.repeat(filled), { color: ui.color('accent') }), ui.dim('░'.repeat(SESSION_BAR - filled))];
197
+ };
198
+ const line = (against) =>
199
+ against
200
+ ? ui.line(
201
+ ' ',
202
+ ui.text(against.text, against.tone ? { color: ui.color(against.tone) } : {}),
203
+ ui.dim(` vs ${against.versus}`),
204
+ )
205
+ : null;
206
+ const out = session.parts.map((part) =>
207
+ ui.line(
208
+ part.name === 'main' ? ' main ' : ' agents',
209
+ part.amount.padStart(7),
210
+ ' ',
211
+ ...bar(part.usd),
212
+ line(part.against),
213
+ ),
214
+ );
215
+ if (session.effortMix.length)
216
+ out.push(
217
+ ui.line(
218
+ ui.dim(' effort'),
219
+ ...session.effortMix.flatMap(([effort, n]) => [
220
+ ui.dim(` ${effort} `),
221
+ ui.text(String(n), { weight: 'semibold' }),
222
+ ]),
223
+ ),
224
+ );
225
+ return out;
226
+ }
227
+
228
+ /**
229
+ * @param {any} ui
230
+ * @param {import('../model/now.mjs').ContextModel} context
231
+ */
232
+ // CONTEXT: the window's fill on the model that runs next, and the cache reuse of the last reply and of the session.
233
+ function contextLines(ui, context) {
234
+ const { fill, cache } = context;
235
+ const [filled, empty] = gaugeCells((fill ?? 0) / 100, 20);
236
+ return [
237
+ ui.head('CONTEXT'),
238
+ fill === null
239
+ ? ui.line(' ctx ', ui.dim(ui.say(context.empty)))
240
+ : ui.line(
241
+ ' ctx ',
242
+ ui.text(filled, { color: ui.color(context.fillTone) }),
243
+ ui.dim(empty),
244
+ ` ${String(fill).padStart(3)}%`,
245
+ ui.dim(` ${context.suffix}`),
246
+ ),
247
+ ui.line(
248
+ ' cache ',
249
+ cache.last
250
+ ? ui.line(ui.text(cache.last.figure, { color: ui.color(cache.tone) }), ui.dim(` ${cache.last.words}`))
251
+ : ui.dim(ui.say(cache.empty)),
252
+ cache.session ? ui.line(ui.dim(' · '), cache.session.figure, ui.dim(` ${cache.session.words}`)) : null,
253
+ ' ',
254
+ detailButton(ui, context),
255
+ ),
256
+ ...(context.detail.open ? readingDetail(ui, context.detail) : []),
257
+ ];
258
+ }
259
+
260
+ /**
261
+ * @param {any} ui
262
+ * @param {import('../model/now.mjs').ContextModel} context
263
+ */
264
+ const detailButton = (ui, context) =>
265
+ ui.Button({
266
+ key: 'usage-detail',
267
+ label: context.detail.open ? 'less' : 'details',
268
+ variant: context.detail.open ? 'primary' : 'secondary',
269
+ onPress: context.detail.toggle,
270
+ });
271
+
272
+ /**
273
+ * @param {any} ui
274
+ * @param {import('../model/now.mjs').ContextModel['detail']} detail
275
+ */
276
+ // Behind `details` on the Now tab: Claude's readings of the last reply, the input per reply, and the estimates the
277
+ // last decision weighed.
278
+ function readingDetail(ui, detail) {
279
+ const { history, tiers } = detail;
280
+ const glyphs = sparkline(history);
281
+ const trend = history.length
282
+ ? ui.line(
283
+ ' ',
284
+ ...[...glyphs].map((glyph, i) => {
285
+ const tier = tiers[tiers.length - history.length + i];
286
+ return tier ? ui.tier(tier, glyph) : ui.text(glyph);
287
+ }),
288
+ ui.dim(` ${ui.say(detail.range)}`),
289
+ )
290
+ : ui.dim(` ${ui.say(detail.empty)}`);
291
+ return [
292
+ ...detail.readings.terminal.map((line) => ui.dim(` ${line}`)),
293
+ ui.text(' '),
294
+ ui.head(ui.say(detail.input)),
295
+ trend,
296
+ ui.text(' '),
297
+ ui.head(ui.say(detail.estimatesHead)),
298
+ ...detail.estimates.map(([label, value]) => ui.text(`${label.padEnd(ESTIMATE_LABEL)}${value}`)),
299
+ ];
300
+ }
@@ -0,0 +1,52 @@
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 and the file. Drawn from the model in ui/model/policy.mjs.
3
+
4
+ // The label column.
5
+ const LABEL = 13;
6
+
7
+ /**
8
+ * @param {any} ui the terminal kit
9
+ * @param {import('../model/policy.mjs').PolicyModel} model
10
+ */
11
+ export function terminalPolicy(ui, model) {
12
+ const { mode, baseline, tune, unsaved, file } = model;
13
+ const label = (words) => ui.text(words, { weight: 'semibold' });
14
+ // The control's unsaved mark and its default where the value differs.
15
+ const note = (field) => [ui.marker(field.changed && unsaved), field.default ? ui.dim(ui.say(field.default)) : null];
16
+ const control = (key) => ui.select(tune[key].control);
17
+ return [
18
+ ui.line(ui.head('POLICY'), ui.dim(' · how readily the router changes models · edit, then Save')),
19
+ ui.text(' '),
20
+ ui.field(LABEL, label('Mode'), [
21
+ ui.line(ui.select(mode.control), ui.dim(` ${mode.note}`)),
22
+ ui.help(ui.say(mode.hint)),
23
+ mode.session ? ui.help(ui.say(mode.session), { color: ui.color('warning') }) : null,
24
+ ]),
25
+ ui.field(LABEL, label('Start'), [
26
+ ui.line('at ', ui.select(baseline.control), ' ', ...note(baseline)),
27
+ ui.help(ui.say(baseline.hint)),
28
+ ]),
29
+ ui.field(LABEL, label('Going down'), [
30
+ ui.line('after ', control('downgradeVotes'), ' agreeing turns ', ...note(tune.downgradeVotes)),
31
+ ui.line('if a cache write pays back within ', control('horizon'), ' ', ...note(tune.horizon)),
32
+ ]),
33
+ ui.field(LABEL, label('Going up'), [ui.help(ui.say(model.up))]),
34
+ ui.field(LABEL, label('Activities'), [
35
+ ui.line('apply at ', control('activityMass'), ' certainty or more ', ...note(tune.activityMass)),
36
+ ui.help(model.activitiesHint),
37
+ ]),
38
+ ui.field(LABEL, label('Credits'), [
39
+ ui.line('cap one cold cache write at ', control('cashCapUsd'), ' ', ...note(tune.cashCapUsd)),
40
+ ui.help(model.creditsHint),
41
+ ]),
42
+ ui.text(' '),
43
+ ui.Button({ key: 'reset-policy', label: model.reset.label, onPress: model.reset.press }),
44
+ ui.text(' '),
45
+ ui.line(
46
+ ui.head('FILE'),
47
+ ` ${file.path} `,
48
+ file.copy ? ui.Button({ key: 'copy-path', label: ui.say(file.copy.label), onPress: file.copy.press }) : null,
49
+ ),
50
+ ui.dim(ui.say(model.subagents)),
51
+ ];
52
+ }
@@ -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
+ }