@awebai/oats 0.57.1 → 0.59.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 (61) hide show
  1. package/bin/oats.mjs +24 -11
  2. package/docs/configuration.md +2 -1
  3. package/docs/desktop-cli-api.md +105 -17
  4. package/docs/desktop.md +30 -3
  5. package/docs/execution-targets.md +15 -0
  6. package/docs/implementation.md +2 -0
  7. package/docs/integrations.md +2 -2
  8. package/docs/knowledge.md +4 -4
  9. package/docs/official-catalog.md +4 -4
  10. package/docs/packages.md +14 -14
  11. package/docs/release-notes/v0.58.0.md +107 -0
  12. package/docs/release-notes/v0.59.0.md +288 -0
  13. package/docs/servers.md +4 -1
  14. package/docs/souls-and-instances.md +30 -0
  15. package/docs/tui.md +653 -117
  16. package/docs/workspaces.md +2 -2
  17. package/lib/core.mjs +227 -46
  18. package/lib/read-ahead-worker.mjs +119 -0
  19. package/lib/read-ahead.mjs +115 -0
  20. package/lib/retire-state.mjs +33 -0
  21. package/lib/servers.mjs +9 -1
  22. package/lib/session-input.mjs +14 -4
  23. package/package-catalog.json +3 -3
  24. package/package.json +1 -1
  25. package/packages/client/cli-locator.mjs +2 -2
  26. package/packages/client/deployment-data.mjs +4 -0
  27. package/packages/client/deployment-panel.mjs +3 -0
  28. package/packages/client/instance-acts.mjs +23 -8
  29. package/packages/client/instance-presentation.mjs +3 -2
  30. package/packages/client/instance-tree.mjs +84 -39
  31. package/packages/client/lifecycle-confirmation.mjs +9 -3
  32. package/packages/client/panel-views.mjs +1 -1
  33. package/packages/client/remote-roster.mjs +4 -1
  34. package/packages/client/retire-in-flight.mjs +65 -0
  35. package/packages/client/retire-state.mjs +30 -0
  36. package/packages/client/spawn-apply-cli.mjs +8 -2
  37. package/packages/client/spawn-apply.mjs +20 -7
  38. package/packages/client/spawn-preview-cli.mjs +2 -1
  39. package/packages/client/spawn-preview.mjs +30 -18
  40. package/packages/record/lib/directory-identity.mjs +14 -0
  41. package/packages/record/lib/native-history.mjs +6 -5
  42. package/packages/tui/README.md +23 -10
  43. package/packages/tui/lib/actions.mjs +63 -5
  44. package/packages/tui/lib/client.mjs +12 -1
  45. package/packages/tui/lib/config.mjs +7 -2
  46. package/packages/tui/lib/main.mjs +315 -82
  47. package/packages/tui/lib/model.mjs +523 -56
  48. package/packages/tui/lib/panel.mjs +519 -144
  49. package/packages/tui/lib/read.mjs +52 -26
  50. package/packages/tui/lib/store.mjs +908 -65
  51. package/packages/tui/lib/term/keys.mjs +9 -2
  52. package/packages/tui/lib/term/text.mjs +4 -0
  53. package/packages/tui/lib/term/through.mjs +12 -1
  54. package/packages/tui/lib/views/frame.mjs +394 -95
  55. package/packages/tui/lib/views/help.mjs +54 -13
  56. package/packages/tui/lib/views/instances.mjs +43 -25
  57. package/packages/tui/lib/views/palette.mjs +61 -0
  58. package/packages/tui/lib/views/souls.mjs +268 -0
  59. package/packages/tui/lib/views/spawn.mjs +299 -0
  60. package/packages/tui/lib/views/split.mjs +29 -0
  61. package/skills/oats-getting-started/SKILL.md +2 -2
@@ -1,16 +1,23 @@
1
1
  // The frame: (model, size) -> the rows of the whole screen. Pure: no IO, no clock, no escape
2
2
  // byte. It places the views: the header, the list of instances and the detail beside it (or as a
3
3
  // page of its own on a narrow terminal), an agent's open terminal in the detail's place (or over
4
- // the whole body, zoomed), the switcher of workspaces over the whole body, the help, and the
5
- // footer. The TUI's own words are lit(); everything else is shown(). Meaning never
6
- // rests on colour alone: what is dim or bold also says what it is in words or by a mark.
7
- import { keyLabel } from "../actions.mjs";
8
- import { lit, shown, shownLines } from "../term/text.mjs";
4
+ // the whole body, zoomed), the souls of the open workspace and a soul's detail in the same places,
5
+ // the switcher of workspaces over the whole body, the palette and the spawn prompt over the whole
6
+ // body too, the help, and the footer. The TUI's own words are lit(); everything else is shown().
7
+ // Meaning never rests on colour alone: what is dim or bold also says what it is in words or by a
8
+ // mark.
9
+ import { LEADER, keyLabel } from "../actions.mjs";
10
+ import { lit, ownEllipsis, shown, shownLines, textOf } from "../term/text.mjs";
9
11
  import { style } from "../term/style.mjs";
10
12
  import { act } from "./act.mjs";
11
13
  import { detail } from "./detail.mjs";
12
14
  import { help, keyText } from "./help.mjs";
13
15
  import { instances, measured } from "./instances.mjs";
16
+ import { textWidth } from "../term/width.mjs";
17
+ import { palette } from "./palette.mjs";
18
+ import { soulDetail, soulsList, soulsListed } from "./souls.mjs";
19
+ import { spawn } from "./spawn.mjs";
20
+ import { splitFits, splitRegions } from "./split.mjs";
14
21
  import { switcher } from "./switcher.mjs";
15
22
 
16
23
  /** The frame is drawn from this size up (docs/tui.md); the design size is 80x24. */
@@ -18,16 +25,17 @@ export const MIN_SIZE = Object.freeze({ cols: 60, rows: 15 });
18
25
  /** From this width up the detail stands beside the list; under it, it is a page of its own. */
19
26
  export const WIDE = 80;
