@nathanstephenson/flow 0.1.10-beta.6 → 0.1.10-beta.7

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 (39) hide show
  1. package/README.md +5 -19
  2. package/dist/build-id +1 -1
  3. package/dist/cli/application.js +4 -37
  4. package/dist/cli/bootstrap.js +2 -2
  5. package/dist/cli/install-guard.js +1 -1
  6. package/dist/client/context-usage.js +2 -3
  7. package/dist/client/enquiry.js +0 -6
  8. package/dist/client/model-choices.js +1 -1
  9. package/dist/client/permission.js +1 -2
  10. package/dist/client/relative-time.js +1 -1
  11. package/dist/client/scope-kind.js +2 -3
  12. package/dist/client/search.js +1 -1
  13. package/dist/client/status.js +1 -1
  14. package/dist/client/subagent-tree.js +1 -1
  15. package/dist/client/tool-chains.js +1 -1
  16. package/dist/client/tool-summary.js +2 -3
  17. package/dist/daemon/config-store.js +2 -0
  18. package/dist/daemon/config.js +32 -1
  19. package/dist/daemon/git.js +33 -2
  20. package/dist/daemon/host.js +12 -3
  21. package/dist/daemon/runtime.js +1 -1
  22. package/dist/daemon/server.js +2 -2
  23. package/dist/daemon/workflow-schedule-driver.js +5 -1
  24. package/dist/workflows/schedules.js +1 -0
  25. package/package.json +1 -1
  26. package/web/dist/assets/index-5E7qbtVD.js +51 -0
  27. package/web/dist/assets/{switch-B00FEUA7.js → select-C-BND5Jn.js} +1 -1
  28. package/web/dist/assets/{settings-mcp-3cgqQB28.js → settings-mcp-CBXZt_cf.js} +1 -1
  29. package/web/dist/assets/{settings-secrets-BIO_hXZ1.js → settings-secrets-29ZG3w6p.js} +1 -1
  30. package/web/dist/assets/{settings-workflows-Cutm8isB.js → settings-workflows-CE11fxNj.js} +2 -2
  31. package/web/dist/assets/workflow-api-DkUjAZ4w.js +6 -0
  32. package/web/dist/assets/{workflows-D6oEUUhj.js → workflows-DdogjBu3.js} +5 -5
  33. package/web/dist/assets/{workflows-pane-KIdSDKJB.js → workflows-pane-B0dyuSuz.js} +1 -1
  34. package/web/dist/index.html +4 -4
  35. package/dist/tui/app.js +0 -612
  36. package/dist/tui/keys.js +0 -90
  37. package/dist/tui/render.js +0 -417
  38. package/web/dist/assets/index-BhfJhrVJ.js +0 -51
  39. package/web/dist/assets/workflow-api-BfExddae.js +0 -6
