@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.
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +74 -0
- package/README.md +55 -7
- package/docs/architecture.md +30 -4
- package/docs/configuration.md +23 -14
- package/docs/evaluation.md +3 -3
- package/docs/native-router.md +12 -4
- package/docs/router-band.svg +140 -142
- package/docs/router-pane-classifier.svg +48 -31
- package/docs/router-pane-now.svg +162 -120
- package/docs/router-pane-policy.svg +79 -47
- package/docs/router-pane-routes.svg +8 -8
- package/docs/router-pane-usage-whatif.svg +14 -14
- package/docs/router-pane-usage.svg +20 -20
- package/docs/routing-modes.md +10 -8
- package/docs/user-guide.md +151 -22
- package/hooks/native-router.mjs +415 -88
- package/lib/activity-stats.mjs +23 -4
- package/lib/activity.mjs +7 -5
- package/lib/auto-engine.mjs +13 -6
- package/lib/auto-route.mjs +10 -4
- package/lib/band.mjs +35 -440
- package/lib/classifier-client.mjs +12 -8
- package/lib/config-file.mjs +7 -2
- package/lib/config.mjs +24 -9
- package/lib/cost.mjs +5 -1
- package/lib/costs.mjs +4 -2
- package/lib/decisions.mjs +13 -9
- package/lib/display.mjs +71 -20
- package/lib/facts.mjs +3 -0
- package/lib/failover.mjs +55 -0
- package/lib/modes.mjs +8 -4
- package/lib/panel.mjs +75 -2167
- package/lib/policy.mjs +7 -6
- package/lib/replay-trace.mjs +5 -2
- package/lib/replay.mjs +7 -1
- package/lib/route-line.mjs +46 -0
- package/lib/route.mjs +51 -32
- package/lib/savings.mjs +14 -3
- package/lib/subagents.mjs +41 -11
- package/lib/subcommands.mjs +60 -0
- package/lib/ui/fallback.mjs +22 -0
- package/lib/ui/graphical/band.mjs +256 -0
- package/lib/ui/graphical/charts.mjs +163 -0
- package/lib/ui/graphical/classifier.mjs +153 -0
- package/lib/ui/graphical/frame.mjs +231 -0
- package/lib/ui/graphical/kit.mjs +156 -0
- package/lib/ui/graphical/meter.mjs +25 -0
- package/lib/ui/graphical/now.mjs +357 -0
- package/lib/ui/graphical/policy.mjs +56 -0
- package/lib/ui/graphical/routes.mjs +264 -0
- package/lib/ui/graphical/squares.mjs +29 -0
- package/lib/ui/graphical/usage.mjs +376 -0
- package/lib/ui/model/band.mjs +441 -0
- package/lib/ui/model/classifier.mjs +308 -0
- package/lib/ui/model/draft.mjs +62 -0
- package/lib/ui/model/format.mjs +41 -0
- package/lib/ui/model/frame.mjs +359 -0
- package/lib/ui/model/now.mjs +421 -0
- package/lib/ui/model/policy.mjs +194 -0
- package/lib/ui/model/routes.mjs +392 -0
- package/lib/ui/model/usage-agents.mjs +311 -0
- package/lib/ui/model/usage-breakdown.mjs +237 -0
- package/lib/ui/model/usage-shared.mjs +58 -0
- package/lib/ui/model/usage.mjs +330 -0
- package/lib/ui/terminal/band.mjs +253 -0
- package/lib/ui/terminal/classifier.mjs +150 -0
- package/lib/ui/terminal/frame.mjs +169 -0
- package/lib/ui/terminal/kit.mjs +162 -0
- package/lib/ui/terminal/now.mjs +293 -0
- package/lib/ui/terminal/policy.mjs +55 -0
- package/lib/ui/terminal/routes.mjs +167 -0
- package/lib/ui/terminal/usage.mjs +363 -0
- package/lib/ui/terminal/wrap.mjs +37 -0
- package/lib/ui/tokens.mjs +106 -0
- package/lib/usage.mjs +5 -5
- package/lib/view.mjs +5 -2
- package/package.json +4 -2
- package/types/index.d.ts +17 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// The band drawn natively on desktop, VS Code and mobile: one row with the route on the left, which yields when the
|
|
2
|
+
// window narrows, and the readings, the pin and Router on the right, which stay. The segments are the terminal band's
|
|
3
|
+
// (bandSegments in ui/model/band.mjs), so what shows and when is decided in one place; only how they are drawn differs.
|
|
4
|
+
import {
|
|
5
|
+
agentParts,
|
|
6
|
+
agentSegments,
|
|
7
|
+
BACK_CELLS,
|
|
8
|
+
bandSegments,
|
|
9
|
+
fitSegments,
|
|
10
|
+
NO_PIN,
|
|
11
|
+
pinOf,
|
|
12
|
+
routeLineSegments,
|
|
13
|
+
sessionSegments,
|
|
14
|
+
} from '../model/band.mjs';
|
|
15
|
+
import { toneColor } from '../tokens.mjs';
|
|
16
|
+
import { tierMeter } from './meter.mjs';
|
|
17
|
+
|
|
18
|
+
// Columns the right group keeps beyond the readings: the pin Select, and the Router button, which is its label in the
|
|
19
|
+
// `[ ` and ` ]` a Button draws (the terminal's Router is a plain label after a gap, narrower).
|
|
20
|
+
const PIN_SELECT = 20;
|
|
21
|
+
const ROUTER_BUTTON = 'Router'.length + 4;
|
|
22
|
+
|
|
23
|
+
function textProps(p, extra = {}) {
|
|
24
|
+
const tone = p.tone ?? (p.dim ? 'muted' : undefined);
|
|
25
|
+
return {
|
|
26
|
+
...(tone ? { color: toneColor(tone, 'graphical') } : {}),
|
|
27
|
+
...(p.weight ? { weight: p.weight } : {}),
|
|
28
|
+
...(p.italic ? { italic: true } : {}),
|
|
29
|
+
...extra,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// A run of text parts as one Text: the parts nested so each keeps its own colour, the run's edges trimmed because
|
|
34
|
+
// the row's gap, not spaces, sits between its items.
|
|
35
|
+
function run(elements, parts, extra) {
|
|
36
|
+
const trimmed = parts.map((p) => ({ ...p }));
|
|
37
|
+
/** @type {[number, 'trimStart' | 'trimEnd', 'shift' | 'pop'][]} */
|
|
38
|
+
const edges = [
|
|
39
|
+
[0, 'trimStart', 'shift'],
|
|
40
|
+
[-1, 'trimEnd', 'pop'],
|
|
41
|
+
];
|
|
42
|
+
for (const [end, trim, drop] of edges) {
|
|
43
|
+
while (trimmed.length) {
|
|
44
|
+
const p = trimmed.at(end);
|
|
45
|
+
p.text = p.text[trim]();
|
|
46
|
+
if (p.text) break;
|
|
47
|
+
trimmed[drop]();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (!trimmed.length) return null;
|
|
51
|
+
const { Text } = elements;
|
|
52
|
+
if (trimmed.length === 1) return Text({ ...textProps(trimmed[0], extra), children: trimmed[0].text });
|
|
53
|
+
return Text({
|
|
54
|
+
...extra,
|
|
55
|
+
children: trimmed.map((p) => Text({ ...textProps(p), children: p.text })),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// A segment's parts as elements: its meter bars as one Svg, consecutive text as one Text, buttons set apart.
|
|
60
|
+
function draw(elements, parts, extra) {
|
|
61
|
+
const out = [];
|
|
62
|
+
const buttons = [];
|
|
63
|
+
let text = [];
|
|
64
|
+
const flush = () => {
|
|
65
|
+
const el = run(elements, text, extra);
|
|
66
|
+
if (el) out.push(el);
|
|
67
|
+
text = [];
|
|
68
|
+
};
|
|
69
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
70
|
+
const p = parts[i];
|
|
71
|
+
if (p.meter) {
|
|
72
|
+
flush();
|
|
73
|
+
const lit = p.meter !== 'off' && p.meter !== 'pending';
|
|
74
|
+
out.push(tierMeter(elements, lit ? p.meter : null, lit ? 'lit' : p.meter));
|
|
75
|
+
} else if (p.button) {
|
|
76
|
+
flush();
|
|
77
|
+
buttons.push(p.button);
|
|
78
|
+
} else text.push(p);
|
|
79
|
+
}
|
|
80
|
+
flush();
|
|
81
|
+
return { out, buttons };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// A graphical row shows a gauge's figure alone. The space a gauge sat between closes up, so "plan 5h ▰▰▱ 37%" reads
|
|
85
|
+
// "plan 5h 37%". The tier meter is drawn as an Svg and stays.
|
|
86
|
+
function withoutGauge(seg) {
|
|
87
|
+
const parts = [];
|
|
88
|
+
for (const p of seg.parts) {
|
|
89
|
+
if (p.gauge !== undefined) continue;
|
|
90
|
+
const before = parts.at(-1);
|
|
91
|
+
parts.push(
|
|
92
|
+
before && !before.button && /\s$/.test(before.text) && !p.button ? { ...p, text: p.text.trimStart() } : p,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return { ...seg, parts };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// "ctx 62%": muted until the figure asks for attention.
|
|
99
|
+
function reading(elements, seg) {
|
|
100
|
+
const alert = seg.parts.find((p) => p.tone === 'warning' || p.tone === 'danger')?.tone;
|
|
101
|
+
const text = seg.parts
|
|
102
|
+
.map((p) => p.text)
|
|
103
|
+
.join('')
|
|
104
|
+
.replace(/\s+/g, ' ')
|
|
105
|
+
.trim();
|
|
106
|
+
return elements.Text({
|
|
107
|
+
size: 'footnote',
|
|
108
|
+
color: toneColor(alert ?? 'muted', 'graphical'),
|
|
109
|
+
tabular: true,
|
|
110
|
+
children: text,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function pinSelect(elements, pin, actions) {
|
|
115
|
+
return elements.Select({
|
|
116
|
+
key: 'band-pin',
|
|
117
|
+
options: pin.options,
|
|
118
|
+
value: pin.value,
|
|
119
|
+
onSelect: (value) => (value === NO_PIN ? actions.unpin() : actions.pin(value)),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// A subagent's band: one row of what was chosen and how, with a way back to the main conversation when the caller has
|
|
124
|
+
// one, and no Router, since a subagent's model is fixed at spawn and the pane belongs to the main thread.
|
|
125
|
+
function renderAgent(elements, parts, back) {
|
|
126
|
+
const text = run(elements, parts, { wrap: 'truncate-end' });
|
|
127
|
+
return elements.Box({
|
|
128
|
+
key: 'router-band',
|
|
129
|
+
flexDirection: 'row',
|
|
130
|
+
flexWrap: 'nowrap',
|
|
131
|
+
alignItems: 'center',
|
|
132
|
+
overflow: 'hidden',
|
|
133
|
+
paddingX: 1,
|
|
134
|
+
gap: 'md',
|
|
135
|
+
children: [
|
|
136
|
+
back ? elements.Button({ key: 'band-back', label: '← Main', variant: 'secondary', onPress: back }) : null,
|
|
137
|
+
text,
|
|
138
|
+
].filter(Boolean),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// The running agents under the band, one Button each; the same words and fit as the terminal's.
|
|
143
|
+
function renderAgents(elements, rows, columns, actions) {
|
|
144
|
+
const segments = agentSegments(rows, columns, actions);
|
|
145
|
+
if (!segments.length) return null;
|
|
146
|
+
const { Box, Text, Button } = elements;
|
|
147
|
+
return Box({
|
|
148
|
+
flexDirection: 'row',
|
|
149
|
+
flexWrap: 'nowrap',
|
|
150
|
+
alignItems: 'center',
|
|
151
|
+
overflow: 'hidden',
|
|
152
|
+
gap: 'sm',
|
|
153
|
+
children: segments.flatMap((seg) => {
|
|
154
|
+
const only = seg?.parts[0];
|
|
155
|
+
if (!only) return [];
|
|
156
|
+
if (only.button) {
|
|
157
|
+
const { plain: _plain, ...props } = only.button;
|
|
158
|
+
return [Button({ ...props, variant: 'secondary' })];
|
|
159
|
+
}
|
|
160
|
+
return [Text({ color: toneColor('muted', 'graphical'), size: 'footnote', tabular: true, children: only.text })];
|
|
161
|
+
}),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** @param {{ columns: number, agentId?: string, agent?: object | null, agents?: any[], back?: boolean, now: number, surface?: string }} opts */
|
|
166
|
+
export function renderGraphicalBand(elements, config, view, usage, opts, actions) {
|
|
167
|
+
const { columns, agentId, agent, agents, back, now, surface } = opts;
|
|
168
|
+
if (agentId) {
|
|
169
|
+
const room = back && Number.isFinite(columns) ? Math.max(0, columns - BACK_CELLS) : columns;
|
|
170
|
+
return renderAgent(elements, agentParts(config, agent, room), back ? actions.back : null);
|
|
171
|
+
}
|
|
172
|
+
const { Box, Button } = elements;
|
|
173
|
+
const pin = pinOf(config, view);
|
|
174
|
+
// Room for the pin Select only where one is drawn.
|
|
175
|
+
const reserved = ROUTER_BUTTON + (pin ? PIN_SELECT : 0);
|
|
176
|
+
const width = Number.isFinite(columns) ? Math.max(0, columns - reserved) : columns;
|
|
177
|
+
// The pin Select shows a pending pin, so its text and ✕ would say it twice; without the Select they are the only way
|
|
178
|
+
// to cancel it. Mobile always hands a Select, but an app that did not name it in `draws` is sent an empty Box for
|
|
179
|
+
// it, and a hook cannot tell which: there the text and ✕ stay beside the Select.
|
|
180
|
+
const keepsUnpin = !pin || surface === 'mobile';
|
|
181
|
+
const all = bandSegments(config, view, usage, actions)
|
|
182
|
+
.filter((s) => keepsUnpin || !s.parts.some((p) => p.button?.key === 'band-unpin'))
|
|
183
|
+
.map((s) => (s.right ? withoutGauge(s) : s));
|
|
184
|
+
const segments = fitSegments(all, width);
|
|
185
|
+
const left = [];
|
|
186
|
+
const buttons = [];
|
|
187
|
+
for (const seg of segments.filter((s) => !s.right)) {
|
|
188
|
+
const drawn = draw(elements, seg.parts, { wrap: 'truncate-end', tabular: true });
|
|
189
|
+
left.push(...drawn.out);
|
|
190
|
+
buttons.push(...drawn.buttons);
|
|
191
|
+
}
|
|
192
|
+
// A narrow band drops the pin's words; its ✕ stays, so the pin can still be cancelled.
|
|
193
|
+
const unpin = all.flatMap((s) => s.parts).find((p) => p.button?.key === 'band-unpin')?.button;
|
|
194
|
+
if (unpin && !buttons.some((b) => b.key === 'band-unpin')) buttons.push(unpin);
|
|
195
|
+
const readings = segments.filter((s) => s.right).map((s) => reading(elements, s));
|
|
196
|
+
const main = {
|
|
197
|
+
flexDirection: 'row',
|
|
198
|
+
flexWrap: 'nowrap',
|
|
199
|
+
alignItems: 'center',
|
|
200
|
+
};
|
|
201
|
+
const bandRow = (key) =>
|
|
202
|
+
Box({
|
|
203
|
+
...(key ? { key } : {}),
|
|
204
|
+
...main,
|
|
205
|
+
justifyContent: 'space-between',
|
|
206
|
+
gap: 'md',
|
|
207
|
+
children: [
|
|
208
|
+
Box({ ...main, gap: 'sm', flexShrink: 1, overflow: 'hidden', children: left }),
|
|
209
|
+
Box({
|
|
210
|
+
...main,
|
|
211
|
+
gap: 'md',
|
|
212
|
+
flexShrink: 0,
|
|
213
|
+
children: [
|
|
214
|
+
...readings,
|
|
215
|
+
...buttons.map(({ plain: _plain, ...props }) => Button(props)),
|
|
216
|
+
pin ? pinSelect(elements, pin, actions) : null,
|
|
217
|
+
Button({ key: 'details', label: 'Router', variant: 'secondary', onPress: actions.open }),
|
|
218
|
+
].filter(Boolean),
|
|
219
|
+
}),
|
|
220
|
+
],
|
|
221
|
+
});
|
|
222
|
+
const running = renderAgents(elements, agents ?? [], columns, actions);
|
|
223
|
+
if (!view.bandDetail && !running) return bandRow('router-band');
|
|
224
|
+
// The session row: each reading one Text, set apart by the row's gap rather than the terminal's spaces.
|
|
225
|
+
const readingsRow = (
|
|
226
|
+
view.bandDetail ? fitSegments(sessionSegments(config, view, usage, now).map(withoutGauge), columns) : []
|
|
227
|
+
)
|
|
228
|
+
.map((seg) => run(elements, seg.parts, { size: 'footnote', tabular: true, wrap: 'truncate-end' }))
|
|
229
|
+
.filter(Boolean);
|
|
230
|
+
return Box({
|
|
231
|
+
key: 'router-band',
|
|
232
|
+
flexDirection: 'column',
|
|
233
|
+
children: [
|
|
234
|
+
bandRow(),
|
|
235
|
+
running,
|
|
236
|
+
readingsRow.length ? Box({ ...main, gap: 'md', overflow: 'hidden', children: readingsRow }) : null,
|
|
237
|
+
].filter(Boolean),
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// The route line the desktop app, VS Code and mobile place under a turn: one footnote row of the band's words, the tier
|
|
242
|
+
// as the band's Svg meter.
|
|
243
|
+
export function graphicalRouteLine(elements, config, line, { columns }) {
|
|
244
|
+
const segments = fitSegments(routeLineSegments(config, line), columns);
|
|
245
|
+
const children = segments.flatMap((seg) => draw(elements, seg.parts, { size: 'footnote', tabular: true }).out);
|
|
246
|
+
return elements.Box({
|
|
247
|
+
key: 'router-line',
|
|
248
|
+
flexDirection: 'row',
|
|
249
|
+
flexWrap: 'nowrap',
|
|
250
|
+
alignItems: 'center',
|
|
251
|
+
overflow: 'hidden',
|
|
252
|
+
gap: 'sm',
|
|
253
|
+
paddingX: 1,
|
|
254
|
+
children,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// The graphical kit's figures: the shared bar row (meter), the session strip and a headline stat.
|
|
2
|
+
import { TIERS } from '../../config.mjs';
|
|
3
|
+
import { cellTier, stripNote, tierLabel } from '../model/format.mjs';
|
|
4
|
+
import { GRAPHICAL_TRACK, svgColor } from '../tokens.mjs';
|
|
5
|
+
|
|
6
|
+
// The shared bar row: a label column, a flexible bar on a quiet track, a right-aligned figure column, then a note.
|
|
7
|
+
const BAR_LABEL = 10;
|
|
8
|
+
const BAR_FIGURE = 7;
|
|
9
|
+
// A trend's Svg: real pixel sizes in the markup (a percent width draws as a blank block), stretched to its slot.
|
|
10
|
+
const TREND_WIDTH = 1200;
|
|
11
|
+
const TREND_HEIGHT = 48;
|
|
12
|
+
|
|
13
|
+
// A fraction as a whole percent of a bar, 0 to 100, and at least 1 for any value above zero so a small reading never
|
|
14
|
+
// reads as empty; anything unreadable is an empty bar.
|
|
15
|
+
const share = (value) =>
|
|
16
|
+
`${Number.isFinite(value) && value > 0 ? Math.max(1, Math.round(Math.min(1, value) * 100)) : 0}%`;
|
|
17
|
+
|
|
18
|
+
// Whole percents for the parts that sum to exactly 100: each part's floor, then the points left over to the parts with
|
|
19
|
+
// the largest remainders; a part above zero never gets 0 while the largest can spare a point.
|
|
20
|
+
export function wholePercents(parts) {
|
|
21
|
+
const total = parts.reduce((sum, n) => sum + n, 0);
|
|
22
|
+
const exact = parts.map((n) => (total > 0 ? (n / total) * 100 : 0));
|
|
23
|
+
const out = exact.map(Math.floor);
|
|
24
|
+
let left = total > 0 ? 100 - out.reduce((sum, n) => sum + n, 0) : 0;
|
|
25
|
+
for (const i of exact
|
|
26
|
+
.map((value, n) => [value - Math.floor(value), n])
|
|
27
|
+
.sort((a, b) => b[0] - a[0] || a[1] - b[1])
|
|
28
|
+
.map(([, n]) => n)) {
|
|
29
|
+
if (left <= 0) break;
|
|
30
|
+
out[i] += 1;
|
|
31
|
+
left -= 1;
|
|
32
|
+
}
|
|
33
|
+
// Every part above zero keeps at least 1, taken from the largest, while there are points to give.
|
|
34
|
+
for (const i of out.keys()) {
|
|
35
|
+
if (parts[i] <= 0 || out[i] > 0) continue;
|
|
36
|
+
const largest = out.indexOf(Math.max(...out));
|
|
37
|
+
if (out[largest] <= 1) break;
|
|
38
|
+
out[largest] -= 1;
|
|
39
|
+
out[i] = 1;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {any} elements
|
|
46
|
+
* @param {{ row: (children: (object | null)[], extra?: object) => object, caption: (children: string, extra?: object) => object, color: (tone: string) => string | undefined }} kit
|
|
47
|
+
*/
|
|
48
|
+
export function charts({ Box, Text, Svg }, { row, caption, color }) {
|
|
49
|
+
return {
|
|
50
|
+
// The shared bar row: the label, the fill as a percent width of the track, and the figure beside it as text. A
|
|
51
|
+
// bar without a label (inline in a table) has no label column.
|
|
52
|
+
meter: ({ label, value, tone: fill, text: figure, caption: note }) =>
|
|
53
|
+
row(
|
|
54
|
+
[
|
|
55
|
+
label === undefined
|
|
56
|
+
? null
|
|
57
|
+
: Box({ width: BAR_LABEL, flexShrink: 0, children: [Text({ wrap: 'truncate-end', children: label })] }),
|
|
58
|
+
Box({
|
|
59
|
+
flexGrow: 1,
|
|
60
|
+
flexShrink: 1,
|
|
61
|
+
minWidth: 10,
|
|
62
|
+
height: 1,
|
|
63
|
+
backgroundColor: GRAPHICAL_TRACK,
|
|
64
|
+
flexDirection: 'row',
|
|
65
|
+
children: [Box({ width: share(value), height: 1, backgroundColor: color(fill) })],
|
|
66
|
+
}),
|
|
67
|
+
Box({
|
|
68
|
+
width: BAR_FIGURE,
|
|
69
|
+
flexShrink: 0,
|
|
70
|
+
justifyContent: 'flex-end',
|
|
71
|
+
children: [Text({ tabular: true, children: figure })],
|
|
72
|
+
}),
|
|
73
|
+
note ? caption(note, { tabular: true, wrap: 'truncate-end' }) : null,
|
|
74
|
+
],
|
|
75
|
+
{ flexWrap: 'nowrap', gap: 'md' },
|
|
76
|
+
),
|
|
77
|
+
// The session's replies in order as runs of tier-coloured cells, then the replies per tier as words beside a
|
|
78
|
+
// swatch, so the colour is never the only signal.
|
|
79
|
+
strip: (strip, { now, since }) => {
|
|
80
|
+
if (!strip?.cells?.length) return null;
|
|
81
|
+
// Percent widths need no fitting to the pane, so no cells merge: only the note is kept.
|
|
82
|
+
const note = stripNote(strip, strip.per, since, now);
|
|
83
|
+
const runs = [];
|
|
84
|
+
for (const one of strip.cells) {
|
|
85
|
+
const name = cellTier(one);
|
|
86
|
+
if (runs.at(-1)?.name === name) runs.at(-1).n += 1;
|
|
87
|
+
else runs.push({ name, n: 1 });
|
|
88
|
+
}
|
|
89
|
+
const swatch = (name) => (name === 'none' ? color('muted') : color(`tier:${name}`));
|
|
90
|
+
const widths = wholePercents(runs.map(({ n }) => n));
|
|
91
|
+
const cells = runs.map(({ name }, i) =>
|
|
92
|
+
Box({ width: `${widths[i]}%`, height: 1, backgroundColor: swatch(name) }),
|
|
93
|
+
);
|
|
94
|
+
const counts = {};
|
|
95
|
+
for (const reply of strip.cells)
|
|
96
|
+
for (const [key, n] of Object.entries(reply)) counts[key] = (counts[key] ?? 0) + n;
|
|
97
|
+
const legend = (name, words) =>
|
|
98
|
+
row([Box({ width: 1, height: 1, backgroundColor: swatch(name) }), caption(words, { tabular: true })], {
|
|
99
|
+
gap: 'xs',
|
|
100
|
+
});
|
|
101
|
+
return Box({
|
|
102
|
+
flexDirection: 'column',
|
|
103
|
+
gap: 'sm',
|
|
104
|
+
children: [
|
|
105
|
+
caption('Replies by tier'),
|
|
106
|
+
Box({ flexDirection: 'row', height: 1, children: cells }),
|
|
107
|
+
row(
|
|
108
|
+
[
|
|
109
|
+
...TIERS.filter((name) => counts[name]).map((name) => legend(name, `${tierLabel(name)} ${counts[name]}`)),
|
|
110
|
+
counts.none ? legend('none', `not routed ${counts.none}`) : null,
|
|
111
|
+
note ? caption(note) : null,
|
|
112
|
+
],
|
|
113
|
+
{ flexWrap: 'wrap', gap: 'md' },
|
|
114
|
+
),
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
trend: (opts) => trendChart({ Box, Svg }, { row, caption }, opts),
|
|
119
|
+
// A headline figure, its delta in the tone's colour beside it, and its caption under it.
|
|
120
|
+
stat: ({ value, caption: words, delta, tone: name = /** @type {import('../tokens.mjs').Tone} */ ('primary') }) =>
|
|
121
|
+
Box({
|
|
122
|
+
flexDirection: 'column',
|
|
123
|
+
gap: 'xs',
|
|
124
|
+
children: [
|
|
125
|
+
row([
|
|
126
|
+
Text({ size: 'title', weight: 'semibold', tabular: true, children: value }),
|
|
127
|
+
delta ? Text({ tabular: true, color: color(name), children: delta }) : null,
|
|
128
|
+
]),
|
|
129
|
+
words ? caption(words, { wrap: 'truncate-end' }) : null,
|
|
130
|
+
].filter(Boolean),
|
|
131
|
+
}),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// A trend as a line over a faint fill, stretched to the slot's width, with its first and last bucket under it; the
|
|
136
|
+
// alt states the numbers.
|
|
137
|
+
function trendChart({ Box, Svg }, { row, caption }, { points, labels, alt, tone: name }) {
|
|
138
|
+
const values = (points.length ? points : [0]).map((v) => (Number.isFinite(v) && v > 0 ? v : 0));
|
|
139
|
+
const max = Math.max(...values);
|
|
140
|
+
const last = values.length - 1;
|
|
141
|
+
const x = (i) => (last ? Math.round((i * TREND_WIDTH) / last) : TREND_WIDTH / 2);
|
|
142
|
+
const y = (v) => Math.round((TREND_HEIGHT - 3 - (max > 0 ? v / max : 0) * (TREND_HEIGHT - 6)) * 10) / 10;
|
|
143
|
+
const line = values.map((v, i) => `${x(i)},${y(v)}`).join(' ');
|
|
144
|
+
const color = svgColor(name);
|
|
145
|
+
const source =
|
|
146
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${TREND_WIDTH}" height="${TREND_HEIGHT}" viewBox="0 0 ${TREND_WIDTH} ${TREND_HEIGHT}" preserveAspectRatio="none">` +
|
|
147
|
+
`<polygon points="${x(0)},${TREND_HEIGHT} ${line} ${x(last)},${TREND_HEIGHT}" fill="${color}" fill-opacity="0.12"/>` +
|
|
148
|
+
`<polyline points="${line}" fill="none" stroke="${color}" stroke-width="2" stroke-linejoin="round" vector-effect="non-scaling-stroke"/></svg>`;
|
|
149
|
+
return Box({
|
|
150
|
+
flexDirection: 'column',
|
|
151
|
+
gap: 'xs',
|
|
152
|
+
children: [
|
|
153
|
+
Box({
|
|
154
|
+
overflow: 'hidden',
|
|
155
|
+
flexDirection: 'column',
|
|
156
|
+
children: [Svg({ source, alt, height: TREND_HEIGHT })],
|
|
157
|
+
}),
|
|
158
|
+
row([caption(labels[0], { tabular: true }), caption(labels.at(-1), { tabular: true })], {
|
|
159
|
+
justifyContent: 'space-between',
|
|
160
|
+
}),
|
|
161
|
+
],
|
|
162
|
+
});
|
|
163
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// The Classifier tab as cards for desktop, VS Code and mobile, drawn from the model in ui/model/classifier.mjs: the active
|
|
2
|
+
// classifier's card (its status, readings, deadline, wait, agreement, health and credentials), the others as compact
|
|
3
|
+
// rows in one card, then Across sessions.
|
|
4
|
+
import { isFiniteNumber } from '../tokens.mjs';
|
|
5
|
+
|
|
6
|
+
// A compact row's fixed columns: the longest label ("Clef Flash") and status ("No account ID"), with room.
|
|
7
|
+
const NAME = 14;
|
|
8
|
+
const STATUS = 16;
|
|
9
|
+
// A pane narrower than this, or one that reports no width, puts a compact row's detail and Buttons on a second line.
|
|
10
|
+
const NARROW = 80;
|
|
11
|
+
|
|
12
|
+
// A reading: its label and its value in its tone.
|
|
13
|
+
const reading = (ui, one) =>
|
|
14
|
+
ui.field(one.label, [ui.text(one.value, { color: ui.color(one.tone ?? 'primary'), tabular: true, wrap: 'wrap' })], {
|
|
15
|
+
help: one.help,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {any} ui the graphical kit
|
|
20
|
+
* @param {import('../model/classifier.mjs').ClassifierModel} model
|
|
21
|
+
* @param {{ columns: number | null }} opts
|
|
22
|
+
*/
|
|
23
|
+
export function graphicalClassifier(ui, model, { columns }) {
|
|
24
|
+
const { Box } = ui;
|
|
25
|
+
const narrow = !isFiniteNumber(columns) || columns < NARROW;
|
|
26
|
+
return [
|
|
27
|
+
Box({
|
|
28
|
+
flexDirection: 'column',
|
|
29
|
+
gap: 'lg',
|
|
30
|
+
children: [
|
|
31
|
+
model.current ? classifierCard(ui, model.current, model.active) : null,
|
|
32
|
+
model.others
|
|
33
|
+
? ui.section(
|
|
34
|
+
model.others.title,
|
|
35
|
+
{},
|
|
36
|
+
model.others.rows.map((row) => compactRow(ui, row, narrow)),
|
|
37
|
+
)
|
|
38
|
+
: null,
|
|
39
|
+
ui.muted(ui.say(model.probeNote), { size: 'footnote', wrap: 'wrap' }),
|
|
40
|
+
model.store
|
|
41
|
+
? ui.section(model.store.title, { hint: model.store.hint }, [
|
|
42
|
+
...model.store.rows.map((row) => reading(ui, row)),
|
|
43
|
+
model.store.unpriced ? ui.muted(model.store.unpriced, { size: 'footnote', wrap: 'wrap' }) : null,
|
|
44
|
+
])
|
|
45
|
+
: null,
|
|
46
|
+
].filter(Boolean),
|
|
47
|
+
}),
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param {any} ui
|
|
53
|
+
* @param {import('../model/classifier.mjs').ClassifierRow} row
|
|
54
|
+
* @param {import('../model/classifier.mjs').ClassifierModel['active']} active
|
|
55
|
+
*/
|
|
56
|
+
// The header's action: the active mark under the classifier's key.
|
|
57
|
+
function activeMark(ui, row, active) {
|
|
58
|
+
return ui.Box({
|
|
59
|
+
key: row.key,
|
|
60
|
+
flexDirection: 'row',
|
|
61
|
+
alignItems: 'center',
|
|
62
|
+
gap: 'xs',
|
|
63
|
+
flexShrink: 0,
|
|
64
|
+
children: [
|
|
65
|
+
ui.text(active.mark.glyph, { color: ui.color(active.mark.tone) }),
|
|
66
|
+
ui.text(active.mark.text, { size: 'footnote', weight: 'semibold', color: ui.color(active.mark.tone) }),
|
|
67
|
+
],
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {any} ui
|
|
73
|
+
* @param {import('../model/classifier.mjs').ClassifierRow} row
|
|
74
|
+
* @param {import('../model/classifier.mjs').ClassifierModel['active']} active
|
|
75
|
+
*/
|
|
76
|
+
// The active classifier's full card.
|
|
77
|
+
function classifierCard(ui, row, active) {
|
|
78
|
+
const { Box } = ui;
|
|
79
|
+
const { credentials } = row;
|
|
80
|
+
return ui.section(row.label, { action: activeMark(ui, row, active) }, [
|
|
81
|
+
ui.text(row.status.text, { color: ui.color(row.status.tone), wrap: 'wrap' }),
|
|
82
|
+
...row.readings.map((one) => reading(ui, one)),
|
|
83
|
+
ui.field(active.deadline.label, [ui.select(active.deadline.control)], { help: ui.say(active.deadline.help) }),
|
|
84
|
+
...active.readings.map((one) => reading(ui, one)),
|
|
85
|
+
ui.field(active.sends.label, [ui.say(active.sends.text)], { help: ui.say(active.sends.help) }),
|
|
86
|
+
reading(ui, { label: active.health.label, value: active.health.value, tone: active.health.tone }),
|
|
87
|
+
credentials
|
|
88
|
+
? Box({
|
|
89
|
+
flexDirection: 'row',
|
|
90
|
+
flexWrap: 'wrap',
|
|
91
|
+
alignItems: 'center',
|
|
92
|
+
gap: 'sm',
|
|
93
|
+
children: [
|
|
94
|
+
ui.Button({
|
|
95
|
+
key: credentials.key,
|
|
96
|
+
label: credentials.label,
|
|
97
|
+
...(credentials.hotkey ? { hotkey: credentials.hotkey } : {}),
|
|
98
|
+
onPress: credentials.press,
|
|
99
|
+
}),
|
|
100
|
+
credentials.note ? ui.muted(credentials.note, { size: 'footnote', wrap: 'wrap' }) : null,
|
|
101
|
+
].filter(Boolean),
|
|
102
|
+
})
|
|
103
|
+
: null,
|
|
104
|
+
]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {any} ui
|
|
109
|
+
* @param {import('../model/classifier.mjs').ClassifierRow} row
|
|
110
|
+
* @param {boolean} narrow
|
|
111
|
+
*/
|
|
112
|
+
// A classifier not in use, on one row: its name, its status, where its prompt goes with its probe result, then Set up
|
|
113
|
+
// where it lacks credentials and Use, right-aligned. A narrow pane puts the detail and Buttons on a second line.
|
|
114
|
+
function compactRow(ui, row, narrow) {
|
|
115
|
+
const { Box } = ui;
|
|
116
|
+
const name = Box({
|
|
117
|
+
width: NAME,
|
|
118
|
+
flexShrink: 0,
|
|
119
|
+
children: [ui.text(row.label, { weight: 'semibold', wrap: 'truncate-end' })],
|
|
120
|
+
});
|
|
121
|
+
const status = Box({
|
|
122
|
+
width: STATUS,
|
|
123
|
+
flexShrink: 0,
|
|
124
|
+
children: [ui.text(row.compact.status.text, { color: ui.color(row.compact.status.tone), wrap: 'truncate-end' })],
|
|
125
|
+
});
|
|
126
|
+
const detail = Box({
|
|
127
|
+
flexGrow: 1,
|
|
128
|
+
flexShrink: 1,
|
|
129
|
+
overflow: 'hidden',
|
|
130
|
+
children: [ui.muted(row.compact.detail, { tabular: true })],
|
|
131
|
+
});
|
|
132
|
+
const buttons = Box({
|
|
133
|
+
flexDirection: 'row',
|
|
134
|
+
alignItems: 'center',
|
|
135
|
+
gap: 'xs',
|
|
136
|
+
flexShrink: 0,
|
|
137
|
+
children: [
|
|
138
|
+
row.setup ? ui.Button({ key: row.setup.key, label: row.setup.label, onPress: row.setup.press }) : null,
|
|
139
|
+
row.use ? ui.Button({ key: row.key, label: row.use.label, onPress: row.press }) : null,
|
|
140
|
+
].filter(Boolean),
|
|
141
|
+
});
|
|
142
|
+
const line = (extra, children) =>
|
|
143
|
+
Box({ ...extra, flexDirection: 'row', flexWrap: 'nowrap', alignItems: 'center', gap: 'sm', children });
|
|
144
|
+
const key = `row-${row.key}`;
|
|
145
|
+
return narrow
|
|
146
|
+
? Box({
|
|
147
|
+
key,
|
|
148
|
+
flexDirection: 'column',
|
|
149
|
+
gap: 'xs',
|
|
150
|
+
children: [line({}, [name, status]), line({}, [detail, buttons])],
|
|
151
|
+
})
|
|
152
|
+
: line({ key }, [name, status, detail, buttons]);
|
|
153
|
+
}
|