@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.
Files changed (81) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/CHANGELOG.md +62 -1
  3. package/README.md +39 -7
  4. package/docs/architecture.md +27 -4
  5. package/docs/auto-engine.md +152 -0
  6. package/docs/configuration.md +47 -3
  7. package/docs/evaluation.md +222 -3
  8. package/docs/native-router.md +11 -3
  9. package/docs/providers.md +28 -0
  10. package/docs/router-band.svg +124 -133
  11. package/docs/router-pane-classifier.svg +24 -23
  12. package/docs/router-pane-now.svg +9 -7
  13. package/docs/router-pane-policy.svg +4 -4
  14. package/docs/router-pane-routes.svg +8 -8
  15. package/docs/router-pane-usage-whatif.svg +14 -14
  16. package/docs/router-pane-usage.svg +20 -20
  17. package/docs/routing-modes.md +13 -4
  18. package/docs/user-guide.md +79 -19
  19. package/hooks/native-router.mjs +1194 -215
  20. package/lib/activity-stats.mjs +23 -4
  21. package/lib/activity.mjs +7 -5
  22. package/lib/auto-engine.mjs +414 -0
  23. package/lib/auto-route.mjs +253 -0
  24. package/lib/band.mjs +35 -440
  25. package/lib/classifier-client.mjs +12 -8
  26. package/lib/config-file.mjs +7 -2
  27. package/lib/config.mjs +64 -15
  28. package/lib/cost.mjs +5 -1
  29. package/lib/costs.mjs +4 -2
  30. package/lib/decisions.mjs +80 -5
  31. package/lib/display.mjs +69 -20
  32. package/lib/facts.mjs +30 -0
  33. package/lib/modes.mjs +8 -4
  34. package/lib/panel.mjs +75 -2163
  35. package/lib/policy.mjs +7 -6
  36. package/lib/providers.mjs +7 -0
  37. package/lib/replay-trace.mjs +550 -0
  38. package/lib/replay.mjs +718 -0
  39. package/lib/route-line.mjs +46 -0
  40. package/lib/route.mjs +35 -21
  41. package/lib/savings.mjs +14 -3
  42. package/lib/subagents.mjs +41 -11
  43. package/lib/subcommands.mjs +60 -0
  44. package/lib/ui/fallback.mjs +22 -0
  45. package/lib/ui/graphical/band.mjs +256 -0
  46. package/lib/ui/graphical/charts.mjs +163 -0
  47. package/lib/ui/graphical/classifier.mjs +153 -0
  48. package/lib/ui/graphical/frame.mjs +227 -0
  49. package/lib/ui/graphical/kit.mjs +156 -0
  50. package/lib/ui/graphical/meter.mjs +25 -0
  51. package/lib/ui/graphical/now.mjs +353 -0
  52. package/lib/ui/graphical/policy.mjs +56 -0
  53. package/lib/ui/graphical/routes.mjs +264 -0
  54. package/lib/ui/graphical/squares.mjs +29 -0
  55. package/lib/ui/graphical/usage.mjs +376 -0
  56. package/lib/ui/model/band.mjs +439 -0
  57. package/lib/ui/model/classifier.mjs +308 -0
  58. package/lib/ui/model/draft.mjs +62 -0
  59. package/lib/ui/model/format.mjs +41 -0
  60. package/lib/ui/model/frame.mjs +359 -0
  61. package/lib/ui/model/now.mjs +390 -0
  62. package/lib/ui/model/policy.mjs +194 -0
  63. package/lib/ui/model/routes.mjs +392 -0
  64. package/lib/ui/model/usage-agents.mjs +311 -0
  65. package/lib/ui/model/usage-breakdown.mjs +237 -0
  66. package/lib/ui/model/usage-shared.mjs +58 -0
  67. package/lib/ui/model/usage.mjs +330 -0
  68. package/lib/ui/terminal/band.mjs +220 -0
  69. package/lib/ui/terminal/classifier.mjs +142 -0
  70. package/lib/ui/terminal/frame.mjs +169 -0
  71. package/lib/ui/terminal/kit.mjs +133 -0
  72. package/lib/ui/terminal/now.mjs +300 -0
  73. package/lib/ui/terminal/policy.mjs +52 -0
  74. package/lib/ui/terminal/routes.mjs +167 -0
  75. package/lib/ui/terminal/usage.mjs +363 -0
  76. package/lib/ui/terminal/wrap.mjs +37 -0
  77. package/lib/ui/tokens.mjs +106 -0
  78. package/lib/usage.mjs +5 -5
  79. package/lib/view.mjs +5 -2
  80. package/package.json +3 -2
  81. package/types/index.d.ts +15 -0
