@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,441 @@
1
+ import { activeClassifier, TIERS } from '../../config.mjs';
2
+ import { resetText } from '../../costs.mjs';
3
+ import {
4
+ API_ESTIMATE,
5
+ baselineName,
6
+ classifierLabel,
7
+ classifierStatus,
8
+ classifierWarns,
9
+ costText,
10
+ diffText,
11
+ modeLever,
12
+ modelName,
13
+ NOT_A_TIER_REASON,
14
+ percentPair,
15
+ pinCard,
16
+ pinShort,
17
+ reasonActivity,
18
+ reasonNamesActivity,
19
+ repliesText,
20
+ routingModeOf,
21
+ sessionCost,
22
+ sessionUsage,
23
+ shortReason,
24
+ tierBars,
25
+ unavailableReason,
26
+ usageMetrics,
27
+ usd,
28
+ } from '../../display.mjs';
29
+ import { isSameModel } from '../../route.mjs';
30
+ import { PASS_WHY } from '../../subagents.mjs';
31
+ import { isRouting } from '../../view.mjs';
32
+ import { limitTone, modelTone, savingTone } from '../tokens.mjs';
33
+
34
+ // Groups on a row sit apart by space alone: the meter, colours and glyphs already separate them.
35
+ export const SEPARATOR = ' ';
36
+ // Cells in the band's gauges: the context window and a plan window.
37
+ const CONTEXT_CELLS = 5;
38
+ const PLAN_CELLS = 8;
39
+ // The tier meter's bars, as many cells wide as it is drawn on the terminal.
40
+ const METER_CELLS = 4;
41
+ // The pin Select's "no pin" choice.
42
+ export const NO_PIN = 'none';
43
+ /** The "no pin" choice's words: what the Select offers with no pin, and how to drop a pending one. */
44
+ export const noPinLabel = (pending) => (pending ? 'No pin' : 'Pin to a tier');
45
+
46
+ /**
47
+ * One piece of a band segment: what it says and what it means, never how a surface draws it. A terminal maps `tone` to
48
+ * a colour and `gauge` and `meter` to glyphs; a graphical surface draws the same as theme colours, a figure and an Svg.
49
+ * @typedef {object} Part
50
+ * @property {string} [text]
51
+ * @property {import('../tokens.mjs').Tone} [tone] what the text means
52
+ * @property {boolean} [dim] secondary text
53
+ * @property {'semibold'} [weight]
54
+ * @property {boolean} [italic]
55
+ * @property {boolean} [chip] drawn as a label on a ground
56
+ * @property {number} [gauge] a gauge at this percent, `cells` wide on the terminal
57
+ * @property {number} [cells]
58
+ * @property {import('../tokens.mjs').Tier | 'off' | 'pending'} [meter] the tier meter: lit to the tier, off, or working
59
+ * @property {object} [button] a Button's props
60
+ * @property {number} [priority] 0 never drops
61
+ */
62
+
63
+ export const part = (text, props = {}) => ({ text, ...props });
64
+ export const dim = (text) => part(text, { dim: true });
65
+ export const button = (props) => ({ button: props });
66
+ const gauge = (percent, cells, tone) => ({ gauge: percent, cells, tone });
67
+ // `priority` 0 never drops; higher numbers drop first when the band is narrow. `right` segments sit at the row's end.
68
+ export const segment = (priority, ...parts) => ({ priority, parts: parts.flat().filter(Boolean) });
69
+ export const right = (seg) => ({ ...seg, right: true });
70
+ // The columns a part takes on the terminal, which both surfaces fit the band by. A Button is its label in `[ ` and ` ]`
71
+ // unless it is plain.
72
+ const partWidth = (p) =>
73
+ p.meter
74
+ ? METER_CELLS
75
+ : p.gauge !== undefined
76
+ ? p.cells
77
+ : p.button
78
+ ? p.button.label.length + (p.button.plain ? 0 : 4)
79
+ : p.text.length;
80
+ export const partsWidth = (parts) => parts.reduce((n, p) => n + partWidth(p), 0);
81
+ export const lineWidth = (segments, separator) =>
82
+ segments.reduce((sum, s, i) => sum + (i ? separator.length : 0) + partsWidth(s.parts), 0);
83
+
84
+ // Drops the highest-priority segments and parts, later ones first on a tie, until the line fits `columns`. If the
85
+ // priority-0 segments alone are too wide, the later ones go too, and the first is cut with an ellipsis: Ink would
86
+ // otherwise shrink every Text in the row and garble it. A width that is not a finite number fits everything.
87
+ export function fitSegments(segments, columns, separator = SEPARATOR) {
88
+ if (!Number.isFinite(columns)) return segments;
89
+ const kept = [...segments];
90
+ while (lineWidth(kept, separator) > columns) {
91
+ /** @type {{ priority: number, i: number, j?: number } | null} */
92
+ let drop = null;
93
+ for (const [i, s] of kept.entries()) {
94
+ for (const [j, p] of s.parts.entries()) {
95
+ if (p.priority > 0 && (!drop || p.priority >= drop.priority)) drop = { priority: p.priority, i, j };
96
+ }
97
+ if (kept.length > 1 && s.priority > 0 && (!drop || s.priority >= drop.priority))
98
+ drop = { priority: s.priority, i };
99
+ }
100
+ const dropped = drop;
101
+ if (dropped?.j !== undefined)
102
+ kept[dropped.i] = { ...kept[dropped.i], parts: kept[dropped.i].parts.filter((_, j) => j !== dropped.j) };
103
+ else if (dropped) kept.splice(dropped.i, 1);
104
+ else if (kept.length > 1) kept.pop();
105
+ else break;
106
+ }
107
+ const overflow = lineWidth(kept, separator) - columns;
108
+ if (overflow > 0 && kept.length) {
109
+ const parts = [...kept[0].parts];
110
+ const last = parts.findLastIndex((p) => p.text?.length > overflow);
111
+ if (last >= 0) parts[last] = { ...parts[last], text: `${parts[last].text.slice(0, -overflow - 1)}…` };
112
+ kept[0] = { ...kept[0], parts };
113
+ }
114
+ return kept;
115
+ }
116
+
117
+ // Signal-strength bars: lit up to the tier, dim above it, or all dim when off.
118
+ const meter = (tier) => ({ meter: tier ?? 'off' });
119
+
120
+ // The classifier is working: every bar in the accent tone.
121
+ const pendingMeter = () => ({ meter: 'pending' });
122
+
123
+ // "Haiku 5.5 · high", the model in bold so the eye finds it first.
124
+ const routeParts = (model, effort) => [
125
+ part(modelName(model), { weight: 'semibold' }),
126
+ effort === null || effort === undefined ? null : part(` · ${effort}`),
127
+ ];
128
+
129
+ function served(view) {
130
+ const { actualModel: actual, selectedModel: selected } = view;
131
+ return Boolean(actual && selected && !isSameModel(selected, actual));
132
+ }
133
+
134
+ // "Jev 93% ≥ 90%": the classifier's support for the switch the policy weighed, against the bar it set.
135
+ export function supportText(config, estimate) {
136
+ if (!Number.isFinite(estimate?.threshold)) return null;
137
+ const mass = Number.isFinite(estimate.upgradeMass) ? estimate.upgradeMass : estimate.downgradeMass;
138
+ if (!Number.isFinite(mass)) return null;
139
+ const [shown, bar] = percentPair(mass, estimate.threshold);
140
+ return `${classifierLabel(config)} ${shown} ${mass >= estimate.threshold ? '≥' : '<'} ${bar}`;
141
+ }
142
+
143
+ // The routing mode as a chip, only when it is not balanced: "cost", or "cost effort only" when the mode kept the model.
144
+ function modeChip(config, view) {
145
+ const { mode } = routingModeOf(view);
146
+ if (mode === 'balanced') return null;
147
+ const lever = modeLever(config, view);
148
+ return segment(5, part(` ${mode} `, { chip: true, tone: 'muted' }), lever ? dim(` ${lever}`) : null);
149
+ }
150
+
151
+ // Context fill and the last reply's cache reuse, kept at the row's end so they stay in one place turn after turn.
152
+ function gauges(config, view, usage) {
153
+ const metrics = usageMetrics(config, view, usage);
154
+ const out = [];
155
+ if (metrics.contextBar.percent !== null) {
156
+ const { percent, tone } = metrics.contextBar;
157
+ out.push(right(segment(3, dim('ctx '), gauge(percent, CONTEXT_CELLS, tone), part(` ${percent}%`, { tone }))));
158
+ }
159
+ if (metrics.cacheBar.percent !== null)
160
+ out.push(
161
+ right(
162
+ segment(4, dim('cache '), part(`${metrics.cacheBar.percent}%`, { tone: metrics.cacheBar.tone }), dim(' last')),
163
+ ),
164
+ );
165
+ return out;
166
+ }
167
+
168
+ export function bandSegments(config, view, usage, actions) {
169
+ const native = () => routeParts(view.nativeModel, view.nativeEffort);
170
+ if (view.phase === 'unavailable')
171
+ return [
172
+ segment(
173
+ 0,
174
+ part('✕ ', { tone: 'danger' }),
175
+ native(),
176
+ part(' '),
177
+ button({ key: 'band-fix', label: 'Fix', onPress: actions.open }),
178
+ ),
179
+ segment(1, part('routing unavailable', { tone: 'danger' })),
180
+ segment(3, part(unavailableReason(view), { tone: 'danger', dim: true })),
181
+ ];
182
+ // Turn on shares the first segment, which never drops and whose cut keeps buttons whole, so the band's one way back
183
+ // on survives any width.
184
+ if (view.mode === 'manual')
185
+ return [
186
+ segment(
187
+ 0,
188
+ dim('○ '),
189
+ native(),
190
+ part(' '),
191
+ button({ key: 'band-auto', label: 'Turn on', variant: 'primary', onPress: () => actions.mode('auto') }),
192
+ ),
193
+ segment(1, dim(`routing off${view.reason === 'model selected manually' ? ' · /model' : ''}`)),
194
+ view.reason === NOT_A_TIER_REASON ? segment(2, dim('not a routing tier')) : null,
195
+ ].filter(Boolean);
196
+ if (view.phase === 'choosing')
197
+ return [
198
+ segment(0, pendingMeter(), dim(' choosing…')),
199
+ segment(3, dim(`${classifierLabel(config)} ≤ ${activeClassifier(config).timeoutMs / 1000} s`)),
200
+ ];
201
+ const pin = view.pendingPin
202
+ ? segment(
203
+ 0,
204
+ part('⏵ next ', { tone: 'accent' }),
205
+ part(tierBars(view.pendingPin), { tone: `tier:${view.pendingPin}` }),
206
+ part(pinShort(config, view.pendingPin) && ` ${pinShort(config, view.pendingPin)}`, {
207
+ tone: `tier:${view.pendingPin}`,
208
+ }),
209
+ part(' '),
210
+ button({ key: 'band-unpin', label: '✕', plain: true, onPress: actions.unpin }),
211
+ )
212
+ : null;
213
+ if (!view.tier && view.phase === 'ready' && !view.actualModel)
214
+ return [segment(0, meter(null), part(' '), native()), segment(1, dim('ready')), pin, modeChip(config, view)].filter(
215
+ Boolean,
216
+ );
217
+ const main = segment(
218
+ 0,
219
+ view.tier ? meter(view.tier) : dim('○'),
220
+ part(' '),
221
+ routeParts(view.actualModel ?? view.selectedModel ?? view.nativeModel, view.effort),
222
+ served(view) ? part(' fallback', { tone: 'warning' }) : null,
223
+ );
224
+ const activityText = view.goal
225
+ ? `${view.goal}${view.taskPhase ? ` / ${view.taskPhase}` : ''}${view.activity && view.activity !== view.goal ? ` → ${view.activity}` : ''}`
226
+ : view.activity;
227
+ const activity =
228
+ activityText &&
229
+ config.activityRouting !== 'off' &&
230
+ ((view.goal && (view.taskPhase || view.goal !== view.activity)) || !reasonNamesActivity(view.reason))
231
+ ? segment(2, part(activityText, { tone: 'activity', italic: true }))
232
+ : null;
233
+ const reason = segment(1, dim(shortReason(view.reason, reasonActivity(config, view))));
234
+ // The warning leads and the route yields: when the band is narrow, what to fix matters more than the model kept.
235
+ if (classifierWarns(view.error))
236
+ return [
237
+ segment(0, part(`⚠ ${classifierStatus(config, view.error)}`, { tone: 'warning' })),
238
+ view.error === 'missing-key' || view.error === 'missing-account'
239
+ ? segment(0, button({ key: 'band-key', label: 'Set up', variant: 'primary', onPress: actions.key }))
240
+ : segment(2, button({ key: 'band-fix', label: 'Details', onPress: actions.open })),
241
+ { ...main, priority: 1 },
242
+ pin,
243
+ { ...reason, priority: 3 },
244
+ ].filter(Boolean);
245
+ const support = supportText(config, view.estimate);
246
+ // The policy's price for the switch it weighed: mostly the prompt cache the other model must write.
247
+ const switchUsd = view.estimate?.taxUsd;
248
+ return [
249
+ main,
250
+ activity,
251
+ pin,
252
+ reason,
253
+ modeChip(config, view),
254
+ support ? segment(6, dim(support)) : null,
255
+ Number.isFinite(switchUsd) && switchUsd >= 0.005 ? segment(6, dim(`switch ≈ ${usd(switchUsd)}`)) : null,
256
+ ...gauges(config, view, usage),
257
+ ].filter(Boolean);
258
+ }
259
+
260
+ // The second row: the plan windows on a Claude plan, then this session's cost with its subagents, and how it compares
261
+ // with your /model (or `unrouted` with subagents):
262
+ // "plan 5h ▰▰▰▱▱▱▱▱ 37% ↻17:00 7d 62% session ≈$7.60 API estimate −$1.76 (−19%) vs Sonnet 5.5 24 replies".
263
+ // An effort-only profile shows its effort mix instead of a saving. When the band is narrow the reset time, the
264
+ // counts and the words go first; the 5h window and the session total stay.
265
+ export function sessionSegments(config, view, usage, now = Date.now()) {
266
+ const cost = sessionCost(config, view, usage, now);
267
+ const out = [];
268
+ for (const limit of cost.limits) {
269
+ const tone = limitTone(limit.percent);
270
+ if (limit.kind === 'five_hour') {
271
+ const reset = resetText(limit.resetsAt, now);
272
+ out.push(
273
+ segment(
274
+ 0,
275
+ dim('plan 5h '),
276
+ gauge(limit.percent, PLAN_CELLS, tone),
277
+ part(` ${limit.percent}%`, { tone }),
278
+ reset ? { ...dim(` ↻${reset}`), priority: 4 } : null,
279
+ ),
280
+ );
281
+ } else out.push(segment(3, dim(`${limit.label} `), part(`${limit.percent}%`, { tone })));
282
+ }
283
+ if (!isRouting(view)) return out.filter(Boolean);
284
+ if (!cost.replies && !cost.agents) return [...out, segment(2, dim('session · no replies yet'))].filter(Boolean);
285
+ out.push(
286
+ segment(0, dim('session '), part(costText(cost.totalUsd, cost.billing), { weight: 'semibold' }), {
287
+ ...dim(` ${API_ESTIMATE}`),
288
+ priority: 5,
289
+ }),
290
+ );
291
+ if (cost.billing === 'mixed') out.push(segment(1, part(`credits ${costText(cost.creditsUsd, cost.billing)}`)));
292
+ if (cost.effortOnly && cost.effortMix.length)
293
+ out.push(segment(2, dim('effort '), part(cost.effortMix.map(([effort, n]) => `${effort} ${n}`).join(' · '))));
294
+ else if (cost.diffUsd !== null)
295
+ out.push(
296
+ segment(
297
+ 1,
298
+ part(diffText(cost.diffUsd, cost.baseUsd), {
299
+ weight: 'semibold',
300
+ ...(cost.diffUsd ? { tone: savingTone(cost.diffUsd) } : {}),
301
+ }),
302
+ dim(` vs ${baselineName(view, sessionUsage(view, now), cost)}`),
303
+ ),
304
+ );
305
+ else if (cost.uncomparedReplies)
306
+ out.push(
307
+ segment(
308
+ 2,
309
+ dim(
310
+ cost.uncomparedReplies === cost.replies
311
+ ? 'not compared'
312
+ : `${cost.uncomparedReplies} of ${cost.replies} not compared`,
313
+ ),
314
+ ),
315
+ );
316
+ out.push(
317
+ segment(
318
+ 6,
319
+ dim(`${repliesText(cost)}${cost.agents > 0 ? ` · ${cost.agents} agent${cost.agents === 1 ? '' : 's'}` : ''}`),
320
+ ),
321
+ );
322
+ return out.filter(Boolean);
323
+ }
324
+
325
+ // The route line under a turn: "▂▄▆█ Sonnet 5.5 · high ↓ cheaper fits code Jev 93% ≥ 90%", the band's words for the
326
+ // route that ran. Narrow, the support goes first, then the activity, then the reason; the route stays.
327
+ export function routeLineSegments(config, line) {
328
+ const support = supportText(config, line.estimate);
329
+ const activity =
330
+ line.activity && config.activityRouting !== 'off' && !reasonNamesActivity(line.reason) ? line.activity : null;
331
+ return [
332
+ segment(0, line.tier ? meter(line.tier) : dim('○'), part(' '), routeParts(line.model, line.effort)),
333
+ segment(1, dim(shortReason(line.reason, line.reasonActivity))),
334
+ activity ? segment(2, part(activity, { tone: 'activity', italic: true })) : null,
335
+ support ? segment(3, dim(support)) : null,
336
+ ].filter(Boolean);
337
+ }
338
+
339
+ // A subagent's model and how it was chosen: "agent ■ Haiku 5.5 ↓ from Sonnet 5.5 (Claude Code’s pick)". The square is
340
+ // filled when the router moved it off Claude Code's pick, as the Usage tab's squares show it. A band too narrow for the
341
+ // reason keeps the name. Without its record (routing off, or the router reloaded since the spawn) only the mode is
342
+ // known: a subagent's model is chosen once, at spawn, and never changes between its steps.
343
+ export function agentParts(config, agent, columns) {
344
+ if (!agent)
345
+ return [
346
+ dim(
347
+ config.subagentRouting === 'on'
348
+ ? 'agent · model chosen at spawn'
349
+ : 'agent · keeps its own model (subagent routing off)',
350
+ ),
351
+ ];
352
+ const moved = !agent.pass && agent.move;
353
+ const name = part(`${moved ? '■' : '□'} ${modelName(agent.routedModel)}`, {
354
+ weight: 'semibold',
355
+ tone: modelTone(agent.routedModel),
356
+ });
357
+ const glyph = { up: '↑', down: '↓' }[agent.move] ?? '↕';
358
+ const why = agent.pass
359
+ ? `= Claude Code’s pick: ${PASS_WHY[agent.pass]}`
360
+ : moved
361
+ ? `${glyph} from ${modelName(agent.coreModel)} (Claude Code’s pick)`
362
+ : agent.choice === 'core'
363
+ ? `= Claude Code’s pick: ${agent.failed ? 'classifier did not answer' : 'the router sent no model'}`
364
+ : '= Claude Code’s pick, kept by the router';
365
+ const parts = [dim('agent '), name, dim(` ${why}`)];
366
+ return Number.isFinite(columns) && partsWidth(parts) > columns ? parts.slice(0, 2) : parts;
367
+ }
368
+
369
+ // Most agents the band lists before it says how many more.
370
+ export const MAX_AGENT_ROWS = 3;
371
+ // The longest task description a row carries; the rest is cut with an ellipsis.
372
+ const AGENT_TASK_CELLS = 24;
373
+ // The Back control of an agent's band, and the room it takes with the gap after it.
374
+ export const BACK_LABEL = '◂ main';
375
+ export const BACK_CELLS = BACK_LABEL.length + SEPARATOR.length;
376
+
377
+ const taskText = (text) => (text.length > AGENT_TASK_CELLS ? `${text.slice(0, AGENT_TASK_CELLS - 1)}…` : text);
378
+
379
+ // One running agent as a row's label: "▸ ■ Haiku 5.5 Explore · retry usages ●". The square is filled when the router
380
+ // moved the agent off Claude Code's pick, as the Usage tab's squares show it; the dot says it is running. `detail` is how
381
+ // much is said: the task and model, the model, or the type alone.
382
+ function agentLabel(row, detail) {
383
+ const { record } = row;
384
+ const moved = record && !record.pass && record.move;
385
+ const model =
386
+ record?.routedModel && detail !== 'type' ? `${moved ? '■' : '□'} ${modelName(record.routedModel)} ` : '';
387
+ const task = detail === 'full' && row.description ? ` · ${taskText(row.description)}` : '';
388
+ return `▸ ${model}${row.type}${task} ●`;
389
+ }
390
+
391
+ /**
392
+ * The band's agents row: "agents 2 ▸ ■ Haiku 5.5 Explore · retry usages ● ▸ □ Sonnet 5.5 general-purpose ●". One
393
+ * Button per agent, up to MAX_AGENT_ROWS, then "+N". A row too wide for `columns` says less (the model, then the type
394
+ * alone), then lists the first agent and the count, then only the count. No running agent, no row.
395
+ * @param {{ id: string, type: string, description?: string, record?: any }[]} rows the agents running, in order
396
+ */
397
+ export function agentSegments(rows, columns, actions) {
398
+ if (!rows.length) return [];
399
+ const build = (detail, shown) =>
400
+ [
401
+ segment(0, dim(`agents ${rows.length}`)),
402
+ ...rows.slice(0, shown).map((row) =>
403
+ segment(
404
+ 0,
405
+ button({
406
+ key: `band-agent-${row.id}`,
407
+ label: agentLabel(row, detail),
408
+ plain: true,
409
+ onPress: () => actions.viewAgent(row.id),
410
+ }),
411
+ ),
412
+ ),
413
+ rows.length > shown ? segment(0, dim(`+${rows.length - shown}`)) : null,
414
+ ].filter(Boolean);
415
+ const fits = (segments) => !Number.isFinite(columns) || lineWidth(segments, SEPARATOR) <= columns;
416
+ const details = ['full', 'model', 'type'];
417
+ const listed = [Math.min(rows.length, MAX_AGENT_ROWS), 1];
418
+ for (const shown of new Set(listed))
419
+ for (const detail of details) {
420
+ const segments = build(detail, shown);
421
+ if (fits(segments)) return segments;
422
+ }
423
+ return [segment(0, dim(`agents ${rows.length}`))];
424
+ }
425
+
426
+ /**
427
+ * The pin Select's data, or null where no pin is offered: not while routing is off, unavailable or choosing, nor while
428
+ * the classifier needs a fix.
429
+ * @returns {{ value: string, options: { value: string, label: string }[] } | null}
430
+ */
431
+ export function pinOf(config, view) {
432
+ if (!isRouting(view) || view.phase === 'choosing' || classifierWarns(view.error)) return null;
433
+ const pending = view.pendingPin;
434
+ return {
435
+ value: pending ?? NO_PIN,
436
+ options: [
437
+ { value: NO_PIN, label: noPinLabel(pending) },
438
+ ...TIERS.map((tier) => ({ value: tier, label: pinCard(config, tier) })),
439
+ ],
440
+ };
441
+ }