20
27
  /** The views, the header's second row: navigation, so no count and no digit stands against a name
21
- * (the help and the footer name the digits). The open one is drawn in reverse; the two that
22
- * cannot be opened yet are dim, and their key says what to run instead. */
23
- const VIEWS = Object.freeze([Object.freeze({ name: "Instances", open: true }), Object.freeze({ name: "Souls", open: false }), Object.freeze({ name: "Capabilities", open: false })]);
28
+ * (the help, the leader's hint and the palette name the keys). The open one is drawn in reverse. */
29
+ const VIEWS = Object.freeze([Object.freeze({ id: "instances", name: "Instances" }), Object.freeze({ id: "souls", name: "Souls" }), Object.freeze({ id: "capabilities", name: "Capabilities" })]);
24
30
  /** The cells between two views' names: each name has one cell on each side of it (the reverse bar
25
31
  * of the open one), and one between. */
26
32
  const VIEW_GAP = 1;
27
33
  const GAP = 3;
28
34
  /** The least width of a terminal beside the list: under it the terminal has the whole body. */
29
35
  export const PANEL_MIN = 40;
30
- /** One cell of margin on each side of the header and the footer. */
36
+ /** The most cells the list keeps beside an open terminal: the agent's screen has the rest. */
37
+ export const LIST_BESIDE = 30;
38
+ /** One cell of margin on each side of the header. The footer has none: its keys start at column 0. */
31
39
  const MARGIN = 1;
32
40
  /** The longest run one lit() holds with room to spare: the display filter bounds a line, and a
33
41
  * terminal can be wider than that bound. */
@@ -36,6 +44,8 @@ const RUN = 500;
36
44
  const REASON_ROWS = 4;
37
45
  /** The rows a deployment's note may take above the list. */
38
46
  const NOTE_ROWS = 2;
47
+ /** The TUI's word before the soul a kept filter of the instances is by. */
48
+ const SOUL_WORD = "soul";
39
49
  /** The cells a kept filter takes in the footer. */
40
50
  const FILTER_COLS = 24;
41
51
  /** What the header holds in the workspace's place while the switcher is shown. */
@@ -73,8 +83,9 @@ function rule(cols, ascii, joint = null) {
73
83
  /** A sentence made of parts (lib/model.mjs OPEN_WORDS): the TUI's own words (`{ own }`) and text
74
84
  * from outside (`{ shown }`, a name), each made safe on its own, so no word of the TUI's is ever
75
85
  * filtered with a name and no name is ever taken for the TUI's. Set on at most `max` rows of at
76
- * most `width` cells, broken at the spaces of the TUI's words; a name and what is written against
77
- * it (a full stop) are never broken apart, and a name wider than a row is cut there. Answers rows
86
+ * most `width` cells, broken at the spaces of the TUI's words but for those of a `{ own, whole }`
87
+ * part (a command: cut in two, it cannot be typed); a name or a whole part and what is written
88
+ * against it (a full stop) are never broken apart, and one wider than a row is cut there. Answers rows
78
89
  * of spans from column 0; when more was left, the last row ends in the cut mark. */
79
90
  export function partRows(parts, width, max, { ascii = false } = {}) {
80
91
  if (width < 1 || max < 1) return [];
@@ -83,7 +94,8 @@ export function partRows(parts, width, max, { ascii = false } = {}) {
83
94
  let glue = false;
84
95
  const put = (piece) => { if (glue && units.length) units.at(-1).push(piece); else units.push([piece]); glue = true; };
85
96
  for (const part of parts) {
86
- if (part.own !== undefined) part.own.split(" ").forEach((word, i) => { if (i > 0) glue = false; if (word) put({ text: lit(word), width: word.length }); });
97
+ if (part.own !== undefined && part.whole) { const own = ownEllipsis(part.own, ascii); put({ text: lit(own), width: own.length }); }
98
+ else if (part.own !== undefined) ownEllipsis(part.own, ascii).split(" ").forEach((word, i) => { if (i > 0) glue = false; if (word) put({ text: lit(word), width: word.length }); });
87
99
  else { const m = measured(part.shown); if (m.width > 0) put(m); }
88
100
  }
89
101
  const unitWidth = (unit) => unit.reduce((sum, piece) => sum + piece.width, 0);
@@ -118,11 +130,14 @@ export function partRows(parts, width, max, { ascii = false } = {}) {
118
130
  * the layout first, because the model's window is as tall, and the sentences in its list as wide,
119
131
  * as the list pane this gives. The answer's `note` is the note's rows and `outcome` the line's:
120
132
  * its sentence and each text under it (what the viewer printed, what the kernel said) set as a
121
- * reason is, on as many rows as a reason may take. */
122
- export function layout(notice, size, { observed = true, note = null, ascii = false, outcome = null } = {}) {
123
- const body = { top: 3, rows: Math.max(0, size.rows - 5) };
133
+ * reason is, on as many rows as a reason may take. While a terminal is open (`terminal`) the
134
+ * list keeps LIST_BESIDE cells at most, whether the terminal or the detail stands beside it, so
135
+ * the agent's screen keeps one size; a name longer than its row is cut there. */
136
+ export function layout(notice, size, { observed = true, note = null, ascii = false, outcome = null, terminal = false } = {}) {
137
+ // The header's two rows and the rule under them, then the body down to the footer's one row.
138
+ const body = { top: 3, rows: Math.max(0, size.rows - 4) };
124
139
  const wide = size.cols >= WIDE;
125
- const listCols = wide ? clamp(Math.floor(size.cols * 0.48), 36, 60) : size.cols;
140
+ const listCols = !wide ? size.cols : terminal ? LIST_BESIDE : clamp(Math.floor(size.cols * 0.48), 36, 60);
126
141
  const across = Math.max(1, size.cols - 2 * MARGIN);
127
142
  const reasonRows = clamp(Math.floor(body.rows / 4), 1, REASON_ROWS);
128
143
  const reason = notice ? shownLines(notice.reason, across, reasonRows, { ascii }) : [];
@@ -172,12 +187,16 @@ function nameSpans(m, col, room) {
172
187
  return [...(width ? [{ col, text: name.text, width }] : []), { col: after(width), text: lit(more), style: DIM, width: more.length }];
173
188
  }
174
189
 
175
- /** The header's counts, right aligned, each a number and a word. When they do not all fit, the
176
- * ones from the shared count are given up from the end, never the two the TUI adds. */
190
+ /** The cells of the header's first row the counts never take: the workspace's name keeps at
191
+ * least NAME_KEPT of them (cut, with the cut mark), and the two after it. */
192
+ const NAME_KEPT = 4;
193
+ /** The header's counts, right aligned, each a number and a word. When they do not all fit beside
194
+ * the least of the workspace's name (NAME_KEPT), the ones from the shared count are given up from
195
+ * the end, never the two the TUI adds. */
177
196
  function countSpans(counts, cols) {
178
197
  const texts = counts.map((count) => ({ ...count, text: `${count.n} ${count.word}` }));
179
198
  const width = (list) => list.reduce((sum, c) => sum + c.text.length, 0) + Math.max(0, list.length - 1) * 2;
180
- const room = cols - 2 * MARGIN - "oats".length - 2;
199
+ const room = cols - 2 * MARGIN - NAME_KEPT - 2;
181
200
  let kept = texts;
182
201
  while (width(kept) > room && kept.some((c) => c.shared)) kept = kept.filter((c, i) => i !== kept.findLastIndex((k) => k.shared));
183
202
  const spans = [];
@@ -186,28 +205,45 @@ function countSpans(counts, cols) {
186
205
  return { spans, start: cols - MARGIN - width(kept) };
187
206
  }
188
207
 
189
- /** The footer: the filter field while it is open; else the actions that apply now, read from the
190
- * effective table, as many as fit, `help` and `quit` last and always: their room is set aside
191
- * first, and a kept filter and the other hints take what is left. The hints stand in the order
208
+ /** The footer, one row from column 0 (docs/tui.md, "The footer"). While the leader waits, its
209
+ * hint (leaderHint). Else, in every focus, first the leader (`C-\ commands`), then: the filter
210
+ * field while it is open; else the actions that apply now, read from the
211
+ * effective table, as many as fit, `help` and `quit` last and always (unless the two do not fit
212
+ * whole after the leader: then `help` goes, then `quit`): their room is set aside first, and a
213
+ * kept filter and the other hints take what is left. The hints stand in the order
192
214
  * given, and when they do not all fit they are given up in an order of their own (`keep`, the
193
215
  * first kept longest), so a hint can stand first and still be given up before another: in the
194
- * list of instances the acts go first (`restart`, `start`, `retire`, then `stop`), then the views
195
- * (one hint for the three, `1-3 views`), then `workspaces`, `refresh`, the key that clears a
216
+ * list of instances the acts go first (`restart`, `start`, `retire`, then `stop`), then
217
+ * `workspaces`, `refresh`, the key that clears a
196
218
  * kept filter, `detail`, the open terminal's `close-terminal` and `zoom`, `open` and last `filter`
197
- * (`filter` before `open` while a filter is kept: its own `/` stands in the footer then). A row
198
- * too narrow for the whole of a hint drops it.
219
+ * (`filter` before `open` while a filter is kept: its own `/` stands in the footer then); in the
220
+ * Souls view `workspaces`, `refresh`, `filter`, `close`, then `i` and last Enter. A row
221
+ * too narrow for the whole of a hint drops it. The views have no hint: the header names them.
222
+ * The palette's footer says how to work it: Enter, Up/Down and Esc; the spawn prompt's, how to
223
+ * work its step (spawnHints).
199
224
  * `open` stands first while Enter would open the selected agent's terminal, or (`focus terminal`)
200
225
  * go to the one open on the selected row; while another machine is asked first, the key that
201
226
  * gives the open up stands in its place. With a terminal open, `zoom` and `close-terminal` follow
202
227
  * it; then the acts whose key opens a confirmation for the selected row now (the model's
203
- * `actable`). While the terminal has the focus the TUI reads no key but the back key, and the
204
- * footer names that alone (tui.json's `open.detachKey`, text from outside). In a confirmation the
205
- * buttons are on the screen, and the footer says how to work them: the close key with the close
228
+ * `actable`, which leaves out a secondary act: Retire while a retire runs). While the terminal
229
+ * has the focus the TUI reads no key but the leader, and the footer names it, and the leader
230
+ * twice, which gives the focus back to the list (tui.json's `open.detachKey`, text from
231
+ * outside). In a confirmation the buttons are on the screen, and the footer says how to work
232
+ * them: the close key with the close
206
233
  * button's label, the keys that move, Enter, and `quit`. The switcher has a filter of its own,
207
234
  * drawn as the list's. A key's label is the user's (views/help.mjs keyText); everything is placed
208
235
  * by cells. */
209
236
  function footer(m, cols) {
210
237
  const action = (id) => m.actions.find((a) => a.id === id && a.keys.length);
238
+ // The Souls view has a filter of its own, drawn as the list's; and with a soul selected, what
239
+ // Enter does (from the list it shows the detail, from the detail the soul's instances) stands
240
+ // first, as `open` does in the list, so a kept filter leaves it its room.
241
+ const soul = m.view === "souls" && m.souls ? m.souls : null;
242
+ const filter = soul ? soul.filter : m.filter;
243
+ // A filter is kept by its text, or in the list of instances by a soul (the Souls view's `i`):
244
+ // drawn `soul <name>` where the text's `/` stands.
245
+ const bySoul = !soul && m.view === "instances" && filter.soul && !filter.query ? filter.soul : null;
246
+ const filtered = Boolean(filter.query) || bySoul !== null;
211
247
  /** A hint: a key and what it does, the TUI's word or safe text with its width. */
212
248
  const made = (key, word) => {
213
249
  const said = typeof word === "string" ? { text: lit(word), width: word.length } : word;
@@ -225,20 +261,37 @@ function footer(m, cols) {
225
261
  const span = (hints) => hints.reduce((sum, h) => sum + h.width, 0) + Math.max(0, hints.length - 1) * GAP;
226
262
  // A word is drawn as one span, unless it was made of spans already.
227
263
  const spansOf = (h) => ({ ...h, word: { ...h.word, spans: h.word.spans ?? [{ col: 0, text: h.word.text, width: h.word.width }] } });
264
+ /** A hint of keys that are no action's (a key name the TUI writes, like `Enter`). */
265
+ const own = (keys, word) => spansOf(made({ text: lit(keys), width: keys.length }, word));
228
266
 
229
- // While what is typed goes to the agent the TUI takes one key, the back key, and the footer
230
- // names it alone (tui.json's open.detachKey, text from outside), in every such frame: before
231
- // anything else the footer could show.
232
- const listing = m.view !== "help" && m.view !== "switcher";
233
- if (listing && m.terminal?.focus === "terminal") {
234
- const key = keyText(m.detachKey), word = "back to the list";
235
- return [{ col: MARGIN, text: key.text, width: Math.max(0, Math.min(key.width, edge - MARGIN)) },
236
- { col: MARGIN + key.width + 1, text: lit(word), width: Math.max(0, Math.min(word.length, edge - MARGIN - key.width - 1)) }].filter((s) => s.width > 0);
267
+ // While the leader waits for its key, the row is its hint (docs/tui.md, "The leader").
268
+ if (m.leader) return leaderHint(m, edge, put, own);
269
+ // The leader stands first in every focus (tui.json's open.detachKey, text from outside): from
270
+ // it, every action is reached.
271
+ const lead = spansOf(made(keyText(m.detachKey), "commands"));
272
+ const start = lead.width + GAP;
273
+ const led = (spans) => [...put(lead, FOOT), ...spans];
274
+ // While what is typed goes to the agent the TUI takes one key, the leader, and the footer names
275
+ // it and the leader twice, which takes the focus back to the list.
276
+ // The list of instances: what Enter opens, the open terminal's keys and the acts are its own
277
+ // (Souls and Capabilities have none of them, the help and the switcher neither: the Souls view's
278
+ // Enter is its own, below).
279
+ const listing = !["help", "switcher", "souls", "capabilities"].includes(m.view);
280
+ if (listing && m.terminal?.focus === "terminal" && !m.palette && !m.spawn) {
281
+ const twice = keyText(`${m.detachKey} ${m.detachKey}`);
282
+ return led(start + twice.width + 1 + "back to the list".length <= edge ? put(spansOf(made(twice, "back to the list")), start) : []);
237
283
  }
238
- if (m.view !== "help" && m.filter.editing) {
284
+ if (m.palette) {
285
+ const hints = [own("Enter", "run"), own("Up/Down", "move"), own("Esc", "close")], spans = [];
286
+ let col = start;
287
+ for (const h of hints) { if (col + h.width > edge) break; spans.push(...put(h, col)); col += h.width + GAP; }
288
+ return led(spans);
289
+ }
290
+ if (m.spawn) return led(spawnHints(m.spawn, start, edge, put, own, m.ascii));
291
+ if (m.view !== "help" && filter.editing) {
239
292
  const hints = [made({ text: lit("Enter"), width: "Enter".length }, "keep"), hint("close", "clear")].filter(Boolean).map(spansOf);
240
- const spans = [{ col: MARGIN, text: lit("/"), style: BOLD, width: 1 }, { col: MARGIN + 1, text: shown(m.filter.query), width: Math.max(0, edge - span(hints) - GAP - MARGIN - 1) }];
241
- let col = Math.max(MARGIN + 2, edge - span(hints));
293
+ const spans = [...put(lead, FOOT), { col: start, text: lit("/"), style: BOLD, width: 1 }, { col: start + 1, text: shown(filter.query), width: Math.max(0, edge - span(hints) - GAP - start - 1) }];
294
+ let col = Math.max(start + 2, edge - span(hints));
242
295
  for (const h of hints) { spans.push(...put(h, col, { style: DIM })); col += h.width + GAP; }
243
296
  return spans;
244
297
  }
@@ -253,40 +306,48 @@ function footer(m, cols) {
253
306
  const quit = hint("quit");
254
307
  const all = [...hints, ...(quit ? [spansOf(quit)] : [])];
255
308
  const spans = [];
256
- let col = MARGIN;
309
+ let col = start;
257
310
  for (const h of all) { if (col + h.width > edge) break; spans.push(...put(h, col)); col += h.width + GAP; }
258
- return spans;
311
+ return led(spans);
259
312
  }
260
313
  // While another machine is asked first, Esc gives the open up, and that is all it does: the
261
314
  // hint stands where `open` stood, is kept as long, and `close` is not listed beside it.
262
315
  const asking = listing && m.opening?.phase === "preflight";
263
316
  // An action with no key (tui.json may unbind one) has no hint: nothing stands first then. On the
264
317
  // row whose terminal is open, Enter goes to it.
265
- const first = (asking ? [["open", hint("close", "stop opening")]] : listing && m.onTerminal ? [["open", hint("open", "focus terminal")]] : listing && m.openable ? [["open", hint("open")]] : []).filter(([, h]) => h);
318
+ const first = (asking ? [["open", hint("close", "stop opening")]] : listing && m.onTerminal ? [["open", hint("open", "focus terminal")]] : listing && m.openable ? [["open", hint("open")]]
319
+ : soul?.detail ? [["open", hint("open", soul.focus === "detail" ? "instances" : "detail")]] : []).filter(([, h]) => h);
266
320
  // With a terminal open, the keys that zoom it and close it stand after `open`, kept as long.
267
321
  const terminal = listing && m.terminal ? [["zoom", hint("zoom")], ["close-terminal", hint("close-terminal")]].filter(([, h]) => h) : [];
268
322
  // The acts the selected row offers now stand after those, and are the first given up.
269
323
  const acts = listing && !asking ? (m.actable ?? []).map((verb) => [verb, hint(verb)]).filter(([, h]) => h) : [];
270
324
  const ACTS = ["stop", "retire", "start", "restart"];
271
- // The three views are one hint, `views`, named by their keys: `1-3` while they are the digits
272
- // in order, else each key (tui.json may rebind one), and a view with no key left out. The
273
- // header shows which views there are; the help names each key.
274
- const viewKeys = ["instances", "souls", "capabilities"].map((id) => action(id)).filter(Boolean).map((a) => keyLabel(a.keys[0]));
275
- const views = viewKeys.length === 0 ? null : made(keyText(viewKeys.join("") === "123" ? "1-3" : viewKeys.join("/")), "views");
276
- hintOf.views = views;
277
- const VIEW_KEYS = ["views"];
325
+ // The views have no hint: the header names them, and the leader's hint and the help their keys.
326
+ const VIEW_KEYS = [];
278
327
  const [ids, keep] = m.view === "help" ? [["close"], ["close"]]
328
+ // The Souls view: after Enter, `i` shows the selected soul's instances, then the filter, a
329
+ // read again, Esc and the workspaces. They are given up from the end of `keep`: the workspaces
330
+ // first, then `refresh`, `filter` and `close`; `i` and Enter last. With no workspace of this
331
+ // machine there is nothing to filter or read: Esc and the workspaces only. No spawn hint: the
332
+ // help names n and N.
333
+ : soul ? (soul.local === false ? [["close", "workspaces"], ["close", "workspaces"]]
334
+ : [[...(soul.detail ? ["soul-instances"] : []), "filter", "refresh", "close", "workspaces"], ["open", "soul-instances", "close", "filter", "refresh", "workspaces"]])
335
+ : m.view === "capabilities" ? [["close", "workspaces"], ["close", "workspaces"]]
279
336
  // Esc closes the switcher only onto a view, and there is none before a workspace is opened.
280
337
  // A kept filter puts `close` before `refresh`, as in the list: the key that clears it is the hint to keep.
281
338
  : m.view === "switcher" ? [["open", "filter", ...(m.filter.query ? ["close", "refresh"] : ["refresh", ...(m.open ? ["close"] : [])])], null]
282
339
  : m.focus === "detail" ? [["detail", "close", "refresh"], ["open", "zoom", "close-terminal", "detail", "close", "refresh", ...ACTS]]
283
340
  // A kept filter shows its own `/` beside the hints: then `open` outlives the `/ filter` hint.
284
- : [["filter", "detail", ...(m.filter.query ? ["close"] : []), "refresh", "workspaces", ...VIEW_KEYS], [...(m.filter.query ? ["open", "zoom", "close-terminal", "filter"] : ["filter", "open", "zoom", "close-terminal"]), "detail", "close", "refresh", "workspaces", ...VIEW_KEYS, ...ACTS]];
341
+ : [["filter", "detail", ...(filtered ? ["close"] : []), "refresh", "workspaces", ...VIEW_KEYS], [...(filtered ? ["open", "zoom", "close-terminal", "filter"] : ["filter", "open", "zoom", "close-terminal"]), "detail", "close", "refresh", "workspaces", ...VIEW_KEYS, ...ACTS]];
285
342
  const hints = [...first, ...terminal, ...acts, ...ids.filter((id) => !(asking && id === "close")).map((id) => [id, id in hintOf ? hintOf[id] : hint(id)])].filter(([, h]) => h).map(([id, h]) => [id, spansOf(h)]);
286
- const last = ["help", "quit"].map((id) => hint(id)).filter(Boolean).map(spansOf);
287
- const spans = [];
288
- let col = MARGIN;
289
- if (m.view !== "help" && m.filter.query) {
343
+ // `help` and `quit` stand last, and always but in one case: when the two do not fit whole
344
+ // after the leader (keys tui.json made long), `help` is given up, then `quit`. The leader
345
+ // reaches the help too, and a hint is never cut.
346
+ let last = ["help", "quit"].map((id) => hint(id)).filter(Boolean).map(spansOf);
347
+ while (last.length && start + span(last) > edge) last = last.slice(1);
348
+ const spans = put(lead, FOOT);
349
+ let col = start;
350
+ if (m.view !== "help" && filtered) {
290
351
  // The kept filter has a place of its own width, so the hints stand where they always do;
291
352
  // it gives way, down to nothing, before `help` and `quit` do, and before the hint that
292
353
  // stands first while Enter would open an agent (or Esc gives an open up).
@@ -294,9 +355,12 @@ function footer(m, cols) {
294
355
  // cannot stand takes no room from the filter.
295
356
  const firstFits = first.length > 0 && col + first[0][1].width + GAP + span(last) <= edge;
296
357
  const held = firstFits ? first[0][1].width + GAP : 0;
297
- const slot = Math.min(FILTER_COLS - GAP, edge - MARGIN - span(last) - (last.length ? GAP : 0) - held);
298
- if (slot >= 2) {
299
- spans.push({ col, text: lit("/"), style: BOLD, width: 1 }, { col: col + 1, text: shown(m.filter.query), style: BOLD, width: slot - 1 });
358
+ const slot = Math.min(FILTER_COLS - GAP, edge - col - span(last) - (last.length ? GAP : 0) - held);
359
+ if (bySoul && slot >= SOUL_WORD.length + 2) {
360
+ spans.push({ col, text: lit(SOUL_WORD), style: BOLD, width: SOUL_WORD.length }, { col: col + SOUL_WORD.length + 1, text: shown(bySoul.name), style: BOLD, width: slot - SOUL_WORD.length - 1 });
361
+ col += slot + GAP;
362
+ } else if (!bySoul && slot >= 2) {
363
+ spans.push({ col, text: lit("/"), style: BOLD, width: 1 }, { col: col + 1, text: shown(filter.query), style: BOLD, width: slot - 1 });
300
364
  col += slot + GAP;
301
365
  }
302
366
  }
@@ -313,6 +377,60 @@ function footer(m, cols) {
313
377
  return spans;
314
378
  }
315
379
 
380
+ /** The footer's first column. */
381
+ const FOOT = 0;
382
+ /** The spawn prompt's hints after the leader (docs/tui.md, "Spawning an agent"): the keys the
383
+ * prompt reads are its own, as the palette's are, so they are the TUI's words. The picker: Enter
384
+ * picks, Up/Down move, Esc closes. The preview: Enter and what it does at the focus (the model's
385
+ * `enter`; none while Enter does nothing), Tab and Shift+Tab move the focus, Esc goes back. A hint
386
+ * that does not fit whole is given up, in an order of its own: in the preview Shift+Tab first,
387
+ * then Tab, then Esc, Enter last; in the picker from the end, as the palette's. */
388
+ function spawnHints(s, start, edge, put, own, ascii) {
389
+ const hints = s.step === "picker" ? [own("Enter", "pick"), own("Up/Down", "move"), own("Esc", "close")]
390
+ : [...(s.enter ? [own("Enter", ownEllipsis(s.enter, ascii))] : []), own("Tab", "next"), own("Shift+Tab", "back"), own("Esc", "back")];
391
+ // The least kept first: Shift+Tab, Tab, Esc, then Enter.
392
+ const order = s.step === "picker" ? [...hints].reverse() : [hints.at(-2), hints.at(-3), hints.at(-1), ...(s.enter ? [hints[0]] : [])];
393
+ let kept = hints;
394
+ const width = (list) => list.reduce((sum, h) => sum + h.width, 0) + Math.max(0, list.length - 1) * GAP;
395
+ for (const least of order) { if (start + width(kept) <= edge) break; kept = kept.filter((h) => h !== least); }
396
+ const spans = [];
397
+ let col = start;
398
+ for (const h of kept) { spans.push(...put(h, col)); col += h.width + GAP; }
399
+ return spans;
400
+ }
401
+ /** The leader's hint (docs/tui.md, "The leader"): the leader's key, then what each key after it
402
+ * does, from actions.mjs LEADER, the views, the instances and the acts each one hint. The keys
403
+ * that need a terminal are named only while one is open, the tabs' only with two or more, and the
404
+ * split's only where they apply.
405
+ * Hints are given up from the end until the rest fit, the last, the list's, never. */
406
+ function leaderHint(m, edge, put, own) {
407
+ const by = (does) => LEADER.find((entry) => entry.does === does);
408
+ const one = (does) => { const entry = by(does); return own(keyLabel(entry.keys[0]), entry.label); };
409
+ const keysOf = (list) => list.map((does) => keyLabel(by(does).keys[0])).join("/");
410
+ const hints = [
411
+ own(keyLabel(by("palette").keys[0]), "palette"),
412
+ own(keysOf(["instances", "souls", "capabilities"]) === "1/2/3" ? "1-3" : keysOf(["instances", "souls", "capabilities"]), "views"),
413
+ own(keysOf(["previous", "next"]), "instance"),
414
+ ...(m.terminal ? [one("zoom"), one("close-terminal")] : []),
415
+ // With two tabs or more, the keys that move between them.
416
+ ...((m.tabs?.length ?? 0) > 1 ? [own(keysOf(["tab-previous", "tab-next"]), "tab")] : []),
417
+ // The split's (docs/tui.md, "Splits"): to split while there is one group; with two, the next
418
+ // group, and the move of the shown tab, or the close of the focused group while it is empty.
419
+ ...(m.panelOpen && !m.split ? [own(keysOf(["split-right", "split-down"]), "split")] : []),
420
+ ...(m.split ? [one("group-next"), ...(m.terminal ? [one("tab-move")] : [one("group-close")])] : []),
421
+ own(keysOf(["stop", "restart", "start", "retire"]), "acts"),
422
+ one("filter"), one("workspaces"), one("help"),
423
+ ];
424
+ const last = one("list"), key = keyText(m.detachKey);
425
+ const width = (list) => list.reduce((sum, h) => sum + h.width + GAP, 0);
426
+ let kept = hints;
427
+ while (kept.length && key.width + GAP + width(kept) + last.width > edge) kept = kept.slice(0, -1);
428
+ const spans = [{ col: FOOT, text: key.text, style: BOLD, width: Math.min(key.width, edge) }];
429
+ let col = key.width + GAP;
430
+ for (const h of [...kept, last]) { if (col + h.width > edge) break; spans.push(...put(h, col)); col += h.width + GAP; }
431
+ return spans;
432
+ }
433
+
316
434
  /** Shift a pane's rows to its place on the screen. */
317
435
  const placed = (spans, col) => spans.map((span) => ({ ...span, col: span.col + col }));
318
436
 
@@ -320,38 +438,59 @@ const placed = (spans, col) => spans.map((span) => ({ ...span, col: span.col + c
320
438
  * pane (of the whole body, for the switcher); `extent` and `room` are how many rows the other
321
439
  * scrolling pane (the help, else the detail) holds and shows. The main loop hands the three back
322
440
  * with the next key, so the store scrolls by what is on the screen without holding a size. */
441
+ /** The frame for model `m` at `size`: its rows, the paging the store scrolls by, the cursor, and
442
+ * for the loop (lib/main.mjs fitPanel) `regions`, each group of the panel this frame draws, empty
443
+ * when it draws none (another view, an overlay, a screen too small). */
323
444
  export function frame(m, size) {
445
+ const made = framed(m, size);
446
+ return made.regions ? made : { ...made, regions: [] };
447
+ }
448
+ function framed(m, size) {
324
449
  if (tooSmall(size)) return { rows: tooSmallRows(size), page: 1, extent: 0, room: 1 };
325
450
  const rows = Array.from({ length: size.rows }, () => []);
326
- const g = layout(m.notice, size, { observed: m.observed, note: m.note, ascii: m.ascii, outcome: m.outcome });
451
+ const g = layout(m.notice, size, { observed: m.observed, note: m.note, ascii: m.ascii, outcome: m.outcome, terminal: panelOpen(m) });
327
452
  const { cols } = size;
328
453
  // The switcher is no view of the three: while it is shown, or is under the help, none is current.
329
454
  const listing = m.under === "switcher";
330
455
 
331
456
  const counts = countSpans(m.counts, cols);
332
- const nameCol = MARGIN + "oats".length + 2;
333
- rows[0] = [{ col: MARGIN, text: lit("oats"), style: BOLD }, ...nameSpans(m, nameCol, counts.start - 2 - nameCol), ...counts.spans];
457
+ // The header starts with the workspace's name: no title before it.
458
+ rows[0] = [...nameSpans(m, MARGIN, counts.start - 2 - MARGIN), ...counts.spans];
334
459
  let col = MARGIN;
335
460
  for (const view of VIEWS) {
336
- // The Instances view is the open one but while the switcher is shown: then none is.
337
- const look = !view.open ? DIM : listing ? null : REVERSE;
338
- if (look === REVERSE) rows[1].push({ col, width: view.name.length + 2, style: REVERSE });
339
- rows[1].push({ col: col + 1, text: lit(view.name), ...(look ? { style: look } : {}), width: view.name.length });
461
+ // The open view is in reverse; while the switcher is shown none is open.
462
+ const open = view.id === m.under;
463
+ if (open) rows[1].push({ col, width: view.name.length + 2, style: REVERSE });
464
+ rows[1].push({ col: col + 1, text: lit(view.name), ...(open ? { style: REVERSE } : {}), width: view.name.length });
340
465
  col += view.name.length + 2 + VIEW_GAP;
341
466
  }
342
467
  if (m.readAt) { const read = `read ${m.readAt}`; rows[1].push({ col: cols - MARGIN - read.length, text: lit(read), style: DIM, width: read.length }); }
343
468
  rows[size.rows - 1] = footer(m, cols);
344
469
 
470
+ // The palette over the whole body, whatever is under it.
471
+ if (m.palette) {
472
+ rows[2] = rule(cols, m.ascii);
473
+ const drawn = palette(m, { cols, rows: g.body.rows });
474
+ drawn.rows.forEach((spans, i) => { rows[g.body.top + i] = spans; });
475
+ return { rows, page: Math.max(1, g.body.rows), extent: 0, room: 1, cursor: { row: g.body.top, col: drawn.cursor } };
476
+ }
477
+ // The spawn prompt in the same place (the palette wins if both are ever set). `extent` and `room`
478
+ // are its preview's, which the store scrolls as it scrolls a confirmation; `armed` says the ready
479
+ // Spawn button is drawn, which the loop tells the store once the frame is written.
480
+ if (m.spawn) {
481
+ rows[2] = rule(cols, m.ascii);
482
+ const drawn = spawn(m, { cols, rows: g.body.rows });
483
+ drawn.rows.forEach((spans, i) => { rows[g.body.top + i] = spans; });
484
+ return { rows, page: Math.max(1, g.body.rows), extent: drawn.extent, room: drawn.room, cursor: drawn.cursor ? { row: g.body.top + drawn.cursor.row, col: drawn.cursor.col } : null, ...(drawn.armed ? { armed: true } : {}) };
485
+ }
345
486
  if (m.view === "act" && m.act) {
346
487
  rows[2] = rule(cols, m.ascii);
347
- rows[size.rows - 2] = rule(cols, m.ascii);
348
488
  const drawn = act(m, { cols, rows: g.body.rows });
349
489
  drawn.rows.forEach((spans, i) => { rows[g.body.top + i] = spans; });
350
490
  return { rows, page: Math.max(1, g.page), extent: drawn.extent, room: drawn.room };
351
491
  }
352
492
  if (m.view === "help") {
353
493
  rows[2] = rule(cols, m.ascii);
354
- rows[size.rows - 2] = rule(cols, m.ascii);
355
494
  const drawn = help(m.actions, { cols, rows: g.body.rows }, { top: m.helpTop, ascii: m.ascii, detachKey: m.detachKey });
356
495
  drawn.rows.forEach((spans, i) => { rows[g.body.top + i] = spans; });
357
496
  return { rows, page: Math.max(1, g.page), extent: drawn.extent, room: g.body.rows };
@@ -361,11 +500,18 @@ export function frame(m, size) {
361
500
  // Nothing beside it scrolls on its own.
362
501
  if (listing) {
363
502
  rows[2] = rule(cols, m.ascii);
364
- rows[size.rows - 2] = rule(cols, m.ascii);
365
503
  switcher(m, { cols, rows: g.body.rows }).forEach((spans, i) => { rows[g.body.top + i] = spans; });
366
504
  return { rows, page: Math.max(1, g.body.rows), extent: 0, room: 1 };
367
505
  }
368
506
 
507
+ // The view of this machine not in this version says what it will hold, and what to run until
508
+ // then. The Souls view is drawn below, in the panes of the list.
509
+ if (m.view === "capabilities") {
510
+ rows[2] = rule(cols, m.ascii);
511
+ partRows(m.body?.said ?? [], cols - 2 * MARGIN, g.body.rows, { ascii: m.ascii }).forEach((spans, i) => { rows[g.body.top + 1 + i] = placed(spans, MARGIN); });
512
+ return { rows, page: Math.max(1, g.body.rows), extent: 0, room: 1 };
513
+ }
514
+
369
515
  // The state of a deployment that is not live, and on rows of its own the reason, which is the
370
516
  // shared home's sentence or the kernel's: nothing of the TUI's stands on a reason's row. Then
371
517
  // the note of the one deployment of a view without headings, the shared home's sentence too.
@@ -378,6 +524,7 @@ export function frame(m, size) {
378
524
  for (const line of g.note) said.push([{ col: MARGIN, text: line, width: cols - 2 * MARGIN }]);
379
525
  for (const spans of g.outcome) said.push(placed(spans, MARGIN));
380
526
  said.slice(0, g.above).forEach((spans, i) => { rows[g.body.top + i] = spans; });
527
+ if (m.view === "souls" && m.souls) return soulsFrame(m, size, g, rows);
381
528
  const area = terminalAt(m, size, g);
382
529
  const zoomed = area !== null && area.whole;
383
530
  const paged = !zoomed && !g.wide && m.focus === "detail" && m.detail;
@@ -388,8 +535,7 @@ export function frame(m, size) {
388
535
  // The rule that opens the panes: the one under the header, or the one under the reason and the note.
389
536
  const opening = m.observed && g.above > 0 ? g.panes.top - 1 : 2;
390
537
  rows[2] = rule(cols, m.ascii, opening === 2 ? joint : null);
391
- if (opening !== 2 && opening < size.rows - 2) rows[opening] = rule(cols, m.ascii, joint);
392
- rows[size.rows - 2] = rule(cols, m.ascii, joint && { ...joint, up: true });
538
+ if (opening !== 2 && opening < size.rows - 1) rows[opening] = rule(cols, m.ascii, joint);
393
539
 
394
540
  const pane = { cols: g.wide ? g.listCols : cols, rows: g.panes.rows };
395
541
  let extent = 0;
@@ -397,56 +543,209 @@ export function frame(m, size) {
397
543
  const bar = split ? [{ col: g.listCols, text: lit(m.ascii ? "|" : "│"), style: DIM, width: 1 }] : [];
398
544
  if (area !== null) {
399
545
  for (let i = 0; i < g.panes.rows; i++) rows[g.panes.top + i] = [...rows[g.panes.top + i], ...bar];
400
- terminalRows(m, area).forEach((spans, i) => { rows[area.title + i] = [...rows[area.title + i], ...spans]; });
546
+ for (const region of area.regions) regionRows(m, region).forEach((spans, i) => { rows[region.title + i] = [...rows[region.title + i], ...spans]; });
547
+ // The rule between two groups: a column side by side, a row one above the other.
548
+ const p = area.panel, r = area.rule;
549
+ if (r?.col !== undefined) for (let i = 0; i <= p.rows; i++) rows[p.title + i] = [...rows[p.title + i], { col: r.col, text: lit(m.ascii ? "|" : "\u2502"), style: DIM, width: 1 }];
550
+ if (r?.row !== undefined) rows[r.row] = [...rows[r.row], { col: p.col, text: lit((m.ascii ? "-" : "\u2500").repeat(p.cols)), style: DIM, width: p.cols }];
401
551
  } else if (split || paged) {
402
552
  const at = paged ? MARGIN : g.listCols + 2;
403
553
  const drawn = detail(m, { cols: Math.max(0, cols - at - MARGIN), rows: g.panes.rows });
404
554
  extent = drawn.extent;
405
555
  drawn.rows.forEach((spans, i) => { rows[g.panes.top + i] = [...rows[g.panes.top + i], ...bar, ...placed(spans, at)]; });
406
556
  }
407
- // The agent's cursor, where the agent has it, while what is typed goes to the agent.
408
- const agent = area !== null && m.terminal.focus === "terminal" ? m.screen?.cursor : null;
409
- const cursor = agent?.visible && agent.y < area.rows && agent.x < area.cols ? { row: area.top + agent.y, col: area.col + agent.x } : null;
410
- return { rows, page: Math.max(1, g.page), extent, room: Math.max(1, g.panes.rows), terminal: area, cursor };
557
+ // The focused group's terminal, when it shows one: the agent's cursor, where the agent has it,
558
+ // while what is typed goes to the agent.
559
+ const shownArea = area !== null && area.id !== null && m.terminal ? { title: area.title, top: area.top, col: area.col, cols: area.cols, rows: area.rows, whole: area.whole } : null;
560
+ const agent = shownArea !== null && m.terminal.focus === "terminal" ? m.screen?.cursor : null;
561
+ const cursor = agent?.visible && agent.y < shownArea.rows && agent.x < shownArea.cols ? { row: shownArea.top + agent.y, col: shownArea.col + agent.x } : null;
562
+ // The regions the loop gives the panel (lib/main.mjs): each group drawn, its shown tab's id and
563
+ // its size; and the panel's room as the terminal's focus would give it, for a split's size.
564
+ const regions = area === null ? [] : area.regions.map(({ group, id, focused, cols: c, rows: r }) => ({ group, id, focused, cols: c, rows: r }));
565
+ return { rows, page: Math.max(1, g.page), extent, room: Math.max(1, g.panes.rows), terminal: shownArea, cursor, regions };
566
+ }
567
+
568
+ /** The Souls view in the panes of the list (views/souls.mjs), under the header and what stands
569
+ * above the panes: the souls in the list's place and width, and beside them, past the panes'
570
+ * rule, the selected soul's detail; under WIDE columns the list alone, and the detail in its
571
+ * place while it has the focus. A body of one sentence (reading, a failed first read, an empty
572
+ * catalog, no workspace of this machine) has the whole width, with no pane beside it. */
573
+ function soulsFrame(m, size, g, rows) {
574
+ const { cols } = size, s = m.souls;
575
+ const paged = !g.wide && s.focus === "detail" && Boolean(s.detail);
576
+ const split = g.wide && g.panes.rows > 0 && soulsListed(s);
577
+ const joint = split ? { col: g.listCols } : null;
578
+ const opening = m.observed && g.above > 0 ? g.panes.top - 1 : 2;
579
+ rows[2] = rule(cols, m.ascii, opening === 2 ? joint : null);
580
+ if (opening !== 2 && opening < size.rows - 1) rows[opening] = rule(cols, m.ascii, joint);
581
+ if (!paged) soulsList(m, { cols: split ? g.listCols : cols, rows: g.panes.rows }).forEach((spans, i) => { rows[g.panes.top + i] = spans; });
582
+ let extent = 0;
583
+ if (split || paged) {
584
+ const at = paged ? MARGIN : g.listCols + 2;
585
+ const bar = split ? [{ col: g.listCols, text: lit(m.ascii ? "|" : "│"), style: DIM, width: 1 }] : [];
586
+ const drawn = soulDetail(m, { cols: Math.max(0, cols - at - MARGIN), rows: g.panes.rows });
587
+ extent = drawn.extent;
588
+ drawn.rows.forEach((spans, i) => { rows[g.panes.top + i] = [...rows[g.panes.top + i], ...bar, ...placed(spans, at)]; });
589
+ }
590
+ return { rows, page: Math.max(1, g.page), extent, room: Math.max(1, g.panes.rows) };
411
591
  }
412
592
 
413
593
  /** Where the open terminal stands for a model and a size, or null while it is not shown (no
414
594
  * terminal; another view over the body; the detail in its place; the list's focus on a terminal
415
- * too narrow for it beside the list). `{ title, top, col, cols, rows, whole }`: `title` is the row
416
- * that names the agent, and the agent's screen is `rows` rows of `cols` cells from (`top`, `col`)
417
- * (the size the panel gives the viewer). With the terminal's focus it has the whole body when it is
595
+ * too narrow for it beside the list). `{ title, top, col, cols, rows, whole }`, the focused
596
+ * group's: `title` is the row that names the agent, and the agent's screen is `rows` rows of
597
+ * `cols` cells from (`top`, `col`) (the size the panel gives the viewer); with `panel` the whole
598
+ * panel's place, `regions` each group drawn (`group`, `id` its shown tab's or null, `focused`),
599
+ * and `rule` between two (views/split.mjs). With the terminal's focus it has the whole body when it is
418
600
  * zoomed, under WIDE columns, and when the side beside the list would be under PANEL_MIN cells
419
601
  * (`whole`); else it stands beside the list, edge to edge, in the detail's place. */
420
602
  export function terminalArea(m, size) {
421
603
  if (tooSmall(size)) return null;
422
- return terminalAt(m, size, layout(m.notice, size, { observed: m.observed, note: m.note, ascii: m.ascii, outcome: m.outcome }));
604
+ return terminalAt(m, size, layout(m.notice, size, { observed: m.observed, note: m.note, ascii: m.ascii, outcome: m.outcome, terminal: panelOpen(m) }));
605
+ }
606
+ /** Where the panel stands as the Instances view gives it with the terminal's focus, whatever this
607
+ * frame draws (an overlay, another view, the list's focus): `{ panel, focused }`, the panel's
608
+ * room with its strip row counted (what a split is measured against: lib/store.mjs
609
+ * splitRefused) and the focused group's screen (`{ cols, rows }`, what a viewer opened now starts
610
+ * at), or null on a screen too small. */
611
+ export function panelPlace(m, size) {
612
+ if (tooSmall(size)) return null;
613
+ const g = layout(m.notice, size, { observed: m.observed, note: m.note, ascii: m.ascii, outcome: m.outcome, terminal: true });
614
+ const area = terminalAt({ ...m, view: "instances", under: "instances", palette: null, spawn: null, panelOpen: true, terminal: { ...(m.terminal ?? {}), focus: "terminal" } }, size, g);
615
+ return { panel: panelRoom(size, g), focused: area === null ? null : { cols: area.cols, rows: area.rows } };
616
+ }
617
+ /** Whether the panel stands: a tab is open (a model made by hand may name only its terminal). */
618
+ const panelOpen = (m) => Boolean(m.panelOpen) || Boolean(m.terminal);
619
+ /** The panel's groups, or one made of the terminal for a model that names no groups. */
620
+ const groupsIn = (m) => m.groups ?? [{ tabs: m.tabs ?? [], shown: m.terminal?.id ?? null }];
621
+ /** The panel's size as the terminal's focus would give it, its strip row counted: what a split
622
+ * is measured against (lib/store.mjs splitRefused). */
623
+ function panelRoom(size, g) {
624
+ const side = size.cols - g.listCols - 1;
625
+ return { cols: !g.wide || side < PANEL_MIN ? size.cols : side, rows: g.panes.rows };
423
626
  }
424
627
  function terminalAt(m, size, g) {
425
628
  const t = m.terminal;
426
- if (!t || m.view !== "instances" || m.under === "switcher" || g.panes.rows < 2) return null;
629
+ if (!panelOpen(m) || m.view !== "instances" || m.under === "switcher" || m.palette || m.spawn || g.panes.rows < 2) return null;
630
+ const focus = t?.focus ?? "list", zoom = Boolean(t?.zoom);
427
631
  const side = size.cols - g.listCols - 1;
428
632
  const narrow = !g.wide || side < PANEL_MIN;
429
- const whole = t.focus === "terminal" && (t.zoom || narrow);
633
+ const whole = focus === "terminal" && (zoom || narrow);
430
634
  // From the list's focus it stands beside the list, unless the detail stands there or there is no room.
431
- if (t.focus !== "terminal" && (m.focus === "detail" || narrow)) return null;
635
+ if (focus !== "terminal" && (m.focus === "detail" || narrow)) return null;
432
636
  const col = whole ? 0 : g.listCols + 1;
433
- return { title: g.panes.top, top: g.panes.top + 1, col, cols: whole ? size.cols : side, rows: g.panes.rows - 1, whole };
637
+ const panel = { title: g.panes.top, top: g.panes.top + 1, col, cols: whole ? size.cols : side, rows: g.panes.rows - 1 };
638
+ // Both groups while the panel holds them and nothing is zoomed; else the focused one alone, the
639
+ // other kept (Settled 3 of T3).
640
+ const groups = groupsIn(m), focused = m.group ?? 0;
641
+ const both = Boolean(m.split) && !zoom && groups.length === 2 && splitFits(m.split, { cols: panel.cols, rows: panel.rows + 1 });
642
+ const [first, second, rule] = both ? splitRegions(panel, m.split) : [panel, null, null];
643
+ const regions = (both ? [{ ...first, group: 0 }, { ...second, group: 1 }] : [{ ...panel, group: focused }])
644
+ .map((r) => ({ ...r, id: groups[r.group]?.shown ?? null, focused: r.group === focused, whole }));
645
+ return { ...regions.find((r) => r.focused), panel, regions, rule };
646
+ }
647
+
648
+ /** A hidden tab's mark for output since it was last shown (docs/tui.md, "Tabs"). */
649
+ const ACTIVITY = "\u2022";
650
+
651
+ /** The tab strip (docs/tui.md, "Tabs"), the panel's top row with two tabs or more: each tab in
652
+ * order as ` name ` (and ` on machine`, dim, for another machine's agent), the shown one bold, in
653
+ * reverse while what is typed goes to it, a hidden one plain with `•` after its name when its
654
+ * agent wrote since it was shown. What does not fit is cut in this order: the hidden tabs'
655
+ * machines, then their names, each to an equal share of what the shown tab leaves with `…`, then
656
+ * the shown tab's machine, then the hidden tabs farthest from the shown one are left out. The
657
+ * shown tab's name stays whole while the row can hold it. */
658
+ function stripRow(given, area, { focused, ascii }) {
659
+ const mark = ascii ? "*" : ACTIVITY;
660
+ const tabs = given.map((tab) => ({ ...tab, name: measured(tab.name), machine: tab.machine === null ? null : measured(tab.machine), mark: !tab.shown && tab.activity }));
661
+ const machineCells = (tab) => (tab.machine ? 1 + ON.length + 1 + tab.machine.width : 0);
662
+ const cellsOf = (tab, { machine, name = tab.name.width, dropped = false }) => (dropped ? 0 : 1 + name + (tab.mark ? 1 : 0) + (machine ? machineCells(tab) : 0) + 1);
663
+ const total = (plan) => tabs.reduce((sum, tab, i) => sum + cellsOf(tab, plan[i]), 0);
664
+ const plan = tabs.map((tab) => ({ machine: true, name: tab.name.width, dropped: false }));
665
+ const fits = () => total(plan) <= area.cols;
666
+ const shown = tabs.findIndex((tab) => tab.shown);
667
+ // The least a hidden tab is drawn in: a cell of its name (the cut mark), its mark, a cell each side.
668
+ const least = (tab) => 3 + (tab.mark ? 1 : 0);
669
+ // The shown tab's cells are taken first; the hidden ones share the rest equally. Those that do
670
+ // not get even their least are left out, the farthest from the shown tab first.
671
+ const share = () => {
672
+ let left = area.cols - cellsOf(tabs[shown], plan[shown]);
673
+ const near = tabs.map((tab, i) => i).filter((i) => i !== shown).sort((a, b) => Math.abs(a - shown) - Math.abs(b - shown) || a - b);
674
+ const kept = [];
675
+ for (const i of near) { plan[i].dropped = left - least(tabs[i]) < 0; if (!plan[i].dropped) { kept.push(i); left -= least(tabs[i]); } }
676
+ const room = area.cols - cellsOf(tabs[shown], plan[shown]), each = Math.floor(room / Math.max(1, kept.length));
677
+ for (const i of kept) plan[i].name = Math.max(1, Math.min(tabs[i].name.width, each - 2 - (tabs[i].mark ? 1 : 0)));
678
+ while (!fits() && kept.length) plan[kept.pop()].dropped = true;
679
+ };
680
+ if (!fits()) tabs.forEach((tab, i) => { if (i !== shown) plan[i].machine = false; });
681
+ if (!fits()) share();
682
+ if (plan.some((p) => p.dropped) && plan[shown].machine && tabs[shown].machine) { plan[shown].machine = false; plan.forEach((p) => { p.dropped = false; }); share(); }
683
+ const row = [];
684
+ let col = area.col;
685
+ const end = area.col + area.cols;
686
+ tabs.forEach((tab, i) => {
687
+ if (col >= end || plan[i].dropped) return;
688
+ const p = plan[i], width = Math.min(cellsOf(tab, p), end - col);
689
+ const style = tab.shown ? (focused ? FOCUSED : BOLD) : null;
690
+ const name = p.name < tab.name.width ? shownLines(textOf(tab.name.text), p.name, 1, { ascii })[0] ?? lit("") : tab.name.text;
691
+ const nameWidth = Math.min(p.name, textWidth(textOf(name)), end - col - 1);
692
+ // In reverse, as T1's title: the name with a cell on each side; its machine stays dim.
693
+ if (tab.shown && focused) row.push({ col, width: Math.min(nameWidth + 2, end - col), style: FOCUSED });
694
+ if (nameWidth > 0) row.push({ col: col + 1, text: name, width: nameWidth, ...(style ? { style } : {}) });
695
+ let at = col + 1 + nameWidth;
696
+ if (tab.mark && at < end) { row.push({ col: at, text: lit(mark), width: 1 }); at += 1; }
697
+ if (p.machine && tab.machine && at + 1 + ON.length + 1 < end) {
698
+ row.push({ col: at + 1, text: lit(ON), style: DIM, width: ON.length });
699
+ row.push({ col: at + 2 + ON.length, text: tab.machine.text, style: DIM, width: Math.max(0, Math.min(tab.machine.width, end - (at + 2 + ON.length))) });
700
+ }
701
+ col += width;
702
+ });
703
+ return row;
434
704
  }
435
705
 
436
706
  /** The open terminal's rows from its title row: the agent's name (and its machine, when it is
437
- * another's), in reverse while what is typed goes to it, then the agent's screen as the last
438
- * capture made it (lib/panel.mjs, term/capture.mjs: spans of the agent's own text and styles), each
439
- * row placed at the terminal's first column and kept inside its cells. */
440
- function terminalRows(m, area) {
441
- const t = m.terminal, focused = t.focus === "terminal";
442
- const name = measured(t.name), title = [];
443
- if (focused) title.push({ col: area.col, width: Math.min(area.cols, name.width + 2), style: FOCUSED });
444
- title.push({ col: area.col + 1, text: name.text, style: focused ? FOCUSED : BOLD, width: Math.max(0, Math.min(name.width, area.cols - 2)) });
445
- if (t.routed && t.machine !== null) {
446
- const where = measured(t.machine), at = area.col + name.width + 3;
447
- if (at + ON.length + 1 < area.col + area.cols) title.push({ col: at, text: lit(ON), style: DIM, width: ON.length }, { col: at + ON.length + 1, text: where.text, style: DIM, width: Math.max(0, area.col + area.cols - (at + ON.length + 1)) });
707
+ * another's), in reverse while what is typed goes to it (with two tabs or more, the tab strip:
708
+ * stripRow), then the shown agent's screen as the last capture made it (lib/panel.mjs,
709
+ * term/capture.mjs: spans of the agent's own text and styles), each row placed at the terminal's
710
+ * first column and kept inside its cells. */
711
+ /** What an empty group shows (docs/tui.md, "Splits"), under an empty strip row: that Enter on an
712
+ * agent in the list opens it here, in reverse while it is the focused group, then the leader's
713
+ * keys for the groups. */
714
+ const EMPTY_HINT = "Enter on an agent opens it here";
715
+ function emptyRows(m, area) {
716
+ const end = area.col + area.cols, at = area.col + 1;
717
+ const hint = [{ col: at, text: lit(EMPTY_HINT), width: Math.max(0, Math.min(EMPTY_HINT.length, end - at - 1)), ...(area.focused ? { style: FOCUSED } : {}) }];
718
+ const leader = keyText(m.detachKey), keys = [];
719
+ let col = at;
720
+ for (const does of ["group-next", "group-close"]) {
721
+ const entry = LEADER.find((each) => each.does === does), key = keyLabel(entry.keys[0]), word = entry.label;
722
+ const after = col + leader.width + 1, at2 = after + key.length + 1;
723
+ if (at2 + word.length > end - 1) break;
724
+ keys.push({ col, text: leader.text, style: BOLD, width: leader.width }, { col: after, text: lit(key), style: BOLD, width: key.length }, { col: at2, text: lit(word), style: DIM, width: word.length });
725
+ col = at2 + word.length + GAP;
726
+ }
727
+ return [[], hint, keys];
728
+ }
729
+ function regionRows(m, area) {
730
+ const group = groupsIn(m)[area.group] ?? { tabs: [], shown: null };
731
+ if (group.shown === null) return emptyRows(m, area);
732
+ // The focused group's shown tab is in reverse while what is typed goes to it; the other group's
733
+ // is bold, never in reverse, so one terminal at a time reads as focused (T3).
734
+ const focused = area.focused && m.terminal?.focus === "terminal";
735
+ const tab = group.tabs.find((each) => each.shown) ?? { name: m.terminal?.name, machine: m.terminal?.routed ? m.terminal.machine : null };
736
+ const name = measured(tab.name);
737
+ let title = [];
738
+ if (group.tabs.length > 1) title = stripRow(group.tabs, area, { focused, ascii: m.ascii });
739
+ else {
740
+ if (focused) title.push({ col: area.col, width: Math.min(area.cols, name.width + 2), style: FOCUSED });
741
+ title.push({ col: area.col + 1, text: name.text, style: focused ? FOCUSED : BOLD, width: Math.max(0, Math.min(name.width, area.cols - 2)) });
742
+ if (tab.machine) {
743
+ const where = measured(tab.machine), at = area.col + name.width + 3;
744
+ if (at + ON.length + 1 < area.col + area.cols) title.push({ col: at, text: lit(ON), style: DIM, width: ON.length }, { col: at + ON.length + 1, text: where.text, style: DIM, width: Math.max(0, area.col + area.cols - (at + ON.length + 1)) });
745
+ }
448
746
  }
449
- const lines = m.screen?.lines ?? [];
747
+ const screenOf = area.focused ? m.screen : m.screens?.get(group.shown) ?? null;
748
+ const lines = screenOf?.lines ?? [];
450
749
  const body = Array.from({ length: area.rows }, (_, r) => (lines[r] ?? [])
451
750
  .filter((span) => span.col < area.cols)
452
751
  .map((span) => ({ ...span, col: area.col + span.col, width: Math.min(span.width ?? area.cols, area.cols - span.col) })));