@@ -0,0 +1,142 @@
1
+ // The Classifier tab as terminal rows: one row per classifier to compare, then the active one's settings and
2
+ // readings, this session's agreement and the counts across sessions. Drawn from the model in
3
+ // ui/model/classifier.mjs.
4
+ import { isFiniteNumber, toneColor } from '../tokens.mjs';
5
+ import { dimLines } from './wrap.mjs';
6
+
7
+ // The wide row's columns: the name, where the prompt goes, the probe and the p95 as fixed cells, then the status and
8
+ // Set up.
9
+ const CELLS = { name: 17, where: 21, probe: 8, p95: 10 };
10
+ const CELLS_WIDTH = Object.values(CELLS).reduce((sum, width) => sum + width, 0);
11
+ // A row's status as drawn: a missing credential leaves room for the Set up Button after it.
12
+ const stateText = (row) => (row.setup ? `${row.state.text} ` : row.state.text);
13
+ // A Button is drawn as its label in `[ ` and ` ]`.
14
+ const wideRowWidth = (row) => CELLS_WIDTH + stateText(row).length + (row.setup ? row.setup.label.length + 4 : 0);
15
+
16
+ /**
17
+ * Whether the Classifier rows are drawn on two lines each: with no width, or where the widest wide row does not fit.
18
+ * No rows fit anywhere.
19
+ * @param {{ state: { text: string }, setup: { label: string } | null }[]} rows
20
+ * @param {number | null} columns
21
+ */
22
+ export const classifierTwoLine = (rows, columns) =>
23
+ !isFiniteNumber(columns) || columns < rows.reduce((widest, row) => Math.max(widest, wideRowWidth(row)), 0);
24
+
25
+ /**
26
+ * @param {any} ui the terminal kit
27
+ * @param {import('../model/classifier.mjs').ClassifierModel} model
28
+ * @param {{ columns: number | null }} opts
29
+ */
30
+ export function terminalClassifier(ui, model, { columns }) {
31
+ const { active } = model;
32
+ const cells = CELLS;
33
+ const twoLine = classifierTwoLine(model.rows, columns);
34
+ const { wait, health, sends, credentials } = active;
35
+ const rows = classifierRows(ui, model, twoLine);
36
+ // The deadline and what is sent: in a narrow pane their help lines hang under the value, as Policy's do.
37
+ const deadline = [
38
+ ui.cell(12, ui.select(active.deadline.control)),
39
+ wait.slow ? ui.text(wait.text, { color: ui.color('warning') }) : ui.dim(wait.text),
40
+ ];
41
+ const settings = twoLine
42
+ ? [
43
+ ui.field(12, active.deadline.label, [ui.line(...deadline), ui.help(ui.say(active.deadline.help))]),
44
+ ui.field(12, sends.label, [ui.text(ui.say(sends.text), { wrap: 'wrap' }), ui.help(ui.say(sends.help))]),
45
+ ]
46
+ : [
47
+ ui.line(ui.cell(12, active.deadline.label), ...deadline),
48
+ ui.dim(`${''.padEnd(12)}${ui.say(active.deadline.help)}`),
49
+ ui.line(ui.cell(12, sends.label), ui.say(sends.text)),
50
+ ui.dim(` ${ui.say(sends.help)}`),
51
+ ];
52
+ return [
53
+ ui.line(ui.head('CLASSIFIER'), ui.dim(' · asked once per new turn · a pick saves at once')),
54
+ twoLine
55
+ ? null
56
+ : ui.dim(
57
+ `${''.padEnd(cells.name)}${'prompt goes to'.padEnd(cells.where)}${'probe'.padEnd(cells.probe)}${'p95'.padEnd(cells.p95)}status`,
58
+ ),
59
+ ...rows,
60
+ ...dimLines(ui, ` ${ui.say(model.probeNote)}`, columns),
61
+ ui.text(' '),
62
+ ui.line(ui.head(active.label.toUpperCase()), ui.dim(' · active')),
63
+ ...settings,
64
+ ui.line(
65
+ ui.cell(12, health.label),
66
+ health.paused ? ui.text(health.paused, { color: ui.color('warning') }) : health.steady,
67
+ ` · ${health.failures}`,
68
+ health.last ? ui.dim(` · ${health.last}`) : null,
69
+ ),
70
+ ui.line(
71
+ ui.cell(12, credentials.label),
72
+ ui.Button({
73
+ key: credentials.button.key,
74
+ label: credentials.button.label,
75
+ hotkey: credentials.button.hotkey,
76
+ onPress: credentials.button.press,
77
+ }),
78
+ ui.dim(` ${credentials.note}`),
79
+ ),
80
+ ...(active.session ? [ui.text(' '), ui.head(active.session.head), ui.text(active.session.text)] : []),
81
+ ...(model.store
82
+ ? [
83
+ ui.text(' '),
84
+ ui.head(model.store.head),
85
+ ...model.store.rows.map((row) => ui.text(row.text)),
86
+ ...(model.store.unpriced ? [ui.text(model.store.unpriced)] : []),
87
+ ]
88
+ : []),
89
+ ];
90
+ }
91
+
92
+ /**
93
+ * The classifiers. Wide: one row of columns to compare. Narrow: the name, its status (cut, never wrapped, where the
94
+ * pane is too narrow for it; its mark stays) and Set up, which keeps its width, then where the prompt goes and the
95
+ * probe result under the name.
96
+ * @param {any} ui the terminal kit
97
+ * @param {any} model the Classifier model (ui/model/classifier.mjs)
98
+ */
99
+ function classifierRows(ui, model, twoLine) {
100
+ const cells = CELLS;
101
+ const name = (row) =>
102
+ ui.cell(
103
+ cells.name,
104
+ ui.Button({
105
+ key: row.key,
106
+ label: `${row.active ? '◉' : '○'} ${row.label.padEnd(10)}`,
107
+ variant: row.active ? 'primary' : 'secondary',
108
+ onPress: row.press,
109
+ }),
110
+ );
111
+ const state = (row, style = {}) =>
112
+ ui.text(stateText(row), { color: toneColor(row.state.tone, 'terminal'), ...style });
113
+ const setup = (row) =>
114
+ row.setup ? ui.Button({ key: row.setup.key, label: row.setup.label, onPress: row.setup.press }) : null;
115
+ return twoLine
116
+ ? model.rows.map((row) =>
117
+ ui.Box({
118
+ flexDirection: 'column',
119
+ children: [
120
+ ui.line(
121
+ name(row),
122
+ ui.Box({ flexShrink: 1, minWidth: 2, children: [state(row, { wrap: 'truncate-end' })] }),
123
+ row.setup ? ui.Box({ flexShrink: 0, children: [setup(row)] }) : null,
124
+ ),
125
+ ui.dim(` ${row.narrowDetail}`),
126
+ ],
127
+ }),
128
+ )
129
+ : model.rows.map((row) =>
130
+ ui.line(
131
+ name(row),
132
+ ui.cell(
133
+ cells.where,
134
+ row.where.local ? ui.text(row.where.text, { color: ui.color('success') }) : ui.dim(row.where.text),
135
+ ),
136
+ ui.cell(cells.probe, row.probe ?? ui.dim('—')),
137
+ ui.cell(cells.p95, row.p95 ?? ui.dim('—')),
138
+ state(row),
139
+ setup(row),
140
+ ),
141
+ );
142
+ }
@@ -0,0 +1,169 @@
1
+ // The pane's frame on the terminal: the header with the routing state and the tab row, the warnings, the save bar's
2
+ // lines and the help.
3
+ import { toneColor } from '../tokens.mjs';
4
+
5
+ // Routing's state, its one mode control and the classifier's state on one line. While routing is unavailable, on and
6
+ // off would change nothing, so the line says why instead of offering them.
7
+ function routingState(ui, frame, actions, help) {
8
+ const end = help ? [' ', help] : [];
9
+ if (frame.unavailable)
10
+ return [
11
+ ui.line(
12
+ ui.head('ROUTER'),
13
+ ' ',
14
+ ui.text('unavailable', { color: ui.color('warning') }),
15
+ ui.dim(' Claude’s model is kept'),
16
+ ...end,
17
+ ),
18
+ ui.text(frame.unavailable.reason, { color: ui.color('warning') }),
19
+ ...frame.unavailable.cleanup.map((line) => ui.text(line, { color: ui.color('warning') })),
20
+ ];
21
+ // A radio pair, marked as the Classifier tab marks its active row.
22
+ const option = (mode, label, hotkey) =>
23
+ ui.Button({
24
+ key: mode,
25
+ label: `${frame.mode === mode ? '◉' : '○'} ${label}`,
26
+ hotkey,
27
+ variant: frame.mode === mode ? 'primary' : 'secondary',
28
+ onPress: () => actions.mode(mode),
29
+ });
30
+ const { classifier } = frame;
31
+ return [
32
+ ui.line(
33
+ ui.head('ROUTER'),
34
+ ' ',
35
+ option('auto', 'on', 'o'),
36
+ ' ',
37
+ option('manual', 'off', 'f'),
38
+ ' ',
39
+ classifier.repeated
40
+ ? ui.dim('see below')
41
+ : ui.text(classifier.text, { color: toneColor(classifier.tone, 'terminal') }),
42
+ ...end,
43
+ ),
44
+ ];
45
+ }
46
+
47
+ // A warning as the terminal draws it, indented; one that carries a Button, or would on another tab, leaves room for it.
48
+ const noticeLine = (ui, notice) =>
49
+ ui.notice(
50
+ notice.tone,
51
+ ` ${notice.text}${notice.action === undefined ? '' : ' '}`,
52
+ notice.action
53
+ ? ui.Button({
54
+ key: notice.action.key,
55
+ label: notice.action.label,
56
+ hotkey: notice.action.hotkey,
57
+ onPress: notice.action.press,
58
+ })
59
+ : null,
60
+ );
61
+
62
+ // The tabs carry their number keys in their labels, so every key the pane answers is on screen. A row wider than the
63
+ // pane drops the digits (the keys still answer), and then moves ? to the header line's end, so Ink never squeezes the
64
+ // spaces between the tabs away.
65
+ function header(ui, frame, tabs, tab, actions, columns) {
66
+ const labels = (numbered) =>
67
+ tabs.map(({ id, key, label }) => `${numbered ? `${key} ` : ''}${label}${frame.unsaved[id] ? ' ●' : ''}`);
68
+ // As drawn: a Button is its label in `[ ` and ` ]`, a space after each tab.
69
+ const width = (row, withHelp) => row.reduce((n, name) => n + name.length + 5, 0) + (withHelp ? 5 : -1);
70
+ const fits = (row, withHelp) => !Number.isFinite(columns) || width(row, withHelp) <= columns;
71
+ const digits = fits(labels(true), true);
72
+ const names = labels(digits);
73
+ const help = ui.Button({
74
+ key: 'help',
75
+ label: '?',
76
+ variant: frame.helpOpen ? 'primary' : 'secondary',
77
+ onPress: actions.help,
78
+ });
79
+ const helpOnRow = fits(names, true);
80
+ return [
81
+ ...routingState(ui, frame, actions, helpOnRow ? null : help),
82
+ ui.line(
83
+ ...tabs.flatMap(({ id, key }, i) => [
84
+ i ? ' ' : null,
85
+ ui.Button({
86
+ key: `tab-${id}`,
87
+ label: names[i],
88
+ hotkey: key,
89
+ variant: tab === id ? 'primary' : 'secondary',
90
+ onPress: () => actions.tab(id),
91
+ }),
92
+ ]),
93
+ ...(helpOnRow ? [' ', help] : []),
94
+ ),
95
+ ...frame.notices.filter((notice) => !notice.graphicalOnly).map((notice) => noticeLine(ui, notice)),
96
+ ui.text(' '),
97
+ ];
98
+ }
99
+
100
+ // The save bar as the terminal's lines: router.json's diff on the two draft tabs, the count with Save and Discard,
101
+ // then the notice and Undo.
102
+ function statusBar(ui, bar) {
103
+ const out = [];
104
+ if (bar.count) {
105
+ out.push(ui.text(' '));
106
+ if (bar.listed) {
107
+ out.push(ui.dim('router.json changes:'));
108
+ for (const row of bar.rows)
109
+ out.push(
110
+ ui.text(`- ${row.key.padEnd(30)} ${ui.say(row.before)}`, { color: toneColor('danger', 'terminal') }),
111
+ ui.text(`+ ${row.key.padEnd(30)} ${ui.say(row.after)}`, { color: toneColor('success', 'terminal') }),
112
+ );
113
+ }
114
+ const { save, discard } = bar;
115
+ out.push(
116
+ ui.line(
117
+ ui.text(`${bar.pending.text} `, { color: toneColor(bar.pending.tone, 'terminal') }),
118
+ ui.Button({ key: save.key, label: save.label, hotkey: save.hotkey, variant: 'primary', onPress: save.press }),
119
+ ' ',
120
+ ui.Button({ key: discard.key, label: discard.label, hotkey: discard.hotkey, onPress: discard.press }),
121
+ bar.listed ? null : ui.dim(` ${bar.elsewhere}`),
122
+ ),
123
+ );
124
+ }
125
+ const undo = bar.undo
126
+ ? ui.Button({ key: bar.undo.key, label: bar.undo.label, hotkey: bar.undo.hotkey, onPress: bar.undo.press })
127
+ : null;
128
+ if (bar.notice || undo) out.push(ui.text(' '));
129
+ if (bar.notice)
130
+ out.push(
131
+ ui.line(
132
+ ui.text(`${bar.notice.text} `, { color: toneColor(bar.notice.tone, 'terminal') }),
133
+ bar.undo?.confirms ? undo : null,
134
+ ),
135
+ );
136
+ if (undo && !bar.undo.confirms) out.push(ui.line(ui.dim(`${bar.undo.last} `), undo));
137
+ return out;
138
+ }
139
+
140
+ // One Text per paragraph, wrapping to the pane.
141
+ const helpLines = (ui, help) => [
142
+ ui.head(help.head),
143
+ ...help.paragraphs.map((paragraph) => ui.text(ui.say(paragraph.text), { dimColor: true, wrap: 'wrap' })),
144
+ ui.text(' '),
145
+ ];
146
+
147
+ /**
148
+ * The pane on the terminal: the header, the profile and help when asked, the tab's rows, then the save bar. There is no
149
+ * Close button: Esc and the pane's own close dismiss it.
150
+ * @param {any} ui the terminal kit
151
+ * @param {ReturnType<typeof import('../model/frame.mjs').frameOf>} frame
152
+ * @param {{ id: string, key: string, label: string }[]} tabs
153
+ * @param {string} tab the shown tab's id
154
+ * @param {any} actions
155
+ * @param {object[]} content the tab's rows
156
+ * @param {number | null} columns
157
+ */
158
+ export function terminalFrame(ui, frame, tabs, tab, actions, content, columns) {
159
+ return ui.Box({
160
+ flexDirection: 'column',
161
+ children: [
162
+ ...header(ui, frame, tabs, tab, actions, columns),
163
+ ...(frame.help ? helpLines(ui, frame.help) : []),
164
+ ...(frame.profile ? [ui.dim(ui.say(frame.profile))] : []),
165
+ ...content,
166
+ ...statusBar(ui, frame.bar),
167
+ ],
168
+ });
169
+ }
@@ -0,0 +1,133 @@
1
+ import { TIERS } from '../../config.mjs';
2
+ import { tierBars } from '../../display.mjs';
3
+ import { mergePairs, STRIP_CELLS } from '../../view.mjs';
4
+ import { cellTier, stripNote } from '../model/format.mjs';
5
+ import { TIER_COLOR, toneColor } from '../tokens.mjs';
6
+
7
+ // Activity verbs are orange italic, as on the band.
8
+ const ACTIVITY_STYLE = { color: toneColor('activity', 'terminal'), italic: true };
9
+ // A trend's blocks, lowest to highest; a bucket without anything is a dot.
10
+ const SPARK = '▁▂▃▄▅▆▇█';
11
+
12
+ // The session strip fitted to the pane beside its note: pairs of cells merge until both fit.
13
+ function fittedStrip(strip, columns, since, now) {
14
+ let { per, cells } = strip;
15
+ const room = () =>
16
+ Number.isFinite(columns) ? Math.max(8, columns - 4 - stripNote(strip, per, since, now).length) : STRIP_CELLS;
17
+ while (cells.length > room()) {
18
+ cells = mergePairs(cells);
19
+ per *= 2;
20
+ }
21
+ return { cells, note: stripNote(strip, per, since, now) };
22
+ }
23
+
24
+ /** @param {any} elements */
25
+ export function kit({ Box, Text, Button, Select }) {
26
+ const text = (children, style = {}) => Text({ ...style, children });
27
+ const dim = (children) => text(children, { dimColor: true });
28
+ const head = (children) => text(children, { weight: 'semibold', color: toneColor('accent', 'terminal') });
29
+ const toPart = (part) => (typeof part === 'string' ? text(part) : part);
30
+ // One row of mixed parts; strings become plain Text, nulls are dropped.
31
+ const line = (...parts) => Box({ children: parts.filter((part) => part !== null && part !== '').map(toPart) });
32
+ // A fixed-width column, so controls of different widths still line up; it never shrinks in a narrow pane.
33
+ const cell = (width, part) => Box({ width, flexShrink: 0, children: [toPart(part)] });
34
+ const bars = (level) => text(tierBars(level), { color: TIER_COLOR[level] });
35
+ const tier = (name, children = name) => text(children, { weight: 'semibold', color: TIER_COLOR[name] });
36
+ const base = {
37
+ Box,
38
+ Button,
39
+ Select,
40
+ text,
41
+ // A sentence in this surface's wording.
42
+ say: (words) => (typeof words === 'string' ? words : words.terminal),
43
+ // A tone's colour on the terminal: a theme key, or a hex for a tier, a model and an activity.
44
+ color: (tone) => toneColor(tone, 'terminal'),
45
+ dim,
46
+ head,
47
+ tier,
48
+ // A tier as its bars, the way the band and Now draw it.
49
+ bars,
50
+ // The tier meter: its bars once a tier is lit, a dim ring before.
51
+ tierMeter: (name, state) => (state === 'lit' && name ? bars(name) : dim('○')),
52
+ activity: (activity) => text(activity, ACTIVITY_STYLE),
53
+ line,
54
+ cell,
55
+ };
56
+ return { ...base, ...painters(base), ...forms(base) };
57
+ }
58
+
59
+ // The composite helpers the terminal renderers draw with, built from the kit's own primitives. The graphical kit draws
60
+ // its own section, meter, strip, stat, choice and table.
61
+ function painters({ text, dim, tier, bars, line }) {
62
+ const stripRows = (strip, { columns, now, since }) => {
63
+ if (!strip?.cells?.length) return [];
64
+ const fitted = fittedStrip(strip, Number.isFinite(columns) ? columns - 8 : columns, since, now);
65
+ const counts = {};
66
+ for (const reply of strip.cells) for (const [key, n] of Object.entries(reply)) counts[key] = (counts[key] ?? 0) + n;
67
+ return [
68
+ line(
69
+ dim(' tiers '),
70
+ ...fitted.cells.map((one) => {
71
+ const name = cellTier(one);
72
+ return name === 'none' ? dim('·') : tier(name, '█');
73
+ }),
74
+ fitted.note ? dim(` ${fitted.note}`) : null,
75
+ ),
76
+ line(
77
+ ' ',
78
+ ...TIERS.filter((name) => counts[name]).flatMap((name) => [bars(name), dim(` ${counts[name]} `)]),
79
+ counts.none ? dim(`· not routed ${counts.none}`) : null,
80
+ ),
81
+ ];
82
+ };
83
+ return {
84
+ // The session's replies as cells coloured by tier, then the replies per tier as their bars; `since` is when the
85
+ // session's usage record starts, for the note after a restart.
86
+ stripRows,
87
+ // A trend as one row of blocks scaled from zero to its peak, a dot for an empty bucket, in the tone's colour.
88
+ trend: ({ points, tone }) => {
89
+ const max = Math.max(...points);
90
+ const glyphs = points.map((v) => (v > 0 ? SPARK[Math.min(7, Math.floor((v / max) * 8))] : '·')).join('');
91
+ return text(glyphs, { color: toneColor(tone, 'terminal') });
92
+ },
93
+ // A one-line message in the tone's colour, with its action beside it.
94
+ notice: (tone, message, action) => line(text(message, { color: toneColor(tone, 'terminal') }), action ?? null),
95
+ };
96
+ }
97
+
98
+ // What the terminal tabs share when they lay out a control: a labelled field, its help, a Select, the unsaved mark and
99
+ // the line that sends you to Policy. The graphical kit draws its own.
100
+ function forms({ Box, Button, Select, text, line, cell, say, color }) {
101
+ return {
102
+ // A field: its label in a fixed column, then its lines stacked in a column of their own, so a line that wraps in a
103
+ // narrow pane stays inside that column instead of falling back to the pane's edge.
104
+ field: (width, label, lines) =>
105
+ Box({
106
+ children: [
107
+ cell(width, label),
108
+ Box({ flexDirection: 'column', flexGrow: 1, flexShrink: 1, children: lines.filter(Boolean) }),
109
+ ],
110
+ }),
111
+ // A line of help under a control, wrapping inside its field's column; dim unless given a style.
112
+ help: (words, style = { dimColor: true }) => text(words, { ...style, wrap: 'wrap' }),
113
+ // A Select over the control's options, or its value as text on a surface without Select.
114
+ select: (control) =>
115
+ Select
116
+ ? Select({
117
+ key: control.key,
118
+ value: control.value,
119
+ options: control.options.map(({ value, label }) => ({ value, label: say(label) })),
120
+ onSelect: control.pick,
121
+ })
122
+ : control.text,
123
+ // A two-cell column holding the model's unsaved mark where a value changed, so the rows beside it line up whether
124
+ // or not it shows.
125
+ marker: (mark) => cell(2, mark ? text(mark.glyph, { color: color(mark.tone) }) : ''),
126
+ // Cache rebuilds cost more than the cheaper models saved: the policy's switch weight is the lever.
127
+ lostLine: (onPress) =>
128
+ line(
129
+ text(' ▲ switches cost more than they saved ', { color: color('warning') }),
130
+ Button({ key: 'session-policy', label: 'Policy', onPress }),
131
+ ),
132
+ };
133
+ }