package/dist/tui/keys.js DELETED
@@ -1,90 +0,0 @@
1
- /** Terminal key sequences, named so the input handler reads as intent rather than escape codes. */
2
- export const KEY = {
3
- ctrlC: "\u0003",
4
- ctrlE: "\u0005",
5
- /**
6
- * Branches.
7
- *
8
- * `^G` for git rather than the obvious `^B` for branch: `^B` is tmux's default prefix, and this
9
- * is a client people run inside tmux.
10
- *
11
- * `^K` compacts the Conversation Context. A chord and not an unmodified key for the reason the
12
- * web client keeps its Revive one click inside a menu: it asks the backend to summarise, and that
13
- * spends tokens.
14
- */
15
- ctrlG: "\u0007",
16
- ctrlK: "\u000b",
17
- ctrlP: "\u0010",
18
- ctrlS: "\u0013",
19
- escape: "\u001b",
20
- enter: "\r",
21
- newline: "\n",
22
- backspace: "\u007f",
23
- backspaceAlt: "\b",
24
- up: "\u001b[A",
25
- down: "\u001b[B",
26
- /** Focus events emitted after the client enables DECSET 1004. Unsupported terminals ignore it. */
27
- focusIn: "\u001b[I",
28
- focusOut: "\u001b[O",
29
- };
30
- export function isPrintable(key) {
31
- return key.length === 1 && key >= " " && key !== KEY.backspace;
32
- }
33
- /**
34
- * Split a stdin chunk into individual keys.
35
- *
36
- * A chunk is not a keystroke: a paste arrives as one chunk of many characters, and an arrow key
37
- * arrives as a three-byte escape sequence. Treating a chunk as one key silently drops both.
38
- */
39
- function parseKeys(chunk) {
40
- const keys = [];
41
- let index = 0;
42
- while (index < chunk.length) {
43
- const char = chunk[index] ?? "";
44
- if (char === "\u001b") {
45
- if (index + 1 === chunk.length)
46
- return { keys, remainder: chunk.slice(index) };
47
- if (chunk[index + 1] === "[") {
48
- let end = index + 2;
49
- while (end < chunk.length && !/[@-~]/.test(chunk[end] ?? ""))
50
- end += 1;
51
- if (end === chunk.length)
52
- return { keys, remainder: chunk.slice(index) };
53
- keys.push(chunk.slice(index, end + 1));
54
- index = end + 1;
55
- continue;
56
- }
57
- }
58
- keys.push(char);
59
- index += 1;
60
- }
61
- return { keys, remainder: "" };
62
- }
63
- export function splitKeys(chunk) {
64
- return parseKeys(chunk).keys;
65
- }
66
- export class KeySplitter {
67
- buffered = "";
68
- timer;
69
- onFlush;
70
- flushAfterMs;
71
- constructor(onFlush, flushAfterMs = 25) {
72
- this.onFlush = onFlush;
73
- this.flushAfterMs = flushAfterMs;
74
- }
75
- push(chunk) {
76
- if (this.timer)
77
- clearTimeout(this.timer);
78
- const parsed = parseKeys(this.buffered + chunk);
79
- this.buffered = parsed.remainder;
80
- if (this.buffered) {
81
- this.timer = setTimeout(() => {
82
- this.timer = undefined;
83
- const buffered = this.buffered;
84
- this.buffered = "";
85
- this.onFlush?.([...buffered]);
86
- }, this.flushAfterMs);
87
- }
88
- return parsed.keys;
89
- }
90
- }
@@ -1,417 +0,0 @@
1
- import { contextUsageLabel } from "../client/context-usage.js";
2
- import { effortCapability, effortChoices, modelChoices } from "../client/model-choices.js";
3
- import { answerLines, progressLabel, rowsFor, } from "../client/enquiry.js";
4
- import { authorisationLabel, permissionChoices } from "../client/permission.js";
5
- import { toolSummary } from "../client/tool-summary.js";
6
- import { relativeTime } from "../client/relative-time.js";
7
- import { scopeKindLabel } from "../client/scope-kind.js";
8
- import { depthOf, producerKey, subagentKey, subagentParents } from "../client/subagent-tree.js";
9
- import { sessionLabel } from "../client/session-label.js";
10
- import { railGroup, railGroupLabel, RAIL_GROUPS } from "../client/status.js";
11
- export function renderFrame(ui, size) {
12
- const width = Math.max(20, size.columns);
13
- const height = Math.max(6, size.rows);
14
- // Only ever one of the two: an Enquiry and a Permission Prompt are both a callback the CLI is
15
- // blocked on, and the composer is locked to whichever is in hand. Concatenated rather than chosen
16
- // between, so if the impossible happens the human can see both rather than one of them silently
17
- // going missing behind a lockout.
18
- const picker = [...enquiryLines(ui, width), ...permissionLines(ui, width)];
19
- // The picker takes its rows off the transcript rather than replacing it, which is the difference
20
- // between this and an Overlay. Bounded so a four-option Question on a short terminal still leaves
21
- // something of the conversation visible.
22
- const body = height - 3 - picker.length;
23
- const lines = [header(ui, width)];
24
- lines.push(...(ui.overlay.kind === "none" ? transcript(ui.view, width, body) : overlay(ui, width, body)));
25
- lines.push(...picker);
26
- lines.push(status(ui, width));
27
- lines.push(promptLine(ui, width));
28
- return lines;
29
- }
30
- function header(ui, width) {
31
- const summary = ui.sessions.find((session) => session.id === ui.selected);
32
- const model = ui.view.model?.label ?? ui.view.model?.id ?? "default model";
33
- const effort = ui.view.effort && effortChoices(ui.view).length > 0 ? ` · ${ui.view.effort}` : "";
34
- /*
35
- * Where the next turn will land, beside what will run it.
36
- *
37
- * The web client draws this as a strip along the bottom of its composer, with the checkout named
38
- * every time. Here it is one line shared with the model and the status, so only the notable case
39
- * is spent width on: `scopeKindLabel` supplies the word either way, so the two clients cannot
40
- * drift on *what* a Worktree is called — only on whether there is room to say it.
41
- *
42
- * Absent when the Scope is not a repository, the same signal the web client hides its control on.
43
- */
44
- const kind = ui.view.worktree ? ` · ${scopeKindLabel(ui.view)}` : "";
45
- const branch = ui.view.branch ? ` · ${ui.view.branch.name}${kind}` : "";
46
- const left = summary ? `${summary.backend} · ${sessionLabel(summary)}${branch}` : "no session";
47
- const right = `${model}${effort} · ${ui.view.status}`;
48
- return clip(pad(left, Math.max(0, width - right.length - 1)) + " " + right, width);
49
- }
50
- /**
51
- * The Enquiry picker: the Question on screen, its Options, and where the cursor is.
52
- *
53
- * Empty when nothing is being asked, which is what keeps `renderFrame` free of a conditional — the
54
- * rows are simply zero and the transcript gets its full height back.
55
- *
56
- * Numbers are a visible column, and that is the one thing this does that an arrow-driven list
57
- * cannot: a row can be addressed without being travelled to. It is what "numbered picker" means, and
58
- * it is why the digits are worth borrowing from the prompt line to get.
59
- */
60
- function enquiryLines(ui, width) {
61
- const asking = ui.view.asking;
62
- const state = ui.answering;
63
- if (!asking || !state)
64
- return [];
65
- const question = asking.questions[state.index];
66
- if (!question)
67
- return [];
68
- const rows = rowsFor(question, ui.input);
69
- const progress = progressLabel(state, asking.questions);
70
- const mode = question.multiSelect ? " (choose any)" : "";
71
- const title = `${question.header}${progress ? ` (${progress})` : ""}${mode}`;
72
- const lines = [rule(title, width), ...(asking.context ? wrap(asking.context, width) : []), ...wrap(question.question, width)];
73
- rows.forEach((row, index) => {
74
- const onCursor = index === state.cursor;
75
- const cursor = onCursor ? ">" : " ";
76
- // The box only where it means something. A single-select has exactly one answer at all times,
77
- // so drawing an empty one beside every row would offer a choice that is not on offer.
78
- const box = question.multiSelect ? ((state.chosen[state.index] ?? []).includes(row.label) ? "[x] " : "[ ] ") : "";
79
- const head = `${cursor} ${index + 1} ${box}${row.label}`;
80
- /*
81
- * The row under the cursor gets its whole description, wrapped underneath; the rest get one
82
- * clipped line.
83
- *
84
- * An Option's description is not a nicety the way a Skill's is in the `/` menu — it is the
85
- * deciding information, and the model writes it long: a hundred characters saying what the
86
- * trade-off is, with the trade-off itself at the end. Clipping every row to the width of a
87
- * terminal reliably cuts off the half that decides it.
88
- *
89
- * So the one being considered is shown in full and the others stay scannable. The picker's
90
- * height therefore changes as the cursor moves, which is the cost of this and is paid
91
- * deliberately: what it buys is never having to choose between options whose descriptions have
92
- * been cut in the same place.
93
- */
94
- if (!row.description) {
95
- lines.push(clip(head, width));
96
- return;
97
- }
98
- if (onCursor) {
99
- lines.push(clip(head, width));
100
- // Indented under the number, so the wrapped text reads as belonging to the row above it.
101
- for (const line of wrap(row.description, Math.max(8, width - 6)))
102
- lines.push(clip(` ${line}`, width));
103
- return;
104
- }
105
- lines.push(ellipsised(`${head} ${row.description}`, width));
106
- });
107
- return lines;
108
- }
109
- /**
110
- * The Permission Prompt picker: what wants to run, and the three things a human can do about it.
111
- *
112
- * Empty when nothing is waiting, which keeps `renderFrame` free of a conditional, exactly as
113
- * `enquiryLines` does.
114
- *
115
- * What is being authorised is read off the `tool` Entry this prompt shares an id with, rather than
116
- * carried on the prompt itself: `toolSummary` already précises a call's arguments for both
117
- * front-ends, and a second description of the same call is a second thing to keep in step. The tool
118
- * name alone would be the withholding `tool-summary.ts` exists to stop — "authorise Bash?" is not a
119
- * question anyone can answer.
120
- */
121
- function permissionLines(ui, width) {
122
- const authorising = ui.view.authorising;
123
- if (!authorising)
124
- return [];
125
- const cursor = ui.deciding ?? 0;
126
- const call = ui.view.entries.find((entry) => entry.kind === "tool" && entry.id === authorising.callId);
127
- const summary = call ? toolSummary(call.input) : undefined;
128
- const lines = [rule(`Authorise ${authorising.tool}?`, width)];
129
- if (authorising.context)
130
- lines.push(...wrap(authorising.context, width));
131
- if (summary)
132
- lines.push(...wrap(summary, width));
133
- if (authorising.authorizationScope)
134
- lines.push(...wrap(`Scope: ${authorising.authorizationScope}`, width));
135
- permissionChoices(authorising.allowAlways).forEach((choice, index) => {
136
- const onCursor = index === cursor;
137
- const head = `${onCursor ? ">" : " "} ${index + 1} ${choice.label}`;
138
- // The description in full on the cursor row and clipped elsewhere, the rule `enquiryLines`
139
- // sets — and here the row it matters for is Always, whose description is the whole warning.
140
- if (!onCursor) {
141
- lines.push(ellipsised(`${head} ${choice.description}`, width));
142
- return;
143
- }
144
- lines.push(clip(head, width));
145
- for (const line of wrap(choice.description, Math.max(8, width - 6))) {
146
- lines.push(clip(` ${line}`, width));
147
- }
148
- });
149
- return lines;
150
- }
151
- /**
152
- * The prompt line, and — while an Enquiry is open — the whole of how the lockout is stated.
153
- *
154
- * The TUI has no placeholder to put it in, so the sigil carries it: `?` rather than `>`, because a
155
- * `>` over a box that cannot send a message tells exactly the lie the web client's placeholder is
156
- * careful to refuse.
157
- */
158
- function promptLine(ui, width) {
159
- // A Permission Prompt takes no text at all, so there is nothing to type and nothing to show
160
- // typed. `!` rather than `?`: the difference between being asked something and being asked to
161
- // allow something is the whole distinction between an Enquiry and this, and the sigil is the only
162
- // place a terminal has to say it.
163
- if (ui.view.authorising)
164
- return clip(`! [1-${permissionChoices(ui.view.authorising.allowAlways).length} to decide]`, width);
165
- if (!ui.view.asking)
166
- return clip(`> ${ui.input}`, width);
167
- return clip(ui.input === "" ? "? [type your own answer]" : `? ${ui.input}`, width);
168
- }
169
- function status(ui, width) {
170
- const parts = [];
171
- if (ui.view.queue.length > 0)
172
- parts.push(`${ui.view.queue.length} queued`);
173
- // Ahead of the reading it is about to change, and in place of it: a percentage that has not moved
174
- // yet is the thing someone asking for a compaction is staring at.
175
- if (ui.view.compacting)
176
- parts.push("compacting…");
177
- else {
178
- const context = contextUsageLabel(ui.view.contextUsage);
179
- if (context)
180
- parts.push(context);
181
- }
182
- if (ui.notice)
183
- parts.push(ui.notice);
184
- // `^K` is listed only where it can be served, on the same rule the web client hides the menu item:
185
- // an affordance a backend cannot honour is worse than no affordance at all.
186
- const compact = ui.view.capabilities?.compaction ? " ^K compact" : "";
187
- /*
188
- * While an Enquiry is open the hints are replaced rather than added to, because most of them are
189
- * no longer true: nothing here sends a message, and `^K` is refused. `esc abort` survives and is
190
- * the one way out — the TUI has no Abort button to give Escape a second job, which is why it means
191
- * abort here where on the web it means going back a Question.
192
- */
193
- parts.push(ui.view.authorising
194
- ? `↑↓ choose 1-${permissionChoices(ui.view.authorising.allowAlways).length} pick enter decide esc deny`
195
- : ui.view.asking
196
- ? "↑↓ choose 1-9 pick enter answer esc abort"
197
- : `^S sessions ^P models ^E effort ^G branches${compact} esc abort ^C quit`);
198
- return clip(parts.join(" · "), width);
199
- }
200
- function transcript(view, width, height) {
201
- const subagents = new Map(view.entries.flatMap((entry) => entry.kind === "subagent" ? [[subagentKey(entry.id), entry]] : []));
202
- const parents = subagentParents([...subagents.keys()], (key) => subagents.get(key));
203
- const lines = view.entries.flatMap((entry) => {
204
- const indent = " ".repeat(depthOf(parents, producerKey(entry)));
205
- return entryLines(entry, Math.max(1, width - indent.length)).map((line) => indent + line);
206
- });
207
- return padTo(lines.slice(-height), height, width);
208
- }
209
- function entryLines(entry, width) {
210
- switch (entry.kind) {
211
- case "user": {
212
- const lines = wrap(`> ${entry.text}`, width);
213
- // A terminal cannot show the image and cannot paste one either, so it says one is there and
214
- // stops. Naming the count rather than each id, because an id is a filename this reader has no
215
- // use for — what they need to know is that the model saw something they cannot.
216
- const count = entry.attachments?.length ?? 0;
217
- return count === 0 ? lines : [...lines, clip(` [${count} image${count === 1 ? "" : "s"}]`, width)];
218
- }
219
- case "assistant":
220
- return wrap(entry.text, width);
221
- case "thinking":
222
- return wrap(`· ${entry.text}`, width);
223
- case "tool": {
224
- // The authorisation only where there is one — most rows have none, and a terminal that printed
225
- // something for them would suggest a decision nobody was asked to make.
226
- const authorised = authorisationLabel(entry.authorisation);
227
- return [clip(` [${entry.status}] ${entry.name}${authorised ? ` — ${authorised}` : ""}`, width)];
228
- }
229
- case "subagent":
230
- // Indented past a tool call: a Subagent is what one of those is doing, not another of them.
231
- return [clip(` ⤷ [${entry.waitingOn ?? entry.status}] ${entry.name}`, width)];
232
- case "background_call":
233
- // Indented for the same reason, and for the same relationship: this is what the tool call
234
- // above it is still doing. Its own glyph, because the row above says `complete` — that call
235
- // handed back a receipt, and this is the work the receipt promised (ADR 0021).
236
- return [clip(` ⟳ [${entry.status}] ${entry.tool}`, width)];
237
- case "enquiry": {
238
- // What was asked and what was chosen, which is the whole requirement of this row. The lines
239
- // come from the shared module, so the terminal and the browser cannot disagree about what an
240
- // answered Enquiry says — only about how it is drawn.
241
- const head = clip(` [${entry.status}] asked ${entry.questions.length === 1 ? "a question" : `${entry.questions.length} questions`}`, width);
242
- if (entry.status !== "answered")
243
- return [head];
244
- return [head, ...answerLines(entry.questions, entry.answers).map((line) => clip(` ${line}`, width))];
245
- }
246
- case "notice":
247
- return wrap(`! ${entry.text}`, width);
248
- case "marker":
249
- return [rule(entry.text, width)];
250
- }
251
- }
252
- /**
253
- * A structural marker is a break in the Presentation Transcript rather than another line of text, so
254
- * it gets a labelled rule — the same `──` vocabulary the Settled divider in the session list uses.
255
- */
256
- function rule(label, width) {
257
- const dashes = Math.max(0, width - label.length - 4);
258
- return clip(`── ${label} ${"─".repeat(dashes)}`, width);
259
- }
260
- function overlay(ui, width, height) {
261
- if (ui.overlay.kind === "sessions") {
262
- const cursor = ui.overlay.index;
263
- const now = ui.now ?? Date.now();
264
- const rows = [];
265
- const counts = new Map(RAIL_GROUPS.map((group) => [group, 0]));
266
- for (const session of ui.sessions) {
267
- const group = railGroup(session);
268
- counts.set(group, (counts.get(group) ?? 0) + 1);
269
- }
270
- let opened;
271
- // Headings are inserted into the rendered rows but never into ui.sessions, so the numeric
272
- // cursor cannot land on one. The host already sorts by this same shared group identity.
273
- for (const [index, session] of ui.sessions.entries()) {
274
- const group = railGroup(session);
275
- if (group !== opened) {
276
- opened = group;
277
- rows.push({ type: "heading", line: clip(` ── ${railGroupLabel(group)} (${counts.get(group) ?? 0}) ──`, width) });
278
- }
279
- const ageAt = session.attention?.at ?? session.restingAt;
280
- const age = relativeTime(ageAt, now);
281
- const attention = session.attention
282
- ? `${session.attention.group === "needs-input" ? "!" : "●"} ${session.attention.reason.toLowerCase()}`
283
- : session.status;
284
- // Backend and fixed columns earn their width. On a narrow terminal the reason, age, and title
285
- // are the actionable parts; dropping metadata is better than clipping the identity away.
286
- const detail = width >= 64
287
- ? `${attention.padEnd(16)} ${session.backend.padEnd(7)} ${age.padEnd(10)}`
288
- : width >= 44
289
- ? `${attention.padEnd(16)} ${age.padEnd(10)}`
290
- : `${attention} ${age}`;
291
- rows.push({
292
- type: "session",
293
- sessionIndex: index,
294
- line: clip(`${index === cursor ? ">" : " "} ${detail} ${sessionLabel(session)}`, width),
295
- });
296
- if (session.outputPreview)
297
- rows.push({ type: "preview", line: ellipsised(` ${session.outputPreview}`, width) });
298
- }
299
- // Group headings consume rows but not cursor positions. Window the rendered rows around the
300
- // selected identity so adding headings never makes keyboard navigation walk off-screen.
301
- const room = Math.max(1, height - 1);
302
- const cursorRow = rows.findIndex((row) => row.type === "session" && row.sessionIndex === cursor);
303
- const start = Math.max(0, Math.min(cursorRow - Math.floor(room / 2), rows.length - room));
304
- let visibleRows = rows.slice(start, start + room);
305
- // When the window starts halfway through a large group, keep its heading as a sticky first row.
306
- // There is always still room for the cursor because a centered/clamped cursor is not first.
307
- if (room > 1 && start > 0 && visibleRows[0]?.type !== "heading") {
308
- let heading = start - 1;
309
- while (heading > 0 && rows[heading]?.type !== "heading")
310
- heading -= 1;
311
- const groupHeading = rows[heading];
312
- if (groupHeading?.type === "heading") {
313
- visibleRows = [groupHeading, ...visibleRows.slice(1)];
314
- }
315
- }
316
- const visible = visibleRows.map((row) => row.line);
317
- return padTo(["sessions (enter to switch, n for new, w for new in a worktree, s to settle, esc to close)", ...visible], height, width);
318
- }
319
- if (ui.overlay.kind === "branches") {
320
- const { index, purpose, branches, head } = ui.overlay;
321
- // Keep the cursor in view, the same way the model list does: a long-lived checkout accumulates
322
- // branches the way the pi backend accumulates models.
323
- const room = Math.max(1, height - 1);
324
- const start = Math.max(0, Math.min(index - Math.floor(room / 2), branches.length - room));
325
- const rows = [];
326
- for (let cursor = start; cursor < Math.min(branches.length, start + room); cursor += 1) {
327
- const name = branches[cursor];
328
- if (name === undefined)
329
- break;
330
- const inForce = name === head ? " (in force)" : "";
331
- rows.push(clip(`${cursor === index ? ">" : " "} ${name}${inForce}`, width));
332
- }
333
- const title = branches.length === 0
334
- ? "no branches here"
335
- : purpose === "switch"
336
- ? "branches (enter to switch, esc to close)"
337
- : "cut a worktree from (enter to start an Agent Session, esc to close)";
338
- return padTo([title, ...rows], height, width);
339
- }
340
- if (ui.overlay.kind === "effort") {
341
- const levels = effortChoices(ui.view);
342
- const cursor = ui.overlay.index;
343
- const rows = levels.map((level, index) => {
344
- const inForce = level === ui.view.effort ? " (in force)" : "";
345
- return clip(`${index === cursor ? ">" : " "} ${level}${inForce}`, width);
346
- });
347
- const capability = effortCapability(ui.view.capabilities, ui.view.model);
348
- const title = levels.length > 0
349
- ? "effort (enter to set, esc to close)"
350
- : capability.status === "unknown"
351
- ? capability.reason
352
- : "this model has no effort control";
353
- return padTo([title, ...rows], height, width);
354
- }
355
- const choices = modelChoices(ui.view.capabilities);
356
- const index = ui.overlay.kind === "models" ? ui.overlay.index : 0;
357
- // Keep the cursor in view: the pi backend can offer hundreds of models.
358
- const start = Math.max(0, Math.min(index - Math.floor((height - 2) / 2), choices.length - (height - 2)));
359
- const rows = [];
360
- let lastProvider;
361
- for (let cursor = start; cursor < Math.min(choices.length, start + height - 2); cursor += 1) {
362
- const choice = choices[cursor];
363
- if (!choice)
364
- break;
365
- if (choice.provider !== lastProvider) {
366
- rows.push(clip(` ${choice.provider}`, width));
367
- lastProvider = choice.provider;
368
- }
369
- rows.push(clip(`${cursor === index ? ">" : " "} ${choice.model.label ?? choice.model.id}`, width));
370
- }
371
- return padTo([`models (${choices.length} across ${providerCount(choices)} providers)`, ...rows], height, width);
372
- }
373
- function providerCount(choices) {
374
- return new Set(choices.map((choice) => choice.provider)).size;
375
- }
376
- function wrap(text, width) {
377
- const out = [];
378
- for (const paragraph of text.split("\n")) {
379
- if (paragraph.length <= width) {
380
- out.push(paragraph);
381
- continue;
382
- }
383
- let rest = paragraph;
384
- while (rest.length > width) {
385
- const cut = rest.lastIndexOf(" ", width);
386
- const at = cut > width / 2 ? cut : width;
387
- out.push(rest.slice(0, at));
388
- rest = rest.slice(at).trimStart();
389
- }
390
- if (rest)
391
- out.push(rest);
392
- }
393
- return out;
394
- }
395
- function padTo(lines, height, width) {
396
- const clipped = lines.slice(0, height).map((line) => clip(line, width));
397
- while (clipped.length < height)
398
- clipped.push("");
399
- return clipped;
400
- }
401
- /**
402
- * Clipped, but saying so.
403
- *
404
- * `clip` cuts silently, which is right for a header where the reader can see the shape of what is
405
- * missing. It is wrong for an Option's description, where a sentence that stops mid-word is
406
- * indistinguishable from one the model wrote that way — and the reader is being asked to choose on
407
- * the strength of it.
408
- */
409
- function ellipsised(text, width) {
410
- return text.length > width ? `${text.slice(0, Math.max(0, width - 1))}…` : text;
411
- }
412
- function clip(text, width) {
413
- return text.length > width ? text.slice(0, width) : text;
414
- }
415
- function pad(text, width) {
416
- return text.length >= width ? text.slice(0, width) : text + " ".repeat(width - text.length);
417
- }